Help us shape the future of Go! Take the 2026 Go Developer Survey

models

package
v0.37.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 3, 2026 License: BSD-3-Clause Imports: 14 Imported by: 0

Documentation

Overview

Package models is the engine's read-only model mount (M7 of the LLM plan, go-wasmcontainers/gpu docs/llm-design.md): a container that asks for a model finds it at /models/NAME.gguf, read-only and range-readable, and can neither write to it nor name a URL.

A model is a registry entry pinned by Hugging Face commit and sha256. The bytes come from a Source (natively a file of the host's cache, in the browser the OPFS cache filled from the pinned URL); before the first use the source's size is checked against the entry and against MaxSize, and its whole content against the entry's sha256. Only then is it mounted, as an external file of a frozen rootfs tree (rootfs.FS.AddExternal), so nothing is copied into memory.

Index

Constants

View Source
const HashChunk = 16 << 20

HashChunk is the size of the reads that verify a source.

View Source
const MaxSize int64 = 2 << 30

MaxSize bounds a model file: the 1.5 GiB GPU grant ceiling, plus the room a GGUF header and the unquantised tensors need.

Variables

View Source
var (
	ErrUnknown  = errors.New("models: unknown model")
	ErrSize     = errors.New("models: wrong or excessive size")
	ErrChecksum = errors.New("models: sha256 mismatch")
	ErrHost     = errors.New("models: host not allowed")
)

Errors.

View Source
var Hosts = []string{"huggingface.co", "us.aws.cdn.hf.co"}

Hosts are the only hosts a model may be fetched from: Hugging Face and the CDN its resolve URLs redirect to (the playground's CSP connect-src lists the same two).

View Source
var Registry = []Entry{
	{Name: "smollm2-135m-instruct-q4_0", Repo: "bartowski/SmolLM2-135M-Instruct-GGUF",
		Revision: "09816acd5d99df7be770d85ea30822623dab342c", File: "SmolLM2-135M-Instruct-Q4_0.gguf",
		Size: 91893088, SHA256: "bcc3af2849ad6095af57e9b5cd43775256efdc66e306acb529172f92d0c04b03", License: "apache-2.0"},
}

Registry is the list of pinned models. Entries are added by pull request, each with its commit and the sha256 read from the repository's LFS pointer.

Functions

func CacheDir

func CacheDir() (string, error)

CacheDir is the native cache: os.UserCacheDir()/go-wasmcontainers/models.

func CachePath

func CachePath(dir string, e Entry) string

CachePath is where an entry lives in dir: named by its sha256, so a file can never be mistaken for another model.

func Mount

func Mount(srcs map[string]Source, entries []Entry) (*rootfs.FS, error)

Mount builds the tree mounted at /models: one external, read-only file per verified source. The tree is frozen. Every source must already be verified (use Open or Verify): Mount does not read it.

func ReportProgress added in v0.35.0

func ReportProgress(ctx context.Context, e Entry, done, total int64)

ReportProgress calls the ProgressFunc of ctx, if any.

func Verify

func Verify(src Source, e Entry) error

Verify checks a source against an entry before it is used: the size first (so nothing is read from a source of the wrong or an excessive size), then the sha256 of the whole content, read in HashChunk pieces.

func WithProgress added in v0.35.0

func WithProgress(ctx context.Context, fn ProgressFunc) context.Context

WithProgress returns a context whose downloads (OpenOPFS) report to fn.

Types

type Cached added in v0.35.0

type Cached struct {
	Name   string // the registry name, or "" for a file the registry does not list
	File   string // the file in the cache: SHA256.gguf
	Size   int64
	Pinned bool // the registry lists it
}

Cached is one model file of the browser cache.

type Entry

type Entry struct {
	Name     string // the mount name: /models/Name.gguf
	Repo     string // Hugging Face repository, "owner/name"
	Revision string // commit, never a branch
	File     string // file in the repository
	Size     int64
	SHA256   string // lowercase hex of the file (the LFS oid)
	License  string
}

Entry is one pinned model.

func Lookup

func Lookup(name string) (Entry, error)

Lookup finds an entry by name.

func (Entry) Check

func (e Entry) Check() error

Check refuses a malformed entry: a name that is not a plain file name, a revision that is not a 40-digit commit, a size above MaxSize, a bad digest.

func (Entry) FileName

func (e Entry) FileName() string

FileName is the name under /models.

func (Entry) URL

func (e Entry) URL() string

URL is the pinned resolve URL (the commit, never main).

type Fetcher

type Fetcher struct {
	Client *http.Client // nil: http.DefaultClient with the host check
	Hosts  []string     // nil: Hosts
}

Fetcher downloads entries into a cache directory.

func (Fetcher) Fetch

func (f Fetcher) Fetch(ctx context.Context, dir string, e Entry, rawURL string) error

Fetch downloads e from its pinned URL into dir, refusing a host outside Hosts at the first request and at every redirect, a Content-Length above the entry's size, and a body that is longer than, or hashes differently from, the entry. The file appears in dir only once verified.

type FileSource

type FileSource struct {
	// contains filtered or unexported fields
}

FileSource is a Source over a host file.

func OpenFile

func OpenFile(path string) (*FileSource, error)

OpenFile opens a host file as a Source.

func (*FileSource) Close

func (s *FileSource) Close() error

Close closes the file.

func (*FileSource) ReadAt

func (s *FileSource) ReadAt(p []byte, off int64) (int, error)

ReadAt implements io.ReaderAt.

func (*FileSource) Size

func (s *FileSource) Size() int64

Size is the file's length.

type HTTPSource

type HTTPSource struct {
	URL    string
	Client *http.Client
	Hosts  []string // nil: Hosts
	// ChunkSize is the range read size; zero means 16 MiB (HashChunk).
	ChunkSize int64
	// contains filtered or unexported fields
}

HTTPSource is a Source over a URL that honours Range requests, read in aligned chunks of ChunkSize (one chunk is kept), as the browser engine reads a model from the pinned Hugging Face URL. It follows redirects only to Hosts, never reads past the entry's size, and refuses a server that answers a range with the wrong length or a whole 200 body.

func NewHTTPSource

func NewHTTPSource(rawURL string, size int64, hosts []string, client *http.Client) (*HTTPSource, error)

NewHTTPSource returns a source of the given size (from the registry; the server is never trusted for it). The URL's host must be allowed.

func (*HTTPSource) Close

func (s *HTTPSource) Close() error

Close drops the cached chunk.

func (*HTTPSource) ReadAt

func (s *HTTPSource) ReadAt(p []byte, off int64) (int, error)

ReadAt implements io.ReaderAt.

func (*HTTPSource) Size

func (s *HTTPSource) Size() int64

Size is the registry's size.

type ProgressFunc added in v0.35.0

type ProgressFunc func(e Entry, done, total int64)

ProgressFunc is told how many bytes of a model are stored so far, and the total (Entry.Size), while a download runs. It is called from the downloading goroutine, never concurrently, at most once per chunk (HashChunk) and once at the end (done == total).

type Source

type Source interface {
	io.ReaderAt
	Size() int64
	Close() error
}

Source is a range-readable model file.

func Open

func Open(dir string, e Entry) (Source, error)

Open returns the verified source of an entry from the cache directory dir. A file already verified in this process (same path, size and modification time) is not hashed again.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL