Documentation
¶
Overview ¶
Package kernelhub reads GPU kernels published on the Hugging Face kernel hub, so that Go programs can check and launch them.
A hub kernel is a repository of type kernel. Version N is the branch vN, and build/<variant>/ holds one build per framework, compute backend, CPU and operating system, such as torch213-cxx11-cu132-aarch64-linux. Each variant has a metadata.json with SHA-256 digests of its files, and a Python extension module whose .nv_fatbin section carries one cubin per GPU architecture.
kernelhub never loads that module. Client.Fetch downloads a variant and checks every file against the digests in its metadata; Images reads the fatbin out of the module with debug/elf; Select picks the image for a device; Kernels lists the entry points of a cubin with their parameters and demangled names. Launching is the caller's: a kernel's grid, block and argument conventions live in the C++ wrapper the module was built from, not in the published files.
Signatures (metadata.json.sigstore) are not verified.
This package is outside the compatibility promise of github.com/tmc/lib/nvidia. Its API may change or be removed in patch releases.
Index ¶
- Constants
- Variables
- type Backend
- type Client
- func (c *Client) Commit(ctx context.Context, repo string, version int) (string, error)
- func (c *Client) Fetch(ctx context.Context, repo string, version int, variant string) (*Fetched, error)
- func (c *Client) Repo(ctx context.Context, repo string) (*Repo, error)
- func (c *Client) Variants(ctx context.Context, repo, commit string) ([]string, error)
- type Digest
- type Fetched
- type Image
- type Kernel
- type Metadata
- type Provenance
- type Repo
- type Revision
- type Variant
Constants ¶
const DefaultBaseURL = "https://huggingface.co"
DefaultBaseURL is the Hugging Face hub.
Variables ¶
var ErrDigest = errors.New("digest mismatch")
ErrDigest reports a file whose contents do not match its digest.
var ErrUntrusted = errors.New("untrusted publisher")
ErrUntrusted reports a repository from a publisher the hub does not mark as trusted, which Client.Trust does not list.
Functions ¶
This section is empty.
Types ¶
type Backend ¶
type Backend struct {
Type string `json:"type"` // cuda, rocm, cpu, metal, xpu, ...
Archs []string `json:"archs,omitempty"` // e.g. "9.0", "12.1+PTX"
}
Backend is the compute backend a variant was built for.
type Client ¶
type Client struct {
BaseURL string
HTTP *http.Client
// CacheDir, if set, holds downloaded variants under
// <repo>/<commit>/<variant>/, so a fetch of a commit already there
// reads no file over the network. Cached files are verified on every
// fetch like downloaded ones.
CacheDir string
// Trust lists repositories to accept from publishers the hub does not
// mark as trusted. Without it, Fetch refuses them.
Trust []string
}
A Client downloads kernels from the hub. The zero value uses DefaultBaseURL and http.DefaultClient, and downloads each variant into a new temporary directory that the caller owns.
func (*Client) Fetch ¶
func (c *Client) Fetch(ctx context.Context, repo string, version int, variant string) (*Fetched, error)
Fetch downloads variant of version of repo and verifies every file it lists against its digest. A repository from an untrusted publisher is refused unless c.Trust lists it.
type Digest ¶
type Digest struct {
Algorithm string `json:"algorithm"` // "sha256"
Files map[string]string `json:"files"` // standard base64
}
Digest holds a hash of every file of a variant, keyed by path relative to the variant directory.
type Fetched ¶
type Fetched struct {
Repo string
Version int
Commit string // the commit of branch v<Version> that was fetched
Variant Variant
Metadata *Metadata
Dir string // the variant directory
}
A Fetched is one verified build variant on disk.
type Image ¶
type Image struct {
SM int // 121 for sm_121 and sm_121a
ArchSpecific bool // an sm_NNNa image, which runs only on that SM
PTX bool
Compressed bool // Data is nil; kernelhub does not decompress
Data []byte // the cubin, or PTX text
}
An Image is one entry of a module's fatbin.
func Images ¶
Images reads the fatbin in the .nv_fatbin section of an extension module and returns its entries. It does not load the module.
type Kernel ¶
type Kernel struct {
elfinspect.Kernel
// Demangled is the C++ signature of Name, or Name if it does not
// demangle.
Demangled string
}
A Kernel is one entry point of a cubin.
type Metadata ¶
type Metadata struct {
Name string `json:"name"`
ID string `json:"id"` // module name; Torch ops live under torch.ops.<ID>
Version int `json:"version"`
License string `json:"license"`
Upstream string `json:"upstream,omitempty"`
Source string `json:"source,omitempty"`
PythonDepends []string `json:"python-depends,omitempty"`
Backend Backend `json:"backend"`
Digest Digest `json:"digest"`
Provenance Provenance `json:"provenance"`
}
Metadata is the metadata.json of one build variant.
func ParseMetadata ¶
ParseMetadata decodes a metadata.json.
type Provenance ¶
type Provenance struct {
KernelBuilder Revision `json:"kernel-builder"`
Kernel Revision `json:"kernel"`
}
Provenance records the sources a variant was built from.
type Repo ¶
type Repo struct {
ID string `json:"id"`
Author string `json:"author"`
TrustedPublisher bool `json:"trustedPublisher"`
SHA string `json:"sha"`
}
A Repo is what the hub reports about a kernel repository.
type Revision ¶
type Revision struct {
Version string `json:"version,omitempty"`
Commit string `json:"commit,omitempty"`
Dirty bool `json:"dirty"`
}
A Revision is a git commit, and whether the tree had uncommitted changes.
func (*Revision) UnmarshalJSON ¶
UnmarshalJSON accepts the commit as "commit", as the hub documentation names it, or as "sha", as published files spell it.
type Variant ¶
type Variant struct {
Name string // as published, e.g. "torch213-cxx11-cu132-aarch64-linux"
Framework string // e.g. "torch213", "torch-stable-abi29", "torch"
Backend string // "cu132", "rocm71", "cpu", "metal", "xpu20261", or "cuda" for Python-only
Arch, OS string // empty for a Python-only variant
}
A Variant is a parsed build variant name.
func ParseVariant ¶
ParseVariant parses a build variant directory name.
func SelectVariant ¶
func SelectVariant(names []string, arch, goos string, driverMajor, driverMinor int) (Variant, error)
SelectVariant returns the native CUDA variant for arch and goos built with the newest CUDA release at or below the driver's (driverMajor, driverMinor), preferring the newest framework among those. Variants that do not parse are skipped.