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: per-language temperatures and checkpoint digest verification

Adeshina Lasisi0cb4750e5625
diff --git a/laya-go/agent/agent.go b/laya-go/agent/agent.go
index e2f50350..7a48c4eb 100644
--- a/laya-go/agent/agent.go
+++ b/laya-go/agent/agent.go
@@ -69,7 +69,7 @@ type Agent struct {
}
// Option adjusts an Agent at Open: WithInstalledHooks, WithHookTimeout, WithHookErrors,
-// WithConcurrentHooks and WithHookWarnings.
+// WithConcurrentHooks, WithHookWarnings, WithLangTemperatures and WithExpectedSHA256.
type Option func(*options)
// Open loads rl_agent_config.json and the tokenizer from checkpoint, a directory holding a
@@ -100,7 +100,10 @@ func open(checkpoint fs.FS, s infer.Session, o options) (*Agent, error) {
if checkpoint == nil {
return nil, errors.New("agent: Open needs a checkpoint")
}
- cfg, temps, err := loadConfig(checkpoint)
+ if err := verifyDigests(checkpoint, o.sha256); err != nil {
+ return nil, err
+ }
+ cfg, temps, err := loadConfig(checkpoint, o.langTemps)
if err != nil {
return nil, err
}
diff --git a/laya-go/agent/config.go b/laya-go/agent/config.go
index 0dba2af0..3afe5099 100644
--- a/laya-go/agent/config.go
+++ b/laya-go/agent/config.go
@@ -75,11 +75,11 @@ type Config struct {
// LoadConfig reads and checks rl_agent_config.json from the root of checkpoint, as ONNXAgent
// does: max_len, head_max_len and option_layout, and a warning for every temperature clamped.
func LoadConfig(checkpoint fs.FS) (Config, error) {
- cfg, _, err := loadConfig(checkpoint)
+ cfg, _, err := loadConfig(checkpoint, nil)
return cfg, err
}
-func loadConfig(checkpoint fs.FS) (Config, *decode.Temperatures, error) {
+func loadConfig(checkpoint fs.FS, langs map[string]any) (Config, *decode.Temperatures, error) {
data, err := fs.ReadFile(checkpoint, configFile)
if err != nil {
return Config{}, nil, &ConfigError{Err: err}
@@ -155,6 +155,11 @@ func loadConfig(checkpoint fs.FS) (Config, *decode.Temperatures, error) {
if err != nil {
return Config{}, nil, &ConfigError{Field: "temperature", Err: err}
}
+ if len(langs) > 0 { // resolved against the raw base, as resolve_lang_temperatures is
+ if temps, err = decode.NewTemperatures(byType, byOptions, langs); err != nil {
+ return Config{}, nil, &ConfigError{Field: langTempsField, Err: err}
+ }
+ }
// ONNXAgent warns once about every entry the clamp changed, buckets first.
var rejected []string
diff --git a/laya-go/agent/digests.go b/laya-go/agent/digests.go
new file mode 100644
index 00000000..29235ed3
--- /dev/null
+++ b/laya-go/agent/digests.go
@@ -0,0 +1,62 @@
+package agent
+
+import (
+ "io/fs"
+ "sort"
+
+ "github.com/NandhaKishorM/laya/laya-go/internal/digest"
+)
+
+// langTempsField is the ConfigError.Field of a lang_temperatures option that cannot be used.
+const langTempsField = "lang_temperatures"
+
+// ErrDigestMismatch is wrapped by the Open error for a file whose SHA-256 is not the expected one.
+var ErrDigestMismatch = digest.ErrMismatch
+
+// ErrUnsafeDigestPath is wrapped by the Open error for a WithExpectedSHA256 name that is absolute
+// or leaves the checkpoint.
+var ErrUnsafeDigestPath = digest.ErrUnsafePath
+
+// WithLangTemperatures sets per-language temperatures, in the raw shape of laya's
+// lang_temperatures option: language tag -> {"temperature": [3 numbers] or nil,
+// "temperature_by_options": {bucket: number}}, an entry or its temperature being nil to inherit the
+// checkpoint's own. Tags are reduced to their primary subtag, lower-cased. A call that names a
+// language through WithLanguage then decodes with that language's table. A malformed entry makes
+// Open fail with a *ConfigError whose Field is "lang_temperatures". Values are clamped to [0.5, 5].
+func WithLangTemperatures(langs map[string]map[string]any) Option {
+ return func(o *options) {
+ o.langTemps = make(map[string]any, len(langs))
+ for lang, cfg := range langs {
+ o.langTemps[lang] = cfg // a nil map inherits the checkpoint's own, as None does
+ }
+ }
+}
+
+// WithExpectedSHA256 makes Open check files of the checkpoint against hex SHA-256 digests, keyed
+// by slash path under it, before any of them is parsed. A file that is absent or a directory fails
+// Open with an error wrapping fs.ErrNotExist, a different digest with one wrapping
+// ErrDigestMismatch that names the file and both digests, and a name that is absolute or climbs
+// out with ".." with one wrapping ErrUnsafeDigestPath. Files are hashed as a stream. To check the
+// graph too, give onnx.Open onnx.WithExpectedSHA256.
+func WithExpectedSHA256(digests map[string]string) Option {
+ return func(o *options) {
+ o.sha256 = make(map[string]string, len(digests))
+ for k, v := range digests {
+ o.sha256[k] = v
+ }
+ }
+}
+
+func verifyDigests(checkpoint fs.FS, want map[string]string) error {
+ names := make([]string, 0, len(want))
+ for name := range want {
+ names = append(names, name)
+ }
+ sort.Strings(names) // the same file is named first on every run
+ for _, name := range names {
+ if err := digest.File(checkpoint, name, want[name]); err != nil {
+ return err
+ }
+ }
+ return nil
+}
diff --git a/laya-go/agent/digests_test.go b/laya-go/agent/digests_test.go
new file mode 100644
index 00000000..20e4c3a3
--- /dev/null
+++ b/laya-go/agent/digests_test.go
@@ -0,0 +1,143 @@
+package agent_test
+
+import (
+ "context"
+ "crypto/sha256"
+ "encoding/hex"
+ "errors"
+ "io/fs"
+ "strings"
+ "testing"
+
+ "github.com/NandhaKishorM/laya/laya-go/agent"
+)
+
+const zhOverride = `{"zh": {"temperature": [2.0, 1.5, 3.0], "temperature_by_options": {"choice:2": 2.5}}}`
+
+func withTemps(t *testing.T, langs map[string]map[string]any, tag string) string {
+ t.Helper()
+ s := &fakeSession{}
+ a, err := agent.Open(toyCheckpoint(`{}`), s, agent.WithLangTemperatures(langs))
+ if err != nil {
+ t.Fatal(err)
+ }
+ defer a.Close()
+ var opts []agent.CallOption
+ if tag != "" {
+ opts = append(opts, agent.WithLanguage(tag))
+ }
+ r, err := a.Predict(context.Background(), "state", questions(t), opts...)
+ if err != nil {
+ t.Fatal(err)
+ }
+ return jsonOf(t, r)
+}
+
+func zh() map[string]map[string]any {
+ return map[string]map[string]any{"zh": {"temperature": []any{2.0, 1.5, 3.0}, "temperature_by_options": map[string]any{"choice:2": 2.5}}}
+}
+
+func TestLangTemperaturesSelectByPrimarySubtag(t *testing.T) {
+ plain := withTemps(t, zh(), "")
+ over := withTemps(t, zh(), "zh")
+ if plain == over {
+ t.Fatal("the zh override changed nothing")
+ }
+ for _, tag := range []string{"zh-CN", "ZH", "Zh-TW"} {
+ if got := withTemps(t, zh(), tag); got != over {
+ t.Errorf("%q did not select the zh override", tag)
+ }
+ }
+ for _, tag := range []string{"en", "yo-NG", "zhx", "z", "zh_Hans"} {
+ if got := withTemps(t, zh(), tag); got != plain {
+ t.Errorf("%q selected the zh override", tag)
+ }
+ }
+ if got := withTemps(t, nil, "zh"); got != plain {
+ t.Error("no overrides must change nothing")
+ }
+}
+
+func TestLangTemperaturesNilEntryInherits(t *testing.T) {
+ plain := withTemps(t, nil, "")
+ for name, langs := range map[string]map[string]map[string]any{
+ "nil entry": {"zh": nil},
+ "nil temperature": {"zh": {"temperature": nil}},
+ "empty entry": {"zh": {}},
+ } {
+ if got := withTemps(t, langs, "zh"); got != plain {
+ t.Errorf("%s: did not inherit the checkpoint's temperatures", name)
+ }
+ }
+}
+
+func TestLangTemperaturesAreRefusedAtOpen(t *testing.T) {
+ for name, langs := range map[string]map[string]map[string]any{
+ "not a list": {"zh": {"temperature": "hot"}},
+ "short list": {"zh": {"temperature": []any{1.0}}},
+ "bad by_options": {"zh": {"temperature_by_options": []any{1}}},
+ } {
+ t.Run(name, func(t *testing.T) {
+ _, err := agent.Open(toyCheckpoint(`{}`), &fakeSession{}, agent.WithLangTemperatures(langs))
+ var ce *agent.ConfigError
+ if !errors.As(err, &ce) || ce.Field != "lang_temperatures" {
+ t.Fatalf("err = %v, want a *ConfigError on lang_temperatures", err)
+ }
+ })
+ }
+}
+
+// A value that is not a number is not refused: clamp_temperature reads it as 1.0, as Python does.
+func TestLangTemperaturesNonNumbersReadAsOne(t *testing.T) {
+ ones := withTemps(t, map[string]map[string]any{"zh": {"temperature": []any{1.0, 1.0, 1.0}, "temperature_by_options": map[string]any{"choice:2": 1.0}}}, "zh")
+ if ones == withTemps(t, zh(), "zh") {
+ t.Fatal("the control does not differ from the override")
+ }
+ got := withTemps(t, map[string]map[string]any{"zh": {"temperature": []any{"a", true, nil}, "temperature_by_options": map[string]any{"choice:2": "x"}}}, "zh")
+ if got != ones {
+ t.Error("a non-number was not read as 1.0")
+ }
+}
+
+func hexOf(b string) string { s := sha256.Sum256([]byte(b)); return hex.EncodeToString(s[:]) }
+
+func TestExpectedSHA256(t *testing.T) {
+ cp := toyCheckpoint(`{}`)
+ good := hexOf(`{}`)
+ open := func(m map[string]string, fsys fs.FS) error {
+ s := &fakeSession{}
+ a, err := agent.Open(fsys, s, agent.WithExpectedSHA256(m))
+ if err == nil {
+ a.Close()
+ } else if s.closes != 1 {
+ t.Errorf("the session was closed %d times on a refused open", s.closes)
+ }
+ return err
+ }
+ if err := open(map[string]string{"rl_agent_config.json": good}, cp); err != nil {
+ t.Fatal(err)
+ }
+ if err := open(nil, cp); err != nil {
+ t.Fatal(err)
+ }
+ err := open(map[string]string{"rl_agent_config.json": hexOf("x")}, cp)
+ if !errors.Is(err, agent.ErrDigestMismatch) || !strings.Contains(err.Error(), "rl_agent_config.json") || !strings.Contains(err.Error(), good) {
+ t.Fatalf("mismatch err = %v", err)
+ }
+ if err := open(map[string]string{"gone.onnx": good}, cp); !errors.Is(err, fs.ErrNotExist) {
+ t.Fatalf("missing err = %v", err)
+ }
+ for _, p := range []string{"../x", "/etc/passwd", "a/../../x", "C:\\x"} {
+ if err := open(map[string]string{p: good}, cp); !errors.Is(err, agent.ErrUnsafeDigestPath) {
+ t.Errorf("%q: err = %v", p, err)
+ }
+ }
+}
+
+func TestDigestsAreVerifiedBeforeTheConfigIsParsed(t *testing.T) {
+ cp := toyCheckpoint(`{not json`)
+ _, err := agent.Open(cp, &fakeSession{}, agent.WithExpectedSHA256(map[string]string{"rl_agent_config.json": hexOf("x")}))
+ if !errors.Is(err, agent.ErrDigestMismatch) {
+ t.Fatalf("err = %v, want the digest mismatch first", err)
+ }
+}
diff --git a/laya-go/agent/hooks.go b/laya-go/agent/hooks.go
index 2fa58824..75206057 100644
--- a/laya-go/agent/hooks.go
+++ b/laya-go/agent/hooks.go
@@ -141,6 +141,8 @@ type options struct {
concurrent bool
warn func(string)
err error
+ langTemps map[string]any // WithLangTemperatures
+ sha256 map[string]string // WithExpectedSHA256
}
// WithInstalledHooks installs hooks on the agent, as AddHook does after Open.
diff --git a/laya-go/internal/digest/digest.go b/laya-go/internal/digest/digest.go
new file mode 100644
index 00000000..4e5d3145
--- /dev/null
+++ b/laya-go/internal/digest/digest.go
@@ -0,0 +1,77 @@
+// Package digest checks SHA-256 digests the way laya's verify_digests does: streamed, so a
+// multi-gigabyte graph is never read whole, and compared case-insensitively.
+package digest
+
+import (
+ "crypto/sha256"
+ "encoding/hex"
+ "errors"
+ "fmt"
+ "io"
+ "io/fs"
+ "path"
+ "strings"
+)
+
+// ErrMismatch is wrapped by the error for a file whose digest is not the expected one.
+var ErrMismatch = errors.New("SHA-256 mismatch")
+
+// ErrUnsafePath is wrapped by the error for a name that is absolute or escapes its directory.
+var ErrUnsafePath = errors.New("unsafe path in expected digests")
+
+// Sum streams r through SHA-256 and returns the lower-case hex digest.
+func Sum(r io.Reader) (string, error) {
+ h := sha256.New()
+ if _, err := io.Copy(h, r); err != nil {
+ return "", err
+ }
+ return hex.EncodeToString(h.Sum(nil)), nil
+}
+
+// Compare is nil when got equals want, ignoring case and surrounding space.
+func Compare(name, got, want string) error {
+ if strings.EqualFold(got, strings.TrimSpace(want)) {
+ return nil
+ }
+ return fmt.Errorf("laya: %w for %s: expected %s, got %s. The artifact does not match the reviewed "+
+ "digest; refusing to load it", ErrMismatch, name, want, got)
+}
+
+// Safe returns rel as a clean slash path under its directory, or an error wrapping ErrUnsafePath
+// when it is empty, absolute (POSIX or a Windows drive or UNC root) or climbs out with "..".
+func Safe(rel string) (string, error) {
+ n := strings.ReplaceAll(rel, "\\", "/")
+ drive := len(n) >= 2 && n[1] == ':' && (n[0]|0x20 >= 'a' && n[0]|0x20 <= 'z')
+ if strings.HasPrefix(n, "/") || drive {
+ return "", fmt.Errorf("laya: %w: absolute path %q", ErrUnsafePath, rel)
+ }
+ if n == "" || n == ".." || strings.HasPrefix(n, "../") || strings.Contains(n, "/../") || strings.HasSuffix(n, "/..") {
+ return "", fmt.Errorf("laya: %w: %q", ErrUnsafePath, rel)
+ }
+ if n = path.Clean(n); n == "." {
+ return "", fmt.Errorf("laya: %w: %q", ErrUnsafePath, rel)
+ }
+ return n, nil
+}
+
+// File hashes the regular file rel in fsys and compares it with want. A file that is absent or
+// is a directory is an error wrapping fs.ErrNotExist.
+func File(fsys fs.FS, rel, want string) error {
+ n, err := Safe(rel)
+ if err != nil {
+ return err
+ }
+ f, err := fsys.Open(n)
+ if err != nil {
+ return fmt.Errorf("laya: cannot verify %q: %w", rel, err)
+ }
+ defer f.Close()
+ if st, err := f.Stat(); err != nil || st.IsDir() {
+ return fmt.Errorf("laya: cannot verify %q: not a file: %w", rel, fs.ErrNotExist)
+ }
+ got, err := Sum(f)
+ if err != nil {
+ return fmt.Errorf("laya: cannot verify %q: %w", rel, err)
+ }
+ return Compare(rel, got, want)
+}
diff --git a/laya-go/internal/digest/digest_test.go b/laya-go/internal/digest/digest_test.go
new file mode 100644
index 00000000..592152d4
--- /dev/null
+++ b/laya-go/internal/digest/digest_test.go
@@ -0,0 +1,72 @@
+package digest
+
+import (
+ "crypto/sha256"
+ "encoding/hex"
+ "errors"
+ "io/fs"
+ "strings"
+ "testing"
+ "testing/fstest"
+ "testing/iotest"
+)
+
+func hexOf(b string) string { s := sha256.Sum256([]byte(b)); return hex.EncodeToString(s[:]) }
+
+func TestFile(t *testing.T) {
+ fsys := fstest.MapFS{"a/b.bin": {Data: []byte("graph")}, "d/x": {Data: []byte("x")}}
+ good := hexOf("graph")
+ for _, c := range []struct {
+ name, rel, want string
+ is error
+ }{
+ {"match", "a/b.bin", good, nil},
+ {"upper-case digest", "a/b.bin", strings.ToUpper(good), nil},
+ {"padded digest", "a/b.bin", " " + good + "\n", nil},
+ {"mismatch", "a/b.bin", hexOf("other"), ErrMismatch},
+ {"missing", "nope", good, fs.ErrNotExist},
+ {"directory", "d", good, fs.ErrNotExist},
+ {"parent", "../x", good, ErrUnsafePath},
+ {"inner parent", "a/../b", good, ErrUnsafePath},
+ {"trailing parent", "a/..", good, ErrUnsafePath},
+ {"bare parent", "..", good, ErrUnsafePath},
+ {"absolute", "/etc/passwd", good, ErrUnsafePath},
+ {"drive", "C:\\x", good, ErrUnsafePath},
+ {"backslash parent", "..\\x", good, ErrUnsafePath},
+ {"dot", ".", good, ErrUnsafePath},
+ {"empty", "", good, ErrUnsafePath},
+ } {
+ t.Run(c.name, func(t *testing.T) {
+ err := File(fsys, c.rel, c.want)
+ if c.is == nil {
+ if err != nil {
+ t.Fatal(err)
+ }
+ return
+ }
+ if !errors.Is(err, c.is) {
+ t.Fatalf("err = %v, want %v", err, c.is)
+ }
+ })
+ }
+}
+
+func TestMismatchNamesTheFileAndBothDigests(t *testing.T) {
+ err := File(fstest.MapFS{"w.onnx": {Data: []byte("a")}}, "w.onnx", "ff")
+ for _, s := range []string{"w.onnx", "ff", hexOf("a")} {
+ if err == nil || !strings.Contains(err.Error(), s) {
+ t.Fatalf("%v does not name %q", err, s)
+ }
+ }
+}
+
+func TestSumStreamsAndReportsReadErrors(t *testing.T) {
+ big := strings.Repeat("0123456789abcdef", 1<<16)
+ got, err := Sum(iotest.OneByteReader(strings.NewReader(big[:5000])))
+ if err != nil || got != hexOf(big[:5000]) {
+ t.Fatalf("got %s, %v", got, err)
+ }
+ if _, err := Sum(iotest.ErrReader(errors.New("boom"))); err == nil {
+ t.Fatal("a read error was swallowed")
+ }
+}
diff --git a/laya-go/onnx/digest_cgo_test.go b/laya-go/onnx/digest_cgo_test.go
new file mode 100644
index 00000000..e911f592
--- /dev/null
+++ b/laya-go/onnx/digest_cgo_test.go
@@ -0,0 +1,28 @@
+//go:build cgo
+
+package onnx
+
+import (
+ "errors"
+ "strings"
+ "testing"
+)
+
+// A wrong digest is refused before the runtime is touched: no library is loaded and no reference
+// is taken, so the library path below, which does not exist, is never reached.
+func TestWrongDigestIsRefusedBeforeTheRuntimeLoads(t *testing.T) {
+ path, sum := stubGraph(t)
+ before := references()
+ _, err := Open(path, WithLibrary("/nonexistent/libonnxruntime"), WithExpectedSHA256(strings.Repeat("0", 64)))
+ if !errors.Is(err, ErrDigestMismatch) {
+ t.Fatalf("Open = %v, want ErrDigestMismatch", err)
+ }
+ if references() != before {
+ t.Errorf("a refused Open took a runtime reference")
+ }
+ // the right digest passes the check and reaches the runtime, which then fails on the library
+ _, err = Open(path, WithLibrary("/nonexistent/libonnxruntime"), WithExpectedSHA256(sum))
+ if err == nil || errors.Is(err, ErrDigestMismatch) {
+ t.Fatalf("Open with the right digest = %v, want a runtime error", err)
+ }
+}
diff --git a/laya-go/onnx/digest_test.go b/laya-go/onnx/digest_test.go
new file mode 100644
index 00000000..23610d34
--- /dev/null
+++ b/laya-go/onnx/digest_test.go
@@ -0,0 +1,55 @@
+package onnx
+
+import (
+ "crypto/sha256"
+ "encoding/hex"
+ "errors"
+ "io/fs"
+ "os"
+ "path/filepath"
+ "strings"
+ "testing"
+)
+
+func stubGraph(t *testing.T) (path, sum string) {
+ t.Helper()
+ path = filepath.Join(t.TempDir(), "laya.onnx")
+ if err := os.WriteFile(path, []byte("not a graph"), 0o644); err != nil {
+ t.Fatal(err)
+ }
+ s := sha256.Sum256([]byte("not a graph"))
+ return path, hex.EncodeToString(s[:])
+}
+
+func TestVerifyGraph(t *testing.T) {
+ path, sum := stubGraph(t)
+ for name, c := range map[string]struct {
+ want string
+ is error
+ }{
+ "match": {sum, nil},
+ "upper": {strings.ToUpper(sum), nil},
+ "mismatch": {strings.Repeat("0", 64), ErrDigestMismatch},
+ "empty want": {"", ErrDigestMismatch},
+ } {
+ t.Run(name, func(t *testing.T) {
+ var o options
+ WithExpectedSHA256(c.want)(&o)
+ err := verifyGraph(path, o)
+ if c.is == nil && err != nil || c.is != nil && !errors.Is(err, c.is) {
+ t.Fatalf("err = %v, want %v", err, c.is)
+ }
+ if c.is != nil && (!strings.Contains(err.Error(), path) || !strings.Contains(err.Error(), sum)) {
+ t.Errorf("%v does not name the file and the actual digest", err)
+ }
+ })
+ }
+ if err := verifyGraph(path, options{}); err != nil {
+ t.Fatalf("no expectation must verify nothing: %v", err)
+ }
+ var o options
+ WithExpectedSHA256(sum)(&o)
+ if err := verifyGraph(filepath.Join(t.TempDir(), "gone.onnx"), o); !errors.Is(err, fs.ErrNotExist) {
+ t.Fatalf("missing graph = %v", err)
+ }
+}
diff --git a/laya-go/onnx/options.go b/laya-go/onnx/options.go
index 0c270ac9..26ee3144 100644
--- a/laya-go/onnx/options.go
+++ b/laya-go/onnx/options.go
@@ -3,9 +3,12 @@ package onnx
import (
"errors"
"fmt"
+ "os"
"path/filepath"
"runtime"
"strings"
+
+ "github.com/NandhaKishorM/laya/laya-go/internal/digest"
)
// LibraryEnv names the environment variable that holds the path of the ONNX Runtime shared
@@ -30,6 +33,8 @@ type Option func(*options)
type options struct {
library string
intra, inter int
+ sha256 string
+ checkSHA bool
}
// WithLibrary sets the ONNX Runtime shared library to load. It overrides LibraryEnv. If a
@@ -44,6 +49,34 @@ func WithIntraOpThreads(n int) Option { return func(o *options) { o.intra = n }
// otherwise.
func WithInterOpThreads(n int) Option { return func(o *options) { o.inter = n } }
+// ErrDigestMismatch is wrapped by the error Open returns for a graph whose SHA-256 is not the
+// expected one.
+var ErrDigestMismatch = digest.ErrMismatch
+
+// WithExpectedSHA256 makes Open hash the graph file, as a stream, and refuse it unless its hex
+// SHA-256 is hexDigest, before ONNX Runtime loads it. The error wraps ErrDigestMismatch and names
+// the file and both digests. Only the graph is hashed, not its .data sidecar, as in Python.
+func WithExpectedSHA256(hexDigest string) Option {
+ return func(o *options) { o.sha256, o.checkSHA = hexDigest, true }
+}
+
+// verifyGraph is the check WithExpectedSHA256 asks for, or nil without it.
+func verifyGraph(path string, o options) error {
+ if !o.checkSHA {
+ return nil
+ }
+ f, err := os.Open(path)
+ if err != nil {
+ return fmt.Errorf("onnx: cannot verify %s: %w", path, err)
+ }
+ defer f.Close()
+ got, err := digest.Sum(f)
+ if err != nil {
+ return fmt.Errorf("onnx: cannot verify %s: %w", path, err)
+ }
+ return digest.Compare(path, got, o.sha256)
+}
+
// resolve applies the options and rejects values that cannot be honored.
func resolve(opts []Option) (options, error) {
var o options
diff --git a/laya-go/onnx/session_cgo.go b/laya-go/onnx/session_cgo.go
index 8bfc3b6c..ea4c9ced 100644
--- a/laya-go/onnx/session_cgo.go
+++ b/laya-go/onnx/session_cgo.go
@@ -42,6 +42,9 @@ func Open(graphPath string, opts ...Option) (*Session, error) {
if err := checkPath(graphPath); err != nil {
return nil, err
}
+ if err := verifyGraph(graphPath, o); err != nil {
+ return nil, err
+ }
version, err := acquire(o.library)
if err != nil {
return nil, err