Repository navigation
Switch Qwen3-Next to use MambaModel #2520
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 20 commits
c1ad4d5
e01cb01
1c3a230
088c43c
5203af9
66461d8
130fe84
8dc1159
f2d2a05
e1139e1
b98e4d5
260fd4c
0e83507
1c6a97b
7f068d8
89ea050
0c8cd89
bb722dc
7bb0b73
a4a841d
5c4725f
bd9fd8a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| # Copyright (c) 2025, NVIDIA CORPORATION. All rights reserved. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| ARG BASE_IMAGE=nvcr.io/nvidia/pytorch:25.11-py3 | ||
| FROM ${BASE_IMAGE} AS megatron_bridge | ||
| WORKDIR /opt/Megatron-Bridge | ||
| ENV PATH="/root/.local/bin:$PATH" | ||
| ENV UV_PROJECT_ENVIRONMENT=/opt/venv | ||
| ENV PATH="$UV_PROJECT_ENVIRONMENT/bin:$PATH" | ||
| ENV VIRTUAL_ENV=/opt/venv | ||
| ENV PATH="$VIRTUAL_ENV/bin:$PATH" | ||
| ENV UV_LINK_MODE=copy | ||
| ENV UV_VERSION="0.7.2" | ||
|
|
||
| RUN curl -LsSf https://astral.sh/uv/${UV_VERSION}/install.sh | sh && \ | ||
| uv venv ${UV_PROJECT_ENVIRONMENT} --system-site-packages && \ | ||
| # Address CVE-2025-68973 | ||
| apt-get update && apt install -y --only-upgrade gnupg && \ | ||
| apt-get clean && \ | ||
| rm -rf /var/lib/apt/lists/* | ||
|
|
||
| COPY pyproject.toml uv.lock /opt/Megatron-Bridge/ | ||
| COPY src/megatron/bridge/__init__.py src/megatron/bridge/package_info.py /opt/Megatron-Bridge/src/megatron/bridge/ | ||
| COPY 3rdparty/Megatron-LM/pyproject.toml 3rdparty/Megatron-LM/setup.py /opt/Megatron-Bridge/3rdparty/Megatron-LM/ | ||
| COPY 3rdparty/Megatron-LM/megatron/core/__init__.py 3rdparty/Megatron-LM/megatron/core/package_info.py /opt/Megatron-Bridge/3rdparty/Megatron-LM/megatron/core/ | ||
| COPY 3rdparty/Megatron-LM/megatron/training/__init__.py /opt/Megatron-Bridge/3rdparty/Megatron-LM/megatron/training/ | ||
| COPY 3rdparty/Megatron-LM/megatron/core/datasets/Makefile 3rdparty/Megatron-LM/megatron/core/datasets/helpers.cpp /opt/Megatron-Bridge/3rdparty/Megatron-LM/megatron/core/datasets/ | ||
|
|
||
| # Build arg to skip --locked when testing with different MCore versions | ||
| ARG MCORE_TRIGGERED_TESTING=false | ||
| ARG UV_CACHE_PRUNE_ARGS | ||
|
|
||
| RUN --mount=type=cache,target=/var/cache/apt,sharing=locked \ | ||
| --mount=type=cache,target=/var/lib/apt,sharing=locked \ | ||
| --mount=type=cache,target=/root/.cache/uv \ | ||
| if [ "$MCORE_TRIGGERED_TESTING" = "true" ]; then \ | ||
| echo "⚙️ MCore testing mode: skipping --locked flag"; \ | ||
| uv sync --only-group build && \ | ||
| uv sync --link-mode copy --all-extras --all-groups; \ | ||
| else \ | ||
| uv sync --locked --only-group build && \ | ||
| uv sync --link-mode copy --locked --all-extras --all-groups; \ | ||
| fi && \ | ||
| uv cache prune ${UV_CACHE_PRUNE_ARGS} | ||
|
|
||
| COPY 3rdparty/Megatron-LM/docker/patches/deepep.patch /workspace/deepep.patch | ||
| RUN bash -ex <<"EOF" | ||
| cd /workspace | ||
| uv pip install nvidia-nvshmem-cu13==3.4.5 | ||
| pushd /opt/venv/lib/python3.12/site-packages/nvidia/nvshmem/lib/ | ||
| ln -s libnvshmem_host.so.3 libnvshmem_host.so | ||
| popd | ||
|
|
||
| git clone --branch hybrid-ep https://github.com/deepseek-ai/DeepEP.git | ||
| pushd DeepEP | ||
| git checkout eb9cee7de5a24193bf09500668d3a619d3d3f3fb | ||
| patch -p1 < /workspace/deepep.patch | ||
| popd | ||
| TORCH_CUDA_ARCH_LIST="9.0 10.0 12.0" uv pip install --no-build-isolation -v DeepEP/. | ||
| rm -rf DeepEP | ||
| EOF | ||
|
|
||
| COPY . /opt/Megatron-Bridge |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| #!/usr/bin/env bash | ||
| # Copyright (c) 2025, NVIDIA CORPORATION. All rights reserved. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| set -xeuo pipefail | ||
|
|
||
| # Workspace directory for checkpoints and results | ||
| WORKSPACE=${WORKSPACE:-/workspace} | ||
|
|
||
| MODEL_NAME=Qwen3-Next-80B-A3B-Instruct | ||
| HF_MODEL_ID=Qwen/$MODEL_NAME | ||
|
|
||
| # Import HF → Megatron | ||
| uv run python examples/conversion/convert_checkpoints.py import \ | ||
| --hf-model $HF_MODEL_ID \ | ||
| --megatron-path ${WORKSPACE}/models/$MODEL_NAME \ | ||
| --trust-remote-code | ||
|
|
||
| # Export Megatron → HF | ||
| uv run python examples/conversion/convert_checkpoints.py export \ | ||
| --hf-model $HF_MODEL_ID \ | ||
| --megatron-path ${WORKSPACE}/models/$MODEL_NAME/iter_0000000 \ | ||
| --hf-path ${WORKSPACE}/models/$MODEL_NAME-hf-export | ||
|
|
||
| # Round-trip validation | ||
| uv run python -m torch.distributed.run --nproc_per_node=8 \ | ||
| examples/conversion/hf_megatron_roundtrip_multi_gpu.py \ | ||
| --hf-model-id $HF_MODEL_ID \ | ||
| --megatron-load-path ${WORKSPACE}/models/$MODEL_NAME/iter_0000000 \ | ||
| --tp 1 --pp 4 \ | ||
| --trust-remote-code | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,174 @@ | ||
| #!/bin/bash | ||
| # Copyright (c) 2026, NVIDIA CORPORATION. All rights reserved. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| # ============================================================================== | ||
| # Qwen3-Next 80B-A3B Pretraining | ||
| # | ||
| # Qwen3-Next 80B-A3B is a hybrid MoE language model with GDN linear attention | ||
| # layers. Supports multiple parallelism configs: each "TP,PP,EP,CP,SP" runs | ||
| # sequentially. | ||
| # | ||
| # Usage: | ||
| # 1. Modify the #SBATCH directives below for your cluster | ||
| # 2. Set CONTAINER_IMAGE to your container path | ||
| # 3. Set PARALLELISM_CONFIGS (TP,PP,EP,CP,SP per entry; CP = context parallel size, 1 = disabled) | ||
| # 4. Submit: sbatch slurm_pretrain.sh | ||
| # ============================================================================== | ||
|
|
||
| #SBATCH --job-name=qwen3next-pretrain | ||
| #SBATCH --nodes=4 | ||
| #SBATCH --ntasks-per-node=8 | ||
| #SBATCH --gpus-per-node=8 | ||
| #SBATCH --time=24:00:00 | ||
| #SBATCH --partition=batch | ||
| #SBATCH --account=coreai_dlalgo_llm | ||
| #SBATCH --output=logs/qwen3_next_pretrain_%j.out | ||
| #SBATCH --error=logs/qwen3_next_pretrain_%j.err | ||
| #SBATCH --exclusive | ||
|
|
||
| # ============================================================================== | ||
| # CONFIGURATION | ||
| # ============================================================================== | ||
|
|
||
| # Workspace directory for checkpoints and results | ||
| WORKSPACE=${WORKSPACE:-/workspace} | ||
|
|
||
| # Base directory for container image and mounts (set if not already set, e.g. by launch_nemo.sh) | ||
| export WKDIR="${WKDIR:-}" | ||
|
|
||
| # Model and training configurations | ||
| MODEL_NAME=qwen3_next_80b_a3b | ||
| DATASET_NAME=mock | ||
| SEQ_LENGTH=4096 | ||
| TRAIN_ITERS=1000 | ||
| GLOBAL_BATCH_SIZE=128 | ||
| MICRO_BATCH_SIZE=1 | ||
| EVAL_ITERS=10 | ||
| LR_WARMUP_ITERS=50 | ||
| LOG_INTERVAL=1 | ||
| WANDB_PROJECT=megatron-bridge-${DATASET_NAME} | ||
|
|
||
| # Parallelism configs: "TP,PP,EP,CP,SP" per entry (TP*PP*EP must equal total GPUs) | ||
| PARALLELISM_CONFIGS=("1,4,8,1,False") | ||
|
|
||
| # Container image (required) | ||
| CONTAINER_IMAGE="" | ||
| # CONTAINER_IMAGE="/path/to/container.sqsh" | ||
|
|
||
| # Container mounts (optional; comma-separated for srun --container-mounts) | ||
| CONTAINER_MOUNTS="" | ||
| # CONTAINER_MOUNTS="/data:/data /workspace:/workspace" | ||
|
|
||
| # ============================================================================== | ||
| # Environment Setup | ||
| # ============================================================================== | ||
|
|
||
| # NCCL optimizations for large-scale training | ||
| export TORCH_NCCL_AVOID_RECORD_STREAMS=1 | ||
| export NCCL_NVLS_ENABLE=0 | ||
|
|
||
| # UV cache on shared filesystem (recommended for multi-node setups) | ||
| # Pre-sync once before submitting jobs: UV_CACHE_DIR=/path/to/cache uv sync | ||
| # export UV_CACHE_DIR="/path/to/shared/uv_cache" | ||
|
|
||
| # HuggingFace cache directory (recommended for shared filesystem) | ||
| # export HF_HOME="/path/to/shared/HF_HOME" | ||
|
|
||
| # Authentication tokens (set these for your environment) | ||
| # export HF_TOKEN="hf_your_token_here" | ||
| # export WANDB_API_KEY="your_wandb_key_here" | ||
|
|
||
| # ============================================================================== | ||
| # Job Execution | ||
| # ============================================================================== | ||
|
|
||
| echo "======================================" | ||
| echo "Qwen3-Next 80B-A3B Pretraining Job" | ||
| echo "======================================" | ||
| echo "Job ID: $SLURM_JOB_ID" | ||
| echo "Nodes: $SLURM_JOB_NUM_NODES" | ||
| echo "GPUs per node: $SLURM_GPUS_PER_NODE" | ||
| echo "Model: $MODEL_NAME" | ||
| echo "Parallelism configs: ${PARALLELISM_CONFIGS[*]}" | ||
| echo "======================================" | ||
|
|
||
| # Create logs directory if it doesn't exist | ||
| mkdir -p logs | ||
|
|
||
| # Require container image | ||
| if [ -z "$CONTAINER_IMAGE" ]; then | ||
| echo "ERROR: CONTAINER_IMAGE must be set. Please specify a valid container image." | ||
| exit 1 | ||
| fi | ||
|
|
||
| # Build srun command (shared across configs) | ||
| SRUN_CMD="srun --mpi=pmix --container-image=$CONTAINER_IMAGE" | ||
| if [ -n "$CONTAINER_MOUNTS" ]; then | ||
| SRUN_CMD="$SRUN_CMD --container-mounts=$CONTAINER_MOUNTS" | ||
| fi | ||
| echo "SRUN base: $SRUN_CMD" | ||
| echo "======================================" | ||
|
|
||
| # Run each parallelism config in sequence | ||
| CONFIG_INDEX=0 | ||
| for CONFIG in "${PARALLELISM_CONFIGS[@]}"; do | ||
| IFS=',' read -r TP PP EP CP SP <<< "$CONFIG" | ||
| CONFIG_INDEX=$((CONFIG_INDEX + 1)) | ||
| echo "" | ||
| echo "======================================" | ||
| echo "Config $CONFIG_INDEX/${#PARALLELISM_CONFIGS[@]}: TP=$TP, PP=$PP, EP=$EP, SP=$SP, CP=$CP" | ||
| echo "======================================" | ||
|
|
||
| # Build CLI overrides for this config | ||
| CLI_OVERRIDES=" \ | ||
| model.seq_length=$SEQ_LENGTH \ | ||
| train.train_iters=$TRAIN_ITERS \ | ||
| train.global_batch_size=$GLOBAL_BATCH_SIZE \ | ||
| train.micro_batch_size=$MICRO_BATCH_SIZE \ | ||
| train.eval_iters=$EVAL_ITERS \ | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧩 Analysis chain🏁 Script executed: #!/bin/bash
# Check which config path is used for eval_iters
rg -n "eval_iters" src/megatron/bridge/recipes/qwen/qwen3_next.py
rg -n "eval_iters" src/megatron/bridge/training/config.pyRepository: NVIDIA-NeMo/Megatron-Bridge Length of output: 147 🏁 Script executed: #!/bin/bash
# Find the slurm_pretrain.sh file
fd -t f "slurm_pretrain.sh" --exec cat -n {} \; 2>/dev/null | head -150
# Also check the full structure of validation and train configs
rg -A 5 "class.*Validation" src/megatron/bridge/training/config.py
rg -A 5 "class.*Train" src/megatron/bridge/training/config.py | head -30Repository: NVIDIA-NeMo/Megatron-Bridge Length of output: 8091 🏁 Script executed: #!/bin/bash
# Find qwen3_next slurm file
fd -t f "slurm_pretrain.sh" examples/models/qwen3_next/ 2>/dev/null
# Also check config structure for validation
rg -B 5 -A 10 "class ValidationConfig" src/megatron/bridge/training/config.pyRepository: NVIDIA-NeMo/Megatron-Bridge Length of output: 115 🏁 Script executed: #!/bin/bash
# Show the actual qwen3_next slurm_pretrain.sh file, especially around line 140
cat -n examples/models/qwen3_next/slurm_pretrain.sh | sed -n '130,150p'
# Check the validation config class structure
rg -B 2 -A 15 "class ValidationConfig" src/megatron/bridge/training/config.pyRepository: NVIDIA-NeMo/Megatron-Bridge Length of output: 1374 🏁 Script executed: #!/bin/bash
# Check the validation config class structure
rg -B 2 -A 10 "class ValidationConfig" src/megatron/bridge/training/config.py
# Also check if there's a Train config with eval_iters
rg -B 2 -A 15 "class TrainConfig" src/megatron/bridge/training/config.py | head -50Repository: NVIDIA-NeMo/Megatron-Bridge Length of output: 53 🏁 Script executed: #!/bin/bash
# Search for validation-related config classes more broadly
rg -i "validation|eval_iters" src/megatron/bridge/training/config.py -B 2 -A 5
# Also check the qwen3_next recipe file to see what config structure it uses
cat -n src/megatron/bridge/recipes/qwen/qwen3_next.py | grep -A 3 -B 3 "eval_iters"Repository: NVIDIA-NeMo/Megatron-Bridge Length of output: 8246 Use Line 140 uses 🤖 Prompt for AI Agents |
||
| scheduler.lr_warmup_iters=$LR_WARMUP_ITERS \ | ||
| checkpoint.save=${WORKSPACE}/results/${MODEL_NAME}_pretrain_tp${TP}_pp${PP}_ep${EP}_sp${SP}_cp${CP} \ | ||
| logger.tensorboard_dir=${WORKSPACE}/tb_logs/${MODEL_NAME}_pretrain_tp${TP}_pp${PP}_ep${EP}_sp${SP}_cp${CP} \ | ||
| logger.log_interval=$LOG_INTERVAL \ | ||
| logger.wandb_project=$WANDB_PROJECT \ | ||
| logger.wandb_exp_name=${MODEL_NAME}_${DATASET_NAME}_pretrain_tp${TP}_pp${PP}_ep${EP}_sp${SP}_cp${CP} \ | ||
| dataset.sequence_length=$SEQ_LENGTH \ | ||
| model.tensor_model_parallel_size=$TP \ | ||
| model.pipeline_model_parallel_size=$PP \ | ||
| model.expert_model_parallel_size=$EP \ | ||
| model.sequence_parallel=$SP \ | ||
| model.context_parallel_size=$CP | ||
| " | ||
|
|
||
| CMD="uv run --no-sync python /opt/Megatron-Bridge/scripts/training/run_recipe.py" | ||
| CMD="$CMD --recipe ${MODEL_NAME}_pretrain_config" | ||
| # Collapse newlines so bash -c receives a single command | ||
| CMD="$CMD $(echo "$CLI_OVERRIDES" | tr '\n' ' ' | sed 's/ \+/ /g')" | ||
|
|
||
| echo "Executing command..." | ||
| echo $CMD | ||
| echo "======================================" | ||
|
|
||
| $SRUN_CMD bash -c "$CMD" | ||
| RUN_EXIT=$? | ||
| if [ $RUN_EXIT -ne 0 ]; then | ||
| echo "ERROR: Config TP=$TP, PP=$PP, EP=$EP, SP=$SP, CP=$CP failed with exit code $RUN_EXIT" | ||
| exit $RUN_EXIT | ||
| fi | ||
| done | ||
|
|
||
| echo "======================================" | ||
| echo "Job completed (all ${#PARALLELISM_CONFIGS[@]} configs)" | ||
| echo "======================================" | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing
--ep(expert parallel) parameter in round-trip validation.The pretrain recipe recommends
TP=1, PP=4, EP=8, but the round-trip validation only passes--tp 1 --pp 4. For proper validation of the MoE model conversion, the expert parallel size should also be specified.🔧 Proposed fix
uv run python -m torch.distributed.run --nproc_per_node=8 \ examples/conversion/hf_megatron_roundtrip_multi_gpu.py \ --hf-model-id $HF_MODEL_ID \ --megatron-load-path ${WORKSPACE}/models/$MODEL_NAME/iter_0000000 \ - --tp 1 --pp 4 \ + --tp 1 --pp 4 --ep 8 \ --trust-remote-code📝 Committable suggestion
🤖 Prompt for AI Agents