Skip to content

Remove module-level RNG reset from gnn.py - #1227

Open
lehendo wants to merge 1 commit into
sunlabuiuc:masterfrom
lehendo:rngfix
Open

Remove module-level RNG reset from gnn.py#1227
lehendo wants to merge 1 commit into
sunlabuiuc:masterfrom
lehendo:rngfix

Conversation

@lehendo

@lehendo lehendo commented Sep 2, 2026

Copy link
Copy Markdown
Collaborator

pyhealth/models/gnn.py called torch.manual_seed(3) and np.random.seed(1) at module level. Since pyhealth/models/init.py imports from this file, simply running import pyhealth.models silently overwrote any seed a caller had already set.

The fix was to remove both module-level seed calls, which has been done.

pyhealth/models/gnn.py called torch.manual_seed(3) and np.random.seed(1)
at module level (right after its imports). Since
pyhealth/models/__init__.py does 'from .gnn import GAT, GCN', this ran
as a side effect of any 'import pyhealth.models' -- silently overwriting
whatever seed the caller had already set for their own script's
reproducibility, regardless of whether GCN/GAT were ever used. This is a
process-wide reproducibility hazard: a user who does
'torch.manual_seed(42); import pyhealth.models; ...' (a completely
standard, recommended pattern) would have their seed silently reset to 3
partway through, with no error or warning.

Confirmed nothing else in the module depends on this running at import
time -- everything else in the file is function/class definitions, whose
bodies don't execute until called. Fix: delete both lines.

Added a regression test that spawns a fresh subprocess (necessary since
pyhealth.models is already imported -- and the bug already fired -- by
the time any in-process test runs), seeds torch/numpy, records the next
random draw, re-seeds identically, imports pyhealth.models, and checks
the next draw is unchanged. Confirmed this test fails against the
pre-fix code with the exact expected symptom (RNG stream diverges after
the import) and passes after the fix; full existing GCN/GAT test suite
(21/21) still passes, confirming the removal has no other effect.
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.

1 participant