kernelhub

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Dec 4, 2026 License: Apache-2.0 Imports: 24 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

View Source
const DefaultBaseURL = "https://huggingface.co"

DefaultBaseURL is the Hugging Face hub.

Variables

View Source
var ErrDigest = errors.New("digest mismatch")

ErrDigest reports a file whose contents do not match its digest.

View Source
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) Commit

func (c *Client) Commit(ctx context.Context, repo string, version int) (string, error)

Commit returns the commit branch v<version> of repo points at.

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.

func (*Client) Repo

func (c *Client) Repo(ctx context.Context, repo string) (*Repo, error)

Repo returns the hub's record of a kernel repository.

func (*Client) Variants

func (c *Client) Variants(ctx context.Context, repo, commit string) ([]string, error)

Variants lists the build variants of repo at commit.

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.

func (*Fetched) Module

func (f *Fetched) Module() (string, error)

Module returns the path of the variant's extension module.

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

func Images(module []byte) ([]Image, error)

Images reads the fatbin in the .nv_fatbin section of an extension module and returns its entries. It does not load the module.

func Select

func Select(imgs []Image, sm int) (Image, bool)

Select returns the cubin a device of compute capability sm (121 for 12.1) runs: an image for sm itself, or else the newest uncompressed cubin of the same major version and an older minor, which the device also runs. An arch-specific image runs only on its own SM.

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.

func Kernels

func Kernels(cubin []byte) ([]Kernel, error)

Kernels lists the entry points of a cubin with their parameters.

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

func ParseMetadata(data []byte) (*Metadata, error)

ParseMetadata decodes a metadata.json.

func (*Metadata) Verify

func (m *Metadata) Verify(fsys fs.FS) error

Verify checks every file m.Digest names against its contents in fsys, which is rooted at the variant directory. A missing file, a mismatch, an unknown algorithm or an empty digest list is an error.

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

func (r *Revision) UnmarshalJSON(b []byte) error

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

func ParseVariant(name string) (Variant, error)

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.

func (Variant) CUDA

func (v Variant) CUDA() (major, minor int, ok bool)

CUDA returns the CUDA version a native CUDA variant was built with: 13, 2 for cu132.

Jump to

Keyboard shortcuts

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