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(go): close the gaps two more reviews found in the predict path and the onnx module

Adeshina Lasisi0dd57c55046e
diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml
index 4b24327a..2e1574c9 100644
--- a/.github/workflows/go.yml
+++ b/.github/workflows/go.yml
@@ -127,11 +127,11 @@ jobs:
LAYA_GO_FIXTURES: ${{ github.workspace }}/.go-goldens
LAYA_CHECKPOINTS: ${{ github.workspace }}/laya-java/.work/checkpoints
run: go test -race -count=1 -json ./... > "$RUNNER_TEMP/go-test.json"
- # Exactly the measured count (1321 tests and subtests, on both Go versions): dropping a
+ # Exactly the measured count (1329 tests and subtests, on both Go versions): dropping a
# case fails here, and adding one means raising this number.
- name: No test was skipped
if: ${{ !cancelled() }}
- run: python laya-go/scripts/check_test_json.py "$RUNNER_TEMP/go-test.json" --min-tests 1321
+ run: python laya-go/scripts/check_test_json.py "$RUNNER_TEMP/go-test.json" --min-tests 1329
# go.mod declares go 1.22, so that is what a consumer may build with.
- name: Run the tests on the oldest supported Go
@@ -143,7 +143,7 @@ jobs:
run: go test -count=1 -json ./... > "$RUNNER_TEMP/go-test-oldest.json"
- name: No test was skipped on the oldest Go either
if: ${{ !cancelled() }}
- run: python laya-go/scripts/check_test_json.py "$RUNNER_TEMP/go-test-oldest.json" --min-tests 1321
+ run: python laya-go/scripts/check_test_json.py "$RUNNER_TEMP/go-test-oldest.json" --min-tests 1329
- uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2
if: ${{ failure() }}
diff --git a/laya-go/agent/agent.go b/laya-go/agent/agent.go
index 0512528a..0c761813 100644
--- a/laya-go/agent/agent.go
+++ b/laya-go/agent/agent.go
@@ -34,7 +34,14 @@ func (e *StateError) Error() string {
return fmt.Sprintf("agent: state %d", e.Index)
}
// The cause is a complete message ("agent: state is nil; ..."); keep one prefix and one subject.
- msg := strings.TrimPrefix(strings.TrimPrefix(e.Err.Error(), "agent: "), "state ")
+ msg := e.Err.Error()
+ if rest, ok := strings.CutPrefix(msg, "agent: state "); ok {
+ if _, nested := e.Err.(*StateError); !nested {
+ msg = rest
+ }
+ } else {
+ msg = strings.TrimPrefix(msg, "agent: ")
+ }
return fmt.Sprintf("agent: state %d: %s", e.Index, msg)
}
@@ -124,9 +131,16 @@ func (a *Agent) Config() Config {
// Close marks the agent closed, waits for calls in flight and closes the session. Close blocks
// until calls in flight return; cancel their contexts to bound it. A call that has not yet
// entered returns ErrClosed without waiting. Close is idempotent: a second call, concurrent or
-// not, waits for the first and returns the same error.
+// not, waits for the first and returns the same error. Close must not be called from inside a
+// call (from the session's Run): it would wait for that call, which waits for Close.
func (a *Agent) Close() error {
a.once.Do(func() {
+ defer func() {
+ if r := recover(); r != nil { // once is spent either way: later calls report this
+ a.closeErr = fmt.Errorf("agent: the session's Close panicked: %v", r)
+ panic(r)
+ }
+ }()
a.mu.Lock()
a.shut = true
a.closed.Store(true)
@@ -196,7 +210,7 @@ func (a *Agent) Predict(ctx context.Context, state any, qs schema.Questions, opt
// run.
//
// Errors may be wrapped; match them with errors.Is and errors.As:
-// - ErrClosed, once Close has been called;
+// - ErrClosed, once Close has been called (a context already done is reported first);
// - ctx.Err(), checked on entry, before each state is encoded and between runs;
// - *StateError, naming the state, around sequence.ErrUnsupportedState (also for a nil
// state), sequence.ErrMissingSpecialToken or a *sequence.MarkersError with its ID set;
diff --git a/laya-go/agent/lifecycle_test.go b/laya-go/agent/lifecycle_test.go
index fc63f7e9..12b1caf8 100644
--- a/laya-go/agent/lifecycle_test.go
+++ b/laya-go/agent/lifecycle_test.go
@@ -284,3 +284,43 @@ func TestStateErrorWithoutACause(t *testing.T) {
t.Errorf("got %q", got)
}
}
+
+func TestZeroAgentRefusesCallsBeforeClose(t *testing.T) {
+ var a agent.Agent
+ if _, err := a.Predict(context.Background(), "x", questions(t)); !errors.Is(err, agent.ErrClosed) {
+ t.Errorf("Predict on the zero Agent = %v, want ErrClosed", err)
+ }
+}
+
+type panickyClose struct{ *fakeSession }
+
+func (panickyClose) Close() error { panic("boom") }
+
+func TestPanickingSessionCloseIsReportedAgain(t *testing.T) {
+ a, err := agent.Open(toyCheckpoint(`{}`), panickyClose{&fakeSession{}})
+ if err != nil {
+ t.Fatal(err)
+ }
+ func() {
+ defer func() { recover() }()
+ a.Close()
+ }()
+ if err := a.Close(); err == nil || !strings.Contains(err.Error(), "panicked: boom") {
+ t.Errorf("second Close = %v, want the panic reported", err)
+ }
+}
+
+func TestStateErrorTexts(t *testing.T) {
+ for _, c := range []struct {
+ err *agent.StateError
+ want string
+ }{
+ {&agent.StateError{Index: 0, Err: errors.New("state machine jammed")}, "agent: state 0: state machine jammed"},
+ {&agent.StateError{Index: 1, Err: &agent.StateError{Index: 3, Err: errors.New("x")}}, "agent: state 1: agent: state 3: x"},
+ {&agent.StateError{Index: 2, Err: errors.New("agent: state is nil")}, "agent: state 2: is nil"},
+ } {
+ if got := c.err.Error(); got != c.want {
+ t.Errorf("got %q, want %q", got, c.want)
+ }
+ }
+}
diff --git a/laya-go/infer/infer.go b/laya-go/infer/infer.go
index 8dcec64a..96ac683e 100644
--- a/laya-go/infer/infer.go
+++ b/laya-go/infer/infer.go
@@ -66,10 +66,13 @@ func (e *ShapeError) Error() string {
// head takes topk(2);
// - Rows*Length and Rows*Markers do not overflow an int (Markers of at least 2 keeps Rows*2 safe too);
// - every slice's length matches Rows, Length and Markers, else a *ShapeError;
-// - every MarkerPos lies in [0, Length).
+// - every MarkerPos lies in [0, Length);
+// - no token id is negative, and every QType is 0, 1 or 2 (the three question types).
//
-// All but the ShapeError are plain errors. Token ids, question types and mask values are not
-// checked: a backend passes them to the graph, which rejects an out-of-range index.
+// All but the ShapeError are plain errors. Token ids at or above the vocabulary size and mask
+// values are not checked: a backend passes them to the graph (a nonzero mask counts as 1). The
+// negative ids and types are refused here because ONNX Runtime wraps them to the last rows of the
+// embedding tables and answers from the wrong rows without an error.
func (b Batch) Validate() error {
if b.Rows < 0 || b.Length < 0 || b.Markers < 0 {
return fmt.Errorf("infer: batch has negative dimensions %d x %d x %d", b.Rows, b.Length, b.Markers)
@@ -104,6 +107,16 @@ func (b Batch) Validate() error {
return fmt.Errorf("infer: marker_pos[%d] is %d, outside [0, %d)", i, p, b.Length)
}
}
+ for i, id := range b.InputIDs {
+ if id < 0 {
+ return fmt.Errorf("infer: input_ids row %d column %d is %d, token ids must not be negative", i/b.Length, i%b.Length, id)
+ }
+ }
+ for i, q := range b.QType {
+ if q < 0 || q > 2 {
+ return fmt.Errorf("infer: qtype row %d is %d, want 0, 1 or 2", i, q)
+ }
+ }
return nil
}
diff --git a/laya-go/infer/infer_test.go b/laya-go/infer/infer_test.go
index 6c19f056..6b37e520 100644
--- a/laya-go/infer/infer_test.go
+++ b/laya-go/infer/infer_test.go
@@ -40,6 +40,10 @@ func TestBatchValidate(t *testing.T) {
{"rows of no tokens", func(b *infer.Batch) { *b = batch(3, 0, 2) }, "other"},
{"marker position below zero", func(b *infer.Batch) { b.MarkerPos[2] = -1 }, "other"},
{"marker position at the length", func(b *infer.Batch) { b.MarkerPos[5] = 4 }, "other"},
+ {"negative token id", func(b *infer.Batch) { b.InputIDs[5] = -1 }, "other"},
+ {"qtype below zero", func(b *infer.Batch) { b.QType[1] = -1 }, "other"},
+ {"qtype above two", func(b *infer.Batch) { b.QType[2] = 3 }, "other"},
+ {"qtype two", func(b *infer.Batch) { b.QType[2] = 2 }, ""},
{"marker position in range", func(b *infer.Batch) { b.MarkerPos[5] = 3 }, ""},
// 2 * 2^62 wraps to the most negative int, which no slice length equals.
{"rows x length overflows", func(b *infer.Batch) { *b = infer.Batch{Rows: 2, Length: math.MaxInt/2 + 1, Markers: 2} }, "overflow"},
@@ -128,6 +132,22 @@ func TestValidateNamesTheEmptyRow(t *testing.T) {
}
}
+// TestValidateNamesTheBadRowAndColumn pins that the error says where the value is.
+func TestValidateNamesTheBadRowAndColumn(t *testing.T) {
+ b := batch(3, 4, 2)
+ b.InputIDs[9] = -1 // row 2, column 1
+ err := b.Validate()
+ if err == nil || !strings.Contains(err.Error(), "row 2 column 1") {
+ t.Fatalf("Validate = %v", err)
+ }
+ b = batch(3, 4, 2)
+ b.QType[1] = -1
+ err = b.Validate()
+ if err == nil || !strings.Contains(err.Error(), "qtype row 1") {
+ t.Fatalf("Validate = %v", err)
+ }
+}
+
// TestNegativeDimensionsAreRefusedWithoutRows pins that the sign check does not depend on Rows.
func TestNegativeDimensionsAreRefusedWithoutRows(t *testing.T) {
for name, b := range map[string]infer.Batch{
diff --git a/laya-go/onnx/doc.go b/laya-go/onnx/doc.go
index d689291c..2e176e21 100644
--- a/laya-go/onnx/doc.go
+++ b/laya-go/onnx/doc.go
@@ -10,6 +10,11 @@
//
// The library is found through WithLibrary, else $LAYA_ONNXRUNTIME_LIBRARY, else the platform's
// default name on the loader's search path. A process holds one ONNX Runtime: sessions share it,
-// and it is unloaded when the last one closes. Built with CGO_ENABLED=0 the package still
-// compiles, and Open returns ErrNoCGO.
+// and it is unloaded when the last one closes. An ONNX Runtime the application initialized itself
+// is adopted instead: the library options are then ignored and it is never unloaded. Only an
+// explicit WithLibrary is checked against a runtime already loaded. Built with CGO_ENABLED=0 the
+// package still compiles, and Open returns ErrNoCGO.
+//
+// Notes on the binding: it wraps Go slices as tensors without copying or pinning them, and
+// on an error path of a run it can leak an output tensor. Neither is mitigated here.
package onnx
diff --git a/laya-go/onnx/errors_test.go b/laya-go/onnx/errors_test.go
new file mode 100644
index 00000000..77058b60
--- /dev/null
+++ b/laya-go/onnx/errors_test.go
@@ -0,0 +1,124 @@
+//go:build cgo
+
+package onnx
+
+import (
+ "context"
+ "errors"
+ "fmt"
+ "os"
+ "path/filepath"
+ "strings"
+ "testing"
+
+ "github.com/NandhaKishorM/laya/laya-go/infer"
+ ort "github.com/yalue/onnxruntime_go"
+)
+
+func TestMissingSidecarIsAGraphError(t *testing.T) {
+ p := need(t)
+ data, err := os.ReadFile(p.graph)
+ if err != nil {
+ t.Fatal(err)
+ }
+ alone := filepath.Join(t.TempDir(), "laya.onnx")
+ if err := os.WriteFile(alone, data, 0o644); err != nil {
+ t.Fatal(err)
+ }
+ before := references()
+ _, err = Open(alone)
+ var ge *GraphError
+ if !errors.As(err, &ge) {
+ t.Fatalf("Open without the sidecar = %v, want a *GraphError", err)
+ }
+ if ge.Path != alone || !strings.Contains(ge.Problem, ".data") || !strings.Contains(ge.Problem, "beside") {
+ t.Errorf("GraphError = %+v", ge)
+ }
+ if references() != before {
+ t.Errorf("a failed Open leaked a runtime reference: %d, was %d", references(), before)
+ }
+}
+
+type recorder struct{ calls []string }
+
+func (r *recorder) SetIntraOpNumThreads(n int) error {
+ r.calls = append(r.calls, fmt.Sprint("intra ", n))
+ return nil
+}
+func (r *recorder) SetInterOpNumThreads(n int) error {
+ r.calls = append(r.calls, fmt.Sprint("inter ", n))
+ return nil
+}
+func (r *recorder) SetExecutionMode(m ort.ExecutionMode) error {
+ r.calls = append(r.calls, "mode "+m.String())
+ return nil
+}
+
+func TestInterOpThreadsSelectParallelMode(t *testing.T) {
+ var r recorder
+ if err := applyThreads(&r, options{inter: 3, intra: 2}); err != nil {
+ t.Fatal(err)
+ }
+ got := strings.Join(r.calls, ",")
+ if !strings.Contains(got, "mode "+ort.ExecutionMode(ort.ExecutionModeParallel).String()) || !strings.Contains(got, "inter 3") || !strings.Contains(got, "intra 2") {
+ t.Fatalf("calls = %s", got)
+ }
+ r = recorder{}
+ if err := applyThreads(&r, options{intra: 2}); err != nil {
+ t.Fatal(err)
+ }
+ if got := strings.Join(r.calls, ","); got != "intra 2" {
+ t.Fatalf("intra only: calls = %s", got)
+ }
+ if err := applyThreads(&r, options{}); err != nil || len(r.calls) != 1 {
+ t.Fatalf("zero options touched the session options: %v %v", err, r.calls)
+ }
+}
+
+func TestOpenWithInterOpThreads(t *testing.T) {
+ p := need(t)
+ closeShared(t)
+ s, err := Open(p.graph, WithInterOpThreads(2), WithIntraOpThreads(2))
+ if err != nil {
+ t.Fatal(err)
+ }
+ defer s.Close()
+ if s.RuntimeVersion() == "" {
+ t.Error("RuntimeVersion is empty")
+ }
+ mustRun(t, s)
+}
+
+func TestRunErrorKeepsORTErrorWhenContextEnded(t *testing.T) {
+ ctx, cancel := context.WithCancel(context.Background())
+ cancel()
+ oe := errors.New("ORT said: bad shape")
+ b := makeBatch(2, 3, 2)
+
+ err := runError(ctx, false, oe, b)
+ if !errors.Is(err, context.Canceled) || !errors.Is(err, oe) || !strings.Contains(err.Error(), "bad shape") {
+ t.Errorf("failed run with ended context = %v, want both errors", err)
+ }
+ err = runError(ctx, true, oe, b)
+ if !errors.Is(err, context.Canceled) || !strings.Contains(err.Error(), "run cancelled") || errors.Is(err, oe) {
+ t.Errorf("terminated run = %v, want the cancellation message", err)
+ }
+ err = runError(context.Background(), false, oe, b)
+ if !errors.Is(err, oe) || errors.Is(err, context.Canceled) {
+ t.Errorf("plain failure = %v", err)
+ }
+}
+
+func TestZeroValueSession(t *testing.T) {
+ var s Session
+ before := references()
+ if err := s.Close(); err != nil {
+ t.Fatalf("Close = %v", err)
+ }
+ if references() != before {
+ t.Errorf("Close of a zero Session released a reference: %d, was %d", references(), before)
+ }
+ if _, err := s.Run(context.Background(), infer.Batch{}); !errors.Is(err, infer.ErrClosed) {
+ t.Fatalf("Run = %v, want infer.ErrClosed", err)
+ }
+}
diff --git a/laya-go/onnx/graph_test.go b/laya-go/onnx/graph_test.go
index 3aa7e5ff..bcfa601c 100644
--- a/laya-go/onnx/graph_test.go
+++ b/laya-go/onnx/graph_test.go
@@ -201,6 +201,16 @@ func TestLoadErrorHint(t *testing.T) {
}
}
+func TestLoadErrorHintOnlyForAPIBaseFailures(t *testing.T) {
+ err := loadError("/x/missing.so", errors.New("dlopen: no such file or directory"))
+ if !errors.Is(err, ErrRuntime) || !strings.Contains(err.Error(), "no such file") {
+ t.Fatalf("%v", err)
+ }
+ if strings.Contains(err.Error(), "1.29") {
+ t.Errorf("a file-not-found error carries the version hint: %v", err)
+ }
+}
+
func TestResolveOptions(t *testing.T) {
o, err := resolve([]Option{WithLibrary("/l"), nil, WithIntraOpThreads(3), WithInterOpThreads(2)})
if err != nil || o.library != "/l" || o.intra != 3 || o.inter != 2 {
diff --git a/laya-go/onnx/options.go b/laya-go/onnx/options.go
index 5c336c9d..0c270ac9 100644
--- a/laya-go/onnx/options.go
+++ b/laya-go/onnx/options.go
@@ -5,6 +5,7 @@ import (
"fmt"
"path/filepath"
"runtime"
+ "strings"
)
// LibraryEnv names the environment variable that holds the path of the ONNX Runtime shared
@@ -17,7 +18,10 @@ var ErrNoCGO = errors.New("onnx: built without cgo")
// ErrRuntime wraps every failure to load or start ONNX Runtime, with the path tried and a hint.
var ErrRuntime = errors.New("onnx: cannot load ONNX Runtime")
-// ErrRuntimeConflict is returned when Open names a library other than the one already loaded.
+// ErrRuntimeConflict is returned when Open is given a WithLibrary path other than the one
+// already loaded. Only an explicit WithLibrary is checked: LibraryEnv and the default name are
+// used only to load the first runtime. An ONNX Runtime the application initialized itself is
+// adopted as is, and then no library option is checked or used.
var ErrRuntimeConflict = errors.New("onnx: ONNX Runtime already loaded from another path")
// Option adjusts Open.
@@ -28,13 +32,16 @@ type options struct {
intra, inter int
}
-// WithLibrary sets the ONNX Runtime shared library to load. It overrides LibraryEnv.
+// WithLibrary sets the ONNX Runtime shared library to load. It overrides LibraryEnv. If a
+// runtime is already loaded from another path, Open fails with ErrRuntimeConflict.
func WithLibrary(path string) Option { return func(o *options) { o.library = path } }
// WithIntraOpThreads sets the threads one operator may use. 0 keeps the runtime default.
func WithIntraOpThreads(n int) Option { return func(o *options) { o.intra = n } }
-// WithInterOpThreads sets the operators that may run at once. 0 keeps the runtime default.
+// WithInterOpThreads sets the operators that may run at once. 0 keeps the runtime default. A
+// value above 0 also selects ONNX Runtime's parallel execution mode, which ignores the count
+// otherwise.
func WithInterOpThreads(n int) Option { return func(o *options) { o.inter = n } }
// resolve applies the options and rejects values that cannot be honored.
@@ -90,6 +97,9 @@ func samePath(a, b string) bool {
// loadError wraps a failure to start the runtime from path as ErrRuntime, with a hint.
func loadError(path string, err error) error {
- return fmt.Errorf("%w: %q: %v (needs ONNX Runtime 1.29 or newer; set WithLibrary or %s to its shared library)",
- ErrRuntime, path, err, LibraryEnv)
+ hint := ""
+ if strings.Contains(err.Error(), "API base") {
+ hint = " (needs ONNX Runtime 1.29 or newer; set WithLibrary or " + LibraryEnv + " to its shared library)"
+ }
+ return fmt.Errorf("%w: %q: %v%s", ErrRuntime, path, err, hint)
}
diff --git a/laya-go/onnx/session_cgo.go b/laya-go/onnx/session_cgo.go
index 8d96aa2c..8bfc3b6c 100644
--- a/laya-go/onnx/session_cgo.go
+++ b/laya-go/onnx/session_cgo.go
@@ -6,6 +6,7 @@ import (
"context"
"errors"
"fmt"
+ "strings"
"sync"
"sync/atomic"
@@ -30,7 +31,9 @@ var (
)
// Open loads a fused laya.onnx, whose .data sidecar must sit beside it, on the CPU provider.
-// A graph that is missing, split, or declares other tensors is a *GraphError.
+// A graph that is missing, split, lacks its sidecar, or declares other tensors is a *GraphError.
+// Open reads the graph twice: once to check it, once to run it, because the binding has no
+// session-level query for a graph's inputs and outputs.
func Open(graphPath string, opts ...Option) (*Session, error) {
o, err := resolve(opts)
if err != nil {
@@ -54,6 +57,10 @@ func Open(graphPath string, opts ...Option) (*Session, error) {
func load(path string, o options) (*Session, error) {
in, out, err := ort.GetInputOutputInfo(path)
if err != nil {
+ if strings.Contains(err.Error(), "External data") {
+ return nil, &GraphError{Path: path, Problem: fmt.Sprintf(
+ "its external-data (.data) sidecar is missing; it must sit beside the graph file (%v)", err)}
+ }
return nil, fmt.Errorf("onnx: cannot read %s: %w", path, err)
}
if err := checkGraph(infos(in), infos(out)); err != nil {
@@ -71,15 +78,8 @@ func load(path string, o options) (*Session, error) {
if err := so.SetGraphOptimizationLevel(ort.GraphOptimizationLevelEnableAll); err != nil {
return nil, fmt.Errorf("onnx: optimization level: %w", err)
}
- if o.intra > 0 {
- if err := so.SetIntraOpNumThreads(o.intra); err != nil {
- return nil, fmt.Errorf("onnx: intra-op threads: %w", err)
- }
- }
- if o.inter > 0 {
- if err := so.SetInterOpNumThreads(o.inter); err != nil {
- return nil, fmt.Errorf("onnx: inter-op threads: %w", err)
- }
+ if err := applyThreads(so, o); err != nil {
+ return nil, err
}
ds, err := ort.NewDynamicAdvancedSession(path, inputNames, outputNames, so)
if err != nil {
@@ -88,6 +88,32 @@ func load(path string, o options) (*Session, error) {
return &Session{s: ds}, nil
}
+// threadConfig is the part of ort.SessionOptions that applyThreads sets, so a test can record it.
+type threadConfig interface {
+ SetIntraOpNumThreads(n int) error
+ SetInterOpNumThreads(n int) error
+ SetExecutionMode(m ort.ExecutionMode) error
+}
+
+// applyThreads sets the thread options. ONNX Runtime ignores the inter-op count in sequential
+// execution mode, so a requested inter-op count also selects parallel mode.
+func applyThreads(c threadConfig, o options) error {
+ if o.intra > 0 {
+ if err := c.SetIntraOpNumThreads(o.intra); err != nil {
+ return fmt.Errorf("onnx: intra-op threads: %w", err)
+ }
+ }
+ if o.inter > 0 {
+ if err := c.SetExecutionMode(ort.ExecutionModeParallel); err != nil {
+ return fmt.Errorf("onnx: execution mode: %w", err)
+ }
+ if err := c.SetInterOpNumThreads(o.inter); err != nil {
+ return fmt.Errorf("onnx: inter-op threads: %w", err)
+ }
+ }
+ return nil
+}
+
func infos(in []ort.InputOutputInfo) []tensorInfo {
out := make([]tensorInfo, len(in))
for i, t := range in {
@@ -113,11 +139,12 @@ func infos(in []ort.InputOutputInfo) []tensorInfo {
func (s *Session) RuntimeVersion() string { return s.version }
// Close waits for runs in flight, releases the graph and drops its hold on ONNX Runtime, which
-// is unloaded when the last session closes. It is idempotent.
+// is unloaded when the last session closes, unless the application initialized that runtime
+// itself. It is idempotent, and a Session not made by Open closes with no effect.
func (s *Session) Close() error {
s.mu.Lock()
defer s.mu.Unlock()
- if s.closed {
+ if s.closed || s.s == nil {
return nil
}
s.closed = true
@@ -132,7 +159,7 @@ func (s *Session) Close() error {
func (s *Session) Run(ctx context.Context, b infer.Batch) (infer.Output, error) {
s.mu.RLock()
defer s.mu.RUnlock()
- if s.closed {
+ if s.closed || s.s == nil {
return infer.Output{}, infer.ErrClosed
}
if err := b.Validate(); err != nil {
@@ -191,11 +218,8 @@ func (s *Session) Run(ctx context.Context, b infer.Batch) (infer.Output, error)
}()
s.runs.Add(1)
- if err := s.execute(ctx, inputs, outputs); err != nil {
- if cerr := ctx.Err(); cerr != nil {
- return infer.Output{}, fmt.Errorf("onnx: run cancelled: %w", cerr)
- }
- return infer.Output{}, fmt.Errorf("onnx: run failed on %d rows of %d tokens: %w", b.Rows, b.Length, err)
+ if terminated, err := s.execute(ctx, inputs, outputs); err != nil {
+ return infer.Output{}, runError(ctx, terminated, err, b)
}
logits, lok := outputs[0].(*ort.Tensor[float32])
act, aok := outputs[1].(*ort.Tensor[float32])
@@ -205,31 +229,51 @@ func (s *Session) Run(ctx context.Context, b infer.Batch) (infer.Output, error)
return collect(b, outTensor{logits.GetShape(), logits.GetData()}, outTensor{act.GetShape(), act.GetData()})
}
-// execute runs the graph, terminating it if ctx ends first. The watcher is always joined before
-// the run options are destroyed.
-func (s *Session) execute(ctx context.Context, inputs, outputs []ort.Value) error {
+// runError explains a failed run. A run the context terminated reports the cancellation; any
+// other failure keeps ONNX Runtime's error, and when the context has also ended it wraps both.
+func runError(ctx context.Context, terminated bool, err error, b infer.Batch) error {
+ cerr := ctx.Err()
+ switch {
+ case cerr != nil && terminated:
+ return fmt.Errorf("onnx: run cancelled: %w", cerr)
+ case cerr != nil:
+ return fmt.Errorf("onnx: run failed on %d rows of %d tokens: %w (context: %w)", b.Rows, b.Length, err, cerr)
+ }
+ return fmt.Errorf("onnx: run failed on %d rows of %d tokens: %w", b.Rows, b.Length, err)
+}
+
+// execute runs the graph, terminating it if ctx ends first, and reports whether it did. The
+// watcher is always joined before the run options are destroyed.
+func (s *Session) execute(ctx context.Context, inputs, outputs []ort.Value) (terminated bool, err error) {
done := ctx.Done()
if done == nil {
- return s.s.Run(inputs, outputs)
+ return false, s.s.Run(inputs, outputs)
}
ro, err := ort.NewRunOptions()
if err != nil {
- return fmt.Errorf("run options: %w", err)
+ return false, fmt.Errorf("run options: %w", err)
}
defer ro.Destroy()
stop := make(chan struct{})
joined := make(chan struct{})
+ var fired atomic.Bool
go func() {
defer close(joined)
select {
case <-done:
+ fired.Store(true)
ro.Terminate()
case <-stop:
}
}()
defer func() {
close(stop)
+ // The join must precede ro.Destroy (deferred above, so it runs after this): the watcher
+ // may still be inside ro.Terminate, and destroying the options under it is a
+ // use-after-free in native code.
<-joined
+ terminated = fired.Load()
}()
- return s.s.RunWithOptions(inputs, outputs, ro)
+ err = s.s.RunWithOptions(inputs, outputs, ro)
+ return false, err
}
diff --git a/laya-go/scripts/gen_go_fixtures.py b/laya-go/scripts/gen_go_fixtures.py
index 22bd8da1..51fcd19e 100644
--- a/laya-go/scripts/gen_go_fixtures.py
+++ b/laya-go/scripts/gen_go_fixtures.py
@@ -989,14 +989,16 @@ def _answer_shape(answer):
"""One answer's type, key sets and order and legend, with no number and no chosen option.
A choice is blanked only when it is one of the answer's options, so a choice that names no
- option, or is not a scalar, still differs. Anything that is not an answer is kept as it is.
+ option, or is not a scalar, still differs. Option membership is by key spelling, so a label's
+ type (false against "false") is not compared here; the Go tests pin it against a golden
+ recorded where they run. Anything that is not an answer is kept as it is.
"""
if not isinstance(answer, dict):
return answer
shaped = _unnumber(answer)
probabilities = answer.get("probabilities")
if "choice" in answer and isinstance(probabilities, dict) and _key(answer["choice"]) in probabilities:
- shaped["choice"] = None
+ shaped["choice"] = "<an option>" # not None, so a choice changed to null still differs
return shaped
@@ -1010,7 +1012,7 @@ def _text_shape(text):
"""answer_text, parsed and shaped; text that is not JSON is kept, so it differs."""
try:
return render(_answer_shape(json.loads(text))).rstrip("\n")
- except (TypeError, ValueError):
+ except (TypeError, ValueError, RecursionError):
return text
@@ -1045,7 +1047,7 @@ def main(argv=None):
try:
with open(path, encoding="utf-8") as handle:
current = render(portable(json.load(handle)))
- except (OSError, ValueError, KeyError, TypeError, AttributeError, IndexError):
+ except (OSError, ValueError, KeyError, TypeError, AttributeError, IndexError, RecursionError):
current = None
if current != render(portable(payload)):
print("gen_go_fixtures: %s is stale or missing; run laya-go/scripts/gen_go_fixtures.py"