Skip to content

[BugFix] Resolve CompositeDistribution name_map with unraveled keys - #2649

Open
YeonwooSung wants to merge 2 commits into
pytorch:mainfrom
YeonwooSung:fix/composite-name-map
Open

YeonwooSung wants to merge 2 commits into
pytorch:mainfrom
YeonwooSung:fix/composite-name-map

Conversation

@YeonwooSung

Copy link
Copy Markdown

Problem

CompositeDistribution.__init__ and from_distributions unraveled both sides of name_map, then looked the distribution up with the original key:

write_name = unravel_key(name_map.get(name, name_unravel))

dict.get never raises, so the except KeyError was dead. A tuple key does not survive unravel_key (("a",) becomes "a", and (("nested",), "disc") becomes ("nested", "disc")), so the lookup missed and the sample kept the unraveled distribution name. String keys only worked because unravel_key("a") is still "a". A name_map entry that did not match any distribution was ignored, despite from_distributions documenting that case as a KeyError.

Fix

Both constructors resolve name_map through one helper. Distribution names are matched after unravel_key, and the sample is written under the mapped key. Names omitted from name_map keep their own name. A name_map key that does not match any distribution raises KeyError.

Tests

TestCompositeDist covers tuple-key rename, nested tuple-key rename, string-key rename, and an unknown name_map key, for __init__ and from_distributions.

Before the fix, the tuple-key tests failed with the sample key left as "a" (nested keys stayed ("nested", "disc")), and the unknown-key tests did not raise. String keys already passed.

After the fix:

pytest test/nn/test_nn.py::TestCompositeDist -q --tb=short
85 passed

ruff check and ruff format are clean on tensordict/nn/distributions/composite.py and test/nn/test_nn.py.

Tuple keys such as ("a",) keep the sample name "a" instead of the
name_map target, and a name_map key that matches no distribution is
ignored. Cover both constructors.
name_map was keyed by unraveled names but looked up with the original
distribution key, so a tuple such as ("a",) never renamed the sample.
Match on the unraveled name in both constructors, and raise KeyError
when a name_map key is not one of the distributions.
@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Oct 14, 2026
@github-actions github-actions Bot added bug Something isn't working Test nn and removed bug Something isn't working labels Oct 14, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. nn Test

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant