Line data Source code
1 2356 : const MAX_REPLACEMENTS = 3
2 :
3 2356 : const QUANTIZATION_TAG = new RegExp(
4 : String.raw`(?<![a-z0-9])(?:` +
5 : [
6 : String.raw`(?:nv|mx)?fp\d+(?:[_-]?e\d+m\d+(?:fn)?(?:uz)?)?`,
7 : String.raw`e\d+m\d+(?:fn)?(?:uz)?`,
8 : String.raw`bf16`,
9 : String.raw`f(?:16|32)`,
10 : String.raw`u?int\d+`,
11 : String.raw`\d+bit`,
12 : String.raw`nf4`,
13 : String.raw`w\d+a\d+`,
14 : String.raw`a\d+w\d+`,
15 : String.raw`[it]?q\d+(?:[_-](?:k|\d|xxs|xs|nl|s|m|l))*`,
16 : String.raw`gptq|awq|bnb|svdq|convrot`
17 : ].join('|') +
18 : String.raw`)(?:[_-]?(?:mixed|scaled|quant(?:ized)?))*(?![a-z0-9])`,
19 : 'g'
20 : )
21 :
22 : interface ModelPath {
23 : fileName: string
24 : folder: string
25 : variantKey: string
26 : }
27 :
28 2906 : function parseModelPath(path: string): ModelPath {
29 2906 : const parts = path.toLowerCase().replace(/\\/g, '/').split('/')
30 2906 : const fileName = parts.pop() ?? ''
31 2906 : const dot = fileName.lastIndexOf('.')
32 2906 : const stem = dot > 0 ? fileName.slice(0, dot) : fileName
33 2906 : const extension = dot > 0 ? fileName.slice(dot) : ''
34 2906 : const stemKey = stem
35 : .replace(QUANTIZATION_TAG, ' ')
36 : .split(/[^a-z0-9.]+/)
37 : .map((part) => part.replace(/^\.+|\.+$/g, ''))
38 : .filter(Boolean)
39 : .join('_')
40 2906 : return {
41 : fileName,
42 : folder: parts.join('/'),
43 : variantKey: stemKey && stemKey + extension
44 : }
45 : }
46 :
47 23 : function isSameFolderOrUnfoldered(a: ModelPath, b: ModelPath): boolean {
48 23 : return !a.folder || !b.folder || a.folder === b.folder
49 : }
50 :
51 : /**
52 : * Available options likely meant by a missing model value: the same file in
53 : * another folder first, then the same model at a different quantization.
54 : */
55 2883 : export function findModelReplacements(
56 : missingName: string,
57 : options: readonly (string | number)[]
58 : ): string[] {
59 2883 : const missing = parseModelPath(missingName)
60 2883 : const candidates = options.flatMap((option) => {
61 23 : if (typeof option !== 'string' || option === missingName) return []
62 23 : const path = parseModelPath(option)
63 23 : return isSameFolderOrUnfoldered(missing, path) ? [{ option, path }] : []
64 : })
65 :
66 2883 : const sameFile = candidates.filter(
67 : ({ path }) => path.fileName === missing.fileName
68 : )
69 2883 : const sameModel = missing.variantKey
70 : ? candidates.filter(
71 : ({ path }) =>
72 : path.fileName !== missing.fileName &&
73 : path.variantKey === missing.variantKey
74 : )
75 : : []
76 :
77 2883 : return [
78 : ...new Set([...sameFile, ...sameModel].map(({ option }) => option))
79 : ].slice(0, MAX_REPLACEMENTS)
80 : }
|