Skip to content

fix broken MX tests from transformers 5.8.1 upgrade#3679

Merged
winglian merged 5 commits into
mainfrom
mx-transformers581-fixes
May 26, 2026
Merged

fix broken MX tests from transformers 5.8.1 upgrade#3679
winglian merged 5 commits into
mainfrom
mx-transformers581-fixes

Conversation

@winglian

@winglian winglian commented May 24, 2026

Copy link
Copy Markdown
Collaborator

Summary by CodeRabbit

Release Notes

  • Bug Fixes

    • Fixed crashes when loading MX-quantized model checkpoints in fresh Python processes by preventing unnecessary weight re-initialization during model loading.
  • Tests

    • Added test to verify MX-quantized checkpoints load correctly across process boundaries.

Review Change Stack

@coderabbitai

coderabbitai Bot commented May 24, 2026

Copy link
Copy Markdown
Contributor

Important

Review skipped

Auto incremental reviews are disabled on this repository.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: d354d747-86cf-4448-8daa-9aa7cd172414

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
📝 Walkthrough

Walkthrough

Adds a Transformers weight-initialization guard patch to prevent re-initialization failures on MXTensor and TorchAO quantized weights. The patch is applied during quantization, model loader setup, and validated via cross-process loading test.

Changes

MX Quantization Initialization Guard

Layer / File(s) Summary
Transformers initialization guard patch
src/axolotl/utils/quantization.py
Introduces patch_transformers_skip_quantized_init() wrapper that patches transformers.PreTrainedModel._initialize_weights to detect and skip re-initialization for modules with TorchAO tensor parameters (e.g., MXTensor); functools import added for wrapping, and single-application sentinel prevents duplicate patching.
MX quantization path integration
src/axolotl/utils/quantization.py
Calls patch_transformers_skip_quantized_init() in the quantize_model MX quantization branch before returning, ensuring the patch is active for subsequent model loads.
Model loader pre-load setup integration
src/axolotl/loaders/model.py
Imports and invokes patch_transformers_skip_quantized_init() in ModelLoader._apply_pre_model_load_setup immediately after quantization config setup, guarding the model-loading path.
Cross-process loading validation
tests/e2e/test_quantization.py
Adds test_mxfp4_cross_process_load to verify MX-quantized checkpoints can reload in fresh subprocesses by applying the patch, using from_pretrained, and asserting reloaded linear weights are MXTensor instances.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~12 minutes

Possibly related PRs

  • axolotl-ai-cloud/axolotl#3205: Modifies the model-loading quantization path in src/axolotl/loaders/model.py for MX/mxfp4, overlapping with load-time behavior changes.

Suggested reviewers

  • ved1beta
  • SalmanMohammadi
🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Title check ⚠️ Warning The title refers to fixing MX tests broken by a transformers upgrade, but the actual changes implement a patch for MX-quantized checkpoints to prevent weight re-initialization errors during model loading. Revise the title to accurately describe the main change, such as 'Add patch to skip quantized weight initialization for MX-quantized models' or 'Fix MX-quantized model loading by patching transformers weight initialization'.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Docstring Coverage ✅ Passed Docstring coverage is 87.50% which is sufficient. The required threshold is 80.00%.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch mx-transformers581-fixes

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Nitpick comments (1)
src/axolotl/utils/quantization.py (1)

170-179: ⚡ Quick win

Trim this to a single short WHY comment per repo policy.

Please reduce this multi-line narrative to one short WHY-focused line to match the src/axolotl/**/*.py comment standard.

