Repository navigation
fix(amd): read ROCm VRAM through amd-smi so the backend keeps no HIP context - #8669
Conversation
9cc50a2 to
01fe57c
Compare
|
Rebased onto current main as #7670 landed on the same function, adding The gate is now a single One deliberate choice worth a look: when the gate drops every card, the amd-smi branch returns The gate itself costs nothing: Also worth correcting something I said earlier in this PR. The 9 |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 01fe57cd6a
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| visible = LlamaCppBackend._resolve_visible_physical_ids() | ||
| if visible is not None: | ||
| allowed = set(visible) | ||
| vram = {idx: v for idx, v in vram.items() if idx in allowed} |
There was a problem hiding this comment.
Preserve UUID-based ROCm visibility masks
When ROCm visibility is expressed with a UUID token, _resolve_visible_physical_ids() returns None just as it does for an unset mask; _visibility_mask_is_unmappable() explicitly recognizes this case. Treating that result as “all GPUs” accepts amd-smi's host-wide physical inventory, and automatic placement can later rewrite the child visibility mask with those numeric IDs, exposing GPUs the inherited UUID mask hid. Fall back to the torch probe when an active mask is unmappable rather than treating every None as unset.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Already addressed in fd0348c, before this review was posted. The UUID mask case now rejects the amd-smi result rather than reading an unresolvable mask as no mask.
| if rocm_gpus: | ||
| return rocm_gpus |
There was a problem hiding this comment.
Reject partial amd-smi memory results
On a mixed AMD host where one amd-smi entry lacks usable used or total memory, get_gpu_vram_mib() skips that entry but still returns the valid rows. This truthy partial result suppresses the torch fallback, so the affected GPU disappears from fitting and automatic placement even though the previous torch-only path enumerated it. Accept the amd-smi result only when it covers every visible device, or use the fallback to fill incomplete results.
Useful? React with 👍 / 👎.
…dict for PR #8669 amd-smi reports only the dedicated VRAM carve-out on a unified-memory APU, while HIP reports the far larger GTT pool the models actually run in. On a 128GiB Strix Halo with an 8GiB BIOS carve-out that is 8GiB against ~100GiB, so the new branch would have handed the GGUF fitter the slice and refused models that fit today. The branch now declines the whole host when any visible device is a unified-memory APU: discrete ROCm cards keep the context saving, APU hosts keep byte-identical pre-PR numbers, and the two branches cannot disagree because only one ever answers. Reconstructing the pool from total_gtt was the other option and was rejected: HIP's mem_get_info on an APU is not documented as equal to GTT alone, and the older vram / fb_memory_usage shapes carry no GTT fields, so the two branches would drift, which is what _rocm_arch_gate_keep was unified to prevent. Separately, _gpu_entries' documented bare single-GPU dict fallback was dead: data.get("gpu", [data]) returns the numeric id for that shape and enumerate(int) raises. An envelope key now only counts when its value is really a list, which also closes enumerate(None) on {"gpus": null}.
|
Two more defects closed in amd-smi ids are not HIP ids. I had been keying rows by amd-smi A UUID mask read as no mask. On correlate versus decline I did both, split by whether the mapping is provable. Blanket declining would gut the branch on every multi-GPU ROCm host; blanket correlation is not available, since One side effect handled: an old CLI rejecting A partial result suppressed the fallback. 10 tests in One trap worth recording for anyone else testing this area, same shape as #8662: the probe asks whether a mask is set before asking what it resolves to, so a shell |
|
@codex review |
…dict for PR #8669 amd-smi reports only the dedicated VRAM carve-out on a unified-memory APU, while HIP reports the far larger GTT pool the models actually run in. On a 128GiB Strix Halo with an 8GiB BIOS carve-out that is 8GiB against ~100GiB, so the new branch would have handed the GGUF fitter the slice and refused models that fit today. The branch now declines the whole host when any visible device is a unified-memory APU: discrete ROCm cards keep the context saving, APU hosts keep byte-identical pre-PR numbers, and the two branches cannot disagree because only one ever answers. Reconstructing the pool from total_gtt was the other option and was rejected: HIP's mem_get_info on an APU is not documented as equal to GTT alone, and the older vram / fb_memory_usage shapes carry no GTT fields, so the two branches would drift, which is what _rocm_arch_gate_keep was unified to prevent. Separately, _gpu_entries' documented bare single-GPU dict fallback was dead: data.get("gpu", [data]) returns the numeric id for that shape and enumerate(int) raises. An envelope key now only counts when its value is really a list, which also closes enumerate(None) on {"gpus": null}.
593a940 to
77f1e64
Compare
|
Rebased onto main now that #8677 has landed. Both touch ROCm APU capacity from opposite directions, so re-verified against the merged tree rather than the old base:
The two APU paths do not overlap: #8677's |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 77f1e64ac3
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| vram = {hip_by_gpu[_g]: _v for _g, _v in vram.items()} | ||
| # amd-smi enumerates every card regardless of the mask, so drop the ones | ||
| # this process cannot see. No mask means all of them. | ||
| visible = set(mask) if mask is not None else set(hip_by_gpu.values()) |
There was a problem hiding this comment.
Keep ROCR masks in ROCr index space
When ROCR_VISIBLE_DEVICES is the active numeric mask on a host where amd-smi list -e reports a non-identity amd-smi-to-HIP mapping, mask contains ROCr agent IDs but this line compares them with the HIP IDs produced above. For example, with mapping {gpu 0: HIP 1, gpu 1: HIP 0} and ROCR_VISIBLE_DEVICES=0, the torch path measures agent 0, while this branch selects the VRAM row for HIP 0 (gpu 1), so fitting can use the wrong card's capacity and subsequently pin the wrong device. Preserve the mask's source/index space, translate ROCr IDs separately, or defer this branch when a ROCR mask is active.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
The premise that ROCr agent index differs from HIP device index does not hold. clr/hipamd/src/hip_context.cpp builds g_devices by straight iteration over amd::Device::getDevices(CL_DEVICE_TYPE_GPU, false) with i as the HIP ordinal, and there is no PCI-bus sort in rocdevice.cpp (unlike CUDA's default), so HIP device i is ROCr GPU agent i. ROCr's agent order is the rank of KFD topology nodes with NumFComputeCores != 0 in ascending node id (amd_topology.cpp BuildTopology), which is the same ranking amdsmi's hip_id = node_id - smallest_node_id computes. It is also unreachable as a stacked mask here: _active_gpu_visibility_mask reads ROCR only when HIP_VISIBLE_DEVICES is unset. And it is not PR-specific: _resolve_visible_physical_ids has treated ROCR entries as physical ids since before this PR, and the torch branch and _emit_child_gpu_visibility use it identically, so changing only this branch would make the two disagree. On the 96 GiB versus 16 GiB example given, the totals check added above now declines rather than misreporting.
84f0b0c to
584f92f
Compare
…dict for PR #8669 amd-smi reports only the dedicated VRAM carve-out on a unified-memory APU, while HIP reports the far larger GTT pool the models actually run in. On a 128GiB Strix Halo with an 8GiB BIOS carve-out that is 8GiB against ~100GiB, so the new branch would have handed the GGUF fitter the slice and refused models that fit today. The branch now declines the whole host when any visible device is a unified-memory APU: discrete ROCm cards keep the context saving, APU hosts keep byte-identical pre-PR numbers, and the two branches cannot disagree because only one ever answers. Reconstructing the pool from total_gtt was the other option and was rejected: HIP's mem_get_info on an APU is not documented as equal to GTT alone, and the older vram / fb_memory_usage shapes carry no GTT fields, so the two branches would drift, which is what _rocm_arch_gate_keep was unified to prevent. Separately, _gpu_entries' documented bare single-GPU dict fallback was dead: data.get("gpu", [data]) returns the numeric id for that shape and enumerate(int) raises. An envelope key now only counts when its value is really a list, which also closes enumerate(None) on {"gpus": null}.
GPU_DEVICE_ORDINAL is a ROCm visibility variable that _resolve_visible_physical_ids does not read, so the branch took no mask from a process that had one. Defer to torch while it is set, the way hardware.py::_rocm_visibility_mask_active gates the system-wide overlay. Compare the visible device count against hipGetDeviceCount as well: a device-cgroup container given one /dev/dri/renderD* node and no env var leaves amd-smi enumerating the whole host, and a row amd-smi omitted leaves a single-GPU shape the id-mapping shortcut accepts as a one-card host. Compare the totals too, with the same 10% margin the System tab's overlay uses. An APU _rocm_classify_unified_memory cannot name (a gfx1103 Phoenix wheel with no is_integrated flag) has amd-smi reporting the BIOS carve-out where HIP reports the GTT pool, and a partition reads the other way round. Devices torch cannot describe fail open.
584f92f to
5f4f53a
Compare
|
Codex Review: Didn't find any major issues. Hooray! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
…dict for PR #8669 amd-smi reports only the dedicated VRAM carve-out on a unified-memory APU, while HIP reports the far larger GTT pool the models actually run in. On a 128GiB Strix Halo with an 8GiB BIOS carve-out that is 8GiB against ~100GiB, so the new branch would have handed the GGUF fitter the slice and refused models that fit today. The branch now declines the whole host when any visible device is a unified-memory APU: discrete ROCm cards keep the context saving, APU hosts keep byte-identical pre-PR numbers, and the two branches cannot disagree because only one ever answers. Reconstructing the pool from total_gtt was the other option and was rejected: HIP's mem_get_info on an APU is not documented as equal to GTT alone, and the older vram / fb_memory_usage shapes carry no GTT fields, so the two branches would drift, which is what _rocm_arch_gate_keep was unified to prevent. Separately, _gpu_entries' documented bare single-GPU dict fallback was dead: data.get("gpu", [data]) returns the numeric id for that shape and enumerate(int) raises. An envelope key now only counts when its value is really a list, which also closes enumerate(None) on {"gpus": null}.
7e3080e to
44609bc
Compare
GPU_DEVICE_ORDINAL is a ROCm visibility variable that _resolve_visible_physical_ids does not read, so the branch took no mask from a process that had one. Defer to torch while it is set, the way hardware.py::_rocm_visibility_mask_active gates the system-wide overlay. Compare the visible device count against hipGetDeviceCount as well: a device-cgroup container given one /dev/dri/renderD* node and no env var leaves amd-smi enumerating the whole host, and a row amd-smi omitted leaves a single-GPU shape the id-mapping shortcut accepts as a one-card host. Compare the totals too, with the same 10% margin the System tab's overlay uses. An APU _rocm_classify_unified_memory cannot name (a gfx1103 Phoenix wheel with no is_integrated flag) has amd-smi reporting the BIOS carve-out where HIP reports the GTT pool, and a partition reads the other way round. Devices torch cannot describe fail open.
|
Rebased onto current main (eee14f2). The branch had picked up two Backend suite on the rebased head: 121 passed, and 121 passed again under CUDA_VISIBLE_DEVICES=-1, so the fixtures declare HIP's inventory rather than inheriting the host's. |
|
Codex Review: Didn't find any major issues. What shall we delve into next? Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
…dict for PR #8669 amd-smi reports only the dedicated VRAM carve-out on a unified-memory APU, while HIP reports the far larger GTT pool the models actually run in. On a 128GiB Strix Halo with an 8GiB BIOS carve-out that is 8GiB against ~100GiB, so the new branch would have handed the GGUF fitter the slice and refused models that fit today. The branch now declines the whole host when any visible device is a unified-memory APU: discrete ROCm cards keep the context saving, APU hosts keep byte-identical pre-PR numbers, and the two branches cannot disagree because only one ever answers. Reconstructing the pool from total_gtt was the other option and was rejected: HIP's mem_get_info on an APU is not documented as equal to GTT alone, and the older vram / fb_memory_usage shapes carry no GTT fields, so the two branches would drift, which is what _rocm_arch_gate_keep was unified to prevent. Separately, _gpu_entries' documented bare single-GPU dict fallback was dead: data.get("gpu", [data]) returns the numeric id for that shape and enumerate(int) raises. An envelope key now only counts when its value is really a list, which also closes enumerate(None) on {"gpus": null}.
GPU_DEVICE_ORDINAL is a ROCm visibility variable that _resolve_visible_physical_ids does not read, so the branch took no mask from a process that had one. Defer to torch while it is set, the way hardware.py::_rocm_visibility_mask_active gates the system-wide overlay. Compare the visible device count against hipGetDeviceCount as well: a device-cgroup container given one /dev/dri/renderD* node and no env var leaves amd-smi enumerating the whole host, and a row amd-smi omitted leaves a single-GPU shape the id-mapping shortcut accepts as a one-card host. Compare the totals too, with the same 10% margin the System tab's overlay uses. An APU _rocm_classify_unified_memory cannot name (a gfx1103 Phoenix wheel with no is_integrated flag) has amd-smi reporting the BIOS carve-out where HIP reports the GTT pool, and a partition reads the other way round. Devices torch cannot describe fail open.
eee14f2 to
d421f29
Compare
…dict for PR #8669 amd-smi reports only the dedicated VRAM carve-out on a unified-memory APU, while HIP reports the far larger GTT pool the models actually run in. On a 128GiB Strix Halo with an 8GiB BIOS carve-out that is 8GiB against ~100GiB, so the new branch would have handed the GGUF fitter the slice and refused models that fit today. The branch now declines the whole host when any visible device is a unified-memory APU: discrete ROCm cards keep the context saving, APU hosts keep byte-identical pre-PR numbers, and the two branches cannot disagree because only one ever answers. Reconstructing the pool from total_gtt was the other option and was rejected: HIP's mem_get_info on an APU is not documented as equal to GTT alone, and the older vram / fb_memory_usage shapes carry no GTT fields, so the two branches would drift, which is what _rocm_arch_gate_keep was unified to prevent. Separately, _gpu_entries' documented bare single-GPU dict fallback was dead: data.get("gpu", [data]) returns the numeric id for that shape and enumerate(int) raises. An envelope key now only counts when its value is really a list, which also closes enumerate(None) on {"gpus": null}.
GPU_DEVICE_ORDINAL is a ROCm visibility variable that _resolve_visible_physical_ids does not read, so the branch took no mask from a process that had one. Defer to torch while it is set, the way hardware.py::_rocm_visibility_mask_active gates the system-wide overlay. Compare the visible device count against hipGetDeviceCount as well: a device-cgroup container given one /dev/dri/renderD* node and no env var leaves amd-smi enumerating the whole host, and a row amd-smi omitted leaves a single-GPU shape the id-mapping shortcut accepts as a one-card host. Compare the totals too, with the same 10% margin the System tab's overlay uses. An APU _rocm_classify_unified_memory cannot name (a gfx1103 Phoenix wheel with no is_integrated flag) has amd-smi reporting the BIOS carve-out where HIP reports the GTT pool, and a partition reads the other way round. Devices torch cannot describe fail open.
d421f29 to
304a0ea
Compare
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 304a0eacda
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| None means "not provable", and the branch then defers to torch, which | ||
| enumerates in HIP's space by construction. | ||
| """ | ||
| if len(enumerated) == 1 and mask in (None, [0]): |
There was a problem hiding this comment.
Verify the inventory before assuming HIP device 0
When amd-smi metric skips all but one row on a multi-GPU host and the active mask is unset or [0], this shortcut treats the surviving row as proof that the host has one card and remaps it to HIP 0 without consulting list -e. If the surviving row is actually another card, the later coverage and HIP-count checks can still pass—especially with equal-total cards—so fitting uses the wrong GPU's free memory and may overcommit the selected device. Fresh evidence beyond the earlier partial-result report is this current singleton shortcut, which bypasses the newly added completeness mapping precisely when a partial response has one row.
Useful? React with 👍 / 👎.
torch.cuda.mem_get_info creates a CUDA/HIP primary context that the process never releases: 612 MiB measured on a B200 with CUDA 13, 692-712 MiB reported elsewhere. get_device_properties and get_device_capability do not, and agree with mem_get_info on total memory exactly. LlamaCppBackend._get_gpu_memory asks nvidia-smi first, so NVIDIA hosts were already clean. ROCm hosts, and NVIDIA hosts without nvidia-smi on PATH, fell straight through to torch, so a backend serving GGUF models held ~700 MiB for a number it only reads, on a card whose models run in a llama-server child that then cannot use it. Two docstrings promised the probe was torch-free. amd-smi answers the same question from a subprocess. The new branch sits between nvidia-smi and torch, reuses utils/hardware/amd.py's runner, unit parsing and circuit breaker, honours the visibility mask, and gives a unified-memory APU the same host reserve and total 0 as the torch branch. The torch fallback stays for hosts with no smi tool. Callers read an empty result as no GPU (_resolve_auto picks a backend from it, _gpu_available gates the embedder, the loader fit drops to CPU), so an unknown there would silently move inference off the GPU; the context is the lesser cost. Parity on GPU 5, amd-smi output synthesised from the live nvidia-smi reading of the same card: free 179165 MiB and total 183359 MiB both branches, 0 MiB context on the new path against 612 MiB on the torch path. NVIDIA numbers are untouched, and the branch is inert off ROCm. Rebased over #7670, which added for_llama_server and the ROCm arch gate to the same function. The new branch returns before the gate ran, so it would have put #7624 back for llama-server placement. _rocm_arch_gate_keep now holds the gate, and both branches call it, so the two cannot drift. It costs no context of its own: get_device_properties measured 0MiB against mem_get_info's 620MiB on GPU 5.
…dict for PR #8669 amd-smi reports only the dedicated VRAM carve-out on a unified-memory APU, while HIP reports the far larger GTT pool the models actually run in. On a 128GiB Strix Halo with an 8GiB BIOS carve-out that is 8GiB against ~100GiB, so the new branch would have handed the GGUF fitter the slice and refused models that fit today. The branch now declines the whole host when any visible device is a unified-memory APU: discrete ROCm cards keep the context saving, APU hosts keep byte-identical pre-PR numbers, and the two branches cannot disagree because only one ever answers. Reconstructing the pool from total_gtt was the other option and was rejected: HIP's mem_get_info on an APU is not documented as equal to GTT alone, and the older vram / fb_memory_usage shapes carry no GTT fields, so the two branches would drift, which is what _rocm_arch_gate_keep was unified to prevent. Separately, _gpu_entries' documented bare single-GPU dict fallback was dead: data.get("gpu", [data]) returns the numeric id for that shape and enumerate(int) raises. An envelope key now only counts when its value is really a list, which also closes enumerate(None) on {"gpus": null}.
…device amd-smi numbers its devices in discovery order over its own KFD view; HIP numbers them by KFD node id (amdsmi computes hip_id as node_id - smallest_node_id). The two agree on most hosts and not on all of them, and the branch was keying VRAM rows by amd-smi's number, comparing it against HIP/ROCR/CUDA visibility tokens and then pinning the child with it. On a host where they differ, the larger card's memory lands on the smaller card's HIP id and a load that fit before this PR can OOM. amd-smi list -e (ROCm 6.4.0+, amdsmi_get_gpu_enumeration_info) is the mapping AMD publishes for exactly this. Read it and re-key, and decline when it is not readable: an older CLI rejects -e, hip_id reads N/A when the library cannot reach the device's KFD node, and identity is then an assumption rather than a fact. A single-GPU host skips the call, since one card on the host forces the mapping, so the ROCm desktops this branch mostly runs on keep the context saving on any amd-smi version. That -e failure is kept out of the circuit breaker: an old CLI is not an amd-smi outage, and three of them would disable the utilization poller too. _resolve_visible_physical_ids returns None both for "no mask" and for a mask it cannot parse, and the branch read the second as the first. ROCr accepts UUID tokens (ROCR_VISIBLE_DEVICES=GPU-<uuid>), and amd-smi ignores the mask entirely, so a deliberately hidden card was being offered for automatic ranking. Use the existing _visibility_mask_is_unmappable and defer to torch, which enumerates the masked set. Separately, get_gpu_vram_mib drops a row whose total reads 0, and what was left was a non-empty list the caller takes as the whole host. On an APU beside a dGPU that turned two devices into one, so the "fewer than 2 usable GPUs" arm switched off tensor parallelism on a host main reports as two. get_gpu_vram_report now also returns every id amd-smi enumerated, and the branch answers for every visible device or declines entirely. The existing rocm fixture only patched _resolve_visible_physical_ids and left the shell's own CUDA_VISIBLE_DEVICES in the environment, which now reads as a mask set but unresolvable; clear it there too, the same trap as #8662.
amd-smi reads the driver over sysfs and libdrm, HIP needs /dev/kfd and a
matching HSA runtime. They come apart on a container started with
--device=/dev/dri but no --device=/dev/kfd, and on a torch wheel built
against a ROCm the installed runtime does not match: amd-smi lists the card
and hipGetDeviceCount returns 0, so torch.cuda.is_available() is False while
_torch_is_rocm() stays True.
The llama-server child is a HIP process too, so it dies exactly where torch
did. Before this branch existed the torch fallback returned [] for such a
host and the load went to CPU. The amd-smi branch returns first and hands
placement a device nothing can open.
Neither existing guard catches it, since both are torch readers that fail
open: _rocm_unified_memory_gpu_ids answers set() and _rocm_arch_by_physical_id
answers {} the moment is_available() is False, so a unified-memory APU is not
even recognised as one and its dedicated carve-out is taken for the whole
pool.
The gate costs nothing this path was not already paying:
_rocm_unified_memory_gpu_ids calls is_available() and get_device_properties()
further down the same function. Neither creates a primary context (measured:
no compute-apps entry, against 612 MiB for mem_get_info), because
is_available() is hipGetDeviceCount rather than _lazy_init.
Both rocm fixtures now declare HIP reachability instead of inheriting the
host's, the same trap as #8662: unpatched they read the test machine's own
GPU and 16 of them fail on a CPU-only runner.
GPU_DEVICE_ORDINAL is a ROCm visibility variable that _resolve_visible_physical_ids does not read, so the branch took no mask from a process that had one. Defer to torch while it is set, the way hardware.py::_rocm_visibility_mask_active gates the system-wide overlay. Compare the visible device count against hipGetDeviceCount as well: a device-cgroup container given one /dev/dri/renderD* node and no env var leaves amd-smi enumerating the whole host, and a row amd-smi omitted leaves a single-GPU shape the id-mapping shortcut accepts as a one-card host. Compare the totals too, with the same 10% margin the System tab's overlay uses. An APU _rocm_classify_unified_memory cannot name (a gfx1103 Phoenix wheel with no is_integrated flag) has amd-smi reporting the BIOS carve-out where HIP reports the GTT pool, and a partition reads the other way round. Devices torch cannot describe fail open.
ROCR_VISIBLE_DEVICES filters and renumbers agents inside the ROCr runtime, and clr's Device::init then indexes HIP_VISIBLE_DEVICES (or its CUDA_VISIBLE_DEVICES twin) into the agent vector ROCr left behind, so the two compose: ROCR_VISIBLE_DEVICES=1,2 with HIP_VISIBLE_DEVICES=0 opens physical GPU 1. _active_gpu_visibility_mask returns the single highest-precedence value, which reads [0] there, and on a host whose cards share a model every later check on the amd-smi branch agrees with it: HIP opens one device, the mask names one, and the totals match. The branch then reported physical GPU 0's free VRAM for a process that can only reach GPU 1. Defer to torch instead, which enumerates in HIP's own space; composing the masks would need the physical to ROCr mapping ROCr just renumbered away. HIP and CUDA together are one layer, not two, and Windows has no ROCr layer, so neither defers.
304a0ea to
e6806cc
Compare
|
@codex review |
|
Codex Review: Didn't find any major issues. Keep it up! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
LlamaCppBackend._get_gpu_memorytriesnvidia-smifirst and falls back totorch.cuda.mem_get_info. On ROCm the first branch never answers, so every ROCm host takes the torch branch, andmem_get_infocreates a HIP primary context in the Studio backend process that nothing can give back.The models this sizing is for run in a llama-server child process, so the backend is paying a permanent context to answer a question a subprocess could have answered.
Measured
One call per process on a B200, per-PID via
nvidia-smi --query-compute-apps:torch.cuda.is_available()torch.cuda.device_count()torch.cuda.get_device_properties(0).total_memorytorch.cuda.get_device_capability(0)torch.cuda.get_device_name(0)torch.cuda.mem_get_info(0)Still 612 MiB after
empty_cache()plusgc.collect()and a 5 second wait. Reproduced through the production function rather than a synthetic one, running_get_gpu_memory()in one process and then again withPATHstripped to stand in for a host with nonvidia-smi:Note the probe distorts the number it reports: its own context eats ~612 MiB out of the free figure.
The change
The order becomes
nvidia-smi, thenamd-smi, then torch._get_gpu_memory_amd_smireuses the existing runner inutils/hardware/amd.py(_run_amd_smi, with its Windows UAC guard,shutil.whichcheck, circuit breaker,child_env_without_native_path_secretandwindows_hidden_subprocess_kwargs; noshell=True, nothing POSIX-only) and its unit parsing. Rather than duplicate, two helpers were extracted:_vram_used_total_mbout of_extract_gpu_metrics, and_gpu_entriesout ofget_visible_gpu_utilization.It honours the visibility mask, since amd-smi enumerates every card, and gives a unified-memory APU the same system RAM cap, host reserve and
total = 0the torch branch does.Torch stays as the last resort deliberately. Callers read
[]as "no GPU":_resolve_autoincore/rag/embeddings.pypicks a whole embedding backend from its truthiness,_gpu_availableinembed_llama_server.pygates the embedder onto CPU, and the loader fit drops every layer to CPU. Returning unknown there would silently move a ROCm user's inference to the CPU, so on a host with neither smi tool the behaviour is exactly what it is today, context included.Parity
amd-smi output synthesised from the live
nvidia-smireading of the same card, so free and total are ground truth:[(5, 178440, 183359)][(5, 177819, 182633)][(5, 179165, 183359)], ground truthfree=179165 total=183359[(5, 177819, 182633)]Exact free and total parity. The 0 MiB in row three also shows the shared-pool path is context free:
_rocm_unified_memory_gpu_idsran there and calledis_available()andget_device_properties()per device.Parser parity across every amd-smi shape the repo already handles:
mem_usageGiB dicts, oldvramMiB strings,fb_memory_usagebare ints, used-above-total clamped to zero, a VRAM-less entry, and a scalar array entry skipped.What was deliberately left alone
video.py:690_h3_device_capacity_bytesreadsmem_get_info()[1]for a total only, so it looks like an easy substitution. On Windows WDDM that total is the driver-granted budget rather than the card total, whichcore/training/worker.pydocuments andtests/test_rocm_oom_guard.pyasserts on by source line, so swapping it would loosen a refusal gate on Windows ROCm. It also buys nothing, since it runs inside a video load that puts tensors on the GPU anyway. Same reasoning for the othertrusted_mem_get_infocallers indiffusion_memory.py,video.pyanddiffusion_dit_trainer.py: all five genuinely need live free memory rather than total, and all run in processes already holding torch CUDA state.No
xpu-smi,pynvmloramdsmibinding exists anywhere in the backend, and the only XPU VRAM path is indiffusion_memory.py, already inside a torch-holding process. MPS has nomem_get_infoat all. CPU-only never reaches the branch.Tests
66 hardware, GPU, VRAM, llama_cpp, inference, embedding and ROCm test files:
The failure set is identical and pre-existing on
main(verified by stashing), all intests/test_amd_apu_unified_memory.py, which mocksys.modules["torch"]partially and lose to the real torch installed on this box.The 10 new passes are
tests/test_rocm_vram_probe_no_hip_context.py. ruff clean on all five files.The two RAG docstring edits correct claims that the probe was "torch-free (nvidia-smi)" and that "the common path needs no torch", which were false on ROCm and are a large part of why this stayed invisible.