Fix the strong-reference fallback in make_weakref - #6856
Open
huthvincent wants to merge 1 commit into
Open
Conversation
Contributor
Author
|
@NVIDIA/mcore-oncall — same ask as on #6859: this is Ready but One file, |
huthvincent
force-pushed
the
fix/B002-cuda-graphs-make-weakref-fallback
branch
from
August 27, 2026 14:45
7cf6cad to
9d226bc
Compare
The `except RuntimeError` handler in `make_weakref` cannot do what its comment says. It interpolates `arg.dtype`, but the parameter is `ten` and no `arg` exists in that scope, so the warning raises `NameError` on rank 0. And `wr` is bound only by `wr = make_weak_ref(ten)` inside the `try`, so on any other rank the handler falls through to `return wr` and raises `UnboundLocalError`. Either way the strong reference the comment promises is never kept and the capture path dies instead of degrading. `arg` is a leftover from `replace_with_weak_ref(arg)`, whose handler ended in `return arg` before NVIDIA#5451 hoisted the body into `make_weakref(ten, inplace=True)`. This restores that behaviour: rename the interpolation and bind the fallback. This is the suggestion posted during review of NVIDIA#5451 (NVIDIA#5451 (comment)), applied verbatim; it was not picked up before that PR merged. Signed-off-by: Rui Zhu <rui.zhu.rz399@yale.edu>
huthvincent
force-pushed
the
fix/B002-cuda-graphs-make-weakref-fallback
branch
from
August 31, 2026 00:46
9d226bc to
5153237
Compare
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.
What does this PR do?
Makes
make_weakref'sexcept RuntimeErrorhandler do what its comment says — keep a strong reference — instead of raising.Two defects in that handler, both on lines the refactor in #5451 left behind:
arg.dtype, but the enclosing function isdef make_weakref(ten, inplace=True)and noargexists in that scope or at module scope, so the handler raisesNameErroron rank 0.wris bound only bywr = make_weak_ref(ten)inside thetry. The handler assigns nothing and falls through toreturn wr, so on any rank other than 0 — where theget_rank() == 0guard skips the warning — it raisesUnboundLocalError.Either way the fallback never happens: instead of accepting a memory overhead, the capture path dies. Before #5451 hoisted this body out of
replace_with_weak_ref(arg), the same handler ended inreturn arg.This is the suggestion posted during review of #5451, applied verbatim: #5451 (comment). It was not picked up before that PR merged, and both names are still broken on
mainand ondev.Issue tracking
For PRs from open-source community contributors:
Linked issue: none — 2-line bug fix. The diagnosis is already on record in #5451 (comment linked above).
Contribution process
Pre-checks
tests/referencesmake_weakref, and the handler needs transformer_engine'smake_weak_refto raise, which is awkward to arrange in a unit test. Happy to add one if you would like it.black --skip-magic-trailing-comma --skip-string-normalization,isortandruff checkall clean on the changed file.How to reproduce, and how we checked it
make_weakrefreturns early unlessHAVE_TE_GRAPHS and torch.is_tensor(ten) and ten.is_from_global_mempool, so reproducing needs TE'smake_weak_refto raise for a tensor that already passed that gate. The handler's own comment names the trigger: "There is a known bug where some dtypes (e.g. torch.float64) are not mapped to a representation in transformer_engine/pytorch/utils.py." One supported configuration puts a float64 tensor on that boundary —--cuda-graph-scope moe_routerwithmoe_router_dtype=fp64, whose router tensor is stampedis_from_global_mempool = Trueatcuda_graphs.py:1436and then passed tomake_weakref.We checked the handler itself on 8xL4 / torch 2.12.0+cu130 by setting
HAVE_TE_GRAPHS = Trueand replacingmake_weak_refwith a function that raisesRuntimeError:NameError: name 'arg' is not definedUnboundLocalError: cannot access local variable 'wr' where it is not associated with a valueWhat we did not do: we did not observe TE's real
make_weak_refraising — transformer_engine is not installed on the machine we tested on, so the gate was simulated in memory and only the handler was exercised. No performance claim is made anywhere in this PR and nothing was benchmarked.The strongest objection to this change
This branch only executes if TE's
make_weak_refraises for a tensor already markedis_from_global_mempool, and no CI job exercises it — so this is a latent crash inside an error handler, and we cannot show anyone has hit it. Two things we deliberately did not fold in, both real and both separate arguments: the baretorch.distributed.get_rank()on line 480 itself raises when no default process group exists, and this module already importslog_single_rank; andexcept RuntimeErroralso catches a failure often.data = wron line 473, which is not the dtype bug the comment names.