Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
f22cec9
Deepspeed + EP changes
yfw Jun 12, 2025
c9626e5
Remove unused
yfw Jun 12, 2025
88303bf
Add deepseek.md
yfw Jun 12, 2025
0a049c8
Update NeMo
yfw Jun 25, 2025
b1bf220
Aggregate/cache all gather objects
guyueh1 Jun 11, 2025
fcc004a
Fix deepcopy error
yfw Jun 26, 2025
91bce4b
More fixes
yfw Jun 26, 2025
9955c40
Fix refit oom + cleanup
yfw Jun 27, 2025
c707989
PP broadcast
yfw Jun 26, 2025
bcf68f9
Cleanup
yfw Jun 27, 2025
3cd24a7
try to handle case where ETP!=TP
ashors1 Jun 21, 2025
ed0d6b1
fixes for etp != tp
ashors1 Jun 24, 2025
b0f6051
Cleanup
yfw Jun 27, 2025
51039e2
Fix offloading for ep
yfw Jun 29, 2025
427548f
Remove deepseek
yfw Jul 1, 2025
aa37e7a
Merge remote-tracking branch 'origin' into yifu/megatron_ep
yfw Jul 1, 2025
41af15d
Fix NeMo
yfw Jul 2, 2025
7ba8dc7
ruff
yfw Jul 2, 2025
ab150c4
Remove debug
yfw Jul 2, 2025
144d57f
cleanup
yfw Jul 2, 2025
d6f730c
ruff
yfw Jul 2, 2025
06b805b
ruff
yfw Jul 2, 2025
70ac17b
Fix test
yfw Jul 2, 2025
aa20ac2
Fix test
yfw Jul 2, 2025
a5ff77d
Remove debug timing code
yfw Jul 2, 2025
7b906da
Address review comments
yfw Jul 2, 2025
0a61c02
Comments
yfw Jul 2, 2025
3a342dd
Fix test + remove unused
yfw Jul 2, 2025
1a8f04c
Use get_process_group_ranks instead of get_all_rank_ids_in_group
yfw Jul 2, 2025
435419b
Add back deleted
yfw Jul 2, 2025
f3a940c
Merge remote-tracking branch 'origin' into yifu/megatron_ep
yfw Jul 3, 2025
d9643ba
lint
yfw Jul 3, 2025
9110864
Update configs
yfw Jul 3, 2025
470ac2a
Forgot to save
yfw Jul 3, 2025
1e22e27
Merge branch 'main' into yifu/megatron_ep
parthchadha Jul 3, 2025
3cd2395
Fix unit test
yfw Jul 7, 2025
c1d0f02
Merge remote-tracking branch 'origin' into yifu/megatron_ep
yfw Jul 7, 2025
2380ab1
Merge remote-tracking branch 'origin' into yifu/megatron_ep
yfw Jul 9, 2025
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 3rdparty/NeMo-workspace/NeMo
Submodule NeMo updated from 4b7ded to 0e0894
6 changes: 6 additions & 0 deletions examples/configs/dpo.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,18 @@ policy:
empty_unused_memory_level: 1
activation_checkpointing: false
tensor_model_parallel_size: 2
expert_tensor_parallel_size: 1
expert_model_parallel_size: 1
pipeline_model_parallel_size: 1
context_parallel_size: 1
pipeline_dtype: ${policy.precision}
num_layers_in_first_pipeline_stage: null
num_layers_in_last_pipeline_stage: null
sequence_parallel: true
freeze_moe_router: false
moe_router_dtype: "fp64"
moe_router_load_balancing_type: "aux_loss"
moe_router_bias_update_rate: 1e-3
#gives ~20% training perf speedup with sequence packing
apply_rope_fusion: True

