Jan is an open source alternative to ChatGPT that runs 100% offline on your computer.
fix(setup): report Metal, not BLAS, as the backend on Apple Silicon (#9462) (#9567)
Thinh Le2456d303f210
diff --git a/extensions/llamacpp-extension/src/index.ts b/extensions/llamacpp-extension/src/index.tsindex 2002f8236..891dcd9ff 100644--- a/extensions/llamacpp-extension/src/index.ts+++ b/extensions/llamacpp-extension/src/index.ts@@ -59,6 +59,7 @@ import {evaluateEmbeddingVector,evaluateGpuOffload,backendFromDeviceIds,+ countAcceleratorDevices,type EmbeddingVectorProblem,type GpuOffloadCheck,type ReadinessStatus,@@ -732,10 +733,11 @@ export default class llamacpp_extension extends AIEngine implements EmbeddingEnglogger.warn('Hardware GPU probe failed during setup verification:', e)}+ const deviceIds = devices.map((d) => d.id)return {- backend: backendFromDeviceIds(devices.map((d) => d.id)),+ backend: backendFromDeviceIds(deviceIds),...evaluateGpuOffload({- engineDeviceCount: devices.length,+ engineDeviceCount: countAcceleratorDevices(deviceIds),hardwareGpuCount,}),}diff --git a/extensions/llamacpp-extension/src/readiness.test.ts b/extensions/llamacpp-extension/src/readiness.test.tsindex f7b28bb09..c06f146bd 100644--- a/extensions/llamacpp-extension/src/readiness.test.ts+++ b/extensions/llamacpp-extension/src/readiness.test.ts@@ -16,7 +16,7 @@ describe('backendFromDeviceIds', () => {expect(backendFromDeviceIds(['ROCm1'])).toBe('rocm')})- it('uses the first device when several are present', () => {+ it('uses the first accelerator when several are present', () => {expect(backendFromDeviceIds(['CUDA0', 'CUDA1'])).toBe('cuda')})@@ -29,6 +29,27 @@ describe('backendFromDeviceIds', () => {it('only strips trailing digits', () => {expect(backendFromDeviceIds(['Vulkan10'])).toBe('vulkan')})++ // On macOS the engine lists the Accelerate BLAS device before the Metal one+ // (`jan-llama-worker --list-devices` -> [BLAS, MTL0]). BLAS is a CPU math+ // library, not where the layers run, and Metal's id is `MTL`, not `Metal`;+ // naming the backend `blas` told Apple Silicon users they were on the CPU.+ it('names the GPU backend, not a CPU helper that is listed first', () => {+ expect(backendFromDeviceIds(['BLAS', 'MTL0'])).toBe('metal')+ expect(backendFromDeviceIds(['CPU', 'CUDA0'])).toBe('cuda')+ expect(backendFromDeviceIds(['BLAS', 'Vulkan0', 'Vulkan1'])).toBe('vulkan')+ })++ it('names Metal for its own device id', () => {+ expect(backendFromDeviceIds(['MTL0'])).toBe('metal')+ })++ // With no GPU device the CPU-side name is the honest answer, and it must+ // keep reading as a CPU backend.+ it('keeps a CPU-only device list as a CPU backend', () => {+ expect(backendFromDeviceIds(['BLAS'])).toBe('blas')+ expect(backendFromDeviceIds(['CPU'])).toBe('cpu')+ })})describe('evaluateGpuOffload', () => {diff --git a/extensions/llamacpp-extension/src/readiness.ts b/extensions/llamacpp-extension/src/readiness.tsindex c04f48292..76487d59b 100644--- a/extensions/llamacpp-extension/src/readiness.ts+++ b/extensions/llamacpp-extension/src/readiness.ts@@ -37,6 +37,33 @@ export interface EmbeddingVectorCheck {problem?: EmbeddingVectorProblem}+/**+ * Devices that are not where model layers run: the CPU itself and the BLAS+ * library that accelerates its matrix maths. The engine lists them next to the+ * GPUs (on macOS `BLAS` comes before `MTL0`), so they only name the backend+ * when nothing else was enumerated.+ */+const HOST_DEVICES: Record<string, true> = { cpu: true, blas: true }++/** `Vulkan0` -> `vulkan`, Metal's `MTL0` -> `metal`; empty ids drop out. */+function deviceNames(deviceIds: string[]): string[] {+ return deviceIds+ .map((id) => id.trim().replace(/\d+$/, '').toLowerCase())+ .filter((name) => name !== '')+ .map((name) => (name === 'mtl' ? 'metal' : name))+}++/**+ * How many of the engine's devices are not the host. This is the count the GPU+ * offload check compares with the hardware, so it excludes the same devices+ * `backendFromDeviceIds` skips: an engine that lists only `BLAS` has not loaded+ * a GPU.+ */+export function countAcceleratorDevices(deviceIds: string[]): number {+ return deviceNames(deviceIds).filter((name) => HOST_DEVICES[name] !== true)+ .length+}+/*** The backend a device id came from, e.g. `Vulkan0` -> `vulkan`.*@@ -44,11 +71,13 @@ export interface EmbeddingVectorCheck {* there is no backend *setting* to read, and the device the engine actually* enumerated is a stronger signal than a configured name ever was -- a* configured `cuda` told us nothing about whether CUDA loaded.+ *+ * The first accelerator wins; a host device (`BLAS`, `CPU`) is named only when+ * there is no accelerator. Metal's own id is `MTL`, which is named `metal`.*/export function backendFromDeviceIds(deviceIds: string[]): string {- const first = deviceIds.find((id) => id.trim() !== '')- if (!first) return ''- return first.trim().replace(/\d+$/, '').toLowerCase()+ const names = deviceNames(deviceIds)+ return names.find((name) => HOST_DEVICES[name] !== true) ?? names[0] ?? ''}/**diff --git a/extensions/llamacpp-extension/src/test/index.test.ts b/extensions/llamacpp-extension/src/test/index.test.tsindex ed6bc347f..eae31ad5c 100644--- a/extensions/llamacpp-extension/src/test/index.test.ts+++ b/extensions/llamacpp-extension/src/test/index.test.ts@@ -1137,6 +1137,30 @@ describe('verifyGpuOffload', () => {expect(result.reason).toBeUndefined()})+ // A host device is not where layers run: an engine that lists only BLAS next+ // to a present GPU has not loaded the GPU, so the offload check must warn+ // instead of counting BLAS as a device.+ it('warns when the engine lists only a host device next to a GPU', async () => {+ await arm([{ id: 'BLAS', name: 'Accelerate', mem: 0, free: 0 }], [{}])+ const result = await extension.verifyGpuOffload()+ expect(result.status).toBe('warning')+ expect(result.reason).toBe('runtimeUnreachable')+ expect(result.engineDeviceCount).toBe(0)+ })++ it('counts only the accelerators when host devices are listed too', async () => {+ await arm(+ [+ { id: 'BLAS', name: 'Accelerate', mem: 0, free: 0 },+ { id: 'MTL0', name: 'M3 Pro', mem: 1, free: 1 },+ ],+ [{}]+ )+ const result = await extension.verifyGpuOffload()+ expect(result.status).toBe('ok')+ expect(result.engineDeviceCount).toBe(1)+ })+// The one actionable failure: the GPU is there but the engine cannot see it.it('warns when a present GPU is invisible to the engine', async () => {await arm([], [{}])diff --git a/web-app/src/hooks/useSetupChecklist.ts b/web-app/src/hooks/useSetupChecklist.tsindex 30adc8746..86fe4aa2a 100644--- a/web-app/src/hooks/useSetupChecklist.ts+++ b/web-app/src/hooks/useSetupChecklist.ts@@ -76,9 +76,10 @@ function gpuVerdict(const label = describeGpus(gpus)if (!report || report.pending || report.unavailable) return { label }- // Metal is compiled into every macOS build and always present, so the engine- // never reports it as "a GPU build" and never enumerates it as a device.- // Reading those absences literally told Apple Silicon users they were on CPU.+ // Metal is compiled into every macOS build and always present, so a Mac is+ // never "a CPU build that found no GPU". The engine lists the Accelerate BLAS+ // device ahead of Metal, which is why the backend is named after the first+ // accelerator rather than the first device (see `backendFromDeviceIds`).if (backendGpuFamily(report.backend) === 'metal') {return { willUse: true, label }}diff --git a/web-app/src/lib/__tests__/gpuBackendMatch.test.ts b/web-app/src/lib/__tests__/gpuBackendMatch.test.tsindex 023e5d6fd..5240de821 100644--- a/web-app/src/lib/__tests__/gpuBackendMatch.test.ts+++ b/web-app/src/lib/__tests__/gpuBackendMatch.test.ts@@ -81,6 +81,18 @@ describe('backendGpuFamily', () => {expect(backendGpuFamily('macos-x64')).toBe('metal')})+ // The setup screen names the backend after the device the engine enumerated+ // (`metal`, from `MTL0`), not after a build asset, so Metal has to be+ // recognised by that name too. It read as a CPU build, and a Mac with a GPU+ // was told the GPU was unused.+ it('recognises Metal by its device name', () => {+ expect(backendGpuFamily('metal')).toBe('metal')+ expect(backendGpuFamily('mtl')).toBe('metal')+ expect(evaluateBackendGpuMatch([{ name: 'Apple M4' }], 'metal')).toEqual({+ kind: 'ok',+ })+ })+it('recognises the short cuda category names', () => {expect(backendGpuFamily('cuda-cu13.0')).toBe('cuda')})diff --git a/web-app/src/lib/gpuBackendMatch.ts b/web-app/src/lib/gpuBackendMatch.tsindex 1a785c8e8..b86bc00b3 100644--- a/web-app/src/lib/gpuBackendMatch.ts+++ b/web-app/src/lib/gpuBackendMatch.ts@@ -82,8 +82,10 @@ export function backendGpuFamily(backend: string | undefined): BackendGpuFamilyif (pattern.test(name)) return family}// Metal is compiled into every macOS build and needs no separate runtime, so- // a macOS backend name with no GPU marker still means GPU-capable.- if (/^mac|darwin/i.test(name)) return 'metal'+ // a macOS backend name with no GPU marker still means GPU-capable. The setup+ // screen names the backend after the enumerated device (`metal`, from `MTL0`)+ // rather than a build asset, so that spelling counts too.+ if (/^mac|darwin|^(metal|mtl)\d*$/i.test(name)) return 'metal'return 'cpu'}