Skip to content

Commit e9cfafa

Browse files
HumphreySun98pytorchmergebot
authored andcommitted
[BE] Fix B018 useless-expression violations in inductor and fx unification (#191866)
--- ### Addendum (post-review): F811 evidence and runtime verification **Literal F811 output** with the two noqas stripped from the PR's files (CI-pinned ruff 0.14.4): ``` core.py:140:5: F811 Redefinition of unused `unify` from line 118: `unify` redefined here variable.py:63:5: F811 Redefinition of unused `isvar` from line 56: `isvar` redefined here Found 2 errors. ``` With the noqas present, the full-config check reports `All checks passed!` — so under RUF100 both the presence and the placement are exactly what ruff requires. **Why only these two sites**: ruff's F811 exempts underscore-prefixed names (dummy-variable convention). Minimal repro — two identical `@dispatch` overload chains, one named `_priv`, one named `pub`: ``` f811_repro.py:9:5: F811 Redefinition of unused `pub` from line 7: `pub` redefined here Found 1 error. ``` Only `pub` is flagged. That is why the five `_reify` redefinitions (and `_eval_is_ge` in `torch/utils/_sympy/singleton_int.py`) need no suppression while the public `unify`/`isvar` do. **Runtime verification**: imported the changed `unification` package and the unmodified main version side by side as standalone packages (the subtree is stdlib-only + vendored multipledispatch) and compared 18 aspects: `unify`/`reify`/`isvar` behavior on tuples/lists/dicts/vars including exact exception types and messages for the unsupported-signature cases, plus the complete dispatch registries of `unify`, `isvar`, `_unify`, and `_reify`. All 18 identical (package-name normalized). This exercises the import-time `@dispatch` registration side effects directly. Pull Request resolved: #191866 Approved by: https://github.com/jansel
1 parent 01d92f4 commit e9cfafa

4 files changed

Lines changed: 13 additions & 23 deletions

File tree

‎torch/_inductor/comm_lowering.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ def register_comm_lowerings():
199199
Register lowerings for the comm subsystem.
200200
"""
201201
try:
202-
torch.ops._c10d_functional.all_reduce
202+
_ = torch.ops._c10d_functional.all_reduce
203203
except AttributeError:
204204
log.info(
205205
"Inductor support for distributed collectives depends on building "
@@ -483,7 +483,7 @@ def register_symm_mem_lowerings():
483483
# torch.ops.symm_mem is a lazy namespace that always exists,
484484
# but the operations may not exist on non-CUDA platforms or
485485
# when USE_DISTRIBUTED is disabled.
486-
symm_mem.one_shot_all_reduce
486+
_ = symm_mem.one_shot_all_reduce
487487
except AttributeError:
488488
log.info("symm_mem ops not available, skipping symm_mem lowerings")
489489
return

‎torch/_inductor/runtime/triton_heuristics.py‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3331,7 +3331,11 @@ def _find_names(obj):
33313331

33323332
frame = inspect.currentframe()
33333333
while frame is not None:
3334-
frame.f_locals
3334+
# On CPython <= 3.12 this access materializes the frame's locals
3335+
# dict so gc.get_referrers below can find obj inside it. On 3.13+
3336+
# f_locals is a fresh write-through proxy (PEP 667) and this loop
3337+
# is a no-op, so function-local names are not discoverable there.
3338+
_ = frame.f_locals
33353339
frame = frame.f_back
33363340
obj_names = []
33373341
for referrer in gc.get_referrers(obj):

‎torch/fx/experimental/unification/core.py‎

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -30,33 +30,21 @@ def _reify(t: Iterator[object], s: dict[Var, object]) -> Iterator[object]:
3030
# return (reify(arg, s) for arg in t)
3131

3232

33-
_reify
34-
35-
3633
@dispatch(tuple, dict) # type: ignore[no-redef]
3734
def _reify(t: tuple[Unpack[_Ts]], s: dict[Var, object]) -> tuple[Unpack[_Ts]]:
3835
return tuple(reify(iter(t), s)) # pyrefly: ignore[bad-argument-type, bad-return]
3936

4037

41-
_reify
42-
43-
4438
@dispatch(list, dict) # type: ignore[no-redef]
4539
def _reify(t: list[object], s: dict[Var, object]) -> list[object]:
4640
return list(reify(iter(t), s)) # pyrefly: ignore[bad-argument-type]
4741

4842

49-
_reify
50-
51-
5243
@dispatch(dict, dict) # type: ignore[no-redef]
5344
def _reify(d: dict[object, object], s: dict[Var, object]) -> dict[object, object]:
5445
return {k: reify(v, s) for k, v in d.items()}
5546

5647

57-
_reify
58-
59-
6048
@dispatch(object, dict) # type: ignore[no-redef]
6149
def _reify(o: object, s: dict[Var, object]) -> object:
6250
return o # catch all, just return the object
@@ -146,9 +134,8 @@ def unify(
146134
return _unify(u, v, s)
147135

148136

149-
unify
150-
151-
152137
@dispatch(object, object) # type: ignore[no-redef]
153-
def unify(u: object, v: object) -> dict[Var, object] | bool:
138+
# The noqa suppresses F811 for this multipledispatch overload redefinition
139+
# (it replaces the bare-name-statement hack that previously silenced it).
140+
def unify(u: object, v: object) -> dict[Var, object] | bool: # noqa: F811
154141
return unify(u, v, {})

‎torch/fx/experimental/unification/variable.py‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,10 @@ def isvar(v: Var) -> Literal[True]:
5757
return True
5858

5959

60-
isvar
61-
62-
6360
@dispatch(object) # type: ignore[no-redef]
64-
def isvar(o: object) -> bool:
61+
# The noqa suppresses F811 for this multipledispatch overload redefinition
62+
# (it replaces the bare-name-statement hack that previously silenced it).
63+
def isvar(o: object) -> bool: # noqa: F811
6564
return bool(_glv and hashable(o) and o in _glv)
6665

6766

0 commit comments

Comments
 (0)