Skip to content

Add Beam BF16 LoRA support on Miles v0.2.0 - #37

Merged
frrad merged 4 commits into
spindle-miles-v0.2.0from
fred/beam-spindle-v020
Nov 17, 2026
Merged

frrad merged 4 commits into
spindle-miles-v0.2.0from
fred/beam-spindle-v020

Conversation

@frrad

@frrad frrad commented Nov 12, 2026 •

Copy link
Copy Markdown

Summary

Adds Beam BF16 training with standard LoRA on Miles v0.2.0. The model, Bridge provider, weight mappings and fused Q/gate/K/V adapter export are ported from radixark/miles#4149, which is still open and is based on v0.1.2.

Scope: trainer only. The bundled SGLang (fd017154) raises NotImplementedError for Beam with LoRA, so Miles' own rollout cannot serve Beam adapters. Spindle samples with its own Beam-enabled SGLang.

Changes

Ported from radixark#4149:

  • Beam's Bridge provider, model implementation, architecture args and model-args snapshot. The provider additionally enforces FP32 routing with Torch matmul immediately before model construction, overriding the CLI router settings copied by shared LoRA setup. This preserves Beam's FP32 router weights and sampler routing numerics without requiring launcher flags.
  • Registering BeamConfig, Beam's LoRA targets, and keeping the attention gate (self_attn.gate_proj) out of the MoE expert checks, unchanged.
  • Shared LoRA setup retains the base branch's runtime-config handling, expert-TP validation, multi-LoRA-only router-bias freeze and distributed-model initialization. The four broader LoRA changes from feat(beam): add Reflection Beam 1.0 RL support radixark/miles#4149 are deliberately excluded from this port.

New in this PR:

  • canonical_lora on Beam's fused Q/gate/K/V layer now raises an error. Bridge's split wrapper only knows Q/K/V, so the gate would get no adapter. Upstream has no check and would silently mis-resolve this. The check runs when LoRA is applied, after the model is built, not during argument parsing.
  • Two test fixtures were updated for the released SGLang: the fused-kernel input normalization, and the module inventory, which is now built from the Bridge mapping registry.

Not ported, because v0.2.0 already covers it or it's out of scope: DistributedOptimizer state in LoRA checkpoints (v0.2.0 has its own), the SGLang target-module rewrite, the launchers, the reward function, the Docker image and the manual scripts. Beam's provider enforces its required router settings even when a launcher omits those flags.

