🧩

gptqmodel-pytorch-gil0

gptqmodel-pytorch-gil0 · v
作者 ModelCloud 公开目录收录

简介

Free-threaded (PYTHON_GIL=0) PyTorch safety. Use before writing, reviewing, or debugging any code that calls PyTorch from more than one thread, runs on a 3.13t/3.14t build, or touches CUDA/HIP peer-to-peer copies, streams, or device selection. Requires reading pytorch_gil_0_compat.md first.

通过你的 Agent 添加

安装目录:gptqmodel-pytorch-gil0/(目录名取自 SKILL.md 的 frontmatter name,不是 URL 里的 slug)。

点「快速安装」复制一段指令贴给你的 Agent:它会先下载到 /tmp、把文件清单和校验值给你看,你确认之后才解压安装。不会一步装完、也不改系统其它位置。

或者手动安装

Claude Code~/.claude/skills/gptqmodel-pytorch-gil0/
Codex CLI~/.codex/skills/gptqmodel-pytorch-gil0/
DeepSeek Harness~/.agents/skills/gptqmodel-pytorch-gil0/

包是「根级 SKILL.md + 作者附带的文件」,解压到上面的 <slug>/ 目录即可; 目录名不用换,也不要把包直接解压到 skills 根目录。 技能申请的权限由作者决定:装之前先看清单里有没有脚本、它会读写什么。

技能正文


SPDX-FileCopyrightText: 2026 ModelCloud.ai

SPDX-License-Identifier: Apache-2.0

name: gptqmodel-pytorch-gil0 description: Free-threaded (PYTHON_GIL=0) PyTorch safety. Use before writing, reviewing, or debugging any code that calls PyTorch from more than one thread, runs on a 3.13t/3.14t build, or touches CUDA/HIP peer-to-peer copies, streams, or device selection. Requires reading pytorch_gil_0_compat.md first.

PyTorch on a free-threaded interpreter

At a glance

Check the ledger before using threaded PyTorch. A free-threaded build (3.13t/3.14t, PYTHON_GIL=0) does not make a C extension thread-safe: concurrent torch.cuda.comm.broadcast_coalesced has both hung and returned torn copies even though it works on a GIL build.

Required reading

Before writing or reviewing any PyTorch code that can run on more than one thread, read pytorch_gil_0_compat.md at the repository root. It is the authoritative ledger of every PyTorch API we use, its measured GIL=0 result, and the workaround for each unsafe entry.

If the API you need is not in the ledger, treat it as UNVERIFIED:

  1. Add a probe to scripts/gil0_api_probe.py.
  2. Run it with PYTHON_GIL=0 python scripts/gil0_api_probe.py --only <name> on a free-threaded build with at least two GPUs.
  3. Record the result in pytorch_gil_0_compat.md (table row plus, for an unsafe API, a "Manual findings" entry).

Do not merge a multi-threaded code path that calls an API the ledger does not cover, and never upgrade a row to SAFE based on a single-threaded run. Keep the ledger current in the same change that introduces or removes a threaded PyTorch call; a concurrency finding from review belongs in the ledger, not only in the pull request.

Rules

  • Thread counts and "run threaded or not" decisions come from an explicit argument or the config object, never an environment variable (see AGENTS.md). Configure P2P through BaseQuantizeConfig.p2p (P2PConfig) or GPTQModel.load(..., p2p=...).
  • Never call torch.cuda.comm.* from worker threads. Use gptqmodel.utils.p2p.peer_copy_batch, which drives cudaMemcpyPeerAsync/hipMemcpyPeerAsync through ctypes (the GIL is released for the call).
  • torch.nn.parallel.replicate internally calls broadcast_coalesced. It is serialized by _THREAD_SAFE_PARALLEL in gptqmodel/utils/looper_helpers.py, but that only prevents concurrent entry; it does not make the entry point safe. clone_module_for_devices skips it when the GIL is disabled and uses the deepcopy + move_module_to_device path. use_torch_replicate is off by default; enable it only after qualifying replicate for the target build.
  • torch.cuda.set_device is thread-local. Set it at the top of every worker; never inherit the device of the thread that spawned it.
  • Join work with per-thread streams and events. Avoid torch.cuda.synchronize(), which waits for the whole device and serializes unrelated workers.
  • After a failed runtime call, drain the sticky error with cudaGetLastError/hipGetLastError (gptqmodel.utils.p2p._clear_last_error_for_backend). HIP leaves error 704 (peer access is already enabled) behind otherwise, and the next unrelated PyTorch call raises it.
  • Prefer copies that are already device-to-device. Host staging through cpu() is correct but defeats P2P and doubles traffic.
  • Guard every fast path with a fallback that is correct without the fast path, and test the fallback.

Verifying

A concurrency claim needs evidence from a free-threaded interpreter, not from a GIL build:

PYTHON_GIL=0 python -c "import sys; assert sys._is_gil_enabled() is False"
PYTHON_GIL=0 python -m pytest tests/test_p2p_copy_utils.py -q
PYTHON_GIL=0 python scripts/gil0_api_probe.py --all --json /tmp/gil0.json

Hangs do not raise, so run concurrency tests under a timeout and keep the probe-per-subprocess pattern. A test that passes once proves nothing; run it several times before calling an API safe.

正文来自原始仓库, 版权归作者所有,许可:Apache-2.0。 权利人认为有不妥,可申请下架。

原始仓库

作者的项目地址:https://github.com/ModelCloud/GPTQModel

标签

没有标签

同类 Skill