Skip to content
Closed
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
16 commits
Select commit Hold shift + click to select a range
5e9e81c
[recipe] feat: Add Qwen3.5 LLM-only SFT recipes with skip_megatron_pa…
cuichenx Mar 30, 2026
fa2a200
fix: Fix llm_sft.sh REPO_ROOT path, packing config, and test mock
cuichenx Mar 31, 2026
b425c64
Merge origin/main into chcui/qwen35-llm-sft-recipe
cuichenx May 4, 2026
5a5d772
Merge remote-tracking branch 'origin/main' into chcui/qwen35-llm-sft-…
cuichenx May 4, 2026
c7510dd
[test] feat: add T1.4 streaming skip test and T2.2 functional vision-…
cuichenx May 5, 2026
837874e
[test] feat: add T2.5 optimizer state functional test for Qwen3.5 LLM…
cuichenx May 5, 2026
1478bbc
[test] feat: add T2.6/T2.7/T2.8 functional tests for Qwen3.5 LLM-only…
cuichenx May 5, 2026
6d59e64
Merge branch 'main' into chcui/qwen35-llm-sft-recipe
cuichenx May 5, 2026
6c1384b
[test] fix: move qwen35 LLM functional tests to test_groups, add laun…
cuichenx May 5, 2026
833db9a
[recipe, model] refactor: Skip vision tower init in Qwen3.5 LLM-only SFT
cuichenx May 7, 2026
2e37443
[ckpt, model] refactor: remove unused skip_megatron_param_globs API
cuichenx May 11, 2026
ae69676
[model, recipe] refactor: rename Qwen3.5-VL provider field init_visio…
cuichenx May 11, 2026
76a6831
[test] test: verify checkpoint resume via sentinel weight round-trip
cuichenx May 11, 2026
b7c21d1
[test] test: extend weight parity to fused QKV and gate_up layouts
cuichenx May 11, 2026
6708918
[test] test: add peak GPU memory ceiling to T2.5
cuichenx May 11, 2026
b9ed11f
[recipe, test] refactor: drop no-op freeze_vision_* in qwen35_llm recipe
cuichenx May 11, 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
24 changes: 24 additions & 0 deletions examples/models/vlm/qwen35_vl/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,30 @@ Pretraining is not verified for this model.

See the [slurm_sft.sh](slurm_sft.sh) script for full parameter fine-tuning with configurable model sizes.

### LLM-only fine-tuning (SQuAD, HF weights without vision)

The `qwen35_llm_*_sft_config` recipes in `megatron.bridge.recipes` load **language-model weights only** from a Qwen3.5-VL HuggingFace checkpoint (vision tower stays at initialization; see `skip_megatron_param_globs` in [`qwen35_llm.py`](../../../../src/megatron/bridge/recipes/qwen_vl/qwen35_llm.py)). Training uses the default SQuAD setup from `_sft_common` and **`vlm_step`** so the hybrid stack runs on text-only batches.

Run from the Bridge repo root with the generic driver [`scripts/training/run_recipe.py`](../../../../scripts/training/run_recipe.py):

```bash
# 800M dense (TP=1 PP=1, 8 GPUs)
uv run torchrun --nproc_per_node=8 scripts/training/run_recipe.py \
--recipe qwen35_llm_800m_sft_config \
--step_func vlm_step \
--hf_path Qwen/Qwen3.5-0.8B

# 35B-A3B MoE (TP=2 PP=1 EP=16)
uv run torchrun --nproc_per_node=8 scripts/training/run_recipe.py \
--recipe qwen35_llm_35b_a3b_sft_config \
--step_func vlm_step \
--hf_path Qwen/Qwen3.5-35B-A3B
```

Available recipes: `qwen35_llm_800m_sft_config`, `qwen35_llm_2b_sft_config`, `qwen35_llm_4b_sft_config`, `qwen35_llm_9b_sft_config`, `qwen35_llm_27b_sft_config`, `qwen35_llm_35b_a3b_sft_config`, `qwen35_llm_122b_a10b_sft_config`, `qwen35_llm_397b_a17b_sft_config`.

See the [llm_sft.sh](llm_sft.sh) wrapper script for a ready-to-run shell command with environment variable overrides.

### Parameter-Efficient Fine-Tuning (PEFT) with LoRA

See the [slurm_peft.sh](slurm_peft.sh) script for LoRA fine-tuning with configurable model sizes.
Expand Down
57 changes: 57 additions & 0 deletions examples/models/vlm/qwen35_vl/llm_sft.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#!/usr/bin/env bash

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.

Nit: This file has mode 100644 (not executable), but it has a shebang and the README tells users to run it directly (./examples/models/vlm/qwen35_vl/llm_sft.sh). Consider chmod +x.

# 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.
set -euo pipefail

# ==============================================================================
# Qwen3.5 LLM-only SFT on SQuAD (HF VLM checkpoint, vision weights skipped)
#
# Uses scripts/training/run_recipe.py with:
# - Recipes from megatron.bridge.recipes (qwen35_llm_*_sft_config)
# - --step_func vlm_step (text-only batches; no images)
#
# From the Bridge repository root:
# ./examples/models/vlm/qwen35_vl/llm_sft.sh
#
# Environment overrides:
# NPROC_PER_NODE GPUs per node (default: 8)
# RECIPE Recipe name (default: qwen35_llm_800m_sft_config)
# HF_PATH HF model id or local directory (default: Qwen/Qwen3.5-0.8B)
# EXTRA_ARGS Extra args passed to run_recipe.py (Hydra-style overrides)
#
# Examples:
# RECIPE=qwen35_llm_2b_sft_config HF_PATH=Qwen/Qwen3.5-2B ./examples/models/vlm/qwen35_vl/llm_sft.sh
#
# EXTRA_ARGS="train.train_iters=500 checkpoint.save=/tmp/qwen35_llm_ckpt" \
# ./examples/models/vlm/qwen35_vl/llm_sft.sh
#
# Optional: pin dataset via run_recipe (default SQuAD matches the recipe):
# EXTRA_ARGS="--dataset llm-finetune dataset.dataset_name=squad" \
# ./examples/models/vlm/qwen35_vl/llm_sft.sh
# ==============================================================================

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
REPO_ROOT="$(cd "${SCRIPT_DIR}/../../../.." && pwd)"
cd "${REPO_ROOT}"

NPROC_PER_NODE="${NPROC_PER_NODE:-8}"
RECIPE="${RECIPE:-qwen35_llm_800m_sft_config}"
HF_PATH="${HF_PATH:-Qwen/Qwen3.5-0.8B}"

