Picking model weights in Add model or Download more also selects the files that usually go with them (chat template, MTP head, mmproj, ...), shown in a strip where each can be unchecked. Variants are matched by quant, otherwise the best single one; kinds already selected aren't added twice. Which kinds is set in Settings -> Downloads. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
94 lines
4.2 KiB
TypeScript
94 lines
4.2 KiB
TypeScript
// Files that usually go with the model weights you pick: chat template, MTP head, vision
|
|
// projector (mmproj), … Which kinds is a setting. Shared by UI and server (pure functions).
|
|
|
|
import { groupSplitFiles } from "./file-groups";
|
|
import { fileRole, type FileRole } from "./file-roles";
|
|
import { quantGroup } from "./selection";
|
|
|
|
/** File kinds that can be recommended, in display order. */
|
|
export const RECOMMENDABLE_ROLES: FileRole[] = ["template", "mtp", "vision", "draft", "config", "tokenizer", "imatrix", "adapter", "docs"];
|
|
export const DEFAULT_RECOMMENDED_ROLES: FileRole[] = ["template", "mtp", "vision"];
|
|
|
|
/** Kinds where a repo has alternatives (one per quant or precision) and one is enough. */
|
|
const VARIANT_ROLES = new Set<FileRole>(["mtp", "vision", "draft", "adapter", "imatrix"]);
|
|
/** Preferred precision when no variant matches the picked quant. */
|
|
const PRECISION_ORDER = ["f16", "bf16", "q8_0", "f32"];
|
|
|
|
const basename = (p: string) => p.slice(p.lastIndexOf("/") + 1);
|
|
const dirname = (p: string) => (p.includes("/") ? p.slice(0, p.lastIndexOf("/")) : "");
|
|
|
|
/** Quant/precision tag from a file name: "mtp-X-Q4_0.gguf" → "q4_0", "mmproj-F16.gguf" → "f16". */
|
|
export function quantTag(path: string): string | null {
|
|
const g = quantGroup(basename(path));
|
|
return g ? g.replace(/^mmproj-/i, "").toLowerCase() : null;
|
|
}
|
|
|
|
const withoutUd = (q: string) => q.replace(/^ud-/, "");
|
|
const bits = (q: string) => q.match(/^(?:ud-)?(?:i?q|tq|bf|f)(\d+)/)?.[1] ?? null;
|
|
|
|
/**
|
|
* Paths to download along with `modelPaths` (model weights), for the given kinds. Variant kinds
|
|
* (MTP, mmproj, …) get the variant matching a picked quant, else the best single one; other kinds
|
|
* (template, config, …) get every such file at the repo root or next to a picked model file.
|
|
* `have`: everything already selected; a variant kind that's already there isn't added again.
|
|
*/
|
|
export function recommendFiles(
|
|
files: { path: string; size: number }[],
|
|
modelPaths: string[],
|
|
roles: FileRole[],
|
|
have: string[] = [],
|
|
): string[] {
|
|
if (modelPaths.length === 0 || roles.length === 0) return [];
|
|
const picked = new Set([...modelPaths, ...have]);
|
|
const haveRoles = new Set([...picked].map(fileRole));
|
|
const quants = modelPaths.map(quantTag).filter((q): q is string => !!q);
|
|
const dirs = new Set(["", ...modelPaths.map(dirname)]);
|
|
const out: string[] = [];
|
|
|
|
for (const role of roles) {
|
|
const entries = groupSplitFiles(files.filter((f) => !picked.has(f.path) && fileRole(f.path) === role));
|
|
if (entries.length === 0) continue;
|
|
if (!VARIANT_ROLES.has(role)) {
|
|
for (const e of entries) if (dirs.has(dirname(e.path))) out.push(...e.parts.map((p) => p.path));
|
|
continue;
|
|
}
|
|
if (haveRoles.has(role)) continue; // e.g. an mmproj is already selected
|
|
if (entries.length === 1) {
|
|
out.push(...entries[0].parts.map((p) => p.path));
|
|
continue;
|
|
}
|
|
const tag = (e: (typeof entries)[number]) => quantTag(e.path) ?? "";
|
|
// Same quant as a picked model file (ignoring unsloth's "UD-" prefix): take those.
|
|
const same = entries.filter((e) => quants.some((q) => withoutUd(q) === withoutUd(tag(e))));
|
|
if (same.length > 0) {
|
|
for (const e of same) out.push(...e.parts.map((p) => p.path));
|
|
continue;
|
|
}
|
|
// Otherwise one variant: next to the model, then preferred precision, then same bit width, then smallest.
|
|
const pickedBits = new Set(quants.map(bits).filter(Boolean));
|
|
const score = (e: (typeof entries)[number]) => {
|
|
const t = tag(e);
|
|
const precision = PRECISION_ORDER.indexOf(t);
|
|
return [
|
|
dirs.has(dirname(e.path)) && dirname(e.path) !== "" ? 0 : 1,
|
|
precision === -1 ? PRECISION_ORDER.length : precision,
|
|
pickedBits.has(bits(t)) ? 0 : 1,
|
|
e.size,
|
|
];
|
|
};
|
|
const best = [...entries].sort((a, b) => {
|
|
const sa = score(a);
|
|
const sb = score(b);
|
|
for (let i = 0; i < sa.length; i++) if (sa[i] !== sb[i]) return sa[i] - sb[i];
|
|
return 0;
|
|
})[0];
|
|
out.push(...best.parts.map((p) => p.path));
|
|
}
|
|
return [...new Set(out)];
|
|
}
|
|
|
|
/** Model-weight paths among `paths`. */
|
|
export function modelFilePaths(paths: Iterable<string>): string[] {
|
|
return [...paths].filter((p) => fileRole(p) === "model");
|
|
}
|