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(dotnet): a temperature wider than a double falls back to 1.0 like Python 0.4.20

kevin-gatimub85e944fdccd
diff --git a/laya-dotnet/src/Laya/Calibration.cs b/laya-dotnet/src/Laya/Calibration.cs
index 09115493..cb2c9a31 100644
--- a/laya-dotnet/src/Laya/Calibration.cs
+++ b/laya-dotnet/src/Laya/Calibration.cs
@@ -27,6 +27,7 @@ public static class Calibration
/// <see cref="ClampTemperature(double)"/> for a value read from JSON, exactly as Python's
/// <c>clamp_temperature</c> treats it: a <see cref="bool"/> is 1.0, and so is anything
/// <c>float()</c> refuses (<see langword="null"/>, a list, a dictionary, a non-numeric string).
+ /// An integer too wide for a double (Python's <c>OverflowError</c>) is 1.0 as well.
/// A numeric string such as <c>"2.5"</c>, <c>" 2.5 "</c> or <c>"1e1"</c> is read the way
/// <c>float()</c> reads it.
/// </summary>
diff --git a/laya-dotnet/src/Laya/CalibrationFile.cs b/laya-dotnet/src/Laya/CalibrationFile.cs
index c25b49a9..472e35e7 100644
--- a/laya-dotnet/src/Laya/CalibrationFile.cs
+++ b/laya-dotnet/src/Laya/CalibrationFile.cs
@@ -182,11 +182,9 @@ internal static partial class CalibrationFile
private static (double[] Temperature, Dictionary<string, double> ByOptions) InstallTemperatures(
List<object?> raw, IReadOnlyDictionary<string, object?> rawByOptions, Action<string> warn)
{
- // `clamp_temperature(<int too large for a float>)` raises an OverflowError that nothing catches.
- foreach (var value in raw.Concat(rawByOptions.Values))
- if (value is BigInteger big && double.IsInfinity((double)big))
- throw new InvalidDataException("OverflowError: int too large to convert to float");
-
+ // An integer too large for a float is an OverflowError inside `clamp_temperature` and `temperature_rejected`,
+ // which both read it as "does not convert": 1.0, and named in the warning digit for digit. TryConvert reads
+ // it as infinity, which the clamp answers with 1.0 and the rejection check reports.
var applied = raw.Select(Calibration.ClampTemperature).ToArray();
var appliedByOptions = new Dictionary<string, double>(StringComparer.Ordinal);
foreach (var (key, value) in rawByOptions) appliedByOptions[key] = Calibration.ClampTemperature(value);
diff --git a/laya-dotnet/src/Laya/LayaConfig.cs b/laya-dotnet/src/Laya/LayaConfig.cs
index e29b302c..d6b120ca 100644
--- a/laya-dotnet/src/Laya/LayaConfig.cs
+++ b/laya-dotnet/src/Laya/LayaConfig.cs
@@ -149,7 +149,6 @@ public sealed class LayaConfig
else { temperature.AddRange([1.0, 1.0, 1.0]); temperatureSource = new List<object?>(temperature); }
// A list shorter than three is kept as it is: Python indexes self.temperature[qtype] when a question is
// answered and raises IndexError there, which Calibration.ResolveTemperature reproduces.
- ThrowIfOverflow(temperature);
// `{k: clamp_temperature(v) for k, v in temperature_by_options_raw.items()}`: a mapping or AttributeError.
var byOptions = new Dictionary<string, object?>(StringComparer.Ordinal);
@@ -160,7 +159,6 @@ public sealed class LayaConfig
foreach (var (key, value) in map)
byOptions[key] = value;
}
- ThrowIfOverflow(byOptions.Values);
var lengths = Lengths(root);
return new LayaConfig(
@@ -196,14 +194,6 @@ public sealed class LayaConfig
return items;
}
- // clamp_temperature(<int too large for a float>) raises OverflowError, which nothing catches, so Python fails
- // to load the checkpoint (onnx_agent.py:249-252); a float literal reads as inf instead.
- private static void ThrowIfOverflow(IEnumerable<object?> values)
- {
- if (values.Any(v => v is System.Numerics.BigInteger big && double.IsInfinity((double)big)))
- throw new InvalidDataException("OverflowError: int too large to convert to float");
- }
-
// `cfg.get("max_len", 512)` and `cfg.get("head_max_len", 192)`. Python keeps whatever the JSON held and fails at
// the first prediction that does arithmetic or slicing with it; this port refuses a value it cannot use when the
// config is loaded, with the TypeError Python raises at that prediction (build_sequence meets head_max_len first,
diff --git a/laya-dotnet/tests/Laya.Tests/ConfigFieldTypeTests.cs b/laya-dotnet/tests/Laya.Tests/ConfigFieldTypeTests.cs
index f781e93a..e8fbf51c 100644
--- a/laya-dotnet/tests/Laya.Tests/ConfigFieldTypeTests.cs
+++ b/laya-dotnet/tests/Laya.Tests/ConfigFieldTypeTests.cs
@@ -36,7 +36,7 @@ public sealed class ConfigFieldTypeTests
{ "{\"temperature_by_options\": 1.5}", "AttributeError: 'float' object has no attribute 'items'", new double[0], new string[0] },
{ "{\"temperature\": 5, \"temperature_by_options\": [1]}", "TypeError: 'int' object is not iterable", new double[0], new string[0] },
{ "{\"temperature\": 1000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000, \"temperature_by_options\": 5}", "TypeError: 'int' object is not iterable", new double[0], new string[0] },
- { "{\"temperature\": [1, 1, 1], \"temperature_by_options\": {\"a\": 1000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000}}", "OverflowError: int too large to convert to float", new double[0], new string[0] },
+ { "{\"temperature\": [1, 1, 1], \"temperature_by_options\": {\"a\": 1000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000}}", null, new double[] { 1.0, 1.0, 1.0 }, new string[] { "a=1000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 -> 1" } },
{ "{}", null, new double[] { 1.0, 1.0, 1.0 }, new string[] { } },
{ "{\"temperature\": [0.2, 1, 1], \"temperature_by_options\": {\"x\": 9}}", null, new double[] { 0.5, 1.0, 1.0 }, new string[] { "x=9 -> 5", "temperature[0]=0.2 -> 0.5" } },
{ "{\"option_layout\": \"sequential\"}", null, new double[] { 1.0, 1.0, 1.0 }, new string[] { } },
diff --git a/laya-dotnet/tests/Laya.Tests/TemperatureConfigTests.cs b/laya-dotnet/tests/Laya.Tests/TemperatureConfigTests.cs
index 9a440959..dc3b8a0c 100644
--- a/laya-dotnet/tests/Laya.Tests/TemperatureConfigTests.cs
+++ b/laya-dotnet/tests/Laya.Tests/TemperatureConfigTests.cs
@@ -113,20 +113,25 @@ public sealed class TemperatureConfigTests
["a=inf -> 1", "b=-inf -> 1", "c='NaN' -> 1", "temperature[0]=nan -> 1"], config.ClampedTemperatures);
}
- // Python: float literals that overflow read as inf; 10**308 is an int that still converts, 10**309 does not.
+ // Python 0.4.20: float literals that overflow read as inf; 10**308 is an int that still converts, and an int too wide
+ // for a double (10**309, 2**1024) is read by clamp_temperature/temperature_rejected as unreadable: 1.0, named in full.
[Fact]
- public void OverflowingFloatLiteralIsInfinityButAHugeIntegerFailsTheLoad()
+ public void OverflowingFloatLiteralIsInfinityAndAHugeIntegerFallsBackToOne()
{
var ok = LayaConfig.Parse("{\"temperature\": [1, 1e400, 1" + new string('0', 308) + "]}");
Assert.Equal<IEnumerable<double>>(new[] { 1.0, 1.0, 5.0 }, ok.Temperature);
Assert.Equal("temperature[1]=inf -> 1", ok.ClampedTemperatures[0]);
Assert.Equal("temperature[2]=1" + new string('0', 308) + " -> 5", ok.ClampedTemperatures[1]);
- foreach (var huge in new[] { "1" + new string('0', 309), "-1" + new string('0', 309) })
+ var wide = System.Numerics.BigInteger.Pow(2, 1024).ToString(System.Globalization.CultureInfo.InvariantCulture);
+ foreach (var huge in new[] { "1" + new string('0', 309), "-1" + new string('0', 309), wide })
{
- var inList = Assert.Throws<InvalidDataException>(() => LayaConfig.Parse("{\"temperature\": [1, 1, " + huge + "]}"));
- Assert.Equal("OverflowError: int too large to convert to float", inList.Message);
- Assert.Throws<InvalidDataException>(() => LayaConfig.Parse("{\"temperature_by_options\": {\"x\": " + huge + "}}"));
+ var config = LayaConfig.Parse(
+ "{\"temperature\": [" + huge + ", 2.0, 3.0], \"temperature_by_options\": {\"choice:2\": " + huge + "}}");
+ Assert.Equal<IEnumerable<double>>(new[] { 1.0, 2.0, 3.0 }, config.Temperature);
+ Assert.Equal(1.0, config.TemperatureByOptions["choice:2"]);
+ Assert.Equal<IEnumerable<string>>(
+ ["choice:2=" + huge + " -> 1", "temperature[0]=" + huge + " -> 1"], config.ClampedTemperatures);
}
}
}
diff --git a/laya-dotnet/tests/Laya.Tests/golden/routing/calibration_probe.json b/laya-dotnet/tests/Laya.Tests/golden/routing/calibration_probe.json
index 89531bf5..66d31e0d 100644
--- a/laya-dotnet/tests/Laya.Tests/golden/routing/calibration_probe.json
+++ b/laya-dotnet/tests/Laya.Tests/golden/routing/calibration_probe.json
@@ -4614,9 +4614,21 @@
}
}
},
- "error_type": "OverflowError",
- "error_message": "int too large to convert to float",
- "warnings": []
+ "result": {
+ "temperature": [
+ 1.0,
+ 1.0,
+ 1.0
+ ],
+ "temperature_by_options": {},
+ "binning_map": null
+ },
+ "warnings": [
+ {
+ "category": "RuntimeWarning",
+ "message": "laya: calibration temperatures are invalid or outside [0.5, 5]; using temperature[0]=10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 -> 1. Treat confidence from the affected entries as uncalibrated."
+ }
+ ]
},
{
"label": "a bucket temperature that is a huge integer",
@@ -4639,9 +4651,23 @@
}
}
},
- "error_type": "OverflowError",
- "error_message": "int too large to convert to float",
- "warnings": []
+ "result": {
+ "temperature": [
+ 1.5,
+ 2.0,
+ 1.25
+ ],
+ "temperature_by_options": {
+ "noul:2": 1.0
+ },
+ "binning_map": null
+ },
+ "warnings": [
+ {
+ "category": "RuntimeWarning",
+ "message": "laya: calibration temperatures are invalid or outside [0.5, 5]; using noul:2=10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 -> 1. Treat confidence from the affected entries as uncalibrated."
+ }
+ ]
},
{
"label": "a huge integer after an identity warning",
@@ -4664,12 +4690,25 @@
}
}
},
- "error_type": "OverflowError",
- "error_message": "int too large to convert to float",
+ "result": {
+ "temperature": [
+ 1.0,
+ 1.0,
+ 1.0
+ ],
+ "temperature_by_options": {
+ "choice:3-5": 2.5
+ },
+ "binning_map": null
+ },
"warnings": [
{
"category": "UserWarning",
"message": "calibration was fitted for model_id_or_path='x' subfolder='multilingual' but this agent is model_id_or_path='convaiinnovations/laya' subfolder='multilingual'. Loading the temperatures anyway."
+ },
+ {
+ "category": "RuntimeWarning",
+ "message": "laya: calibration temperatures are invalid or outside [0.5, 5]; using temperature[0]=10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 -> 1. Treat confidence from the affected entries as uncalibrated."
}
]
}
diff --git a/laya-dotnet/tests/Laya.Tests/golden/routing/meta.json b/laya-dotnet/tests/Laya.Tests/golden/routing/meta.json
index d63b466c..785e0684 100644
--- a/laya-dotnet/tests/Laya.Tests/golden/routing/meta.json
+++ b/laya-dotnet/tests/Laya.Tests/golden/routing/meta.json
@@ -1,6 +1,6 @@
{
"generator": "tools/dump_routing_golden.py",
- "laya_version": "0.4.17",
+ "laya_version": "0.4.20",
"default_shortlist_k": 20,
"notes": {
"route_probe": "RouteDecision is serialized as {model, reason, detection, workflow}; 'repo' is intentionally omitted (not part of the ported RouteDecision).",