Skip to content

armgit / laya

Public Imported from GitHub
0

Non-autoregressive System 1 decision engine. Typed choice, score and yes/no decisions over any text in a single forward pass, in 100+ languages, with a router that picks the right checkpoint per request.

fix(train): move the model to the device before the base evaluation

František Karešbd73c83dfa65
diff --git a/laya/train.py b/laya/train.py
index facf1cfb..ccbf70db 100644
--- a/laya/train.py
+++ b/laya/train.py
@@ -969,6 +969,9 @@ def finetune(data: str, model_dir: str, output_dir: str, config: Optional[TrainC
config.validate()
dev = resolve_device(device)
model, tok, cfg = load_checkpoint(model_dir)
+ # The base checkpoint is scored on eval_items before train_model runs, and calibration_records
+ # sends its inputs to `dev`, so the weights have to be there first (CUDA and MPS crash otherwise).
+ model.to(dev)
max_len = config.max_len or cfg.get("max_len", 512)
head_max_len = config.head_max_len or cfg.get("head_max_len", 192)
if config.option_layout is not None:
diff --git a/tests/test_train.py b/tests/test_train.py
index 85dfc5c3..6b3ca51c 100644
--- a/tests/test_train.py
+++ b/tests/test_train.py
@@ -574,6 +574,37 @@ class EndToEndTests(unittest.TestCase):
self.assertTrue(torch.allclose(before[name].half().float(), after[name].float()), name)
self.assertFalse(torch.allclose(before["scorer.1.weight"].half().float(), after["scorer.1.weight"].float()))
+ def test_model_is_on_the_device_before_the_base_evaluation(self):
+ # finetune scores the base checkpoint before train_model moves the model, which crashed
+ # on CUDA and MPS (inputs on the device, weights on CPU). CPU cannot show the crash, so
+ # record the order instead: the model must be moved before the first calibration_records.
+ import laya.train as lt
+
+ events = []
+ real_load, real_records = lt.load_checkpoint, lt.calibration_records
+
+ def load_checkpoint(model_dir):
+ model, tok, cfg = real_load(model_dir)
+ real_to = model.to
+
+ def to(*args, **kwargs):
+ events.append(("to", torch.device(args[0] if args else kwargs["device"])))
+ return real_to(*args, **kwargs)
+
+ model.to = to
+ return model, tok, cfg
+
+ def calibration_records(model, tok, items, device, *args, **kwargs):
+ events.append(("records", device))
+ return real_records(model, tok, items, device, *args, **kwargs)
+
+ with patch("laya.train.load_checkpoint", load_checkpoint), \
+ patch("laya.train.calibration_records", calibration_records):
+ self.run_finetune("out_device", epochs=1)
+ cpu = torch.device("cpu")
+ first_eval = events.index(("records", cpu))
+ self.assertIn(("to", cpu), events[:first_eval], events)
+
def test_frozen_encoder_runs_without_dropout(self):
from laya.train import load_checkpoint