LLM Foundry developer on retainer: train_loader.dataset.max_seq_len=2048 silently truncates all training sequences while model.max_seq_len=4096 — fine-tuned model falls below baseline on documents >2048 tokens — $75,000 — 3 months

LLM Foundry 0.4.0; Composer 0.17.1; MosaicML Streaming 0.6.1; Python 3.11; CUDA 12.1; 8× A100 80GB; fine-tuning MPT-7B for 4096-token financial document summarization; model.max_seq_len: 4096 in model YAML; train_loader.dataset.max_seq_len: 2048 (copied from short-context template, never updated); StreamingDataset silently truncates all training sequences to 2048 tokens; LLM Foundry performs no cross-section validation; fine-tuned model: ROUGE-L 0.42 on <2048-token docs (beats baseline 0.34) but ROUGE-L 0.19 on 2048–4096-token docs (below baseline 0.33); fix: align all max_seq_len fields • Brightfield LLM Analytics Ltd. (6-person; 3 months; 1.5h)

Brightfield LLM Analytics Ltd. is a 6-person data analytics startup that fine-tuned MPT-7B-instruct on 50,000 financial reports from SEC EDGAR to build a document summarization product. Their use case required summarizing full 15-page annual reports in a single pass — average document length of 3,800 tokens, with the longest reaching 4,200 tokens. They chose LLM Foundry 0.4.0 for its native MPT architecture support and MosaicML Streaming for high-throughput data loading. The target context length was 4,096 tokens, set via model.max_seq_len: 4096 in the model YAML config. Training used 8× A100 80GB GPUs over three epochs — 72 GPU-hours total at $2.50/hour on a cloud cluster.

The training YAML config was based on a template from a previous shorter-context fine-tuning experiment. In that earlier run, the team had used train_loader.dataset.max_seq_len: 2048 and eval_loader.dataset.max_seq_len: 2048 for a 2,048-token task. When extending to the 4,096-token financial summarization task, the team updated model.max_seq_len: 4096 in the model section and updated the tokenizer context window in the tokenizer config. The train_loader and eval_loader max_seq_len values remained at 2,048. LLM Foundry 0.4.0 performs no cross-section validation between model.max_seq_len and train_loader.dataset.max_seq_len — they are independent fields parsed separately by the YAML config loader.

Training completed normally over three epochs. The loss curve showed a healthy descent from 1.42 (epoch 1) to 1.21 (epoch 2) to 1.14 (epoch 3). No warnings or errors were raised. The model appeared to train correctly. On short financial documents (under 2,048 tokens), the fine-tuned model achieved ROUGE-L 0.42 against a held-out test set, comfortably above the MPT-7B base model’s ROUGE-L 0.34 — a solid 24% relative improvement. On long financial documents (2,048 to 4,096 tokens), the fine-tuned model achieved ROUGE-L 0.19 — 42% below the unmodified MPT-7B base model’s ROUGE-L 0.33. The model that was supposed to outperform the base on long documents was significantly worse.

# training_config.yaml — Brightfield LLM Analytics Ltd.
# LLM Foundry 0.4.0 | Composer 0.17.1 | MosaicML Streaming 0.6.1

max_seq_len: 4096        # top-level variable — used in model section only

model:
  name: hf_causal_lm
  pretrained_model_name_or_path: mosaicml/mpt-7b-instruct
  max_seq_len: 4096      # ✓ correctly set for 4096-token context

tokenizer:
  name: EleutherAI/gpt-neox-20b
  kwargs:
    model_max_length: 4096   # ✓ tokenizer context updated

train_loader:
  name: finetuning
  dataset:
    hf_name: brightfield/financial-reports-mds
    split: train
    max_seq_len: 2048    # ✗ BUG: not updated from 2048-token template
    # StreamingDataset truncates every training sample to 2048 tokens here
    # model.max_seq_len=4096 is ignored by the dataloader
    # LLM Foundry performs no cross-validation between these two values

eval_loader:
  name: finetuning
  dataset:
    hf_name: brightfield/financial-reports-mds
    split: eval
    max_seq_len: 2048    # ✗ BUG: also not updated

optimizer:
  name: decoupled_lionw
  lr: 5.0e-5
  betas: [0.9, 0.95]
  weight_decay: 0.0

scheduler:
  name: cosine_with_warmup
  t_warmup: 100ba
  alpha_f: 0.1

trainer:
  max_duration: 3ep
  device_train_microbatch_size: 4
  precision: amp_bf16
  fsdp_config:
    sharding_strategy: FULL_SHARD
    mixed_precision: PURE

# Effect: every financial report is truncated to its first 2048 tokens
# The second half of each document (tokens 2049-3800) is never seen during training
# MPT-7B positions 2048-4095 receive zero gradient updates across all 3 epochs
# Training loss correctly decreases — the model is learning the domain on 2048-token windows
#
# At inference on 3800-token documents:
# Tokens 0-2047: fine-tuned domain weights (financial vocabulary, summarization patterns)
# Tokens 2048-3799: base MPT-7B weights for these positions (pretraining distribution)
# Layer interactions between fine-tuned low layers and base-model high-position representations
# → ROUGE-L 0.19 on long docs (vs MPT-7B base 0.33)

During the retainer engagement, the first diagnostic step was printing the actual batch shape at the first training step using a custom Composer callback. The batch tensor shape was torch.Size([16, 2048]) — every sample had been padded or truncated to exactly 2,048 tokens, while the model was configured for 4,096. Inspecting the YAML config revealed the mismatch immediately. A confirmation diagnostic: sampling 100 training documents and measuring their token lengths showed 73 out of 100 were longer than 2,048 tokens (average 3,800 tokens) — meaning 73% of training documents were being cut in half silently. The second half of every long financial report — often containing key financial figures, risk factors, and management commentary — was never processed by the model during training.

Fix: align all max_seq_len fields across the YAML config

# Fixed training_config.yaml — Brightfield LLM Analytics Ltd.

max_seq_len: &max_seq_len 4096   # YAML anchor: define once, reference everywhere

model:
  name: hf_causal_lm
  pretrained_model_name_or_path: mosaicml/mpt-7b-instruct
  max_seq_len: *max_seq_len      # 4096 — via YAML anchor

tokenizer:
  name: EleutherAI/gpt-neox-20b
  kwargs:
    model_max_length: *max_seq_len  # 4096

train_loader:
  name: finetuning
  dataset:
    hf_name: brightfield/financial-reports-mds
    split: train
    max_seq_len: *max_seq_len    # ✓ 4096 — now matches model

eval_loader:
  name: finetuning
  dataset:
    hf_name: brightfield/financial-reports-mds
    split: eval
    max_seq_len: *max_seq_len    # ✓ 4096 — now matches model

# Pre-training assertion (add to train.py before Trainer initialization):
def assert_config_consistency(cfg):
    model_msl = cfg.model.max_seq_len
    train_msl = cfg.train_loader.dataset.max_seq_len
    eval_msl = cfg.eval_loader.dataset.max_seq_len
    assert model_msl == train_msl, (
        f"max_seq_len mismatch: model={model_msl}, "
        f"train_loader.dataset={train_msl}. "
        "Training will silently truncate sequences to the smaller value."
    )
    assert model_msl == eval_msl, (
        f"max_seq_len mismatch: model={model_msl}, "
        f"eval_loader.dataset={eval_msl}."
    )

# Batch shape verification callback:
from composer import Callback, Event, State, Logger

class BatchShapeVerifier(Callback):
    def __init__(self, expected_seq_len: int):
        self.expected = expected_seq_len
        self.checked = False

    def before_forward(self, state: State, logger: Logger):
        if not self.checked:
            seq_len = state.batch['input_ids'].shape[1]
            assert seq_len == self.expected, (
                f"Batch sequence length {seq_len} != expected {self.expected}. "
                "Check dataset max_seq_len in YAML."
            )
            self.checked = True

# Results after fix (2-epoch retraining, same 72 GPU-hours):
#   Train batch shape: torch.Size([16, 4096])  ✓
#   ROUGE-L on <2048-token docs:  0.44  (was 0.42, slight improvement)
#   ROUGE-L on 2048-4096-token docs: 0.44  (was 0.19 — now above baseline 0.33)
#
#   6 engineers × 3 months × $4,167/month compute + team time = $75,000

Related retainer posts


Track retainer hours without a client portal

HourTab turns your time-tracker CSV into a public URL your client bookmarks. No login. No portal. CSV in, URL out.

See pricing →