
    .`iiJ                         d Z ddlZddlZddlZddlmZmZmZmZm	Z	m
Z
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  ee          Z G d dee
          Z G d	 d
e          ZdS )z$Argument parsing utilities for vLLM.    N)ActionArgumentDefaultsHelpFormatterArgumentParserArgumentTypeError	NamespaceRawDescriptionHelpFormatter_ArgumentGroup)defaultdict)Any)init_loggerc                   (     e Zd ZdZd Z fdZ xZS )SortedHelpFormatterzASortedHelpFormatter that sorts arguments by their option strings.c                     t          j        d          }t          j        d          }|                    d|          }t          j        ||          }t	          fd|D             g           S )z
        1. Sentences split across lines have their single newlines removed.
        2. Paragraphs and explicit newlines are split into separate lines.
        3. Each line is wrapped to the specified width (width of terminal).
        z(?<!\n)\n(?!\n)\s*z	\n{2,}\s* c                 :    g | ]}t          j        |          S  )textwrapwrap).0linewidths     m/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/vllm/utils/argparse_utils.py
<listcomp>z4SortedHelpFormatter._split_lines.<locals>.<listcomp>*   s%    AAA4HM$..AAA    )recompilesubsplitsum)selftextr   single_newlinemultiple_newlinesliness     `   r   _split_linesz SortedHelpFormatter._split_lines   sq     $9::J|44!!#t,,*D11AAAA5AAA2FFFr   c                 n    t          |d           }t                                          |           d S )Nc                     | j         S N)option_strings)xs    r   <lambda>z3SortedHelpFormatter.add_arguments.<locals>.<lambda>-   s	    0@ r   )key)sortedsuperadd_arguments)r    actions	__class__s     r   r/   z!SortedHelpFormatter.add_arguments,   s7    &@&@AAAg&&&&&r   )__name__
__module____qualname____doc__r%   r/   __classcell__r1   s   @r   r   r      sP        KKG G G' ' ' ' ' ' ' ' 'r   r   c                   D    e Zd ZU dZ e            Zee         ed<   dZe	ed<   dZ
e	dz  ed<    fdZej        dk     rd fd		Z fd
Z G d de          Zd Z fdZ	 	 ddee	         dz  dedz  f fdZd Zdee	         dee	         fdZde	dee	         fdZ xZS )FlexibleArgumentParserz=ArgumentParser that allows both underscore and dash in names._deprecateda}  When passing JSON CLI arguments, the following sets of arguments are equivalent:
   --json-arg '{"key1": "value1", "key2": {"key3": "value2"}}'
   --json-arg.key1 value1 --json-arg.key2.key3 value2

Additionally, list elements can be passed individually using +:
   --json-arg '{"key4": ["value3", "value4", "value5"]}'
   --json-arg.key4+ value3 --json-arg.key4+='value4,value5'

	_json_tipN_search_keywordc                     d|vr
t           |d<   |                    dd          | _         t                      j        |i | d S )Nformatter_classadd_json_tipT)r   popr?   r.   __init__)r    argskwargsr1   s      r   rA   zFlexibleArgumentParser.__init__@   sQ    F**(;F$%"JJ~t<<$)&)))))r   )      c                 
   t                                          ||          \  }}t          j        D ]M}t	          ||j        x}          r4t          ||          |j        k    rt          	                    d|           N||fS )Nzargument '%s' is deprecated)
