Files
llama.cpp/tools/ui/tests/unit/gguf-metadata.test.ts
T
Aleksander Grygier 2b66bfbf71 ui: show audio in Discover for models whose mmproj has an audio encoder (#31837)
* ui: show audio in Discover for models whose mmproj has an audio encoder

Discover took audio only from the pipeline tag, so omni models tagged
any-to-any, such as Gemma 4 and LFM 3 Omni, showed vision but never audio,
though their mmproj carries an audio encoder.

Read the encoders from the mmproj's GGUF metadata instead: a ranged read of
its first 64 KB, once per repo, for clip.has_vision_encoder and
clip.has_audio_encoder. The Discover list and details now show audio when
either the pipeline tag or the mmproj says so.

Assisted by: Claude (Opus 5.5 Medium)

* ui: take Discover modalities from the mmproj's encoders in one place

Review fixes for the mmproj header read:

- drop vision for audio-only mmprojs: the converter writes
  clip.has_vision_encoder only when true, so a header read in full without
  it has no vision encoder; a header cut short leaves vision as it was
- accept only a 206 to the ranged read, so a server that ignores Range
  never streams the whole mmproj
- encode the mmproj path in the resolve URL
- derive the list and details modalities with one HuggingFaceService
  helper, so both views agree
- point the key comment at tools/mtmd/mtmd-impl.h, where the keys live now

Assisted-by: Claude (Opus 5.5 Medium)

* ui: keep a gated repo's mmproj from being read again on every mount

A gated, private or missing mmproj answers 401, 403 or 404 to every read,
so caching nothing made each mount of its Discover row request it again and
log an error. Cache it as unknown instead: no audio, and vision from the
mmproj being there, as before the header was read.

Assisted-by: Claude (Opus 5.5 Medium)
2026-11-10 22:34:46 +01:00

99 lines
2.9 KiB
TypeScript

import { readGgufMetadata } from '$lib/utils/gguf-metadata';
import { describe, expect, it } from 'vitest';
type Entry =
| [key: string, type: 'bool', value: boolean]
| [key: string, type: 'u32', value: number]
| [key: string, type: 'string', value: string]
| [key: string, type: 'string[]', value: string[]]
| [key: string, type: 'f32[]', value: number[]];
/** A GGUF header with the given metadata and no tensors. */
function gguf(entries: Entry[]): ArrayBuffer {
const bytes: number[] = [];
const u32 = (n: number) => bytes.push(...new Uint8Array(new Uint32Array([n]).buffer));
const u64 = (n: number) => bytes.push(...new Uint8Array(new BigUint64Array([BigInt(n)]).buffer));
const str = (s: string) => {
const encoded = new TextEncoder().encode(s);
u64(encoded.length);
bytes.push(...encoded);
};
u32(0x46554747);
u32(3);
u64(0);
u64(entries.length);
for (const [key, type, value] of entries) {
str(key);
if (type === 'bool') {
u32(7);
bytes.push(value ? 1 : 0);
} else if (type === 'u32') {
u32(4);
u32(value);
} else if (type === 'string') {
u32(8);
str(value);
} else if (type === 'string[]') {
u32(9);
u32(8);
u64(value.length);
value.forEach(str);
} else {
u32(9);
u32(6);
u64(value.length);
value.forEach((n) => bytes.push(...new Uint8Array(new Float32Array([n]).buffer)));
}
}
return new Uint8Array(bytes).buffer;
}
const MMPROJ: Entry[] = [
['general.architecture', 'string', 'clip'],
['clip.projector_type', 'string', 'gemma4'],
['clip.vision.image_mean', 'f32[]', [0.5, 0.5, 0.5]],
['tokenizer.ggml.tokens', 'string[]', ['<a>', '<b>']],
['clip.has_vision_encoder', 'bool', true],
['clip.has_audio_encoder', 'bool', true],
['clip.audio.num_mel_bins', 'u32', 128]
];
describe('readGgufMetadata', () => {
it('reads scalars and strings, skipping arrays', () => {
const { complete, metadata } = readGgufMetadata(gguf(MMPROJ));
expect(complete).toBe(true);
expect(metadata.get('general.architecture')).toBe('clip');
expect(metadata.get('clip.has_vision_encoder')).toBe(true);
expect(metadata.get('clip.has_audio_encoder')).toBe(true);
expect(metadata.get('clip.audio.num_mel_bins')).toBe(128);
expect(metadata.has('clip.vision.image_mean')).toBe(false);
});
it('returns what it read before a buffer cut short', () => {
const full = gguf(MMPROJ);
const { complete, metadata } = readGgufMetadata(full.slice(0, full.byteLength - 20));
expect(complete).toBe(false);
expect(metadata.get('clip.has_vision_encoder')).toBe(true);
expect(metadata.has('clip.audio.num_mel_bins')).toBe(false);
});
it('returns nothing for a file that is not GGUF', () => {
expect(
readGgufMetadata(new TextEncoder().encode('not a gguf file at all...').buffer)
).toStrictEqual({ complete: false, metadata: new Map() });
expect(readGgufMetadata(new ArrayBuffer(0))).toStrictEqual({
complete: false,
metadata: new Map()
});
});
});