
    (HJjz                        d dl 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 d dlmZ ddlmZmZmZmZmZ 	 ddedej*                  d	ed
eeej*                  egeeef   f   fdZg dZg dZ	 	 	 	 	 	 	 	 	 	 	 d dedededee   dee   dedee   dedee   dedeeeeej*                  egeeef   f   ef      defdZd
e j:                  fdZd Ze dk(  r e!d        e        yy)!    N)Path)CallableOptionalUnion)tree_map_with_path   )dequantize_modelloadquantize_modelsaveupload_to_hubrecipemodel
group_sizereturnc                   	
 d
d| dk(  rd	n(| dk(  rd	dn| dk(  rd	n| d	k(  rd	nt        d
|        |j                         D cg c]  \  }}d|v s| }}}t        |      dk(  rt        d      t        |d   j	                  d            D ]  \  }|j                         s n t        |j                        dt        dt        j                  dt        t        t        f   f	
fd}|S c c}}w )Naffine   	mixed_2_6   	mixed_3_4      	mixed_3_6	mixed_4_6zInvalid quant recipe 	down_projr   z2Model does not have expected keys for mixed quant..pathmoduler   c                 &   t        | j                  d            kD  rt        | j                  d               nd}|	dz  k  xs |d	z  dz  k\  xs |	dz  z
  dz  dk(  }d| v sd| v sd	| v r|rd
S d| v r|rd
S d| v rd
S d
S )a=  Implements mixed quantization predicates with similar choices to, for example, llama.cpp's Q4_K_M.
        Ref: https://github.com/ggerganov/llama.cpp/blob/917786f43d0f29b7c77a0c56767c0fa4df68b1c5/src/llama.cpp#L5265
        By Alex Barron: https://gist.github.com/barronalex/84addb8078be21969f1690c1454855f3
        r   r         r   r   v_projv_a_projv_b_proj)r   bitsmoder   lm_head)lensplitint)
r   r   indexuse_more_bitsr   	high_bitslayer_locationlow_bitsr'   
num_layerss
       X/Users/ahmed/devFolder/claude-voice/.venv/lib/python3.12/site-packages/mlx_lm/convert.pymixed_quant_predicatez<mixed_quant_predicate_builder.<locals>.mixed_quant_predicate0   s     4::c?#n4 

3/0 	 J!O# 2J!++2
a'1,1 	 
d 2jD6H",iNN$=",iNN",iNN((DII    )
ValueErrornamed_modulesr)   	enumerater*   isdigitlayersstrnnModuler   booldict)r   r   r   k_	down_keysr3   r.   r/   r0   r'   r1   s     `    @@@@@r2   mixed_quant_predicate_builderrB      s$    DI	;			;		;	09::$224I4tq!q8H4II
9~MNN 'y|'9'9#'>?99; @ U\\"JJJ		J 
tTz	J J: ! O Js   C=C=)r   r   r   r   )float16bfloat16float32hf_pathmlx_pathquantizeq_group_sizeq_bitsq_modedtypeupload_reporevision
dequantizequant_predicatetrust_remote_codec                 f   t        |t              rt        |      }|j                         rt	        d| d      t        d       t        | |dd|id      \  }}}t        |
t              r|dk7  rt	        d      t        |
||      }
|j                  d	d       &|j                  d
d       x}r|j                  dd       t        v rYt        d       t        t              t        |dd       fd}|j                  t        ||j                                      |r|	rt	        d      |rt        d       t        ||||||
      \  }}|	r:t        d       |j!                  dd        |j!                  dd        t#        |      }t%        || |||       |t'        ||       y y )NzCannot save to the path zY as it already exists. Please delete the file/directory or specify a new path to save to.z[INFO] LoadingTrQ   )rN   return_configtokenizer_configlazyr   z4Quant predicates only support 'affine' quantization.torch_dtypetext_configrL   z[INFO] Using dtype:cast_predicatec                      y)NT )r@   s    r2   <lambda>zconvert.<locals>.<lambda>   s    Dr4   c                      |       r?t        j                  |j                  t         j                        r|j	                        S |S )N)mx
issubdtyperL   floatingastype)r?   vrX   rL   s     r2   	set_dtypezconvert.<locals>.set_dtype   s3    a R]]177BKK%Hxx&r4   z/Choose either quantize or dequantize, not both.z[INFO] Quantizing)r'   rP   z[INFO] Dequantizingquantizationquantization_config)
isinstancer:   r   existsr5   printr
   rB   getMODEL_CONVERSION_DTYPESgetattrr]   updater   
parametersr   popr	   r   r   )rF   rG   rH   rI   rJ   rK   rL   rM   rN   rO   rP   rQ   r   	tokenizerconfigrW   rb   rX   s         `          @r2   convertrp   U   s   " (C >&xj 1R R
 	

 

#-/@A E9f /3'XSUU7
 }

=$/}M4)HH+H.''#U+E" (8.I	 	'	53C3C3EFGJJKK!"&+
v #$

>4(

($/ ' h, r4   c                  \   t        j                  d      } | j                  ddt        d       | j                  dt        dd	
       | j                  dddd       | j                  ddt        d       | j                  ddt        d       | j                  ddt        dg d       | j                  ddt
        t        d       | j                  dd t        t        d!       | j                  d"d#t        d       | j                  d$d%d&dd'       | j                  d(d)dd'       | S )*z
    Configures and returns the argument parser for the script.

    Returns:
        argparse.ArgumentParser: Configured argument parser.
    z(Convert Hugging Face model to MLX format)descriptionz	--hf-pathz--modelzSPath to the model. This can be a local path or a Hugging Face Hub model identifier.)typehelpz
--mlx-path	mlx_modelzPath to save the MLX model.)rs   defaultrt   z-qz
--quantizezGenerate a quantized model.
store_true)rt   actionz--q-group-sizezGroup size for quantization.N)rt   rs   rv   z--q-bitsz!Bits per weight for quantization.z--q-modezThe quantization mode.r   )r   mxfp4nvfp4mxfp8)rt   rs   rv   choicesz--quant-predicatezMixed-bit quantization recipe.F)rt   r|   rs   requiredz--dtypezvType to save the non-quantized parameters. Defaults to config.json's `torch_dtype` or the current model weights dtype.)rt   rs   r|   rv   z--upload-repoz-The Hugging Face repo to upload the model to.z-dz--dequantizezDequantize a quantized model.)rt   rx   rv   z--trust-remote-codez)Trust remote code when loading tokenizer.)argparseArgumentParseradd_argumentr:   r+   QUANT_RECIPESri   )parsers    r2   configure_parserr      s    $$>F b	   3:W   l!>|   +	   0	   %5   -    F'   <	   ,   8	   Mr4   c                  `    t               } | j                         }t        di t        |       y )NrZ   )r   
parse_argsrp   vars)r   argss     r2   mainr      s'    FDd4jr4   __main__zCalling `python -m mlx_lm.convert ...` directly is deprecated. Use `mlx_lm.convert ...` or `python -m mlx_lm convert ...` instead.)@   )ru   FNNr   NNNFNF)"r~   pathlibr   typingr   r   r   mlx.corecorer]   mlx.nnr;   	mlx.utilsr   utilsr	   r
   r   r   r   r:   r<   r+   r>   r=   rB   r   ri   rp   r   r   r   __name__rg   rZ   r4   r2   <module>r      s     , ,   (  689!9!		9!/29!sBIIt$eD$J&7789!x E< 
  "& " 	#Z-Z-Z- Z- 3-	Z-
 SMZ- Z- C=Z- Z- smZ- Z- hRYY-uT4Z/@@A3FGZ- Z-zK(11 K\ z		O 	F r4   