Skip to content

Add qwen2 implementation#3113

Open
ChingTsai wants to merge 1 commit intomainfrom
jimmytsai/bringup-qwen2-5
Open

Add qwen2 implementation#3113
ChingTsai wants to merge 1 commit intomainfrom
jimmytsai/bringup-qwen2-5

Conversation

@ChingTsai
Copy link
Collaborator

@ChingTsai ChingTsai commented Feb 9, 2026

Description

Changes

  • Added Qwen2 implementation.
    • Implemented Qwen2 layers, these are mostly identical to Qwen3 but include the ability to apply attention bias.
    • Enabled attention bias specifically for QKV (excluding O) when using Qwen2. ref
    • Renamed Qwen3 to Qwen in hf_shape and param_mapping, and adding conversion for attention bias weights
    • Added end-to-end testing scripts.

Fix b/471703114

Tests

Logit Verification for HF -> MT

JAX_PLATFORMS=cpu python3 -m tests.utils.forward_pass_logit_checker src/maxtext/configs/base.yml run_name=forward_pass_test_unscanned model_name=qwen2.5-7b tokenizer_path=Qwen/Qwen2.5-7B-Instruct load_parameters_path=${CHECKPOINT_PATH} max_prefill_predict_length=4 max_target_length=4 dataset_type=synthetic scan_layers=true per_device_batch_size=1 skip_jax_distributed_system=True --max_kl_div=0.017 --run_hf_model=True weight_dtype=bfloat16 --hf_model_path=Qwen/Qwen2.5-7B-Instruct

MT -> HF Checkpoint Check

Checklist

Before submitting this PR, please make sure (put X in square brackets):

  • I have performed a self-review of my code. For an optional AI review, add the gemini-review label.
  • I have necessary comments in my code, particularly in hard-to-understand areas.
  • I have run end-to-end tests tests and provided workload links above if applicable.
  • I have made or will make corresponding changes to the doc if needed, including adding new documentation pages to the relevant Table of Contents (toctree directive) as explained in our documentation.

@codecov
Copy link

codecov bot commented Feb 9, 2026

@ChingTsai ChingTsai force-pushed the jimmytsai/bringup-qwen2-5 branch 2 times, most recently from 2c556cf to 7f84e0a Compare February 10, 2026 03:04
@github-actions
Copy link

🤖 Hi @ChingTsai, I've received your request, and I'm working on it now! You can track my progress in the logs for more details.

Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

📋 Review Summary

This Pull Request introduces the implementation of Qwen2 models, including new decoder layers, weight mappings, and configuration updates. The changes integrate Qwen2 into the existing MaxText framework, extending its model compatibility.

🔍 General Feedback

  • The generalization of Qwen3 mappings and hook functions to a unified Qwen approach in hf_shape.py and param_mapping.py is a good practice, improving code reusability and maintainability.
  • New configuration files for Qwen2.5 models are well-structured and consistent with existing model configurations.
  • Ensure consistent handling of attention biases across the model definition and weight mapping to prevent potential runtime issues.

@RissyRan
Copy link
Collaborator

Thanks for bringing up new models! We usually verify implementation using this script against the HF version. Please let us know if you meet any issues.

@RissyRan
Copy link
Collaborator

cc @parambole who is working on Qwen3 for helping review PRs

@ChingTsai ChingTsai force-pushed the jimmytsai/bringup-qwen2-5 branch 2 times, most recently from dcc4282 to 88f6034 Compare February 11, 2026 09:05
@ChingTsai
Copy link
Collaborator Author

ChingTsai commented Feb 11, 2026

Thanks for bringing up new models! We usually verify implementation using this script against the HF version. Please let us know if you meet any issues.

Hi @RissyRan, I noticed that the 7b scanned checkpoint has a higher max KL divergence of 0.016245 (see logs). I've updated the threshold (0.015 -> 0.017) to allow this to pass, but please let me know if this level of divergence is a concern.

