2024-10-16 17:45:00 -07:00
|
|
|
package discover
|
2023-11-29 11:00:37 -08:00
|
|
|
|
2024-05-07 14:54:26 -07:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log/slog"
|
|
|
|
|
|
|
|
"github.com/ollama/ollama/format"
|
|
|
|
)
|
|
|
|
|
2023-12-22 15:43:31 -08:00
|
|
|
type memInfo struct {
|
2023-11-29 11:00:37 -08:00
|
|
|
TotalMemory uint64 `json:"total_memory,omitempty"`
|
|
|
|
FreeMemory uint64 `json:"free_memory,omitempty"`
|
2024-10-15 11:36:08 -07:00
|
|
|
FreeSwap uint64 `json:"free_swap,omitempty"` // TODO split this out for system only
|
2023-12-22 15:43:31 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Beginning of an `ollama info` command
|
2024-10-15 11:36:08 -07:00
|
|
|
type GpuInfo struct { // TODO better name maybe "InferenceProcessor"?
|
2023-12-22 15:43:31 -08:00
|
|
|
memInfo
|
|
|
|
Library string `json:"library,omitempty"`
|
2023-11-29 11:00:37 -08:00
|
|
|
|
2024-01-05 12:13:08 -08:00
|
|
|
// Optional variant to select (e.g. versions, cpu feature flags)
|
2024-05-30 21:54:07 -07:00
|
|
|
Variant string `json:"variant"`
|
2024-01-05 12:13:08 -08:00
|
|
|
|
2024-03-18 10:45:22 +01:00
|
|
|
// MinimumMemory represents the minimum memory required to use the GPU
|
2024-04-05 14:50:38 -07:00
|
|
|
MinimumMemory uint64 `json:"-"`
|
2024-03-18 10:45:22 +01:00
|
|
|
|
2024-03-30 09:50:05 -07:00
|
|
|
// Any extra PATH/LD_LIBRARY_PATH dependencies required for the Library to operate properly
|
2024-11-12 10:31:52 -08:00
|
|
|
DependencyPath []string `json:"lib_path,omitempty"`
|
2024-03-30 09:50:05 -07:00
|
|
|
|
2024-05-31 16:15:21 -07:00
|
|
|
// Extra environment variables specific to the GPU as list of [key,value]
|
|
|
|
EnvWorkarounds [][2]string `json:"envs,omitempty"`
|
|
|
|
|
2024-06-19 13:35:38 -07:00
|
|
|
// Set to true if we can NOT reliably discover FreeMemory. A value of true indicates
|
|
|
|
// the FreeMemory is best effort, and may over or under report actual memory usage
|
|
|
|
// False indicates FreeMemory can generally be trusted on this GPU
|
|
|
|
UnreliableFreeMemory bool
|
|
|
|
|
2024-03-30 09:50:05 -07:00
|
|
|
// GPU information
|
2024-05-07 14:54:26 -07:00
|
|
|
ID string `json:"gpu_id"` // string to use for selection of this specific GPU
|
|
|
|
Name string `json:"name"` // user friendly name if available
|
|
|
|
Compute string `json:"compute"` // Compute Capability or gfx
|
|
|
|
|
|
|
|
// Driver Information - TODO no need to put this on each GPU
|
|
|
|
DriverMajor int `json:"driver_major,omitempty"`
|
|
|
|
DriverMinor int `json:"driver_minor,omitempty"`
|
2024-03-30 09:50:05 -07:00
|
|
|
|
|
|
|
// TODO other performance capability info to help in scheduling decisions
|
2023-11-29 11:00:37 -08:00
|
|
|
}
|
2024-02-11 14:50:06 -08:00
|
|
|
|
build: Make target improvements (#7499)
* llama: wire up builtin runner
This adds a new entrypoint into the ollama CLI to run the cgo built runner.
On Mac arm64, this will have GPU support, but on all other platforms it will
be the lowest common denominator CPU build. After we fully transition
to the new Go runners more tech-debt can be removed and we can stop building
the "default" runner via make and rely on the builtin always.
* build: Make target improvements
Add a few new targets and help for building locally.
This also adjusts the runner lookup to favor local builds, then
runners relative to the executable, and finally payloads.
* Support customized CPU flags for runners
This implements a simplified custom CPU flags pattern for the runners.
When built without overrides, the runner name contains the vector flag
we check for (AVX) to ensure we don't try to run on unsupported systems
and crash. If the user builds a customized set, we omit the naming
scheme and don't check for compatibility. This avoids checking
requirements at runtime, so that logic has been removed as well. This
can be used to build GPU runners with no vector flags, or CPU/GPU
runners with additional flags (e.g. AVX512) enabled.
* Use relative paths
If the user checks out the repo in a path that contains spaces, make gets
really confused so use relative paths for everything in-repo to avoid breakage.
* Remove payloads from main binary
* install: clean up prior libraries
This removes support for v0.3.6 and older versions (before the tar bundle)
and ensures we clean up prior libraries before extracting the bundle(s).
Without this change, runners and dependent libraries could leak when we
update and lead to subtle runtime errors.
2024-12-10 09:47:19 -08:00
|
|
|
func (gpu GpuInfo) RunnerName() string {
|
|
|
|
if gpu.Variant != "" {
|
|
|
|
return gpu.Library + "_" + gpu.Variant
|
|
|
|
}
|
|
|
|
return gpu.Library
|
|
|
|
}
|
|
|
|
|
2024-05-15 15:13:16 -07:00
|
|
|
type CPUInfo struct {
|
|
|
|
GpuInfo
|
2024-10-15 11:36:08 -07:00
|
|
|
CPUs []CPU
|
|
|
|
}
|
|
|
|
|
|
|
|
// CPU type represents a CPU Package occupying a socket
|
|
|
|
type CPU struct {
|
|
|
|
ID string `cpuinfo:"processor"`
|
|
|
|
VendorID string `cpuinfo:"vendor_id"`
|
|
|
|
ModelName string `cpuinfo:"model name"`
|
|
|
|
CoreCount int
|
|
|
|
EfficiencyCoreCount int // Performance = CoreCount - Efficiency
|
|
|
|
ThreadCount int
|
2024-05-15 15:13:16 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
type CudaGPUInfo struct {
|
|
|
|
GpuInfo
|
2024-06-13 20:46:14 -07:00
|
|
|
OSOverhead uint64 // Memory overhead between the driver library and management library
|
|
|
|
index int //nolint:unused,nolintlint
|
|
|
|
computeMajor int //nolint:unused,nolintlint
|
|
|
|
computeMinor int //nolint:unused,nolintlint
|
2024-05-15 15:13:16 -07:00
|
|
|
}
|
|
|
|
type CudaGPUInfoList []CudaGPUInfo
|
|
|
|
|
|
|
|
type RocmGPUInfo struct {
|
|
|
|
GpuInfo
|
2024-06-05 12:07:20 -07:00
|
|
|
usedFilepath string //nolint:unused,nolintlint
|
|
|
|
index int //nolint:unused,nolintlint
|
2024-05-15 15:13:16 -07:00
|
|
|
}
|
|
|
|
type RocmGPUInfoList []RocmGPUInfo
|
|
|
|
|
|
|
|
type OneapiGPUInfo struct {
|
|
|
|
GpuInfo
|
2024-06-05 12:07:20 -07:00
|
|
|
driverIndex int //nolint:unused,nolintlint
|
|
|
|
gpuIndex int //nolint:unused,nolintlint
|
2024-05-15 15:13:16 -07:00
|
|
|
}
|
|
|
|
type OneapiGPUInfoList []OneapiGPUInfo
|
|
|
|
|
2024-03-30 09:50:05 -07:00
|
|
|
type GpuInfoList []GpuInfo
|
|
|
|
|
2024-10-14 16:26:45 -07:00
|
|
|
type UnsupportedGPUInfo struct {
|
|
|
|
GpuInfo
|
|
|
|
Reason string `json:"reason"`
|
|
|
|
}
|
|
|
|
|
2024-03-30 09:50:05 -07:00
|
|
|
// Split up the set of gpu info's by Library and variant
|
|
|
|
func (l GpuInfoList) ByLibrary() []GpuInfoList {
|
|
|
|
resp := []GpuInfoList{}
|
|
|
|
libs := []string{}
|
|
|
|
for _, info := range l {
|
|
|
|
found := false
|
|
|
|
requested := info.Library
|
2025-01-29 15:03:38 -08:00
|
|
|
if info.Variant != "" {
|
2024-05-30 21:54:07 -07:00
|
|
|
requested += "_" + info.Variant
|
2024-03-30 09:50:05 -07:00
|
|
|
}
|
|
|
|
for i, lib := range libs {
|
|
|
|
if lib == requested {
|
|
|
|
resp[i] = append(resp[i], info)
|
|
|
|
found = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !found {
|
2024-08-23 15:11:56 -07:00
|
|
|
libs = append(libs, requested)
|
2024-03-30 09:50:05 -07:00
|
|
|
resp = append(resp, []GpuInfo{info})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return resp
|
2024-02-11 14:50:06 -08:00
|
|
|
}
|
2024-03-30 09:50:05 -07:00
|
|
|
|
2024-05-07 14:54:26 -07:00
|
|
|
// Report the GPU information into the log an Info level
|
|
|
|
func (l GpuInfoList) LogDetails() {
|
|
|
|
for _, g := range l {
|
|
|
|
slog.Info("inference compute",
|
|
|
|
"id", g.ID,
|
|
|
|
"library", g.Library,
|
2024-06-19 09:36:30 -07:00
|
|
|
"variant", g.Variant,
|
2024-05-07 14:54:26 -07:00
|
|
|
"compute", g.Compute,
|
|
|
|
"driver", fmt.Sprintf("%d.%d", g.DriverMajor, g.DriverMinor),
|
|
|
|
"name", g.Name,
|
|
|
|
"total", format.HumanBytes2(g.TotalMemory),
|
|
|
|
"available", format.HumanBytes2(g.FreeMemory),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-30 09:50:05 -07:00
|
|
|
// Sort by Free Space
|
|
|
|
type ByFreeMemory []GpuInfo
|
|
|
|
|
|
|
|
func (a ByFreeMemory) Len() int { return len(a) }
|
|
|
|
func (a ByFreeMemory) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
|
|
|
|
func (a ByFreeMemory) Less(i, j int) bool { return a[i].FreeMemory < a[j].FreeMemory }
|
2024-05-15 15:13:16 -07:00
|
|
|
|
2024-10-14 16:26:45 -07:00
|
|
|
type SystemInfo struct {
|
|
|
|
System CPUInfo `json:"system"`
|
|
|
|
GPUs []GpuInfo `json:"gpus"`
|
|
|
|
UnsupportedGPUs []UnsupportedGPUInfo `json:"unsupported_gpus"`
|
|
|
|
DiscoveryErrors []string `json:"discovery_errors"`
|
|
|
|
}
|
2024-10-15 11:36:08 -07:00
|
|
|
|
|
|
|
// Return the optimal number of threads to use for inference
|
|
|
|
func (si SystemInfo) GetOptimalThreadCount() int {
|
|
|
|
if len(si.System.CPUs) == 0 {
|
|
|
|
return 0
|
|
|
|
}
|
2024-10-30 15:05:45 -07:00
|
|
|
|
|
|
|
coreCount := 0
|
|
|
|
for _, c := range si.System.CPUs {
|
|
|
|
coreCount += c.CoreCount - c.EfficiencyCoreCount
|
|
|
|
}
|
|
|
|
|
|
|
|
return coreCount
|
2024-10-15 11:36:08 -07:00
|
|
|
}
|
2024-12-04 10:57:19 +11:00
|
|
|
|
|
|
|
// For each GPU, check if it does NOT support flash attention
|
|
|
|
func (l GpuInfoList) FlashAttentionSupported() bool {
|
|
|
|
for _, gpu := range l {
|
|
|
|
supportsFA := gpu.Library == "metal" ||
|
|
|
|
(gpu.Library == "cuda" && gpu.DriverMajor >= 7) ||
|
|
|
|
gpu.Library == "rocm"
|
|
|
|
|
|
|
|
if !supportsFA {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|