r.   parse_known_argsr9   r:   hasattrdestgetattrdefaultloggerwarning_once)r    rB   	namespaceactionrI   r1   s        r   rG   z'FlexibleArgumentParser.parse_known_argsK   s    #gg66tYGGOIt0< M MIv{':t;;M	400FNBB''(EtLLLd?"r   c                     |                     dd          } t                      j        |i |}|rt          j                            |           |S N
deprecatedFr@   r.   add_argumentr9   r:   addr    rB   rC   rR   rO   r1   s        r   rT   z#FlexibleArgumentParser.add_argumentU   sU    L%88J)UWW)4:6::F ?&266v>>>Mr   c                        e Zd Z fdZ xZS )-FlexibleArgumentParser._FlexibleArgumentGroupc                     |                     dd          } t                      j        |i |}|rt          j                            |           |S rQ   rS   rV   s        r   rT   z:FlexibleArgumentParser._FlexibleArgumentGroup.add_argument]   sV    #ZZe<<
--t>v>> C*6::6BBBr   )r2   r3   r4   rT   r6   r7   s   @r   _FlexibleArgumentGrouprX   \   s8                r   rZ   c                 \     | j         | g|R i |}| j                            |           |S r(   )rZ   _action_groupsappend)r    rB   rC   groups       r   add_argument_groupz)FlexibleArgumentParser.add_argument_groupd   s@    /D/FtFFFvFFE&&u---Lr   c                 N   | j          t                                                      S |                                 }| j        x                                                    dd          dk    r,| j        | _        t                                                      S | j	        D ]}|j
        r|j
                                        k    r|                    |j
                   |                    |j                   |                    |j                   |                                 |                    | j                   |                                c S g }| j	        D ]A}|j        D ]7}t#          fd|j        D                       r|                    |           8B|rp|                    d d           |                    |           |                                 |                    | j                   |                                S |                    d d           |                                S |                    | j        | j        | j                   |                    | j                   |                    d	           d
}| j	        D ]%}|j        s
|j
        }|j        pd
}||d| dz  }&|                    |           |                                 |                    | j                   |                                S )N_-allc              3   D   K   | ]}|                                 v V  d S r(   )lower)r   optsearch_keywords     r   	<genexpr>z5FlexibleArgumentParser.format_help.<locals>.<genexpr>   sA        :=#))++5     r   zArguments matching ''z No group or arguments matching 'zX'.
Use '--help' to see available groups or '--help=all' to see all available parameters.zConfig Groups z <24
)_subparsersr.   format_help_get_formatterr<   re   replacer;   epilogr\   titlestart_sectionadd_textdescriptionr/   _group_actionsend_sectionanyr)   r]   	add_usageusage_actions_mutually_exclusive_groups)
r    	formatterr^   matched_actionsrO   config_groupsrq   rt   rg   r1   s
           @r   rm   z"FlexibleArgumentParser.format_helpi   s   '77&&(((''))	 #22N?+1133;;CEEN&&"nww**,,, , 3 3; 35;#4#4#6#6.#H#H++EK888&&u'8999++E,@AAA))+++&&t~666$0022222 !O, 7 7#2 7 7F    AGAV     7 (..v6667  /''(P~(P(P(PQQQ''888%%'''""4>222 ,,... @> @ @ @  
 ((*** 	DJt7VWWW 	4+,,, 	000( 	< 	<E' KE+1rK;;K;;;;MM=))) 	4;''' $$&&&r   rB   rN   c                    |t           j        dd          }|r|d         dk    r	 t          d t          |          D                       }t                              d           ||         dk    r||dz            }|dz   }n ||                             d          }|dz   }d|g|d|         ||d          }n# t          $ r Y nw xY wd	|v r|                     |          }d
t          j
        dt          fd}t          j        d          }t          t                               }t          |          D ]\  }	}
|
                    d          rN|
                    dd          d                                         t"          _        |                    d           i|
                    d          r~d|
v rL|
                    dd          \  }|                    |d          |                     d|            |                    ||
d          |                               |
                    d          r/|
dk    r)|
d         dk    r
|
dd          n	|
dd          }|d|gz  }@|
dk    r:|	dz   t+          |          k     r$||	dz            dv r|                    d           |                    |
           dt          t                   dt          dt,          t          t.          f         fd}dt,          t          t.          f         dt,          t          t.          f         dt0          t                   ffdt1          t2                               t5          t          t,          t          t.          f         f         t,                    }t1          t                               }t1          t                               }t          |          D ]\  }	}|	v r|                    d          rWd|vrSd|v r|                    dd          d         }n|}||v r|                    |           n|                    |           w|                    d          r+d|v r&d|v r|                    dd          \  }}d|vrn#||	dz            }                    |	dz              |                    d           r>|d d         }t;          j        t          |                    d!                              }|                    d          ^}	 t;          j        |          }n# t:          j         j!        $ r |}Y nw xY w |||          } |         |          }|fd"|D             z  }                    |	           fd#t          |          D             }|r.t                              d$d%"                    |                     |#                                D ]A\  }}|                    |           |                    t;          j        |                     BtI                      %                    ||          S )&N   r   servec              3   V   K   | ]$\  }}|d k    s|                     d           |V  %dS )--model--model=N)
