Add nn.Sequential: callable ModuleList that chains forward calls#2823
Add nn.Sequential: callable ModuleList that chains forward calls#2823justinchuby merged 8 commits intomainfrom
Conversation
nn.Sequential mirrors torch.nn.Sequential — children are registered with numeric keys and forward() passes each child's output as the next child's input. Key design decisions: - Subclasses ModuleList for registration, indexing, iteration - Overrides _set_name to keep children with simple '0', '1' names (avoids double-prefixing since __call__ already pushes the parent name) - Raises RuntimeError on empty Sequential This enables matching HF diffusers naming conventions (e.g. nn.Sequential(SiLU(), Linear(...)) producing 'mod.1.weight') without needing preprocess_weights renames. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
This PR adds nn.Sequential, a callable container that chains forward calls through its child modules, mirroring PyTorch's torch.nn.Sequential. This enables cleaner model definitions and matches HuggingFace diffusers naming conventions without requiring weight preprocessing.
Changes:
- Implements
Sequentialclass that subclassesModuleListand chains forward calls - Updates import statements across the codebase to use aliased
buildermodule imports - Adds comprehensive test coverage for
Sequentialfunctionality
Reviewed changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| onnxscript/nn/_sequential.py | New file implementing Sequential container with forward chaining and custom naming behavior |
| onnxscript/nn/_module_test.py | Adds four test cases covering Sequential's forward chaining, parameter naming, mixed modules, and empty container handling |
| onnxscript/nn/init.py | Exports Sequential in public API |
| onnxscript/nn/_module.py | Refactors builder imports to use aliased form |
| onnxscript/nn/_module_list.py | Refactors builder imports to use aliased form |
| onnxscript/nn/_parameter.py | Refactors builder imports to use aliased form |
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #2823 +/- ##
==========================================
+ Coverage 71.48% 71.66% +0.18%
==========================================
Files 237 238 +1
Lines 28645 28864 +219
Branches 2841 2849 +8
==========================================
+ Hits 20476 20686 +210
- Misses 7199 7207 +8
- Partials 970 971 +1 ☔ View full report in Codecov by Sentry. |
…ldren
When children are appended to a ModuleList after the parent Module has
already called _set_name (e.g. via __setattr__), _register_child now
qualifies the child name with the ModuleList's own name. This ensures
that children appended after registration produce correct ONNX
initializer names like 'mid_block.attentions.0.weight' instead of
'mid_block.0.weight'.
Sequential overrides _register_child to keep simple index names ('0',
'1') since Sequential.__call__ already pushes its own scope.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Signed-off-by: Justin Chu <justinchuby@users.noreply.github.com>
Signed-off-by: Justin Chu <justinchuby@users.noreply.github.com>
Usage PR: https://github.com/justinchuby/onnx-genai-models/pull/19
nn.Sequential mirrors torch.nn.Sequential — children are registered
with numeric keys and forward() passes each child's output as the
next child's input.
Key design decisions:
(avoids double-prefixing since call already pushes the parent name)
This enables matching HF diffusers naming conventions (e.g.
nn.Sequential(SiLU(), Linear(...)) producing 'mod.1.weight') without
needing preprocess_weights renames.