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: review fixes for digests and language temperatures

Adeshina Lasisid653e442125a
diff --git a/laya-go/agent/agent.go b/laya-go/agent/agent.go
index 7a48c4eb..4f9fcfe6 100644
--- a/laya-go/agent/agent.go
+++ b/laya-go/agent/agent.go
@@ -100,7 +100,11 @@ func open(checkpoint fs.FS, s infer.Session, o options) (*Agent, error) {
if checkpoint == nil {
return nil, errors.New("agent: Open needs a checkpoint")
}
- if err := verifyDigests(checkpoint, o.sha256); err != nil {
+ want, err := expectedDigests(o.sha256)
+ if err != nil {
+ return nil, err
+ }
+ if err := verifyDigests(checkpoint, want); err != nil {
return nil, err
}
cfg, temps, err := loadConfig(checkpoint, o.langTemps)
diff --git a/laya-go/agent/digests.go b/laya-go/agent/digests.go
index 29235ed3..b6cb854d 100644
--- a/laya-go/agent/digests.go
+++ b/laya-go/agent/digests.go
@@ -37,7 +37,10 @@ func WithLangTemperatures(langs map[string]map[string]any) Option {
// 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.
+// graph too, give onnx.Open onnx.WithExpectedSHA256. Without this option Open reads
+// LAYA_SHA256_DIGESTS, a JSON object of name -> digest, as Python does (a value that is not valid
+// JSON, not an object or not a string is a *ConfigError on that field); its "onnx" and "onnx_path"
+// keys are left to onnx.Open. Giving the option, even empty, ignores the variable.
func WithExpectedSHA256(digests map[string]string) Option {
return func(o *options) {
o.sha256 = make(map[string]string, len(digests))
@@ -47,6 +50,22 @@ func WithExpectedSHA256(digests map[string]string) Option {
}
}
+// expectedDigests is what to verify: the option when WithExpectedSHA256 was given, else
+// LAYA_SHA256_DIGESTS, as Python does, without the "onnx" and "onnx_path" keys, which name the
+// graph and are checked by onnx.Open. A bad variable is a *ConfigError.
+func expectedDigests(given map[string]string) (map[string]string, error) {
+ if given != nil {
+ return given, nil
+ }
+ env, err := digest.FromEnv()
+ if err != nil {
+ return nil, &ConfigError{Field: digest.Env, Err: err}
+ }
+ delete(env, "onnx")
+ delete(env, "onnx_path")
+ return env, nil
+}
+
func verifyDigests(checkpoint fs.FS, want map[string]string) error {
names := make([]string, 0, len(want))
for name := range want {
diff --git a/laya-go/agent/digests_env_test.go b/laya-go/agent/digests_env_test.go
new file mode 100644
index 00000000..5cada249
--- /dev/null
+++ b/laya-go/agent/digests_env_test.go
@@ -0,0 +1,70 @@
+package agent_test
+
+import (
+ "errors"
+ "strings"
+ "testing"
+
+ "github.com/NandhaKishorM/laya/laya-go/agent"
+)
+
+func TestDigestsFromEnv(t *testing.T) {
+ cp := toyCheckpoint(`{}`)
+ good, bad := hexOf(`{}`), hexOf("x")
+ open := func(env string, opts ...agent.Option) error {
+ t.Setenv("LAYA_SHA256_DIGESTS", env)
+ a, err := agent.Open(cp, &fakeSession{}, opts...)
+ if err == nil {
+ a.Close()
+ }
+ return err
+ }
+ if err := open(` {"rl_agent_config.json":"` + good + `"} `); err != nil {
+ t.Fatal(err)
+ }
+ if err := open(""); err != nil {
+ t.Fatal(err)
+ }
+ if err := open(`{"onnx":"` + bad + `","onnx_path":"` + bad + `"}`); err != nil {
+ t.Fatalf("graph keys belong to onnx.Open: %v", err)
+ }
+ if err := open(`{"rl_agent_config.json":"` + bad + `"}`); !errors.Is(err, agent.ErrDigestMismatch) {
+ t.Fatalf("env mismatch = %v", err)
+ }
+ if err := open(`{"rl_agent_config.json":"`+bad+`"}`, agent.WithExpectedSHA256(map[string]string{})); err != nil {
+ t.Fatalf("an explicit option, even empty, ignores the variable: %v", err)
+ }
+ for _, v := range []string{"nope", `["a"]`, `{"rl_agent_config.json":1}`} {
+ err := open(v)
+ var ce *agent.ConfigError
+ if !errors.As(err, &ce) || ce.Field != "LAYA_SHA256_DIGESTS" {
+ t.Errorf("%q: err = %v", v, err)
+ }
+ }
+}
+
+func TestTemperatureByOptionsFalsyIsEmpty(t *testing.T) {
+ mk := func(v any) map[string]map[string]any {
+ return map[string]map[string]any{"zh": {"temperature": []any{2.0, 1.5, 3.0}, "temperature_by_options": v}}
+ }
+ want := withTempsFrom(t, mk(map[string]any{}))
+ for _, v := range []any{false, 0, 0.0, "", []any{}, nil} {
+ if got := withTempsFrom(t, mk(v)); got != want {
+ t.Errorf("%#v: %s != %s", v, got, want)
+ }
+ }
+}
+
+func withTempsFrom(t *testing.T, langs map[string]map[string]any) string {
+ t.Helper()
+ return withTemps(t, langs, "zh")
+}
+
+func TestDuplicatePrimarySubtagIsRefused(t *testing.T) {
+ langs := map[string]map[string]any{"PT": {"temperature": []any{2.0, 2.0, 2.0}}, "pt-BR": {"temperature": []any{3.0, 3.0, 3.0}}}
+ _, err := agent.Open(toyCheckpoint(`{}`), &fakeSession{}, agent.WithLangTemperatures(langs))
+ var ce *agent.ConfigError
+ if !errors.As(err, &ce) || !strings.Contains(err.Error(), "PT") || !strings.Contains(err.Error(), "pt-BR") {
+ t.Fatalf("err = %v", err)
+ }
+}
diff --git a/laya-go/agent/doc.go b/laya-go/agent/doc.go
index cc80b82f..59bf6d01 100644
--- a/laya-go/agent/doc.go
+++ b/laya-go/agent/doc.go
@@ -35,8 +35,8 @@
//
// # Scope
//
-// Not here yet: per-language temperature overrides, the calibration file, min_confidence,
-// hooks, digest checks and per-call max_len overrides. PredictLong ports predict_long for
-// aggregate="auto" and applies neither the gate nor hooks. WithLanguage is accepted
-// so callers need not change when overrides arrive; without overrides it does nothing.
+// Shipped: hooks, the min_confidence gate, per-language temperatures (WithLangTemperatures,
+// selected by WithLanguage) and digest checks (WithExpectedSHA256, LAYA_SHA256_DIGESTS). Not here
+// yet: the calibration file and the router. PredictLong ports predict_long for aggregate="auto"
+// and applies neither the gate nor hooks. Without overrides WithLanguage does nothing.
package agent
diff --git a/laya-go/internal/decode/decode_test.go b/laya-go/internal/decode/decode_test.go
index c386531b..ca701a64 100644
--- a/laya-go/internal/decode/decode_test.go
+++ b/laya-go/internal/decode/decode_test.go
@@ -257,7 +257,7 @@ func TestNewTemperaturesRefuses(t *testing.T) {
"language not a mapping": {nil, map[string]any{"de": 5}, "must be a mapping"},
"language list too short": {nil, map[string]any{"de": map[string]any{"temperature": []any{1.0}}}, "list of 3"},
"language list not a list": {nil, map[string]any{"de": map[string]any{"temperature": 2}}, "list of 3"},
- "by options not a mapping": {nil, map[string]any{"de": map[string]any{"temperature_by_options": []any{}}}, "mapping of bucket"},
+ "by options not a mapping": {nil, map[string]any{"de": map[string]any{"temperature_by_options": []any{1.0}}}, "mapping of bucket"},
}
for name, c := range cases {
_, err := NewTemperatures(c.byType, nil, c.langs)
@@ -708,3 +708,18 @@ func TestQuotientOverflowIsRefused(t *testing.T) {
t.Errorf("probabilities = %v, want 1 and 0", ps)
}
}
+
+func TestNewTemperaturesFalsyByOptionsIsEmpty(t *testing.T) {
+ for _, v := range []any{false, 0, 0.0, "", []any{}, map[string]any{}, nil} {
+ if _, err := NewTemperatures(nil, nil, map[string]any{"de": map[string]any{"temperature_by_options": v}}); err != nil {
+ t.Errorf("%#v: %v", v, err)
+ }
+ }
+}
+
+func TestNewTemperaturesRefusesSharedPrimarySubtag(t *testing.T) {
+ _, err := NewTemperatures(nil, nil, map[string]any{"PT": map[string]any{}, "pt-BR": map[string]any{}})
+ if err == nil || !strings.Contains(err.Error(), `"PT"`) || !strings.Contains(err.Error(), `"pt-BR"`) {
+ t.Fatalf("err = %v", err)
+ }
+}
diff --git a/laya-go/internal/decode/temperature.go b/laya-go/internal/decode/temperature.go
index 6c354a32..09e26dc5 100644
--- a/laya-go/internal/decode/temperature.go
+++ b/laya-go/internal/decode/temperature.go
@@ -76,12 +76,18 @@ func NewTemperatures(byType []any, byOptions map[string]any, byLanguage map[stri
for lang := range byLanguage {
langs = append(langs, lang)
}
+ seen := map[string]string{}
sort.Strings(langs) // a repeated primary subtag resolves the same way on every run
for _, lang := range langs {
resolved, err := resolveLanguage(lang, byLanguage[lang], base)
if err != nil {
return nil, err
}
+ p := primarySubtag(lang)
+ if first, dup := seen[p]; dup {
+ return nil, fmt.Errorf("decode: language overrides %q and %q share the primary subtag %q; Python keeps the last inserted, which a Go map cannot say, so give one", first, lang, p)
+ }
+ seen[p] = lang
t.ByLanguage[primarySubtag(lang)] = resolved
}
return t, nil
@@ -113,7 +119,7 @@ func resolveLanguage(lang string, raw any, base []any) (LanguageTemperatures, er
for i, v := range list {
out.ByType[i] = Clamp(v)
}
- if bo := cfg["temperature_by_options"]; bo != nil {
+ if bo := cfg["temperature_by_options"]; !falsy(bo) { // `or {}`: false, 0, "", [] and {} are empty
m, ok := asMap(bo)
if !ok {
return LanguageTemperatures{}, fmt.Errorf("decode: language override %q temperature_by_options must be a mapping of bucket -> float, got %T", lang, bo)
@@ -238,3 +244,28 @@ func isRange(err error) bool {
ne, ok := err.(*strconv.NumError)
return ok && ne.Err == strconv.ErrRange
}
+
+// falsy is Python truthiness for the values `cfg.get(...) or {}` meets: None, False, zero, "" and
+// an empty container.
+func falsy(v any) bool {
+ rv := reflect.ValueOf(v)
+ switch rv.Kind() {
+ case reflect.Invalid:
+ return true
+ case reflect.Bool:
+ return !rv.Bool()
+ case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
+ return rv.Int() == 0
+ case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
+ return rv.Uint() == 0
+ case reflect.Float32, reflect.Float64:
+ return rv.Float() == 0
+ case reflect.String, reflect.Slice, reflect.Array, reflect.Map:
+ return rv.Len() == 0
+ }
+ if n, ok := v.(json.Number); ok {
+ f, err := n.Float64()
+ return err == nil && f == 0
+ }
+ return false
+}
diff --git a/laya-go/internal/digest/digest.go b/laya-go/internal/digest/digest.go
index 4e5d3145..5fecd39f 100644
--- a/laya-go/internal/digest/digest.go
+++ b/laya-go/internal/digest/digest.go
@@ -5,10 +5,12 @@ package digest
import (
"crypto/sha256"
"encoding/hex"
+ "encoding/json"
"errors"
"fmt"
"io"
"io/fs"
+ "os"
"path"
"strings"
)
@@ -54,24 +56,61 @@ func Safe(rel string) (string, error) {
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.
+// File hashes the regular file rel in fsys and compares it with want. A file that is absent, a
+// directory, a device or a FIFO, or a name ending in "/", is an error wrapping fs.ErrNotExist.
+// Python raises FileNotFoundError for "a/.." and "."; here they are ErrUnsafePath (both refuse).
func File(fsys fs.FS, rel, want string) error {
n, err := Safe(rel)
if err != nil {
return err
}
+ if strings.HasSuffix(strings.ReplaceAll(rel, "\\", "/"), "/") { // "x/" names a directory, never the file x
+ return fmt.Errorf("laya: cannot verify %q: not a file: %w", rel, fs.ErrNotExist)
+ }
+ // Stat before Open: opening a FIFO blocks, and a device never ends. Python's isfile refuses both.
+ if st, err := fs.Stat(fsys, n); err != nil {
+ return fmt.Errorf("laya: cannot verify %q: %w", rel, err)
+ } else if !st.Mode().IsRegular() {
+ return fmt.Errorf("laya: cannot verify %q: not a regular file: %w", rel, fs.ErrNotExist)
+ }
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)
}
+
+// Env is the environment variable that carries expected digests when none are given in code.
+const Env = "LAYA_SHA256_DIGESTS"
+
+// FromEnv reads Env as a JSON object of artifact -> hex digest, as laya's verify_digests does when
+// no digests are given. A blank or unset variable is no expectation (nil). Anything that is not
+// an object of strings is an error.
+func FromEnv() (map[string]string, error) {
+ raw := strings.TrimSpace(os.Getenv(Env))
+ if raw == "" {
+ return nil, nil
+ }
+ var v any
+ if err := json.Unmarshal([]byte(raw), &v); err != nil {
+ return nil, fmt.Errorf("%s must be a JSON object of artifact->sha256: %w", Env, err)
+ }
+ obj, ok := v.(map[string]any)
+ if !ok {
+ return nil, fmt.Errorf("%s must be a JSON object of artifact->sha256, got %T", Env, v)
+ }
+ out := make(map[string]string, len(obj))
+ for k, x := range obj {
+ s, ok := x.(string)
+ if !ok {
+ return nil, fmt.Errorf("%s: the digest of %q must be a string, got %T", Env, k, x)
+ }
+ out[k] = s
+ }
+ return out, nil
+}
diff --git a/laya-go/internal/digest/digest_r_test.go b/laya-go/internal/digest/digest_r_test.go
new file mode 100644
index 00000000..eb5ca4db
--- /dev/null
+++ b/laya-go/internal/digest/digest_r_test.go
@@ -0,0 +1,55 @@
+package digest
+
+import (
+ "errors"
+ "io/fs"
+ "os"
+ "path/filepath"
+ "testing"
+ "testing/fstest"
+)
+
+func TestFileRefusesTrailingSlash(t *testing.T) {
+ fsys := fstest.MapFS{"a/b.bin": {Data: []byte("graph")}}
+ err := File(fsys, "a/b.bin/", hexOf("graph"))
+ if !errors.Is(err, fs.ErrNotExist) {
+ t.Fatalf("err = %v", err)
+ }
+}
+
+func TestFileRefusesNonRegularFiles(t *testing.T) {
+ dir := t.TempDir()
+ if err := os.Symlink("/dev/zero", filepath.Join(dir, "zero.onnx")); err != nil {
+ t.Fatal(err)
+ }
+ err := File(os.DirFS(dir), "zero.onnx", hexOf("x"))
+ if !errors.Is(err, fs.ErrNotExist) {
+ t.Fatalf("err = %v", err)
+ }
+}
+
+func TestFromEnv(t *testing.T) {
+ for _, c := range []struct {
+ name, val string
+ want map[string]string
+ bad bool
+ }{
+ {"unset", "", nil, false},
+ {"blank", " \n", nil, false},
+ {"object", ` {"a":"b"} `, map[string]string{"a": "b"}, false},
+ {"not json", "nope", nil, true},
+ {"array", `["a"]`, nil, true},
+ {"non-string", `{"a":1}`, nil, true},
+ } {
+ t.Run(c.name, func(t *testing.T) {
+ t.Setenv(Env, c.val)
+ got, err := FromEnv()
+ if c.bad != (err != nil) {
+ t.Fatalf("err = %v", err)
+ }
+ if len(got) != len(c.want) || got["a"] != c.want["a"] {
+ t.Fatalf("got %v", got)
+ }
+ })
+ }
+}
diff --git a/laya-go/onnx/digest_env_test.go b/laya-go/onnx/digest_env_test.go
new file mode 100644
index 00000000..61ddae32
--- /dev/null
+++ b/laya-go/onnx/digest_env_test.go
@@ -0,0 +1,43 @@
+package onnx
+
+import (
+ "errors"
+ "strings"
+ "testing"
+)
+
+func TestVerifyGraphFromEnv(t *testing.T) {
+ path, sum := stubGraph(t)
+ zero := strings.Repeat("0", 64)
+ for name, c := range map[string]struct {
+ env string
+ ok bool
+ }{
+ "onnx ok": {`{"onnx":"` + sum + `"}`, true},
+ "onnx_path ok": {`{"onnx_path":"` + sum + `"}`, true},
+ "onnx bad": {`{"onnx":"` + zero + `"}`, false},
+ "onnx_path bad": {`{"onnx_path":"` + zero + `"}`, false},
+ "both equal": {`{"onnx":"` + sum + `","onnx_path":"` + strings.ToUpper(sum) + `"}`, true},
+ "both differ": {`{"onnx":"` + sum + `","onnx_path":"` + zero + `"}`, false},
+ "other keys only": {`{"rl_agent_config.json":"` + zero + `"}`, true},
+ "unset": {"", true},
+ "not json": {"nope", false},
+ "non-string value": {`{"onnx":1}`, false},
+ } {
+ t.Run(name, func(t *testing.T) {
+ t.Setenv("LAYA_SHA256_DIGESTS", c.env)
+ if err := verifyGraph(path, options{}); c.ok != (err == nil) {
+ t.Fatalf("err = %v", err)
+ }
+ })
+ }
+ t.Setenv("LAYA_SHA256_DIGESTS", `{"onnx":"`+zero+`"}`)
+ var o options
+ WithExpectedSHA256(sum)(&o)
+ if err := verifyGraph(path, o); err != nil {
+ t.Fatalf("an explicit option wins: %v", err)
+ }
+ if err := verifyGraph(path, options{}); !errors.Is(err, ErrDigestMismatch) {
+ t.Fatalf("env mismatch = %v", err)
+ }
+}
diff --git a/laya-go/onnx/options.go b/laya-go/onnx/options.go
index 26ee3144..aed7af3c 100644
--- a/laya-go/onnx/options.go
+++ b/laya-go/onnx/options.go
@@ -55,15 +55,32 @@ 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.
+// the file and both digests. Without this option Open reads the "onnx" or "onnx_path" key of the
+// LAYA_SHA256_DIGESTS JSON object, as Python does. 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 {
+ want := o.sha256
if !o.checkSHA {
- return nil
+ env, err := digest.FromEnv()
+ if err != nil {
+ return fmt.Errorf("onnx: %w", err)
+ }
+ a, hasA := env["onnx"]
+ b, hasB := env["onnx_path"]
+ switch {
+ case hasA && hasB && !strings.EqualFold(strings.TrimSpace(a), strings.TrimSpace(b)):
+ return fmt.Errorf("onnx: %s gives two different digests for the graph (onnx and onnx_path)", digest.Env)
+ case hasA:
+ want = a
+ case hasB:
+ want = b
+ default:
+ return nil
+ }
}
f, err := os.Open(path)
if err != nil {
@@ -74,7 +91,7 @@ func verifyGraph(path string, o options) error {
if err != nil {
return fmt.Errorf("onnx: cannot verify %s: %w", path, err)
}
- return digest.Compare(path, got, o.sha256)
+ return digest.Compare(path, got, want)
}
// resolve applies the options and rejects values that cannot be honored.