Skip to content

fix(triton): resolve autotuner future on pruning exceptions (#4477) - #4527

Merged
Qubitium merged 2 commits into
mainfrom
fix/issue-4477-autotuner-prune-futures
Nov 17, 2026
Merged

Qubitium merged 2 commits into
mainfrom
fix/issue-4477-autotuner-prune-futures

Conversation

@Qubitium

Copy link
Copy Markdown
Collaborator

Fixes #4477

TL;DR

_get_config_for_key published a per-key CacheFuture and then ran
prune_configs() before entering the try/finally that resolves the future.
A pruning exception escaped with the future still pending, so every waiter and
every same-key retry parked on future.event.wait() forever. Pruning now runs
inside the owner-finalization scope (+30/−13 in nogil_patcher.py).

1. The problem

flowchart TD
    A["_get_config_for_key(key)"] --> B{"_cache has key"}
    B -- yes --> C["return cached config"]
    B -- no --> D{"_cache_futures has key"}
    D -- yes --> E["waiter: future.event.wait (no timeout)"]
    D -- no --> F["owner: publish CacheFuture"]
    F --> G["prune_configs(kwargs)"]
    G -- "raises" --> H["exception escapes: future retained, Event unset, error None"]
    H --> I["waiters and same-key retries block forever"]
    G -- ok --> J["try: benchmark / cache / return"]
Loading
Mechanism Before After
Owner scope prune_configs at utils/nogil_patcher.py:355 (461b42023), outside the try at :369 prune_configs is the first statement inside the owner try
Pruning exception future retained in _cache_futures, future.event unset, future.error unset future.error set, future.event set, key popped in finally
Same-key retry reuses the pending future and blocks forever becomes a new owner and completes
Waiter future.event.wait() with no timeout wakes and re-raises the owner's error

2. The new flow

flowchart TD
    A["_get_config_for_key(key)"] --> B{"_cache has key"}
    B -- yes --> C["return cached config"]
    B -- no --> D{"_cache_futures has key"}
    D -- yes --> E["waiter: future.event.wait"]
    E --> F{"future.error set"}
    F -- yes --> G["raise future.error"]
    F -- no --> H["return future.config"]
    D -- no --> I["owner: publish CacheFuture"]
    I --> J["try: prune_configs(kwargs)"]
    J -- "raises" --> K["except BaseException: future.error = exc"]
    J -- ok --> L["benchmark, disk cache, store best config"]
    L --> M["return best config"]
    K --> N["finally: future.event.set"]
    M --> N
    N --> O["finally: _cache_futures.pop(key)"]
Loading

3. Failure contract

Situation Result
prune_configs raises future error recorded, Event set, key removed; waiters re-raise, next call retries
benchmark or disk-cache raises same path (already covered by the existing except BaseException)
owner succeeds best config cached, future resolved, key removed

4. Tests / guarantees

Test Invariant
tests/test_triton_patch_api.py::test_patched_autotuner_pruning_exception_resolves_future after a pruning ValueError: future.error is exc_info.value, future.event.is_set(), a same-key retry completes as a new owner, and _cache_futures is empty

5. Validation

All runs with OMP_NUM_THREADS=4 MKL_NUM_THREADS=4 OPENBLAS_NUM_THREADS=4 NUMEXPR_NUM_THREADS=4.

Before the fix, the new test fails with the user-visible symptom:

Failed: same-key retry blocked after a pruning exception

After the fix:

# CUDA venv, full related test file
PYTHONPATH=$PWD CUDA_VISIBLE_DEVICES=GPU-d458708b-c760-bbf1-08fb-8b8803a2157d \
  /opt/venvs/cu130t/bin/python -m pytest tests/test_triton_patch_api.py -q
# 14 passed, 1 warning in 3.22s

# ROCm venv, full related test file
PYTHONPATH=$PWD HIP_VISIBLE_DEVICES=2 \
  /opt/venvs/rocm101t/bin/python -m pytest tests/test_triton_patch_api.py -q
# 14 passed, 1 warning in 4.20s

# free-threaded runs of the new test (CUDA venv, PYTHON_GIL=0)
# 5/5 passed before the origin/main merge, 3/3 after it

Issue probe (the shipped CacheFuture / _get_config_for_key executed via AST,
with a fake Autotuner whose prune_configs raises):

Before:

retained, signaled, error: True False None
same-key retry blocked: True

After:

retained after failure: False
signaled: True
error: ValueError
same-key retry blocked: False
retry returned config: True

ruff check gptqmodel docs/eora tests setup.py -> All checks passed!
The branch was merged with git merge --no-edit origin/main (main at
76272a7fb) and ruff plus both test-file runs were repeated with the same
results.

6. Hardware / limitations

  • CUDA: RTX 4090 GPU-d458708b-c760-bbf1-08fb-8b8803a2157d; ROCm: AMD R9700
    (HIP_VISIBLE_DEVICES=2).
  • No real Triton kernel or GPU launch was executed for this fix. The unit test
    installs the shipped patch on a fake Autotuner; the probe executes the
    shipped CacheFuture and _get_config_for_key through AST with a fake
    object. The defect and the fix are pure Python future-lifecycle control flow.
  • tests/test_triton_patch_api.py::test_patched_autotuner_lazily_initializes_concurrent_first_lookup
    is a known ~1/5 pre-existing flake; it passed in every run here and is not
    claimed as a regression.
  • Reference page: docs/concurrency.md (section 2, "Autotuner single-flight
    futures resolve on every owner exit"), linked from README.md.

_get_config_for_key published the per-key CacheFuture and then called
prune_configs before entering the try/finally that sets the future Event
and removes the key. A pruning exception therefore left the future pending:
waiters blocked forever on future.event.wait() and same-key retries reused
the unresolved future.

Move prune_configs inside the owner-finalization try so every exit path
records the error, signals the Event, and pops the key. Add a regression
test and document the finalization contract.
@Qubitium
Qubitium merged commit 336946e into main Nov 17, 2026
6 checks passed
@Qubitium
Qubitium deleted the fix/issue-4477-autotuner-prune-futures branch November 17, 2026 01:02
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.

[BUG] Triton autotuner pruning exceptions leave unresolved futures and block subsequent calls

1 participant