Skip to content

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

Reset context singletons between tests (#2390)

Shubham Padkonded2a25f1be6d3
diff --git a/test/conftest.py b/test/conftest.py
index 7b85a985d..530b9b62b 100644
--- a/test/conftest.py
+++ b/test/conftest.py
@@ -45,6 +45,28 @@ def pytest_addoption(parser):
)
[email protected](autouse=True)
+def reset_context_singletons():
+ """Clear the process-wide context singletons between tests.
+
+ Contexts live for the whole process, so a test that runs a real
+ quantization leaves one behind and changes what later tests observe. For
+ example ``is_immediate_saving_mode()`` consults ``CompressContext`` first,
+ so a leaked context makes it report an ordinary model as immediately
+ saved, and ``test_export_utils.py`` fails only when it runs after
+ ``test_generation.py``.
+ """
+ yield
+
+ from auto_round.compressors.shard_writer import ShardWriter
+ from auto_round.context.compress import CompressContext
+ from auto_round.context.model import ModelContext
+
+ for context in (CompressContext, ModelContext):
+ context.reset_context()
+ ShardWriter.reset()
+
+
backup_env = pytest.StashKey[Mapping]()