# shellcheck disable=SC2086
exec uv run torchrun --nproc_per_node="${NPROC_PER_NODE}" scripts/training/run_recipe.py \
--recipe "${RECIPE}" \
--step_func vlm_step \
--hf_path "${HF_PATH}" \
${EXTRA_ARGS:-}
31 changes: 26 additions & 5 deletions src/megatron/bridge/models/conversion/auto_bridge.py
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,7 @@ def load_hf_weights(
model: list[MegatronModelT],
hf_path: str | Path | None = None,
allowed_mismatched_params: list[str] | None = None,
skip_megatron_param_globs: list[str] | None = None,
) -> None:
"""
Load HuggingFace weights into a Megatron model.
Expand All @@ -403,6 +404,8 @@ def load_hf_weights(
from the bridge's hf_pretrained instance
allowed_mismatched_params: Optional list of parameter names or patterns
to allow mismatch (skip instead of raise error).
skip_megatron_param_globs: Optional :mod:`fnmatch` patterns for Megatron
parameter names to skip loading (see :meth:`MegatronModelBridge.load_weights_hf_to_megatron`).

Returns:
The input model with loaded weights
Expand Down Expand Up @@ -434,7 +437,12 @@ def load_hf_weights(
trust_remote_code = getattr(self.hf_pretrained, "trust_remote_code", False)
pre_trained = PreTrainedCausalLM.from_pretrained(hf_path, trust_remote_code=trust_remote_code)
bridge = self._model_bridge
bridge.load_weights_hf_to_megatron(pre_trained, model, allowed_mismatched_params=allowed_mismatched_params)
bridge.load_weights_hf_to_megatron(
pre_trained,
model,
allowed_mismatched_params=allowed_mismatched_params,
skip_megatron_param_globs=skip_megatron_param_globs,
)
# Get unquantized_state_dict from the bridge instance that was used for optimizer reload
self.unquantized_state_dict = getattr(bridge, "unquantized_state_dict", None)
return model
Expand Down Expand Up @@ -1278,17 +1286,23 @@ def to_megatron_model(
self,
load_weights: bool = True,
hf_path: str | Path | None = None,
skip_megatron_param_globs: list[str] | None = None,
**kwargs: Unpack[GetModelKwargs],
) -> list[MegatronModelT]:
provider = self.to_megatron_provider(load_weights, hf_path)
provider = self.to_megatron_provider(load_weights, hf_path, skip_megatron_param_globs)

# Finalize the provider before creating models
if hasattr(provider, "finalize"):
provider.finalize()

return provider.provide_distributed_model(**kwargs)

def to_megatron_provider(self, load_weights: bool = True, hf_path: str | Path | None = None) -> GPTModelProvider:
def to_megatron_provider(
self,
load_weights: bool = True,
hf_path: str | Path | None = None,
skip_megatron_param_globs: list[str] | None = None,
) -> GPTModelProvider:
"""
Convert to a Megatron model provider.

