Repository navigation
Conversation
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/90112
Note: Links to docs will display an error until the docs builds have been completed. ❌ 3 FailuresAs of commit b3416fa: NEW FAILURES - The following jobs have failed:
This comment was automatically generated by Dr. CI and updates every 15 minutes. |
|
@malfet ping |
malfet
left a comment
There was a problem hiding this comment.
Hmm, what would happen if one tries to clone container with numerical object names
If I'm understanding correctly, that case is covered in the test: it mixes named and unnamed modules (modules of the sequence take the respective index value in the unnamed case) |
|
@malfet nudge- job failures seemingly unrelated, from jenkins |
|
@malfet - ping |
|
Looks like this PR hasn't been updated in a while so we're going to go ahead and mark this as |
Fixes #71069. `SequentialImpl::clone()` rebuilt the container with the *unnamed* `push_back` overload, which assigns each submodule the positional name `std::to_string(modules_.size())`. Every registered name was replaced by its index, so cloning a named `Sequential` silently rewrote its `state_dict` keys — `encoder.weight` became `0.weight` — breaking a save/load round trip against the original module with no error. `ModuleDictImpl::clone()` is unaffected because its `modules_` is an `OrderedDict` and it clones through `insert(key, value)`. `Sequential`'s `modules_` is a plain `std::vector<AnyModule>` and carries no names, so they have to come from the registered children. `clone()` now reads them from the public `named_children()` and uses the named `push_back`. This deliberately does not use `modules_[i].ptr()->name()`: that returns the demangled RTTI type name, so a container holding two modules of the same type would produce duplicate keys. ### Test `SequentialTest.ClonePreservesSubmoduleNames`, next to the existing `IsCloneable`. It asserts on `named_children().keys()` and `named_parameters().keys()` rather than on `Module::name()` — the latter is the *type* name and compares equal whether or not the bug is present, which is what made the test in the earlier abandoned attempt (#90112) vacuous. I cannot launch gtest binaries on my machine (Device Guard blocks unsigned executables), so the same scenarios were run through a Python extension module against the real container. Before: ``` named clone : 0, 1 params clone : 0.weight, 0.bias mixed clone : 0, 1, 2 unnamed clone : 0, 1, 2, 3 ``` After: ``` named clone : encoder, act params clone : encoder.weight, encoder.bias mixed clone : first, 1, last unnamed clone : 0, 1, 2, 3 ``` An unnamed submodule keeps the positional name it was originally given rather than being renumbered around the named ones, and a container with no names at all is unchanged. ### On numeric submodule names @malfet asked this on the earlier attempt (#90112) and it was never answered, so: ``` numeric original : 2, 0, 1 numeric clone : 2, 0, 1 numeric clone^2 : 2, 0, 1 collision build : THREW on the original -> Submodule '1' already defined ``` Names round trip verbatim, so numeric names that do not match their position are preserved rather than renumbered, and cloning is idempotent. A container whose auto-name would collide with an explicit numeric one cannot be built at all: `register_module` inserts into an `OrderedDict`, which throws on a duplicate key, so `Sequential({{"1", A}})` followed by an unnamed `push_back` throws on the *original* long before `clone()` is reached. The proposed behaviour is strictly closer to the original than today's renumbering. Developed with AI assistance (Claude); I reviewed the change and the reasoning above. Pull Request resolved: #195107 Approved by: https://github.com/Skylion007 Co-authored-by: Claude Opus 5 <[email protected]>
…95107) Fixes pytorch#71069. `SequentialImpl::clone()` rebuilt the container with the *unnamed* `push_back` overload, which assigns each submodule the positional name `std::to_string(modules_.size())`. Every registered name was replaced by its index, so cloning a named `Sequential` silently rewrote its `state_dict` keys — `encoder.weight` became `0.weight` — breaking a save/load round trip against the original module with no error. `ModuleDictImpl::clone()` is unaffected because its `modules_` is an `OrderedDict` and it clones through `insert(key, value)`. `Sequential`'s `modules_` is a plain `std::vector<AnyModule>` and carries no names, so they have to come from the registered children. `clone()` now reads them from the public `named_children()` and uses the named `push_back`. This deliberately does not use `modules_[i].ptr()->name()`: that returns the demangled RTTI type name, so a container holding two modules of the same type would produce duplicate keys. ### Test `SequentialTest.ClonePreservesSubmoduleNames`, next to the existing `IsCloneable`. It asserts on `named_children().keys()` and `named_parameters().keys()` rather than on `Module::name()` — the latter is the *type* name and compares equal whether or not the bug is present, which is what made the test in the earlier abandoned attempt (pytorch#90112) vacuous. I cannot launch gtest binaries on my machine (Device Guard blocks unsigned executables), so the same scenarios were run through a Python extension module against the real container. Before: ``` named clone : 0, 1 params clone : 0.weight, 0.bias mixed clone : 0, 1, 2 unnamed clone : 0, 1, 2, 3 ``` After: ``` named clone : encoder, act params clone : encoder.weight, encoder.bias mixed clone : first, 1, last unnamed clone : 0, 1, 2, 3 ``` An unnamed submodule keeps the positional name it was originally given rather than being renumbered around the named ones, and a container with no names at all is unchanged. ### On numeric submodule names @malfet asked this on the earlier attempt (pytorch#90112) and it was never answered, so: ``` numeric original : 2, 0, 1 numeric clone : 2, 0, 1 numeric clone^2 : 2, 0, 1 collision build : THREW on the original -> Submodule '1' already defined ``` Names round trip verbatim, so numeric names that do not match their position are preserved rather than renumbered, and cloning is idempotent. A container whose auto-name would collide with an explicit numeric one cannot be built at all: `register_module` inserts into an `OrderedDict`, which throws on a duplicate key, so `Sequential({{"1", A}})` followed by an unnamed `push_back` throws on the *original* long before `clone()` is reached. The proposed behaviour is strictly closer to the original than today's renumbering. Developed with AI assistance (Claude); I reviewed the change and the reasoning above. Pull Request resolved: pytorch#195107 Approved by: https://github.com/Skylion007 Co-authored-by: Claude Opus 5 <[email protected]>
…95107) Fixes pytorch#71069. `SequentialImpl::clone()` rebuilt the container with the *unnamed* `push_back` overload, which assigns each submodule the positional name `std::to_string(modules_.size())`. Every registered name was replaced by its index, so cloning a named `Sequential` silently rewrote its `state_dict` keys — `encoder.weight` became `0.weight` — breaking a save/load round trip against the original module with no error. `ModuleDictImpl::clone()` is unaffected because its `modules_` is an `OrderedDict` and it clones through `insert(key, value)`. `Sequential`'s `modules_` is a plain `std::vector<AnyModule>` and carries no names, so they have to come from the registered children. `clone()` now reads them from the public `named_children()` and uses the named `push_back`. This deliberately does not use `modules_[i].ptr()->name()`: that returns the demangled RTTI type name, so a container holding two modules of the same type would produce duplicate keys. ### Test `SequentialTest.ClonePreservesSubmoduleNames`, next to the existing `IsCloneable`. It asserts on `named_children().keys()` and `named_parameters().keys()` rather than on `Module::name()` — the latter is the *type* name and compares equal whether or not the bug is present, which is what made the test in the earlier abandoned attempt (pytorch#90112) vacuous. I cannot launch gtest binaries on my machine (Device Guard blocks unsigned executables), so the same scenarios were run through a Python extension module against the real container. Before: ``` named clone : 0, 1 params clone : 0.weight, 0.bias mixed clone : 0, 1, 2 unnamed clone : 0, 1, 2, 3 ``` After: ``` named clone : encoder, act params clone : encoder.weight, encoder.bias mixed clone : first, 1, last unnamed clone : 0, 1, 2, 3 ``` An unnamed submodule keeps the positional name it was originally given rather than being renumbered around the named ones, and a container with no names at all is unchanged. ### On numeric submodule names @malfet asked this on the earlier attempt (pytorch#90112) and it was never answered, so: ``` numeric original : 2, 0, 1 numeric clone : 2, 0, 1 numeric clone^2 : 2, 0, 1 collision build : THREW on the original -> Submodule '1' already defined ``` Names round trip verbatim, so numeric names that do not match their position are preserved rather than renumbered, and cloning is idempotent. A container whose auto-name would collide with an explicit numeric one cannot be built at all: `register_module` inserts into an `OrderedDict`, which throws on a duplicate key, so `Sequential({{"1", A}})` followed by an unnamed `push_back` throws on the *original* long before `clone()` is reached. The proposed behaviour is strictly closer to the original than today's renumbering. Developed with AI assistance (Claude); I reviewed the change and the reasoning above. Pull Request resolved: pytorch#195107 Approved by: https://github.com/Skylion007 Co-authored-by: Claude Opus 5 <[email protected]>
What?
Fixes #71069
Further changes:
torch::nn::Sequential::push_back()method used to unpack the parameters from the variadic constructorWhy?
push_back()method to solve what turned out to be a non-issue. Afterwards, I found these changes made it easier to understand. Also, now we don't need to rely on any publicpush_back()methods in unpacking: originally, the base case of 1 parameter in the variadic template was covered by the publicpush_back(M&& module).Further Development?
Possible changes with C++17 (lmk if there's a wish list for that):
c10::optionalwithstd::optionalclass Sequential; will save following the dependency chainP.S.
I think you guys squash commits; if not, I can smash them.