API Reference: Export & Edge Optimization¶
Graph rewriting, compile safety validation, and multi-target model exporter engines.
CoreExporter¶
corecv.engine.exporter.CoreExporter(model, target='onnx', opset_version=17, target_hardware='server', input_shape=(1, 3, 640, 640), dynamic_axes=None, output_dir='./exports')
¶
End-to-end model export pipeline for CoreCV.
Orchestrates three stages:
- Rewrite – deep-copies the model and applies
:class:
~corecv.engine.rewriter.TargetRewritertransforms whentarget_hardware='edge'(GELU -> ReLU, SiLU -> Hardswish, LayerNorm permutation collapse). - Validate – runs :class:
~corecv.engine.validator.MetaProberwithdevice='meta'zero-VRAM shape auditing and static-graph dynamic-operation detection. - Export – serialises to ONNX (
.onnx) and/or ExecuTorch (.pte) usingtorch.export/torch.onnx.export.
The exporter handles all three CoreCV task types:
- Classification – single tensor
(B, C)output. - Segmentation – single tensor
(B, C, H, W)or(B, H, W). - Detection –
dictwith lists of per-level tensors.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
Module
|
The CoreCV model to export. Can be any |
required |
target
|
str
|
Export target. One of |
'onnx'
|
opset_version
|
int
|
ONNX opset version. Must be |
17
|
target_hardware
|
str
|
Hardware target profile. |
'server'
|
input_shape
|
tuple[int, ...]
|
Input tensor shape |
(1, 3, 640, 640)
|
dynamic_axes
|
dict[str, dict[int, str]] | None
|
ONNX-style dynamic axes dictionary, e.g.
|
None
|
output_dir
|
str | Path
|
Directory for exported model files.
Defaults to |
'./exports'
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Initialise the CoreExporter.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
Module
|
The CoreCV model to export. |
required |
target
|
str
|
Export target ( |
'onnx'
|
opset_version
|
int
|
ONNX opset version ( |
17
|
target_hardware
|
str
|
Hardware target ( |
'server'
|
input_shape
|
tuple[int, ...]
|
Input tensor shape |
(1, 3, 640, 640)
|
dynamic_axes
|
dict[str, dict[int, str]] | None
|
ONNX dynamic axes dict, or |
None
|
output_dir
|
str | Path
|
Output directory for exported files. |
'./exports'
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If any argument is invalid. |
Source code in src/corecv/engine/exporter.py
export_executorch(model, output_path)
¶
Export the model to ExecuTorch format (.pte).
Uses torch.export.export() to obtain an
:class:torch.export.ExportedProgram and saves it with
torch.export.save().
Optionally applies XNNPACK delegate optimisation when the
executorch package is installed.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
Module
|
The (rewritten) model to export. Must be in eval mode. |
required |
output_path
|
str
|
Full path for the output |
required |
Returns:
| Type | Description |
|---|---|
str
|
The absolute path to the exported |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If |
Source code in src/corecv/engine/exporter.py
export_onnx(model, output_path)
¶
Export the model to ONNX format.
Uses the modern torch.export.export path when available
(PyTorch >= 2.3), which produces cleaner ONNX graphs with better
dynamic-shape support. Falls back to torch.onnx.export with
FX tracing when the modern path is unavailable.
Detection models returning dict outputs are automatically
wrapped with :class:_ONNXCompatModel to flatten outputs into a
tuple of tensors.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
Module
|
The (rewritten) model to export. Must be in eval mode. |
required |
output_path
|
str
|
Full path for the output |
required |
Returns:
| Type | Description |
|---|---|
str
|
The absolute path to the exported |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If ONNX export fails. |
Source code in src/corecv/engine/exporter.py
654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 | |
rewrite_model()
¶
Apply hardware-specific rewrites on a copy of the model.
When target_hardware == 'edge', the copy is traced through
:class:~corecv.engine.rewriter.TargetRewriter which:
- Replaces
nn.GELU/F.geluwithnn.ReLU. - Replaces
nn.SiLU/F.siluwithnn.Hardswish. - Collapses redundant NCHW <-> NHWC permutations around
LayerNorm.
The original model passed to the constructor is never mutated.
Returns:
| Type | Description |
|---|---|
Module
|
A rewritten copy of the model ( |
Module
|
|
Module
|
deep copy). |
Source code in src/corecv/engine/exporter.py
run_export()
¶
Run the full export pipeline: rewrite -> validate -> export.
Convenience method that chains all three stages:
- :meth:
rewrite_model– deep-copy and apply edge rewrites. - :meth:
validate_compatibility– zero-VRAM MetaProber audit. - :meth:
export_onnxand/or :meth:export_executorchdepending on thetargetsetting.
Exported files are placed in self.output_dir with filenames
following the pattern {model_class}_{timestamp}.{ext}.
Returns:
| Type | Description |
|---|---|
dict[str, str]
|
A dictionary mapping format names to file paths: |
dict[str, str]
|
|
dict[str, str]
|
|
dict[str, str]
|
|
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If validation fails or any export step errors. |
ValueError
|
If |
Source code in src/corecv/engine/exporter.py
validate_compatibility(model)
¶
Validate model compatibility for the target hardware.
Runs :class:~corecv.engine.validator.MetaProber with
device='meta' for zero-VRAM shape propagation and static-graph
auditing.
The validation strategy is:
- Component-aware – If the passed model exposes
backbone,neck, andheadattributes that satisfy theBaseBackboneinterface, the full MetaProber pipeline is used. - Original-model fallback – If FX tracing has erased submodule
type information (common after :mod:
torch.fxrewriting), the original model passed to the constructor is used for component validation instead. - Shape-only fallback – If neither approach works, a simplified meta-device forward pass is performed to at least verify that shapes are compatible.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
Module
|
The (possibly rewritten) model to validate. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
A |
ValidationResult
|
class: |
ValidationResult
|
descriptive details or errors. |
Source code in src/corecv/engine/exporter.py
528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 | |
TargetRewriter¶
corecv.engine.rewriter.TargetRewriter()
¶
Rewrites FX graphs for edge hardware compatibility.
This class performs graph transformations to make models compatible with edge deployment targets like ExecuTorch (XNNPACK) and ONNX by: - Replacing GELU/SiLU activations with ReLU/HardSwish - Collapsing redundant NCHW <-> NHWC permutations around LayerNorm
Initialize the TargetRewriter.
Source code in src/corecv/engine/rewriter.py
rewrite_for_edge(model)
¶
Rewrite a model's FX graph for edge hardware compatibility.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
Module
|
The PyTorch model to rewrite. Can be a regular nn.Module or an already traced GraphModule. |
required |
Returns:
| Type | Description |
|---|---|
Module
|
A new GraphModule with edge-compatible graph transformations applied. |
Module
|
Preserves training/eval mode and gradient flow. |
Source code in src/corecv/engine/rewriter.py
MetaProber¶
corecv.engine.validator.MetaProber()
¶
Validates model compatibility for edge hardware deployment.
Performs zero-VRAM shape propagation and static graph analysis to ensure models can be compiled to ExecuTorch/ONNX without graph breaks or dynamic operations.
Initialize the MetaProber.
Source code in src/corecv/engine/validator.py
validate_compatibility(backbone, neck, head, input_size=(224, 224), target_hardware='edge')
¶
Validate end-to-end model compatibility for edge deployment.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
backbone
|
Module
|
Backbone module (must implement BaseBackbone interface). |
required |
neck
|
Module | None
|
Optional neck module (FPN, PAN, etc.). |
required |
head
|
Module
|
Head module (classification, detection, segmentation). |
required |
input_size
|
tuple[int, int]
|
Input image size as (height, width). |
(224, 224)
|
target_hardware
|
str
|
Target deployment hardware. One of
|
'edge'
|
Returns:
| Type | Description |
|---|---|
bool
|
True if the model is compatible with edge hardware. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If backbone does not implement BaseBackbone interface. |
ValueError
|
If compatibility check fails with descriptive message, or hardware-specific constraints are violated. |
RuntimeError
|
If torch.export fails due to graph breaks. |
Source code in src/corecv/engine/validator.py
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 | |