Skip to content

Commit 6fea888

Browse files
fynnsudsikka
andauthored
Fix Qwen3Next import errors on old transformers versions (#2030)
SUMMARY: Currently, we support `transformers>=4.54.0`, however `Qwen3Next` was only added in `transformers==4.57.0`. This updates the code so that: 1. In `src/llmcompressor/modeling/qwen3_next_moe.py` we only used the imports for typing, so they've been moved into an `if TYPE_CHECKING` block. 2. In `tests/llmcompressor/modeling/test_calib_qwen3_next.py` we wrap the import statement in a try-except and skip the Qwen3Next test if they fail to import. TEST PLAN: Run CI. --------- Signed-off-by: Fynn Schmitt-Ulms <[email protected]> Co-authored-by: Dipika Sikka <[email protected]>
1 parent 81eff05 commit 6fea888

File tree

2 files changed

+24
-8
lines changed

2 files changed

+24
-8
lines changed

src/llmcompressor/modeling/qwen3_next_moe.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from __future__ import annotations
2+
13
# coding=utf-8
24
# Copyright 2025 The Qwen team, Alibaba Group and the HuggingFace Inc. team.
35
# All rights reserved.
@@ -13,19 +15,21 @@
1315
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1416
# See the License for the specific language governing permissions and
1517
# limitations under the License.
18+
from typing import TYPE_CHECKING
1619

1720
import torch
1821

1922
from llmcompressor.modeling.moe_context import MoECalibrationModule
2023

21-
22-
@MoECalibrationModule.register("Qwen3NextSparseMoeBlock")
23-
class CalibrationQwen3NextSparseMoeBlock(MoECalibrationModule):
24+
if TYPE_CHECKING:
2425
from transformers import Qwen3NextConfig
2526
from transformers.models.qwen3_next.modeling_qwen3_next import (
2627
Qwen3NextSparseMoeBlock,
2728
)
2829

30+
31+
@MoECalibrationModule.register("Qwen3NextSparseMoeBlock")
32+
class CalibrationQwen3NextSparseMoeBlock(MoECalibrationModule):
2933
"""
3034
Calibration version of Qwen3NextSparseMoeBlock that sends all tokens to all experts.
3135
"""

tests/llmcompressor/modeling/test_calib_qwen3_next.py

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,22 @@
1111
from llmcompressor.utils.helpers import DisableQuantization, calibration_forward_context
1212
from tests.testing_utils import requires_cadence, requires_gpu
1313

14+
try:
15+
from transformers import Qwen3NextConfig
16+
from transformers.models.qwen3_next.modeling_qwen3_next import (
17+
Qwen3NextSparseMoeBlock,
18+
)
19+
except ImportError:
20+
Qwen3NextConfig = None
21+
Qwen3NextSparseMoeBlock = None
22+
1423

1524
@requires_cadence("weekly")
1625
@pytest.mark.parametrize("model_stub", ["Qwen/Qwen3-Next-80B-A3B-Instruct"])
26+
@pytest.mark.skipif(
27+
Qwen3NextConfig is None,
28+
reason="Qwen3Next not available in this version of transformers",
29+
)
1730
def test_calib_replace_qwen3moe_all_experts(model_stub):
1831
with skip_weights_download():
1932
model = AutoModelForCausalLM.from_pretrained(model_stub)
@@ -60,12 +73,11 @@ def hook_fn(i, module, input, output):
6073

6174

6275
@requires_gpu
76+
@pytest.mark.skipif(
77+
Qwen3NextConfig is None,
78+
reason="Qwen3Next not available in this version of transformers",
79+
)
6380
def test_calib_qwen3_moe_module():
64-
from transformers import Qwen3NextConfig
65-
from transformers.models.qwen3_next.modeling_qwen3_next import (
66-
Qwen3NextSparseMoeBlock,
67-
)
68-
6981
config = Qwen3NextConfig()
7082
with torch.device("cuda"):
7183
original = Qwen3NextSparseMoeBlock(config).eval()

0 commit comments

Comments
 (0)