Expand Down
6 changes: 6 additions & 0 deletions examples/configs/grpo_math_1B_megatron.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,18 @@ policy:
activation_checkpointing: false
converter_type: "Qwen2ForCausalLM"
tensor_model_parallel_size: 1
expert_tensor_parallel_size: 1
expert_model_parallel_size: 1
pipeline_model_parallel_size: 1
num_layers_in_first_pipeline_stage: null
num_layers_in_last_pipeline_stage: null
context_parallel_size: 1
pipeline_dtype: ${policy.precision}
sequence_parallel: false
freeze_moe_router: true
moe_router_dtype: "fp64"
moe_router_load_balancing_type: "none" # "seq_aux_loss" causes logprob error divergence for grpo
moe_router_bias_update_rate: 0.0 # by default, disable bias updates for grpo
#gives ~20% training perf speedup with sequence packing
apply_rope_fusion: True

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,18 @@ policy:
empty_unused_memory_level: 1
activation_checkpointing: false
tensor_model_parallel_size: 2
expert_tensor_parallel_size: 1
expert_model_parallel_size: 1
pipeline_model_parallel_size: 1
context_parallel_size: 1
pipeline_dtype: ${policy.precision}
num_layers_in_first_pipeline_stage: null
num_layers_in_last_pipeline_stage: null
sequence_parallel: true
freeze_moe_router: false
moe_router_dtype: "fp64"
moe_router_load_balancing_type: "aux_loss"
moe_router_bias_update_rate: 1e-3
#gives ~20% training perf speedup with sequence packing
apply_rope_fusion: True

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,18 @@ policy:
empty_unused_memory_level: 1
activation_checkpointing: false
tensor_model_parallel_size: 2
expert_tensor_parallel_size: 1
expert_model_parallel_size: 1
pipeline_model_parallel_size: 2
context_parallel_size: 1
pipeline_dtype: ${policy.precision}
num_layers_in_first_pipeline_stage: null
num_layers_in_last_pipeline_stage: null
sequence_parallel: true
freeze_moe_router: false
moe_router_dtype: "fp64"
moe_router_load_balancing_type: "aux_loss"
moe_router_bias_update_rate: 1e-3
#gives ~20% training perf speedup with sequence packing
apply_rope_fusion: True

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,18 @@ policy:
empty_unused_memory_level: 1
activation_checkpointing: false
tensor_model_parallel_size: 2
expert_tensor_parallel_size: 1
expert_model_parallel_size: 1
pipeline_model_parallel_size: 2
context_parallel_size: 1
pipeline_dtype: ${policy.precision}
num_layers_in_first_pipeline_stage: null
num_layers_in_last_pipeline_stage: null
sequence_parallel: false
freeze_moe_router: false
moe_router_dtype: null
moe_router_load_balancing_type: "aux_loss"
moe_router_bias_update_rate: 1e-3
#gives ~20% training perf speedup with sequence packing
apply_rope_fusion: True

Expand Down
6 changes: 6 additions & 0 deletions examples/configs/sft.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,18 @@ policy:
empty_unused_memory_level: 1
activation_checkpointing: false
tensor_model_parallel_size: 2
expert_tensor_parallel_size: 1
expert_model_parallel_size: 1
pipeline_model_parallel_size: 2
context_parallel_size: 1
pipeline_dtype: ${policy.precision}
num_layers_in_first_pipeline_stage: null
num_layers_in_last_pipeline_stage: null
sequence_parallel: false
freeze_moe_router: false
moe_router_dtype: null
moe_router_load_balancing_type: "aux_loss"
moe_router_bias_update_rate: 1e-3
#gives ~20% training perf speedup with sequence packing
apply_rope_fusion: True

