perf(bagel): accelerate agent SFT replay with FlexAttention - #383
Open
zzhuoxin1508 wants to merge 4 commits into
Open
perf(bagel): accelerate agent SFT replay with FlexAttention#383zzhuoxin1508 wants to merge 4 commits into
zzhuoxin1508 wants to merge 4 commits into
Conversation
Collaborator
Author
zzhuoxin1508
force-pushed
the
feat/bagel-flex-attention
branch
from
August 25, 2026 06:11
b7db6c5 to
d4c506f
Compare
Use BlockMask-based FlexAttention only in the agent SFT recipe while retaining SDPA as the generic Bagel replay default.
zzhuoxin1508
force-pushed
the
feat/bagel-flex-attention
branch
from
August 25, 2026 07:36
e91d93f to
10bff83
Compare
zzhuoxin1508
marked this pull request as ready for review
August 25, 2026 08:20
zzhuoxin1508
requested review from
Ideny42,
celve,
haonan3,
heguangxin and
xshrz
as code owners
August 25, 2026 08:20
2 tasks
Import FlexAttention only when selected so SDPA replay remains compatible with PyTorch builds that do not expose the optional API.
zzhuoxin1508
force-pushed
the
feat/bagel-flex-attention
branch
from
August 27, 2026 07:55
238e2ab to
4c180d4
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

Summary
replay_attention_backendswitch for Bagel train replay:sdpapreserves the existing dense per-sample mask path, whileflexbuilds a semantically equivalentBlockMaskfor FlexAttention.BagelPipelineandBagelARStagewith validation; only the train-replay path consumes it.flexis selected, preserving a clear missing-version error without affecting the SDPA path.bagel_agent_sftrecipe while retaining SDPA as the generic default; no other recipe or inference-replay path changes behavior.micro_batch_size=1); it does not add multi-sample packing.Benchmark results
Interleaved image-text agent SFT
50-step performance A/B
Clean 50-step A/B on 8x NVIDIA H20, global batch 8, micro-batch 1, same deterministic 72-example SearchGen manifest, with profiler/W&B/eval/checkpointing disabled:
Conclusion: long SearchGen trajectories contain multiple interleaved full-image and causal-text regions, leaving enough fully invalid 128x128 tiles for Flex to skip substantial work.
1000-step quality parity
A 1000-step quality run compared this PR at
b7db6c5(Flex replay) with the merge-base main commit25a2fa5(SDPA replay), using the same cooked SearchGen manifests and the same training settings documented below.train/sft_pplcurves overlap throughout the 1000 steps.Conclusion: the long-run loss curves and final validation loss show no material SFT quality regression from using FlexAttention replay.
1000-step SFT test setup
Files and parameters
b7db6c5cba373f0b872a71136a3819f32d3f9a1c; the current SFT path is behavior-equivalent, with only scope cleanup and a rebase onto latest main afterward.python -m unirl.train_sft(unirl/train_sft.py), usingexamples/sft/bagel_agent_sft.yamlwith the long-run overrides below. SearchGen manifests were prepared bydatasets/searchgen/prepare_sft.py.max_traces=5000,candidates_per_query=4,max_image_px=448,max_target_chars=8000,val_fraction=0.02, and seed42. The cooked manifests contain 8,247 train records and 132 validation records.8, micro-batch size1, max prompt length24576, max response length2048, token-mean SFT loss, and gradient clipping at1.0.replay_mode=trainandreplay_attention_backend=flex.64, alpha128, dropout0; targets are the understanding-expertq/k/v/oprojections and MLPgate/up/downprojections.1e-4, betas(0.9, 0.999), epsilon1e-8, weight decay1e-4, constant schedule, and no warmup.torch.compile; evaluation every 100 steps (eval_batch_size=8,eval_num_samples=64); adapter checkpoint saved at step 1,000.unirl-sft, run namepr383-bagel-flex-sft1000-searchgen5k-8xh20-20260824.Test Plan
flex.