modelopt.torch.distill.bypass#

bypass(student, teacher, train_dataloader, config=None, *, validation_dataloader=None, device_mesh=None)#

Optimize IQ native scales on streamed frozen teacher boundary inputs.

Parameters:
  • student (Module) – Existing ModelOpt IQ PTQ model; frozen formats may coexist.

  • teacher (Module) – Separate original-weight callable with matching boundary paths.

  • train_dataloader – Per-rank batches that exactly fill each update’s sample budget: config.global_batch_size without a mesh, or global_batch_size // dp_size with a mesh, where dp_size counts actual data lanes (including FSDP, excluding TP and CP). The global batch must divide exactly over those lanes. Data lanes supply unique samples; TP peers supply identical samples and masks. CP peers replay identical full sequences and masks; valid-token ownership is partitioned across CP ranks, so CP does not create additional sample lanes. Ranks must execute the same number of microbatches per update. Dict batches accept boolean/integer valid_mask or loss_mask, in that order, with nonzero meaning valid rather than a token weight. Otherwise, attention_mask must be 2D boolean/integer. Floating or 4D attention masks require an explicit validity mask. Weighted masks are unsupported; other metadata does not define the loss mask. The HF adapter additionally derives default validity from _packed_seq_ids: zero denotes padding and positive IDs identify documents. Explicit loss masks win.

  • config – A BypassConfig, a dict, or None for the established defaults.

  • validation_dataloader – Optional held-out batches for finalized-student valid-token MSE. Batches obey the same sequence length, nonempty batch and validity-mask rules as training, with identical TP/CP samples and unique samples across data lanes. Ranks must have matching batch-presence and microbatch-count schedules; validation need not fill the training global-batch budget. Diagnostic failures propagate after committing training updates to the student. Zero-step preflight skips validation as well as training.

  • device_mesh – Caller-owned placement mesh, adopted by supported integrations. It determines the per-rank data-lane budget and TP/CP replication contract described above; use a data-lane sampler, rather than sharding across TP/CP. TP DTensor boundary targets must be Replicate or sharded along the final feature dimension; token/batch shards and partial sums refuse before replay. Plain ordinary/composite TP outputs promise full replicated targets; custom local-shard/partial composites must preserve DTensor placement. This producer contract cannot be inferred from arbitrary Python output values. Exact native HF output declarations are honored. Generic directly distributed owners must keep DTensor outputs (use_local_output=False), including local-full owners whose erased output declaration is unavailable. Only the selected boundary module itself is checked; distributed descendants remain subject to the composite producer contract above.

Return type:

Module

Finalization commits boundaries in order. Any failure during finalization may leave earlier boundaries committed; reload a saved checkpoint before retrying.

For HF PTQ, use TransformersBypassAdapter.weight_patterns with the same selectors to obtain ordinary ModelOpt weight_quantizer globs before quantizing the student. These patterns exclude frozen routing gates.

Returns:

The supplied student, finalized for ordinary ModelOpt save/export. Temporary fields, hooks, modes and gradient flags are restored on every exit.

Parameters:
  • student (Module)

  • teacher (Module)

Return type:

Module