Cursor Agent AHAD commited on
Fix PyTorch 2.6 checkpoint loading for Diac Lightning models
Browse filesRegister yacs CfgNode safe globals and default weights_only=False when
loading from_pretrained checkpoints (PyTorch 2.6 weights_only=True default).
Co-authored-by: AHAD <ahad-m@users.noreply.github.com>
Files changed (2)
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576"""PyTorch 2.6+ compatibility for Lightning checkpoints from the Diac project.
Lightning checkpoints embed yacs.config.CfgNode in hyperparameters. PyTorch 2.6defaults torch.load(..., weights_only=True), which rejects those objects unlessallowlisted or weights_only=False is used (trusted checkpoints only)."""
from __future__ import annotations
import logging
logger = logging.getLogger(__name__)
_PATCHED = False
def prepare_checkpoint_loading() -> None: """Configure torch/Lightning so Diac HF checkpoints load on PyTorch >= 2.6.""" global _PATCHED if _PATCHED: return
_register_safe_globals() _patch_lightning_load_from_checkpoint() _PATCHED = True logger.debug("Checkpoint loading compatibility applied (PyTorch 2.6+ / yacs)")
def _register_safe_globals() -> None: try: import torch except ImportError: return
if not hasattr(torch.serialization, "add_safe_globals"): return
entries: list[type] = [] try: from yacs.config import CfgNode
entries.append(CfgNode) except ImportError: pass
try: from types import SimpleNamespace
entries.append(SimpleNamespace) except ImportError: pass
if entries: torch.serialization.add_safe_globals(entries)
def _patch_lightning_load_from_checkpoint() -> None: """ Diac's from_pretrained() does not pass weights_only=False. Trusted NAACL/Hugging Face checkpoints require full pickle load. """ try: from lightning.pytorch.core import LightningModule except ImportError: return
original = LightningModule.__dict__["load_from_checkpoint"]
@classmethod def load_from_checkpoint_compat(cls, *args, **kwargs): if kwargs.get("weights_only") is None: kwargs["weights_only"] = False return original(cls, *args, **kwargs)
LightningModule.load_from_checkpoint = load_from_checkpoint_compat4 unmodified lines567891029 unmodified lines4041424344454 unmodified linesimport loggingfrom typing import Optionallogger = logging.getLogger(__name__)try:29 unmodified lines)logger.info("Loading model %s with constants at %s", model_name, constants_path)self._model = DiacritizationModule.from_pretrained(model_name,tokenizer_constants_path=constants_path,4 unmodified lines5678910111229 unmodified lines424344454647484 unmodified linesimport loggingfrom typing import Optionalfrom backend.app.checkpoint_compat import prepare_checkpoint_loadinglogger = logging.getLogger(__name__)try:29 unmodified lines)logger.info("Loading model %s with constants at %s", model_name, constants_path)prepare_checkpoint_loading()self._model = DiacritizationModule.from_pretrained(model_name,tokenizer_constants_path=constants_path,
backend/app/checkpoint_compat.pyAdded+76
backend/app/services/model_service.py+3