
    (HJjx                         d dl Z d dlZd dlZd dlZd dlmZ ddlmZ ddl	m
Z
mZ ddlmZ dZd Zd Zed	k(  r ed
        e        yy)    N   )generate_step)make_prompt_cachesave_prompt_cache)loadi  c                     t        j                  d      } | j                  dt        dd       | j                  dt        d	       | j                  d
dd       | j                  dt        dd       | j                  dt        dd       | j                  ddd       | j                  ddd       | j                  dt        dd       | j                  dt        dd       | j                  d d!t        t
        "       | S )#z&Set up and return the argument parser.z=Cache the state of a prompt to be reused with mlx_lm.generate)descriptionz--model	mlx_modelz;The path to the local model directory or Hugging Face repo.)typedefaulthelpz--adapter-pathz9Optional path for the trained adapter weights and config.)r   r   z--trust-remote-code
store_truez)Enable trusting remote code for tokenizer)actionr   z--eos-tokenNz#End of sequence token for tokenizerz--max-kv-sizez$Set the maximum key-value cache sizez--prompt-cache-filez$The file to save the prompt cache inT)r   requiredz--promptz;Message to be processed by the model ('-' reads from stdin))r   r   z	--kv-bitszFNumber of bits for KV cache quantization. Defaults to no quantization.)r   r   r   z--kv-group-sizez%Group size for KV cache quantization.@   z--quantized-kv-startzLWhen --kv-bits is set, start quantizing the KV cache from this step onwards.)r   r   r   )argparseArgumentParseradd_argumentstrintDEFAULT_QUANTIZED_KV_START)parsers    ]/Users/ahmed/devFolder/claude-voice/.venv/lib/python3.12/site-packages/mlx_lm/cache_prompt.pysetup_arg_parserr      s^   $$SF J	   H  
 8  
 2	   3	   3  
 J  
 '   4	   "*   M    c            
         t               } | j                         }d|j                  rdnd i}|j                  |j                  |d<   t	        |j
                  |j                  |      \  }}|j                  dk(  rt        j                  j                         n|j                  |_        |j                  r%d|j                  dg}|j                  |dd	      }n|j                  |j                        }t        ||j                        }t!        j"                  |      }t%        j$                         d
fd}	t'        ||d
||j(                  |j*                  |j,                  |	      D ]  }
 t/                t/        dt!        j0                         dz  dd       t/        d       i }|j
                  |d<   t3        j4                  |      |d<   t7        |j8                  ||       y )Ntrust_remote_codeT	eos_token)adapter_pathtokenizer_config-user)rolecontentF)add_generation_promptcontinue_final_messager   c                     t        j                          }| |z
  z  }d| dd|dd}t        t        |            t        |dt        |      z
  z  z   dd	       y )
NzProcessed 6dz	 tokens (z6.2fz tok/s)  T)endflush)timemaxlenprint)	processedtotal_tokenscurrentspeedmsgmax_msg_lenstarts        r   callbackzmain.<locals>.callbackv   sc    ))+Wu_-YrN)E$<wG+s3x0cC;S122$Gr   )
max_tokensprompt_cachekv_bitskv_group_sizequantized_kv_startprompt_progress_callbackzPeak memory: g    eAz.3fz GBz	Saving...modelr    )r   
parse_argsr   r   r   r?   r   promptsysstdinreadhas_chat_templateapply_chat_templateencoder   max_kv_sizemxarrayr-   r   r;   r<   r=   r0   get_peak_memoryjsondumpsr   prompt_cache_file)r   argsr    r?   	tokenizermessagesrA   cacheyr8   _metadatar6   r7   s               @@r   mainrV   S   s   FD ,T5K5KTQUV~~!(,%

&&)E9 '+kkS&8#)).."dkkDK""#<=.."'#' / 
 !!$++.eT%5%56E
A IIKEKH 	((22!)	 		 
G	M",,.4S9
=>	+H

HW#'::.>#?H d,,eX>r   __main__zCalling `python -m mlx_lm.cache_prompt...` directly is deprecated. Use `mlx_lm.cache_prompt...` or `python -m mlx_lm cache_prompt ...` instead.)r   rL   rB   r-   mlx.corecorerI   generater   models.cacher   r   utilsr   r   r   rV   __name__r0    r   r   <module>r_      sU      
   # > ! ?D>?B z		X 	F r   