Restore derived LNS scale buffers after checkpoint loading
Browse files- modeling_neollm.py +22 -1
modeling_neollm.py
CHANGED
|
@@ -6752,7 +6752,7 @@ class NeoLLMPreTrainedModel(PreTrainedModel):
|
|
| 6752 |
|
| 6753 |
@classmethod
|
| 6754 |
def from_pretrained(cls, *args, **kwargs):
|
| 6755 |
-
"""
|
| 6756 |
|
| 6757 |
Current checkpoints persist this buffer and must retain its exact saved
|
| 6758 |
values. The previous compatibility path regenerated the grid after
|
|
@@ -6767,6 +6767,27 @@ class NeoLLMPreTrainedModel(PreTrainedModel):
|
|
| 6767 |
**kwargs,
|
| 6768 |
)
|
| 6769 |
missing_keys = set(loading_info.get("missing_keys", ()))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6770 |
for module_name, module in model.named_modules():
|
| 6771 |
if not isinstance(module, LeviathanGenerator):
|
| 6772 |
continue
|
|
|
|
| 6752 |
|
| 6753 |
@classmethod
|
| 6754 |
def from_pretrained(cls, *args, **kwargs):
|
| 6755 |
+
"""Restore derived buffers and support legacy ``knot_grid`` loading.
|
| 6756 |
|
| 6757 |
Current checkpoints persist this buffer and must retain its exact saved
|
| 6758 |
values. The previous compatibility path regenerated the grid after
|
|
|
|
| 6767 |
**kwargs,
|
| 6768 |
)
|
| 6769 |
missing_keys = set(loading_info.get("missing_keys", ()))
|
| 6770 |
+
# LNS.scale is derived, non-persistent state. Low-memory loaders can
|
| 6771 |
+
# materialize its meta storage without running the original constructor
|
| 6772 |
+
# initializer. Rebuild the constant; never infer validity from finiteness.
|
| 6773 |
+
for module in model.modules():
|
| 6774 |
+
if isinstance(module, LNS):
|
| 6775 |
+
scale_device = module.scale.device
|
| 6776 |
+
if scale_device.type == "meta":
|
| 6777 |
+
scale_device = model.device
|
| 6778 |
+
if scale_device.type == "meta":
|
| 6779 |
+
scale_device = torch.device("cpu")
|
| 6780 |
+
scale_dtype = (
|
| 6781 |
+
requested_dtype
|
| 6782 |
+
if isinstance(requested_dtype, torch.dtype)
|
| 6783 |
+
and requested_dtype.is_floating_point
|
| 6784 |
+
else module.scale.dtype
|
| 6785 |
+
)
|
| 6786 |
+
module._buffers["scale"] = torch.tensor(
|
| 6787 |
+
1.0 / math.sqrt(module.layer_idx),
|
| 6788 |
+
device=scale_device,
|
| 6789 |
+
dtype=torch.float32,
|
| 6790 |
+
).to(dtype=scale_dtype)
|
| 6791 |
for module_name, module in model.named_modules():
|
| 6792 |
if not isinstance(module, LeviathanGenerator):
|
| 6793 |
continue
|