Repository navigation
Description
The problem
laya.train builds one sequence per (question, state), so a row with n questions runs the encoder over its state n times. Tokenizing the state once (#109, #343) removed repeated tokenization, but the encoder work still scales with n, as noted in #49. On typed-decisions (≈4.7 questions per row) an epoch is 1,631,547 tokens.
Proposed solution
A training-only packed layout behind a flag (sketch: TrainConfig(layout="packed")):
- One sequence per row:
[CLS] q1 … [SEP] … [CLS] qn … [SEP] state [SEP]. - Question tokens attend to their own segment and the state; state tokens attend to everything.
- Position ids place each question directly before the state, so every allowed query/key pair keeps its standard relative offset. The local-attention window is built from position ids, not sequence index (the ModernBERT trap described in Proposal: encode the state once and reuse it across questions #49).
- Each question's tokens get its type embedding; the state gets the mean of the row's question types. The decision head runs on the packed sequence with the same mask.
- Loss, optimizer, schedules and calibration are unchanged.
Unlike the shared-state reader in #49, nothing changes at inference: the trained checkpoint has the standard architecture and is used with laya.load().predict() as is.
Measured with a reference implementation against the training script at 4aa6761 (before laya.train):
- One-question packed row vs the standard forward: max |Δlogit| 0.00e+00.
- NVIDIA T4, one epoch: 26.6 → 9.9 min (2.69×); tokens 1,631,547 → 634,226.
- typed-decisions test (2,000 decisions), scored one question at a time with
bench_local.score_cases: 4 epochs packed 0.7595 vs the published laya-typed-decisions 0.766; 1 epoch vanilla 0.677 vs packed 0.681 / 0.658 (two runs, ≈±2 points run-to-run). predict()latency of a packed-trained checkpoint: 313 vs 312 ms median per 5-question request (Apple M4 Pro, MPS).
Alternatives considered
- State-blind packing (state attends only to itself, as in Proposal: encode the state once and reuse it across questions #49 / ikken): reusable state at inference, but it needs a new inference path and new checkpoints.
- Tokenize once (perf(agent): reuse the tokenized state across questions; autocast on MPS #109, perf+fix(onnx): tune ORT session, tokenize state once, report answer_confidence #343): merged; it removes tokenization only.
Use case
Fine-tuning on your own workflow where each input carries several typed questions (the typed-decisions workflows ask 5). Gain = n(S+H)/(S+nH); with one question per row the packed sequence is the standard one, so there is no gain.
Prior art
#49 and johnmofficial16-prog's ikken library linked there. Reference implementation: https://github.com/sathwikbs/laya-fast-finetune/blob/main/notebooks/laya_packed.py
Would you accept this in laya.train behind a flag? If so, I'll open a PR with an assert-based check and before/after numbers against the current recipe (micro_batch=8 × grad_accum=8, option shuffling, option_layout).
Before submitting
- I searched the existing issues. Proposal: encode the state once and reuse it across questions #49 is related (closed because changing the encoder's attention layout would need new checkpoints); this proposal changes training only and keeps inference unchanged.
Activity
ashishgit4 commentedon Oct 10, 2026
/assign
sathwikbs commentedon Oct 10, 2026
Thanks for the interest! I wrote this proposal and the reference implementation, and I plan to open the PR myself once the maintainer confirms the direction (packing as a training-only flag in
laya.train). @NandhaKishorM, would you accept it in that form?sathwikbs commentedon Oct 17, 2026
Friendly ping @NandhaKishorM, no rush. I'm happy to open a draft PR to make this concrete:
TrainConfig(layout="packed")inlaya.train, an assert-based parity check (a one-question packed row equals the standard forward), and before/after numbers on typed-decisions against the current recipe. If you'd rather not take training-layout changes (as in #49), just say so and I'll keep it in my fork.