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(cpp): schema and sequence review round 1

Adeshina Lasisib4da92e2ce42
diff --git a/.github/workflows/cpp.yml b/.github/workflows/cpp.yml
index 076166ff..b23b7eb7 100644
--- a/.github/workflows/cpp.yml
+++ b/.github/workflows/cpp.yml
@@ -42,7 +42,7 @@ concurrency:
env:
# The test floor: the number of ctest tests laya-cpp registers (the GoogleTest cases plus
# compile_flags). Raise it with every test added.
- MIN_TESTS: "352"
+ MIN_TESTS: "365"
GOLDENS: ${{ github.workspace }}/.cpp-goldens
LAYA_GOLDENS: ${{ github.workspace }}/.cpp-goldens
LAYA_CHECKPOINTS: ${{ github.workspace }}/laya-java/.work/checkpoints # the tokenizer tests need both shipped checkpoints
diff --git a/laya-cpp/src/schema.cpp b/laya-cpp/src/schema.cpp
index 9e5a225d..ce67e5b8 100644
--- a/laya-cpp/src/schema.cpp
+++ b/laya-cpp/src/schema.cpp
@@ -4,6 +4,7 @@
#include <cmath>
#include <cstdlib>
+#include "cnum.hpp"
#include "laya/pyjson.hpp"
#include "laya/unicodedata.hpp"
@@ -202,7 +203,7 @@ std::string Label::EqKey() const {
case Kind::Float: break;
}
if (text_ == "NaN" || text_ == "Infinity" || text_ == "-Infinity") return "n:" + text_;
- double d = std::strtod(text_.c_str(), nullptr); // the C locale: the library never changes it
+ double d = cnum::ToDouble(text_); // text_ is Python-spelled with a dot, whatever the process locale
if (d == std::trunc(d)) return "n:" + ExactInteger(d);
return "f:" + text_; // shortest repr is unique per double
}
diff --git a/laya-cpp/src/sequence.cpp b/laya-cpp/src/sequence.cpp
index ae10bc95..42e635ec 100644
--- a/laya-cpp/src/sequence.cpp
+++ b/laya-cpp/src/sequence.cpp
@@ -4,6 +4,7 @@
#include <cstdio>
#include <set>
+#include "cnum.hpp"
#include "laya/pyjson.hpp"
namespace laya {
@@ -190,11 +191,7 @@ namespace {
constexpr int64_t kClampWarnRatio = 2;
constexpr int64_t kBatchBlowup = 2;
-std::string Ratio1(double x) {
- char buf[48];
- std::snprintf(buf, sizeof buf, "%.1f", x);
- return buf;
-}
+std::string Ratio1(double x) { return cnum::Fixed(x, 1); }
} // namespace
Result<WindowPlan> PlanWindows(const Tokenizer& tok, const std::vector<Question>& questions, const Budget& b,
diff --git a/laya-cpp/tests/golden.hpp b/laya-cpp/tests/golden.hpp
index f97db557..84499a06 100644
--- a/laya-cpp/tests/golden.hpp
+++ b/laya-cpp/tests/golden.hpp
@@ -4,6 +4,7 @@
// With LAYA_REQUIRE_GOLDENS=1 (CI) a missing golden is a test FAILURE, never a skip.
#include <gtest/gtest.h>
+#include <clocale>
#include <cstdlib>
#include <fstream>
#include <map>
@@ -94,4 +95,26 @@ inline double Num(const Value& v) {
return v.kind() == Value::Kind::Int ? double(v.as_int()) : v.as_float();
}
+// Switches the process to a comma-decimal locale for a scope; ok() is false when none is installed.
+class CommaLocale {
+ public:
+ CommaLocale() : saved_(std::setlocale(LC_ALL, nullptr)) {
+ for (const char* c : {"de_DE.UTF-8", "de_DE.utf8", "fr_FR.UTF-8", "fr_FR.utf8", "de_DE", "de_DE.ISO8859-1"}) {
+ if (std::setlocale(LC_ALL, c) != nullptr && *std::localeconv()->decimal_point == ',') {
+ ok_ = true;
+ return;
+ }
+ }
+ std::setlocale(LC_ALL, saved_.c_str());
+ }
+ ~CommaLocale() { std::setlocale(LC_ALL, saved_.c_str()); }
+ CommaLocale(const CommaLocale&) = delete;
+ CommaLocale& operator=(const CommaLocale&) = delete;
+ bool ok() const { return ok_; }
+
+ private:
+ std::string saved_;
+ bool ok_ = false;
+};
+
} // namespace laya_test
diff --git a/laya-cpp/tests/schema_test.cpp b/laya-cpp/tests/schema_test.cpp
index 00de4c4b..3256ca7d 100644
--- a/laya-cpp/tests/schema_test.cpp
+++ b/laya-cpp/tests/schema_test.cpp
@@ -166,6 +166,18 @@ TEST(Labels, PythonDictKeying) {
"1.0 and True are one key)");
}
+// strtod follows the process locale: under de_DE "1.5" read as 1, so 1.5 and 1 looked like one key.
+TEST(Labels, EqKeyIgnoresACommaDecimalLocale) {
+ laya_test::CommaLocale comma;
+ if (!comma.ok()) GTEST_SKIP() << "no comma-decimal locale installed";
+ using L = sc::Label;
+ EXPECT_NE(L::OfFloat(1.5).EqKey(), L::OfInt(1).EqKey());
+ EXPECT_EQ(L::OfFloat(1.5).EqKey(), "f:1.5");
+ EXPECT_EQ(L::OfFloat(2.0).EqKey(), L::OfInt(2).EqKey());
+ EXPECT_EQ(L::OfFloat(1e22).EqKey(), "n:10000000000000000000000");
+ EXPECT_TRUE(sc::Question::Choice("Pick.", {{L::OfInt(1), Value()}, {L::OfFloat(1.5), Value()}}).ok());
+}
+
TEST(Questions, Duplicates) {
auto q = sc::Question::Noul("Holds?").value();
EXPECT_FALSE(sc::Questions::Of({{"a", q}, {"a", q}}).ok());
diff --git a/laya-cpp/tests/sequence_test.cpp b/laya-cpp/tests/sequence_test.cpp
index 8922e814..fed04190 100644
--- a/laya-cpp/tests/sequence_test.cpp
+++ b/laya-cpp/tests/sequence_test.cpp
@@ -278,6 +278,32 @@ TEST(PlanWindows, ClampRatioBoundary) {
EXPECT_TRUE(found) << "no budget with a default window of twice the room";
}
+// snprintf follows the process locale: under de_DE the ratio read "2,5x".
+TEST(PlanWindows, ClampRatioIgnoresACommaDecimalLocale) {
+ LAYA_CHECKPOINT_TOK(tok, "english");
+ laya_test::CommaLocale comma;
+ if (!comma.ok()) GTEST_SKIP() << "no comma-decimal locale installed";
+ bool found = false;
+ for (int n = 1; n <= 40 && !found; n++) {
+ auto q = WindowQuestion(n);
+ for (int64_t head = 32; head <= 192 && !found; head += 8) {
+ // room = max_len - len(head) - 1 and the default window = max_len - head_max_len - 8, so the
+ // default is 2.5 * room at 3 * max_len = 5 * len(head) - 2 * head_max_len - 11.
+ const int64_t len = int64_t(sq::BuildHead(*tok, q, head).value().ids.size());
+ const int64_t top = 5 * len - 2 * head - 11;
+ if (top <= 0 || top % 3 != 0) continue;
+ sq::Budget b{top / 3, head};
+ auto room = sq::StateRoom(*tok, q, b);
+ if (!room.ok() || room.value() <= 0 || 2 * b.DefaultWindow() != 5 * room.value()) continue;
+ auto plan = sq::PlanWindows(*tok, {q}, b, {});
+ if (!plan.ok() || plan.value().warnings.size() != 1) continue;
+ found = true;
+ EXPECT_NE(plan.value().warnings[0].find("roughly 2.5x as many windows"), std::string::npos) << plan.value().warnings[0];
+ }
+ }
+ EXPECT_TRUE(found) << "no budget with a default window of 2.5 times the room";
+}
+
TEST(BatchCap, MatchesPythonGrid) {
LAYA_GOLDEN_LIST(grid, "window_plan.json", "batch_cap");
LAYA_EXPECT_ROWS(grid, 810);