-
Notifications
You must be signed in to change notification settings - Fork 79
Implement basic memory estimation in scheduler #106
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
f90e470
Implement basic memory estimation in scheduler
d559e1b
VRAM size getter for linux
9e6e41f
VRAM size getter for windows
3b42bc2
Move VRAM size getters to a separate package
517484b
Use nv-gpu-info on Windows to get VRAM size
f99d4a2
gpuinfo: Release Metal device handle in VRAM size getter
a3b83a8
inference, gpuinfo: Limit allowed models to 1 on windows/arm64 for now
ca187f9
inference: Keep track of RAM allocated by runners
7d39c76
inference: Fix failing llama_config unit tests
ac9da88
inference: Fix nv-gpu-info path and wrap errors
1d066f2
gpuinfo: Use go:build instead of obsolete +build
cd5f08d
inference: Always return 1 as VRAM size on win/arm64
b6d86e5
inference: Fallback behaviour if reading RAM/VRAM size fails
2e872f9
inference: Fix typo in log
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| package gpuinfo | ||
|
|
||
| type GPUInfo struct { | ||
| // modelRuntimeInstallPath is the location where DMR installed it's llama-server | ||
| // and accompanying tools | ||
| modelRuntimeInstallPath string | ||
| } | ||
|
|
||
| func New(modelRuntimeInstallPath string) *GPUInfo { | ||
| return &GPUInfo{ | ||
| modelRuntimeInstallPath: modelRuntimeInstallPath, | ||
| } | ||
| } | ||
|
|
||
| func (g *GPUInfo) GetVRAMSize() (uint64, error) { | ||
| return getVRAMSize(g.modelRuntimeInstallPath) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| package gpuinfo | ||
|
|
||
| /* | ||
| #cgo LDFLAGS: -framework Metal | ||
| #include "metal.h" | ||
| */ | ||
| import "C" | ||
| import "errors" | ||
|
|
||
| // getVRAMSize returns total system GPU memory in bytes | ||
| func getVRAMSize(_ string) (uint64, error) { | ||
| vramSize := C.getVRAMSize() | ||
| if vramSize == 0 { | ||
| return 0, errors.New("could not get metal VRAM size") | ||
| } | ||
| return uint64(vramSize), nil | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| package gpuinfo | ||
|
|
||
| /* | ||
| #cgo LDFLAGS: -ldl | ||
| #include "nvidia.h" | ||
| */ | ||
| import "C" | ||
| import "errors" | ||
|
|
||
| // getVRAMSize returns total system GPU memory in bytes | ||
| func getVRAMSize(_ string) (uint64, error) { | ||
| vramSize := C.getVRAMSize() | ||
| if vramSize == 0 { | ||
| return 0, errors.New("could not get nvidia VRAM size") | ||
| } | ||
| return uint64(vramSize), nil | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| package gpuinfo | ||
|
|
||
| import ( | ||
| "bufio" | ||
| "context" | ||
| "errors" | ||
| "os/exec" | ||
| "path/filepath" | ||
| "runtime" | ||
| "strconv" | ||
| "strings" | ||
| "time" | ||
| ) | ||
|
|
||
| // getVRAMSize returns total system GPU memory in bytes | ||
| func getVRAMSize(modelRuntimeInstallPath string) (uint64, error) { | ||
| if runtime.GOARCH == "arm64" { | ||
| // TODO(p1-0tr): For now, on windows/arm64, stick to the old behaviour. This will | ||
| // require backend.GetRequiredMemoryForModel to return 1 as well. | ||
| return 1, nil | ||
| } | ||
|
|
||
| nvGPUInfoBin := filepath.Join(modelRuntimeInstallPath, "bin", "com.docker.nv-gpu-info.exe") | ||
|
|
||
| ctx, _ := context.WithTimeout(context.Background(), 30*time.Second) | ||
| cmd := exec.CommandContext(ctx, nvGPUInfoBin) | ||
| out, err := cmd.CombinedOutput() | ||
| if err != nil { | ||
| return 0, err | ||
| } | ||
| sc := bufio.NewScanner(strings.NewReader(string(out))) | ||
| for sc.Scan() { | ||
| vram, found := strings.CutPrefix(sc.Text(), "GPU[0]: dedicated memory:") | ||
| if found { | ||
| vram = strings.TrimSpace(vram) | ||
| return strconv.ParseUint(vram, 10, 64) | ||
| } | ||
| } | ||
| return 0, errors.New("unexpected nv-gpu-info output format") | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| //go:build darwin | ||
|
|
||
| #include <stddef.h> | ||
|
|
||
| size_t getVRAMSize(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| //go:build darwin | ||
|
|
||
| #include <Metal/Metal.h> | ||
|
|
||
| #include "metal.h" | ||
|
|
||
| size_t getVRAMSize() { | ||
| id<MTLDevice> device = MTLCreateSystemDefaultDevice(); | ||
| if (device) { | ||
| size_t vramsz = [device recommendedMaxWorkingSetSize]; | ||
| [device release]; | ||
| return vramsz; | ||
| } | ||
| return 0; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| //go:build linux | ||
|
|
||
| #include "nvidia.h" | ||
|
|
||
| typedef enum { | ||
| NVML_SUCCESS = 0 | ||
| } nvmlReturn_t; | ||
|
|
||
| typedef struct { | ||
| unsigned long long total; | ||
| unsigned long long free; | ||
| unsigned long long used; | ||
| } nvmlMemory_t; | ||
|
|
||
| typedef void* nvmlDevice_t; | ||
|
|
||
| size_t getVRAMSize() { | ||
| void* handle; | ||
| nvmlReturn_t (*nvmlInit)(void); | ||
| nvmlReturn_t (*nvmlShutdown)(void); | ||
| nvmlReturn_t (*nvmlDeviceGetHandleByIndex)(unsigned int index, nvmlDevice_t* device); | ||
| nvmlReturn_t (*nvmlDeviceGetMemoryInfo)(nvmlDevice_t device, nvmlMemory_t* memory); | ||
|
|
||
| nvmlReturn_t result; | ||
| nvmlDevice_t device; | ||
| nvmlMemory_t memory; | ||
|
|
||
| // Try to load libnvidia-ml.so.1 first, then fallback to libnvidia-ml.so | ||
| handle = dlopen("libnvidia-ml.so.1", RTLD_LAZY); | ||
| if (!handle) { | ||
| handle = dlopen("libnvidia-ml.so", RTLD_LAZY); | ||
| if (!handle) { | ||
| return 0; | ||
| } | ||
| } | ||
|
|
||
| // Load required functions | ||
| nvmlInit = dlsym(handle, "nvmlInit"); | ||
| nvmlShutdown = dlsym(handle, "nvmlShutdown"); | ||
| nvmlDeviceGetHandleByIndex = dlsym(handle, "nvmlDeviceGetHandleByIndex"); | ||
| nvmlDeviceGetMemoryInfo = dlsym(handle, "nvmlDeviceGetMemoryInfo"); | ||
|
|
||
| if (!nvmlInit || !nvmlShutdown || !nvmlDeviceGetHandleByIndex || !nvmlDeviceGetMemoryInfo) { | ||
| dlclose(handle); | ||
| return 0; | ||
| } | ||
|
|
||
| result = nvmlInit(); | ||
| if (result != NVML_SUCCESS) { | ||
| dlclose(handle); | ||
| return 0; | ||
| } | ||
|
|
||
| result = nvmlDeviceGetHandleByIndex(0, &device); | ||
| if (result != NVML_SUCCESS) { | ||
| nvmlShutdown(); | ||
| dlclose(handle); | ||
| return 0; | ||
| } | ||
|
|
||
| result = nvmlDeviceGetMemoryInfo(device, &memory); | ||
| if (result != NVML_SUCCESS) { | ||
| nvmlShutdown(); | ||
| dlclose(handle); | ||
| return 0; | ||
| } | ||
|
|
||
| nvmlShutdown(); | ||
| dlclose(handle); | ||
| return memory.total; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| //go:build linux | ||
|
|
||
| #include <stddef.h> | ||
| #include <dlfcn.h> | ||
|
|
||
| size_t getVRAMSize(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.