Base: spindle-miles-v0.2.0 @ 15afe217 (upstream v0.2.0 8e419cc7 plus #39 and #38). SGLang and Bridge (a0c6d6e0) pins are unchanged.

Validation

Run at 69abc2c0, before the rebase, on the v0.2.0 image (Torch 2.14.1, Transformers 5.19.0):

  • 23 Beam tests passed on one H200, none skipped. They cover config and provider registration, softcap and residual numerics against the SGLang kernels, LoRA targets, Q/gate/K/V export and the rejection checks.
  • The Beam checkpoint config resolves to 218 mappings, none grouped.
  • pre-commit passed.

After the review changes: 71 focused CPU tests passed; the Beam architecture test module (including the new router-override regression) skipped because local SGLang/Megatron dependencies are unavailable. A separate CPU check executed the production Beam provider class with a stubbed model-construction boundary and confirmed that FP32/Torch routing and disabled fusions reach construction after simulated CLI overrides. Pre-commit passed for all changed files. Bridge configuration fixture tests could not collect because SGLang is missing. The earlier H200 results above predate these review changes; no GPU training smoke test was run for this revision. No full-model training, distributed checkpoint restore or learning run has been done on this branch. Earlier Beam results used the v0.1.2 fork.

Spindle integration: modal-labs/spindle#252.

):
# Even a Q-only canonical selection wraps the whole fused linear. Bridge's
# split wrapper emits Q/K/V rows, omitting Beam's attention gate rows.
raise ValueError(

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think for now this is ok but (very) soon we want to get off of megatron-bridge completely and do evrything within the miles native lora setup

would it be possible to add native beam model provider on top of the current bridge provider? my agent says that biggest change is going to be adding multi lora support for shared outer expert lora, which it seems miles doesn't have rn

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's going to be a lot of work to merge this in but modal-labs/spindle#312 is my ultimate goal

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer to keep the scope small to start with.


from miles.backends.megatron_utils.model_provider import _apply_bridge_runtime_config

_apply_bridge_runtime_config(provider, args)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we drop this and the other three shared LoRA changes in this file (expert TP assert, expert_bias freeze for single LoRA, data_parallel_random_init)? None of them are on upstream main, and they change behaviour for every bridge-mode LoRA model, not just Beam. For example, calculate_per_token_loss now applies, which changes gradient scaling. Beam on Spindle doesn't need them: multi-LoRA already freezes expert_bias and requires expert TP 1, and the Beam provider already sets the router and softmax settings. Dropping them keeps the fork close to radixark#4149. It also fixes the bridge config fixture tests these changes break.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if any(".experts." in target for target in adapter_targets):
assert (
provider.expert_tensor_parallel_size or 1
) == 1, "LoRA on MoE experts requires --expert-tensor-parallel-size 1"

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above: please drop. Multi-LoRA already enforces expert TP 1. For single LoRA, this makes TP>1 runs that never set expert TP fail at build.

for chunk in transformed:
for module in chunk.modules():
if isinstance(module, TopKRouter):
module.frozen_expert_bias = True

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above: please drop. Upstream radixark#4709 freezes expert_bias a different way (freeze_router_expert_bias) and deletes this loop, so we should follow that once it lands rather than diverge here.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there reason to believe that PR is more likely to land or land sooner than radixark#4149 ?

wrap_with_ddp=True,
ddp_config=ddp_config,
data_parallel_random_init=args.backend.data_parallel_random_init,
)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above: please drop. Spindle never sets data_parallel_random_init, and it defaults to False.

assert self.num_query_groups % self.tensor_model_parallel_size == 0, "Beam TP must divide num_key_value_heads"
# Beam is validated with unfused RoPE and SwiGLU. The LoRA path copies the CLI fusion flags,
# which would change the base model's numerics.
self.apply_rope_fusion = False

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we set self.moe_router_dtype = "fp32" and self.moe_router_use_torch_mm = True here, next to the fusion overrides? The LoRA path copies the CLI router flags over the provider_bridge defaults, so a launcher that omits them gets a TE router GEMM error or BF16 routing that doesn't match the sampler. scripts/models/beam.py passes only --moe-router-dtype. Forcing both here removes the launcher requirement from the PR body and matches how radixark#4149 already handles fusion.

raise ValueError(
"Beam fused QKVG attention does not support canonical_lora: the Bridge wrapper emits only Q/K/V rows. "
"Use --lora-type lora with all fused attention targets, or select independent projections such as o_proj."
)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Non-blocking: this check and its tests are fork-only, and Spindle always uses --lora-type lora, so it never fires for us. Consider dropping it here and filing an issue on radixark/miles about radixark#4149 silently mis-resolving canonical_lora on the fused QKVG layer.

@frrad

frrad commented Nov 17, 2026

Copy link
Copy Markdown
Author

I'm going to do a new Beam validation run before merging anything.

frrad added a commit to modal-labs/spindle that referenced this pull request Nov 17, 2026
Miles moves to d1915e68, the tip of modal-projects/miles#37 after review.
It drops the shared Bridge LoRA changes from the upstream port and keeps
Beam's FP32 router settings inside its own provider.

SGLang moves to 568ab742 on spindle-sglang-v0.5.23, the merge of
modal-projects/sglang#95, instead of the personal branch.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@frrad frrad assigned frrad and unassigned micahtyong Nov 17, 2026
@frrad
frrad merged commit 3280b7a into spindle-miles-v0.2.0 Nov 17, 2026
frrad added a commit to modal-labs/spindle that referenced this pull request Nov 17, 2026
modal-projects/miles#37 was squash-merged into spindle-miles-v0.2.0 as
3280b7a. Its tree is identical to d1915e68, the commit the validation
run trained on, so the Beam image moves to the merged commit under the
name miles-v0.2.0-3280b7a.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants