Skip to content

Fix race condition in runAtenTest #9306

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

benawilson
Copy link

The logic in runAtenTest has a race condition that can trigger if the "x.to(device)" is delayed, such as by acquiring a lock on the XLA device. In this case, the host-to-XLA device transfer may not start until after the test function has executed on the host CPU.

For test functions which mutate their inputs, such as test_diagonal_write_transposed_r3, this can result in the test fn being applied twice; once from the invocation of fn(*tensors) on CPU, and then a second time from the invocation of fn(*xla_tensors). Empirically, this happens at a rate of approximately 0.03% (about 1 in 3000 executions).

Adding a full clone on the CPU before calling the test function ensures that the CPU invocation and the XLA invocation will not interfere even if they execute in a different order, eliminating the race condition. This was verified by 1 million successful sequential executions of test_diagonal_write_transposed_r3.

@benawilson
Copy link
Author

benawilson commented Jun 6, 2025

This addresses #https://github.com/pytorch/xla/issues/8985, and may also address other flaky tests that use in-place mutations.

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.

1 participant