Skip to content

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

Fix hpu error (#1766)

Heng Guo244091f9cd5b
diff --git a/auto_round/modeling/__init__.py b/auto_round/modeling/__init__.py
index 144e78ca3..d9371bb59 100644
--- a/auto_round/modeling/__init__.py
+++ b/auto_round/modeling/__init__.py
@@ -12,5 +12,9 @@
# See the License for the specific language governing permissions and
# limitations under the License.
+# NOTE: hpu_patch must be imported before any module that triggers loading of
+# `transformers.modeling_utils` (e.g. `fp8_quant`), otherwise the upstream
+# `transformers.integrations.finegrained_fp8` module is loaded without the
+# HPU-compatible overrides.
+from . import hpu_patch as _hpu_patch
from .fp8_quant import *
-from .hpu_patch import *
diff --git a/auto_round/modeling/hpu_patch.py b/auto_round/modeling/hpu_patch.py
index db321f21a..a62298f61 100644
--- a/auto_round/modeling/hpu_patch.py
+++ b/auto_round/modeling/hpu_patch.py
@@ -36,11 +36,35 @@ def patch_finegrained_fp8():
finegrained_fp8_patch = importlib.import_module(patch_file_name)
- # Replace transformers.integrations.finegrained_fp8 in sys.modules
- sys.modules["transformers.integrations.finegrained_fp8"] = finegrained_fp8_patch
+ # Patch the upstream transformers module in-place rather than replacing it
+ # entirely. Replacing the whole module via sys.modules drops other public
+ # symbols that newer transformers (e.g. >=4.57) expects to import from
+ # `transformers.integrations.finegrained_fp8` (such as
+ # `ALL_FP8_EXPERTS_FUNCTIONS`, `FP8Experts`, ...), causing ImportError.
+ try:
+ upstream = importlib.import_module("transformers.integrations.finegrained_fp8")
+ except Exception as import_err: # pragma: no cover - defensive
+ # Fallback to legacy behavior if the upstream module cannot be imported.
+ sys.modules["transformers.integrations.finegrained_fp8"] = finegrained_fp8_patch
+ logger.warning(
+ "Failed to import upstream transformers.integrations.finegrained_fp8"
+ f" ({import_err}); falling back to full module replacement."
+ )
+ return
+
+ patched_names = []
+ for name in dir(finegrained_fp8_patch):
+ if name.startswith("_"):
+ continue
+ setattr(upstream, name, getattr(finegrained_fp8_patch, name))
+ patched_names.append(name)
logger.info(
- "✓ Replaced transformers.integrations.finegrained_fp8 with auto_round.modeling.finegrained_fp8_patch"
+ "✓ Patched transformers.integrations.finegrained_fp8 with HPU-compatible"
+ f" overrides from {patch_file_name} ({len(patched_names)} symbols)"
+ )
+ logger.debug(
+ "Patched symbols for transformers.integrations.finegrained_fp8 from " f"{patch_file_name}: {patched_names}"
)
except Exception as e: