Skip to content
Merged
Show file tree
Hide file tree
Changes from 30 commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
552a8bf
refactor(update-weight): re-home the colocated protocol's backend imp…
yueming-yuan Aug 25, 2026
c5b685a
refactor(update-weight): move the transfer protocols into training_ut…
yueming-yuan Aug 25, 2026
cfe81d8
pre-commit fixes
yueming-yuan Aug 25, 2026
de8453d
fix stale module path in a test docstring
yueming-yuan Aug 25, 2026
701e1a9
merge colocate _group_name fix
yueming-yuan Aug 25, 2026
2d739b4
cascade .rxignore removal
yueming-yuan Aug 25, 2026
9ecb877
refactor(update-weight): group protocols/ and hf_weight_iterator/ pac…
yueming-yuan Aug 25, 2026
6abe3ee
refactor(update-weight): dissolve common.py
yueming-yuan Aug 25, 2026
3ad6be3
cascade from yueming/hf-weight-iterator-colocate
yueming-yuan Aug 25, 2026
c874c64
cascade from yueming/hf-weight-iterator-colocate
yueming-yuan Aug 25, 2026
e4b658d
cascade from yueming/hf-weight-iterator-dist
yueming-yuan Aug 25, 2026
6525c54
cascade from yueming/hf-weight-iterator-colocate
yueming-yuan Aug 26, 2026
7a0a131
cascade from yueming/hf-weight-iterator-colocate
yueming-yuan Aug 26, 2026
7c65be1
cascade from yueming/hf-weight-iterator-dist
yueming-yuan Aug 26, 2026
7ca7a25
restore comments native to the pre-refactor code (moved homes)
yueming-yuan Aug 26, 2026
4883938
restore the remaining native comments
yueming-yuan Aug 26, 2026
aa18ce0
restore the expert-bias comment
yueming-yuan Aug 26, 2026
edd641c
cascade from yueming/hf-weight-iterator-dist
yueming-yuan Aug 26, 2026
1861dd9
cascade from yueming/hf-weight-iterator-bucketing
yueming-yuan Aug 26, 2026
691feed
sort imports
yueming-yuan Aug 26, 2026
28d5ed2
cascade from yueming/hf-weight-iterator-bucketing
yueming-yuan Aug 26, 2026
3320b78
cascade from yueming/hf-weight-iterator-dist
yueming-yuan Aug 26, 2026
94246d4
cascade from yueming/hf-weight-iterator-colocate
yueming-yuan Aug 26, 2026
33ed338
format
yueming-yuan Aug 26, 2026
4190a1d
cascade from yueming/hf-weight-iterator-updater
yueming-yuan Aug 26, 2026
7e05d74
cascade from yueming/hf-weight-iterator-colocate
yueming-yuan Aug 26, 2026
d66f304
cascade from yueming/hf-weight-iterator-refactor
yueming-yuan Aug 26, 2026
417345f
cascade from yueming/hf-weight-iterator-updater
yueming-yuan Aug 27, 2026
85c60f8
cascade from yueming/hf-weight-iterator-dist
yueming-yuan Aug 28, 2026
a0b0615
cascade from yueming/hf-weight-iterator-dist
yueming-yuan Aug 28, 2026
39c8acd
cascade from yueming/hf-weight-iterator-ep-allgather
yueming-yuan Sep 1, 2026
b87846b
cascade from yueming/hf-weight-iterator-colocate
yueming-yuan Sep 1, 2026
e561707
cascade from yueming/hf-weight-iterator-updater
yueming-yuan Sep 1, 2026
4f2a315
cascade from yueming/hf-weight-iterator-updater
yueming-yuan Sep 2, 2026
c0e121b
cascade from yueming/hf-weight-iterator-colocate
yueming-yuan Sep 2, 2026
62f6b35
cascade from yueming/hf-weight-iterator-colocate
yueming-yuan Sep 2, 2026
a9eae86
cascade from yueming/hf-weight-iterator-dist
yueming-yuan Sep 2, 2026
0722a10
cascade from yueming/hf-weight-iterator-colocate
yueming-yuan Sep 3, 2026
a434e26
merge origin/yueming/hf-weight-iterator-colocate
yueming-yuan Sep 3, 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
2 changes: 1 addition & 1 deletion miles/backends/megatron_utils/actor.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@
from .initialize import init, is_first_replica_megatron_main_rank
from .lora_utils import is_lora_enabled, lora_rollout_enabled
from .model import TrainStepOutcome, forward_only, initialize_model_and_optimizer, save, train
from .named_weights import named_params_and_buffers
from .parallel import verify_megatron_parallel_state
from .replay_utils import register_replay_list_moe
from .update_weight.common import named_params_and_buffers

if TYPE_CHECKING:
from miles.ray.rollout.rollout_manager import EnginesAndLock
Expand Down
2 changes: 1 addition & 1 deletion miles/backends/megatron_utils/hf_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from megatron.core.distributed import DistributedDataParallel as DDP

from miles.backends.megatron_utils.lora_utils import is_lora_model, save_lora_checkpoint
from miles.backends.megatron_utils.update_weight.common import named_params_and_buffers
from miles.backends.megatron_utils.named_weights import named_params_and_buffers
from miles.backends.megatron_utils.update_weight.hf_weight_iterator_direct import HfWeightIteratorDirect
from miles.backends.training_utils.parallel import get_parallel_state
from miles.backends.training_utils.weight_update.hf_weight_iterator import WeightUpdatePlacement
Expand Down
5 changes: 0 additions & 5 deletions miles/backends/megatron_utils/lora_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,6 @@
# ---------------------------------------------------------------------------


