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.
Python calibrationclassificationdecision-modelhuggingfacejevmodernbertmultilingualnlppythonpytorchroutingtyped-decisionszero-shot
laya-go: runnable quickstart and benchmark samples
Adeshina Lasisia72798d0a603
diff --git a/.github/workflows/go.yml b/.github/workflows/go.ymlindex 093a8071..ad419bb4 100644--- a/.github/workflows/go.yml+++ b/.github/workflows/go.yml@@ -135,6 +135,12 @@ jobs: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.+ - 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 ./...+- name: Run the tests on the oldest supported Goworking-directory: laya-goenv:diff --git a/laya-go/samples/benchmark/main.go b/laya-go/samples/benchmark/main.gonew file mode 100644index 00000000..d99aa764--- /dev/null+++ b/laya-go/samples/benchmark/main.go@@ -0,0 +1,150 @@+// Benchmark measures what the SDK costs the only way a Go measurement means anything: warm, best+// of N, with the cold first call reported on its own instead of averaged into the rest.+//+// go run ./benchmark # model-free half only+// go run ./benchmark <checkpoint-dir> <graph> [lib] # also the forward pass+package main++import (+ "context"+ "fmt"+ "os"+ "path/filepath"+ "strings"+ "time"++ "github.com/NandhaKishorM/laya/laya-go/agent"+ "github.com/NandhaKishorM/laya/laya-go/lang"+ "github.com/NandhaKishorM/laya/laya-go/onnx"+ "github.com/NandhaKishorM/laya/laya-go/samples/internal/demo"+)++const (+ warmup = 20 // model-free calls are microseconds+ rounds = 5+ modelWarmup = 1 // a forward pass is tens of milliseconds+ modelRounds = 2+ batchSize = 8+)++func main() {+ if err := run(os.Args[1:]); err != nil {+ fmt.Fprintln(os.Stderr, "benchmark:", err)+ os.Exit(1)+ }+}++func run(args []string) error {+ short := "I cannot log in and need a password reset."+ 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 })+ 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 })))++ if len(args) < 2 {+ fmt.Println("\npass <checkpoint-dir> <graph> to also measure the forward pass")+ return nil+ }+ graph := args[1]+ if fi, err := os.Stat(graph); err == nil && fi.IsDir() {+ graph = filepath.Join(graph, "laya.onnx")+ }+ var opts []onnx.Option+ if len(args) > 2 {+ opts = append(opts, onnx.WithLibrary(args[2]))+ }+ qs, err := demo.Questions()+ if err != nil {+ return err+ }+ sess, err := onnx.Open(graph, opts...)+ if err != nil {+ return err+ }+ a, err := agent.Open(os.DirFS(args[0]), sess)+ if err != nil {+ _ = sess.Close()+ return err+ }+ defer a.Close()++ ctx := context.Background()+ state := demo.State("Charged twice for order 8812",+ "I was billed two times this morning. Please refund one charge.")+ states := make([]any, batchSize)+ for i := range states {+ states[i] = state+ }+ 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 })+ 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 {+ return err+ }+ report("Predict, 1 state", one)+ batch, err := best(modelWarmup, modelRounds, func() error { _, err := a.PredictBatch(ctx, states, qs); return err })+ if err != nil {+ return err+ }+ report(fmt.Sprintf("PredictBatch, %d states", batchSize), batch)+ report(" ... per state", batch/batchSize)+ long2, err := best(0, 1, func() error { _, err := a.PredictLong(ctx, longState, qs); return err })+ if err != nil {+ return err+ }+ report("PredictLong, multi-window state", long2)+ fmt.Println(" (PredictLong is one timed call: every window is a forward pass, and a sample must finish)")++ fmt.Printf("\nModel-free figures are the best of %d after %d warmup calls; model-backed ones the best of\n"++ "%d after %d. The cold figures are single calls. Best, not mean: the mean is a mean over how\n"++ "much other work the machine was doing. On CPU the per-state batch figure is expected to be close\n"++ "to the single-state one: the matmuls dominate and do not amortize.\n",+ rounds, warmup, modelRounds, modelWarmup)+ return nil+}++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 {+ start := time.Now()+ if err := f(); err != nil {+ panic(err)+ }+ return time.Since(start)+}++// best is the fastest of n timed calls after warm untimed ones.+func best(warm, n int, f func() error) (time.Duration, error) {+ for i := 0; i < warm; i++ {+ if err := f(); err != nil {+ return 0, err+ }+ }+ b := time.Duration(1<<63 - 1)+ for i := 0; i < n; i++ {+ start := time.Now()+ if err := f(); err != nil {+ return 0, err+ }+ if d := time.Since(start); d < b {+ b = d+ }+ }+ return b, nil+}diff --git a/laya-go/samples/go.mod b/laya-go/samples/go.modnew file mode 100644index 00000000..59b78b66--- /dev/null+++ b/laya-go/samples/go.mod@@ -0,0 +1,15 @@+module github.com/NandhaKishorM/laya/laya-go/samples++go 1.22++require (+ github.com/NandhaKishorM/laya/laya-go v0.0.0-00010101000000-000000000000+ github.com/NandhaKishorM/laya/laya-go/onnx v0.0.0-00010101000000-000000000000+)++require github.com/yalue/onnxruntime_go v1.36.0 // indirect++replace (+ github.com/NandhaKishorM/laya/laya-go => ../+ github.com/NandhaKishorM/laya/laya-go/onnx => ../onnx+)diff --git a/laya-go/samples/go.sum b/laya-go/samples/go.sumnew file mode 100644index 00000000..0b8081bf--- /dev/null+++ b/laya-go/samples/go.sum@@ -0,0 +1,2 @@+github.com/yalue/onnxruntime_go v1.36.0 h1:iH1Q++DcsyT9sWtN26KYimESlI5hhXpKaChHDS44oV4=+github.com/yalue/onnxruntime_go v1.36.0/go.mod h1:b4X26A8pekNb1ACJ58wAXgNKeUCGEAQ9dmACut9Sm/4=diff --git a/laya-go/samples/internal/demo/demo.go b/laya-go/samples/internal/demo/demo.gonew file mode 100644index 00000000..4052854d--- /dev/null+++ b/laya-go/samples/internal/demo/demo.go@@ -0,0 +1,28 @@+// Package demo holds what the samples share: the email questions and the example states.+package demo++import (+ "github.com/NandhaKishorM/laya/laya-go/lang"+ "github.com/NandhaKishorM/laya/laya-go/schema"+)++// emailQuestions is laya's email preset in its wire form.+const emailQuestions = `{+ "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?"}+}`++// Questions returns the four email questions.+func Questions() (schema.Questions, error) { return schema.ParseQuestions([]byte(emailQuestions)) }++// State is a support email. A lang.Object, not a map: a Go map is written in sorted key order+// (body before subject), which is not the order a Python dict gives the model.+func State(subject, body string) lang.Object {+ return lang.Object{{Key: "subject", Value: subject}, {Key: "body", Value: body}}+}diff --git a/laya-go/samples/internal/demo/demo_test.go b/laya-go/samples/internal/demo/demo_test.gonew file mode 100644index 00000000..69064b35--- /dev/null+++ b/laya-go/samples/internal/demo/demo_test.go@@ -0,0 +1,31 @@+package demo++import (+ "reflect"+ "testing"++ "github.com/NandhaKishorM/laya/laya-go/sequence"+)++// The expected text is 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)+ }+}++func TestQuestionsAreTheEmailPreset(t *testing.T) {+ 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)+ }+}diff --git a/laya-go/samples/quickstart/main.go b/laya-go/samples/quickstart/main.gonew file mode 100644index 00000000..7c839b63--- /dev/null+++ b/laya-go/samples/quickstart/main.go@@ -0,0 +1,91 @@+// Quickstart asks four typed questions about one support email, in English and Hindi, and prints+// each answer by its type.+//+// go run ./quickstart <checkpoint-dir> <graph-dir-or-laya.onnx> [onnxruntime-library]+package main++import (+ "context"+ "fmt"+ "os"+ "path/filepath"++ "github.com/NandhaKishorM/laya/laya-go/agent"+ "github.com/NandhaKishorM/laya/laya-go/lang"+ "github.com/NandhaKishorM/laya/laya-go/onnx"+ "github.com/NandhaKishorM/laya/laya-go/samples/internal/demo"+ "github.com/NandhaKishorM/laya/laya-go/schema"+)++func main() {+ if err := run(os.Args[1:]); err != nil {+ fmt.Fprintln(os.Stderr, "quickstart:", err)+ os.Exit(1)+ }+}++func run(args []string) error {+ if len(args) < 2 {+ return fmt.Errorf("usage: quickstart <checkpoint-dir> <graph-dir-or-laya.onnx> [onnxruntime-library]")+ }+ graph := args[1]+ if fi, err := os.Stat(graph); err == nil && fi.IsDir() {+ graph = filepath.Join(graph, "laya.onnx")+ }+ var opts []onnx.Option+ if len(args) > 2 {+ opts = append(opts, onnx.WithLibrary(args[2]))+ }+ qs, err := demo.Questions()+ if err != nil {+ return err+ }+ sess, err := onnx.Open(graph, opts...)+ if err != nil {+ return err+ }+ // The agent owns the session; closing the agent closes it.+ a, err := agent.Open(os.DirFS(args[0]), sess)+ if err != nil {+ _ = sess.Close()+ return err+ }+ defer a.Close()++ ctx := context.Background()+ states := []struct {+ label string+ state lang.Object+ }{+ {"English", demo.State("Charged twice for order 8812",+ "I was billed two times for the same order this morning. Please refund one of the charges. I have attached both receipts.")},+ {"Hindi", demo.State("ऑर्डर 8812 का दो बार बिल",+ "मुझे उसी ऑर्डर के लिए दो बार बिल किया गया। कृपया रिफंड करें।")},+ }+ for _, s := range states {+ pred, err := a.Predict(ctx, s.state, qs)+ if err != nil {+ return fmt.Errorf("%s: %w", s.label, err)+ }+ report(s.label, pred)+ }+ return nil+}++// report prints every answer by its concrete type: a choice is not a score.+func report(label string, p schema.Prediction) {+ fmt.Printf("\n== %s ==\n", label)+ for _, ans := range p.Answers {+ switch v := ans.Answer.(type) {+ case *schema.ChoiceAnswer:+ fmt.Printf(" %-12s %-22s confidence %.3f\n", ans.ID, v.Choice, v.Confidence)+ case *schema.ScoreAnswer:+ fmt.Printf(" %-12s %-22.3f confidence %.3f\n", ans.ID, v.Score, v.Confidence)+ case *schema.NoulAnswer:+ fmt.Printf(" %-12s %-22.3f confidence %.3f\n", ans.ID, v.Noul, v.Confidence)+ }+ }+ // Usage says whether the model saw the whole state.+ fmt.Printf(" usage %d input tokens, %d state tokens dropped, truncated=%t\n",+ p.Usage.InputTokens, p.Usage.StateTokensDropped, p.Usage.Truncated)+}