Spaces:
Sleeping
Sleeping
File size: 6,537 Bytes
3e3ee14 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 | # Private compiler configuration is loaded only when compilation is enabled.
# ruff: noqa: PLC0415
"""Model setup: compile, EMA, forward.
Mixin split out of :class:`morph.model.lightning.base.LightningModuleRates`.
"""
import os
import torch
from morph.model.layers.attention import fix_efficient_attention_bias_metadata
from morph.utils.checkpoints import canonicalize_state_dict, state_dict_for_module
class SetupMixin:
def compile(self):
"""Compile current and EMA models in place, unless MORPH_COMPILE=0.
Both entry points use this setup. Compilation is lazy: training and
inference acquire the appropriate graphs when first executed. In-place
compilation keeps module identity, parameter references and state keys.
"""
if self.is_compiled:
return
if os.environ.get("MORPH_COMPILE", "1") == "0":
print("Model compilation disabled (MORPH_COMPILE=0).")
return
print("Compiling current and EMA models (lazy; MORPH_COMPILE=0 disables).")
fix_efficient_attention_bias_metadata()
# PyTorch 2.9's DDP graph partitioner can lose FakeTensor metadata on
# recompilation after validation. Keep this workaround opt-in.
import torch._dynamo.config as dynamo_config
if os.environ.get("MORPH_COMPILE_DDP_OPTIMIZER") == "0":
dynamo_config.optimize_ddp = False
print("DDP compiler graph splitting disabled.")
if os.environ.get("MORPH_COMPILE_FALLBACK") == "1":
dynamo_config.suppress_errors = True
print("Compiler errors will warn and fall back to eager execution.")
# Per-loss grad-norm logging calls torch.autograd.grad(..., retain_graph=True)
# before the main backward. torch.compile's donated-buffer optimization frees
# activations after the first backward and rejects any subsequent one, so
# disable it when grad-norm tracking is active.
if self.log_grad_norms_every_n_steps > 0:
import torch._functorch.config as _functorch_config
_functorch_config.donated_buffer = False
for model in (self.model, self.model_ema):
if model is not None:
model.compile(dynamic=True)
self.is_compiled = True
def on_save_checkpoint(self, checkpoint):
"""Save portable parameter names independent of ``torch.compile``."""
state_dict = checkpoint.get("state_dict")
if state_dict is not None:
checkpoint["state_dict"] = canonicalize_state_dict(state_dict)
def on_load_checkpoint(self, checkpoint):
"""Adapt eager or compiled checkpoint keys to the current module."""
state_dict = checkpoint.get("state_dict")
if state_dict is not None:
checkpoint["state_dict"] = state_dict_for_module(state_dict, self)
# Lightning logs a microbatch counter. Rebase it when a resumed manual
# run changes microbatch size/accumulation, preserving the update axis.
if not getattr(self, "automatic_optimization", True):
loop_state = (
checkpoint.get("loops", {})
.get("fit_loop", {})
.get("epoch_loop.state_dict")
)
if loop_state is not None and "global_step" in checkpoint:
loop_state["_batches_that_stepped"] = (
checkpoint["global_step"] * self.manual_gradient_accumulation_steps
)
def _get_model(self):
"""Return model or model_ema for inference based on use_ema_for_eval."""
return (
self.model_ema
if self.use_ema_for_eval and self.model_ema is not None
else self.model
)
def _update_ema(self):
"""Update EMA parameters: ema = decay * ema + (1 - decay) * model."""
# In automatic optimization this hook runs just before Lightning marks
# the optimizer step complete; manual optimization has already marked
# both optimizer branches complete. Convert both cases to the same
# one-based count of logical model updates.
update_step = self._optimizer_update_step()
if self.automatic_optimization:
update_step += 1
ema_decay = 0.0
if update_step > self.ema_warmup_steps:
ema_decay = float(self.ema_decay)
if ema_decay and self.ema_decay_scheduler is not None:
ema_decay = self.ema_decay_scheduler.get_decay(
current_epoch=self.current_epoch,
)
with torch.no_grad():
ema_tensors = getattr(self, "_ema_tensors", None)
if ema_tensors is None:
ema_params = tuple(self.model_ema.parameters())
model_params = tuple(self.model.parameters())
ema_buffers = tuple(self.model_ema.buffers())
model_buffers = tuple(self.model.buffers())
if len(ema_params) != len(model_params):
raise RuntimeError("EMA and training model parameter sets differ")
if len(ema_buffers) != len(model_buffers):
raise RuntimeError("EMA and training model buffer sets differ")
ema_tensors = (
ema_params,
model_params,
ema_buffers,
model_buffers,
)
self._ema_tensors = ema_tensors
ema_params, model_params, ema_buffers, model_buffers = ema_tensors
# One grouped multi-tensor recurrence instead of two launches per
# parameter. A representative CPU transformer mix was ~22% faster;
# CUDA benefits more from eliminating the Python/kernel-launch loop.
if ema_decay == 0.0:
torch._foreach_copy_(ema_params, model_params)
else:
torch._foreach_lerp_(ema_params, model_params, 1.0 - ema_decay)
# Buffers are state rather than trainable weights; keep them exact,
# including integer counters that cannot be averaged.
if ema_buffers:
torch._foreach_copy_(ema_buffers, model_buffers)
self.log(
"ema/decay",
ema_decay,
on_step=True,
on_epoch=False,
prog_bar=False,
logger=True,
)
def forward(self, *args, **kwargs):
"""Forward through the trainable model."""
return self.model(*args, **kwargs)
|