@@ -41,50 +41,6 @@ def detect_gguf_multimodal(model: str) -> Path | None:
4141 return None
4242
4343
44- def extract_vocab_size_from_gguf (model_path : str ) -> int | None :
45- """Extract vocabulary size from GGUF file's embedding tensor.
46-
47- Reads the token_embd.weight tensor shape to determine the actual
48- vocabulary size in the GGUF file. This is the source of truth for
49- GGUF models and may differ from the HuggingFace tokenizer config,
50- particularly for models with extended vocabularies (e.g., Unsloth).
51-
52- Args:
53- model_path: Path to GGUF model file
54-
55- Returns:
56- Vocabulary size (second dimension of token_embd.weight tensor)
57- or None if extraction fails
58-
59- Note:
60- GGUF embedding tensor format: [hidden_size, vocab_size]
61- We extract vocab_size from shape[1]
62- """
63- try :
64- reader = gguf .GGUFReader (model_path )
65- for tensor in reader .tensors :
66- if tensor .name == "token_embd.weight" :
67- # Tensor shape: [hidden_size, vocab_size]
68- vocab_size = int (tensor .shape [1 ])
69- logger .info (
70- "Extracted vocab_size=%d from GGUF embedding tensor" ,
71- vocab_size ,
72- )
73- return vocab_size
74-
75- logger .warning (
76- "Could not find token_embd.weight tensor in GGUF file: %s" ,
77- model_path ,
78- )
79- return None
80- except Exception as e :
81- logger .warning (
82- "Failed to extract vocab_size from GGUF file %s: %s" ,
83- model_path ,
84- e ,
85- )
86- return None
87-
8844
8945def extract_vision_config_from_gguf (mmproj_path : str ) -> "SiglipVisionConfig | None" :
9046 """Extract vision config parameters from mmproj.gguf metadata.
@@ -208,21 +164,4 @@ def maybe_patch_hf_config_from_gguf(
208164 )
209165 hf_config = new_hf_config
210166
211- # Override vocab_size from GGUF embedding tensor for all GGUF models
212- # This handles models with extended vocabularies (e.g., Unsloth)
213- if model .endswith (".gguf" ):
214- vocab_size_from_gguf = extract_vocab_size_from_gguf (model )
215- if vocab_size_from_gguf is not None :
216- # Get text config (handles both regular and multimodal configs)
217- text_config = hf_config .get_text_config ()
218- original_vocab_size = text_config .vocab_size
219-
220- if original_vocab_size != vocab_size_from_gguf :
221- logger .info (
222- "Overriding vocab_size: %d (HF config) → %d (GGUF file)" ,
223- original_vocab_size ,
224- vocab_size_from_gguf ,
225- )
226- text_config .vocab_size = vocab_size_from_gguf
227-
228167 return hf_config
0 commit comments