As per coding guidelines, "Only add comments when explaining the WHY behind non-obvious logic, hidden constraints, or workarounds for specific bugs... Comments should be a maximum of one short line".

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/axolotl/utils/quantization.py` around lines 170 - 179, Replace the
multi-line explanatory docstring in src/axolotl/utils/quantization.py that
describes why we skip re-initializing torchao-quantized weights with a single
short WHY comment line; concisely state that we skip modules in
from_pretrained/_init_weights to avoid re-initializing quantized tensors (e.g.,
MXTensor) which would drop the _is_hf_initialized flag and raise
NotImplementedError.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@src/axolotl/loaders/model.py`:
- Around line 226-230: The unguarded import and call to
axolotl.utils.quantization.patch_transformers_skip_quantized_init() inside
_apply_pre_model_load_setup() can raise on platforms where torchao is
unavailable; change the code to avoid importing torchao-backed quantization
unconditionally by either (a) moving the import and call behind the existing
MX/quantization feature check or (b) wrapping the import/call in a try/except
ImportError that silently no-ops when axolotl.utils.quantization (or torchao) is
missing; reference patch_transformers_skip_quantized_init() and
_apply_pre_model_load_setup() when making the change.

---

Nitpick comments:
In `@src/axolotl/utils/quantization.py`:
- Around line 170-179: Replace the multi-line explanatory docstring in
src/axolotl/utils/quantization.py that describes why we skip re-initializing
torchao-quantized weights with a single short WHY comment line; concisely state
that we skip modules in from_pretrained/_init_weights to avoid re-initializing
quantized tensors (e.g., MXTensor) which would drop the _is_hf_initialized flag
and raise NotImplementedError.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 9d5a015b-c257-4359-b370-4b1a44c84bde

📥 Commits

Reviewing files that changed from the base of the PR and between 1d68aca and 43a686a.

📒 Files selected for processing (3)
  • src/axolotl/loaders/model.py
  • src/axolotl/utils/quantization.py
  • tests/e2e/test_quantization.py

Comment thread src/axolotl/loaders/model.py Outdated
Comment on lines +226 to +230
from axolotl.utils.quantization import (
patch_transformers_skip_quantized_init,
)

patch_transformers_skip_quantized_init()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major | ⚡ Quick win

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
set -euo pipefail

echo "== Check whether torchao is mandatory in project metadata =="
fd -i 'pyproject.toml|setup.py|requirements*.txt|uv.lock|poetry.lock' | while read -r f; do
  echo "--- $f ---"
  rg -n "torchao" "$f" || true
done

echo "== Confirm unconditional call site =="
rg -n -C3 'patch_transformers_skip_quantized_init|from axolotl.utils.quantization import' src/axolotl/loaders/model.py

echo "== Confirm torchao top-level imports in quantization module =="
rg -n -C2 '^from torchao|^import torchao' src/axolotl/utils/quantization.py

Repository: axolotl-ai-cloud/axolotl

Length of output: 1569


Avoid importing torchao-backed quantization unconditionally in the generic pre-load path

src/axolotl/loaders/model.py imports axolotl.utils.quantization and calls patch_transformers_skip_quantized_init() from _apply_pre_model_load_setup(). axolotl/utils/quantization.py imports torchao at module import time, and pyproject.toml declares torchao conditionally (missing on darwin/aarch64). This can cause import-time failures for non-quantized model loads in those environments—guard the import/call behind the MX/quantization condition (or wrap it in try/except ImportError with a no-op fallback).

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/axolotl/loaders/model.py` around lines 226 - 230, The unguarded import
and call to axolotl.utils.quantization.patch_transformers_skip_quantized_init()
inside _apply_pre_model_load_setup() can raise on platforms where torchao is
unavailable; change the code to avoid importing torchao-backed quantization
unconditionally by either (a) moving the import and call behind the existing
MX/quantization feature check or (b) wrapping the import/call in a try/except
ImportError that silently no-ops when axolotl.utils.quantization (or torchao) is
missing; reference patch_transformers_skip_quantized_init() and
_apply_pre_model_load_setup() when making the change.

@codecov

codecov Bot commented May 24, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 91.89189% with 3 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
src/axolotl/loaders/model.py 85.71% 3 Missing ⚠️

📢 Thoughts on this report? Let us know!

@winglian winglian merged commit b433cbd into main May 26, 2026
16 of 17 checks passed
@winglian winglian deleted the mx-transformers581-fixes branch May 26, 2026 12:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant