fix: flatten_tree fails loudly on flattened-key collisions instead of corrupting silently#1920
fix: flatten_tree fails loudly on flattened-key collisions instead of corrupting silently#1920arham766 wants to merge 2 commits into
Conversation
… corrupting
Flattened names join nested keys with '.', and the dict() pass deduped
colliding names AND their values while TreeSpec.spec kept both leaf
slots - so {'a': {'b': 1}, 'a.b': 2} silently dropped a value and the
later unflatten_tree crashed with IndexError far from the root cause
(observable at inference time through the ONNX deploy paths, which
flatten user-supplied input/output mappings where dotted state-dict
style keys are idiomatic). Duplicate flattened names now raise a
ValueError naming the colliding key; distinct keys are untouched.
Part of the findings in NVIDIA#1902 (wave 2).
Signed-off-by: arham766 <arhamislam766@yahoo.com>
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Enterprise Run ID: 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
📝 WalkthroughWalkthrough
Changesflatten_tree duplicate key detection
Estimated code review effort: 2 (Simple) | ~10 minutes Related PRs: None specified.
🚥 Pre-merge checks | ✅ 6✅ Passed checks (6 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
modelopt/torch/utils/_pytree.py (1)
107-117: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick winConsider documenting the new
ValueErrorbehavior.
flatten_treecan now raiseValueErroron colliding flattened keys, but the docstring has noRaisessection.📝 Suggested docstring addition
Returns: A tuple (values, pytree) where values is a list of values flattened from the provided pytree, and tree_spec is the pytree spec describing the structure of the pytree. + + Raises: + ValueError: If two distinct leaves map to the same flattened key (e.g. ``{"a": {"b": 1}, "a.b": 2}``). """As per coding guidelines, "Document public APIs with docstrings, including examples when useful."
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@modelopt/torch/utils/_pytree.py` around lines 107 - 117, Update the public API docstring for flatten_tree in _pytree.py to document its new ValueError behavior. Add a Raises section that mentions the collision case for flattened keys, and keep the existing description of the function’s return values and prefix handling aligned with the implementation. Ensure the docstring for flatten_tree clearly tells callers that colliding flattened keys will fail with ValueError.Source: Coding guidelines
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Nitpick comments:
In `@modelopt/torch/utils/_pytree.py`:
- Around line 107-117: Update the public API docstring for flatten_tree in
_pytree.py to document its new ValueError behavior. Add a Raises section that
mentions the collision case for flattened keys, and keep the existing
description of the function’s return values and prefix handling aligned with the
implementation. Ensure the docstring for flatten_tree clearly tells callers that
colliding flattened keys will fail with ValueError.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Enterprise
Run ID: ca2d9879-a098-4734-814e-1c4929002a2a
📒 Files selected for processing (2)
modelopt/torch/utils/_pytree.pytests/unit/torch/utils/test_pytree.py
Signed-off-by: arham766 <arhamislam766@yahoo.com>
What does this PR do?
Type of change: Bug fix
Silent value loss + delayed crash in flatten_tree (found writing #1916): dotted-key collisions ({'a': {'b': 1}, 'a.b': 2}) were deduped by a dict() pass that dropped a VALUE while TreeSpec kept both leaf slots — unflatten_tree then died with IndexError far from the cause. The ONNX deploy paths flatten user-supplied input/output mappings where state-dict-style dotted keys are idiomatic, so this was reachable at inference time. Duplicate flattened names now raise ValueError naming the colliding key. Caller audit and near-collision round-trip tests included; collision tests verified to fail pre-fix (silent loss reproduced).
Usage
N/A
Testing
Regression tests included, each verified to FAIL with the fix reverted. Combined battery with all sibling wave fixes: 381 passed across tests/unit/torch/opt, tests/unit/torch/utils, tests/unit/recipe, and quantization test_mode. Note: the unmerged suite PRs #1913-#1916 contain behavior-documenting NOTE tests pinning the OLD behavior fixed here — whichever lands second gets the NOTE tests flipped (happy to rebase either way).
Before your PR is "Ready for review"
Additional Information
Issue: #1902 (wave-2 findings)
Summary by CodeRabbit