Expand All @@ -1303,6 +1317,8 @@ def to_megatron_provider(self, load_weights: bool = True, hf_path: str | Path |
hf_path: Optional path to load weights from. If None, uses weights
from the bridge's hf_pretrained instance. Useful for loading
weights from a different checkpoint.
skip_megatron_param_globs: When ``load_weights`` is True, optional
:mod:`fnmatch` patterns for Megatron parameters to skip (e.g. vision-only weights).

Returns:
GPTModelProvider: A configured model provider ready to create
Expand Down Expand Up @@ -1338,15 +1354,20 @@ def to_megatron_provider(self, load_weights: bool = True, hf_path: str | Path |
)
# Skip weights initialization since we are going to load weights
provider.perform_initialization = False
load_kw = {}
if skip_megatron_param_globs is not None:
load_kw["skip_megatron_param_globs"] = skip_megatron_param_globs
if hf_path is None:
provider.register_pre_wrap_hook(
partial(self._model_bridge.load_weights_hf_to_megatron, self.hf_pretrained)
partial(self._model_bridge.load_weights_hf_to_megatron, self.hf_pretrained, **load_kw)
)
else:
# Load from specified path
trust_remote_code = getattr(self.hf_pretrained, "trust_remote_code", False)
pre_trained = PreTrainedCausalLM.from_pretrained(hf_path, trust_remote_code=trust_remote_code)
provider.register_pre_wrap_hook(partial(self._model_bridge.load_weights_hf_to_megatron, pre_trained))
provider.register_pre_wrap_hook(
partial(self._model_bridge.load_weights_hf_to_megatron, pre_trained, **load_kw)
)

hf_identifier: str | None = None
if hf_path is not None:
Expand Down
23 changes: 23 additions & 0 deletions src/megatron/bridge/models/conversion/model_bridge.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,21 @@ class WeightConversionTask(Generic[MappingT]):
param_weight: Optional[torch.Tensor] = None


def megatron_param_matches_skip_globs(
global_param_name: str, local_param_name: str, globs: Optional[List[str]]
) -> bool:
"""Return True if a Megatron parameter should be skipped during HF→Megatron load.

Patterns use :mod:`fnmatch` (e.g. ``*vision_model*``).
"""
if not globs:
return False
for pattern in globs:
if fnmatch.fnmatch(global_param_name, pattern) or fnmatch.fnmatch(local_param_name, pattern):
return True
return False


class _HFNameSuffixMapping:
"""A lightweight wrapper mapping that appends a suffix to all HF param names on export.

Expand Down Expand Up @@ -845,6 +860,7 @@ def load_weights_hf_to_megatron(
hf_pretrained: HFPreTrained,
megatron_model: Union[MegatronModel, List[MegatronModel]],
allowed_mismatched_params: Optional[List[str]] = None,
skip_megatron_param_globs: Optional[List[str]] = None,
) -> List[MegatronModel]:
"""Load HuggingFace weights into Megatron models.

Expand All @@ -862,6 +878,8 @@ def load_weights_hf_to_megatron(
or list of model instances (one per virtual pipeline stage).
allowed_mismatched_params (Optional[List[str]]): List of parameter names or patterns
to allow mismatch (skip instead of raise error).
skip_megatron_param_globs (Optional[List[str]]): If set, skip loading parameters whose
global or local name matches a :mod:`fnmatch` pattern (e.g. ``["*vision_model*"]``).

Returns:
List[MegatronModel]: The input megatron_model as a list with loaded weights.
Expand Down Expand Up @@ -923,6 +941,8 @@ def load_weights_hf_to_megatron(
# None means megatron module not on current rank, skip if this task is not going to happen
if task.megatron_module is None:
continue
if megatron_param_matches_skip_globs(task.global_param_name, task.param_name, skip_megatron_param_globs):
continue
# 1) Fetch source tensor(s) from HF state dict, with caching for grouped mappings
hf_param_key = str(task.mapping.hf_param)
is_grouped = getattr(task.mapping, "is_grouped_export", False)
Expand Down Expand Up @@ -999,6 +1019,7 @@ def stream_weights_hf_to_megatron(
hf_pretrained: HFPreTrained,
megatron_model: Union[MegatronModel, List[MegatronModel]],
conversion_tasks: Optional[List[WeightConversionTask]] = None,
skip_megatron_param_globs: Optional[List[str]] = None,
) -> Iterable[MegatronWeightTuple]:
"""Generator variant of load_weights_hf_to_megatron for streaming weight conversion.

Expand Down Expand Up @@ -1051,6 +1072,8 @@ def stream_weights_hf_to_megatron(
# None means megatron module not on current rank, skip if this task is not going to happen
if task.megatron_module is None:
continue
if megatron_param_matches_skip_globs(task.global_param_name, task.param_name, skip_megatron_param_globs):
continue
hf_state_dict: Mapping[str, torch.Tensor] = hf_pretrained.state
if isinstance(task.mapping.hf_param, str):
hf_weights = hf_state_dict[task.mapping.hf_param]
Expand Down
21 changes: 21 additions & 0 deletions src/megatron/bridge/recipes/qwen_vl/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,17 @@
)

# Qwen3.5 models
from .qwen35_llm import (
QWEN35_LLM_SFT_SKIP_VISION_GLOBS,
qwen35_llm_2b_sft_config,
qwen35_llm_4b_sft_config,
qwen35_llm_9b_sft_config,
qwen35_llm_27b_sft_config,
qwen35_llm_35b_a3b_sft_config,
qwen35_llm_122b_a10b_sft_config,
qwen35_llm_397b_a17b_sft_config,
qwen35_llm_800m_sft_config,
)
from .qwen35_vl import (
qwen35_vl_2b_peft_config,
qwen35_vl_2b_sft_config,
Expand All @@ -64,6 +75,16 @@


__all__ = [
# Qwen3.5 LLM-only (text) SFT configs
"QWEN35_LLM_SFT_SKIP_VISION_GLOBS",
"qwen35_llm_800m_sft_config",
"qwen35_llm_2b_sft_config",
"qwen35_llm_4b_sft_config",
"qwen35_llm_9b_sft_config",
"qwen35_llm_27b_sft_config",
"qwen35_llm_35b_a3b_sft_config",
"qwen35_llm_122b_a10b_sft_config",
"qwen35_llm_397b_a17b_sft_config",
# Qwen3.5-VL pretrain configs
"qwen35_vl_9b_pretrain_mock_config",
"qwen35_vl_35b_a3b_pretrain_mock_config",
Expand Down
Loading
Loading