Skip to content

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

fix: apply debug layer limits to nested configs (#2431)

Shubham Padkonde6afaecdbe409
diff --git a/auto_round/utils/model.py b/auto_round/utils/model.py
index ae982495f..00ea444e2 100644
--- a/auto_round/utils/model.py
+++ b/auto_round/utils/model.py
@@ -378,10 +378,13 @@ def _maybe_truncate_debug_layers(config) -> bool:
# hard-coding one name.
targets = [config]
seen = {id(config)}
- for value in list(getattr(config, "__dict__", {}).values()):
- if hasattr(value, "num_hidden_layers") and id(value) not in seen:
- targets.append(value)
- seen.add(id(value))
+ for cfg in targets:
+ for value in list(getattr(cfg, "__dict__", {}).values()):
+ if id(value) not in seen and (
+ isinstance(value, transformers.PretrainedConfig) or hasattr(value, "num_hidden_layers")
+ ):
+ targets.append(value)
+ seen.add(id(value))
changed = False
for cfg in targets:
diff --git a/test/unit/common/utils/test_model_utils.py b/test/unit/common/utils/test_model_utils.py
index c561de4af..ecbc6a1c7 100644
--- a/test/unit/common/utils/test_model_utils.py
+++ b/test/unit/common/utils/test_model_utils.py
@@ -22,6 +22,35 @@ import pytest
import torch
[email protected]("depth", [0, 1, 2, 3])
+def test_debug_layers_reaches_nested_configs(monkeypatch, depth):
+ from transformers import PretrainedConfig
+
+ from auto_round.utils.model import _maybe_truncate_debug_layers
+
+ monkeypatch.setenv("AR_DEBUG_LAYER_NUM", "2")
+ decoder = PretrainedConfig(num_hidden_layers=28)
+ config = decoder
+ for _ in range(depth):
+ config = PretrainedConfig(text_config=config)
+ assert _maybe_truncate_debug_layers(config)
+ assert decoder.num_hidden_layers == 2
+ assert not _maybe_truncate_debug_layers(config)
+
+
+def test_debug_layers_handles_shared_and_cyclic_configs(monkeypatch):
+ from transformers import PretrainedConfig
+
+ from auto_round.utils.model import _maybe_truncate_debug_layers
+
+ monkeypatch.setenv("AR_DEBUG_LAYER_NUM", "2")
+ decoder = PretrainedConfig(num_hidden_layers=1)
+ config = PretrainedConfig(text_config=decoder, shared_config=decoder)
+ decoder.parent_config = config
+ assert not _maybe_truncate_debug_layers(config)
+ assert decoder.num_hidden_layers == 1
+
+
class TestGetBlockNames:
"""Test get_block_names function."""