startswith)r   iargs      r   rh   z4FlexibleArgumentParser.parse_args.<locals>.<genexpr>   sN       ! !3i''3>>*+E+E' ''''! !r   zWith `vllm serve`, you should provide the model as a positional argument or in a config file instead of via the `--model` option. The `--model` option will be removed in v0.13.r      r   --configmatchreturnc                 T    |                      d                              dd          S )z7Replaces underscores with dashes in the matched string.r   ra   rb   )r^   ro   )r   s    r   replz/FlexibleArgumentParser.parse_args.<locals>.repl   s"    ;;q>>))#s333r   z(?<=--)[^\.]*z--help==z--help--)countz-OrD   z--optimization-level>   0123keysvaluec                 6    |}t          |           D ]}||i}|S )zCreates a nested dictionary from a list of keys and a value.

            For example, `keys = ["a", "b", "c"]` and `value = 1` will create:
            `{"a": {"b": {"c": 1}}}`
            )reversed)r   r   nested_dictr,   s       r   create_nested_dictz=FlexibleArgumentParser.parse_args.<locals>.create_nested_dict  s0      %K~~ 1 1"K0r   originalupdatec                 
   t          t                               }|                                D ]\  }t          |t                    rLt          |                               t                    r$ |          |          }|fd|D             z  }ft          |t                    r9t          |                               t                    r| xx         |z  cc<   | v r|                               || <   |S )zRecursively updates a dictionary with another dictionary.
            Returns a set of duplicate keys that were overwritten.
            c                     h | ]	} d | 
S .r   )r   dks     r   	<setcomp>zSFlexibleArgumentParser.parse_args.<locals>.recursive_dict_update.<locals>.<setcomp>  s#    "I"I"I!a::!::"I"I"Ir   )setstritems
isinstancedictgetlistrU   )r   r   
duplicatesvnested_duplicatesr   recursive_dict_updates        @r   r   z@FlexibleArgumentParser.parse_args.<locals>.recursive_dict_update  s    SJ 	$ 	$1a&& $:hll1oot+L+L $(=(=hqk1(M(M%"I"I"I"I7H"I"I"IIJJ4(( $ZQ-N-N $QKKK1$KKKKH}}"q)))"#HQKKr   r   rb   +,c                     h | ]	} d | 
S r   r   )r   r   r,   s     r   r   z4FlexibleArgumentParser.parse_args.<locals>.<setcomp>Q  s#    DDD#||||DDDr   c                 "    g | ]\  }}|v	|S r   r   )r   r   adeletes      r   r   z5FlexibleArgumentParser.parse_args.<locals>.<listcomp>T  s"    UUU1Qf__!___r   zFound duplicate keys %sz, )&sysargvnext	enumeraterL   warningremoveprefixStopIteration_pull_args_from_configr   Matchr   r   r   r   r   re   r9   r<   r]   r   lenr   r   r   intr
   rU   endswithjsondumpsloadsdecoderJSONDecodeErrorjoinr   r.   
parse_args)r    rB   rN   	model_idx	model_tagrest_start_idxr   patternprocessed_argsr   r   r   optimization_levelr   	dict_argsr   regular_args_seenprocessed_argarg_name	value_strr   arg_dictarg_duplicatesdict_arg
dict_valuer   r,   r   r1   s                            @@@r   r   z!FlexibleArgumentParser.parse_args   s   
 <8ABB<D  !	DGw&&   ! !"+D//! ! !  	
 E   	?i// $Y] 3I%.]NN $Y < <Z H HI%.]N  !I+& .//*	 !    ..t44D	4 	4S 	4 	4 	4 	4
 *-.. coo 	+ 	+FAs~~i(( +9<39J9J29N9T9T9V9V&6%%h////%% +#::!$3!2!2JC!++dCq+99C"))S*:*:5*:*:;;;;!++dCq+99C"))#....%% +#++ 14A#SWW3qrr7"#9;M"NNtECII%%QK#777 %%&<====%%c****		T#Y 		s 		tCH~ 		 		 		 			38n	cN	 X	 	 	 	 	 	( ST#s(^ 34T::	XZZ
HJJ ). 9 9 (	 (	A}F{{''-- 
#]2J2J-'',223::1=HH,H000NN8,,,,%))(333'',, 1E1E-''/</B/B3/J/J,M9-//  0 !/q1u 5IJJq1u%%% ))#.. G$1#2#$6M $
4	0D0D+E+E F FI*0055
d& Jy11EE|3 & & &%EEE& .-dE::!6!6y~x!P!PDDDD^DDDD


1UUUU	.(A(AUUU 	MNN4dii
6K6KLLL %.OO$5$5 	: 	: Hj!!(+++!!$*Z"8"89999ww!!.)<<<s$   BC   
CC(T==UUc                     	 t          |          }n # t          $ r d}t          |          d w xY wd|cxk    rdk    sn t          d          |S )NzPort must be an integeri   i  z#Port must be between 1024 and 65535)r   
ValueErrorr   )r    r   msgs      r   
check_portz!FlexibleArgumentParser.check_port_  sy    	3JJEE 	3 	3 	3+C#C((d2	3 &&&&&&&&#$IJJJs    /r   c                 6   |                     d          dk    s
J d            |                    d          }|t          |          dz
  k    rt          d          ||dz            }|                     |          }|d                             d          r||d|         z   ||dz   d         z   }n|d         d	k    rt          |          dk    o|d                             d           }t          d
 |D                       }|s|st          d          |r0|d         g|d         gz   |z   |d|         z   ||dz   d         z   }nK|d         g|z   |d|         z   ||dz   d         z   }n%|d         g|z   |d|         z   ||dz   d         z   }|S )a  Method to pull arguments specified in the config file
        into the command-line args variable.

        The arguments in config file will be inserted between
        the argument list.

        example:
        ```yaml
            port: 12323
            tensor-parallel-size: 4
        ```
        ```python
        $: vllm {serve,chat,complete} "facebook/opt-12B"             --config config.yaml -tp 2
        $: args = [
            "serve,chat,complete",
            "facebook/opt-12B",
            '--config', 'config.yaml',
            '-tp', '2'
        ]
        $: args = [
            "serve,chat,complete",
            "facebook/opt-12B",
            '--port', '12323',
            '--tensor-parallel-size', '4',
            '-tp', '2'
            ]
        ```

        Please note how the config args are inserted after the sub command.
        this way the order of priorities is maintained when these are args
        parsed by super().
        r   r   z$More than one config file specified!zCNo config file specified! Please check your command-line arguments.r   rb   r   Nr   c              3   "   K   | ]
}|d k    V  dS )r   Nr   )r   r   s     r   rh   z@FlexibleArgumentParser._pull_args_from_config.<locals>.<genexpr>  s'      !J!Js#"2!J!J!J!J!J!Jr   z]No model specified! Please specify model either as a positional argument or in a config file.)r   indexr   r   load_config_filer   rw   )r    rB   r   	file_pathconfig_argsmodel_in_climodel_in_configs          r   r   z-FlexibleArgumentParser._pull_args_from_configk  s	   D zz*%%***,R***

:&&CIIM!!U   O	++I66 7c"" 	Oag.eaikk1BBDD!Wt99q=Ha1C1CC1H1H-HL!!J!Jk!J!J!JJJO   D  
  S !WIAwi !" 1U7m$ 519;;'	(  Qy;.ag>eaikkARRG9{*T!E']:T%!)++=NNDr   r   c                     |                     d          d         }|dvrt          d| d          g }i }	 t          |          5 }t          j        |          }ddd           n# 1 swxY w Y   n/# t
          $ r"}t                              d|           |d}~ww xY w|                                D ]\  }}t          |t                    r|r|                    d|z              5t          |t                    rB|r?|                    d|z              |D ]$}	|                    t          |	                     %|                    d|z              |                    t          |                     |S )	ac  Loads a yaml file and returns the key value pairs as a
        flattened list with argparse like pattern
        ```yaml
            port: 12323
            tensor-parallel-size: 4
        ```
        returns:
            processed_args: list[str] = [
                '--port': '12323',
                '--tensor-parallel-size': '4'
            ]
        r   r   )yamlymlz(Config file must be of a yaml/yml type. z	 suppliedNz<Unable to read the config file at %s. Check path correctnessr   )r   r   openr   	safe_load	ExceptionrL   errorr   r   boolr]   r   r   )
r    r   	extensionr   configconfig_fileexr,   r   items
             r   r   z'FlexibleArgumentParser.load_config_file  s    #--b1	O++O9OOO  
 %'')	i 5K445 5 5 5 5 5 5 5 5 5 5 5 5 5 5 	 	 	LLN   H	 !,,.. 	2 	2JC%&& 
2 6"))$*555E4(( 2 9"))$*555 % 9 9&--c$ii8888%%dSj111%%c%jj1111s;   A4 A(A4 (A,,A4 /A,0A4 4
B >BB )NN)r2   r3   r4   r5   r   r:   r   __annotations__r;   r   r<   rA   r   version_inforG   rT   r	   rZ   r_   rm   r   r   r   r   r   r   r6   r7   s   @r   r9   r9   1   s        GG"suuKV$$$	J s    #'OS4Z&&&* * * * * '!!	# 	# 	# 	# 	# 	#	 	 	 	 		 	 	 	 	^ 	 	 		 	 	
H' H' H' H' H'X "&&*j= j=3i$j= t#j= j= j= j= j= j=X
 
 
P49 Pc P P P Pd.# .$s) . . . . . . . .r   r9   )r5   r   r   r   argparser   r   r   r   r   r   r	   collectionsr
   typingr   regexr   r   vllm.loggerr   r2   rL   r   r9   r   r   r   <module>r      sJ   + *  



                   $ # # # # #            # # # # # #	X		' ' ' ' '79T ' ' '*z z z z z^ z z z z zr   