Skip to content

Update Tensor* users for Graph::initializers_'s new unique_ptr storage - #9

Open
take-cheeze wants to merge 1 commit into
claude/tensor-hashing-research-iepmh2from
claude/stable-initializer-storage-633
Open

Update Tensor* users for Graph::initializers_'s new unique_ptr storage#9
take-cheeze wants to merge 1 commit into
claude/tensor-hashing-research-iepmh2from
claude/stable-initializer-storage-633

Conversation

@take-cheeze

Copy link
Copy Markdown
Member

Summary

onnx's Graph::initializers_ changed from vector<Tensor> to vector<unique_ptr<Tensor>> so a Tensor's address survives other initializers being inserted/erased around it in the same vector (see onnxsim/onnx#1, part of onnxsim issue #633's follow-up investigation into onnxsim's remaining speed gap vs onnxslim).

Base branch note: targets claude/tensor-hashing-research-iepmh2 (the commit onnxsim's master actually has third_party/onnx-optimizer pinned to) rather than main, matching this repo's existing pattern for onnxsim-specific patches (see e.g. #8). Retarget to main if/when that catches up.

Changes

  • eliminate_duplicate_initializer.h: iterates the new vector<unique_ptr<Tensor>>, dereferencing one extra level. No behavior change — the CSETensorHash/CSETensorEqual-based dedup logic and its within-one-call TensorContentDigest cache usage are unaffected; this container swap does not, by itself, extend that cache's lifetime across calls (see onnxsim's bench/RESULTS_issue633_followup.md for why that's a separate, larger, unattempted change).
  • pass_util.h's FetchConstantTensor: getInitializer() now returns const Tensor* directly instead of an iterator, so this drops the now-redundant &*.
  • Bumps the third_party/onnx submodule pin to the corresponding commit on the onnx side (onnxsim/onnx@781592f, a cherry-pick of the same onnx/onnx#1 fix onto this repo's existing third_party/onnx lineage, since that lineage had already diverged from the one onnxsim's own third_party/onnx submodule tracks — see that commit for details) so this repo's own standalone build/tests stay consistent with these call-site changes.

Testing

Verified via onnxsim's full rebuild (onnxsim's own third_party/onnx and third_party/onnx-optimizer submodules pointed at the equivalent commits on the lineage onnxsim actually uses) plus its core pytest suite and end-to-end simplify() runs — see onnxsim/onnx#1's PR description for the full validation (51/53 passing, 2 pre-existing unrelated failures; cait_xxs36_224/mixer_l16_224_in21k output unchanged). This repo's own standalone build against the bumped submodule (this PR's actual diff) is left for CI to confirm.


Generated by Claude Code

onnx's Graph::initializers_ changed from vector<Tensor> to
vector<unique_ptr<Tensor>> so a Tensor's address survives other
initializers being inserted/erased around it in the same vector (see
onnxsim/onnx#1, part of onnxsim issue #633's follow-up investigation
into onnxsim's remaining speed gap vs onnxslim).

Updates this repo's two call sites accordingly:
- eliminate_duplicate_initializer.h: iterates the new
  vector<unique_ptr<Tensor>>, dereferencing one extra level. No
  behavior change -- the CSETensorHash/CSETensorEqual-based dedup
  logic and its within-one-call TensorContentDigest cache usage are
  unaffected; this container swap does not, by itself, extend that
  cache's lifetime across calls (see onnxsim's
  bench/RESULTS_issue633_followup.md for why that's a separate,
  larger, unattempted change).
- pass_util.h's FetchConstantTensor: getInitializer() now returns
  const Tensor* directly instead of an iterator, so this drops the
  now-redundant &*.

Bumps the third_party/onnx submodule pin to the corresponding commit
on the onnx side (onnxsim/onnx@781592f, a cherry-pick of the same
onnx/onnx#1 fix onto this repo's existing third_party/onnx lineage,
since that lineage had already diverged from the one onnxsim's own
third_party/onnx submodule tracks -- see that commit for details) so
this repo's own standalone build/tests stay consistent with these
call-site changes.

Verified via onnxsim's full rebuild (onnxsim's own third_party/onnx
and third_party/onnx-optimizer submodules pointed at the equivalent
commits on the lineage onnxsim actually uses) plus its core pytest
suite and end-to-end simplify() runs -- see onnxsim/onnx#1's PR
description for the full validation. This repo's own standalone build
against the bumped submodule is left for CI to confirm.
@take-cheeze

Copy link
Copy Markdown
Member Author

CI status: the "Build whls" matrix is red across most platforms, but investigating a couple of them shows this is pre-existing breakage in this repo's own (long-diverged) third_party/onnx nested submodule lineage, unrelated to this PR's actual diff:

  • macOS (cp311/cp312): fails compiling third_party/onnx/onnx/defs/printer.ccto_chars for float is unavailable on this SDK/deployment target. printer.cc isn't touched by this PR or by the third_party/onnx bump's actual diff; this is a pre-existing platform-compatibility gap in the nested onnx lineage this repo's standalone build has been pinned to (separate from the lineage onnxsim's own third_party/onnx tracks, which doesn't have this issue).
  • ubuntu-24.04-arm (cp314t): 5 test failures, all in test_eliminate_shape_gather_* / test_fuse_mul_into_conv_* — passes this PR doesn't touch (eliminate_duplicate_initializer/pass_util only).

Verified via onnxsim's own full build+test suite instead (see PR description) — this repo's standalone CI wasn't green before this PR either (every other open PR here — #1 through #8 — is unmerged), so I'm not attempting to fix unrelated, pre-existing platform/test breakage as part of this small, bounded change.


Generated by Claude Code

take-cheeze pushed a commit to onnxsim/onnxsim that referenced this pull request Aug 19, 2026
Pulls in onnxsim/onnx#1 (Graph::initializers_: vector<Tensor> ->
vector<unique_ptr<Tensor>>, so a Tensor's address survives other
initializers being inserted/erased around it in the same vector) and
the corresponding onnxsim/optimizer#9 call-site updates. See
bench/RESULTS_issue633_followup.md for why this was worth doing (issue
#633's follow-up) and why it's a bounded correctness/robustness fix,
not a performance change on its own.

Updates onnxsim.cpp's own EliminateZeroRnnInitialState, the one
onnxsim-side caller of Graph::initializers() left after the onnx/
onnx-optimizer changes: initializers()[i] is now a unique_ptr<Tensor>,
so building the by-name Tensor* lookup map needs .get() instead of &.

Verified with a full rebuild against these exact submodule commits:
onnxsim's core pytest suite (test_simple.py, test_backend.py,
test_model_checking.py, test_constant_fold_determinism.py,
test_moved_optimizer_passes.py) -- 51 passed, 2 failed on the
pre-existing unrelated missing onnxscript dependency, 0 regressions --
plus end-to-end simplify() on cait_xxs36_224 (1758->1558 nodes) and
mixer_l16_224_in21k (733->582 nodes), both ok=True and node-count
identical to pre-bump output.
@take-cheeze

Copy link
Copy Markdown
Member Author

Same correction as onnxsim/onnx#1: my first validation pass accidentally ran against an unmodified onnxsim build (submodule-pointer sequencing mistake). Re-ran for real against this PR's actual diff (plus the corresponding onnx#1 commit and one more onnxsim-side fix, onnxsim/onnxsim#644) — 51/53 pytest passing (2 pre-existing unrelated failures), cait_xxs36_224/mixer_l16_224_in21k output unchanged. Same numbers as originally reported, just now backed by an actual test of the diff.


Generated by Claude Code

@take-cheeze

Copy link
Copy Markdown
Member Author

Update: upstream fix landed, no changes needed here

onnxsim/onnx#1 (which this PR's call-site changes depend on) had a real data-loss bug in its first version: encodeGraph's non-consuming Export path in ir_pb_converter.cc silently used the wrong (moving/consuming) encodeTensor overload due to a unique_ptr::operator*() const-propagation pitfall, caught by onnxsim's CI on onnxsim/onnxsim#644. Fixed there as 6801eebd; see that PR's description for the full root-cause writeup.

This PR's own diff (eliminate_duplicate_initializer.h, pass_util.h) is unaffected — it doesn't touch encodeGraph or the Export path at all, so it needed no changes for this bug.

Re-verified via onnxsim's full rebuild against the corresponding fixed commit on this repo's own third_party/onnx lineage: tests/test_python_api.py 48 passed/1 deselected/0 failed, core suite 53/53 passing, plus GitHub's own Model Regression workflow on onnxsim#644 fully green.


Generated by Claude Code

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.

2 participants