From a3a3339c97329634a42aaa5e61c2b028bd861ab1 Mon Sep 17 00:00:00 2001 From: Benedykt Bela Date: Tue, 12 May 2026 15:59:37 +0000 Subject: [PATCH] Fix flaky reentrant backward test by passing explicit grad for non-scalar output (#179704) Fixes #179703 More detailed description of the problem is in the issue #179703 ## What this PR changes In `test/test_autograd.py`, `_test_reentrant_parent_error_on_cpu` [(THIS LINE)](https://github.com/pytorch/pytorch/blob/3b39608061aac9da750ad3954197400b191286c3/test/test_autograd.py#L12574) defines a reentrant autograd function whose backward currently does: ```python reentrant_root.backward() ``` This PR changes it to: ```python reentrant_root.backward(grad) ``` ## Why this solution `reentrant_root` is non-scalar (`[3, 3]`). For non-scalar outputs, `.backward()` requires an explicit gradient seed. Calling `.backward()` without that seed can raise: `RuntimeError: grad can be implicitly created only for scalar outputs` ## Effect on test behavior Before this change, the test had two competing error sources: 1. Intended error: `"Simulate error"` from `SimulateBackwardError` 2. Accidental error: non-scalar `.backward()` without explicit grad Because execution order is nondeterministic, CI could intermittently fail when (2) happened first. After this change, the accidental error source is removed. The test now consistently validates only the intended behavior. Pull Request resolved: https://github.com/pytorch/pytorch/pull/179704 Approved by: https://github.com/jeffdaily (cherry picked from commit 24cdfbdd4ce3d3b080e64e6a8b03a8ecda547bd0) --- test/test_autograd.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/test_autograd.py b/test/test_autograd.py index dbd1454ff7459..76fe65ea86b17 100644 --- a/test/test_autograd.py +++ b/test/test_autograd.py @@ -11755,7 +11755,7 @@ def forward(ctx, inp): @staticmethod def backward(ctx, grad): # Reentrant backward in child will take much longer. - reentrant_root.backward() + reentrant_root.backward(grad) return grad # Parent gpu graph.