Skip to content

[release/2.9] Fix flaky reentrant backward test by passing explicit grad for non-scalar output (#179704)#3418

Open
darren-amd wants to merge 1 commit into
release/2.9from
users/darren-amd/cherrypick-flaky-reentrant-fix
Open

[release/2.9] Fix flaky reentrant backward test by passing explicit grad for non-scalar output (#179704)#3418
darren-amd wants to merge 1 commit into
release/2.9from
users/darren-amd/cherrypick-flaky-reentrant-fix

Conversation

@darren-amd

Copy link
Copy Markdown

Cherry-pick of upstream pytorch#179704 (commit 24cdfbd) into release/2.9.

Why

test/test_autograd.py::TestAutogradDeviceTypeCUDA::test_reentrant_parent_error_on_cpu_cuda is flaky on ROCm Linux CI. ReentrantFunc.backward() calls reentrant_root.backward() on a non-scalar [3, 3] tensor with no explicit gradient, which is always invalid. This races two errors through the autograd engine: the intended "Simulate error" and an accidental RuntimeError: grad can be implicitly created only for scalar outputs. On ROCm the accidental error wins, so assertRaisesRegex("Simulate error") fails.

The one-line fix (reentrant_root.backward() -> reentrant_root.backward(grad)) removes the accidental error source and makes the test deterministic. Already present on upstream main and release/2.10; this backports it to release/2.9.

Change

test/test_autograd.py - 1 line, inside _test_reentrant_parent_error_on_cpu.

Fixes ROCm/TheRock#6170

…alar output (pytorch#179704)

Fixes pytorch#179703

More detailed description of the problem is in the issue pytorch#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: pytorch#179704
Approved by: https://github.com/jeffdaily

(cherry picked from commit 24cdfbd)
@darren-amd darren-amd requested a review from jithunnair-amd July 9, 2026 17:20
@rocm-repo-management-api

rocm-repo-management-api Bot commented Jul 9, 2026

Copy link
Copy Markdown

Jenkins build for a3a3339c97329634a42aaa5e61c2b028bd861ab1 commit finished as FAILURE
Links: Pipeline Overview / Build artifacts / Test Results

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants