Skip to content

fix(python): isolate the torch.compile cache per xdist worker - #9355

Open
jackylee-ch wants to merge 1 commit into
lance-format:mainfrom
jackylee-ch:fix/isolate-torch-inductor-cache
Open

jackylee-ch wants to merge 1 commit into
lance-format:mainfrom
jackylee-ch:fix/isolate-torch-inductor-cache

Conversation

@jackylee-ch

Copy link
Copy Markdown
Contributor

test_torch_index_with_nans fails intermittently on the Windows python job with PermissionError: [Errno 13] under torchinductor_runneradmin, now on two consecutive runs of one branch.

lance.torch.distance decorates six kernels with @torch.compile, the suite runs with PYTEST_WORKERS=auto, and nothing sets TORCHINDUCTOR_CACHE_DIR, so every worker reaching the torch accelerator compiles those six into torch's single default cache directory. Inductor writes a generated module by replacing it, which POSIX permits while another process holds it open and Windows refuses -- hence only this job.

Point each worker at its own subdirectory; serial runs keep torch's default.

Testing

uv run make lint from python/. A throwaway test asserting each worker's TORCHINDUCTOR_CACHE_DIR ends in its worker id passes under -n 2 and fails with the helper disabled. I cannot reproduce the Windows failure on macOS, so the race being the cause stays inferred.

#8655 also adds an os.environ line at the top of pytest_configure; these two will conflict.

`test_torch_index_with_nans` fails intermittently on the Windows python job with
`PermissionError: [Errno 13]` on a path under `torchinductor_runneradmin`. It has
now reproduced on two consecutive runs of the same branch, once per parametrized
case, so it is not a one-off.

`lance.torch.distance` decorates six kernels with `@torch.compile`, the suite runs
with `PYTEST_WORKERS=auto`, and nothing sets `TORCHINDUCTOR_CACHE_DIR`. Every
worker that reaches the torch accelerator therefore compiles the same six
functions into torch's single default cache directory. Inductor writes a generated
module by replacing it, which POSIX permits while another process holds it open
and Windows refuses -- which is why only this job is affected.

Point each worker at its own subdirectory. Serial runs keep torch's default so the
cache stays warm, and the per-worker directories share one temp root for the same
reason. `TORCH_COMPILE_DISABLE` would also stop the race but would drop coverage of
those six kernels, which the Windows job sets up MSVC specifically to exercise.

## Testing

`uv run make lint` from `python/`. Verified the mechanism with a throwaway test
asserting that each worker's `TORCHINDUCTOR_CACHE_DIR` ends in its own worker id:
under `-n 2` the two workers get `.../lance_torchinductor/gw0` and `.../gw1`, and
with the helper disabled the variable is unset for both. I cannot reproduce the
Windows failure itself on macOS, so that the race is what fails there remains
inferred from the log and the configuration above.
@github-actions github-actions Bot added A-python Python bindings bug Something isn't working labels Sep 17, 2026

@lance-gatekeeper lance-gatekeeper Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gate recommendation: request changes.

The per-worker cache strategy addresses the Windows xdist race and preserves serial runs and explicit cache roots. Please make the fix merge-ready by adding durable, lightweight regression coverage for the helper mapping; reproducing Windows file locking is unnecessary.

)

workerinput = getattr(config, "workerinput", None)
_isolate_torch_inductor_cache(workerinput)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This bug fix has no committed regression test, so the worker isolation can regress without detection and the current head does not meet the repository requirement that every bug fix have corresponding tests. Please add a lightweight test for _isolate_torch_inductor_cache covering None (serial execution leaves the environment unchanged), two worker IDs (distinct child directories), and a pre-set cache root. Those mapping cases exercise the contract directly without needing to reproduce Windows file locking.

@lance-gatekeeper lance-gatekeeper Bot added the K-changes Latest Gatekeeper recommendation requests changes. label Sep 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-python Python bindings bug Something isn't working K-changes Latest Gatekeeper recommendation requests changes.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant