Jan is an open source alternative to ChatGPT that runs 100% offline on your computer.
fix(web-app): join Vulkan GPU details to llama.cpp devices by name (#9631)
Akarshan Biswas1b51586ce1e4
diff --git a/web-app/src/lib/__tests__/gpuDeviceMatch.test.ts b/web-app/src/lib/__tests__/gpuDeviceMatch.test.tsnew file mode 100644index 000000000..14fb64d42--- /dev/null+++ b/web-app/src/lib/__tests__/gpuDeviceMatch.test.ts@@ -0,0 +1,86 @@+import { describe, it, expect } from 'vitest'+import { findGpuForDevice } from '../gpuDeviceMatch'+import type { GPU } from '@/hooks/useHardware'++const gpu = (name: string, vulkanIndex: number | null, cudaIndex?: number) =>+ ({+ name,+ uuid: name,+ driver_version: name,+ nvidia_info:+ cudaIndex === undefined+ ? null+ : { index: cudaIndex, compute_capability: '8.9' },+ vulkan_info:+ vulkanIndex === null+ ? null+ : {+ index: vulkanIndex,+ device_id: 0,+ device_type: '',+ api_version: '1',+ },+ }) as unknown as GPU++const nvidia = gpu('RTX 4060 Ti', 0, 0)+const intel = gpu('Intel UHD 770', 1)++describe('findGpuForDevice', () => {+ it('joins a Vulkan device by name when llama.cpp reorders devices', () => {+ // GGML_VK_VISIBLE_DEVICES=1,0 makes Intel Vulkan0.+ const found = findGpuForDevice(+ { id: 'Vulkan0', name: 'Intel UHD 770' },+ [nvidia, intel]+ )+ expect(found).toBe(intel)+ })++ it('pairs identical twin cards by rank, not raw index', () => {+ // iGPU at raw index 0, twin dGPUs at 1 and 2; llama.cpp keeps only the twins.+ const a = gpu('Twin', 1)+ const b = gpu('Twin', 2)+ const devices = [+ { id: 'Vulkan0', name: 'Twin' },+ { id: 'Vulkan1', name: 'Twin' },+ ]+ expect(findGpuForDevice(devices[0], [intel, b, a], devices)).toBe(a)+ expect(findGpuForDevice(devices[1], [intel, b, a], devices)).toBe(b)+ })++ it('pairs the one leftover device and GPU when an NVML name differs', () => {+ const devices = [+ { id: 'Vulkan0', name: 'NVIDIA GeForce RTX 4060 Ti' },+ { id: 'Vulkan1', name: 'Intel UHD 770' },+ ]+ expect(findGpuForDevice(devices[0], [intel, nvidia], devices)).toBe(nvidia)+ expect(findGpuForDevice(devices[1], [intel, nvidia], devices)).toBe(intel)+ })++ it('does not guess when several devices are unmatched', () => {+ const devices = [+ { id: 'Vulkan0', name: 'A' },+ { id: 'Vulkan1', name: 'B' },+ ]+ expect(+ findGpuForDevice(devices[0], [nvidia, gpu('Other', 1)], devices)+ ).toBeUndefined()+ })++ it('returns nothing when the name matches no GPU', () => {+ expect(+ findGpuForDevice({ id: 'Vulkan0', name: 'Mystery' }, [nvidia, intel])+ ).toBeUndefined()+ })++ it('keeps CUDA joined by index', () => {+ expect(+ findGpuForDevice({ id: 'CUDA0', name: 'RTX 4060 Ti' }, [intel, nvidia])+ ).toBe(nvidia)+ })++ it('returns nothing for other backends', () => {+ expect(+ findGpuForDevice({ id: 'Metal0', name: 'x' }, [nvidia])+ ).toBeUndefined()+ })+})diff --git a/web-app/src/lib/gpuDeviceMatch.ts b/web-app/src/lib/gpuDeviceMatch.tsnew file mode 100644index 000000000..315cd11af--- /dev/null+++ b/web-app/src/lib/gpuDeviceMatch.ts@@ -0,0 +1,65 @@+import type { GPU } from '@/hooks/useHardware'++/** Split a llama.cpp device id such as `Vulkan1` into backend and index. */+export function parseDeviceId(id: string): { backend: string; index: number } {+ const match = /^([A-Za-z]+?)(\d+)$/.exec(id ?? '')+ if (!match) return { backend: id ?? '', index: 0 }+ return { backend: match[1], index: Number(match[2]) }+}++const isVulkan = (id: string) =>+ parseDeviceId(id).backend.toLowerCase() === 'vulkan'++/**+ * Find the hardware-plugin GPU behind a llama.cpp device.+ *+ * llama.cpp's `Vulkan<N>` is a position among the devices it kept (filtered and+ * reordered by `GGML_VK_VISIBLE_DEVICES`, hybrid-graphics setups), while the+ * plugin's `vulkan_info.index` is the raw enumeration index. Joining on the+ * number attaches one GPU's driver and compute capability to another's card, so+ * Vulkan devices are joined by name instead. Names are not always equal: the+ * plugin keeps the NVML name for NVIDIA cards and Vulkan's `deviceName` for the+ * rest. So when exactly one Vulkan device and one GPU are left unmatched by+ * name, they are paired. Identical twin cards, which a name cannot tell apart,+ * are paired by rank among same-named devices. Two or more differently named+ * mismatches are left blank rather than guessed. CUDA numbering is the+ * plugin's own, so it stays index-based.+ */+export function findGpuForDevice(+ device: { id: string; name: string },+ gpus: GPU[],+ allDevices: { id: string; name: string }[] = [device]+): GPU | undefined {+ const { backend, index } = parseDeviceId(device.id)+ if (backend.toLowerCase() === 'cuda') {+ return gpus.find((gpu) => gpu.nvidia_info?.index === index)+ }+ if (backend.toLowerCase() !== 'vulkan') return undefined++ const vulkanGpus = gpus.filter((gpu) => gpu.vulkan_info)+ const named = vulkanGpus.filter((gpu) => gpu.name === device.name)+ if (named.length === 1) return named[0]+ if (named.length > 1) {+ // The plugin numbers every physical device, llama.cpp only the ones it+ // kept, so the raw numbers disagree. Pair twins by rank instead.+ const rank = allDevices+ .filter((d) => isVulkan(d.id) && d.name === device.name)+ .map((d) => parseDeviceId(d.id).index)+ .sort((a, b) => a - b)+ .indexOf(index)+ return [...named].sort(+ (a, b) => (a.vulkan_info?.index ?? 0) - (b.vulkan_info?.index ?? 0)+ )[rank]+ }++ const vulkanDevices = allDevices.filter((d) => isVulkan(d.id))+ const unmatchedDevices = vulkanDevices.filter(+ (d) => !vulkanGpus.some((gpu) => gpu.name === d.name)+ )+ const unmatchedGpus = vulkanGpus.filter(+ (gpu) => !vulkanDevices.some((d) => d.name === gpu.name)+ )+ return unmatchedDevices.length === 1 && unmatchedGpus.length === 1+ ? unmatchedGpus[0]+ : undefined+}diff --git a/web-app/src/routes/settings/hardware.tsx b/web-app/src/routes/settings/hardware.tsxindex f92995c6d..19e298a7f 100644--- a/web-app/src/routes/settings/hardware.tsx+++ b/web-app/src/routes/settings/hardware.tsx@@ -19,6 +19,7 @@ import type {} from '@/services/hardware/types'import { cn, formatMegaBytes } from '@/lib/utils'import { toNumber } from '@/utils/number'+import { findGpuForDevice, parseDeviceId } from '@/lib/gpuDeviceMatch'import { useModelProvider } from '@/hooks/useModelProvider'import { useAppState } from '@/hooks/useAppState'import { Button } from '@/components/ui/button'@@ -39,33 +40,10 @@ const BACKEND_LABELS: Record<string, string> = {cpu: 'CPU',}-function parseDeviceId(id: string): { backend: string; index: number } {- const match = /^([A-Za-z]+?)(\d+)$/.exec(id ?? '')- if (!match) return { backend: id ?? '', index: 0 }- return { backend: match[1], index: Number(match[2]) }-}-function backendLabel(backend: string): string {return BACKEND_LABELS[backend.toLowerCase()] ?? backend.toUpperCase()}-// Devices are joined to hardware-plugin GPUs by per-backend index, not by name:-// the same physical GPU appears at different indices under different backends.-function findGpuForDevice(- backend: string,- index: number,- gpus: GPU[]-): GPU | undefined {- const key = backend.toLowerCase()- if (key === 'vulkan') {- return gpus.find((gpu) => gpu.vulkan_info?.index === index)- }- if (key === 'cuda') {- return gpus.find((gpu) => gpu.nvidia_info?.index === index)- }- return undefined-}-function BackendChip({ label, active }: { label: string; active: boolean }) {const { t } = useTranslation()return (@@ -175,19 +153,21 @@ function selectedDevice(group: GpuGroup): ActiveDevice {function GpuGroupCard({group,gpus,+ allDevices,onToggle,onSelect,}: {group: GpuGroupgpus: GPU[]+ allDevices: DeviceList[]onToggle: () => voidonSelect: (deviceId: string) => void}) {const { t } = useTranslation()const activated = group.devices.some((device) => device.activated)const device = selectedDevice(group)- const { backend, index } = parseDeviceId(device.id)- const gpuInfo = findGpuForDevice(backend, index, gpus)+ const { backend } = parseDeviceId(device.id)+ const gpuInfo = findGpuForDevice(device, gpus, allDevices)const used = Math.max(0, device.mem - device.free)const usedPercent = device.mem > 0 ? (used / device.mem) * 100 : 0@@ -680,6 +660,7 @@ function HardwareContent() {key={group.key}group={group}gpus={gpus}+ allDevices={llamacppDevices}onToggle={() => handleToggleGroup(group)}onSelect={(deviceId) =>handleSelectBackend(group, deviceId)