Skip to content

Add nn.Sequential: callable ModuleList that chains forward calls#2823

Merged
justinchuby merged 8 commits intomainfrom
justinchu/sequential
Feb 24, 2026
Merged

Add nn.Sequential: callable ModuleList that chains forward calls#2823
justinchuby merged 8 commits intomainfrom
justinchu/sequential

Conversation

@justinchuby
Copy link
Collaborator

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:

  • 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.

justinchuby and others added 2 commits February 23, 2026 16:44
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>
Signed-off-by: Justin Chu <justinchuby@users.noreply.github.com>
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 Sequential class that subclasses ModuleList and chains forward calls
  • Updates import statements across the codebase to use aliased builder module imports
  • Adds comprehensive test coverage for Sequential functionality

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
Copy link

codecov bot commented Feb 24, 2026

Codecov Report

❌ Patch coverage is 96.08696% with 9 lines in your changes missing coverage. Please review.
✅ Project coverage is 71.66%. Comparing base (f99fa7d) to head (02ec841).
⚠️ Report is 1 commits behind head on main.
✅ All tests successful. No failed tests found.

Files with missing lines Patch % Lines
onnxscript/nn/_sequential.py 74.07% 6 Missing and 1 partial ⚠️
onnxscript/nn/_module_test.py 98.94% 2 Missing ⚠️
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.
📢 Have feedback on the report? Share it here.

…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>
@justinchuby justinchuby self-assigned this Feb 24, 2026
Signed-off-by: Justin Chu <justinchuby@users.noreply.github.com>
Signed-off-by: Justin Chu <justinchuby@users.noreply.github.com>
Signed-off-by: Justin Chu <justinchuby@users.noreply.github.com>
@justinchuby justinchuby enabled auto-merge (squash) February 24, 2026 20:37
@justinchuby justinchuby merged commit f6a7de1 into main Feb 24, 2026
29 of 31 checks passed
@justinchuby justinchuby deleted the justinchu/sequential branch February 24, 2026 20:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Development

Successfully merging this pull request may close these issues.

3 participants