[Seq Packing] KK load balancing: balance packs across DP shards (P5)#1665
Open
yuxuandexter wants to merge 1 commit into
Open
[Seq Packing] KK load balancing: balance packs across DP shards (P5)#1665yuxuandexter wants to merge 1 commit into
yuxuandexter wants to merge 1 commit into
Conversation
pack_sequences replaces greedy first-fit with Karmarkar-Karp balanced packing (new default packing_strategy='kk'; 'first_fit' kept as fallback). KK splits the sequences into k groups (k a multiple of num_packs) with near-equal token sums, so the [num_packs, T] rows -> DP shards carry balanced work (fewer dummy microbatches at the num_packs-multiple boundary + no straggler shard). - utils.py: karmarkar_karp (largest-differencing, pure python + heapq), group_by_version (a pack never mixes policy_versions), balanced_pack (token-count workload + capacity guard k+=num_packs until every pack <= T). Balance by workload, capacity-check by tokens (kept separate for a future attention-aware workload; see TODO). - pack_sequences: packing_strategy arg; first_fit block unchanged (fallback). Tests (tests/rl/rl_utils_test.py): KK known answers (spread 2 / all-9), no-drop/dup, k>n empty groups, KK preserves all sequences, capacity guard, invalid strategy raises. Existing first_fit layout test pinned to first_fit.
yuxuandexter
requested review from
abheesht17,
hgao327,
jiangyangmu,
lc5211,
s-noghabi,
sizhit2,
tianshub and
wang2yn84
as code owners
July 13, 2026 18:34
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.
What
Load balancing for sequence packing (P5), stacked on the loss-normalization PR (#1662). Replaces the greedy first-fit packer with Karmarkar-Karp balanced packing so the
[num_packs, T]rows — one per DP shard — carry near-equal work.Why stacked on P3 (not parallel)
KK changes which sequences land in which pack / how many sequences per microbatch, which changes each microbatch's sequence count and denominator. P3's Layer 2 (global gradient weighting,
Σgrads / Σdenom) is exactly what keeps the loss correct when microbatch denominators vary. So KK's regrouping is only end-to-end correct on top of P3 — hence the sequential stack.Change
utils.py:karmarkar_karp(vals, k)— largest-differencing heuristic (pure python +heapq): each sequence starts as its own arrangement, repeatedly merge the two most imbalanced arrangements (pairing each one's largest bucket with the other's smallest) until one k-bucket arrangement remains.group_by_version(valid)— a pack never mixespolicy_versions (single group under synchronous training).balanced_pack(...)— balances by token count; picksk= smallest multiple ofnum_packswhose average pack fits the budget, then bumpsk += num_packsuntil every pack fitsT(a static-shape capacity guard KK itself does not enforce). Balances by workload, capacity-checks by tokens — kept separate for a future attention-aware (L^2) workload; see TODO.pack_sequences: newpacking_strategyarg —"kk"(default) or"first_fit". The first-fit block is unchanged, kept as a fallback.What KK buys (and what it doesn't)
num_packsmultiple), so the gain is real but lumpy; balancing by token count also leaves attention O(L^2) imbalanced (TODO).Tests (
tests/rl/rl_utils_test.py)karmarkar_karpknown answers:[8,7,6,5,4]k=2 → spread 2 (14/16);[8..1]k=4 → all 9. No drop/dup;k > nleaves empty groups.pack_sequencesKK: preserves all sequences (no drop/dup); capacity guard keeps every pack<= T; invalid strategy raises. The existing first-fit layout test is pinned topacking_strategy="first_fit".Notes
Stacked on
yuxzhang/seqpack_loss_norm(#1662). Re-target tomainonce the parent PRs merge.