[4/19] Add streamed Bypass runner and expose its configuration

Signed-off-by: Sepehr Sameni <ssameni@nvidia.com>
This commit is contained in:
Sepehr Sameni
2026-12-02 02:58:43 -08:00
parent 8f6109cc0b
commit a9869db09f
10 changed files with 1301 additions and 2 deletions
+1
View File
@@ -50,6 +50,7 @@ Changelog
- Prefix agent skill names with ``modelopt-``; existing names remain available as forwarding stubs.
- The ``hf`` extra now supports transformers up to 5.23 (previously below 5.19), which current Megatron-Bridge requires.
- Expert-parallel loading and export now support Muse Spark (transformers 5.20+), whose experts normalize their outputs with a post-expert norm; models from remote code with such norms load, but their export refuses the norms.
- Add ``modelopt.torch.distill.bypass`` for streaming IQ scale-only repair with Lion, a direct 0.001 peak learning rate, and fractional warmup.
**Backward Breaking Changes**
+3
View File
@@ -25,5 +25,8 @@ from .losses import *
from .registry import *
# isort: off
# Bypass imports quantizers, whose Trainer plugin imports the completed KD API.
from .bypass import *
# Import plugins last to avoid circular imports
from . import plugins
@@ -0,0 +1,84 @@
# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: Apache-2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Lion on bounded FP32 native-field chunks and a direct peak learning rate."""
import math
import torch
from torch.optim import Optimizer
_LION_CHUNK_ELEMENTS = 1 << 18
class Lion(Optimizer):
"""Sign-momentum Lion with FP32 state, betas (0.9, 0.99), and zero decay."""
def __init__(self, params, lr=0.001):
"""Configure native-field groups without a study-specific LR multiplier."""
if not math.isfinite(lr) or lr < 0:
raise ValueError("Lion learning rate must be finite and nonnegative")
super().__init__(params, {"lr": lr, "betas": (0.9, 0.99), "weight_decay": 0.0})
for group in self.param_groups:
for parameter in group["params"]:
if parameter.dtype != torch.float32 or not parameter.is_contiguous():
raise ValueError("Bypass Lion requires contiguous FP32 native-field masters")
@torch.no_grad()
def step(self, closure=None):
"""Apply Lion's direction and moment coefficients without modifying gradients."""
loss = None
if closure is not None:
with torch.enable_grad():
loss = closure()
for group in self.param_groups:
for p in group["params"]:
if p.grad is not None and (p.grad.is_sparse or not p.grad.is_contiguous()):
raise ValueError("Bypass Lion requires contiguous dense gradients")
for group in self.param_groups:
beta1, beta2 = group["betas"]
for parameter in group["params"]:
if parameter.grad is None:
continue
state = self.state[parameter]
if "exp_avg" not in state:
state["exp_avg"] = torch.zeros_like(parameter)
value, gradient, momentum = (
parameter.view(-1),
parameter.grad.view(-1),
state["exp_avg"].view(-1),
)
for start in range(0, parameter.numel(), _LION_CHUNK_ELEMENTS):
chunk = slice(start, start + _LION_CHUNK_ELEMENTS)
direction = momentum[chunk].mul(beta1).add_(gradient[chunk], alpha=1 - beta1)
value[chunk].add_(direction.sign_(), alpha=-group["lr"])
momentum[chunk].mul_(beta2).add_(gradient[chunk], alpha=1 - beta2)
return loss
def learning_rate(config, step):
"""One-based update LR: ceil(fraction * budget) warmup, then the chosen decay."""
warmup = min(config.num_steps, math.ceil(config.num_steps * config.warmup_fraction))
if step <= warmup:
return config.max_lr * step / max(1, warmup)
fraction = (step - warmup) / max(1, config.num_steps - warmup)
factor = (
(1 + math.cos(math.pi * fraction)) / 2
if config.scheduler == "cosine"
else 1 - fraction
if config.scheduler == "linear"
else 1.0
)
return config.max_lr * factor
+228
View File
@@ -0,0 +1,228 @@
# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: Apache-2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Full-global-batch IQ Bypass updates over streamed independent boundaries."""
import logging
import random
from functools import partial
import numpy as np
import torch
from modelopt.torch.quantization.repair import (
IQRepairPolicy,
_validate_ownership,
_validate_quantizer,
)
from modelopt.torch.quantization.utils.core_utils import (
enable_weight_access_and_writeback,
module_name_maps,
)
from ._bypass_capture import mse_statistics
from ._bypass_optimizer import Lion, learning_rate
_LOG = logging.getLogger(__name__)
_OPTIMIZERS = {"lion": Lion}
class _BypassRunner:
def __init__(self, student, teacher, units, plan, config, adapter):
self.student, self.teacher, self.units = student, teacher, units
self.plan, self.config, self.adapter = plan, config, adapter
self.policies = {}
self.optimizers = {}
self.fields = {}
self.modules = {u.name: student.get_submodule(u.student_path) for u in units}
self.names = module_name_maps(student)
def _weight_access(self, name, *, writeback=True, rollback=False):
return enable_weight_access_and_writeback(
self.modules[name], self.student, self.names, writeback=writeback, rollback=rollback
)
def _window(self, batches):
window, samples = [], 0
while samples < self.config.global_batch_size:
try:
batch = next(batches)
except StopIteration as exc:
raise ValueError(
"Bypass data exhausted; provide unique samples for every update"
) from exc
self.adapter.validate_batch(batch, self.config)
samples += self.adapter.batch_size(batch)
if samples > self.config.global_batch_size:
raise ValueError("Dataloader batches must exactly fill the configured global batch")
saved, _ = self.adapter.snapshot_inputs((batch,), {})
window.append(saved[0])
return window
def _measure(self, batches, training):
statistics = {}
def consume(unit, record):
with torch.enable_grad() if training else torch.no_grad():
prediction = self.adapter.replay(self.modules[unit.name], record)
error, count = mse_statistics(prediction, record.target, record.valid_mask)
if training:
error.backward()
if unit.name not in statistics:
statistics[unit.name] = [error.detach(), count.detach()]
else:
statistics[unit.name][0].add_(error.detach())
statistics[unit.name][1].add_(count)
for batch in batches:
for microbatch in self.adapter.microbatches(batch, self.config.micro_batch_size):
self.adapter.teacher_microbatch(self.teacher, self.units, microbatch, consume)
if set(statistics) != set(self.modules):
raise ValueError("Every selected Bypass boundary must execute in the teacher forward")
return statistics
def _update(self, statistics, step):
# Normalize and clip every unit before any update, so invalid gradients cannot
# partially advance the model. Counts cover the entire global batch.
for unit in self.units:
count = statistics[unit.name][1]
if count.item() == 0:
raise ValueError("A Bypass update has no valid target elements")
fields = self.fields[unit.name]
for parameter in fields:
if parameter.grad is not None:
parameter.grad.div_(count.to(parameter.device))
torch.nn.utils.clip_grad_norm_(
fields, self.config.max_grad_norm, error_if_nonfinite=True
)
lr = learning_rate(self.config, step)
for unit in self.units:
optimizer = self.optimizers[unit.name]
for group in optimizer.param_groups:
group["lr"] = lr
optimizer.step()
self.policies[unit.name].project_()
if step == 1 or step % 16 == 0 or step == self.config.num_steps:
_LOG.info(
"Bypass step=%d/%d lr=%g mse=%s",
step,
self.config.num_steps,
lr,
{name: (error / count).item() for name, (error, count) in statistics.items()},
)
def run(self, train_dataloader, validation_dataloader=None):
"""Finalize the supplied student and restore all caller modes/gradient flags."""
devices = sorted(
{
p.device.index
for model in (self.student, self.teacher)
for p in (*model.parameters(), *model.buffers())
if p.is_cuda
}
)
python_state, numpy_state = random.getstate(), np.random.get_state()
try:
with torch.random.fork_rng(devices=devices):
random.seed(self.config.seed)
np.random.seed(self.config.seed % 2**32)
torch.random.default_generator.manual_seed(self.config.seed)
for device in devices:
with torch.cuda.device(device):
torch.cuda.manual_seed(self.config.seed)
return self._run(train_dataloader, validation_dataloader, devices)
finally:
random.setstate(python_state)
np.random.set_state(numpy_state)
def _run(self, train_dataloader, validation_dataloader, devices):
batches = iter(train_dataloader)
first = self._window(batches)
modes = {
module: module.training
for root in (self.student, self.teacher)
for module in root.modules()
}
teacher_flags = [
(module, name, parameter.requires_grad)
for module in self.teacher.modules()
for name, parameter in module.named_parameters(recurse=False, remove_duplicate=False)
]
error = None
try:
self.student.eval()
self.teacher.eval().requires_grad_(False)
for index, unit in enumerate(self.units):
policy = IQRepairPolicy()
self.policies[unit.name] = policy
with self._weight_access(unit.name, writeback=False):
fields = policy.prepare_plan(self.plan, index)
self.fields[unit.name] = fields
self.optimizers[unit.name] = _OPTIMIZERS[self.config.optimizer](
fields, lr=self.config.max_lr
)
for step in range(1, self.config.num_steps + 1):
window = first if step == 1 else self._window(batches)
for optimizer in self.optimizers.values():
optimizer.zero_grad(set_to_none=True)
with torch.profiler.record_function("modelopt.bypass.update"):
self._update(self._measure(window, training=True), step)
window.clear()
states = {
binding: state
for policy in self.policies.values()
for binding, state, _, _ in policy.adapters
}
for binding, state in states.items():
_validate_quantizer(binding.quantizer)
if binding.quantizer.num_bits != state.name:
raise ValueError("The native IQ format changed during repair")
_validate_ownership((self.student,), (states,))
del states
for name, policy in self.policies.items():
policy.finalize_(
weight_access=partial(self._weight_access, name),
)
if validation_dataloader is not None:
statistics = self._measure(validation_dataloader, training=False)
_LOG.info(
"Bypass final validation mse=%s",
{n: (e / c).item() if c.item() else None for n, (e, c) in statistics.items()},
)
return self.student
except BaseException as exc:
error = exc
raise
finally:
cleanup_error = None
for name, policy in self.policies.items():
try:
if policy._pending_rollback is not None:
policy._restore_publication(partial(self._weight_access, name), None)
policy.close()
except BaseException as exc:
if cleanup_error is None:
cleanup_error = exc
else:
cleanup_error.add_note(f"Another Bypass policy cleanup failed: {exc!r}")
for module, name, flag in teacher_flags:
module.get_parameter(name).requires_grad_(flag)
for module, mode in modes.items():
module.training = mode
if cleanup_error is not None:
if error is not None:
error.add_note(f"Bypass cleanup also failed: {cleanup_error!r}")
else:
raise cleanup_error
+92
View File
@@ -0,0 +1,92 @@
# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: Apache-2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""One-call Bypass repair of an existing IQ-quantized student."""
import logging
import torch
from torch import nn
from modelopt.torch.quantization.repair import plan_iq_repair
from ._bypass_capture import ModuleBypassAdapter
from ._bypass_runner import _BypassRunner
from .config import BypassConfig
__all__ = ["bypass"]
def bypass(
student: nn.Module,
teacher: nn.Module,
train_dataloader,
config=None,
*,
validation_dataloader=None,
device_mesh=None,
) -> nn.Module:
"""Optimize IQ native scales on streamed frozen teacher boundary inputs.
Args:
student: Existing ModelOpt IQ PTQ model; frozen formats may coexist.
teacher: Separate original-weight callable with matching boundary paths.
train_dataloader: Unique sample batches that exactly fill each global 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.
Batches are snapshotted before the next reader call. Named model inputs
and loss masks have a leading sample axis; other 1D dict metadata is
shared across samples. Represent auxiliary sample vectors as ``[batch, 1]``.
Global Python/NumPy/CPU RNG and CUDA RNG on resident model devices are
seeded and restored. Readers using other CUDA devices must own their
generators; reader generators and worker state advance independently.
config: A :class:`BypassConfig`, a dict, or None for the established defaults.
validation_dataloader: Optional held-out batches for finalized-student valid-token MSE.
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.
Finalization commits boundaries in order. Any failure during finalization may
leave earlier boundaries committed; reload a saved checkpoint before retrying.
Persistent owner restoration failures may also retain temporary repair state;
discard the affected model and reload it before reuse. Cleanup failures are
attached to the primary exception when one is already propagating.
Returns:
The supplied student, finalized for ordinary ModelOpt save/export. Temporary
fields and hooks are removed unless owner restoration fails; caller modes
and teacher gradient flags are restored on every exit.
"""
config = config if isinstance(config, BypassConfig) else BypassConfig(**(config or {}))
if device_mesh is not None or (
torch.distributed.is_initialized() and torch.distributed.get_world_size() > 1
):
raise ValueError("Distributed Bypass requires its native placement integration")
if getattr(getattr(student, "config", None), "model_type", None):
raise ValueError("Transformers Bypass requires its 5.23.x integration")
adapter = ModuleBypassAdapter()
units = adapter.resolve_units(student, teacher, config)
plan = plan_iq_repair(
[student.get_submodule(u.student_path) for u in units], root_model=student
)
logging.getLogger(__name__).info("Bypass selected boundaries: %s", [u.name for u in units])
if config.num_steps == 0:
return student
return _BypassRunner(student, teacher, units, plan, config, adapter).run(
train_dataloader, validation_dataloader
)
+1 -1
View File
@@ -26,7 +26,7 @@ from modelopt.torch.utils.network import ModelLike
from .loss_balancers import DistillationLossBalancer
__all__ = ["ExportStudentConfig", "KDLossConfig", "LayerwiseKDConfig"]
__all__ = ["BypassConfig", "ExportStudentConfig", "KDLossConfig", "LayerwiseKDConfig"]
Criterion: TypeAlias = Loss | dict[tuple[str, str], Loss]
+852
View File
@@ -0,0 +1,852 @@
# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: Apache-2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Offline end-to-end public Bypass flows, including real native save/restore."""
import copy
import json
import random
import statistics
import subprocess
import sys
import time
from collections import OrderedDict
from contextlib import ExitStack
import numpy as np
import pytest
import torch
from torch import nn
import modelopt.torch.distill as mtd
import modelopt.torch.opt as mto
import modelopt.torch.quantization as mtq
from modelopt.torch.quantization.ggml import IQ_FORMAT_REGISTRY
from modelopt.torch.quantization.ggml.common import pinned_packed_weight
from modelopt.torch.quantization.ggml.scale_fields import decode_scale_fields, replace_scale_fields
class _Network(nn.Sequential):
def forward(
self,
hidden_states,
attention_mask=None,
_packed_seq_ids=None,
padding_mask=None,
position_ids=None,
):
if position_ids is not None:
hidden_states = hidden_states + position_ids[..., None]
return super().forward(hidden_states)
def _problem():
torch.manual_seed(37)
student = _Network(
OrderedDict(
[
("mlp", nn.Linear(256, 256, bias=False)),
("activation", nn.SiLU()),
("moe", nn.Linear(256, 256, bias=False)),
("head", nn.Linear(256, 4, bias=False)),
]
)
)
batches = [torch.randn(2, 3, 256) for _ in range(8)]
mtq.quantize(
student,
{
"quant_cfg": [
{"quantizer_name": "*", "enable": False},
{
"quantizer_name": "mlp.weight_quantizer",
"cfg": {"num_bits": "iq1_m", "backend": "ggml"},
},
{
"quantizer_name": "moe.weight_quantizer",
"cfg": {"num_bits": "iq2_s", "backend": "ggml"},
},
],
"algorithm": "max",
},
lambda m: m(batches[0]),
)
teacher = _Network(
OrderedDict(
[
("mlp", nn.Linear(256, 256, bias=False)),
("activation", nn.SiLU()),
("moe", nn.Linear(256, 256, bias=False)),
("head", nn.Linear(256, 4, bias=False)),
]
)
)
with torch.no_grad():
for name in ("mlp", "moe"):
layer = student.get_submodule(name)
teacher.get_submodule(name).weight.copy_(layer.weight_quantizer(layer.weight) * 1.02)
teacher.head.weight.copy_(student.head.weight)
teacher.head.eval()
return student, teacher, batches
@pytest.mark.parametrize("validation_failure", [False, True])
def test_bypass_streams_independent_units_and_finalizes(tmp_path, validation_failure):
student, teacher, batches = _problem()
teacher_state = copy.deepcopy(teacher.state_dict())
initial = (student(batches[0]) - teacher(batches[0])).square().mean().item()
baseline = copy.deepcopy(student)
original_codes = {
name: IQ_FORMAT_REGISTRY[bits]
.pack(student.get_submodule(name).weight, student.get_submodule(name).weight_quantizer)
.clone()
for name, bits in [("mlp", "iq1_m"), ("moe", "iq2_s")]
}
teacher_inputs, student_inputs = [], []
handles = [
teacher.moe.register_forward_pre_hook(
lambda m, args: teacher_inputs.append(args[0].detach().clone())
),
student.moe.register_forward_pre_hook(
lambda m, args: student_inputs.append(args[0].detach().clone())
),
]
config = {
"num_steps": 8,
"global_batch_size": 2,
"micro_batch_size": 1,
"sequence_length": 3,
"scheduler": "constant",
"warmup_fraction": 0,
}
def validation_batches():
yield batches[0]
if validation_failure:
raise RuntimeError("deliberate validation failure")
try:
if validation_failure:
with pytest.raises(RuntimeError, match="deliberate validation failure"):
mtd.bypass(
student, teacher, batches, config, validation_dataloader=validation_batches()
)
result = student
else:
result = mtd.bypass(
student, teacher, batches, config, validation_dataloader=validation_batches()
)
finally:
for handle in handles:
handle.remove()
assert result is student
assert len(teacher_inputs) == len(student_inputs) == 18
for actual, expected in zip(student_inputs, teacher_inputs):
torch.testing.assert_close(actual, expected, rtol=0, atol=0)
assert student.training and teacher.training and not teacher.head.training
assert all(torch.equal(teacher_state[k], v) for k, v in teacher.state_dict().items())
torch.testing.assert_close(student.head.weight, baseline.head.weight, rtol=0, atol=0)
assert (student(batches[0]) - teacher(batches[0])).square().mean() < initial
for name, bits in [("mlp", "iq1_m"), ("moe", "iq2_s")]:
q = student.get_submodule(name).weight_quantizer
fmt = IQ_FORMAT_REGISTRY[bits]
payload = pinned_packed_weight(q, bits)
global_scale, local, _ = decode_scale_fields(original_codes[name], fmt)
assert torch.equal(
replace_scale_fields(payload, fmt, global_scale, local), original_codes[name]
)
config["micro_batch_size"] = 2
mtd.bypass(baseline, teacher, batches, mtd.BypassConfig(**config))
torch.testing.assert_close(student(batches[0]), baseline(batches[0]), rtol=0, atol=0)
assert not any("repair" in name or "teacher" in name for name, _ in student.named_modules())
mto.save(student, tmp_path / "student.pt")
plain = _Network(
OrderedDict(
[
("mlp", nn.Linear(256, 256, bias=False)),
("activation", nn.SiLU()),
("moe", nn.Linear(256, 256, bias=False)),
("head", nn.Linear(256, 4, bias=False)),
]
)
)
restored = mto.restore(plain, tmp_path / "student.pt")
torch.testing.assert_close(restored(batches[0]), student(batches[0]), rtol=0, atol=0)
def test_bypass_refuses_aliases_and_cleans_failed_replay():
student, teacher, batches = _problem()
original = copy.deepcopy(student.state_dict())
for path in ("missing", "mlp.missing"):
with pytest.raises(ValueError):
mtd.bypass(student, teacher, batches, {"num_steps": 0, "modules": [path]})
with pytest.raises(ValueError):
mtd.bypass(student, teacher, batches, {"num_steps": 0, "modules": ["", "mlp"]})
for malformed in (
{"hidden_states": batches[0]},
(batches[0],),
(torch.ones(2, 3, dtype=torch.long),),
{"inputs_embeds": batches[0]},
):
with pytest.raises(ValueError, match="sequence_length"):
mtd.bypass(
student,
teacher,
[malformed],
{"num_steps": 1, "global_batch_size": 2, "sequence_length": 4},
)
teacher.mlp.weight = nn.Parameter(student.mlp.weight.detach())
with pytest.raises(ValueError):
mtd.bypass(student, teacher, batches, {"num_steps": 0, "modules": ["mlp"]})
assert all(torch.equal(v, original[n]) for n, v in student.state_dict().items())
student, teacher, batches = _problem()
hook_counts = [(m, len(m._forward_hooks), len(m._forward_pre_hooks)) for m in teacher.modules()]
flags = [(p, p.requires_grad) for p in [*student.parameters(), *teacher.parameters()]]
def fail(module, args):
raise RuntimeError("deliberate replay failure")
handle = student.moe.register_forward_pre_hook(fail)
try:
with pytest.raises(RuntimeError, match="deliberate replay"):
mtd.bypass(
student,
teacher,
batches,
{"num_steps": 1, "global_batch_size": 2, "sequence_length": 3},
)
finally:
handle.remove()
assert all(p.requires_grad == flag for p, flag in flags)
assert all(
len(m._forward_hooks) == after and len(m._forward_pre_hooks) == before
for m, after, before in hook_counts
)
assert not any("repair" in n for n, _ in student.named_modules())
@pytest.mark.parametrize(
"reader", ["reused_storage", "shared_positions", "positional_cache", "default_positional_cache"]
)
def test_bypass_preserves_reader_samples_and_shared_positions(reader):
student, teacher, batches = _problem()
batches = [batch[:, :2].clone() for batch in batches[:2]]
observed = []
handle = teacher.mlp.register_forward_pre_hook(
lambda module, args: observed.append(args[0].clone())
)
if reader in ("positional_cache", "default_positional_cache"):
original_forward = teacher.forward
def forward(hidden_states, use_cache=True):
assert use_cache is False
return original_forward(hidden_states)
teacher.forward = forward
if reader == "default_positional_cache":
def positional_forward(hidden_states, use_cache=True, /):
return forward(hidden_states, use_cache)
teacher.forward = positional_forward
def read():
staging = torch.empty_like(batches[0])
for batch in batches:
if reader == "reused_storage":
yield staging.copy_(batch)
elif reader == "positional_cache":
yield (batch, True)
elif reader == "default_positional_cache":
yield (batch,)
else:
yield {"hidden_states": batch, "position_ids": torch.arange(2)}
try:
mtd.bypass(
student,
teacher,
read(),
{
"num_steps": 1,
"global_batch_size": 4,
"micro_batch_size": 1,
"sequence_length": 2,
"modules": ["mlp"],
},
)
finally:
handle.remove()
expected = torch.cat(batches)
if reader == "shared_positions":
expected = expected + torch.arange(2)[:, None]
torch.testing.assert_close(torch.cat(observed), expected, rtol=0, atol=0)
@pytest.mark.parametrize("heldout", [False, True])
def test_bypass_rejects_misaligned_loss_masks_before_slicing(heldout):
student, teacher, batches = _problem()
malformed = {"hidden_states": batches[0], "valid_mask": torch.ones(1, 3, dtype=torch.bool)}
with pytest.raises(ValueError):
mtd.bypass(
student,
teacher,
batches if heldout else [malformed],
{
"num_steps": 1,
"global_batch_size": 2,
"micro_batch_size": 1,
"sequence_length": 3,
"modules": ["mlp"],
},
validation_dataloader=[malformed] if heldout else None,
)
@pytest.mark.parametrize(
"mask_kind",
[
"valid_mask",
"loss_mask",
"attention_mask",
"attention_with_metadata",
"additive_attention",
"weighted_valid",
"weighted_loss",
"integer_valid",
"integer_loss",
"integer_attention",
"attention4d",
"explicit_attention4d",
],
)
def test_bypass_normalizes_valid_elements_over_the_whole_update(monkeypatch, mask_kind):
from modelopt.torch.distill._bypass_optimizer import Lion
from modelopt.torch.quantization.repair import IQRepairPolicy
student, teacher, batches = _problem()
reference = copy.deepcopy(student)
x = batches[0]
valid = torch.tensor([[True, True, True], [True, False, False]])
attention4d = torch.ones(3, 3, dtype=torch.bool).tril()[None, None] & valid[:, None, None, :]
if mask_kind in ("additive_attention", "weighted_valid", "weighted_loss", "attention4d"):
key, mask = {
"additive_attention": (
"attention_mask",
torch.zeros_like(valid, dtype=torch.float32).masked_fill_(~valid, -torch.inf),
),
"weighted_valid": ("valid_mask", valid.float() * 0.5),
"weighted_loss": ("loss_mask", valid.float() * 0.5),
"attention4d": ("attention_mask", attention4d),
}[mask_kind]
before = copy.deepcopy(student.state_dict())
with pytest.raises(ValueError):
mtd.bypass(
student,
teacher,
[{"hidden_states": x, key: mask}],
{"num_steps": 1, "global_batch_size": 2, "sequence_length": 3, "modules": ["mlp"]},
)
assert all(torch.equal(value, before[name]) for name, value in student.state_dict().items())
return
policy = IQRepairPolicy()
try:
fields = policy.prepare(reference.mlp)
# Match projection shapes so this isolates normalization from BLAS batch-shape roundoff.
prediction = torch.cat([reference.mlp(sample) for sample in x.split(1)])
target = torch.cat([teacher.mlp(sample) for sample in x.split(1)]).detach()
torch.nn.functional.mse_loss(prediction[valid], target[valid]).backward()
expected = [p.grad.detach().clone() for p in fields]
finally:
policy.close()
observed = []
original_step = Lion.step
def record_actual_gradients(optimizer, *args, **kwargs):
observed.extend(
p.grad.detach().clone() for g in optimizer.param_groups for p in g["params"]
)
return original_step(optimizer, *args, **kwargs)
monkeypatch.setattr(Lion, "step", record_actual_gradients)
key = mask_kind if mask_kind in ("valid_mask", "loss_mask") else "attention_mask"
batch = {"hidden_states": x, key: valid}
if mask_kind.startswith("integer_"):
key = {
"integer_valid": "valid_mask",
"integer_loss": "loss_mask",
"integer_attention": "attention_mask",
}[mask_kind]
batch = {"hidden_states": x, key: valid.long()}
if mask_kind == "explicit_attention4d":
batch.update(valid_mask=valid, attention_mask=attention4d)
if mask_kind == "loss_mask":
batch["attention_mask"] = torch.ones_like(valid)
if mask_kind == "attention_with_metadata":
batch.update(_packed_seq_ids=torch.tensor([[0, 0, 1], [0, 1, 1]]), padding_mask=~valid)
mtd.bypass(
student,
teacher,
[batch],
{
"modules": ["mlp"],
"num_steps": 1,
"global_batch_size": 2,
"micro_batch_size": 1,
"sequence_length": 3,
"max_grad_norm": 1e9,
"scheduler": "constant",
"warmup_fraction": 0,
},
)
assert len(observed) == len(expected)
for actual, wanted in zip(observed, expected):
torch.testing.assert_close(actual, wanted, rtol=1e-5, atol=1e-10)
@pytest.mark.parametrize(
("alias", "offload"),
[
("parameter", False),
("storage_view", False),
("module", False),
("quantizer", False),
("router_weight", False),
("router_quantizer", False),
("parameter", True),
("storage_view", True),
("router_weight", True),
("teacher_storage", False),
("teacher_storage", "teacher"),
("teacher_storage", "student"),
("teacher_storage", "both"),
("teacher_storage", "preload_teacher"),
("teacher_storage", "preload_student"),
("buffer", False),
("buffer", True),
],
)
def test_bypass_refuses_selected_state_shared_with_excluded_owners(alias, offload):
student, teacher, batches = _problem()
selected = "mlp"
if alias.startswith("router_"):
class MoE(nn.Module):
def __init__(self):
super().__init__()
self.gate = nn.Linear(256, 1, bias=False)
self.experts = nn.ModuleList([nn.Linear(256, 256, bias=False)])
def forward(self, x):
return self.experts[0](x) * self.gate(x).sigmoid()
teacher = nn.Sequential(OrderedDict([("moe", MoE())]))
student = mtq.quantize(
copy.deepcopy(teacher),
{
"quant_cfg": [
{"quantizer_name": "*", "enable": False},
{
"quantizer_name": "*weight_quantizer",
"cfg": {"num_bits": "iq2_s", "backend": "ggml"},
},
],
"algorithm": "max",
},
lambda model: model(batches[0]),
)
expert, router = student.moe.experts[0], student.moe.gate
if alias == "router_weight":
router.weight = nn.Parameter(expert.weight[:1].detach())
else:
router.weight_quantizer = expert.weight_quantizer
selected = "moe"
elif alias == "buffer":
control = torch.ones(4)
student.mlp.register_buffer("shared_control", control)
student.moe.register_buffer("shared_control", control)
elif alias == "teacher_storage":
teacher.mlp.weight = nn.Parameter(student.mlp.weight.detach())
elif alias == "parameter":
student.moe.weight = student.mlp.weight
elif alias == "storage_view":
student.moe.weight = nn.Parameter(student.mlp.weight.detach().T)
elif alias == "module":
student.moe = student.mlp
else:
student.moe.weight_quantizer = student.mlp.weight_quantizer
before = copy.deepcopy(student.state_dict())
teacher_before = copy.deepcopy(teacher.state_dict())
if offload in (True, "student", "both", "preload_student"):
pytest.importorskip("accelerate").cpu_offload(
student,
execution_device="cpu",
offload_buffers=True,
preload_module_classes=[type(student).__name__] if offload == "preload_student" else [],
)
if offload in ("teacher", "both", "preload_teacher"):
pytest.importorskip("accelerate").cpu_offload(
teacher,
execution_device="cpu",
offload_buffers=True,
preload_module_classes=[type(teacher).__name__] if offload == "preload_teacher" else [],
)
parameters = dict(student.named_parameters(remove_duplicate=False))
flags = {name: parameter.requires_grad for name, parameter in parameters.items()}
with pytest.raises(ValueError):
mtd.bypass(student, teacher, batches, {"num_steps": 0, "modules": [selected]})
assert all(
parameter is parameters[name]
for name, parameter in student.named_parameters(remove_duplicate=False)
)
assert {
name: parameter.requires_grad
for name, parameter in student.named_parameters(remove_duplicate=False)
} == flags
with mtq.utils.enable_weight_access_and_writeback(student, student, writeback=False):
assert all(torch.equal(value, before[name]) for name, value in student.state_dict().items())
with mtq.utils.enable_weight_access_and_writeback(teacher, teacher, writeback=False):
assert all(
torch.equal(value, teacher_before[name]) for name, value in teacher.state_dict().items()
)
assert not any("_local_repair_state" in name for name in student.state_dict())
@pytest.mark.parametrize("fail", [False, True])
@pytest.mark.parametrize("teacher_offload", [False, True], ids=["resident", "cpu-offload"])
def test_public_bypass_preserves_caller_state_and_seeds_initial_reader(fail, teacher_offload):
initial, teacher, batches = _problem()
teacher.head.weight.requires_grad_(False)
if teacher_offload:
pytest.importorskip("accelerate").cpu_offload(teacher, execution_device="cpu")
teacher_flags = {
name: parameter.requires_grad for name, parameter in teacher.named_parameters()
}
teacher_modes = {module: module.training for module in teacher.modules()}
class RandomBatches:
def __init__(self):
self.draws = []
def draw(self):
values = (random.random(), np.random.random(), torch.rand(()).item())
self.draws.append(values)
return sum(values) * 0.01
def __iter__(self):
self.draw()
return self.read()
def read(self):
for index, batch in enumerate(batches):
if fail and index == 1:
raise OSError("reader failure")
yield batch + self.draw()
previous = None
for caller_seed in (101, 203):
random.seed(caller_seed)
np.random.seed(caller_seed)
torch.manual_seed(caller_seed)
caller = (
random.getstate(),
np.random.get_state(),
torch.get_rng_state(),
torch.cuda.get_rng_state_all() if torch.cuda.is_available() else [],
)
data, student = RandomBatches(), copy.deepcopy(initial)
recipe = {
"num_steps": 2,
"global_batch_size": 2,
"sequence_length": 3,
"scheduler": "constant",
"warmup_fraction": 0,
}
if fail:
with pytest.raises(OSError):
mtd.bypass(student, teacher, data, recipe)
else:
mtd.bypass(student, teacher, data, recipe)
assert {
name: parameter.requires_grad for name, parameter in teacher.named_parameters()
} == teacher_flags
assert all(module.training == mode for module, mode in teacher_modes.items())
assert random.getstate() == caller[0]
actual_numpy = np.random.get_state()
assert actual_numpy[0] == caller[1][0]
np.testing.assert_array_equal(actual_numpy[1], caller[1][1])
assert actual_numpy[2:] == caller[1][2:]
assert torch.equal(torch.get_rng_state(), caller[2])
assert all(
torch.equal(torch.cuda.get_rng_state(i), state) for i, state in enumerate(caller[3])
)
if previous is not None:
draws, state = previous
assert data.draws == draws
assert all(
torch.equal(value, state[name]) for name, value in student.state_dict().items()
)
previous = (data.draws, copy.deepcopy(student.state_dict()))
@pytest.mark.parametrize("offload", ["cpu", "disk"])
def test_public_bypass_offloaded_nested_boundary_matches_resident(offload, tmp_path):
accelerate = pytest.importorskip("accelerate")
student, teacher, batches = _problem()
student.mlp, teacher.mlp = nn.Sequential(student.mlp), nn.Sequential(teacher.mlp)
reference = copy.deepcopy(student)
config = {
"num_steps": 2,
"global_batch_size": 2,
"micro_batch_size": 1,
"sequence_length": 3,
"modules": ["mlp"],
}
mtd.bypass(reference, teacher, batches, config)
if offload == "cpu":
accelerate.cpu_offload(student, execution_device="cpu", offload_buffers=True)
else:
accelerate.disk_offload(
student, tmp_path / "offload", execution_device="cpu", offload_buffers=True
)
assert not hasattr(student.mlp, "_hf_hook")
devices = {name: parameter.device for name, parameter in student.named_parameters()}
flags = {name: parameter.requires_grad for name, parameter in student.named_parameters()}
assert mtd.bypass(student, teacher, batches, config) is student
assert {name: parameter.device for name, parameter in student.named_parameters()} == devices
assert {
name: parameter.requires_grad for name, parameter in student.named_parameters()
} == flags
assert not hasattr(student.mlp[0].weight_quantizer, "_local_repair_state")
torch.testing.assert_close(student(batches[0]), reference(batches[0]), rtol=0, atol=0)
@pytest.mark.timeout(240)
@pytest.mark.parametrize("first", ["distill", "quantization"])
def test_distill_quantization_import_order_keeps_trainer_plugin(first):
pytest.importorskip("transformers")
code = "\n".join(
[
f"import modelopt.torch.{first}",
"import sys",
"import modelopt.torch.distill as mtd",
"import modelopt.torch.quantization as mtq",
"assert callable(mtd.bypass)",
"assert 'modelopt.torch.quantization.plugins.transformers_trainer' in sys.modules",
"assert hasattr(mtq.plugins, 'QATTrainer')",
"assert hasattr(mtq.plugins, 'QADTrainer')",
]
)
subprocess.run([sys.executable, "-c", code], check=True, timeout=180)
@pytest.mark.skipif(not torch.cuda.is_available(), reason="CUDA memory and kernel qualification")
def test_bypass_cuda_mse_bounds_scratch_and_preserves_masked_vjp(monkeypatch, capsys):
from modelopt.torch.distill._bypass_capture import _MSE_CHUNK_ELEMENTS, mse_statistics
prediction = torch.ones((1, 32768, 4096), device="cuda", dtype=torch.bfloat16)
prediction.requires_grad_(True)
target = torch.zeros_like(prediction)
mask = torch.arange(32768, device="cuda").remainder(2).eq(0).unsqueeze(0)
metrics = {"device": torch.cuda.get_device_name(), "shape": list(prediction.shape)}
for elements in (1 << 18, _MSE_CHUNK_ELEMENTS["cuda"]):
monkeypatch.setitem(_MSE_CHUNK_ELEMENTS, "cuda", elements)
times, peaks = [], []
for repeat in range(4):
prediction.grad = None
torch.cuda.synchronize()
baseline = torch.cuda.memory_allocated()
torch.cuda.reset_peak_memory_stats()
start = time.perf_counter()
loss, count = mse_statistics(prediction, target, mask)
loss.backward()
torch.cuda.synchronize()
elapsed = time.perf_counter() - start
peak = torch.cuda.max_memory_allocated() - baseline
if repeat:
times.append(elapsed)
peaks.append(peak)
assert loss.item() == count.item() == prediction.numel() // 2
assert torch.all(prediction.grad[:, ::2] == 2)
assert torch.all(prediction.grad[:, 1::2] == 0)
# One narrow gradient plus chunk-sized FP32 operands; prohibit full FP32 copies.
budget = prediction.numel() * prediction.element_size() + 16 * elements + (16 << 20)
assert max(peaks) <= budget
metrics[str(elements)] = {
"median_forward_backward_seconds": statistics.median(times),
"extra_peak_allocated_bytes": max(peaks),
"extra_budget_bytes": budget,
}
with capsys.disabled():
print("Bypass CUDA MSE metrics: " + json.dumps(metrics, sort_keys=True), flush=True)
@pytest.mark.parametrize("shared", [False, True])
def test_bypass_checks_retained_backings_inside_resident_offload_windows(shared):
accelerate = pytest.importorskip("accelerate")
student, teacher, batches = _problem()
student_state = {
name: value.double() if value.is_floating_point() else value.clone()
for name, value in student.state_dict().items()
}
teacher_state = {
name: value.double() if value.is_floating_point() else value.clone()
for name, value in teacher.state_dict().items()
}
if shared:
teacher_state["mlp.weight"] = student_state["mlp.weight"].view_as(
student_state["mlp.weight"]
)
for model, state in ((student, student_state), (teacher, teacher_state)):
accelerate.cpu_offload(
model, execution_device="cpu", offload_buffers=True, state_dict=state
)
with ExitStack() as stack:
for model in (student, teacher):
stack.enter_context(
mtq.utils.enable_weight_access_and_writeback(model, model, writeback=False)
)
assert not student.mlp._hf_hook.offload and not teacher.mlp._hf_hook.offload
assert (
student.mlp.weight.untyped_storage().data_ptr()
!= teacher.mlp.weight.untyped_storage().data_ptr()
)
parameters = list(student.parameters())
if shared:
with pytest.raises(ValueError):
mtd.bypass(student, teacher, batches, {"num_steps": 0, "modules": ["mlp"]})
else:
assert (
mtd.bypass(student, teacher, batches, {"num_steps": 0, "modules": ["mlp"]})
is student
)
assert all(
actual is original
for actual, original in zip(student.parameters(), parameters, strict=True)
)
assert not any(hasattr(module, "_local_repair_state") for module in student.modules())
def test_public_bypass_retries_backup_recovery_before_restoring_caller_state():
accelerate = pytest.importorskip("accelerate")
student, teacher, batches = _problem()
teacher.head.weight.requires_grad_(False)
modes = {module: module.training for model in (student, teacher) for module in model.modules()}
teacher_flags = {
name: parameter.requires_grad for name, parameter in teacher.named_parameters()
}
before = copy.deepcopy(student.state_dict())
masters = {
name: value.double() if value.is_floating_point() else value.clone()
for name, value in before.items()
}
accelerate.cpu_offload(
student, execution_device="cpu", offload_buffers=True, state_dict=masters
)
publication_error = RuntimeError("deliberate publication failure")
remaining_failures = 2
def fail_publication(module, state_dict, prefix, metadata):
nonlocal remaining_failures
if remaining_failures:
remaining_failures -= 1
if remaining_failures == 1:
raise publication_error
raise RuntimeError("transient owner recovery failure")
handle = student.mlp.register_state_dict_post_hook(fail_publication)
caught = None
try:
mtd.bypass(
student,
teacher,
batches,
{"num_steps": 1, "global_batch_size": 2, "sequence_length": 3},
)
except BaseException as exc:
caught = exc
finally:
handle.remove()
assert remaining_failures == 0
assert caught is publication_error
assert all(module.training == mode for module, mode in modes.items())
assert {
name: parameter.requires_grad for name, parameter in teacher.named_parameters()
} == teacher_flags
assert not any(hasattr(module, "_local_repair_state") for module in student.modules())
with mtq.utils.enable_weight_access_and_writeback(student, student, writeback=False):
torch.testing.assert_close(student.state_dict(), before, rtol=0, atol=0)
@pytest.mark.parametrize("failure", ["format", "publication"])
def test_bypass_finalization_failure_preserves_documented_boundary_commits(failure):
accelerate = pytest.importorskip("accelerate")
student, teacher, batches = _problem()
before = copy.deepcopy(student.state_dict())
teacher_before = copy.deepcopy(teacher.state_dict())
teacher.head.weight.requires_grad_(False)
flags = {name: parameter.requires_grad for name, parameter in teacher.named_parameters()}
modes = {module: module.training for root in (student, teacher) for module in root.modules()}
accelerate.cpu_offload(student, execution_device="cpu", offload_buffers=True)
publication_error = RuntimeError("deliberate second-boundary publication failure")
armed = True
def change_format(module, args, output):
nonlocal armed
if armed:
armed = False
module.weight_quantizer.num_bits = "iq1_s"
def fail_publication(module, state_dict, prefix, metadata):
nonlocal armed
if armed:
armed = False
raise publication_error
handle = (
student.moe.register_forward_hook(change_format)
if failure == "format"
else student.moe.register_state_dict_post_hook(fail_publication)
)
caught = None
try:
mtd.bypass(
student,
teacher,
batches,
{"num_steps": 1, "global_batch_size": 2, "sequence_length": 3},
)
except BaseException as exc:
caught = exc
finally:
handle.remove()
assert not armed
if failure == "format":
assert isinstance(caught, ValueError)
else:
assert caught is publication_error
assert not any(hasattr(module, "_local_repair_state") for module in student.modules())
assert all(module.training == mode for module, mode in modes.items())
assert {
name: parameter.requires_grad for name, parameter in teacher.named_parameters()
} == flags
torch.testing.assert_close(teacher.state_dict(), teacher_before, rtol=0, atol=0)
with mtq.utils.enable_weight_access_and_writeback(student, student, writeback=False):
torch.testing.assert_close(student.moe.weight, before["moe.weight"], rtol=0, atol=0)
if failure == "format":
torch.testing.assert_close(student.mlp.weight, before["mlp.weight"], rtol=0, atol=0)
else:
assert not torch.equal(student.mlp.weight, before["mlp.weight"])
assert pinned_packed_weight(student.mlp.weight_quantizer, "iq1_m") is not None
@@ -89,6 +89,7 @@ def test_iq_scales_backpropagate_through_frozen_projection(frozen_format):
_config(
[
_iq("shared.*weight_quantizer", "iq2_s"),
_iq("router.weight_quantizer", "iq2_s"),
_iq("experts.*.gate.weight_quantizer", "iq1_m"),
_iq("experts.*.up.weight_quantizer", "iq1_m"),
{"quantizer_name": "experts.*.down.weight_quantizer", "cfg": frozen_cfg},
@@ -14,6 +14,8 @@
# limitations under the License.
import dataclasses
import subprocess
import sys
import warnings
from functools import partial
from types import SimpleNamespace
@@ -45,6 +47,23 @@ from modelopt.torch.quantization.nn import TensorQuantizer
SEED = 1234
@pytest.mark.timeout(240)
def test_distill_import_initializes_optional_megatron_plugin():
subprocess.run(
[
sys.executable,
"-c",
(
"import sys; import modelopt.torch.distill as mtd; "
"assert 'modelopt.torch.distill.plugins.megatron' in sys.modules; "
"assert hasattr(mtd.plugins, 'megatron')"
),
],
check=True,
timeout=180,
)
def _test_logits_kl_loss(rank, size):
"""Test basic LogitsKLLoss with simple forward/backward pass."""
set_seed(SEED)
+20 -1
View File
@@ -21,6 +21,7 @@ from contextlib import nullcontext
import pytest
import torch
from modelopt.torch.distill import BypassConfig
from modelopt.torch.distill._bypass_capture import (
_MSE_CHUNK_ELEMENTS,
BypassUnit,
@@ -28,7 +29,7 @@ from modelopt.torch.distill._bypass_capture import (
mse_statistics,
validate_unit_ownership,
)
from modelopt.torch.distill.config import BypassConfig
from modelopt.torch.distill._bypass_optimizer import Lion, learning_rate
from modelopt.torch.quantization.nn import TensorQuantizer
@@ -205,3 +206,21 @@ def test_capture_boundary_ownership_is_checked_before_replay(sharing):
tuple((name, id(value)) for name, value in student.named_parameters(remove_duplicate=False))
== before
)
def test_lion_conflicting_gradients_and_effective_schedule():
parameter = torch.nn.Parameter(torch.tensor([1.0, -1.0]))
optimizer = Lion([parameter], lr=0.001)
parameter.grad = torch.tensor([2.0, -4.0])
optimizer.step()
torch.testing.assert_close(parameter, torch.tensor([0.999, -0.999]))
torch.testing.assert_close(optimizer.state[parameter]["exp_avg"], torch.tensor([0.02, -0.04]))
parameter.grad = torch.tensor([-0.1, 0.1])
optimizer.step()
torch.testing.assert_close(parameter, torch.tensor([0.998, -0.998]))
torch.testing.assert_close(
optimizer.state[parameter]["exp_avg"], torch.tensor([0.0188, -0.0386])
)
config = BypassConfig(num_steps=23, max_lr=0.001, warmup_fraction=0.1)
for step, expected in {1: 0.001 / 3, 2: 0.002 / 3, 3: 0.001, 13: 0.0005, 23: 0.0}.items():
assert learning_rate(config, step) == pytest.approx(expected, abs=1e-12)