Repository navigation
feat: ResNet-18 end to end, Conv+BN folding, preload anonymous weights - #5
Merged
Merged
Conversation
- ResNet-18 now verified against ONNX Runtime on Model Zoo v1-7/v2-7 and torchvision legacy + dynamo exports (tests/test_resnet.py, new CI job). - Add ReduceMean/Sum/Max/Min; the dynamo exporter emits ReduceMean for global average pooling. - Pre-load static weights that onnx-ir passes as anonymous constants (keyed by data id). Previously every conv weight was converted from TensorData on each forward pass. - Fold BatchNormalization into a directly preceding Conv2d at load time when the conv output has no other consumer. Works around slow [1,C,1,1] broadcasting in burn-flex: Model Zoo v1 batch 8 279 -> 103 ms. - Only use the pre-fused BN fast path for 4D inputs; rank-2 BN was being broadcast to a 4D output. - Linear/Gemm bias goes through ctx.resolve (uses pre-loaded weights). Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
ResNet-18 now runs end to end and matches ONNX Runtime across four real exports:
resnet18-v1-7resnet18-v2-7resnet18torch.onnxlegacyresnet18torch.onnxdynamo(batch 1 and 4, pretrained weights)
Changes
ReduceMeanover H, W.ValueSource::Staticargs. These were skipped at load and converted fromTensorDataon every forward pass. They're now keyed by data id (static::{id}) and loaded once. ~10% faster at batch 1 across the board.W' = W·scale,b' = b·scale + offset, BN node removed. Only when the conv output feeds nothing but that BN (and isn't a graph output). Unfoldable BNs keep the existing mul+add path.[1,C,1,1]pre-fused params turned a rank-2[N,C]BN into a 4D output.ctx.resolve, so it benefits from pre-loading too.Performance (CPU, Apple M-series 10 cores, median of 10)
ORT is ~14.8 ms at batch 1. v2 still trails at batch 8 because its pre-activation BNs (after Add/Relu, not Conv) can't be folded and hit a slow path in burn-flex: a
[1,C,1,1]broadcast multiply over[8,64,112,112]takes ~24 ms vs ~3.6 ms same-shape (numpy: 0.9 ms). Worth reporting upstream.Testing
tests/test_resnet.py(new): downloads + caches Model Zoo models, exports torchvision models if torch is installed, compares vs ORT;--benchprints timings.actions/cache). Not added to required checks — your call.tests/test_ops.py: BN folding cases (folded / no-bias / conv output reused / conv output is graph output / BN after Relu / rank-2 BN) with node-count assertions, plus reductions. 43 checks pass.test_bridge.py,compare_ort.py,cargo fmt --check,clippy -D warningsclean.Note: torch.export specializes size-1 dims, so a dynamo export traced with batch 1 bakes
[1, 512]into the final Reshape. The test exports with batch 2.🤖 Generated with Claude Code