Skip to content

Commit cf14432

Browse files
authored
[safetnesors] increase index.json parse limit from 10MB -> 20MB (#1790)
## Fix parsing of large safetensors index files (>10MB) ### Problem Models with large index files (>10MB) were failing to parse. The code was truncating index files at 10MB, causing JSON parsing errors for models with many experts/shards. **Failure example:** [moonshotai/Kimi-K2-Instruct-0905](https://huggingface.co/moonshotai/Kimi-K2-Instruct-0905) has a 13.5MB index file (384 experts per layer, 62 shards) and was failing with: ``` Failed to parse file model.safetensors.index.json: not a valid JSON. ``` ### Solution - Increased index file size limit from 10MB to 20MB in `parse-safetensors-metadata.ts` - Added test case for large index files using the moonshotai model ### Changes - `packages/hub/src/lib/parse-safetensors-metadata.ts`: Bump limit from `10_000_000` to `20_000_000` bytes - `packages/hub/src/lib/parse-safetensors-metadata.spec.ts`: Add comprehensive test for large index files
1 parent e081bf7 commit cf14432

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

packages/hub/src/lib/parse-safetensors-metadata.spec.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,4 +302,15 @@ describe("parseSafetensorsMetadata", () => {
302302
assert.strictEqual(parameterCount.FP4, 20000);
303303
assert.strictEqual(parameterCount.UE8, 5000);
304304
});
305+
306+
it("fetch info for large index file (>10MB) with many experts (moonshotai/Kimi-K2-Instruct-0905)", async () => {
307+
// This model has a 13.5MB index file due to having 384 experts per layer
308+
const parse = await parseSafetensorsMetadata({
309+
repo: "moonshotai/Kimi-K2-Instruct-0905",
310+
revision: "7152993552508c9f22042b3bb93b5e6acd06ce73",
311+
});
312+
313+
assert(parse.sharded);
314+
assert.strictEqual(Object.keys(parse.headers).length, 62);
315+
});
305316
});

packages/hub/src/lib/parse-safetensors-metadata.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ async function parseShardedIndex(
190190

191191
try {
192192
// no validation for now, we assume it's a valid IndexJson.
193-
const index = JSON.parse(await indexBlob.slice(0, 10_000_000).text());
193+
const index = JSON.parse(await indexBlob.slice(0, 20_000_000).text());
194194
return index;
195195
} catch (error) {
196196
throw new SafetensorParseError(`Failed to parse file ${path}: not a valid JSON.`);

0 commit comments

Comments
 (0)