Skip to content

feat: export classifier and regressor pipelines to ONNX - #346

Merged
stephantul merged 2 commits into
MinishLab:mainfrom
serhiizghama:feat/export-classifier-to-onnx
Aug 19, 2026
Merged

feat: export classifier and regressor pipelines to ONNX#346
stephantul merged 2 commits into
MinishLab:mainfrom
serhiizghama:feat/export-classifier-to-onnx

Conversation

@serhiizghama

Copy link
Copy Markdown
Contributor

Closes #307.

scripts/export_to_onnx.py could only export the encoder (StaticModel) — there was no way to get a trained StaticModelForClassification/Regression pipeline into ONNX, which is what the issue asks for. This teaches the same script to handle both: it auto-detects whether the given path is a plain encoder or a pipeline with a head, and exports accordingly, so the existing --model_path/--save_path interface is unchanged.

The classifier path wraps the encoder plus the MLP head into one torch.nn.Module. The head's Layers store weights as [out, in] and compute x @ weight.T + bias, which is exactly nn.Linear(in, out), so rebuilding them is a direct copy. The output activation matches the head: softmax/sigmoid produce probabilities, an identity head (regressor/projector) produces raw predictions, and the ONNX output is named accordingly.

Added tests that export a trained pipeline and run it through onnxruntime, checking the result against predict_proba for classifiers and predict for the identity head — single-label, multilabel, and projector cases all match within 1e-4. Existing inference tests still pass, and ruff/mypy are clean.

@stephantul

Copy link
Copy Markdown
Contributor

Hey this is cool, thanks for making it. We were about to add this ourselves, but nice you got to it first. I'll check it out ASAP

@codecov

codecov Bot commented Aug 19, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@stephantul
stephantul merged commit e532088 into MinishLab:main Aug 19, 2026
11 checks passed
@stephantul

Copy link
Copy Markdown
Contributor

Thanks for adding this! I will make some changes to the exporting code in parallel, so this is fine to merge.

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.

export model2vec classifier to onnx

2 participants