From 7f9dba8abc629336f0dbca0aeea841f34193c814 Mon Sep 17 00:00:00 2001 From: daiyu Date: Wed, 14 Oct 2026 23:40:05 +0800 Subject: [PATCH] test(inference): run the real-inference test on CPU and move it out of tests/inference/tests MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Closes #109 - 这条集成测试没有指定 device_map,用的是 inference_transformers 的默认值 "auto"。在有可见 CUDA GPU 的机器上模型会被搬到显卡上,Triton 随后 JIT 编译辅助 kernel,在缺 Python 开发头文件的环境里编不过 (Python.h: No such file or directory),一条本该纯 CPU 的测试直接失败。 现在显式传 device_map="cpu",并用 autouse fixture 把 CUDA_VISIBLE_DEVICES 置空兜底。 - CPU 上跑 fp16 既慢又容易撞到 PyTorch 未实现的 fp16 CPU 算子,这里显式用 dtype="float32"(_resolve_dtype 接受字符串,解析成 torch.float32)。 - 目录从 tests/inference/tests/integration/ 挪到 tests/integration/, 去掉多余的嵌套;仓库里没有任何地方按旧路径引用它,CI 用的是裸 pytest。 - 补上 @pytest.mark.slow(pyproject 里已注册),以及文件末尾缺失的换行。 只动这一个测试文件,未触碰 llmsql/ 下的产品代码。 --- .../test_inference_transformers_real_limit.py | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) rename tests/{inference/tests => }/integration/test_inference_transformers_real_limit.py (77%) diff --git a/tests/inference/tests/integration/test_inference_transformers_real_limit.py b/tests/integration/test_inference_transformers_real_limit.py similarity index 77% rename from tests/inference/tests/integration/test_inference_transformers_real_limit.py rename to tests/integration/test_inference_transformers_real_limit.py index 96be139..2dc46ba 100644 --- a/tests/inference/tests/integration/test_inference_transformers_real_limit.py +++ b/tests/integration/test_inference_transformers_real_limit.py @@ -13,7 +13,21 @@ def _write_jsonl(data, path: Path): f.write(json.dumps(row) + "\n") +@pytest.fixture(autouse=True) +def hide_cuda(monkeypatch): + """ + 把 CUDA 藏起来,保证这条测试真的跑在 CPU 上 + + ``inference_transformers`` 默认 ``device_map="auto"``,在有可见 GPU 的 + 机器上模型会被搬到显卡上,随后 Triton 尝试 JIT 编译辅助 kernel,在缺少 + Python 开发头文件的环境里编不过(``Python.h: No such file or directory``), + 于是这条本该跑 CPU 的集成测试会直接失败。 + """ + monkeypatch.setenv("CUDA_VISIBLE_DEVICES", "") + + @pytest.mark.integration +@pytest.mark.slow def test_real_inference_limit(): """ Real integration test: @@ -67,6 +81,9 @@ def test_real_inference_limit(): max_new_tokens=16, temperature=0.0, do_sample=False, + # 固定放 CPU:device_map="auto" 会把模型扔到可见的 GPU 上 + device_map="cpu", + dtype="float32", ) except (OSError, RuntimeError) as exc: pytest.skip(f"Unable to load/run model in CI: {exc}") @@ -94,4 +111,4 @@ def test_real_inference_limit(): assert "completion" in row assert isinstance(row["question_id"], str) - assert isinstance(row["completion"], str) \ No newline at end of file + assert isinstance(row["completion"], str)