File size: 8,908 Bytes
21dd449 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 |
import type { CredentialsParams, RepoDesignation } from "../types/public";
import { omit } from "../utils/omit";
import { toRepoId } from "../utils/toRepoId";
import { typedEntries } from "../utils/typedEntries";
import { downloadFile } from "./download-file";
import { fileExists } from "./file-exists";
import { promisesQueue } from "../utils/promisesQueue";
import type { SetRequired } from "../vendor/type-fest/set-required";
export const SAFETENSORS_FILE = "model.safetensors";
export const SAFETENSORS_INDEX_FILE = "model.safetensors.index.json";
/// We advise model/library authors to use the filenames above for convention inside model repos,
/// but in some situations safetensors weights have different filenames.
export const RE_SAFETENSORS_FILE = /\.safetensors$/;
export const RE_SAFETENSORS_INDEX_FILE = /\.safetensors\.index\.json$/;
export const RE_SAFETENSORS_SHARD_FILE =
/^(?<prefix>(?<basePrefix>.*?)[_-])(?<shard>\d{5})-of-(?<total>\d{5})\.safetensors$/;
export interface SafetensorsShardFileInfo {
prefix: string;
basePrefix: string;
shard: string;
total: string;
}
export function parseSafetensorsShardFilename(filename: string): SafetensorsShardFileInfo | null {
const match = RE_SAFETENSORS_SHARD_FILE.exec(filename);
if (match && match.groups) {
return {
prefix: match.groups["prefix"],
basePrefix: match.groups["basePrefix"],
shard: match.groups["shard"],
total: match.groups["total"],
};
}
return null;
}
const PARALLEL_DOWNLOADS = 20;
const MAX_HEADER_LENGTH = 25_000_000;
class SafetensorParseError extends Error {}
type FileName = string;
export type TensorName = string;
export type Dtype = "F64" | "F32" | "F16" | "BF16" | "I64" | "I32" | "I16" | "I8" | "U8" | "BOOL";
export interface TensorInfo {
dtype: Dtype;
shape: number[];
data_offsets: [number, number];
}
export type SafetensorsFileHeader = Record<TensorName, TensorInfo> & {
__metadata__: Record<string, string>;
};
export interface SafetensorsIndexJson {
dtype?: string;
/// ^there's sometimes a dtype but it looks inconsistent.
metadata?: Record<string, string>;
/// ^ why the naming inconsistency?
weight_map: Record<TensorName, FileName>;
}
export type SafetensorsShardedHeaders = Record<FileName, SafetensorsFileHeader>;
export type SafetensorsParseFromRepo =
| {
sharded: false;
header: SafetensorsFileHeader;
parameterCount?: Partial<Record<Dtype, number>>;
}
| {
sharded: true;
index: SafetensorsIndexJson;
headers: SafetensorsShardedHeaders;
parameterCount?: Partial<Record<Dtype, number>>;
};
async function parseSingleFile(
path: string,
params: {
repo: RepoDesignation;
revision?: string;
hubUrl?: string;
/**
* Custom fetch function to use instead of the default one, for example to use a proxy or edit headers.
*/
fetch?: typeof fetch;
} & Partial<CredentialsParams>
): Promise<SafetensorsFileHeader> {
const blob = await downloadFile({ ...params, path });
if (!blob) {
throw new SafetensorParseError(`Failed to parse file ${path}: failed to fetch safetensors header length.`);
}
const bufLengthOfHeaderLE = await blob.slice(0, 8).arrayBuffer();
const lengthOfHeader = new DataView(bufLengthOfHeaderLE).getBigUint64(0, true);
// ^little-endian
if (lengthOfHeader <= 0) {
throw new SafetensorParseError(`Failed to parse file ${path}: safetensors header is malformed.`);
}
if (lengthOfHeader > MAX_HEADER_LENGTH) {
throw new SafetensorParseError(
`Failed to parse file ${path}: safetensor header is too big. Maximum supported size is ${MAX_HEADER_LENGTH} bytes.`
);
}
try {
// no validation for now, we assume it's a valid FileHeader.
const header: SafetensorsFileHeader = JSON.parse(await blob.slice(8, 8 + Number(lengthOfHeader)).text());
return header;
} catch (err) {
throw new SafetensorParseError(`Failed to parse file ${path}: safetensors header is not valid JSON.`);
}
}
async function parseShardedIndex(
path: string,
params: {
repo: RepoDesignation;
revision?: string;
hubUrl?: string;
/**
* Custom fetch function to use instead of the default one, for example to use a proxy or edit headers.
*/
fetch?: typeof fetch;
} & Partial<CredentialsParams>
): Promise<{ index: SafetensorsIndexJson; headers: SafetensorsShardedHeaders }> {
const indexBlob = await downloadFile({
...params,
path,
});
if (!indexBlob) {
throw new SafetensorParseError(`Failed to parse file ${path}: failed to fetch safetensors index.`);
}
// no validation for now, we assume it's a valid IndexJson.
let index: SafetensorsIndexJson;
try {
index = JSON.parse(await indexBlob.slice(0, 10_000_000).text());
} catch (error) {
throw new SafetensorParseError(`Failed to parse file ${path}: not a valid JSON.`);
}
const pathPrefix = path.slice(0, path.lastIndexOf("/") + 1);
const filenames = [...new Set(Object.values(index.weight_map))];
const shardedMap: SafetensorsShardedHeaders = Object.fromEntries(
await promisesQueue(
filenames.map(
(filename) => async () =>
[filename, await parseSingleFile(pathPrefix + filename, params)] satisfies [string, SafetensorsFileHeader]
),
PARALLEL_DOWNLOADS
)
);
return { index, headers: shardedMap };
}
/**
* Analyze model.safetensors.index.json or model.safetensors from a model hosted
* on Hugging Face using smart range requests to extract its metadata.
*/
export async function parseSafetensorsMetadata(
params: {
/** Only models are supported */
repo: RepoDesignation;
/**
* Relative file path to safetensors file inside `repo`. Defaults to `SAFETENSORS_FILE` or `SAFETENSORS_INDEX_FILE` (whichever one exists).
*/
path?: string;
/**
* Will include SafetensorsParseFromRepo["parameterCount"], an object containing the number of parameters for each DType
*
* @default false
*/
computeParametersCount: true;
hubUrl?: string;
revision?: string;
/**
* Custom fetch function to use instead of the default one, for example to use a proxy or edit headers.
*/
fetch?: typeof fetch;
} & Partial<CredentialsParams>
): Promise<SetRequired<SafetensorsParseFromRepo, "parameterCount">>;
export async function parseSafetensorsMetadata(
params: {
/** Only models are supported */
repo: RepoDesignation;
/**
* Will include SafetensorsParseFromRepo["parameterCount"], an object containing the number of parameters for each DType
*
* @default false
*/
path?: string;
computeParametersCount?: boolean;
hubUrl?: string;
revision?: string;
/**
* Custom fetch function to use instead of the default one, for example to use a proxy or edit headers.
*/
fetch?: typeof fetch;
} & Partial<CredentialsParams>
): Promise<SafetensorsParseFromRepo>;
export async function parseSafetensorsMetadata(
params: {
repo: RepoDesignation;
path?: string;
computeParametersCount?: boolean;
hubUrl?: string;
revision?: string;
/**
* Custom fetch function to use instead of the default one, for example to use a proxy or edit headers.
*/
fetch?: typeof fetch;
} & Partial<CredentialsParams>
): Promise<SafetensorsParseFromRepo> {
const repoId = toRepoId(params.repo);
if (repoId.type !== "model") {
throw new TypeError("Only model repos should contain safetensors files.");
}
if (RE_SAFETENSORS_FILE.test(params.path ?? "") || (await fileExists({ ...params, path: SAFETENSORS_FILE }))) {
const header = await parseSingleFile(params.path ?? SAFETENSORS_FILE, params);
return {
sharded: false,
header,
...(params.computeParametersCount && {
parameterCount: computeNumOfParamsByDtypeSingleFile(header),
}),
};
} else if (
RE_SAFETENSORS_INDEX_FILE.test(params.path ?? "") ||
(await fileExists({ ...params, path: SAFETENSORS_INDEX_FILE }))
) {
const { index, headers } = await parseShardedIndex(params.path ?? SAFETENSORS_INDEX_FILE, params);
return {
sharded: true,
index,
headers,
...(params.computeParametersCount && {
parameterCount: computeNumOfParamsByDtypeSharded(headers),
}),
};
} else {
throw new Error("model id does not seem to contain safetensors weights");
}
}
function computeNumOfParamsByDtypeSingleFile(header: SafetensorsFileHeader): Partial<Record<Dtype, number>> {
const counter: Partial<Record<Dtype, number>> = {};
const tensors = omit(header, "__metadata__");
for (const [, v] of typedEntries(tensors)) {
if (v.shape.length === 0) {
continue;
}
counter[v.dtype] = (counter[v.dtype] ?? 0) + v.shape.reduce((a, b) => a * b);
}
return counter;
}
function computeNumOfParamsByDtypeSharded(shardedMap: SafetensorsShardedHeaders): Partial<Record<Dtype, number>> {
const counter: Partial<Record<Dtype, number>> = {};
for (const header of Object.values(shardedMap)) {
for (const [k, v] of typedEntries(computeNumOfParamsByDtypeSingleFile(header))) {
counter[k] = (counter[k] ?? 0) + (v ?? 0);
}
}
return counter;
}
|