Skip to content

Commit 4937e3b

Browse files
authored
Make safetensor shard filename parsing more flexible (#1817)
Context: A lot of deepseek models have unconventional number of digits for the total shard number in their filename, which leads to a filename parsing fail. It breaks the file info navigation on the hub: https://github.com/user-attachments/assets/75676c00-387c-4a92-808d-0d84caba7e51 [here](https://huggingface.co/deepseek-ai/DeepSeek-V3.2-Exp?show_file_info=model-00001-of-000163.safetensors) for instance, `model-00001-of-000163.safetensors` should be `model-00001-of-00163.safetensors` (there's 1 extra "0" in "model-00001-of-.safetensors") Most of deepseek models with a lot of shards have this issue, since they are important models, I suggest we make the regex more flexible to accept a variable number of digits for shards in the filename. Would that be acceptable?
1 parent a75c7a2 commit 4937e3b

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,16 @@ describe("parseSafetensorsMetadata", () => {
143143
assert.strictEqual(safetensorsShardFileInfo?.total, "00072");
144144
});
145145

146+
it("should detect sharded safetensors filename with 6 digits", async () => {
147+
const safetensorsFilename = "model-00001-of-000163.safetensors"; // https://huggingface.co/deepseek-ai/DeepSeek-V3.2-Exp/blob/main/model-00001-of-000163.safetensors
148+
const safetensorsShardFileInfo = parseSafetensorsShardFilename(safetensorsFilename);
149+
150+
assert.strictEqual(safetensorsShardFileInfo?.prefix, "model-");
151+
assert.strictEqual(safetensorsShardFileInfo?.basePrefix, "model");
152+
assert.strictEqual(safetensorsShardFileInfo?.shard, "00001");
153+
assert.strictEqual(safetensorsShardFileInfo?.total, "000163");
154+
});
155+
146156
it("should support sub-byte data types", async () => {
147157
const newDataTypes: Array<"F4" | "F6_E2M3" | "F6_E3M2" | "E8M0"> = ["F4", "F6_E2M3", "F6_E3M2", "E8M0"];
148158

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export const SAFETENSORS_INDEX_FILE = "model.safetensors.index.json";
1414
export const RE_SAFETENSORS_FILE = /\.safetensors$/;
1515
export const RE_SAFETENSORS_INDEX_FILE = /\.safetensors\.index\.json$/;
1616
export const RE_SAFETENSORS_SHARD_FILE =
17-
/^(?<prefix>(?<basePrefix>.*?)[_-])(?<shard>\d{5})-of-(?<total>\d{5})\.safetensors$/;
17+
/^(?<prefix>(?<basePrefix>.*?)[_-])(?<shard>\d{5,6})-of-(?<total>\d{5,6})\.safetensors$/;
1818
export interface SafetensorsShardFileInfo {
1919
prefix: string;
2020
basePrefix: string;

0 commit comments

Comments
 (0)