Verified Solution[pytorch/pytorch] inductor sam accuracy test very flaky
Sponsored Content
### ROOT CAUSE
The flakiness is likely due to non-deterministic behavior from uninitialized CUDA devices or race conditions in the test setup. The test may be running on a GPU with inconsistent states between runs, causing accuracy variations.
### CODE FIX
```python
# Before test execution
import torch
torch.use_deterministic_algorithms(True)
torch.backends.cudnn.benchmark = False
torch.backends.cudnn.deterministic = True
# Ensure all GPUs are cleared
for i in range(torch.cuda.device_count()):
torch.cuda.set_device(i)
torch.cuda.empty_cache()
# Add explicit seed setting in the test
torch.manual_seed(42)
torch.cuda.manual_seed_all(42)
```
This ensures deterministic operations and clears GPU state before each test run.
Deploy on DigitalOcean ($200 Credit)
Related Fixes
Race condition in thread pool
[pytorch/pytorch] torch.compile error in unit tests, but test passes when ran individually
[golang/go] testing: tests killed with "signal: killed" on go1.26.1 (regression from go1.25.7)