Documentation
¶
Index ¶
- Variables
- func ByLibrary(l []DeviceInfo) [][]DeviceInfo
- func Dump(ctx Context, t Tensor, optsFuncs ...DumpOptions) string
- func FlashAttentionSupported(l []DeviceInfo) bool
- func GetDevicesEnv(l []DeviceInfo) map[string]string
- func LibraryPaths(l []DeviceInfo) []string
- func RegisterBackend(name string, f func(string, BackendParams) (Backend, error))
- func SimilarDeviceDescription(a, b string) bool
- func SimilarDeviceMemory(a, b uint64) bool
- type Backend
- type BackendCacheConfig
- type BackendParams
- type BaseRunner
- type ByFreeMemory
- type CacheConfig
- type Context
- type DType
- type DeviceComparison
- type DeviceID
- type DeviceInfo
- func (d DeviceInfo) AddInitValidation(env map[string]string)
- func (a DeviceInfo) Compare(b DeviceInfo) DeviceComparison
- func (d DeviceInfo) Compute() string
- func (d DeviceInfo) Driver() string
- func (a DeviceInfo) IsBetter(b DeviceInfo) bool
- func (d DeviceInfo) MinimumMemory() uint64
- func (d DeviceInfo) NeedsInitValidation() bool
- func (d DeviceInfo) PreferredLibrary(other DeviceInfo) bool
- type DumpOptions
- type FilteredRunnerDiscovery
- type FlashAttentionType
- type RunnerDiscovery
- type SamplingMode
- type ScaledDotProductAttention
- type SystemInfo
- type Tensor
Constants ¶
This section is empty.
Variables ¶
var LibOllamaPath = func() string { exe, err := os.Executable() if err != nil { return "" } if eval, err := filepath.EvalSymlinks(exe); err == nil { exe = eval } cwd, err := os.Getwd() if err != nil { cwd = "" } return findLibOllamaPath(libOllamaPathSearch{ executable: exe, workingDir: cwd, goos: runtime.GOOS, goarch: runtime.GOARCH, }) }()
LibOllamaPath is the root used to find bundled llama.cpp and MLX runtime libraries. GPU-specific libraries live in backend subdirectories such as cuda_v12, rocm_v7_2, vulkan, and mlx_cuda_v13.
Functions ¶
func ByLibrary ¶ added in v0.12.7
func ByLibrary(l []DeviceInfo) [][]DeviceInfo
func FlashAttentionSupported ¶ added in v0.12.7
func FlashAttentionSupported(l []DeviceInfo) bool
FlashAttentionSupported reports whether flash attention can be used across all selected devices.
func GetDevicesEnv ¶ added in v0.23.0
func GetDevicesEnv(l []DeviceInfo) map[string]string
Given the list of GPUs this instantiation is targeted for, figure out the device environment variables and any recorded per-device runner environment overrides.
func LibraryPaths ¶ added in v0.12.7
func LibraryPaths(l []DeviceInfo) []string
func RegisterBackend ¶
func RegisterBackend(name string, f func(string, BackendParams) (Backend, error))
func SimilarDeviceDescription ¶ added in v0.30.0
SimilarDeviceDescription reports whether two backend device descriptions are close enough to identify the same physical GPU across different libraries.
func SimilarDeviceMemory ¶ added in v0.30.0
Types ¶
type Backend ¶
type Backend interface {
// Close frees all memory associated with this backend
Close()
Load(ctx context.Context, progress func(float32)) error
Config() fs.Config
Get(name string) Tensor
NewContext() Context
NewContextSize(size int) Context
// Enumerate the devices available for inference via this backend
BackendDevices() []DeviceInfo
}
func NewBackend ¶
func NewBackend(modelPath string, params BackendParams) (Backend, error)
type BackendCacheConfig ¶ added in v0.5.13
type BackendCacheConfig interface {
CacheConfig() CacheConfig
}
BackendCacheConfig should be implemented by backends that need special output from the cache to meet specific requirements. It is frequently implemented in conjunction with ScaledDotProductAttention.
type BackendParams ¶
type BackendParams struct {
// AllocMemory causes the backend to allocate memory for the model. If
// false, this is only being used for discovering the required amount of
// memory and cannot load the model for running.
AllocMemory bool
// NumThreads sets the number of threads to use if running on the CPU
NumThreads int
// FlashAttention indicates that we should use a fused flash attention kernel
FlashAttention FlashAttentionType
}
BackendParams controls how the backend loads and executes models
type BaseRunner ¶ added in v0.12.7
type BaseRunner interface {
// GetPort returns the localhost port number the runner is running on
GetPort() int
// HasExited indicates if the runner is no longer running. This can be used during
// bootstrap to detect if a given filtered device is incompatible and triggered an assert
HasExited() bool
}
type ByFreeMemory ¶ added in v0.12.7
type ByFreeMemory []DeviceInfo
Sort by Free Space. iGPUs are reported first, thus Reverse() yields the largest discrete GPU first
func (ByFreeMemory) Len ¶ added in v0.12.7
func (a ByFreeMemory) Len() int
func (ByFreeMemory) Less ¶ added in v0.12.7
func (a ByFreeMemory) Less(i, j int) bool
func (ByFreeMemory) Swap ¶ added in v0.12.7
func (a ByFreeMemory) Swap(i, j int)
type CacheConfig ¶ added in v0.5.13
type CacheConfig struct {
// CachePadding specifies the multiple for the number of tokens of cache history
// that will be returned from cache Get for k, v and mask. The capacity of the
// cache itself will also be increased to a multiple of this size if needed.
CachePadding int
// PermutedV performs Permute(ctx, 1, 2, 0, 3) on v tensors stored via Put
// and return the permuted version via Get. This uses the cache copy operation
// to avoid a Contiguous call on the permuted tensor.
PermutedV bool
// MaskDType specifies the data type for generating the mask. If unset it will
// default to DTypeF32.
MaskDType DType
}
CacheConfig controls optimizations (mostly backend-specific) that may transform the output the cache to work better with specific kernels.
type Context ¶
type Context interface {
Empty(dtype DType, shape ...int) Tensor
Zeros(dtype DType, shape ...int) Tensor
FromBytes(dtype DType, s []byte, shape ...int) Tensor
FromFloats(s []float32, shape ...int) Tensor
FromInts(s []int32, shape ...int) Tensor
// Arange creates a 1D tensor with values within an interval (start, stop] increased by step.
Arange(start, stop, step float32, dtype DType) Tensor
Forward(...Tensor) Context
// SetBatchSize provides a hint on the batch size to optimize processing
// Uses heuristics if not set
SetBatchSize(int)
Compute(...Tensor)
ComputeWithNotify(func(), ...Tensor) // notify callback once compute has begun
// Reserve is analogous to Compute but rather than executing a
// graph, simply preallocates memory. Typically called with a
// worst case graph to ensure all resources are available for
// for future inference.
Reserve()
MaxGraphNodes() int
Close()
// Input returns a context appropriate for creating tensors that are
// inputs to the model (which includes things like output locations)
Input() Context
// Layer returns a context appropriate for creating intermediate tensors
Layer(int) Context
}
type DeviceComparison ¶ added in v0.12.4
type DeviceComparison int
const ( UniqueDevice DeviceComparison = iota SameBackendDevice // The device is the same, and the library/backend is the same DuplicateDevice // The same physical device but different library/backend (overlapping device) )
type DeviceID ¶ added in v0.12.4
type DeviceID struct {
// ID is an identifier for the device for matching with system
// management libraries. The ID is only unique for other devices
// using the same Library.
// This ID represents a "post filtered" view of the enumerated devices
// if the ID is numeric
ID string `json:"id"`
// Library identifies which library is used for the device (e.g. CUDA, ROCm, etc.)
Library string `json:"backend,omitempty"`
}
Minimal unique device identification
type DeviceInfo ¶ added in v0.12.4
type DeviceInfo struct {
DeviceID
// Name is the name of the device as labeled by the backend. It
// may not be persistent across instances of the runner.
Name string `json:"name"`
// Description is the longer user-friendly identification of the device
Description string `json:"description"`
// FilterID is populated with the unfiltered device ID if a numeric ID is used
// so the device can be included.
FilterID string `json:"filter_id,omitempty"`
// Integrated is set true for integrated GPUs, false for Discrete GPUs
Integrated bool `json:"integration,omitempty"`
// PCIID is the bus, device and domain ID of the device for deduplication
// when discovered by multiple backends
PCIID string `json:"pci_id,omitempty"`
// TotalMemory is the total amount of memory the device can use for loading models
TotalMemory uint64 `json:"total_memory"`
// FreeMemory is the amount of memory currently available on the device for loading models
FreeMemory uint64 `json:"free_memory,omitempty"`
// ComputeMajor is the major version of capabilities of the device
// if unsupported by the backend, -1 will be returned
ComputeMajor int
// ComputeMinor is the minor version of capabilities of the device
// if unsupported by the backend, -1 will be returned
ComputeMinor int
// Driver Information
DriverMajor int `json:"driver_major,omitempty"`
DriverMinor int `json:"driver_minor,omitempty"`
// NVIDIADriverMajor is the NVIDIA kernel driver branch. CUDA driver APIs
// expose a separate CUDA compatibility version, so keep this distinct.
NVIDIADriverMajor int `json:"-"`
// GFXTarget is the AMD GPU gfx target string (e.g. "gfx1100") for ROCm
// device validation. Empty on non-AMD devices.
GFXTarget string `json:"gfx_target,omitempty"`
// Where backends were loaded from
LibraryPath []string
// RunnerEnvOverrides stores exceptional per-device runner environment
// overrides discovered during bootstrap. This is internal server state and
// is not serialized.
RunnerEnvOverrides map[string]string `json:"-"`
}
func (DeviceInfo) AddInitValidation ¶ added in v0.12.10
func (d DeviceInfo) AddInitValidation(env map[string]string)
Set the init validation environment variable
func (DeviceInfo) Compare ¶ added in v0.12.4
func (a DeviceInfo) Compare(b DeviceInfo) DeviceComparison
func (DeviceInfo) Compute ¶ added in v0.12.4
func (d DeviceInfo) Compute() string
func (DeviceInfo) Driver ¶ added in v0.12.4
func (d DeviceInfo) Driver() string
func (DeviceInfo) IsBetter ¶ added in v0.12.4
func (a DeviceInfo) IsBetter(b DeviceInfo) bool
For a SameBackendDevice, return true if b is better than a e.g. newer GPU library version
func (DeviceInfo) MinimumMemory ¶ added in v0.12.7
func (d DeviceInfo) MinimumMemory() uint64
MinimumMemory reports the amount of memory that should be set aside on the device for overhead (e.g. VRAM consumed by context structures independent of model allocations)
func (DeviceInfo) NeedsInitValidation ¶ added in v0.12.10
func (d DeviceInfo) NeedsInitValidation() bool
NeedsInitValidation returns true if the device in question has the potential to crash at inference time and requires deeper validation before we include it in the supported devices list.
func (DeviceInfo) PreferredLibrary ¶ added in v0.12.10
func (d DeviceInfo) PreferredLibrary(other DeviceInfo) bool
PreferredLibrary returns true if this library is preferred over the other input library Used to filter out Vulkan in favor of CUDA or ROCm
type DumpOptions ¶
type DumpOptions func(*dumpOptions)
func DumpWithEdgeItems ¶ added in v0.7.0
func DumpWithEdgeItems(n int) DumpOptions
DumpWithEdgeItems sets the number of elements to print at the beginning and end of each dimension.
func DumpWithPrecision ¶ added in v0.7.0
func DumpWithPrecision(n int) DumpOptions
DumpWithPrecision sets the number of decimal places to print. Applies to float32 and float64.
func DumpWithThreshold ¶ added in v0.7.0
func DumpWithThreshold(n int) DumpOptions
DumpWithThreshold sets the threshold for printing the entire tensor. If the number of elements is less than or equal to this value, the entire tensor will be printed. Otherwise, only the beginning and end of each dimension will be printed.
type FilteredRunnerDiscovery ¶ added in v0.12.7
type FilteredRunnerDiscovery interface {
RunnerDiscovery
// GetActiveDeviceIDs returns the filtered set of devices actively in
// use by this runner for running models. If the runner is a bootstrap runner, no devices
// will be active yet so no device IDs are returned.
// This routine will not query the underlying device and will return immediately
GetActiveDeviceIDs() []DeviceID
}
type FlashAttentionType ¶ added in v0.13.4
type FlashAttentionType int32
const ( // Aligned with llama_flash_attn_type FlashAttentionAuto FlashAttentionType = -1 FlashAttentionDisabled FlashAttentionType = 0 FlashAttentionEnabled FlashAttentionType = 1 )
func (FlashAttentionType) LogValue ¶ added in v0.13.4
func (f FlashAttentionType) LogValue() slog.Value
func (FlashAttentionType) String ¶ added in v0.13.4
func (f FlashAttentionType) String() string
type RunnerDiscovery ¶ added in v0.12.7
type RunnerDiscovery interface {
BaseRunner
// GetDeviceInfos will perform a query of the underlying device libraries
// for device identification and free VRAM information
// During bootstrap scenarios, this routine may take seconds to complete
GetDeviceInfos(ctx context.Context) []DeviceInfo
}
type SamplingMode ¶ added in v0.13.0
type SamplingMode int
const ( SamplingModeNearest SamplingMode = iota SamplingModeBilinear )
type ScaledDotProductAttention ¶
type ScaledDotProductAttention interface {
ScaledDotProductAttention(ctx Context, key, value, mask, sinks Tensor, vmla Tensor, scale float64, cacheConfigApplied bool) Tensor
}
ScaledDotProductAttention implements a fused attention operation equivalent to following code on a tensor named query:
query = query.Permute(ctx, 0, 2, 1, 3) key = key.Permute(ctx, 0, 2, 1, 3) value = value.Permute(ctx, 1, 2, 0, 3).Contiguous(ctx)
kq := key.MulmatFullPrec(ctx, query)
kq = kq.Scale(ctx, scale)
if mask != nil {
kq = kq.Add(ctx, mask)
}
kq = kq.Softmax(ctx)
kqv := value.Mulmat(ctx, kq) return kqv.Permute(ctx, 0, 2, 1, 3).Contiguous(ctx)
cacheConfigApplied indicates whether the optimizations requested through CacheConfig have been performed
type SystemInfo ¶ added in v0.12.7
type SystemInfo struct {
// TotalMemory is the total amount of system memory
TotalMemory uint64 `json:"total_memory,omitempty"`
// FreeMemory is the amount of memory currently available on the system for loading models
FreeMemory uint64 `json:"free_memory,omitempty"`
// FreeSwap is the amount of system swap space reported as available
FreeSwap uint64 `json:"free_swap,omitempty"`
}
type Tensor ¶
type Tensor interface {
Dim(n int) int
Stride(n int) int
Shape() []int
DType() DType
Cast(ctx Context, dtype DType) Tensor
Bytes() []byte
Floats() []float32
BackendGet() []float32
FromBytes([]byte)
FromFloats([]float32)
FromInts([]int32)
Add(ctx Context, t2 Tensor) Tensor
Sub(ctx Context, t2 Tensor) Tensor
Mul(ctx Context, t2 Tensor) Tensor
Div(ctx Context, t2 Tensor) Tensor
Mulmat(ctx Context, t2 Tensor) Tensor
MulmatFullPrec(ctx Context, t2 Tensor) Tensor
MulmatID(ctx Context, t2, ids Tensor) Tensor
AddID(ctx Context, t2, ids Tensor) Tensor
Softmax(ctx Context) Tensor
L2Norm(ctx Context, eps float32) Tensor
LayerNorm(ctx Context, weight, bias Tensor, eps float32) Tensor
RMSNorm(ctx Context, weight Tensor, eps float32) Tensor
Scale(ctx Context, s float64) Tensor
SumRows(ctx Context) Tensor
AvgPool2D(ctx Context, k, s int, p float32) Tensor
Conv2D(ctx Context, weight Tensor, s0, s1, p0, p1, d0, d1 int) Tensor
Conv3D(ctx Context, weight Tensor, c, s0, s1, s2, p0, p1, p2, d0, d1, d2 int) Tensor
Conv1DDW(ctx Context, weight Tensor, s, p, d int) Tensor
SSMConv(ctx Context, kernel Tensor) Tensor
SSMScan(ctx Context, x, dt, A, B, C, ids Tensor) Tensor
IM2Col(ctx Context, weight Tensor, s0, s1, p0, p1, d0, d1 int) Tensor
Sin(ctx Context) Tensor
Cos(ctx Context) Tensor
Tanh(ctx Context) Tensor
GELU(ctx Context, up ...Tensor) Tensor
GELU_ERF(ctx Context) Tensor
QuickGELU(ctx Context, up ...Tensor) Tensor
SILU(ctx Context, up ...Tensor) Tensor
RELU(ctx Context, up ...Tensor) Tensor
Sigmoid(ctx Context) Tensor
SigmoidOut(ctx Context) Tensor
// AlphaLimitSILU is a variant of SILU that clamps the input to the range [-limit, limit]
SILUAlphaLimit(ctx Context, up Tensor, alpha, limit float32) Tensor
Reshape(ctx Context, shape ...int) Tensor
View(ctx Context, offset int, shape ...int) Tensor
Permute(ctx Context, shape ...int) Tensor
Contiguous(ctx Context, shape ...int) Tensor
Pad(ctx Context, shape ...int) Tensor
// PadExt pads with independent left/right amounts per dimension.
// Arguments: lp0, rp0, lp1, rp1, lp2, rp2, lp3, rp3 for dims 0-3.
PadExt(ctx Context, lp0, rp0, lp1, rp1, lp2, rp2, lp3, rp3 int) Tensor
Stack(ctx Context, dim int, s ...Tensor) Tensor
// Repeat repeats the tensor n times along dimension dim
Repeat(ctx Context, dim, n int) Tensor
Concat(ctx Context, t2 Tensor, dim int) Tensor
Rows(ctx Context, t2 Tensor) Tensor
SetRows(ctx Context, src Tensor, idxs Tensor) Tensor
SetInplace(ctx Context, src Tensor, nb1, nb2, nb3, offset int) Tensor
Copy(ctx Context, t2 Tensor) Tensor
Duplicate(ctx Context) Tensor
Slice(ctx Context, dim, low, high, step int) Tensor
Chunk(ctx Context, dim int, size int) []Tensor
ChunkSections(ctx Context, dim int, sections ...int) []Tensor
TopK(ctx Context, k int) Tensor
Argsort(ctx Context) Tensor
Mean(ctx Context) Tensor
Variance(ctx Context) Tensor
Stddev(ctx Context) Tensor
Sqr(ctx Context) Tensor
Sqrt(ctx Context) Tensor
Exp(ctx Context) Tensor
Neg(ctx Context) Tensor
// Clamp clamps values to [min, max] range
Clamp(ctx Context, min, max float32) Tensor
// Softplus computes ln(1 + exp(x))
Softplus(ctx Context) Tensor
// CumSum computes cumulative sum along dimension 0
CumSum(ctx Context) Tensor
// Diag creates a diagonal matrix from a 1D tensor
Diag(ctx Context) Tensor
// Tri converts a matrix to triangular form (0=upper+diag, 1=upper, 2=lower+diag, 3=lower)
Tri(ctx Context, triType int) Tensor
// Fill fills a tensor with a constant value (in-place)
Fill(ctx Context, value float32) Tensor
// Repeat4D repeats tensor to match target shape
Repeat4D(ctx Context, dim0, dim1, dim2, dim3 int) Tensor
// SolveTri solves a triangular system Ax = B
SolveTri(ctx Context, b Tensor, lower, left, unitDiag bool) Tensor
Interpolate(ctx Context, dims [4]int, samplingMode SamplingMode) Tensor
}