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.

laya-go samples: review fixes

Adeshina Lasisi8bc753a90726
diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml
index ad419bb4..520d9ec5 100644
--- a/.github/workflows/go.yml
+++ b/.github/workflows/go.yml
@@ -134,13 +134,22 @@ jobs:
if: ${{ !cancelled() }}
run: python laya-go/scripts/check_test_json.py "$RUNNER_TEMP/go-test.json" --min-tests 1603
- # go.mod declares go 1.22, so that is what a consumer may build with.
# The samples are a nested module (they link the cgo onnx module), so the root steps do not
- # see them. They must keep building against the API, and build without cgo too.
+ # see them. They must keep building against the API.
+ - name: The samples' email questions are what Python records now
+ run: python laya-go/samples/scripts/gen_email_golden.py --check
- name: The samples build and their tests pass
working-directory: laya-go/samples
- run: go vet ./... && go build ./... && go test -count=1 ./... && CGO_ENABLED=0 go build ./...
+ run: go vet ./... && go build ./... && go test -count=1 -json ./... > "$RUNNER_TEMP/go-samples.json"
+ - name: No samples test was skipped
+ if: ${{ !cancelled() }}
+ run: python laya-go/scripts/check_test_json.py "$RUNNER_TEMP/go-samples.json" --min-tests 2
+ # Samples only: it does not guard the root module, whose own steps build it without cgo.
+ - name: The samples build without cgo
+ working-directory: laya-go/samples
+ run: CGO_ENABLED=0 go build ./...
+ # go.mod declares go 1.22, so that is what a consumer may build with.
- name: Run the tests on the oldest supported Go
working-directory: laya-go
env:
@@ -302,6 +311,11 @@ jobs:
run: |
go install "honnef.co/go/tools/cmd/staticcheck@$STATICCHECK_VERSION"
"$(go env GOPATH)/bin/staticcheck" ./...
+ - name: samples staticcheck
+ working-directory: laya-go/samples
+ run: |
+ go install "honnef.co/go/tools/cmd/staticcheck@$STATICCHECK_VERSION"
+ "$(go env GOPATH)/bin/staticcheck" ./...
- name: onnx builds and tests without cgo
working-directory: laya-go/onnx
run: CGO_ENABLED=0 go build ./... && CGO_ENABLED=0 go test ./...
@@ -335,3 +349,9 @@ jobs:
run: |
go install "golang.org/x/vuln/cmd/govulncheck@$GOVULNCHECK_VERSION"
"$(go env GOPATH)/bin/govulncheck" ./...
+
+ - name: govulncheck (samples module)
+ working-directory: laya-go/samples
+ run: |
+ go install "golang.org/x/vuln/cmd/govulncheck@$GOVULNCHECK_VERSION"
+ "$(go env GOPATH)/bin/govulncheck" ./...
diff --git a/laya-go/samples/benchmark/main.go b/laya-go/samples/benchmark/main.go
index d99aa764..01e069e1 100644
--- a/laya-go/samples/benchmark/main.go
+++ b/laya-go/samples/benchmark/main.go
@@ -39,10 +39,18 @@ func run(args []string) error {
long := strings.Repeat(short, 90) // about 3.8 KB
fmt.Println("model-free (no checkpoint, no graph)")
- cold := once(func() error { lang.Analyze(short); return nil })
+ cold, err := once(func() error { lang.Analyze(short); return nil })
+ if err != nil {
+ return err
+ }
fmt.Printf(" %-34s %8.3f ms <- cold\n", "detect, first ever call", ms(cold))
- report("detect, 42-char state", must(best(warmup, rounds, func() error { lang.Analyze(short); return nil })))
- report("detect, 3.8 KB state", must(best(warmup, rounds, func() error { lang.Analyze(long); return nil })))
+ for _, c := range []struct{ label, text string }{{"detect, 42-char state", short}, {"detect, 3.8 KB state", long}} {
+ d, err := best(warmup, rounds, func() error { lang.Analyze(c.text); return nil })
+ if err != nil {
+ return err
+ }
+ report(c.label, d)
+ }
if len(args) < 2 {
fmt.Println("\npass <checkpoint-dir> <graph> to also measure the forward pass")
@@ -81,7 +89,10 @@ func run(args []string) error {
longState := demo.State("Charged twice", strings.Repeat("I was billed two times this morning. ", 60))
fmt.Printf("\nforward pass (%d questions per state)\n", len(qs.IDs()))
- c := once(func() error { _, err := a.Predict(ctx, state, qs); return err })
+ c, err := once(func() error { _, err := a.Predict(ctx, state, qs); return err })
+ if err != nil {
+ return err
+ }
fmt.Printf(" %-34s %8.3f ms <- cold, includes the first run of the session\n", "Predict, first ever call", ms(c))
one, err := best(modelWarmup, modelRounds, func() error { _, err := a.Predict(ctx, state, qs); return err })
if err != nil {
@@ -113,20 +124,13 @@ func ms(d time.Duration) float64 { return float64(d) / float64(time.Millisecond)
func report(label string, d time.Duration) { fmt.Printf(" %-34s %8.3f ms\n", label, ms(d)) }
-func must(d time.Duration, err error) time.Duration {
- if err != nil {
- panic(err)
- }
- return d
-}
-
// once times a single call.
-func once(f func() error) time.Duration {
+func once(f func() error) (time.Duration, error) {
start := time.Now()
if err := f(); err != nil {
- panic(err)
+ return 0, err
}
- return time.Since(start)
+ return time.Since(start), nil
}
// best is the fastest of n timed calls after warm untimed ones.
diff --git a/laya-go/samples/internal/demo/demo_test.go b/laya-go/samples/internal/demo/demo_test.go
index 69064b35..75846883 100644
--- a/laya-go/samples/internal/demo/demo_test.go
+++ b/laya-go/samples/internal/demo/demo_test.go
@@ -1,31 +1,58 @@
package demo
import (
+ "encoding/json"
+ "os"
"reflect"
"testing"
"github.com/NandhaKishorM/laya/laya-go/sequence"
)
-// The expected text is json.dumps(state, ensure_ascii=False) from Python.
+// Expected texts are json.dumps(state, ensure_ascii=False) from Python.
func TestStateSerializesInPythonDictOrder(t *testing.T) {
- got, err := sequence.SerializeState(State("Charged twice for order 8812", "I was billed two times this morning."))
- if err != nil {
- t.Fatal(err)
- }
- const want = `{"subject": "Charged twice for order 8812", "body": "I was billed two times this morning."}`
- if got != want {
- t.Fatalf("state = %s, want %s", got, want)
+ for _, c := range []struct{ subject, body, want string }{
+ {"Charged twice for order 8812", "I was billed two times this morning.",
+ `{"subject": "Charged twice for order 8812", "body": "I was billed two times this morning."}`},
+ {"ऑर्डर 8812 का दो बार बिल", "कृपया रिफंड करें।",
+ `{"subject": "ऑर्डर 8812 का दो बार बिल", "body": "कृपया रिफंड करें।"}`},
+ } {
+ got, err := sequence.SerializeState(State(c.subject, c.body))
+ if err != nil {
+ t.Fatal(err)
+ }
+ if got != c.want {
+ t.Errorf("state = %s, want %s", got, c.want)
+ }
}
}
+// The golden is laya.presets.email_questions() minus is_phishing, recorded by
+// scripts/gen_email_golden.py. Every field of every question is compared, in order.
func TestQuestionsAreTheEmailPreset(t *testing.T) {
+ golden, err := os.ReadFile("testdata/email_questions.json")
+ if err != nil {
+ t.Fatal(err)
+ }
qs, err := Questions()
if err != nil {
t.Fatal(err)
}
- want := []string{"category", "is_spam", "urgency", "needs_reply"}
- if got := qs.IDs(); !reflect.DeepEqual(got, want) {
- t.Fatalf("ids = %v, want %v", got, want)
+ got, err := json.Marshal(qs)
+ if err != nil {
+ t.Fatal(err)
+ }
+ var g, w any
+ if err := json.Unmarshal(got, &g); err != nil {
+ t.Fatal(err)
+ }
+ if err := json.Unmarshal(golden, &w); err != nil {
+ t.Fatal(err)
+ }
+ if !reflect.DeepEqual(g, w) {
+ t.Fatalf("questions differ from the Python preset:\n got %s\nwant %s", got, golden)
+ }
+ if ids := qs.IDs(); !reflect.DeepEqual(ids, []string{"category", "is_spam", "urgency", "needs_reply"}) {
+ t.Fatalf("ids = %v", ids)
}
}
diff --git a/laya-go/samples/internal/demo/testdata/email_questions.json b/laya-go/samples/internal/demo/testdata/email_questions.json
new file mode 100644
index 00000000..d9127b10
--- /dev/null
+++ b/laya-go/samples/internal/demo/testdata/email_questions.json
@@ -0,0 +1,31 @@
+{
+ "category": {
+ "type": "choice",
+ "instructions": "Which team should handle the email in `body`?",
+ "criteria": {
+ "billing": "invoices, payments, refunds",
+ "technical": "bugs, outages, integrations",
+ "sales": "pricing, demos, new purchases",
+ "security": "phishing, scams, account compromise",
+ "hr": "hiring, leave, payroll",
+ "other": "none of the above"
+ }
+ },
+ "is_spam": {
+ "type": "noul",
+ "instructions": "Is this email unsolicited spam or bulk marketing?"
+ },
+ "urgency": {
+ "type": "score",
+ "instructions": "How urgent is the request in `body`?",
+ "criteria": [
+ "no time pressure",
+ "needs attention soon",
+ "blocking issue or hard deadline"
+ ]
+ },
+ "needs_reply": {
+ "type": "noul",
+ "instructions": "Does the sender expect a reply?"
+ }
+}
diff --git a/laya-go/samples/scripts/gen_email_golden.py b/laya-go/samples/scripts/gen_email_golden.py
new file mode 100644
index 00000000..06768760
--- /dev/null
+++ b/laya-go/samples/scripts/gen_email_golden.py
@@ -0,0 +1,40 @@
+#!/usr/bin/env python3
+"""Record internal/demo/testdata/email_questions.json: laya.presets.email_questions() minus
+is_phishing, as json.dumps(ensure_ascii=False) writes it.
+
+ python laya-go/samples/scripts/gen_email_golden.py # write
+ python laya-go/samples/scripts/gen_email_golden.py --check # fail if the file differs
+"""
+import argparse
+import json
+import pathlib
+import sys
+
+ROOT = pathlib.Path(__file__).resolve().parents[3]
+sys.path.insert(0, str(ROOT))
+from laya.presets import email_questions # noqa: E402
+
+OUT = pathlib.Path(__file__).resolve().parents[1] / "internal" / "demo" / "testdata" / "email_questions.json"
+
+
+def render():
+ questions = email_questions()
+ del questions["is_phishing"]
+ return json.dumps(questions, ensure_ascii=False, indent=1) + "\n"
+
+
+def main():
+ parser = argparse.ArgumentParser(description=__doc__.splitlines()[0])
+ parser.add_argument("--check", action="store_true")
+ args = parser.parse_args()
+ text = render()
+ if args.check:
+ if not OUT.exists() or OUT.read_text(encoding="utf-8") != text:
+ sys.exit("%s differs from what Python records now; rerun without --check" % OUT)
+ print("email_questions.json matches")
+ return
+ OUT.write_text(text, encoding="utf-8")
+
+
+if __name__ == "__main__":
+ main()