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(common): support torch 2.0-2.3 in the action-head dtype check

thread3d9c37ad6d5069
diff --git a/laya/common.py b/laya/common.py
index 1cf74f26..eb19f7a6 100644
--- a/laya/common.py
+++ b/laya/common.py
@@ -19,6 +19,25 @@ QTYPES = {"choice": 0, "score": 1, "noul": 2}
QTYPE_NAMES = {v: k for k, v in QTYPES.items()}
_DEFAULT_NOUL_LABELS = {"false": "false", "true": "true"}
+
+def _autocast_enabled(device_type: str) -> bool:
+ """Whether an autocast scope covers `device_type`.
+
+ `torch.is_autocast_enabled` only accepted a `device_type` from torch 2.4; before that it
+ reported CUDA alone and CPU and MPS had no autocast to report. This package declares
+ `torch>=2.0.0`, so call the device-aware form where it exists and fall back otherwise,
+ using torch's separate CPU predicate.
+ """
+ try:
+ return torch.is_autocast_enabled(device_type)
+ except TypeError:
+ # torch < 2.4 took no `device_type`: the no-argument call reports CUDA alone, CPU has
+ # its own predicate, and MPS had no autocast to report.
+ if device_type == "cpu":
+ return torch.is_autocast_cpu_enabled()
+ return torch.is_autocast_enabled()
+
+
# A fast tokenizer is not read-only: `truncation=True` / `padding=True` make it call
# `enable_truncation` / `enable_padding`, which mutates the shared Rust object. One tokenizer is
# parsed per checkpoint directory and shared by every Agent that wants it, so concurrent
@@ -542,7 +561,7 @@ class DecisionModel(nn.Module):
act_input = torch.cat([pooled, feats], -1)
# Explicit-dtype exports run without autocast; keep the confidence maths in fp32,
# then match the head's weights. Autocast already chooses the linear's input dtype.
- if not torch.is_autocast_enabled(h.device.type):
+ if not _autocast_enabled(h.device.type):
act_input = act_input.to(self.act_head[0].weight.dtype)
act_logits = self.act_head(act_input)
return logits, act_logits