Skip to content

[feature]: packed fine-tuning in laya.train, ≈2.7× faster epochs, checkpoints stay standard (follow-up to #49) #1070

Open

Description

@sathwikbs

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

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

Activity

  1. ashishgit4 commented on Oct 10, 2026

    @ashishgit4

    /assign

  2. sathwikbs commented on Oct 10, 2026

    @sathwikbs
    Author

    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?

  3. sathwikbs commented on Oct 17, 2026

    @sathwikbs
    Author

    Friendly ping @NandhaKishorM, no rush. I'm happy to open a draft PR to make this concrete: TrainConfig(layout="packed") in laya.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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions