Documentation
¶
Overview ¶
Package ml inference defines bounded, transport-free interfaces for running on-device models through a caller-selected provider. The package owns the contracts only: it never loads a model, chooses a provider, allocates a tensor, or runs inference itself. Native providers ship beside these contracts as platform implementations (iOS CoreML in pkg/ext-darwin/int/model, Android NNAPI/TensorFlow Lite in pkg/ext-android/int/model) or as product adapters.
Package ml defines portable, transport-free contracts for on-device model support: model manifests, verified artifact installation, model inspection, and bounded inference through a caller-selected provider.
The package owns manifest and store contracts only. It never downloads a model, chooses a provider, runs inference itself, authorizes access, or exposes a remote protocol. Native model backends ship beside this contract as platform implementations (iOS CoreML in pkg/ext-darwin/int/model, Android NNAPI/TensorFlow Lite in pkg/ext-android/int/model) or as product adapters.
Package registry provides a thread-safe provider registry for model inference backends. Consumers register providers by name and resolve them when running inference. The registry itself knows nothing about specific provider implementations; those are registered by adapters or the host.
Package ml store implements bounded, caller-selected model artifact installation. A Store is rooted at a directory the caller selects and authorizes; the library never picks the path and never deletes anything the caller did not explicitly name.
Index ¶
- Constants
- Variables
- type Artifact
- type ArtifactSource
- type InMemoryArtifactSource
- type InstalledModel
- type Manifest
- type Provider
- type Reference
- type Registry
- type Session
- type Store
- func (s *Store) Close() error
- func (s *Store) Install(ctx context.Context, manifest Manifest, files map[string]ArtifactSource) (*InstalledModel, error)
- func (s *Store) InstallFromBytes(ctx context.Context, manifest Manifest, files map[string][]byte) (*InstalledModel, error)
- func (s *Store) List(ctx context.Context) ([]InstalledModel, error)
- func (s *Store) Path() string
- func (s *Store) Remove(ctx context.Context, provider, name, version string) error
- func (s *Store) Resolve(ctx context.Context, provider, name, version string) (*InstalledModel, error)
- type StoreLimits
- type Tensor
Constants ¶
const ( // ManifestAPIVersion is the declared version of the manifest contract. ManifestAPIVersion = "content.model/v1" // MaxManifestBytes bounds a model manifest in bytes. MaxManifestBytes = 64 << 10 // MaxNameLength bounds model names, providers, roles, and file names. MaxNameLength = 128 // MaxCapabilities bounds declared capabilities. MaxCapabilities = 32 )
const ( // DefaultMaxArtifacts bounds the number of artifacts a model may declare. DefaultMaxArtifacts = 64 // MaxArtifacts is the library ceiling for DefaultMaxArtifacts. MaxArtifacts = 256 // DefaultMaxArtifactBytes bounds one artifact file in bytes. DefaultMaxArtifactBytes = 64 << 30 // MaxArtifactBytes is the library ceiling for DefaultMaxArtifactBytes. MaxArtifactBytes = 1 << 40 // DefaultMaxTotalBytes bounds the total bundle size in bytes. DefaultMaxTotalBytes int64 = 1 << 40 // MaxTotalBytes is the library ceiling for DefaultMaxTotalBytes. MaxTotalBytes int64 = 1 << 50 )
Variables ¶
var ( // requested operation on this platform. ErrUnavailable = errors.New("inference provider unavailable") // ErrNotLoaded reports that a session is not bound to a loaded model. ErrNotLoaded = errors.New("model is not loaded") )
var ( // ErrInvalid reports a malformed manifest, artifact, or store value. ErrInvalid = errors.New("invalid model value") // ErrNotFound reports a model or artifact that is not installed. ErrNotFound = errors.New("model not found") // ErrExist reports a model that is already installed. ErrExist = errors.New("model already installed") // ErrDigest reports a SHA-256 digest mismatch during installation or load. ErrDigest = errors.New("digest mismatch") )
var ManifestAPIVersionPattern = ManifestAPIVersion
ManifestAPIVersionPattern anchors manifest version validation.
Functions ¶
This section is empty.
Types ¶
type Artifact ¶
type Artifact struct {
Role string `json:"role"`
File string `json:"file"`
SHA256 string `json:"sha256"`
}
Artifact is one file in a model bundle. Role is the semantic role (for example "model", "labels", "metadata"), File is the portable file name within the bundle directory, and SHA256 is the hex-encoded digest of the file contents.
type ArtifactSource ¶
type ArtifactSource interface {
// Open returns a readable stream for the artifact. The caller must close
// the returned reader. Open is called once per artifact.
Open(ctx context.Context) (io.ReadCloser, error)
// Size returns the declared byte size of the artifact, or -1 if unknown.
Size() int64
}
ArtifactSource is a bounded, readable source for one model artifact. It is the primary installation API: callers supply streaming sources so that large artifacts can be hashed and staged without ever being held entirely in memory.
type InMemoryArtifactSource ¶
type InMemoryArtifactSource struct {
// contains filtered or unexported fields
}
InMemoryArtifactSource wraps an in-memory byte slice as an ArtifactSource. It is a convenience wrapper for small artifacts; large artifacts should use a streaming source that reads from disk or network.
func NewInMemoryArtifactSource ¶
func NewInMemoryArtifactSource(data []byte) *InMemoryArtifactSource
NewInMemoryArtifactSource wraps data as an ArtifactSource.
func (*InMemoryArtifactSource) Open ¶
func (s *InMemoryArtifactSource) Open(ctx context.Context) (io.ReadCloser, error)
func (*InMemoryArtifactSource) Size ¶
func (s *InMemoryArtifactSource) Size() int64
type InstalledModel ¶
type InstalledModel struct {
Manifest Manifest `json:"manifest"`
Directory string `json:"directory"`
}
InstalledModel is an inspected, verified model bundle. Directory is the caller-selected store directory containing the bundle.
type Manifest ¶
type Manifest struct {
APIVersion string `json:"apiVersion"`
Name string `json:"name"`
Version string `json:"version"`
Provider string `json:"provider"`
Capabilities []string `json:"capabilities"`
Artifacts []Artifact `json:"artifacts"`
}
ModelManifest describes one model bundle. Name selects the model, Version selects the release, Provider names the native runtime that can load it, Capabilities declare what the model can do (for example "image.classify", "text.generate", "audio.transcribe"), and Artifacts list the files that make up the bundle with their SHA-256 digests.
type Provider ¶
type Provider interface {
// Name returns the provider identity.
Name() string
// Capabilities returns the model kinds this provider can load.
Capabilities() []string
// Load binds an installed model to a new session. The session is bound to
// the exact manifest and directory returned by the store.
Load(ctx context.Context, model *InstalledModel) (Session, error)
}
Provider is a caller-selected native model inference backend. Name reports the provider identity (for example "coreml", "nnapi", "onnxruntime", "directml"); Capabilities lists the model kinds it can load; Load binds a verified installed model to a Session.
type Reference ¶
Reference identifies one installed model.
func ReferenceFrom ¶
ReferenceFrom parses a provider/name@version reference string. The provider and name may not contain '/', '@', or whitespace; the version may not contain '@' or whitespace.
func (Reference) CanonicalKey ¶
CanonicalKey returns a stable store key for the reference.
type Registry ¶
type Registry struct {
// contains filtered or unexported fields
}
Registry maps provider names to Provider implementations. It is safe for concurrent use.
func (*Registry) Register ¶
Register adds a provider to the registry under its name. Registering a provider with the same name replaces the previous one.
func (*Registry) Resolve ¶
Resolve returns the provider registered under the given name, or ErrUnavailable if no such provider is registered.
func (*Registry) Unregister ¶
Unregister removes a provider from the registry.
type Session ¶
type Session interface {
// Infer runs one inference pass over the input tensors and returns the
// output tensors. Inputs and outputs are bounded by the provider.
Infer(ctx context.Context, inputs []Tensor) ([]Tensor, error)
// Close releases the model and any native resources. It is safe to call
// after an error and idempotent.
Close() error
}
Session runs bounded inference against one loaded model.
type Store ¶
type Store struct {
// contains filtered or unexported fields
}
Store manages one caller-selected model root directory. Bundles are stored under <root>/<provider>/<name>/<version>/ with a manifest.json and artifact files. The caller selects and authorizes the root; the library never picks it.
func OpenStore ¶
OpenStore opens an existing caller-selected model root directory. It refuses non-directories, symlinks, and broadly writable directories.
func (*Store) Close ¶
Close releases the directory handle held by the store. Callers should close each store they open after its operations and sessions have finished.
func (*Store) Install ¶
func (s *Store) Install(ctx context.Context, manifest Manifest, files map[string]ArtifactSource) (*InstalledModel, error)
Install stages and installs one model bundle from streaming artifact sources. It never replaces an existing bundle: an identical manifest is reported as already installed; a different one fails with ErrExist.
Each artifact is streamed through a hash and size check as it is copied into the staging directory, so large bundles never need to be held entirely in memory. If any check fails, the staging directory is removed and no partial bundle is visible at the target path.
func (*Store) InstallFromBytes ¶
func (s *Store) InstallFromBytes(ctx context.Context, manifest Manifest, files map[string][]byte) (*InstalledModel, error)
InstallFromBytes is a convenience wrapper around Install for small artifacts that are already in memory. It delegates to Install by wrapping each byte slice in an InMemoryArtifactSource.
func (*Store) List ¶
func (s *Store) List(ctx context.Context) ([]InstalledModel, error)
List returns every installed model, with digests re-verified.
type StoreLimits ¶
StoreLimits bound model installation. Zero values select the defaults.
type Tensor ¶
type Tensor struct {
Shape []int `json:"shape"`
Data []byte `json:"data"`
Format string `json:"format"`
}
Tensor is one bounded multi-dimensional array. Shape is the dimension lengths; Data is the row-major little-endian byte buffer; Format names the element encoding (for example "float32").
func FromFloat32 ¶
FromFloat32 builds a float32 tensor from a shape and values. The values are copied into little-endian bytes.
func RunModel ¶
func RunModel(ctx context.Context, reg *Registry, store *Store, ref Reference, inputs []Tensor) ([]Tensor, error)
RunModel resolves a model from the store, selects a provider from the registry, loads a session, runs inference, and closes the session. It is the portable helper that product commands (such as ctn model run) build on. ctx is used for cancellation and timeouts.