Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.
Python agentaichatgptdeepseekfine-tuninggemmaimage-generationllamallmllmsopenaipythonqwenreinforcement-learningself-hostedstable-diffusiontext-to-speechttsuiunsloth
Count a unified-memory APU's window as shared host memory, not dedicated VRAM (#11366)
Daniel Hanf10041ad9e82
diff --git a/studio/frontend/src/hooks/gpu-vram.ts b/studio/frontend/src/hooks/gpu-vram.tsindex 1b5603aed5d..b8a377ff3f8 100644--- a/studio/frontend/src/hooks/gpu-vram.ts+++ b/studio/frontend/src/hooks/gpu-vram.ts@@ -14,6 +14,9 @@ export interface MemoryTotalDevice {shared_memory?: boolean;/** host-backed portion of the shared pool; the rest is reserved GPU memory. */shared_memory_host_backed_gb?: number | null;+ /** `hardware.py` sets `shared_memory` only on Windows, so a Linux ROCm APU arrives+ * as `unified_memory: true, shared_memory: false`. */+ unified_memory?: boolean;}export interface GpuMemoryTotalsGb {@@ -29,6 +32,13 @@ export interface VramReportingGpu {vram_used_gb_aggregate?: number | null;}+function sharesHostMemoryDevice(device: MemoryTotalDevice): boolean {+ return sharesHostMemory({+ sharedMemory: device.shared_memory === true,+ unifiedMemory: device.unified_memory === true,+ });+}+/** Sum dedicated VRAM while counting a shared host-memory pool only once. Devices arrive rounded to 2dp, so* summing them reintroduces float error (three B200s at 179.06 give 537.1800000000001) and not every caller rounds* again before printing. Round back to the precision they arrived with. */@@ -45,13 +55,14 @@ export function gpuMemoryTotalsGb(const total = device.memory_total_gb ?? 0;return Number.isFinite(total) && total > 0 ? total : 0;};+ // A no-op for callers that already fold on their way in (use-gpu-info, memory-fit).const dedicatedDevices = roundToDevicePrecision(devices- .filter((device) => !device.shared_memory)+ .filter((device) => !sharesHostMemoryDevice(device)).reduce((sum, device) => sum + size(device), 0),);const sharedPool = devices- .filter((device) => device.shared_memory)+ .filter(sharesHostMemoryDevice).reduce((totals, device) => {const total = size(device);@@ -63,14 +74,26 @@ export function gpuMemoryTotalsGb(? Math.min(total, hostBackedReported as number): total;return {- hostBacked: Math.max(totals.hostBacked, hostBacked),+ // `shared_memory` means "this budget IS the host pool", so several are views+ // of one and the largest is it. `unified_memory` alone means only shared with+ // its OWN cpu: a pool per socket, and collapsing those reports MI300A as one card.+ hostBacked:+ device.shared_memory === true+ ? Math.max(totals.hostBacked, hostBacked)+ : totals.hostBacked,+ perDevice:+ device.shared_memory === true+ ? totals.perDevice+ : totals.perDevice + hostBacked,reserved:totals.reserved + (hostBackedKnown ? total - hostBacked : 0),};},- { hostBacked: 0, reserved: 0 },+ { hostBacked: 0, perDevice: 0, reserved: 0 },);- const shared = roundToDevicePrecision(sharedPool.hostBacked);+ const shared = roundToDevicePrecision(+ sharedPool.hostBacked + sharedPool.perDevice,+ );const dedicated = roundToDevicePrecision(dedicatedDevices + sharedPool.reserved,);@@ -136,6 +159,12 @@ function budgetedGpuMemoryGb(let partialSharedDemand = 0;let fullySharedDemand = 0;let sharedPool = 0;+ // The same split `gpuMemoryTotalsGb` makes, for the same reason: `shared_memory`+ // devices are views of ONE host pool and collapse, while `unified_memory`-only+ // devices are a pool per socket and stay additive. Without this the capacity path+ // answers 48 GiB for the inventory the totals path calls 96.+ let perDevicePool = 0;+ let perDeviceDemand = 0;for (const device of devices) {const total =Number.isFinite(device.memoryTotalGb) && device.memoryTotalGb > 0@@ -155,6 +184,11 @@ function budgetedGpuMemoryGb(const deviceReserved = total - hostBacked;reserved += Math.min(deviceReserved, deviceCapacity);const deviceSharedDemand = Math.max(0, deviceCapacity - deviceReserved);+ if (device.sharedMemory !== true) {+ perDeviceDemand += deviceSharedDemand;+ perDevicePool += hostBacked;+ continue;+ }if (deviceReserved > 0) {partialSharedDemand += deviceSharedDemand;} else {@@ -167,7 +201,8 @@ function budgetedGpuMemoryGb(total: roundToDevicePrecision(dedicated +reserved +- Math.min(sharedPool, partialSharedDemand + fullySharedDemand),+ Math.min(sharedPool, partialSharedDemand + fullySharedDemand) ++ Math.min(perDevicePool, perDeviceDemand),),independent: roundToDevicePrecision(dedicated + reserved),};@@ -232,10 +267,12 @@ export function resolveMemoryCapacityGb(input: MemoryCapacityInput): {const pinnedTotals = gpuMemoryTotalsGb(input.pinnedDevices.map((device) => ({memory_total_gb: device.memoryTotalGb,- // Folded, not passed through: a Linux ROCm APU reports shared_memory false- // and unified_memory true, and counting its carved window as dedicated VRAM- // added 46.56 GiB of capacity that is already inside system RAM.- shared_memory: sharesHostMemory(device),+ // Both flags raw. Folding here first loses the distinction gpuMemoryTotalsGb+ // needs: one host pool collapses, a pool per socket does not, and pre-folding+ // made this path answer 48 GiB where the totals path answered 96 for the very+ // same inventory (reported by ItsRoy69 on #11366).+ shared_memory: device.sharedMemory === true,+ unified_memory: device.unifiedMemory === true,shared_memory_host_backed_gb: device.sharedMemoryHostBackedGb,})),);@@ -275,10 +312,12 @@ export function resolveMemoryCapacityGb(input: MemoryCapacityInput): {const capacityDeviceTotals = gpuMemoryTotalsGb(capacityDevices.map((device) => ({memory_total_gb: device.memoryTotalGb,- // Folded, not passed through: a Linux ROCm APU reports shared_memory false- // and unified_memory true, and counting its carved window as dedicated VRAM- // added 46.56 GiB of capacity that is already inside system RAM.- shared_memory: sharesHostMemory(device),+ // Both flags raw. Folding here first loses the distinction gpuMemoryTotalsGb+ // needs: one host pool collapses, a pool per socket does not, and pre-folding+ // made this path answer 48 GiB where the totals path answered 96 for the very+ // same inventory (reported by ItsRoy69 on #11366).+ shared_memory: device.sharedMemory === true,+ unified_memory: device.unifiedMemory === true,shared_memory_host_backed_gb: device.sharedMemoryHostBackedGb,})),);diff --git a/studio/frontend/src/hooks/use-gpu-info.ts b/studio/frontend/src/hooks/use-gpu-info.tsindex af5ad1f618f..77202fc3d07 100644--- a/studio/frontend/src/hooks/use-gpu-info.ts+++ b/studio/frontend/src/hooks/use-gpu-info.ts@@ -135,20 +135,12 @@ function toGpuInfo(const loadDevice = pickLoadDevice(devices);return {...base,- // Folded, not raw `shared_memory`: hardware.py sets that flag only on Windows, so a Linux ROCm- // APU arrives unified true / shared false and its GTT window was never subtracted here. The- // RAM tier then offered the very bytes the window is a view INTO as a second budget.+ // Raw: `gpuSharedHostMemoryGb` folds the two flags itself. Folding here first also+ // collapsed a multi-socket unified host's pools into one, so it subtracted one+ // socket's worth and offered the rest again as a RAM budget.systemRamAvailableGb: systemRamAvailableOutsideSharedPoolGb(base.systemRamAvailableGb,- gpuSharedHostMemoryGb(- devices.map((device) => ({- ...device,- shared_memory: sharesHostMemory({- sharedMemory: device.shared_memory === true,- unifiedMemory: device.unified_memory === true,- }),- })),- ),+ gpuSharedHostMemoryGb(devices),),sharedMemory: memoryTotals.shared > 0 && memoryTotals.dedicated === 0,// Additive, and deliberately some() where sharedMemory above is "no dedicated pool at all": onediff --git a/studio/frontend/tests/on-device-dot-and-header-alignment.test.ts b/studio/frontend/tests/on-device-dot-and-header-alignment.test.tsindex a64a659806f..5a4f45983de 100644--- a/studio/frontend/tests/on-device-dot-and-header-alignment.test.ts+++ b/studio/frontend/tests/on-device-dot-and-header-alignment.test.ts@@ -385,10 +385,16 @@ test("chat and the Hub answer the fit question with one formula", () => {"and that is every expander there is",);// The APU window comes out of the RAM tier where the figure is built, so every rule downstream- // sees one pool counted once.+ // sees one pool counted once. The tier hands the RAW devices to gpuSharedHostMemoryGb, which+ // folds the two flags itself: pre-folding here collapsed a multi-socket unified host to one+ // socket's worth, so it subtracted 48 GiB of a 96 GiB pool (#11366).assert.ok(- GPU_INFO.includes("shared_memory: sharesHostMemory({"),- "the RAM tier folds unified in",+ GPU_INFO.includes("gpuSharedHostMemoryGb(devices)"),+ "the RAM tier folds unified in, on the raw devices",+ );+ assert.ok(+ !GPU_INFO.includes("shared_memory: sharesHostMemory({"),+ "and never pre-folds them on the way in",);assert.ok(HUB_CARD.includes("gpuCount?: number;"));assert.ok(RECOMMENDED.includes("budgetFraction: opts.budgetFraction,"));diff --git a/studio/frontend/tests/unified-memory-apu-totals.test.ts b/studio/frontend/tests/unified-memory-apu-totals.test.tsnew file mode 100644index 00000000000..7094a73645f--- /dev/null+++ b/studio/frontend/tests/unified-memory-apu-totals.test.ts@@ -0,0 +1,198 @@+// SPDX-License-Identifier: AGPL-3.0-only+// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0++/**+ * A Linux ROCm APU's GTT window was counted as dedicated VRAM standing beside the+ * system RAM it is a view into. The split moves; the TOTAL must not, since fit+ * verdicts are measured against the aggregate.+ */++import assert from "node:assert/strict";+import test from "node:test";+import {+ aggregateGpuMemoryTotalGb,+ gpuMemoryTotalsGb,+ gpuSharedHostMemoryGb,+ resolveMemoryCapacityGb,+ sharesHostMemory,+} from "../src/hooks/gpu-vram.ts";++/** Measured on a Strix Halo gfx1151 box (Radeon 8060S) under ROCm on Linux. */+const linuxApu = {+ memory_total_gb: 64.0,+ shared_memory: false,+ unified_memory: true,+};+/** The same part on Windows, where `hardware.py` does set the flag. */+const windowsApu = {+ memory_total_gb: 68.0,+ shared_memory: true,+ unified_memory: true,+ shared_memory_host_backed_gb: 44.0,+};+const discrete = { memory_total_gb: 179.06, shared_memory: false };++test("a Linux ROCm APU's window is shared host memory, not dedicated VRAM", () => {+ const totals = gpuMemoryTotalsGb([linuxApu]);+ assert.equal(totals.dedicated, 0);+ assert.equal(totals.shared, 64);+ assert.equal(totals.total, 64);+ assert.equal(aggregateGpuMemoryTotalGb([linuxApu]), 64);+});++test("a discrete card beside an APU keeps its own VRAM separate", () => {+ const totals = gpuMemoryTotalsGb([+ { memory_total_gb: 16, shared_memory: false },+ { memory_total_gb: 48, shared_memory: false, unified_memory: true },+ ]);+ // Previously 64 dedicated: a 64 GiB card on a machine whose only VRAM is 16 GiB.+ assert.equal(totals.dedicated, 16);+ assert.equal(totals.shared, 48);+ assert.equal(totals.total, 64);+});++test("a multi-socket unified host keeps one pool per socket", () => {+ // `unified_memory` alone says a device shares memory with its OWN cpu, which on a+ // multi-socket node is a pool per socket: collapsing these reports it as one card.+ const totals = gpuMemoryTotalsGb([+ { memory_total_gb: 48, shared_memory: false, unified_memory: true },+ { memory_total_gb: 48, shared_memory: false, unified_memory: true },+ ]);+ assert.equal(totals.dedicated, 0);+ assert.equal(totals.shared, 96);+ assert.equal(totals.total, 96);+});++test("devices that already carried the shared flag are still one pool", () => {+ // The Vulkan case the max was written for: duplicate ICD rows for one device.+ const totals = gpuMemoryTotalsGb([+ { memory_total_gb: 48, shared_memory: true },+ { memory_total_gb: 48, shared_memory: true },+ ]);+ assert.equal(totals.shared, 48);+ assert.equal(totals.total, 48);+});++test("the aggregate survives the reclassification in every shape", () => {+ const inventories = [+ [{ memory_total_gb: 64, shared_memory: false, unified_memory: true }],+ [+ { memory_total_gb: 16, shared_memory: false },+ { memory_total_gb: 48, shared_memory: false, unified_memory: true },+ ],+ [+ { memory_total_gb: 48, shared_memory: false, unified_memory: true },+ { memory_total_gb: 48, shared_memory: false, unified_memory: true },+ ],+ ];+ for (const devices of inventories) {+ const totals = gpuMemoryTotalsGb(devices);+ const asDedicatedSum = devices.reduce(+ (sum, device) => sum + (device.memory_total_gb ?? 0),+ 0,+ );+ assert.equal(totals.total, asDedicatedSum, JSON.stringify(devices));+ }+});++test("the platforms that already reported a shared pool do not move", () => {+ const totals = gpuMemoryTotalsGb([windowsApu]);+ assert.equal(totals.dedicated, 24);+ assert.equal(totals.shared, 44);+ assert.equal(totals.total, 68);+});++test("a discrete-only host is untouched", () => {+ const totals = gpuMemoryTotalsGb([discrete]);+ assert.equal(totals.dedicated, 179.06);+ assert.equal(totals.shared, 0);+ assert.equal(totals.total, 179.06);+ assert.deepEqual(gpuMemoryTotalsGb([]), { dedicated: 0, shared: 0, total: 0 });+});++test("only the two flags route; nothing else about a device does", () => {+ for (const device of [+ { memory_total_gb: 64, shared_memory: false, unified_memory: false },+ { memory_total_gb: 64, shared_memory: false },+ { memory_total_gb: 64 },+ ]) {+ assert.equal(gpuMemoryTotalsGb([device]).dedicated, 64, JSON.stringify(device));+ assert.equal(gpuMemoryTotalsGb([device]).shared, 0, JSON.stringify(device));+ }+});++test("callers that already folded the two flags get the same answer", () => {+ // use-gpu-info and memory-fit hand in an already-folded flag: that has to be a no-op.+ // Single device only. Folding is NOT idempotent for several unified devices, which is+ // why no caller pre-folds any more; `every call site agrees` below pins that.+ for (const device of [linuxApu, windowsApu, discrete]) {+ const preFolded = {+ memory_total_gb: device.memory_total_gb,+ shared_memory: sharesHostMemory({+ sharedMemory: device.shared_memory === true,+ unifiedMemory: (device as { unified_memory?: boolean }).unified_memory === true,+ }),+ shared_memory_host_backed_gb: (+ device as { shared_memory_host_backed_gb?: number }+ ).shared_memory_host_backed_gb,+ };+ assert.deepEqual(gpuMemoryTotalsGb([preFolded]), gpuMemoryTotalsGb([device]));+ }+});++test("every call site agrees on a multi-socket unified host", () => {+ // Reported by ItsRoy69 on #11366: the totals path read these as 96 while the capacity+ // path pre-folded both flags into `shared_memory` and then collapsed them to 48, so the+ // same inventory had two answers depending on who asked. A pin covering both sockets+ // reported half its memory.+ const devices = [+ { memory_total_gb: 48, shared_memory: false, unified_memory: true },+ { memory_total_gb: 48, shared_memory: false, unified_memory: true },+ ];+ assert.equal(gpuMemoryTotalsGb(devices).total, 96);+ assert.equal(gpuSharedHostMemoryGb(devices), 96);++ const pinned = devices.map((device) => ({+ memoryTotalGb: device.memory_total_gb,+ sharedMemory: device.shared_memory,+ unifiedMemory: device.unified_memory,+ sharedMemoryHostBackedGb: null,+ }));+ const capacity = resolveMemoryCapacityGb({+ pinnedDevices: pinned,+ hostDevices: pinned,+ hostGpuTotalGb: 96,+ hostDedicatedGpuTotalGb: 0,+ hostSharesSystemRam: true,+ systemRamTotalGb: 128,+ unifiedMemory: false,+ });+ assert.equal(capacity.gpuCapacityGb, 96);+});++test("one host pool is still counted once, on every path", () => {+ // The other half of the same rule: `shared_memory` devices ARE views of one pool+ // (duplicate Vulkan ICD rows), so widening the unified case must not widen this one.+ const devices = [+ { memory_total_gb: 48, shared_memory: true },+ { memory_total_gb: 48, shared_memory: true },+ ];+ assert.equal(gpuMemoryTotalsGb(devices).total, 48);+ assert.equal(gpuSharedHostMemoryGb(devices), 48);++ const pinned = devices.map((device) => ({+ memoryTotalGb: device.memory_total_gb,+ sharedMemory: device.shared_memory,+ sharedMemoryHostBackedGb: null,+ }));+ const capacity = resolveMemoryCapacityGb({+ pinnedDevices: pinned,+ hostDevices: pinned,+ hostGpuTotalGb: 48,+ hostDedicatedGpuTotalGb: 0,+ hostSharesSystemRam: true,+ systemRamTotalGb: 128,+ unifiedMemory: false,+ });+ assert.equal(capacity.gpuCapacityGb, 48);+});