Skip to content

Improve ONNX save performance: cache proto, update weights only - #353

Merged
adamantivm merged 8 commits into
jonbinney:mainfrom
adamantivm:copilot-worktree-2026-03-09T19-06-22
Mar 10, 2026
Merged

Improve ONNX save performance: cache proto, update weights only#353
adamantivm merged 8 commits into
jonbinney:mainfrom
adamantivm:copilot-worktree-2026-03-09T19-06-22

Conversation

@adamantivm

@adamantivm adamantivm commented Mar 9, 2026

Copy link
Copy Markdown
Collaborator

ONNX Save Optimization Results

Setup

  • Experiment config: deep_quoridor/experiments/B5W3/test_onnx_export.yaml
  • finish_after: 10 models, model_save_timing: true, save_onnx: true
  • Tested on both MLP and ResNet network types
  • Timing reported by trainer covers both PyTorch .pt save and ONNX save together

MLP Network

Baseline (before)

Save # Time (s)
1 1.0424
2 0.9356
3 1.0927
4 1.0188
5 1.0655

Average per save: ~1.03 s · Total for 10 saves: ~10.3 s

After (cached proto)

Save # Time (s)
2 0.0052
3 0.0041
4 0.0041
5 0.0041
6–11 ~0.0041

Average per save (saves 2+): ~0.0043 s · Total for 10 saves: ~0.043 s

MLP Comparison

Metric Before After Speedup
First save ~1.03 s ~1.03 s
Subsequent saves (avg) ~1.03 s ~0.0043 s ~240×
Total for 10 saves ~10.3 s ~0.043 s ~240×

ResNet Network

Baseline (before)

Save # Time (s)
1 1.0264
2 0.9065
3 1.0306
4 0.9120
5 1.0329
6 0.9685
7 1.0510
8 0.9162
9 1.0497
10 1.0103

Average per save: ~0.987 s · Total for 10 saves: ~9.87 s

After (cached proto)

Save # Time (s)
2 0.0050
3 0.0043
4 0.0045
5 0.0045
6 0.0111
7 0.0064
8 0.0042
9 0.0040
10 0.0039
11 0.0039

Average per save (saves 2+): ~0.0052 s · Total for 10 saves: ~0.052 s

ResNet Comparison

Metric Before After Speedup
First save ~0.99 s ~0.99 s
Subsequent saves (avg) ~0.99 s ~0.0052 s ~190×
Total for 10 saves ~9.87 s ~0.052 s ~190×

Verification

  • All 11 MLP .onnx files passed onnx.checker.check_model()
  • All 11 ResNet .onnx files passed onnx.checker.check_model()
  • All models ran inference via onnxruntime without error ✅
  • Output shapes: policy_logits=(1, 57), value=(1, 1) — correct for 5×5 Quoridor ✅
  • Value outputs in reasonable range for both architectures ✅

Implementation Summary

  • _onnx_proto = None and _onnx_init_name_to_idx = {} added to AlphaZeroAgent.__init__
  • First call to save_model_onnx: full torch.onnx.export() as before, then loads the
    written 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).
  • Subsequent calls: iterates state_dict(), looks up each weight's index in the map,
    calls CopyFrom(onnx.numpy_helper.from_array(...)) in place, then onnx.save().
    Graph tracing is skipped entirely.
  • Works identically for both MLP and ResNet architectures.

Julian Cerruti and others added 5 commits March 9, 2026 16:08
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>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 ModelProto and an initializer name→index map after the first torch.onnx.export().
  • On later saves, update cached initializers from state_dict() and re-serialize via onnx.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.

Comment thread deep_quoridor/src/agents/alphazero/alphazero.py
Comment thread deep_quoridor/src/agents/alphazero/alphazero.py
Comment thread deep_quoridor/src/agents/alphazero/alphazero.py Outdated
Comment thread deep_quoridor/src/agents/alphazero/alphazero.py Outdated
Comment thread deep_quoridor/coding-agents/onnx_save_optimization_results.md

@alejandromarcu alejandromarcu left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MSTM (Makes Sense To Me)

adamantivm and others added 3 commits March 10, 2026 14:15
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
adamantivm force-pushed the copilot-worktree-2026-03-09T19-06-22 branch from c44d8c0 to 6e57c3c Compare March 10, 2026 19:44
@adamantivm
adamantivm merged commit b9e6d7a into jonbinney:main Mar 10, 2026
1 check passed
@adamantivm
adamantivm deleted the copilot-worktree-2026-03-09T19-06-22 branch March 10, 2026 19:51
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.

3 participants