Skip to content

PTQ-Level Efficiency, QAT-Level Accuracy | 训练后量化的效率,量化感知训练的精度

Enable torch.compile for selected tests and update related configurations (#2124)

Xin Hea2f6b3161ab3
diff --git a/pyproject.toml b/pyproject.toml
index e1978e006..5d351b0b9 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -5,6 +5,7 @@ build-backend = "setuptools.build_meta"
[tool.pytest.ini_options]
markers = [
"perf: performance benchmark tests (excluded from default CI runs; select with `-m perf`)",
+ "enable_torch_compile: opt in to real torch.compile for tests that need compile behavior",
]
[tool.codespell]
diff --git a/test/conftest.py b/test/conftest.py
index 060b7e9a1..34aa6d90a 100644
--- a/test/conftest.py
+++ b/test/conftest.py
@@ -52,6 +52,11 @@ def pytest_configure(config):
pytest.mode = config.getoption("--mode")
assert pytest.mode.lower() in ["lazy", "compile"]
+ config.addinivalue_line(
+ "markers",
+ "enable_torch_compile: allow this test to use real torch.compile instead of the default no-op patch",
+ )
+
config.stash[backup_env] = os.environ
if pytest.mode == "lazy":
@@ -64,3 +69,20 @@ def pytest_configure(config):
def pytest_unconfigure(config):
os.environ.clear()
os.environ.update(config.stash[backup_env])
+
+
[email protected](autouse=True)
+def disable_torch_compile_by_default(request, monkeypatch):
+ """Use a no-op torch.compile by default to reduce test overhead and flakiness.
+
+ Mark tests with ``@pytest.mark.enable_torch_compile`` to opt in to real torch.compile.
+ """
+ if request.node.get_closest_marker("enable_torch_compile"):
+ return
+
+ try:
+ import torch
+ except Exception:
+ return
+
+ monkeypatch.setattr(torch, "compile", lambda function, *args, **kwargs: function, raising=False)
diff --git a/test/test_cpu/conftest.py b/test/test_cpu/conftest.py
index 9cd601db0..510ebc2e9 100644
--- a/test/test_cpu/conftest.py
+++ b/test/test_cpu/conftest.py
@@ -1,8 +1,6 @@
-import pytest
-import torch
+"""CPU test-specific fixtures.
-
[email protected](autouse=True)
-def disable_torch_compile(monkeypatch):
- """Skip torch.compile overhead in CPU tests."""
- monkeypatch.setattr(torch, "compile", lambda function, *args, **kwargs: function)
+torch.compile behavior is controlled in test/conftest.py:
+- default: disabled for all tests via a no-op patch
+- opt in: use @pytest.mark.enable_torch_compile
+"""
diff --git a/test/test_cuda/integrations/test_vllm.py b/test/test_cuda/integrations/test_vllm.py
index c4aa4e5ab..8646bfbd6 100644
--- a/test/test_cuda/integrations/test_vllm.py
+++ b/test/test_cuda/integrations/test_vllm.py
@@ -41,10 +41,13 @@ def _is_sm12_with_old_cuda() -> bool:
return False
-pytestmark = pytest.mark.skipif(
- _is_sm12_with_old_cuda(),
- reason="SM 12.x (Blackwell) GPU requires CUDA >= 12.9 for vLLM GPTQ marlin JIT kernels",
-)
+pytestmark = [
+ pytest.mark.skipif(
+ _is_sm12_with_old_cuda(),
+ reason="SM 12.x (Blackwell) GPU requires CUDA >= 12.9 for vLLM GPTQ marlin JIT kernels",
+ ),
+ pytest.mark.enable_torch_compile,
+]
MODELS = [
"OPEA/Qwen2.5-0.5B-Instruct-int4-sym-inc", ##auto_round:auto_gptq
diff --git a/test/test_cuda/quantization/test_torch_compile.py b/test/test_cuda/quantization/test_torch_compile.py
index 07e1117fa..ec61870c7 100644
--- a/test/test_cuda/quantization/test_torch_compile.py
+++ b/test/test_cuda/quantization/test_torch_compile.py
@@ -13,6 +13,8 @@ from auto_round.compressors.utils import block_forward
from ...envs import require_gguf
from ...helpers import get_model_path, get_tiny_model
+pytestmark = pytest.mark.enable_torch_compile
+
class TestTorchCompile:
save_dir = "./saved"