Skip to content

feat: ONNX export and torch-free runtime for the tube classifier - #76

Merged
MateoLostanlen merged 12 commits into
mainfrom
feat/onnx-export
Sep 10, 2026
Merged

MateoLostanlen merged 12 commits into
mainfrom
feat/onnx-export

Conversation

@MateoLostanlen

Copy link
Copy Markdown
Member
  • Edge devices already run their own YOLO and cannot afford torch, timm and ultralytics. This adds model_onnx.zip, a torch-free runtime artifact published next to model.zip under the same HF tag.
  • predict() moves into a backend-agnostic TubePipelineModel; BboxTubeTemporalModel keeps torch and YOLO, the new OnnxTemporalModel runs on onnxruntime with supplied detections.
  • temporal-export-onnx derives the archive with fixed shapes, checks torch/ONNX parity on several mask patterns and records the source hash and I/O contract in its manifest. release publish --onnx-file and fetch --onnx ship it.
  • Core dependencies split into [torch] and [onnx] extras; the base package is numpy, pillow, pydantic and pyyaml. Verified on a Pi 5: same logits as torch to 1e-5, 161 MB install.

…backend

Move predict() into TubePipelineModel, keep torch only in the
BboxTubeTemporalModel scoring and detection overrides. Patches are
built in numpy, the trigger search takes a numpy score callable, and
StageTimer only imports torch for accelerator syncs.
temporal-export-onnx derives model_onnx.zip (fixed-shape classifier,
config, calibrator, source sha256 and I/O contract) from a model.zip and
checks torch/onnxruntime parity on several mask patterns.
OnnxTemporalModel runs the shared pipeline on supplied detections
without importing torch.
The base package is now numpy/pillow/pydantic/pyyaml only. Training,
packaging and the torch backend use [torch]; the onnxruntime backend
uses [onnx]. Dependent packages pin temporal-model-core[torch]; the core
dev group installs both extras for the test suite.
publish --onnx-file uploads the exported archive under the same HF tag,
fetch --onnx downloads it. Add make export-onnx / fetch-model-onnx and
document the [torch] / [onnx] extras and the ONNX runtime usage.
torch 2.12's dynamo exporter needs onnxscript >= 0.7 (onnx-ir with
schemas); the export's parity check needs onnxruntime, so the [torch]
extra carries it. numpy < 2 has no 3.13 wheels, and newer onnxscript
made the universal lock fail on that split, so say so in requires-python.
Stamping the version rewrites model.zip, so the SHA-256 recorded at
export time no longer identified the released archive.
…model.zip

Compare the manifest's recorded source hash with the input model.zip
before re-stamping it from the version-stamped copy.
…e runbook

torch.onnx.export(dynamo=True, external_data=False) needs 2.5. The
runbook chained cd commands that ended up in core/api.
- export_classifier/verify_export work on deep copies: the caller's module
  is never moved to CPU or de-fused, and parity is now measured against the
  as-served (fused-attention) torch path, not the export-friendly variant.
  This also fixes the order-dependent module-scoped fixture in test_onnx.py.
- Export with a dynamic batch axis (batch-2 example input so the tracer
  cannot fold the size-1 batch dim): OnnxTemporalModel._score is one
  session.run for all tubes instead of a per-tube Python loop, and
  verify_export adds a batched parity pass.
- build_onnx_package refuses an uncalibrated model.zip by default
  (--allow-uncalibrated to override), instead of shipping an artifact that
  only fails on the edge device after an immutable tag is cut.
- Re-export UncalibratedModelError from onnx_model so [onnx]-only installs
  have a torch-free import path for it.
- StageTimer validates/normalizes the device via torch.device when torch is
  installed (a typo like "gpu" raises instead of silently disabling sync).
- load_model_package(with_detector=False) no longer extracts YOLO weights.
- release.py: reuse core's ONNX_MODEL_FILENAME, stream SHA-256 with
  hashlib.file_digest, stamp the staged ONNX manifest in one rewrite.
- torch floor to >= 2.6 (external_data kwarg); torchvision declared in
  core's dev group for the parity test; providers=None vs [] fixed.
- docs: single-publish release runbook (the tag is immutable, a second
  publish cannot add the ONNX archive), drop stale score_tubes references,
  add docs/specs/2026-09-09-onnx-export-design.md referenced from the new
  module docstrings.
@MateoLostanlen
MateoLostanlen merged commit fb7f6ca into main Sep 10, 2026
8 checks passed
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