Improve ONNX save performance: cache proto, update weights only - #353
Merged
adamantivm merged 8 commits intoMar 10, 2026
Merged
Conversation
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Contributor
There was a problem hiding this comment.
Pull request overview
Optimizes repeated ONNX model saves by caching the exported ONNX graph and updating only initializer tensors (weights/buffers) on subsequent saves, dramatically reducing save time after the first export.
Changes:
- Cache ONNX
ModelProtoand an initializer name→index map after the firsttorch.onnx.export(). - On later saves, update cached initializers from
state_dict()and re-serialize viaonnx.save(). - Add internal documentation/benchmarks describing the optimization plan and measured speedups.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 5 comments.
| File | Description |
|---|---|
| deep_quoridor/src/agents/alphazero/alphazero.py | Implements ONNX proto caching + initializer-only updates for faster subsequent ONNX saves. |
| deep_quoridor/coding-agents/onnx_save_optimization_results.md | Adds benchmark results and verification notes for the optimization. |
| deep_quoridor/coding-agents/onnx_save_optimization_plan.md | Adds an implementation plan documenting the intended approach. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
alejandromarcu
approved these changes
Mar 10, 2026
alejandromarcu
left a comment
Collaborator
There was a problem hiding this comment.
MSTM (Makes Sense To Me)
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
- Pass str(path) to torch.onnx.export for version compatibility - Filter initializer cache to state_dict names only (excludes constant-folded tensors) - Warn when cached initializers are not fully updated from state_dict - Tighten type hint: dict[str, int] instead of bare dict Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
adamantivm
force-pushed
the
copilot-worktree-2026-03-09T19-06-22
branch
from
March 10, 2026 19:44
c44d8c0 to
6e57c3c
Compare
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.
ONNX Save Optimization Results
Setup
deep_quoridor/experiments/B5W3/test_onnx_export.yamlfinish_after: 10 models,model_save_timing: true,save_onnx: true.ptsave and ONNX save togetherMLP Network
Baseline (before)
Average per save: ~1.03 s · Total for 10 saves: ~10.3 s
After (cached proto)
Average per save (saves 2+): ~0.0043 s · Total for 10 saves: ~0.043 s
MLP Comparison
ResNet Network
Baseline (before)
Average per save: ~0.987 s · Total for 10 saves: ~9.87 s
After (cached proto)
Average per save (saves 2+): ~0.0052 s · Total for 10 saves: ~0.052 s
ResNet Comparison
Verification
.onnxfiles passedonnx.checker.check_model()✅.onnxfiles passedonnx.checker.check_model()✅onnxruntimewithout error ✅policy_logits=(1, 57),value=(1, 1)— correct for 5×5 Quoridor ✅Implementation Summary
_onnx_proto = Noneand_onnx_init_name_to_idx = {}added toAlphaZeroAgent.__init__save_model_onnx: fulltorch.onnx.export()as before, then loads thewritten file with
onnx.load()and builds a name→index map over all graph initializers(21 for MLP, 21 for ResNet with
num_blocks=2, num_channels=32).state_dict(), looks up each weight's index in the map,calls
CopyFrom(onnx.numpy_helper.from_array(...))in place, thenonnx.save().Graph tracing is skipped entirely.