Skip to content

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

[fix]AttributeError: 'Autotuner' object has no attribute '_cache_lock' (#1414)

Xin He6093abc28b2c
diff --git a/auto_round_extension/triton/triton_utils/dequant.py b/auto_round_extension/triton/triton_utils/dequant.py
index 404819159..d599289db 100644
--- a/auto_round_extension/triton/triton_utils/dequant.py
+++ b/auto_round_extension/triton/triton_utils/dequant.py
@@ -34,6 +34,7 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
import itertools
+import threading
import torch
import triton
@@ -49,6 +50,18 @@ def make_dequant_configs(block_sizes, num_warps):
DEFAULT_DEQUANT_CONFIGS = make_dequant_configs([128, 256, 512, 1024], [4, 8])
+# Patch Autotuner if it's missing _cache_lock (compatibility fix for older triton versions)
+try:
+ from triton.runtime.autotuner import Autotuner
+
+ if not hasattr(Autotuner, "_cache_lock"):
+ # Add missing attributes to make autotune work
+ Autotuner._cache_lock = threading.RLock()
+ if not hasattr(Autotuner, "cache"):
+ Autotuner.cache = {}
+except (ImportError, AttributeError):
+ pass
+
@triton.autotune(DEFAULT_DEQUANT_CONFIGS, key=["numels"])
@triton.jit
diff --git a/auto_round_extension/triton/triton_utils_zp/dequant.py b/auto_round_extension/triton/triton_utils_zp/dequant.py
index 7d1e63091..288d27ea8 100644
--- a/auto_round_extension/triton/triton_utils_zp/dequant.py
+++ b/auto_round_extension/triton/triton_utils_zp/dequant.py
@@ -34,6 +34,7 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
import itertools
+import threading
import torch
import triton
@@ -49,6 +50,18 @@ def make_dequant_configs(block_sizes, num_warps):
DEFAULT_DEQUANT_CONFIGS = make_dequant_configs([128, 256, 512, 1024], [4, 8])
+# Patch Autotuner if it's missing _cache_lock (compatibility fix for older triton versions)
+try:
+ from triton.runtime.autotuner import Autotuner
+
+ if not hasattr(Autotuner, "_cache_lock"):
+ # Add missing attributes to make autotune work
+ Autotuner._cache_lock = threading.RLock()
+ if not hasattr(Autotuner, "cache"):
+ Autotuner.cache = {}
+except (ImportError, AttributeError):
+ pass
+
@triton.autotune(DEFAULT_DEQUANT_CONFIGS, key=["numels"])
@triton.jit
diff --git a/test/test_cuda/schemes/test_auto_scheme.py b/test/test_cuda/schemes/test_auto_scheme.py
index b91c98428..7b4195f37 100644
--- a/test/test_cuda/schemes/test_auto_scheme.py
+++ b/test/test_cuda/schemes/test_auto_scheme.py
@@ -36,14 +36,12 @@ class TestAutoScheme:
scheme = AutoScheme(avg_bits=target_bits, options=("GGUF:Q4_K_M", "GGUF:Q8_0"))
ar = AutoRound(model=tiny_qwen_model_path, scheme=scheme, iters=1, enable_alg_ext=True)
ar.quantize_and_save(self.save_dir, format="gguf:q2_k_s")
- shutil.rmtree(self.save_dir, ignore_errors=True)
def test_gguf_k_1(self, tiny_qwen_model_path):
target_bits = 3.5
scheme = AutoScheme(avg_bits=target_bits, options=("GGUF:Q2_K_S", "GGUF:Q4_1"))
ar = AutoRound(model=tiny_qwen_model_path, scheme=scheme, iters=1, enable_alg_ext=True)
ar.quantize_and_save(self.save_dir, format="gguf:q2_k_s")
- shutil.rmtree(self.save_dir, ignore_errors=True)
#
def test_embedding_fallback(self, tiny_qwen_model_path):
@@ -51,14 +49,12 @@ class TestAutoScheme:
scheme = AutoScheme(avg_bits=target_bits, options=("GGUF:Q4_K_M", "GGUF:Q8_0"))
ar = AutoRound(model=tiny_qwen_model_path, scheme=scheme, iters=1, enable_alg_ext=True)
ar.quantize_and_save(self.save_dir, format="gguf:q2_k_s")
- shutil.rmtree(self.save_dir, ignore_errors=True)
def test_gguf_export(self, tiny_qwen_model_path):
target_bits = 3
scheme = AutoScheme(avg_bits=target_bits, options=("GGUF:Q2_K_S", "GGUF:Q4_K_M"), ignore_scale_zp_bits=True)
ar = AutoRound(model=tiny_qwen_model_path, scheme=scheme, iters=0)
ar.quantize_and_save(self.save_dir, format="gguf:q2_k_s")
- shutil.rmtree(self.save_dir, ignore_errors=True)
def test_gguf(self):
model_name = get_model_path("qwen/Qwen3-8B")
@@ -238,7 +234,7 @@ class TestAutoScheme:
# cause using tiny model
assert 5.78 < avg_bits <= target_bits + 1e-3
- def test_auto_scheme_export(self, tiny_qwen_model_path):
+ def test_auto_scheme_export(self):
model_name = get_model_path("facebook/opt-125m")
scheme = AutoScheme(avg_bits=3, options=("W2A16", "W4A16", "W8A16", "BF16"))
ar = AutoRound(model=model_name, scheme=scheme)
@@ -247,12 +243,11 @@ class TestAutoScheme:
result = simple_evaluate(model="hf", model_args=model_args, tasks="lambada_openai", batch_size="auto")
print(result["results"]["lambada_openai"]["acc,none"])
assert result["results"]["lambada_openai"]["acc,none"] > 0.25
- shutil.rmtree(self.save_dir, ignore_errors=True)
+ def test_auto_scheme_export_gguf(self, tiny_qwen_model_path):
scheme = AutoScheme(avg_bits=3, options=("gguf:q2_k_s,gguf:q4_k_s"), nsamples=1, ignore_scale_zp_bits=True)
ar = AutoRound(model=tiny_qwen_model_path, scheme=scheme, iters=0, nsamples=1)
ar.quantize_and_save(self.save_dir)
- shutil.rmtree(self.save_dir, ignore_errors=True)
def test_enable_torch_compile(self):
model_name = get_model_path("facebook/opt-125m")
@@ -263,4 +258,3 @@ class TestAutoScheme:
result = simple_evaluate(model="hf", model_args=model_args, tasks="lambada_openai", batch_size="auto")
print(result["results"]["lambada_openai"]["acc,none"])
assert result["results"]["lambada_openai"]["acc,none"] > 0.10
- shutil.rmtree(self.save_dir, ignore_errors=True)