Skip to content

[GIL=0] DeviceThreadPool os._exit on task error, Triton autotuner nargs race, retained workspace and env mutation #3972

Closed

Description

@Qubitium

Summary

Three issues affect free-threaded (PYTHON_GIL=0) and multi-worker use: process lifetime, shared Triton state, and GPU memory retention. None of them duplicates #3927, #3947, #3951, #3878 or #2441. #2441 was about _cache_lock missing on Autotuner instances created before the patch. Audited at a16977e.

Findings

1. Any exception in a DeviceThreadPool task calls os._exit(1) (High)

  • Code: gptqmodel/utils/threadx.py.
    • Task errors: _DeviceWorker._run (556-562) runs except BaseException → fut.set_exception(exc) → self._abort_process(exc).
    • Warmup failures: lines 531-535 do the same.
    • _abort_process (570-581) prints the traceback and calls os._exit(1), falling back to os.system("kill -9 <pid>").
  • What triggers it: any error from a submitted task. Examples include torch.OutOfMemoryError, a validation ValueError, a CUDA error in event.synchronize(), and an OSError from the async log writer.
  • Where tasks are submitted:
    • looper/stage_subset.py:1541
    • looper/stage_layer.py:1305
    • looper/forward_executor.py:708-735
    • looper/loop_processor.py:567
    • utils/stream.py:212
    • linalg warmups
  • Consequences:
    • A host app that embeds quantize() (Jupyter kernel, web service, training script) is killed without unwinding.
    • atexit handlers do not run, including gptqmodel's own pool join (__init__.py:302) and the tempdir rmtree (utils/structure.py:1157).
    • finally blocks and checkpoint signal restore (gptq_checkpoint.py:266-279) are skipped.
    • The caller's try/except can never catch the error.
    • The library's own handling in stage_layer._drain_finalize_futures (1371-1378) and stage_subset._collect_worker_results becomes dead code, because whether the main thread observes the future first is a race.
  • Inconsistencies:
    • _SyncWorker.submit (605-627) only sets the exception and does not abort.
    • tests/test_threadx.py:801 monkeypatches _abort_process to a no-op, which hides the behaviour.
  • Fix:
    • Drop _abort_process from the task path and propagate errors through the future.
    • For warmup failures, fail the pending future instead of exiting.
    • If fail-fast is wanted, make it an explicit config option.

2. GIL=0 Triton autotuner patch shares self.nargs across threads (Medium)

  • The patch is process-wide: gptqmodel/__init__.py:69 TritonPatch.apply() replaces Autotuner.run for every @triton.autotune kernel in the process, including third-party ones (fla, Liger).
  • The race: utils/nogil_patcher.py:257 sets self.nargs = nargs and :291 sets self.nargs = None on the shared Autotuner, without a lock. best_config (271) is written the same way.
  • Who reads it: upstream Triton reads self.nargs inside _bench (full_nargs = {**self.nargs, ...}), prune_configs (early_config_prune(self.configs, self.nargs)) and perf_model. The patch's own benchmark() reads it at line 220.
  • What happens:
    1. Thread A benchmarks a cold key.
    2. Thread B finishes a call to the same kernel and resets nargs = None.
    3. A raises TypeError: 'NoneType' object is not a mapping.
    4. If B is still running instead, A benchmarks, prunes and runs pre_hook / reset_to_zero / restore_value against B's tensors. That gives silent corruption, or a config chosen from the wrong shapes and then cached.
  • Concurrency path: forward_executor.py:708-735 runs the same layer class on several GPUs at once.
  • Fix: keep nargs in thread-local storage, or pass it explicitly to _bench / prune_configs, and stop mutating the shared attribute. At minimum, hold a per-Autotuner lock across benchmark plus run for cold keys.
  • Same pattern in gptqmodel's own autotuner: nn_modules/triton_utils/custom_autotune.py:91-123 (GGUF Triton kernels) has an unlocked check-then-set on self.cache and the same shared self.nargs.

3. Low

  • GPTQ Hessian workspace pool is never released:
    • quantization/gptq.py:110-111 defines the module-level _WORKSPACE_SLOTS / _WORKSPACE_CACHE, filled by _lease_workspace (231-289).
    • It keeps up to 8 fp32 slots plus staging slots per (device, dtype), and they survive quantize() returning and model deletion.
    • It is reached when HessianConfig.chunk_size / chunk_bytes is set.
    • torch.cuda.empty_cache() cannot reclaim the memory.
    • Fix: clear the pool in the looper finalize / torch_empty_cache path, or expose a release API.
  • os.environ is changed process-wide during JIT builds:
    • utils/cpp.py:951-969 _temporary_merged_cuda_arch_override rewrites TORCH_CUDA_ARCH_LIST for the whole load() (1911-1914, which can take minutes).
    • load_pack_block_extension / load_floatx_cpu_extension set and pop GPTQMODEL_EXT_VERBOSE (2193-2203, 2229-2237).
    • Concurrent torch, triton and inductor compiles in pool workers see the temporary values.
    • In glibc, setenv racing with native getenv is undefined behaviour.
    • Fix: pass arch flags through extra_cuda_cflags or a copied env instead.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions