npx skills add ...
npx skills add nvidia/skills --skill nemo-mbridge-perf-sequence-packing
Validate and use packed sequences and long-context training in Megatron-Bridge, including offline LLM packing, collate-time VLM packing, Energon online packing, and CP constraints.
npx skills add nvidia/skills --skill nemo-mbridge-perf-sequence-packing
For stable background and recommendation level, see:
Offline packed SFT for LLM finetuning:
For text-only LLM SFT and PEFT verification, start with an 8192-token offline pack when the model context limit, memory, and model-family support allow it. Benchmark pack lengths at equal token slots per optimizer step:
For example, 2K/GBS32, 4K/GBS16, and 8K/GBS8 each expose 65,536 token slots per step. Longer packs aggregate more source examples into each physical MBS1 row and can reduce gradient accumulation and per-step overhead. They also increase activation memory and may expose kernel-width constraints, so select the largest measured configuration that fits rather than assuming longer is always faster.
Offline packing requires MBS1. Require global_batch_size % data_parallel_size == 0 and global_batch_size >= data_parallel_size; an 8K/GBS8 workload
therefore needs DP no larger than 8. Keep model.seq_length,
dataset.seq_length, and packed_sequence_size equal, use a fresh packed-data
output root after changing any of them, and inspect the resolved post-setup
configuration.
Equal token slots do not make different pack lengths numerically identical: the longer target changes truncation and pack membership. Rerun finite-loss, no-skip/NaN, and convergence sentinels before replacing verified evidence.
For finetuning with CP enabled:
Use the same alignment formula for SFT and PEFT. It produces 1 for TP1/CP1 with SP disabled and 4 for TP4/CP1 with SP enabled. Offline packing does not derive the value automatically, so pin it explicitly and rebuild packed data after a topology change.
If a dispatcher or kernel requires a fixed final token width:
Choose packed_sequence_size to satisfy the kernel multiple. For example,
HybridEP with a 128-token combine chunk requires a width divisible by 128.
This is separate from pad_seq_to_mult, which aligns each constituent
sequence for CP/SP.
If CUDA graphs are enabled for this packed path, fixed token width is required and packed metadata must also have a static shape:
Note: pad_cu_seqlens = True also requires a metadata JSON file alongside
the packed dataset (asserted in src/megatron/bridge/data/datasets/sft.py).
Custom packed datasets that omit the metadata file will hit an assertion at
dataset initialization.
In-batch packing for GPT SFT and supported VLM finetuning:
For local or materialized GPT-SFT JSONL, this keeps the existing mmap-backed
dataset and performs tokenization lazily. Both prompt/completion
(GPTSFTDataset) and chat (GPTSFTChatDataset) preserve their loss-mask
semantics. Use dataloader_type="single" or "cyclic" so every DataLoader
yield is one logical microbatch; GPT-SFT in-batch packing does not support the
global-batch "batch" dataloader.
Energon online packing for Qwen-VL uses Energon's per-worker candidate buffer instead of limiting selection to one collator micro batch:
packing_buffer_size is the sole native-packing selector; leave the legacy
collator- and step-owned packing flags at their defaults. Use vlm_step. The
buffer size counts prepared candidate samples per worker, not bytes or packed
tokens. Since prepared image/video patch tensors remain in
host memory until selection, start at 8-16 for high-resolution or video data
and measure worker RSS, first-batch latency, and bin fill before increasing it.
This path does not write offline packs; the source WebDataset shards remain
unchanged. It supports eager Qwen-VL with MBS1 and rejects MTP, CUDA graphs,
Qwen3-VL DistTrain, and PP. Requested MoE expert-parallel communication overlap
is disabled with a warning. Standard eager alltoall EP has functional coverage
for Qwen3.6-35B-A3B at TP1/PP1/EP8 with overlap disabled; this is not performance
evidence. Other EP dispatchers are accepted with fixed-width native packs but do
not yet have equivalent runtime evidence. The Qwen-VL model derives a MoE
padding mask from logical and physical THD boundaries so fixed-width gaps do not
enter auxiliary-loss, z-loss, or expert-bias statistics. Current MCore may still
dispatch padded positions; expert-capacity/token-dropping configurations lack
native-packing runtime coverage.
Long-context baseline:
LLM packed SFT config surface:
The shared text-dataset helper currently opts into fixed-width packs. Treat that as a helper default, not a universal offline-packing runtime requirement; preserve it when the selected dispatcher, kernel, or CUDA-graph path requires static width.
Bridge validation:
Collate-time in-batch runtime used by VLM providers:
GPT-SFT direct-row packing:
Packed THD runtime constraint:
dataloader_type="single" or "cyclic"; it does not support "batch".2 * context_parallel_size divisibility.calculate_per_token_loss=True and ddp.average_in_collective=False are required.pad_cu_seqlens=True also requires pad_to_max_length=True.Qwen3-Next, GLM-4.5, and Qwen3.5-VL contain explicit opt-outs in different paths.samples_mapping, must retain an all-zero loss mask.global_batch_size must be divisible by and no smaller than data parallel size when offline packing uses MBS1.pad_seq_to_mult from CP/TP/SP for both SFT and PEFT; do not hardcode different values by workload type.pad_to_max_length controls final pack width and is conditional on fixed-shape execution requirements.packing_buffer_size is per worker and also affects validation; global/eval batch counts refer to physical packs rather than source conversations.Use the checked-in unit coverage:
Success criteria:
"batch" dataloadertoken_slots_per_step = packed_sequence_size * global_batch_sizecfg.model.context_parallel_size = 2
cfg.model.calculate_per_token_loss = True
cfg.ddp.average_in_collective = Falsecfg.dataset.dataset_kwargs = {
**(cfg.dataset.dataset_kwargs or {}),
"pad_to_max_length": True,
}cfg.dataset.offline_packing_specs.pad_cu_seqlens = True
cfg.dataset.dataset_kwargs["pad_to_max_length"] = Truecfg.dataset.enable_in_batch_packing = True
cfg.dataset.dataloader_type = "single"
cfg.train.micro_batch_size = 4cfg.dataset.packing_buffer_size = 16
cfg.dataset.micro_batch_size = 1
cfg.train.micro_batch_size = 1
cfg.model.calculate_per_token_loss = True
cfg.ddp.average_in_collective = Falsecfg.model.seq_length = 16384
cfg.dataset.seq_length = 16384
cfg.model.context_parallel_size = 2