Expand Down
4 changes: 4 additions & 0 deletions nemo_rl/algorithms/grpo.py
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,10 @@ def refit_policy_generation(
grouped_param_keys = policy.prepare_weights_for_ipc(
_refit_buffer_size_gb=_refit_buffer_size_gb
)
total_num_keys = sum(len(k) for k in grouped_param_keys)
print(
f"[Refit] Split {total_num_keys} keys into {len(grouped_param_keys)} groups"
)
# do update
for keys in grouped_param_keys:
ipc_handles = policy.get_weights_ipc_handles(keys)
Expand Down
1 change: 0 additions & 1 deletion nemo_rl/models/generation/vllm_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@ def update_weights_from_ipc_handles(self, ipc_handles):

# Load weights into the model
self.model_runner.model.load_weights(weights=weights)
torch.cuda.synchronize()
return True
except Exception as e:
print(
Expand Down
6 changes: 5 additions & 1 deletion nemo_rl/models/megatron/converters/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,15 @@
# limitations under the License.

from .common import (
get_global_expert_num,
get_global_layer_num,
get_local_expert_num,
get_local_layer_num,
)

__all__ = [
"get_local_layer_num",
"get_global_expert_num",
"get_global_layer_num",
"get_local_expert_num",
"get_local_layer_num",
]
65 changes: 61 additions & 4 deletions nemo_rl/models/megatron/converters/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,15 @@ def get_local_layer_num(s):
return number


def get_local_expert_num(s):
"""Assumes experts have 'experts.' in their name. Expert num succeeds '.weight'."""
segments = s.split(".")
if "experts" not in segments or segments[-1] == "_extra_state":
return None
number = int(segments[-1].strip("weight"))
return number


def get_global_layer_num(s, cfg):
"""Assumes layer number is preceeded by 'layers.'.

Expand Down Expand Up @@ -79,6 +88,23 @@ def get_global_layer_num(s, cfg):
return global_offset + local_layer_num


def get_global_expert_num(s, cfg):
"""Assumes experts have 'experts.' in their name. Expert num succeeds '.weight'.

Assumes expert model parallel size is set.
In the state dict, the expert number is the local expert number (expert local).
This function converts the local expert number to the global expert number.
"""
local_expert_num = get_local_expert_num(s)
global_expert_num = (
parallel_state.get_expert_model_parallel_rank()
* cfg.num_moe_experts
// parallel_state.get_expert_model_parallel_world_size()
+ local_expert_num
)
return global_expert_num


def get_global_key_from_local_key(local_key, model_cfg):
local_layer = get_local_layer_num(local_key)
if local_layer is not None:
Expand All @@ -87,6 +113,11 @@ def get_global_key_from_local_key(local_key, model_cfg):
global_key = re.sub(r"(?<=layers\.)\d+", str(global_layer), local_key, count=1)
else:
global_key = local_key
local_expert = get_local_expert_num(global_key)
if local_expert is not None:
global_expert = get_global_expert_num(global_key, model_cfg)
# Replace the last occurrence of the digits after "weight" with the global expert number.
global_key = re.sub(r"(?<=weight)\d+", str(global_expert), global_key)
return global_key


Expand All @@ -97,14 +128,27 @@ def split_fc1_tp(ctx: TransformCTX, linear_fc1: torch.Tensor):
# [ gate_tp1 ] --/ [ up_tp0 ] --/ (split up)
# [ up_tp1 ] [ up_tp1 ]
megatron_config = ctx.source.config
# TODO: handle expert_tensor_parallel_size
tp = megatron_config.tensor_model_parallel_size
linear_fc1 = einops.rearrange(linear_fc1, "(t c d) a1 -> c (t d) a1", c=2, t=tp)
mlp_gate_proj_weight = linear_fc1[0]
mlp_up_proj_weight = linear_fc1[1]
return mlp_gate_proj_weight, mlp_up_proj_weight


def split_fc1_etp(ctx: TransformCTX, linear_fc1: torch.Tensor):
# gate proj and up proj are mixed right now, and we need to reshape them
# [ gate_tp0 ] [ gate_tp0 ]
# [ up_tp0 ] --\ [ gate_tp1 ] --\ (split gate)
# [ gate_tp1 ] --/ [ up_tp0 ] --/ (split up)
# [ up_tp1 ] [ up_tp1 ]
megatron_config = ctx.source.config
etp = megatron_config.expert_tensor_parallel_size
linear_fc1 = einops.rearrange(linear_fc1, "(t c d) a1 -> c (t d) a1", c=2, t=etp)
mlp_gate_proj_weight = linear_fc1[0]
mlp_up_proj_weight = linear_fc1[1]
return mlp_gate_proj_weight, mlp_up_proj_weight


def split_qkv_gpu(ctx: TransformCTX, linear_qkv: torch.Tensor):
"""Split interleave-concatenated qkv to q, k, v.

Expand Down Expand Up @@ -177,8 +221,13 @@ def update_transforms_for_nemorl(export_transforms):
# In place update
for transform in export_transforms:
if transform.transform.__name__ == "split_fc1":
# Need to modify this transform to take into account the TP size
transform.transform = split_fc1_tp
if (
"experts" in transform.source_key
and "shared_experts" not in transform.source_key
):
transform.transform = split_fc1_etp
else:
transform.transform = split_fc1_tp
elif transform.transform.__name__ == "split_qkv":
# This transform previously moved qkv weights to cpu
transform.transform = split_qkv_gpu
Expand Down Expand Up @@ -211,7 +260,15 @@ def __init__(self, hf_model_name, megatron_model):
)
pp_gathered_global_keys = list({k for l in pp_gathered_global_keys for k in l}) # type: ignore

global_keys = pp_gathered_global_keys
ep_group = parallel_state.get_expert_model_parallel_group()
ep_world_size = parallel_state.get_expert_model_parallel_world_size()
ep_gathered_global_keys = [None] * ep_world_size
torch.distributed.all_gather_object(
ep_gathered_global_keys, pp_gathered_global_keys, group=ep_group
)
ep_gathered_global_keys = list({k for l in ep_gathered_global_keys for k in l})

global_keys = ep_gathered_global_keys
global_keys_map = {k: None for k in global_keys}

if "qwen" in hf_model_name.lower():
Expand Down
Loading