Skip to content

Replace deprecated torch.range with torch.arange - #154

Open
xyf5432 wants to merge 1 commit into
THUMNLab:mainfrom
xyf5432:fix/torch-range-deprecation
Open

Replace deprecated torch.range with torch.arange#154
xyf5432 wants to merge 1 commit into
THUMNLab:mainfrom
xyf5432:fix/torch-range-deprecation

Conversation

@xyf5432

@xyf5432 xyf5432 commented Aug 22, 2026

Copy link
Copy Markdown

Fixes #153

Summary

torch.range is deprecated (unconditional UserWarning on every call, on every torch version — no silent window) and scheduled for removal. This PR replaces the three uses with torch.arange across two files:

 # autogl/module/model/pyg/robust/gnnguard.py:167 (GCN4GNNGuard.add_loop_sparse — active in fit)
-        row = torch.range(0, int(adj.shape[0]-1), dtype=torch.int64)
+        row = torch.arange(0, adj.shape[0], dtype=torch.int64)
 # autogl/module/model/pyg/robust/gnnguard.py:391 (GCN4GNNGuard_attack.add_loop_sparse — dead code)
-        row = torch.range(0, int(adj.shape[0]-1), dtype=torch.int64)
+        row = torch.arange(0, adj.shape[0], dtype=torch.int64)
 # autogl/module/nas/space/grna.py:393 (GNNGuard.add_loop_sparse — dead code)
-        row = torch.range(0, int(adj.shape[0]-1), dtype=torch.int64)
+        row = torch.arange(0, adj.shape[0], dtype=torch.int64)

The migration is lossless for these call sites, for two reasons:

  • Closed vs. half-open range: torch.range(0, N-1) (inclusive end) yields 0..N-1, exactly what torch.arange(0, N) (exclusive end) yields — the -1 disappears.
  • Explicit dtype: all three calls pass dtype=torch.int64, so the historical dtype difference (float32 default for torch.range vs. integer-argument int64 inference for torch.arange since 1.4) is irrelevant here.

torch.arange has existed since torch 1.0, so no torch version floor is raised. setup.py currently declares no torch constraint.

Type of change

  • Bug fix (non-breaking change which fixes an issue)

Validation

Verified on torch 2.11.0:

  • Control: torch.range(0, N-1, dtype=torch.int64) raises UserWarning under warnings.simplefilter("error", UserWarning) — the issue is real and the filter is sensitive.
  • Fix: torch.arange(0, N, dtype=torch.int64) is warning-free under the same filter.
  • Equivalence: the full add_loop_sparse body (rowstacksparse.FloatTensoradj + I_n) produces element-wise identical results and identical sparse layout for both versions on a 7-node adjacency.
  • py_compile passes on both touched files; the repo has zero bare torch.range references (grep-verified).

User impact

No behavior change on any torch version: GCN4GNNGuard.fit() no longer emits the UserWarning, warning-as-error environments (-W error::UserWarning, pytest -W error) no longer fail during training, and the code will not break when torch.range is removed.

Notes for reviewers

  • Only gnnguard.py:167 is on a live path (called unconditionally from fit); the other two sites are dead code (gnnguard.py:391's only call site is commented out; GNNGuard.add_loop_sparse in nas/space/grna.py has no caller anywhere — forward calls att_coef). They are fixed for consistency and to keep the code removal-proof.
  • Same rule, same fix: kornia/kornia#3380.

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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant