
    .`i^%              	          d Z ddlmZ ddlmZ ddlmZ ddlZddlZ	ddl
mZmZ ddlmZ ddlmZmZmZ dd	lmZ d
dlmZ  ee          Zedeez  defd            Zedeez  defd            ZdedefdZdeez  deeef         fdZdeez  defdZ dededz  fdZ!deddfdZ"dededefdZ#	 ddeez  dededz  defdZ$dS )zGGUF utility functions.    )cache)PathLike)PathN)KeysVisionProjectorType)GGMLQuantizationType)Gemma3ConfigPretrainedConfigSiglipVisionConfig)init_logger   )list_filtered_repo_filesmodelreturnc                 `   t          |           } |                                 sdS | j        dk    rdS 	 |                     d          5 }|                    d          }ddd           n# 1 swxY w Y   |dk    S # t
          $ r'}t                              d| |           Y d}~dS d}~ww xY w)	z"Check if the file is a GGUF model.F.ggufTrb   Ns   GGUFzError reading file %s: %s)r   is_filesuffixopenread	Exceptionloggerdebug)r   fheaderes       v/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/vllm/transformers_utils/gguf_utils.pycheck_gguf_filer       s     KKE==?? u		 	 tZZ 	VVAYYF	 	 	 	 	 	 	 	 	 	 	 	 	 	 	      0%;;;uuuuus;   A< 	A+A< +A//A< 2A/3A< <
B-B((B-c                     d}t          |           } t          j        ||           r(|                     dd          \  }}t	          |          S dS )z*Check if the model is a remote GGUF model.zG^[a-zA-Z0-9][a-zA-Z0-9._-]*/[a-zA-Z0-9][a-zA-Z0-9._-]*:[A-Za-z0-9_+-]+$:r   F)strre	fullmatchrsplitis_valid_gguf_quant_type)r   pattern_
quant_types       r   is_remote_ggufr+   )   sS     YGJJE	|GU## 4S!,,:'
3335    gguf_quant_typec                 2    t          t          | d          duS )z3Check if the quant type is a valid GGUF quant type.N)getattrr   )r-   s    r   r'   r'   4   s    '$??tKKr,   c                     t          |           } t          |           r&|                     dd          }|d         |d         fS t          d|  dt          j                   )z,Split the model into repo_id and quant type.r"   r   r   z-Wrong GGUF model or invalid GGUF quant type: zT.
- It should be in repo_id:quant_type format.
- Valid GGMLQuantizationType values: )r#   r+   r&   
ValueErrorr   _member_names_)r   partss     r   split_remote_ggufr4   9   s{    JJEe $S!$$a%(##
	V 	V 	V0D0S	V 	V  r,   c                 `    t          |           } t          |           rdS t          |           S )zCheck if the model is a GGUF model.

    Args:
        model: Model name, path, or Path object to check.

    Returns:
        True if the model is a GGUF model, False otherwise.
    T)r#   r    r+   )r   s    r   is_ggufr6   F   s6     JJE u t %   r,   c                    |                      d          sdS 	 t          |           }|                                sdS |j        }g d}|D ]0}t	          |                    |                    }|r
|d         c S 1dS # t          $ r Y dS w xY w)zCheck if GGUF model has multimodal projector file.

    Args:
        model: Model path string

    Returns:
        Path to mmproj file if found, None otherwise
    r   N)zmmproj.ggufzmmproj-*.ggufz*mmproj*.ggufr   )endswithr   r   parentlistglobr   )r   
model_path	model_dirmmproj_patternsr(   mmproj_filess         r   detect_gguf_multimodalr@   Y   s     >>'"" t%[[
!!## 	4%	KKK& 	' 	'G	w 7 788L '#A&&&'t   tts   #A> <A> ;A> >
BBmmproj_pathzSiglipVisionConfig | Nonec                    t          j        t          |                     }d}|                    t          j        j                  }|rh	 t          |j        d                   	                    d          }n9# t          t          f$ r%}t                              d|           Y d}~nd}~ww xY wt          j        j        dt           ft          j        j        dt           ft          j        j        dt           ft          j        j        j        dt           ft          j        j        d	t           ft          j        j        d
t           ft          j        j        j        dt0          fi}i }|                                D ]V\  }\  }}	|                    |          }
|
t                              d|            dS  |	|
j        d                   ||<   W|t4          j        k    rd|d<   t                              d           t;          di |}|rt                              d|           nt                              d           |S )a[  Extract vision config parameters from mmproj.gguf metadata.

    Reads vision encoder configuration from GGUF metadata fields using
    standardized GGUF constants. Automatically detects the projector type
    (e.g., gemma3, llama4) and applies model-specific parameters accordingly.

    The function extracts standard CLIP vision parameters from GGUF metadata
    and applies projector-type-specific customizations. For unknown projector
    types, it uses safe defaults from SiglipVisionConfig.

    Args:
        mmproj_path: Path to mmproj.gguf file (str or Path)

    Returns:
        SiglipVisionConfig if extraction succeeds, None if any required
        field is missing from the GGUF metadata

    Raises:
        Exception: Exceptions from GGUF reading (file not found, corrupted
            file, etc.) propagate directly from gguf.GGUFReader
    Nzutf-8z-Failed to decode projector type from GGUF: %shidden_sizeintermediate_sizenum_hidden_layersnum_attention_heads
image_size
patch_sizelayer_norm_epsz8Missing required vision config field '%s' in mmproj.ggufFvision_use_headz8Detected Gemma3 projector, disabling vision pooling headz=Extracted vision config from mmproj.gguf (projector_type: %s)z1Extracted vision config from mmproj.gguf metadata )gguf
GGUFReaderr#   	get_fieldr   ClipPROJECTOR_TYPEbytesr3   decodeAttributeErrorUnicodeDecodeErrorr   warning
ClipVisionEMBEDDING_LENGTHintFEED_FORWARD_LENGTHBLOCK_COUNT	Attention
HEAD_COUNT
IMAGE_SIZE
PATCH_SIZELAYERNORM_EPSfloatitemsr   GEMMA3infor   )rA   readerprojector_typeprojector_type_fieldr   VISION_CONFIG_FIELDSconfig_paramsgguf_key
param_namedtypefieldconfigs               r   extract_vision_config_from_ggufro   u   sJ   , _S--..F N!++DI,DEE O	O"#7#=b#ABBII'RRNN 23 	O 	O 	ONNJANNNNNNNN	O 	(=#*>+.A3-G#&93%?!,/Dc.J"\3$7"\3$7!/2BE1J M)=)C)C)E)E 	; 	;%%:u  **=NNJ   44$)E%+b/$:$:j!! ,333 ,1'(NOOO  00-00F IK	
 	
 	
 	

 	GHHHMs   -A9 9B/
B**B/	hf_configc                     t          |           }|Wt          t          |                    }|                                }|j        dv }||rt          j        ||dg          }|}|S )aC  Patch HF config for GGUF models.

    Applies GGUF-specific patches to HuggingFace config:
    1. For multimodal models: patches architecture and vision config
    2. For all GGUF models: overrides vocab_size from embedding tensor

    This ensures compatibility with GGUF models that have extended
    vocabularies (e.g., Unsloth) where the GGUF file contains more
    tokens than the HuggingFace tokenizer config specifies.

    Args:
        model: Model path string
        hf_config: HuggingFace config to patch in-place

    Returns:
        Updated HuggingFace config
    N)gemma3gemma3_textGemma3ForConditionalGeneration)text_configvision_configarchitectures)r@   ro   r#   get_text_config
model_typer	   from_text_vision_configs)r   rp   rA   rv   ru   	is_gemma3new_hf_configs          r   maybe_patch_hf_config_from_ggufr}      s    , )//K7K8H8HII  //11(,EE	$$(A'+?@  M
 &Ir,   repo_idr*   revisionc                     t          |           } d| dd| dd| dd| dg}t          | ||          }t          |          dk    rt          d| |          |                    d 	           |d         }|S )
as  Get the GGUF file path from HuggingFace Hub based on repo_id and quant_type.

    Args:
        repo_id: The HuggingFace repository ID (e.g., "Qwen/Qwen3-0.6B")
        quant_type: The quantization type (e.g., "Q4_K_M", "F16")
        revision: Optional revision/branch name

    Returns:
        The path to the GGUF file on HuggingFace Hub (e.g., "filename.gguf"),
    z*-r   z-*.ggufz*/*-)allow_patternsr   r   z:Could not find GGUF file for repo %s with quantization %s.c                 0    |                      d          | fS )N-)count)xs    r   <lambda>z,get_gguf_file_path_from_hf.<locals>.<lambda>  s    qwws||Q&7 r,   )key)r#   r   lenr1   sort)r~   r*   r   gguf_patternsmatching_filesgguf_filenames         r   get_gguf_file_path_from_hfr      s     'llGZ Z    z   "z"""	M .$  N >aH
 
 	
 77888"1%Mr,   )N)%__doc__	functoolsr   osr   pathlibr   rM   regexr$   gguf.constantsr   r   gguf.quantsr   transformersr	   r
   r   vllm.loggerr   
repo_utilsr   __name__r   r#   boolr    r+   r'   tupler4   r6   r@   ro   r}   r   rL   r,   r   <module>r      s                            4 4 4 4 4 4 4 4 , , , , , , K K K K K K K K K K # # # # # # 0 0 0 0 0 0	X		 3> d    $ #*     Lc Ld L L L L

S4Z 
E#s(O 
 
 
 
!3: !$ ! ! ! !&# $+    8R R9T R R R Rj%%% % % % %V  & &4Z&& Dj& 		& & & & & &r,   