Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
16 commits
Select commit Hold shift + click to select a range
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ For a step-by-step walkthrough, see the [Getting started tutorial](https://dojo.

| Family | Models |
|---|---|
| Beam | <ul><li>[Beam-1.0-501B-A23B](https://huggingface.co/reflection/Beam-1.0-501B-A23B)</li></ul> |
| DeepSeek | <ul><li>[DeepSeek-V4.1-Flash](https://huggingface.co/deepseek-ai/DeepSeek-V4.1-Flash)</li><li>[DeepSeek-V4.1-Flash-1017](https://huggingface.co/deepseek-ai/DeepSeek-V4.1-Flash-1017)</li></ul> |
| GLM | <ul><li>[GLM-4.7](https://huggingface.co/zai-org/GLM-4.7)</li><li>[GLM-5.3-Flash](https://huggingface.co/zai-org/GLM-5.3-Flash)</li></ul> |
| Gemma | <ul><li>[gemma-4-26B-A4B-it](https://huggingface.co/google/gemma-4-26B-A4B-it)</li></ul> |
Expand Down
3 changes: 3 additions & 0 deletions docs-next/public/llms.txt
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ Repo: https://github.com/modal-projects/modal-dojo

### Models

- [Beam_1_0_501B_A23B](https://dojo.modal.dev/reference/beam_1_0_501b_a23b)
- [Beam_1_0_501B_A23B_LoRA](https://dojo.modal.dev/reference/beam_1_0_501b_a23b_lora)
- [DeepSeek_V4_1_Flash](https://dojo.modal.dev/reference/deepseek_v4_1_flash)
- [DeepSeek_V4_1_Flash_LoRA](https://dojo.modal.dev/reference/deepseek_v4_1_flash_lora)
- [Gemma4_26B_A4B](https://dojo.modal.dev/reference/gemma4_26b_a4b)
Expand Down Expand Up @@ -102,6 +104,7 @@ Repo: https://github.com/modal-projects/modal-dojo

### Recipes

- [Beam_1_0_501B_A23B_LoRA_256k_Recipe](https://dojo.modal.dev/reference/beam_1_0_501b_a23b_lora_256k_recipe)
- [DeepSeek_V4_1_Flash_LoRA_Recipe](https://dojo.modal.dev/reference/deepseek_v4_1_flash_lora_recipe)
- [DeepSeek_V4_1_Flash_Recipe](https://dojo.modal.dev/reference/deepseek_v4_1_flash_recipe)
- [Gemma4_26B_A4B_Recipe](https://dojo.modal.dev/reference/gemma4_26b_a4b_recipe)
Expand Down
9 changes: 9 additions & 0 deletions modal_dojo/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@
from importlib import import_module

_EXPORTS = {
"Beam_1_0_501B_A23B_LoRA_256k_Recipe": (
"modal_dojo.train_recipes.miles_recipe",
"Beam_1_0_501B_A23B_LoRA_256k_Recipe",
),
"Beam_1_0_501B_A23B": ("modal_dojo.common.models", "Beam_1_0_501B_A23B"),
"Beam_1_0_501B_A23B_LoRA": ("modal_dojo.common.models", "Beam_1_0_501B_A23B_LoRA"),
"Checkpoint": ("modal_dojo.common.checkpoint", "Checkpoint"),
"CheckpointType": ("modal_dojo.common.checkpoint", "CheckpointType"),
"convert_megatron_checkpoint_to_hf": (
Expand Down Expand Up @@ -220,6 +226,9 @@
}

__all__ = [
"Beam_1_0_501B_A23B_LoRA_256k_Recipe",
"Beam_1_0_501B_A23B",
"Beam_1_0_501B_A23B_LoRA",
"CustomDeployment",
"DASHBOARD_OVERLAY_VOLUME_NAME",
"DashboardComponent",
Expand Down
3 changes: 3 additions & 0 deletions modal_dojo/common/models/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from .beam import Beam_1_0_501B_A23B, Beam_1_0_501B_A23B_LoRA
from .base import (
HFModelConfiguration,
ModelArchitecture,
Expand Down Expand Up @@ -33,6 +34,8 @@
from .qwen3_vl_8b import Qwen3_VL_8B

__all__ = [
"Beam_1_0_501B_A23B",
"Beam_1_0_501B_A23B_LoRA",
"HFModelConfiguration",
"ModelArchitecture",
"ModelConfig",
Expand Down
14 changes: 14 additions & 0 deletions modal_dojo/common/models/beam.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
"""Reflection Beam's hybrid attention and MoE semantics are supplied by Miles Bridge."""

from .base import HFModelConfiguration


class Beam_1_0_501B_A23B(HFModelConfiguration):
"""Reflection Beam 1.0: 501B total parameters, 23B active."""

model_name = "reflection/Beam-1.0-501B-A23B"
revision = "50a18f8ccf28c75cfdfd9cfe602451c5c81acaef"


class Beam_1_0_501B_A23B_LoRA(Beam_1_0_501B_A23B):
"""Select the rank-16 Bridge LoRA preset."""
7 changes: 7 additions & 0 deletions modal_dojo/common/models/validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

from ..framework import Framework
from .base import ModelConfig
from .beam import Beam_1_0_501B_A23B_LoRA
from .deepseek_v41_flash import DeepSeek_V4_1_Flash, DeepSeek_V4_1_Flash_LoRA
from .gemma4_26b_a4b import Gemma4_26B_A4B
from .glm_4_7 import GLM_4_7
Expand Down Expand Up @@ -85,6 +86,12 @@ def find(cls, name: str) -> "_ValidationConfig":


VALIDATION_CONFIGS: set[_ValidationConfig] = {
_ValidationConfig(
"Beam-1.0-501B-A23B-LoRA",
Beam_1_0_501B_A23B_LoRA,
Framework.MILES,
timeout_s=2 * 60 * 60,
),
_ValidationConfig(
"Muse-Spark-Open", Muse_Spark_Open, Framework.MILES, timeout_s=24 * 60 * 60
),
Expand Down
7 changes: 6 additions & 1 deletion modal_dojo/common/sample_extraction.py
Original file line number Diff line number Diff line change
Expand Up @@ -631,6 +631,7 @@ def _sample_to_dict(
image_store: RolloutImageStore | None = None,
include_trajectory: bool = False,
n_samples_per_prompt: int = 1,
reward_key: str | None = None,
) -> dict[str, Any]:
"""Best-effort extraction of (prompt, response, reward, metadata) from a
slime Sample-like object. Duck-typed so we don't import slime here."""
Expand Down Expand Up @@ -709,7 +710,11 @@ def get(key: str, default: Any = None) -> Any:
metadata["_metadata_type"] = "image"

response_text = _coerce_text(response)
# Score via gym Sample: numeric reward, else metadata["shaped_reward"] (OPD).
# Frameworks train on the configured scalar inside dictionary rewards.
# Do not guess a key: OPD dictionaries can instead hold teacher payloads.
if isinstance(reward, dict) and reward_key:
reward = reward.get(reward_key)
# Fall back to metadata["shaped_reward"] for non-scalar rewards (OPD).
numeric_reward = (
float(reward)
if isinstance(reward, (int, float)) and not isinstance(reward, bool)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
"""
image: radixark/miles:v0.1.2
file: megatron/core/transformer/moe/experts.py

Bound Beam expert activation temporaries without enabling fused numerics.

TODO: Remove once the pinned Megatron bounds expert activation temporaries
with equivalent BF16 rounding and supports Bridge LoRA, CP2, and full recompute.
The open chunked-EP implementation is related, not a drop-in replacement:
it requires MXFP8/CP1 and rejects full recompute.
https://github.com/NVIDIA/Megatron-LM/pull/7331
"""

from pathlib import Path

HELPER = '''class _BeamChunkedSwiGLU(torch.autograd.Function):
"""Bound temporaries while preserving Beam's eager BF16 rounding points."""

@staticmethod
def _activation(x, probs):
gate, value = x.chunk(2, dim=-1)
activated = F.silu(gate) * (value + 0.0)
return (activated * probs).to(x.dtype)

@staticmethod
def forward(ctx, x, probs):
ctx.save_for_backward(x, probs)
output = x.new_empty((*x.shape[:-1], x.shape[-1] // 2))
for start in range(0, x.shape[0], 8192):
end = start + 8192
output[start:end].copy_(_BeamChunkedSwiGLU._activation(x[start:end], probs[start:end]))
return output

@staticmethod
def backward(ctx, grad_output):
x, probs = ctx.saved_tensors
dx = torch.empty_like(x) if ctx.needs_input_grad[0] else None
dp = torch.empty_like(probs) if ctx.needs_input_grad[1] else None
for start in range(0, x.shape[0], 8192):
end = start + 8192
with torch.enable_grad():
xc = x[start:end].detach().requires_grad_(True)
pc = probs[start:end].detach().requires_grad_(True)
y = _BeamChunkedSwiGLU._activation(xc, pc)
gx, gp = torch.autograd.grad(y, (xc, pc), grad_output[start:end])
if dx is not None:
dx[start:end].copy_(gx)
if dp is not None:
dp[start:end].copy_(gp)
return dx, dp


'''
ANCHOR = """ def bias_act_func(intermediate_parallel, bias_parallel, permuted_probs):
"""
REPLACEMENT = """ def bias_act_func(intermediate_parallel, bias_parallel, permuted_probs):
# MODAL_DOJO_BEAM_CHUNKED_ACTIVATION: preserve unfused model numerics.
if (
intermediate_parallel.dtype == torch.bfloat16
and intermediate_parallel.ndim == 2
and bias_parallel is None
and permuted_probs is not None
and self.activation_func == F.silu
and self.config.gated_linear_unit
and self.config.gated_activation_func is None
and self.config.glu_linear_offset == 0.0
and self.config.activation_func_clamp_value is None
and self.config.moe_mlp_glu_interleave_size is None
and not self.config.moe_activation_in_fp32
and not self.config.use_te_activation_func
and not self.config.bias_activation_fusion
):
return _BeamChunkedSwiGLU.apply(intermediate_parallel, permuted_probs)
"""


def patch_source(source: str) -> str:
if HELPER in source and REPLACEMENT in source:
return source
marker = "class TEGroupedMLP(MegatronModule):"
if source.count(ANCHOR) != 1 or source.count(marker) != 1:
raise RuntimeError("Pinned expert activation implementation changed")
return source.replace(marker, HELPER + marker).replace(ANCHOR, REPLACEMENT)


def main() -> None:
path = Path("/root/Megatron-LM/megatron/core/transformer/moe/experts.py")
path.write_text(patch_source(path.read_text()))
print(f"Applied Beam activation patch to {path}")


if __name__ == "__main__":
main()
45 changes: 45 additions & 0 deletions modal_dojo/frameworks/miles/modal_helpers/patches/patch_beam_cp.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
"""
image: radixark/miles:v0.1.2
commit: https://github.com/radixark/miles/commit/819cfd23ab74de9975931597894500632dd708fc
file: miles/backends/megatron_utils/model_provider.py

Preserve explicit context communication and normalize singleton CLI lists
for the pinned Miles bridge provider.

TODO: Remove once the pinned Miles propagates explicit cp_comm_type and
normalizes singleton CLI lists. The merged runtime-config fix intentionally
leaves cp_comm_type provider-owned, so it does not replace this patch:
https://github.com/radixark/miles/pull/1483
Beam support is still tracked in https://github.com/radixark/miles/pull/4149.
"""

from pathlib import Path

MARKER = "# MODAL_DOJO_BEAM_CP_RUNTIME"
ANCHOR = " provider.context_parallel_size = args.context_parallel_size\n"
INSERT = """ # MODAL_DOJO_BEAM_CP_RUNTIME
if getattr(args, "cp_comm_type", None) is not None:
cp_comm_type = args.cp_comm_type
# CLI nargs produces a singleton list; MCore expects a scalar for all layers.
if isinstance(cp_comm_type, (list, tuple)) and len(cp_comm_type) == 1:
cp_comm_type = cp_comm_type[0]
provider.cp_comm_type = cp_comm_type
"""


def patch_source(source: str) -> str:
if MARKER in source:
return source
if source.count(ANCHOR) != 1:
raise RuntimeError("Pinned Miles bridge CP anchor changed")
return source.replace(ANCHOR, ANCHOR + INSERT)


def main() -> None:
path = Path("/root/miles/miles/backends/megatron_utils/model_provider.py")
path.write_text(patch_source(path.read_text()))
print("Beam bridge runtime patch: cp_comm_type propagated from args")


if __name__ == "__main__":
main()
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
"""
image: radixark/miles:v0.1.2
file: megatron/bridge/peft/{utils,lora_layers}.py

Avoid redundant full-width buffers in Beam's BF16 expert-TP1 adapters.

TODO: Remove once the pinned Bridge avoids the grouped-expert input copy,
full-width scaling temporary, and base-output clone for BF16 expert TP1.
The merged long-context LoRA fix is already reflected in the pinned wrapper;
it still clones the base output and does not fix these grouped-adapter copies:
https://github.com/NVIDIA-NeMo/Megatron-Bridge/pull/5906
Revalidate forward/backward and 256k peak memory before removing this patch.
"""

from pathlib import Path

INPUT = " grouped_input = grouped_inputs[0] if len(grouped_inputs) == 1 else torch.cat(grouped_inputs, dim=0)\n"
INPUT_PATCH = """ # MODAL_DOJO_BEAM_LORA_MEMORY: no padding or sharding at BF16 expert TP1.
if not use_te_fp8 and expert_tp_size == 1:
grouped_input = x
else:
grouped_input = grouped_inputs[0] if len(grouped_inputs) == 1 else torch.cat(grouped_inputs, dim=0)
"""
SCALE = """ if all(pad_len == 0 for pad_len in pad_lengths):
return expert_output * (self.alpha / self.dim)
"""
SCALE_PATCH = """ if all(pad_len == 0 for pad_len in pad_lengths):
if not use_te_fp8 and expert_tp_size == 1:
return expert_output.mul_(self.alpha / self.dim)
return expert_output * (self.alpha / self.dim)
"""


def patch_source(source: str) -> str:
if INPUT_PATCH in source and SCALE_PATCH in source:
return source
if source.count(INPUT) != 1 or source.count(SCALE) != 1:
raise RuntimeError("Pinned grouped expert LoRA implementation changed")
return source.replace(INPUT, INPUT_PATCH).replace(SCALE, SCALE_PATCH)


WRAPPER = """ # Serialize full-width branch buffers to keep long-context peak at two outputs.
combined_output = linear_output.clone()
del linear_output
adapter_output = self.adapter_forward(self.adapter, layernorm_output.contiguous(), *args, **kwargs)
adapter_output = adapter_output.reshape(combined_output.shape)
combined_output.add_(adapter_output)
"""
WRAPPER_PATCH = """ # MODAL_DOJO_BEAM_LORA_ACCUMULATE: leave the base output unmodified.
from megatron.bridge.peft.utils import GroupedExpertLinearAdapter
if (
isinstance(self.adapter, GroupedExpertLinearAdapter)
and (self.adapter.config.expert_tensor_parallel_size or 1) == 1
and not self.adapter._is_te_fp8_enabled()
and linear_output.dtype == torch.bfloat16
):
adapter_output = self.adapter_forward(self.adapter, layernorm_output.contiguous(), *args, **kwargs)
combined_output = adapter_output.reshape(linear_output.shape)
combined_output.add_(linear_output)
else:
combined_output = linear_output.clone()
del linear_output
adapter_output = self.adapter_forward(self.adapter, layernorm_output.contiguous(), *args, **kwargs)
adapter_output = adapter_output.reshape(combined_output.shape)
combined_output.add_(adapter_output)
"""


def patch_wrapper(source: str) -> str:
if WRAPPER_PATCH in source:
return source
if source.count(WRAPPER) != 1:
raise RuntimeError("Pinned LoRA output accumulation changed")
return source.replace(WRAPPER, WRAPPER_PATCH)


def main() -> None:
path = Path(
"/opt/sglang/lib/python3.12/site-packages/megatron/bridge/peft/utils.py"
)
path.write_text(patch_source(path.read_text()))
wrapper = path.with_name("lora_layers.py")
wrapper.write_text(patch_wrapper(wrapper.read_text()))
print(
"Beam expert LoRA memory patch: reuse unpadded input and scale output in place"
)


if __name__ == "__main__":
main()
Loading
Loading