Skip to content

Add an ONNX export script#17

Open
AlJohri wants to merge 1 commit into
basnetsoyuj:masterfrom
AlJohri:add-onnx-export-script
Open

Add an ONNX export script#17
AlJohri wants to merge 1 commit into
basnetsoyuj:masterfrom
AlJohri:add-onnx-export-script

Conversation

@AlJohri

@AlJohri AlJohri commented Jun 6, 2026

Copy link
Copy Markdown

What

Adds src/deep_impact/scripts/export_onnx.py, a small utility that exports a trained DeepImpact checkpoint to a single-file ONNX model and verifies it against PyTorch:

python -m src.deep_impact.scripts.export_onnx \
    --model_checkpoint_path soyuj/deeper-impact \
    --output_path onnx/model.onnx

Also adds onnx and onnxruntime to requirements.txt (used only by this script).

Why

There's currently no ONNX export of DeepImpact, which makes it awkward to run inference outside Python (ONNX Runtime from Rust/C++/JS, etc.).

optimum-cli export onnx can't produce a correct export here: DeepImpact sets architectures: ["DeepImpact"] but isn't registered with transformers' AutoModel*, so optimum falls back to a plain BertModel (via model_type: "bert") and silently drops the impact_score_encoder head — emitting hidden states instead of impact scores, with no error. This script sidesteps that by tracing a thin wrapper around the real module with torch.onnx.export.

What gets exported

name dtype shape
inputs input_ids, attention_mask, token_type_ids int64 [batch, seq]
output impact_scores float32 [batch, seq]

Batch and sequence axes are dynamic (one model serves any batch size / length up to 512). A term's impact is the score at its first subword token — the same indexing DeepImpact.compute_term_impacts already does.

Parity

After export, the script runs a built-in check: it feeds a padded batch of two documents through both PyTorch and ONNX Runtime and asserts the per-term scores agree.

Wrote onnx/model.onnx (435.8 MB); onnx.checker passed
Parity vs PyTorch over 2 documents, 45 terms: max |diff| = 5.72e-06 (tolerance 0.0001)
Parity check passed

Verified on the repo's pinned torch==2.0.1 (Python 3.11). The output is a single self-contained model.onnx (~436 MB, no external-data sidecar).

Companion PR

A matching PR on the Hugging Face repo uploads the exported model.onnx (to onnx/model.onnx) and adds an ONNX section to the model card: https://huggingface.co/soyuj/deeper-impact/discussions/1

Add src/deep_impact/scripts/export_onnx.py to export a trained DeepImpact
checkpoint to a single-file ONNX model, with a built-in PyTorch-vs-ONNX
Runtime parity check over a padded batch of documents.

optimum-cli can't be used because DeepImpact isn't registered with
transformers' AutoModel machinery -- it falls back to a plain BertModel and
silently drops the impact-score head -- so this exports a thin wrapper via
torch.onnx.export directly.

Adds onnx and onnxruntime to requirements.txt; both are used only by this
script. Verified on the repo's pinned torch 2.0.1 (single-file export, parity
max|diff| 5.7e-06).
@AlJohri AlJohri marked this pull request as ready for review June 6, 2026 21:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant