Cursor Agent AHAD commited on
Commit
7b0cf07
·
unverified ·
1 Parent(s): 7a6eba8

Fix PyTorch 2.6 checkpoint loading for Diac Lightning models

Browse files

Register 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>

backend/app/checkpoint_compat.py ADDED
@@ -0,0 +1,76 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ PyTorch 2.6+ compatibility for Lightning checkpoints from the Diac project.
3
+
4
+ Lightning checkpoints embed yacs.config.CfgNode in hyperparameters. PyTorch 2.6
5
+ defaults torch.load(..., weights_only=True), which rejects those objects unless
6
+ allowlisted or weights_only=False is used (trusted checkpoints only).
7
+ """
8
+
9
+ from __future__ import annotations
10
+
11
+ import logging
12
+
13
+ logger = logging.getLogger(__name__)
14
+
15
+ _PATCHED = False
16
+
17
+
18
+ def prepare_checkpoint_loading() -> None:
19
+ """Configure torch/Lightning so Diac HF checkpoints load on PyTorch >= 2.6."""
20
+ global _PATCHED
21
+ if _PATCHED:
22
+ return
23
+
24
+ _register_safe_globals()
25
+ _patch_lightning_load_from_checkpoint()
26
+ _PATCHED = True
27
+ logger.debug("Checkpoint loading compatibility applied (PyTorch 2.6+ / yacs)")
28
+
29
+
30
+ def _register_safe_globals() -> None:
31
+ try:
32
+ import torch
33
+ except ImportError:
34
+ return
35
+
36
+ if not hasattr(torch.serialization, "add_safe_globals"):
37
+ return
38
+
39
+ entries: list[type] = []
40
+ try:
41
+ from yacs.config import CfgNode
42
+
43
+ entries.append(CfgNode)
44
+ except ImportError:
45
+ pass
46
+
47
+ try:
48
+ from types import SimpleNamespace
49
+
50
+ entries.append(SimpleNamespace)
51
+ except ImportError:
52
+ pass
53
+
54
+ if entries:
55
+ torch.serialization.add_safe_globals(entries)
56
+
57
+
58
+ def _patch_lightning_load_from_checkpoint() -> None:
59
+ """
60
+ Diac's from_pretrained() does not pass weights_only=False.
61
+ Trusted NAACL/Hugging Face checkpoints require full pickle load.
62
+ """
63
+ try:
64
+ from lightning.pytorch.core import LightningModule
65
+ except ImportError:
66
+ return
67
+
68
+ original = LightningModule.__dict__["load_from_checkpoint"]
69
+
70
+ @classmethod
71
+ def load_from_checkpoint_compat(cls, *args, **kwargs):
72
+ if kwargs.get("weights_only") is None:
73
+ kwargs["weights_only"] = False
74
+ return original(cls, *args, **kwargs)
75
+
76
+ LightningModule.load_from_checkpoint = load_from_checkpoint_compat
backend/app/services/model_service.py CHANGED
@@ -5,6 +5,8 @@ from __future__ import annotations
5
  import logging
6
  from typing import Optional
7
 
 
 
8
  logger = logging.getLogger(__name__)
9
 
10
  try:
@@ -40,6 +42,7 @@ class ModelService:
40
  )
41
 
42
  logger.info("Loading model %s with constants at %s", model_name, constants_path)
 
43
  self._model = DiacritizationModule.from_pretrained(
44
  model_name,
45
  tokenizer_constants_path=constants_path,
 
5
  import logging
6
  from typing import Optional
7
 
8
+ from backend.app.checkpoint_compat import prepare_checkpoint_loading
9
+
10
  logger = logging.getLogger(__name__)
11
 
12
  try:
 
42
  )
43
 
44
  logger.info("Loading model %s with constants at %s", model_name, constants_path)
45
+ prepare_checkpoint_loading()
46
  self._model = DiacritizationModule.from_pretrained(
47
  model_name,
48
  tokenizer_constants_path=constants_path,