def lora_base_cpu_backup_enabled(args: Namespace) -> bool:
"""LoRA + --colocate + --lora-base-cpu-backup all set."""
return is_lora_enabled(args) and getattr(args, "colocate", False) and getattr(args, "lora_base_cpu_backup", False)


def sglang_lora_target_all_sentinel(args) -> bool:
"""Hand SGLang the ``"all"`` shorthand so it auto-detects module names (required for Inkling)."""
from miles.utils.chat_template_utils.inkling import is_inkling_checkpoint
Expand Down
130 changes: 130 additions & 0 deletions miles/backends/megatron_utils/named_weights.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
"""Enumerate a megatron model's named parameters and buffers.

Global naming folds virtual-PP and EP offsets into layer indices
(decoder.layers, mtp.layers, expert_bias) so names are comparable across
ranks; witness params are skipped.
"""

import inspect
import re
from argparse import Namespace
from collections.abc import Iterator, Sequence

import torch
from megatron.core.transformer.transformer_layer import get_transformer_layer_offset

from miles.backends.megatron_utils.misc_utils import strip_param_name_prefix
from miles.backends.training_utils.parallel import get_parallel_state


def named_params_and_buffers(
args: Namespace,
model: Sequence[torch.nn.Module],
convert_to_global_name: bool = True,
) -> Iterator[tuple[str, torch.Tensor]]:
if convert_to_global_name:
return _named_params_and_buffers_global(args, model)
return _named_params_and_buffers_vanilla(model)


def _named_params_and_buffers_vanilla(model: Sequence[torch.nn.Module]) -> Iterator[tuple[str, torch.Tensor]]:
for vp_stage, model_module in enumerate(model):

def _compute_fqn(name, vp_stage=vp_stage):
return f"vp_stages.{vp_stage}.{strip_param_name_prefix(name)}"

for name, param in model_module.named_parameters():
if getattr(param, "_is_witness_param", False):
continue
yield _compute_fqn(name), param

for name, buffer in model_module.named_buffers():
# TODO shall we handle (almost) all buffers like Megatron Bridge
if "expert_bias" not in name:
continue
yield _compute_fqn(name), buffer


def _named_params_and_buffers_global(
args: Namespace, model: Sequence[torch.nn.Module]
) -> Iterator[tuple[str, torch.Tensor]]:
"""
Yield (global_name, param/buffer) with consistent names across PP/EP. Adjusts indices for
virtual PP + EP offsets. Handles decoder.layers, mtp.layers (Multi-Token Prediction), expert_bias.
"""
ep_size = get_parallel_state().ep.size
ep_rank = get_parallel_state().ep.rank
if args.num_experts:
expert_offset = ep_rank * args.num_experts // ep_size

sig = inspect.signature(get_transformer_layer_offset)
need_vp_stage = "vp_stage" in sig.parameters

for vp_stage, model_module in enumerate(model):
if need_vp_stage:
layer_offset = get_transformer_layer_offset(model_module.config, vp_stage)
else:
layer_offset = get_transformer_layer_offset(model_module.config)
for name, param in model_module.named_parameters():
if getattr(param, "_is_witness_param", False):
continue
# for model without ddp wrap
if not name.startswith("module.module."):
name = "module." + name

decoder_layers_pattern = r"module\.module\.(?:language_model\.)?decoder\.layers\.(\d+)\.(.+)"
match = re.match(decoder_layers_pattern, name)
if not match:
# MTP (Multi-Token Prediction) layers for speculative decoding
mtp_layers_pattern = r"module\.module\.mtp\.layers\.(\d+)\.(.+)"
match = re.match(mtp_layers_pattern, name)
if not match:
yield name, param
continue

# MTP layer indices start from 0
layer_idx, rest = match.groups()
expert_pattern = r"(transformer_layer|mtp_model_layer)\.mlp\.experts\.(.+)\.weight(\d+)"
match = re.match(expert_pattern, rest)
if not match:
yield name, param
continue

inner, rest, expert_idx = match.groups()
expert_idx = int(expert_idx) + expert_offset
yield (
f"module.module.mtp.layers.{layer_idx}.{inner}.mlp.experts.{rest}.weight{expert_idx}",
param,
)
continue

layer_idx, rest = match.groups()
layer_idx = int(layer_idx) + layer_offset

# this is hardcoded for te grouped matmul
expert_pattern = r"mlp.experts\.(.+)\.weight(\d+)"
match = re.match(expert_pattern, rest)
if match:
rest, expert_idx = match.groups()
expert_idx = int(expert_idx) + expert_offset
yield f"module.module.decoder.layers.{layer_idx}.mlp.experts.{rest}.weight{expert_idx}", param
else:
yield f"module.module.decoder.layers.{layer_idx}.{rest}", param

# treat expert bias as normal parameters
for name, buffer in model_module.named_buffers():
# TODO shall we handle (almost) all buffers like Megatron Bridge
if "expert_bias" not in name:
continue
# for model without ddp wrap
if not name.startswith("module.module."):
name = "module." + name

decoder_layers_pattern = r"module\.module\.(?:language_model\.)?decoder\.layers\.(\d+)\.(.+)"
match = re.match(decoder_layers_pattern, name)
if not match:
yield name, buffer
else:
layer_idx, rest = match.groups()
layer_idx = int(layer_idx) + layer_offset
yield f"module.module.decoder.layers.{layer_idx}.{rest}", buffer
Loading
Loading