Repository navigation
fix(threadx): give every device-pool worker its own intra-op team (#4356) - #4390
Conversation
) libgomp creates one OpenMP team per pthread that enters a parallel region, sized from that thread's intra-op count. Device pool workers inherited the spawning thread's count, so the pool multiplied the process-wide team: the default pool on a 320-CPU host held 2622 OS threads for 64 submitted CPU tasks, and a 2-layer GPTQ quantize() run held 2943. Capping the number of workers does not bound the team size. DeviceThreadPool now takes intraop_threads_per_worker and applies it once at worker start, before the worker's first task or warmup, because that is when the team is materialized. The default (None) splits torch.get_num_threads() across the pool's worker count via split_intraop_threads(), which never returns less than 1; 0 keeps the inherited pool. The value comes from KernelConfig.pool_intraop_threads, not an environment variable. Measured on the same host and workload: the isolated repro goes from 2622 to 308 threads and the quantize() run from 2943 to 833. The residual growth is the tokenizer rayon pool, which torch.set_num_threads does not control. torch.set_num_threads is thread-local for the intra-op pool, so the caller's pool is untouched; the GIL=0 ledger row and probe (scripts/gil0_api_probe.py --only 'torch.set_num_threads (per worker)') pin that on CUDA and ROCm. Fixes #4356
|
Note on the The findings are in |
TL;DR
DeviceThreadPoolnow assigns each worker thread its own Torch intra-op poolsize, taken from
KernelConfig.pool_intraop_threads(None= split theprocess budget across the pool's workers). libgomp gives every pthread that
enters a parallel region its own OpenMP team, so without this the pool
multiplied the process-wide team by its worker count.
Net change: 9 files, +276/-1. One new
DeviceThreadPoolkeyword argument, onenew
KernelConfigfield, one regression test file extension, one reference page.Measured on the same host and workload, before → after:
quantize()torch.get_num_threads()on the caller stays 206 in both cases.Fixes #4356.
1. The problem
_default_cpu_workers()capped only the number of CPU workers(
gptqmodel/__init__.py:391). Nothing capped the team size, and workerthreads inherited the spawning thread's intra-op count.
flowchart TD A["main thread: torch.get_num_threads() = 206<br/>on a 320-CPU host"] --> B["_build_device_thread_pool()<br/>gptqmodel/__init__.py:420"] B --> C["DeviceThreadPool.__init__<br/>workers = {'cpu': 12, 'cuda:per': 4, ...}"] C --> D["12 x _DeviceWorker threads<br/>gptqmodel/utils/threadx.py"] D --> E["worker runs matmul"] E --> F["libgomp creates a team of 206<br/>for that pthread"] F --> G["12 x 206 + caller = 2622 OS threads"] style G fill:#fdd,stroke:#900Mechanisms being removed:
gptqmodel/__init__.py:399-405(_default_cpu_workers)gptqmodel/utils/threadx.py:398(_DeviceWorker), noset_num_threadsanywhere in the poolgptqmodel/__init__.py:401(has_gil_disabled()guard)A single
torch.set_num_threadsat import is not an option:import gptqmodeldeliberately does not change a user's global pool
(
gptqmodel/utils/threads.py:79-99,docs/thread-caps.md).2. The new flow
flowchart TD A["KernelConfig.pool_intraop_threads<br/>gptqmodel/utils/kernel.py:248"] --> B["_build_device_thread_pool()<br/>gptqmodel/__init__.py:419"] B --> C["DeviceThreadPool(intraop_threads_per_worker=...)<br/>gptqmodel/utils/threadx.py:739"] C --> D{"request is None?"} D -- yes --> E["count pool workers from the worker table<br/>threadx.py:906-920"] E --> F["split_intraop_threads(pool_workers, torch.get_num_threads())<br/>threadx.py:398"] D -- no --> G["use the explicit value<br/>(0 keeps the inherited pool)"] F --> H["self._intraop_threads_per_worker"] G --> H H --> I["_spawn_worker(intraop_threads=...)<br/>threadx.py:1243"] I --> J["_DeviceWorker thread start<br/>threadx.py:584 _apply_intraop_threads()"] J --> K{"torch.get_num_threads() == target?"} K -- yes --> L["no call"] K -- no --> M["torch.set_num_threads(target)<br/>before queue loop, warmup, first task"] M --> N["this pthread's team is `target`"] L --> N N --> O["caller pool unchanged<br/>(thread-local)"] style O fill:#dfd,stroke:#060 style F fill:#dfd,stroke:#060 style M fill:#dfd,stroke:#060The order matters:
_apply_intraop_threads()runs atthreadx.py:584, abovethe queue loop, so it precedes both the worker's warmup (
_run_warmup()) andits first task. The team is created on the first parallel region, so setting it
later would be a no-op.
split_intraop_threads()returnsmax(1, total // worker_count): a pool withmore workers than the process budget still gives each worker a valid team.
3. Contract
intraop_threads_per_worker=None(default, andKernelConfig.pool_intraop_threads=None)torch.get_num_threads()across this pool's workersintraop_threads_per_worker=n,n > 0nintraop_threads_per_worker=0(KernelConfig.pool_intraop_threads=0)torch.set_num_threadsraisesThe value is resolved once when the pool is built. A worker restart does not
re-resolve it, and a later
torch.set_num_threadsby the caller does notpropagate into existing workers.
4. Failure and ambiguity contract
stateDiagram-v2 [*] --> Request: pool built Request --> Split: request is None Request --> Explicit: request > 0 Request --> Inherit: request == 0 Split --> Applied: split_intraop_threads >= 1 Explicit --> Applied: set_num_threads(target) Inherit --> Inherit: no call (documented opt-out) Applied --> Applied: team bounded by target Applied --> InheritWarn: set_num_threads raised InheritWarn --> InheritWarn: warn once, tasks still run style InheritWarn fill:#fdd,stroke:#900 style Inherit fill:#ffd,stroke:#9905. Change inventory
gptqmodel/utils/threadx.pysplit_intraop_threads(),intraop_threads_per_worker,_DeviceWorker._apply_intraop_threads()gptqmodel/utils/kernel.pyKernelConfig.pool_intraop_threads(Optional[int], minimum 0)gptqmodel/__init__.pyget_kernel_config().pool_intraop_threadstests/test_thread_caps.py0pass-through, KernelConfig wiringpytorch_gil_0_compat.mdtorch.set_num_threads/get_num_threadsrow with measured resultscripts/gil0_api_probe.pytorch.set_num_threads (per worker)probedocs/device-thread-pool-intraop-budget.md(new),docs/thread-caps.md,docs/kernel-config.md,docs/README.md,README.mdNo environment variable is involved: the policy is a
KernelConfigfield, perAGENTS.mdand the direction of #4180.6. Tests
test_pool_workers_get_split_intraop_budgetsplit_intraop_threadsarithmetic, including theworkers > budgetfloor of 1test_device_thread_pool_workers_cap_their_own_intraop_teamtest_device_thread_pool_explicit_worker_intraop_overrideintraop_threads_per_worker=1is appliedtest_device_thread_pool_can_keep_the_caller_intraop_pool0means "do not touch the worker's pool"test_kernel_config_pool_intraop_threads_drives_the_default_pool_build_device_thread_pool()reads the policy for both3and0scripts/gil0_api_probe.py --only 'torch.set_num_threads (per worker)'7. Validation
test_thread_caps,test_threadx,test_threadx_cpu,test_threadx_cpu_locks,test_threadx_shutdown,test_threadx_error_propagation,test_threadx_warmup_accounting,test_threadpoolctl,test_cpu_pin,test_excluded_device_warmup,test_linalg_warmup,test_quant_telemetry,test_stream,test_thread_pool_exittests/test_thread_caps.pywithOMP_NUM_THREADS=8ruff checkon changed filesquantize()+ pool repro8. Reviewer notes
quantize()run (833) is dominated by the HFtokenizers rayon pool, which sizes itself independently of
torch.set_num_threads. It is out of scope here and called out in the newdocs page so the next reader does not attribute it to this pool.
_SyncWorker(the inline fallback used when threads are unsafe) isintentionally untouched: it runs on the caller's thread, so changing the
intra-op count there would leak into the caller's own pool.
task would let a
quantize()that changes the caller's pool silently changeworker teams mid-run, which is a larger behavioural change than this fix.