@ChingTsai ChingTsai force-pushed the jimmytsai/bringup-qwen2-5 branch 2 times, most recently from baac10d to ccdb2ea Compare February 12, 2026 08:27
@ChingTsai ChingTsai force-pushed the jimmytsai/bringup-qwen2-5 branch 6 times, most recently from 8d0d39d to 890d7c1 Compare March 2, 2026 02:05
Copy link
Collaborator

@RissyRan RissyRan left a comment

Choose a reason for hiding this comment

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

Thanks for adding new models and tests! Overall LGTM!

As checkpoint conversion is bi-directional here, could you try convert your orbax to huggingface and compare the original huggingface checkpoint and see if value matches? One reference.

@hengtaoguo could you also take a review, especially for weight transfer part?

@@ -0,0 +1,38 @@
# Copyright 2023–2025 Google LLC
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit: 2026, similar for other files if applies.

# See the License for the specific language governing permissions and
# limitations under the License.

# model config for qwen2.5-14b
Copy link
Collaborator

Choose a reason for hiding this comment

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

Nit: could you help add a huggingface reference link for them, like https://huggingface.co/Qwen/Qwen2.5-14B-Instruct/blob/main/config.json. Similar for other files if applies.

vocab_size: 152064

decoder_block: "qwen2"

Copy link
Collaborator

Choose a reason for hiding this comment

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

nit: shall we remove the extra lines to align with 7b if no specific comment?


## Running the End-to-End Test

The `test_qwen2.5-14b.sh` script automates the following steps:
Copy link
Collaborator

Choose a reason for hiding this comment

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

Shall we put 7b here (swap it in this doc)? I see HF downloads from 7b is more popular with 19M last month. https://screenshot.googleplex.com/56Gg4wPjEWEMMsw

--run_hf_model=True \
--hf_model_path=${HF_MODEL_ID}

# Step 3: SFT
Copy link
Collaborator

Choose a reason for hiding this comment

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

Shall we add one test for pre-train (without loading ckpt) and decode (with unscanned ckpt)? I think 7b and 14b are same structure, and it's fine to just add one into end_to_end test script.

@ChingTsai ChingTsai force-pushed the jimmytsai/bringup-qwen2-5 branch 4 times, most recently from 51f8989 to 4432d31 Compare March 4, 2026 06:35
@ChingTsai
Copy link
Collaborator Author

ChingTsai commented Mar 4, 2026

As checkpoint conversion is bi-directional here, could you try convert your orbax to huggingface and compare the original huggingface checkpoint and see if value matches? One reference.

Hi @RissyRan , I have completed the HF conversion checks for qwen2.5-7B and qwen2.5-14B.
I also added a small change to include a progress bar in the checker here .
All the comments should be addressed, but please let me know if I missed anything.

Thanks!

@ChingTsai ChingTsai requested a review from RissyRan March 4, 2026 06:54
bash tests/end_to_end/tpu/qwen/dense/qwen2.5-7b/test_qwen2.5-7b.sh
```

#### Expected output
Copy link
Collaborator

Choose a reason for hiding this comment

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

Let's remove this section? The alignment could be changed based on precision an other configs. I don't want to give a wrong impression to customers that rank_agreement_percentage is at 40.0%. Similar to performance number in pre-train.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

fixed

Copy link
Collaborator

@RissyRan RissyRan left a comment

Choose a reason for hiding this comment

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

LGTM! Please consider to remove the expected results.

@ChingTsai ChingTsai force-pushed the jimmytsai/bringup-qwen2-5 branch from 4432d31 to def7166 Compare March 6, 2026 02:51
Copy link
Collaborator

@parambole parambole left a comment

Choose a reason for hiding this comment

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

LGTM

@ChingTsai ChingTsai force-pushed the jimmytsai/bringup-qwen2-5 branch from def7166 to d6c9842 Compare March 16, 2026 02:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants