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: PredictLong review fixes
Adeshina Lasisiba5cfd209a04
diff --git a/.github/workflows/go.yml b/.github/workflows/go.ymlindex 4f6b3084..6fef89d1 100644--- a/.github/workflows/go.yml+++ b/.github/workflows/go.yml@@ -128,11 +128,11 @@ jobs:LAYA_GO_FIXTURES: ${{ github.workspace }}/.go-goldensLAYA_CHECKPOINTS: ${{ github.workspace }}/laya-java/.work/checkpointsrun: go test -race -count=1 -json ./... > "$RUNNER_TEMP/go-test.json"- # Exactly the measured count (1409 tests and subtests, on both Go versions): dropping a+ # Exactly the measured count (1412 tests and subtests, on both Go versions): dropping a# case fails here, and adding one means raising this number.- name: No test was skippedif: ${{ !cancelled() }}- run: python laya-go/scripts/check_test_json.py "$RUNNER_TEMP/go-test.json" --min-tests 1409+ run: python laya-go/scripts/check_test_json.py "$RUNNER_TEMP/go-test.json" --min-tests 1412# go.mod declares go 1.22, so that is what a consumer may build with.- name: Run the tests on the oldest supported Go@@ -144,7 +144,7 @@ jobs:run: go test -count=1 -json ./... > "$RUNNER_TEMP/go-test-oldest.json"- name: No test was skipped on the oldest Go eitherif: ${{ !cancelled() }}- run: python laya-go/scripts/check_test_json.py "$RUNNER_TEMP/go-test-oldest.json" --min-tests 1409+ run: python laya-go/scripts/check_test_json.py "$RUNNER_TEMP/go-test-oldest.json" --min-tests 1412- uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2if: ${{ failure() }}diff --git a/laya-go/agent/long.go b/laya-go/agent/long.goindex ceb1232c..78556cc4 100644--- a/laya-go/agent/long.go+++ b/laya-go/agent/long.go@@ -47,8 +47,8 @@ type LongPrediction struct {// WithWindow, WithStride, WithBatchSize (windows per run; by default the scan bounds a run as// laya's window_batch_cap does) and WithLanguage apply. A *StateError names a window, not the// state. Errors are those of PredictBatch, and sequence.ErrNoRoom and sequence.ErrStrideGap when-// the questions leave no room or the stride leaves gaps. The confidence gate, hooks and-// calibration are not applied.+// the questions leave no room or the stride leaves gaps. The confidence gate and hooks are not+// applied, and Go has no calibration.func (a *Agent) PredictLong(ctx context.Context, state any, qs schema.Questions, opts ...CallOption) (LongPrediction, error) {var c callfor _, opt := range opts {diff --git a/laya-go/agent/long_test.go b/laya-go/agent/long_test.goindex 05c1b5db..d7be8e27 100644--- a/laya-go/agent/long_test.go+++ b/laya-go/agent/long_test.go@@ -4,9 +4,11 @@ import ("context""encoding/json""errors"+ "fmt""os""path/filepath""reflect"+ "slices""strings""testing"@@ -143,6 +145,9 @@ func TestPredictLongWithoutQuestions(t *testing.T) {if err != nil {t.Fatal(err)}+ if js, _ := json.Marshal(got.Usage); string(js) != fmt.Sprintf(`{"input_tokens":0,"output_tokens":0,"windows":%d}`, got.Usage.Windows) {+ t.Errorf("usage = %s", js)+ }if len(got.Answers) != 0 || got.Usage.Windows < 3 || len(s.seen()) != 0 {t.Errorf("answers = %v, windows = %d, runs = %d", got.Answers, got.Usage.Windows, len(s.seen()))}@@ -245,7 +250,7 @@ func TestPredictLongGoldenCases(t *testing.T) {}}want := fixtures.Strings(t, rec.Must("warnings"))- if len(want) != 2*len(got.Warnings) {+ if !slices.Equal(want, append(slices.Clone(got.Warnings), got.Warnings...)) {t.Errorf("warnings = %q, recorded %q", got.Warnings, want)}})diff --git a/laya-go/onnx/predictlong_test.go b/laya-go/onnx/predictlong_test.goindex 141ca7c4..2a21fbd1 100644--- a/laya-go/onnx/predictlong_test.go+++ b/laya-go/onnx/predictlong_test.go@@ -6,6 +6,7 @@ import ("context""encoding/json""fmt"+ "math""os""strings""testing"@@ -107,6 +108,13 @@ func TestPredictLongThroughTheGraph(t *testing.T) {}}w := an.Answer.Base().Window+ if topGap(scored, r) <= 2e-4 {+ // batch size changes the floats a little, so a near tie may go either way+ if d := deciding(an.Answer) - bestKey; w == nil || d > 2e-4 || d < -2e-4 {+ t.Errorf("%s: window = %+v, deciding value off by %.2e", an.ID, w, d)+ }+ continue+ }if w == nil || w.Index != best || w.Count != len(texts) || w.TokenStart != starts[best] || w.TokenEnd != min(starts[best]+plan.Window, len(st.IDs)) {t.Errorf("%s: window = %+v, want index %d of %d", an.ID, w, best, len(texts))continue@@ -123,7 +131,7 @@ func TestPredictLongThroughTheGraph(t *testing.T) {}for r, an := range long.Answers {other := capped.Answers[r].Answer- if an.Answer.Base().Window.Index != other.Base().Window.Index {+ if an.Answer.Base().Window.Index != other.Base().Window.Index && topGap(scored, r) > 2e-4 {t.Errorf("%s: a window cap moved the deciding window %d -> %d", an.ID, an.Answer.Base().Window.Index, other.Base().Window.Index)}if d := deciding(an.Answer) - deciding(other); d > 2e-4 || d < -2e-4 {@@ -132,6 +140,20 @@ func TestPredictLongThroughTheGraph(t *testing.T) {}}+// topGap is the gap between the two highest deciding values of question r across the windows.+func topGap(scored []schema.Prediction, r int) float64 {+ hi, second := math.Inf(-1), math.Inf(-1)+ for j := range scored {+ switch k := deciding(scored[j].Answers[r].Answer); {+ case k > hi:+ hi, second = k, hi+ case k > second:+ second = k+ }+ }+ return hi - second+}+func deciding(a schema.Answer) float64 {if n, ok := a.(*schema.NoulAnswer); ok {return n.Nouldiff --git a/laya-go/schema/answer.go b/laya-go/schema/answer.goindex 4b2c77ce..da5cd2b5 100644--- a/laya-go/schema/answer.go+++ b/laya-go/schema/answer.go@@ -398,6 +398,9 @@ func windowFrom(v any) (*Window, error) {}*dst = int(f)}+ if w.Count < 1 || w.Index < 0 || w.Index >= w.Count || w.TokenStart < 0 || w.TokenEnd < w.TokenStart {+ return nil, errors.New(`schema: answer "window" is out of range`)+ }return w, nil}diff --git a/laya-go/schema/prediction.go b/laya-go/schema/prediction.goindex d2e4621a..7aca2c02 100644--- a/laya-go/schema/prediction.go+++ b/laya-go/schema/prediction.go@@ -3,6 +3,7 @@ package schemaimport ("bytes""encoding/json"+ "errors""fmt""reflect""strconv"@@ -137,6 +138,12 @@ func (u Usage) scanned() bool { return u.Windows > 1 }// MarshalJSON writes laya's usage dict by hand, so the bytes are Python's: truncated_questions is// always a list, never null, and the ids are not HTML-escaped.func (u Usage) MarshalJSON() ([]byte, error) {+ if u.scanned() && u.StateTokens == 0 && u.StateTokensDropped == 0 && u.TruncatedWindows == 0 &&+ len(u.TruncatedQuestions) == 0 && len(u.CollapsedOptions) == 0 {+ // laya's usage for a scan with no questions: no state was read, so only these keys.+ return []byte(fmt.Sprintf(`{"input_tokens":%d,"output_tokens":%d,"windows":%d}`,+ u.InputTokens, u.OutputTokens, u.Windows)), nil+ }var b strings.Buildertruncated := strconv.FormatBool(u.Truncated)if u.scanned() {@@ -183,12 +190,18 @@ func (u *Usage) UnmarshalJSON(data []byte) error {switch raw := strings.TrimSpace(string(aux.Truncated)); raw {case "", "null":case "true", "false":+ if u.scanned() {+ return errors.New(`schema: usage "truncated" of a scan of several windows must be a count`)+ }u.Truncated = raw == "true"default:n, err := strconv.Atoi(raw)if err != nil || n < 0 {return fmt.Errorf("schema: usage \"truncated\" must be a flag or a window count, got %s", raw)}+ if !u.scanned() {+ return errors.New(`schema: usage "truncated" is a count only for a scan of several windows`)+ }u.TruncatedWindows, u.Truncated = n, n > 0}return nildiff --git a/laya-go/schema/schema_test.go b/laya-go/schema/schema_test.goindex c0a8c7f4..56909681 100644--- a/laya-go/schema/schema_test.go+++ b/laya-go/schema/schema_test.go@@ -639,3 +639,55 @@ func TestScanUsageAndWindowJSON(t *testing.T) {}}}++func TestUsageTruncatedMatchesWindows(t *testing.T) {+ for _, bad := range []string{`{"truncated":true,"windows":3}`, `{"truncated":2}`, `{"truncated":2,"windows":1}`} {+ var u schema.Usage+ if err := json.Unmarshal([]byte(bad), &u); err == nil {+ t.Errorf("%s accepted", bad)+ }+ }+ for _, ok := range []string{`{"truncated":2,"windows":3}`, `{"truncated":true,"windows":1}`, `{"input_tokens":0,"output_tokens":0,"windows":3}`} {+ var u schema.Usage+ if err := json.Unmarshal([]byte(ok), &u); err != nil {+ t.Errorf("%s: %v", ok, err)+ }+ }+}++func TestUsageOfScanWithoutQuestionsIsBare(t *testing.T) {+ const want = `{"input_tokens":0,"output_tokens":0,"windows":3}`+ b, err := json.Marshal(schema.Usage{TruncatedQuestions: []string{}, Windows: 3})+ if err != nil || string(b) != want {+ t.Fatalf("got %s, %v", b, err)+ }+ var u schema.Usage+ if err := json.Unmarshal(b, &u); err != nil {+ t.Fatal(err)+ }+ if again, _ := json.Marshal(u); string(again) != want {+ t.Errorf("round trip %s", again)+ }+ full, _ := json.Marshal(schema.Usage{StateTokens: 1, TruncatedQuestions: []string{}, Windows: 3})+ if !strings.Contains(string(full), `"truncated":0`) {+ t.Errorf("a scan that read state lost its keys: %s", full)+ }+}++func TestWindowRange(t *testing.T) {+ const base = `{"type":"noul","noul":0.5,"confidence":0.5,"answer_confidence":0.5,"action":{"act_probability":0.0},"window":`+ for _, bad := range []string{+ `{"index":3,"token_start":0,"token_end":1,"count":3}`,+ `{"index":-1,"token_start":0,"token_end":1,"count":3}`,+ `{"index":0,"token_start":-1,"token_end":1,"count":3}`,+ `{"index":0,"token_start":5,"token_end":4,"count":3}`,+ `{"index":0,"token_start":0,"token_end":1,"count":0}`,+ } {+ if _, err := schema.UnmarshalAnswer([]byte(base + bad + `}`)); err == nil {+ t.Errorf("%s accepted", bad)+ }+ }+ if _, err := schema.UnmarshalAnswer([]byte(base + `{"index":2,"token_start":4,"token_end":4,"count":3}}`)); err != nil {+ t.Error(err)+ }+}