Skip to content

Conversation

DhyeyMavani2003
Copy link

Description

Fixes #10393 - Sequential initialization causing program to run twice when called from a script with a valid Python identifier name.

Problem

When torch_geometric.nn.Sequential was initialized from a script with a valid Python identifier name (e.g., foo.py), the entire program would execute twice. This only occurred when the filename was a syntactically valid Python function name.

Example

# foo.py
import torch_geometric.nn as pyg

print("before")
pyg.Sequential('x', [(lambda x: x, 'x -> y')])
print("after")

Running python foo.py would output:

before
before
after
after

Root Cause

The jinja template used by Sequential was importing the caller module with from <module> import *. When the caller was the __main__ script (e.g., foo.py), this caused Python to import and execute the module again, resulting in double execution.

Solution

Modified _set_jittable_template method to filter out the caller module from imports if:

  • The caller module is __main__ (the script being executed)
  • The caller module is not yet in sys.modules (would be imported for the first time)

This prevents circular imports and double execution while maintaining functionality for legitimate module imports.

Changes

  • Modified torch_geometric/nn/sequential.py to add filtering logic before passing modules to the jinja template
  • Added test case test_sequential_no_double_execution to prevent regression

Testing

  • ✅ Reproduced the original bug and verified the fix resolves it
  • ✅ All existing tests in test/nn/test_sequential.py pass (7/7)
  • ✅ New test validates no double execution occurs during initialization
  • ✅ Tested with both valid Python identifier filenames and hyphenated filenames

Checklist

  • I have read the contributing guidelines
  • I have added tests that prove my fix is effective
  • I have updated the documentation (if applicable)
  • All new and existing tests passed

Fixes issue where initializing torch_geometric.nn.Sequential from a script
with a valid Python identifier name (e.g., foo.py) would cause the entire
program to run twice.

Root cause: The jinja template was importing the caller module with
'from <module> import *', which caused re-execution when the caller was
the __main__ script.

Solution: Filter out the caller module from imports if it's __main__ or
not yet in sys.modules, preventing circular imports and double execution.

- Modified torch_geometric/nn/sequential.py to check if caller module
  should be imported before passing it to the template
- Added test case to verify Sequential initialization doesn't cause
  double execution

Co-authored-by: Ona <no-reply@ona.com>
pre-commit-ci bot and others added 2 commits October 4, 2025 07:58
- Fix PEP8 line length violation (E501) in sequential.py
- Remove stray pass statement from test
- Simplify test assertions and add output shape validation
- Ensure all pre-commit checks pass

Co-authored-by: Ona <no-reply@ona.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Initialising pyg.Sequential makes the program run twice

1 participant