Replace deprecated torch.range with torch.arange - #154
Open
xyf5432 wants to merge 1 commit into
Open
Conversation
torch.range emits an unconditional UserWarning on every call (any torch version) and is scheduled for removal. All three call sites pass an explicit dtype=torch.int64, so the closed-to-half-open switch (range(0, N-1) -> arange(0, N)) is lossless: same elements, same dtype. - gnnguard.py: GCN4GNNGuard.add_loop_sparse (active in fit) and GCN4GNNGuard_attack.add_loop_sparse (dead code) - nas/space/grna.py: GNNGuard.add_loop_sparse (dead code) Verified on torch 2.11.0: arange path warning-free under warnings-as-errors, results element-wise identical to the old path. Co-Authored-By: Claude <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #153
Summary
torch.rangeis deprecated (unconditionalUserWarningon every call, on every torch version — no silent window) and scheduled for removal. This PR replaces the three uses withtorch.arangeacross two files:The migration is lossless for these call sites, for two reasons:
torch.range(0, N-1)(inclusive end) yields0..N-1, exactly whattorch.arange(0, N)(exclusive end) yields — the-1disappears.dtype=torch.int64, so the historical dtype difference (float32default fortorch.rangevs. integer-argumentint64inference fortorch.arangesince 1.4) is irrelevant here.torch.arangehas existed since torch 1.0, so no torch version floor is raised.setup.pycurrently declares no torch constraint.Type of change
Validation
Verified on torch 2.11.0:
torch.range(0, N-1, dtype=torch.int64)raisesUserWarningunderwarnings.simplefilter("error", UserWarning)— the issue is real and the filter is sensitive.torch.arange(0, N, dtype=torch.int64)is warning-free under the same filter.add_loop_sparsebody (row→stack→sparse.FloatTensor→adj + I_n) produces element-wise identical results and identical sparse layout for both versions on a 7-node adjacency.py_compilepasses on both touched files; the repo has zero baretorch.rangereferences (grep-verified).User impact
No behavior change on any torch version:
GCN4GNNGuard.fit()no longer emits theUserWarning, warning-as-error environments (-W error::UserWarning, pytest-W error) no longer fail during training, and the code will not break whentorch.rangeis removed.Notes for reviewers
fit); the other two sites are dead code (gnnguard.py:391's only call site is commented out;GNNGuard.add_loop_sparseinnas/space/grna.pyhas no caller anywhere —forwardcallsatt_coef). They are fixed for consistency and to keep the code removal-proof.