Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions test/nn/test_sequential.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,3 +174,31 @@ def test_sequential_to_hetero():
assert isinstance(out_dict, dict) and len(out_dict) == 2
assert out_dict['paper'].size() == (100, 64)
assert out_dict['author'].size() == (100, 64)


def test_sequential_no_double_execution():
# Test for issue #10393: Sequential should not cause double execution
# when initialized from a script with a valid Python identifier name
execution_count = [0]

def increment_counter(x):
execution_count[0] += 1
return x

x = torch.randn(4, 16)

# Create Sequential model - should not execute forward pass
model = Sequential('x', [(increment_counter, 'x -> y')])

# Counter should not be incremented during initialization
assert execution_count[0] == 0

# Execute forward pass
output = model(x)
assert execution_count[0] == 1
assert output.shape == x.shape

# Execute again to verify it works correctly
output = model(x)
assert execution_count[0] == 2
assert output.shape == x.shape
11 changes: 10 additions & 1 deletion torch_geometric/nn/sequential.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,12 +246,21 @@ def _set_jittable_template(self, raise_on_error: bool = False) -> None:
root_dir = osp.dirname(osp.realpath(__file__))
uid = '%06x' % random.randrange(16**6)
jinja_prefix = f'{self.__module__}_{self.__class__.__name__}_{uid}'

# Filter out modules that would cause re-execution:
# - '__main__' is the script being executed
# - Modules not in sys.modules would be imported first time
modules_to_import = []
if (self._caller_module != '__main__'
and self._caller_module in sys.modules):
modules_to_import.append(self._caller_module)

module = module_from_template(
module_name=jinja_prefix,
template_path=osp.join(root_dir, 'sequential.jinja'),
tmp_dirname='sequential',
# Keyword arguments:
modules=[self._caller_module],
modules=modules_to_import,
signature=self.signature,
children=self._children,
)
Expand Down