Jan is an open source alternative to ChatGPT that runs 100% offline on your computer.
fix(llamacpp): join Vulkan devices to AMD VRAM by name, not index (#9443) (#9444)
Thinh Le587e063931b2
diff --git a/extensions/llamacpp-extension/src/deviceMemory.test.ts b/extensions/llamacpp-extension/src/deviceMemory.test.tsnew file mode 100644index 000000000..96397436a--- /dev/null+++ b/extensions/llamacpp-extension/src/deviceMemory.test.ts@@ -0,0 +1,147 @@+import { describe, expect, it } from 'vitest'+import type { DeviceList } from '@janhq/tauri-plugin-llamacpp-api'+import {+ overrideAmdVulkanMemory,+ type HardwareGpu,+ type HardwareGpuUsage,+} from './deviceMemory'++const WX = 'AMD Radeon Pro WX 5100 (RADV POLARIS10)'+const APU = 'AMD Radeon Graphics (RADV RENOIR)'++// What ggml-vulkan reports before the override: the APU's shared memory is+// counted as device-local, so it is larger than its carve-out.+const ggml = (id: string, name: string, mem: number): DeviceList => ({+ id,+ name,+ mem,+ free: mem,+})++const amd = (name: string, uuid: string, index: number): HardwareGpu => ({+ name,+ vendor: 'AMD',+ uuid,+ vulkan_info: { index },+})++const used = (+ uuid: string,+ total_memory: number,+ used_memory = 0+): HardwareGpuUsage => ({ uuid, total_memory, used_memory })++const WX_USAGE = used('uuid-wx', 8192, 1000)+const APU_USAGE = used('uuid-apu', 16384, 2000)++const memOf = (list: DeviceList[], id: string) => {+ const d = list.find((x) => x.id === id)+ return d && { mem: d.mem, free: d.free }+}++describe('overrideAmdVulkanMemory', () => {+ it('gives each device its own VRAM when both lists agree on order', () => {+ const out = overrideAmdVulkanMemory(+ [ggml('Vulkan0', APU, 24000), ggml('Vulkan1', WX, 8192)],+ [amd(APU, 'uuid-apu', 0), amd(WX, 'uuid-wx', 1)],+ [WX_USAGE, APU_USAGE]+ )+ expect(memOf(out, 'Vulkan0')).toEqual({ mem: 16384, free: 14384 })+ expect(memOf(out, 'Vulkan1')).toEqual({ mem: 8192, free: 7192 })+ })++ it('keeps the right VRAM when GGML_VK_VISIBLE_DEVICES hides the APU', () => {+ // ggml numbers only the devices it kept, so the dGPU becomes Vulkan0 while+ // the hardware plugin still lists it second.+ const out = overrideAmdVulkanMemory(+ [ggml('Vulkan0', WX, 8192)],+ [amd(APU, 'uuid-apu', 0), amd(WX, 'uuid-wx', 1)],+ [WX_USAGE, APU_USAGE]+ )+ expect(memOf(out, 'Vulkan0')).toEqual({ mem: 8192, free: 7192 })+ })++ it('keeps the right VRAM when a software device shifts raw indices', () => {+ // llvmpipe is enumerated first. ggml skips it; the hardware plugin counts+ // it in `index` but does not list it.+ const out = overrideAmdVulkanMemory(+ [ggml('Vulkan0', WX, 8192), ggml('Vulkan1', APU, 24000)],+ [amd(WX, 'uuid-wx', 1), amd(APU, 'uuid-apu', 2)],+ [WX_USAGE, APU_USAGE]+ )+ expect(memOf(out, 'Vulkan0')).toEqual({ mem: 8192, free: 7192 })+ expect(memOf(out, 'Vulkan1')).toEqual({ mem: 16384, free: 14384 })+ })++ it('keeps the right VRAM when ggml orders devices differently', () => {+ // ggml's driver de-duplication can move a device to the end of its list.+ const out = overrideAmdVulkanMemory(+ [ggml('Vulkan0', WX, 8192), ggml('Vulkan1', APU, 24000)],+ [amd(APU, 'uuid-apu', 0), amd(WX, 'uuid-wx', 1)],+ [WX_USAGE, APU_USAGE]+ )+ expect(memOf(out, 'Vulkan0')).toEqual({ mem: 8192, free: 7192 })+ expect(memOf(out, 'Vulkan1')).toEqual({ mem: 16384, free: 14384 })+ })++ it('leaves identical GPUs alone: their free memory cannot be told apart', () => {+ const twins = [amd(WX, 'uuid-a', 0), amd(WX, 'uuid-b', 1)]+ const twinUsage = [used('uuid-a', 8192, 100), used('uuid-b', 8192, 4000)]++ const both = overrideAmdVulkanMemory(+ [ggml('Vulkan0', WX, 8192), ggml('Vulkan1', WX, 8192)],+ twins,+ twinUsage+ )+ expect(memOf(both, 'Vulkan0')).toEqual({ mem: 8192, free: 8192 })+ expect(memOf(both, 'Vulkan1')).toEqual({ mem: 8192, free: 8192 })++ // One twin hidden by GGML_VK_VISIBLE_DEVICES: still no way to say which.+ const one = overrideAmdVulkanMemory(+ [ggml('Vulkan0', WX, 8192)],+ twins,+ twinUsage+ )+ expect(memOf(one, 'Vulkan0')).toEqual({ mem: 8192, free: 8192 })+ })++ it('leaves a device alone when no hardware GPU has its name', () => {+ // Never fall back to the index: a wrong size is worse than ggml's own.+ const out = overrideAmdVulkanMemory(+ [ggml('Vulkan0', 'Some Other GPU', 4096)],+ [amd(APU, 'uuid-apu', 0)],+ [APU_USAGE]+ )+ expect(memOf(out, 'Vulkan0')).toEqual({ mem: 4096, free: 4096 })+ })++ it('leaves non-Vulkan and non-AMD devices alone', () => {+ const nvidia: HardwareGpu = {+ name: 'NVIDIA GeForce RTX 4060',+ vendor: 'NVIDIA',+ uuid: 'uuid-nv',+ vulkan_info: { index: 0 },+ }+ const out = overrideAmdVulkanMemory(+ [+ ggml('CUDA0', 'NVIDIA GeForce RTX 4060', 8000),+ ggml('Vulkan0', 'NVIDIA GeForce RTX 4060', 8000),+ ggml('Vulkan1', WX, 8192),+ ],+ [nvidia, amd(WX, 'uuid-wx', 1)],+ [used('uuid-nv', 1, 0), WX_USAGE]+ )+ expect(memOf(out, 'CUDA0')).toEqual({ mem: 8000, free: 8000 })+ expect(memOf(out, 'Vulkan0')).toEqual({ mem: 8000, free: 8000 })+ expect(memOf(out, 'Vulkan1')).toEqual({ mem: 8192, free: 7192 })+ })++ it('leaves a device alone when the plugin has no usage for its GPU', () => {+ const out = overrideAmdVulkanMemory(+ [ggml('Vulkan0', WX, 8192)],+ [amd(WX, 'uuid-wx', 0)],+ []+ )+ expect(memOf(out, 'Vulkan0')).toEqual({ mem: 8192, free: 8192 })+ })+})diff --git a/extensions/llamacpp-extension/src/deviceMemory.ts b/extensions/llamacpp-extension/src/deviceMemory.tsnew file mode 100644index 000000000..4480b41b1--- /dev/null+++ b/extensions/llamacpp-extension/src/deviceMemory.ts@@ -0,0 +1,90 @@+import type { DeviceList } from '@janhq/tauri-plugin-llamacpp-api'++/** The subset of the hardware plugin's `GpuInfo` this module reads. */+export interface HardwareGpu {+ name?: string+ vendor?: unknown+ uuid?: string+ vulkan_info?: { index?: number } | null+}++/** The subset of the hardware plugin's `GpuUsage` this module reads. */+export interface HardwareGpuUsage {+ uuid?: string+ total_memory?: number+ used_memory?: number+}++/**+ * Replace the memory ggml-vulkan reports for AMD devices with the dedicated+ * VRAM the hardware plugin reads from sysfs.+ *+ * ggml-vulkan reports an integrated GPU's shared (UMA) memory as device-local,+ * so an APU shows far more memory than its carve-out.+ *+ * The two lists number devices independently. ggml's `Vulkan<N>` is a position+ * among the devices it kept (software devices skipped, duplicate drivers+ * merged, `GGML_VK_VISIBLE_DEVICES` applied), while the plugin's+ * `vulkan_info.index` is the raw enumeration index. Joining on the number gives+ * a device another GPU's VRAM, so devices are joined by name instead: both+ * sides read Vulkan's `deviceName`. A device whose name matches nothing, or+ * matches more than one GPU (identical cards, which cannot be told apart),+ * keeps ggml's own figures.+ */+export function overrideAmdVulkanMemory(+ devices: DeviceList[],+ gpus: HardwareGpu[],+ usage: HardwareGpuUsage[]+): DeviceList[] {+ const uuidToUsage = new Map<string, HardwareGpuUsage>()+ for (const u of usage) {+ if (u && typeof u.uuid === 'string') uuidToUsage.set(u.uuid, u)+ }++ // A name identifies a GPU only when exactly one hardware GPU and one ggml+ // device carry it. Identical cards share total VRAM but not free memory, and+ // nothing here says which twin is which, so they keep ggml's figures.+ const gpuByName = new Map<string, string | null>()+ for (const gpu of gpus) {+ if (+ typeof gpu?.vendor !== 'string' ||+ !gpu.vendor.toUpperCase().includes('AMD') ||+ typeof gpu.vulkan_info?.index !== 'number' ||+ typeof gpu.uuid !== 'string' ||+ typeof gpu.name !== 'string'+ ) {+ continue+ }+ gpuByName.set(gpu.name, gpuByName.has(gpu.name) ? null : gpu.uuid)+ }+ if (gpuByName.size === 0) return devices++ const deviceCountByName = new Map<string, number>()+ for (const dev of devices) {+ if (/^Vulkan\d+/.test(dev.id ?? '')) {+ deviceCountByName.set(+ dev.name,+ (deviceCountByName.get(dev.name) ?? 0) + 1+ )+ }+ }+ const uuidFor = (dev: DeviceList) =>+ /^Vulkan\d+/.test(dev.id ?? '') && deviceCountByName.get(dev.name) === 1+ ? gpuByName.get(dev.name)+ : undefined++ return devices.map((dev) => {+ const uuid = uuidFor(dev)+ const u = uuid ? uuidToUsage.get(uuid) : undefined+ if (+ !u ||+ typeof u.total_memory !== 'number' ||+ typeof u.used_memory !== 'number'+ ) {+ return dev+ }+ const total = Math.max(0, Math.floor(u.total_memory))+ const free = Math.max(0, Math.floor(u.total_memory - u.used_memory))+ return { ...dev, mem: total, free }+ })+}diff --git a/extensions/llamacpp-extension/src/index.ts b/extensions/llamacpp-extension/src/index.tsindex ec41a5dcf..2002f8236 100644--- a/extensions/llamacpp-extension/src/index.ts+++ b/extensions/llamacpp-extension/src/index.ts@@ -98,6 +98,7 @@ import {DeviceList,EngineVersion,} from '@janhq/tauri-plugin-llamacpp-api'+import { overrideAmdVulkanMemory } from './deviceMemory'import { getSystemUsage, getSystemInfo } from '@janhq/tauri-plugin-hardware-api'const EMBEDDING_CHECK_VERSION = 3@@ -3058,62 +3059,7 @@ export default class llamacpp_extension extends AIEngine implements EmbeddingEngif (sysInfo?.os_type === 'linux' && Array.isArray(sysInfo.gpus)) {const usage = await getSystemUsage()if (usage && Array.isArray(usage.gpus)) {- const uuidToUsage: Record<- string,- { total_memory: number; used_memory: number }- > = {}- for (const u of usage.gpus as any[]) {- if (u && typeof u.uuid === 'string') {- uuidToUsage[u.uuid] = u- }- }-- const indexToAmdUuid = new Map<number, string>()- for (const gpu of sysInfo.gpus as any[]) {- const vendorStr =- typeof gpu?.vendor === 'string'- ? gpu.vendor- : typeof gpu?.vendor === 'object' && gpu.vendor !== null- ? String(gpu.vendor)- : ''- if (- vendorStr.toUpperCase().includes('AMD') &&- gpu?.vulkan_info &&- typeof gpu.vulkan_info.index === 'number' &&- typeof gpu.uuid === 'string'- ) {- indexToAmdUuid.set(gpu.vulkan_info.index, gpu.uuid)- }- }-- if (indexToAmdUuid.size > 0) {- const adjusted = dList.map((dev) => {- if (dev.id?.startsWith('Vulkan')) {- const match = /^Vulkan(\d+)/.exec(dev.id)- if (match) {- const vIdx = Number(match[1])- const uuid = indexToAmdUuid.get(vIdx)- if (uuid) {- const u = uuidToUsage[uuid]- if (- u &&- typeof u.total_memory === 'number' &&- typeof u.used_memory === 'number'- ) {- const total = Math.max(0, Math.floor(u.total_memory))- const free = Math.max(- 0,- Math.floor(u.total_memory - u.used_memory)- )- return { ...dev, mem: total, free }- }- }- }- }- return dev- })- return adjusted- }+ return overrideAmdVulkanMemory(dList, sysInfo.gpus, usage.gpus)}}} catch (e) {