Skip to content
Closed
Show file tree
Hide file tree
Changes from 20 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
c1ad4d5
Switch Qwen3-Next to use MambaModel
Phlip79 Feb 25, 2026
e01cb01
Add qwen3-next scripts
Phlip79 Feb 26, 2026
1c3a230
Remove peft and add tensorboard_dir
Phlip79 Mar 2, 2026
088c43c
Merge branch 'main' of https://github.com/NVIDIA-NeMo/Megatron-Bridge…
Phlip79 Mar 3, 2026
5203af9
Create deepdep dockerfile
Phlip79 Mar 3, 2026
66461d8
Fix slurm sft script
Phlip79 Mar 11, 2026
130fe84
Update sft config to MambaModel
Phlip79 Mar 11, 2026
8dc1159
Merge branch 'main' of https://github.com/NVIDIA-NeMo/Megatron-Bridge…
Phlip79 Mar 11, 2026
f2d2a05
Update base image
Phlip79 Mar 12, 2026
e1139e1
Merge branch 'main' of https://github.com/NVIDIA-NeMo/Megatron-Bridge…
Phlip79 Mar 12, 2026
b98e4d5
Merge branch 'main' of https://github.com/NVIDIA-NeMo/Megatron-Bridge…
Phlip79 Mar 18, 2026
260fd4c
Use old base image
Phlip79 Mar 18, 2026
0e83507
Updates to get SFT working
Phlip79 Mar 22, 2026
1c6a97b
Add support for QK norm
Phlip79 Mar 31, 2026
7f068d8
Merge remote-tracking branch 'origin/main' into philip/qwen-on-mamba
Phlip79 Mar 31, 2026
89ea050
Add megatron.training
Phlip79 Mar 31, 2026
0c8cd89
Update mamba
Phlip79 Mar 31, 2026
bb722dc
Restore mamba_builder.py and mamba_provider.py to main
Phlip79 Mar 31, 2026
7bb0b73
Add tests for MambaModel physical layer indexing in Qwen3-Next bridge
Phlip79 Mar 31, 2026
a4a841d
Handle missing safetensors index in MTP detection
Phlip79 Mar 31, 2026
5c4725f
Add README and cleanup
Phlip79 Apr 1, 2026
bd9fd8a
Make conversion script executable
Phlip79 Apr 1, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 74 additions & 0 deletions docker/Dockerfile.deepep
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
42 changes: 42 additions & 0 deletions examples/models/qwen3_next/conversion.sh
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
Comment on lines +36 to +42

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 | 🟡 Minor

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

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
# 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
# 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 --ep 8 \
--trust-remote-code
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@examples/models/qwen3_next/conversion.sh` around lines 36 - 42, The
round-trip validation command for
examples/conversion/hf_megatron_roundtrip_multi_gpu.py omits the expert-parallel
flag; update the uv run invocation to pass the expert parallel size (add the
--ep argument) — e.g., include --ep 8 (or use the EP env var) alongside the
existing --tp 1 --pp 4 so the script runs with TP=1, PP=4, EP=8 for correct MoE
conversion validation.

174 changes: 174 additions & 0 deletions examples/models/qwen3_next/slurm_pretrain.sh
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 \

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 | 🟡 Minor

🧩 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.py

Repository: 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 -30

Repository: 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.py

Repository: 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.py

Repository: 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 -50

Repository: 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 validation.eval_iters instead of deprecated train.eval_iters.

Line 140 uses train.eval_iters, which is deprecated. While it will work via auto-migration to validation.eval_iters, this path triggers a deprecation warning. Use validation.eval_iters directly instead, matching the pattern in qwen3_next.py (line 201).

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@examples/models/qwen3_next/slurm_pretrain.sh` at line 140, Replace the
deprecated configuration key train.eval_iters with validation.eval_iters in the
slurm_pretrain.sh invocation (currently setting train.eval_iters=$EVAL_ITERS);
update the argument to validation.eval_iters=$EVAL_ITERS so the script uses the
non-deprecated key (matching the pattern used in qwen3_next.py), avoiding the
deprecation warning.

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 "======================================"
Loading
Loading