
    -`iu                       U d Z ddlZddlZddlZddlZddlZddlZddlZddlZddl	m
Z
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 dd	lmZ dd
lmZ ddlmZmZ ddlZddl m!Z! ddl"m#Z# ddl$m%Z% ddl&m'Z' ddl(m)Z) ddl*m+Z+ ddl,m-Z- ddl.m/Z/ 	 ddl0m1Z1 n+# e2$ r#  e/d          Z0e03                    d          Z1Y nw xY w	 ddl4Z5n# e2$ r  e/d          Z5Y nw xY w	 ddl6Z6n# e2$ r  e/d          Z6Y nw xY w	 ddl7m8Z8 n# e2$ r	 ddlm9Z8 Y nw xY w ej:        e;          Z<e G d d                      Z= G d de
          Z>	 	 	 	 dtd#e?d$e?d%e?d&e?d'e?d(e@d)e@fd*ZAed+eBd)eBfd,            ZCi ZDeEe?e-f         eFd-<   d.ed)eeBef         fd/ZGd0ed)eeBef         fd1ZH	 	 	 dud3e-d4eIe?         d5e?d6e?d7e@d8ej        jJ        dz  d)eKeBeIe?         f         fd9ZL G d: d;e>          ZM G d< d=eM          ZN G d> d?eM          ZO G d@ dAe>          ZP G dB dCejQ                  ZRdDe8fdEZSdFe8ejT        z  d)dfdGZUdFe8ejT        z  d)dfdHZVd3e-d)eIe=         fdIZW G dJ dKe>          ZX G dL dMeX          ZY e#dN           G dO dPe>                      ZZ G dQ dRe>          Z[ G dS dTe>          Z\ G dU dVe\          Z] G dW dXe\          Z^ G dY dZe\          Z_ G d[ d\e\          Z` G d] d^e\          Za G d_ d`e\          Zb G da dbe\          Zc G dc dde\          ZddeZe	 dvdgeEdheBd)eEfdiZf G dj dke\          Zg G dl dme\          Zh G dn doe\          Zi G dp dqe>          Zj G dr dse\          ZkdS )wa!  
This module defines a framework for sampling benchmark requests from various
datasets. Each dataset subclass of BenchmarkDataset must implement sample
generation. Supported dataset types include:
  - ShareGPT
  - Random (synthetic)
  - Sonnet
  - BurstGPT
  - HuggingFace
  - VisionArena
    N)ABCabstractmethod)CallableIteratorMapping)suppress)deepcopy)	dataclass)cache)BytesIO)NamedTemporaryFile)Anycast)Image)
deprecated)LoRARequestget_adapter_absolute_path)MultiModalDataDict)convert_image_mode)TokenizerLike)PlaceholderModule)load_datasetdatasetsr   pandaslibrosa)FlexibleArgumentParser)ArgumentParserc                       e Zd ZU dZeee         z  ed<   eed<   eed<   dZe	e
z  ee
         z  dz  ed<   dZedz  ed<   dZedz  ed<   dS )	SampleRequestzA
    Represents a single inference request for benchmarking.
    prompt
prompt_lenexpected_output_lenNmulti_modal_datalora_request
request_id)__name__
__module____qualname____doc__strlist__annotations__intr$   r   dictr%   r   r&        l/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/vllm/benchmarks/datasets.pyr    r    H   s           $s)OOOOFJ(4/$t*<tCJJJ'+L+$+++!Jd
!!!!!r1   r    c                      e Zd ZdZdZdedfdedz  dededdfdZ	 dd	ed
e	e
z  ee
         z  dz  dee
         fdZddZ	 	 ddedz  dedz  dedz  fdZe	 	 ddededededee         f
d            Z	 	 ddee         dedededdf
dZdS )BenchmarkDatasetr   FNdataset_pathrandom_seeddisable_shufflereturnc                 P    || _         ||n| j        | _        || _        d| _        dS )a  
        Initialize the BenchmarkDataset with an optional dataset path and random
        seed.

        Args:
            dataset_path (Optional[str]): Path to the dataset. If None, it
                indicates that a default or random dataset might be used.
            random_seed (int): Seed value for reproducible shuffling or
                sampling. Defaults to DEFAULT_SEED.
        N)r5   DEFAULT_SEEDr6   r7   data)selfr5   r6   r7   kwargss        r2   __init__zBenchmarkDataset.__init___   s5    " ) +6*A;;tGX.			r1   r!   
mm_contentc                 j   |ddg}|t          |t                    rF|                    t          t          t          t
          t          f                  |                     nKt          |t                    r|                    |           n t          dt          |           z             d|dgS )z
        Transform a prompt and optional multimodal content into a chat format.
        This method is used for chat models that expect a specific conversation
        format.
        text)rA   typeNz.Could not process multimodal content of type: userrolecontent)

isinstancer,   extendr   r/   r+   r   append	TypeErrorrB   )r<   r!   r?   rF   s       r2   $apply_multimodal_chat_transformationz5BenchmarkDataset.apply_multimodal_chat_transformationw   s     #F334!*d++ tDc3h$8*EEFFFFJ-- z****Dj))+,    G4455r1   c                      t          d          )a3  
        Load data from the dataset path into self.data.

        This method must be overridden by subclasses since the method to load
        data will vary depending on the dataset format and source.

        Raises:
            NotImplementedError: If a subclass does not implement this method.
        z,load_data must be implemented in subclasses.NotImplementedErrorr<   s    r2   	load_datazBenchmarkDataset.load_data   s     ""PQQQr1   	max_loras	lora_pathc                     ||dS t          j        d|          }t          t          |          |t	          |                    }|S )a3  
        Optionally select a random LoRA request.

        This method is used when LoRA parameters are provided.  It randomly
        selects a LoRA based on max_loras.

        Args:
            max_loras (Optional[int]): The maximum number of LoRAs available.
                If `None`, LoRA is not used.
            lora_path (Optional[str]): Path to the LoRA parameters on disk.
                If `None`, LoRA is not used.

        Returns:
            A new [`LoRARequest`][vllm.lora.request.LoRARequest]
            (or `None` if not applicable).
        N   )	lora_namelora_int_idrR   )randomrandintr   r+   lora_path_on_disk)r<   rQ   rR   lora_idr%   s        r2   get_random_lora_requestz(BenchmarkDataset.get_random_lora_request   sZ    * 	 14 .I.."'ll'	22
 
 

 r1    	tokenizernum_requestsrequest_id_prefixno_oversamplec                      t          d          )aZ  
        Abstract method to generate sample requests from the dataset.

        Subclasses must override this method to implement dataset-specific logic
        for generating a list of SampleRequest objects.

        Args:
            tokenizer (TokenizerLike): The tokenizer to be used
                for processing the dataset's text.
            num_requests (int): The number of sample requests to generate.
            request_id_prefix (str): The prefix of request_id.

        Returns:
            list[SampleRequest]: A list of sample requests generated from the
            dataset.
        z)sample must be implemented in subclasses.rM   )r<   r]   r^   r_   r`   s        r2   samplezBenchmarkDataset.sample   s    0 ""MNNNr1   requestsc                    |r*t                               dt          |                     dS t          |          |k     rt          j        | j                   |t          |          z
  }g }t          |          D ]_}t          t          j        |                    }|t          t          |          |z             z   |_
        |                    |           `|                    |           t                               d|           d |D             }	t          |	          t          t          |	                    k    rt          d          dS )a  
        Oversamples the list of requests if its size is less than the desired
        number.

        Args:
            requests (List[SampleRequest]): The current list of sampled
                requests.
            num_requests (int): The target number of requests.
            request_id_prefix (str): The prefix applied to generated request
                identifiers.

        z)Skipping oversampling. Total samples: %d.Nz/Oversampled requests to reach %d total samples.c                     g | ]	}|j         
S r0   )r&   .0reqs     r2   
<listcomp>z>BenchmarkDataset.maybe_oversample_requests.<locals>.<listcomp>   s    222#s~222r1   zaDuplicate request_id found in the sampled requests. Please ensure that each request_id is unique.)loggerinfolenrW   seedr6   ranger	   choicer+   r&   rI   rH   set
ValueError)
r<   rc   r^   r_   r`   needed
additionalirh   idss
             r2   maybe_oversample_requestsz*BenchmarkDataset.maybe_oversample_requests   s=   &  	KKCS]]SSSFx==<''K()))!CMM1FJ6]] ' 'v}X6677!2SX9J5K5K!K!!#&&&&OOJ'''KKI<XXX22222s88s3s88}}$$   %$r1   Nr8   N)NNr\   F)r'   r(   r)   r:   IS_MULTIMODALr+   r.   boolr>   r   r/   r,   rK   rP   r   r[   r   r   r    rb   rv   r0   r1   r2   r4   r4   [   s       LM $(' %	 Dj  	 
   6 EI6 66 '-T
:TA6 
d	6 6 6 6.R R R R !% $ : : 
t		   B 
 "$#O O O O 	O
 O 
m	O O O ^O: "$#( (}%( ( 	(
 ( 
( ( ( ( ( (r1   r4            Fr"   
output_lenmin_lenmax_prompt_lenmax_total_lenskip_min_output_len_checkr8   c                 P    | |k     }| o||k     }| |k    }| |z   |k    }	|p|p|p|	 S )a  
    Validate a sequence based on prompt and output lengths.

    Default pruning criteria are copied from the original `sample_hf_requests`
    and `sample_sharegpt_requests` functions in benchmark_serving.py, as well as
    from `sample_requests` in benchmark_throughput.py.
    r0   )
r"   r   r   r   r   r   prompt_too_shortoutput_too_shortprompt_too_longcombined_too_longs
             r2   is_valid_sequencer     s\      "G+55QJ<P >1O#j0MA 	T,TTCT r1   rR   c                      t          |           S rw   r   )rR   s    r2   rY   rY   !  s    $Y///r1   lora_tokenizer_cacheimagec                 v   t          | t                    r+d| v r't          j        t	          | d                             } t          | t          j                  rt          | d          } t          j                    5 }|                     |d           t          j	        |
                                                              d          }ddd           n# 1 swxY w Y   ddd	| id
S t          | t                    r#|                     d          r| nd|  }dd|id
S t          d|  d          )a  
    Process a single image input and return a multimedia content dictionary.

    Supports the following input types:

    1. Dictionary with raw image bytes: - Expects a dict with a 'bytes' key
       containing raw image data.  - Loads the bytes as a PIL.Image.Image.

    2. PIL.Image.Image input: - Converts the image to RGB.  - Saves the image as
       a JPEG in memory.  - Encodes the JPEG data as a base64 string.  - Returns
       a dictionary with the image as a base64 data URL.

    3. String input: - Treats the string as a URL or local file path.  -
       Prepends "file://" if the string doesn't start with "http://" or
       "file://".  - Returns a dictionary with the image URL.

    Raises:
        ValueError: If the input is not a supported type.
    bytesRGBJPEG)formatutf-8N	image_urlurlzdata:image/jpeg;base64,)rB   r   zhttp://zhttps://file://r   zInvalid image input zF. Must be a PIL.Image.Image or str or dictionary with raw image bytes.)rG   r/   r   openr   r   iosavebase64	b64encodegetvaluedecoder+   
startswithrq   )r   
image_dataimage_base64r   s       r2   process_imager   *  s   ( % 47e#3#3
75>2233%%% 
"5%00Z\\ 	SZJJz&J111!+J,?,?,A,ABBII'RRL	S 	S 	S 	S 	S 	S 	S 	S 	S 	S 	S 	S 	S 	S 	S  !I<!I!IJ
 
 	

 % F  BCC#EE"5"" 	
 $5)2DEEE
	6u 	6 	6 	6  s   >ACC"Cvideoc                 <   t          | t                    r=d| v r9| d         }t          j        |                              d          }ddd| idS t          | t
                    r#|                     d          r| nd|  }dd|idS t          d	|  d
          )a  
    Process a single video input and return a multimedia content dictionary.

    Supports the following input types:

    1. Dictionary with raw video bytes: - Expects a dict with a 'bytes' key
       containing raw video data.

    2. String input: - Treats the string as a URL or local file path.  -
       Prepends "file://" if the string doesn't start with "http://" or
       "file://".  - Returns a dictionary with the image URL.

    Raises:
        ValueError: If the input is not a supported type.
    r   r   	video_urlr   zdata:video/mp4;base64,)rB   r   r   r   zInvalid video input z~. Must be a string of local path/remote url, or a dictionary with raw video bytes in the form of `{'bytes': raw_video_bytes}`.)rG   r/   r   r   r   r+   r   rq   )r   video_bytesvideo_base64r   s       r2   process_videor   X  s      % 
7e#3#3Gn'44;;GDD!H,!H!HI
 
 	

 % F  BCC#EE"5"" 	
 $5)2DEEE
 	gu  	g  	g  	g  r1   
   r]   token_sequencetarget_token_len	max_retryadd_special_tokensrngc                    |}d}	 |                      |          }|                     ||          }|dk    r&t          |          |k    rt          |          |z
  }nt          |          |k    rnt          |          |k     r|@|                    d| j        |t          |          z
                                            }	nIt          j                            d| j        |t          |          z
                                            }	|	                    |	           nt          |          |k    r
|d|         }|dz  }D|||fS )uV  
    Ensure decoded-then-encoded prompt length matches the target token length.

    This function decodes an initial token sequence to text and re-encodes it
    , iteratively adjusting the token sequence length to match a target.
    This is necessary because some tokenizers do not guarantee a 1:1 mapping
    between consecutive tokens and the decoded-then-encoded sequence length.
    For example, for GPT2Tokenizer:
    [6880, 6881] -> ['Ġcalls', 'here'] ->
    [1650, 939, 486] -> ['Ġcall', 'sh', 'ere']

    Returns a tuple of the final prompt string and the adjusted token sequence.
    r   Tr   NsizerT   )
r   encoderl   integers
vocab_sizetolistnprW   rX   rH   )
r]   r   r   r   r   r   remain_num_trytoken_mismatchr!   extra_tokenss
             r2   gen_prompt_decode_to_target_lenr   }  s   * NN!!.11"))&EW)XXQ>""&666!$^!4!47G!G~"222  #333"||()C,?,??  ,     &((	   "y00()C,?,??  1     &((	 
 !!,////  #333+,=-=,=>N!7: >>11r1   c                   N    e Zd ZdZdZdZdZdZd fdZd	d
eeeedfde	de
dedede
dede
de
de
dee         fdZdej        de
dee
         fdZde
dede
de
de	deej        ej        ej        f         fdZde	dee
         de
de
de
de
de
dej        deee
e
f         fdZ xZS )RandomDataseta/  
    Synthetic text-only dataset for serving/throughput benchmarks.

    Strategy:
    - Sample input/output token lengths per request from integer-uniform ranges
      around configured means (controlled by range_ratio).
    - Prepend a fixed random prefix of length prefix_len.
    - Generate the remaining tokens as a reproducible sequence:
      (offset + index + arange(input_len)) % vocab_size.
    - Decode then re-encode/truncate to ensure prompt token counts match.
    - Uses numpy.default_rng seeded with random_seed for reproducible sampling.
    r           r}      r8   Nc                      t                      j        di | t          j                            | j                  | _        d S Nr0   )superr>   r   rW   default_rngr6   _rngr<   r=   	__class__s     r2   r>   zRandomDataset.__init__  s@    ""6""" I))$*:;;			r1   r\   FrT   r]   r^   r_   r`   
prefix_lenrange_ratio	input_lenr   	batchsizec
                 0   t          |                                          }t          dt          |          |z
            }t          j        |dt          |          z
  z            }t          |          |z   }|dk     rt          d| d| d| d          |                     |||||          \  }}}|j        }|j	        }t          j        |          }t          j        t          t          |          t          |          z
                      }|                     ||          }g }d}t!          |          D ]}|                     ||||t          ||                   t          ||                   ||          \  }}}||z  }|                    t'          ||t          ||                   |t)          |          z   	                     |	dk    rg }t!          d||	          D ]i}||||	z            }|                    t'          d
 |D             t+          d |D                       d|t)          ||	z            z   	                     j|}|dk    r3|dk    rdnd}t,                              dt1          |          |           |S )Nr         ?rT   z?--random-input-len is too small: with tokenizer special tokens z and --random-range-ratio z@, the minimum possible total input tokens (prefix + sampled) is z. Increase --random-input-len and/or --random-prefix-len, or decrease --random-range-ratio so that prefix_len + floor(max(0, random_input_len - num_special)) * (1 - range_ratio) >= 1.r]   prefix_token_idsr   r   r   offsetindexallowed_tokensr!   r"   r#   r&   c                     g | ]	}|j         
S r0   )r!   rf   s     r2   ri   z(RandomDataset.sample.<locals>.<listcomp>  s    <<<s
<<<r1   c              3   $   K   | ]}|j         V  d S rw   )r"   rf   s     r2   	<genexpr>z'RandomDataset.sample.<locals>.<genexpr>  s$      &G&G#s~&G&G&G&G&G&Gr1   morefewerAcross all generated prompts, there were %d %s tokens than expected after decoding and re-encoding. This is expected due to the imperfect nature of the sampling procedure.)r.   num_special_tokens_to_addmaxmathfloorfloatrq   get_sampling_paramsr   all_special_idsr   arangearrayr,   rp   
get_prefixrn   generate_token_sequencerI   r    r+   sumrj   warningabs) r<   r]   r^   r_   r`   r   r   r   r   r   r=   num_specialreal_input_lenmin_sampled_inputmin_total_input
input_lensoutput_lensoffsetsr   prohibited_tokens
all_tokensr   r   rc   token_mismatch_totalrt   r!   total_input_lenr   batch_requestsbatchsigns                                    r2   rb   zRandomDataset.sample  s    )==??@@QI <== J~u[?Q?Q9Q'RSSj//,==Q,%, ,AL, , #, , ,   ,0+C+C+y*i,
 ,
(
K )
%5Yz**
$s:=N9O9O'O"P"PQQ  ??>:FF |$$ 	 	A6:6R6R#!1%%jm,,71:- 7S 	7 	73FO^ !N2 OO!.(+KN(;(;03q669	      q==N1lI66 	 	 Q]!23%%!<<e<<<#&&G&G&G&G&G#G#G,-#4s1	>7J7J#J	      &H1$$1A55667DNN ())   r1   r   c                     |dk    rB|| j                             dt          |          |                                                   ng S )z1
        Get the prefix for the dataset.
        r   r   )r   r   rl   r   )r<   r   r   s      r2   r   zRandomDataset.get_prefix0  sL     A~~ 	""1c.&9&9
"KKfhhh	
r1   c                 F   d|cxk    rdk     sn t          d          t          |                                          }t          dt          |          |z
            }t	          j        |d|z
  z            }t	          j        |d|z   z            }	t	          j        |d|z
  z            }
t	          j        |d|z   z            }t          |
d          }
t          |d          }||	k    rt          d| d|	           |
|k    rt          d|
 d|           t                              d	||	|
|           | j	        
                    ||	dz   |
          }| j	        
                    |
|dz   |
          }| j	        
                    d|j        |
          }|||fS )z>
        Get the sampling parameters for the dataset.
        r   r   zrange_ratio must be in [0, 1).r   rT   z%Invalid input sampling interval: low=z > high=z&Invalid output sampling interval: low=z=Sampling input_len from [%s, %s] and output_len from [%s, %s]r   )rq   r.   r   r   r   r   ceilrj   rk   r   r   r   )r<   r^   r   r   r   r]   num_special_tokensr   	input_low
input_high
output_lowoutput_highr   r   r   s                  r2   r   z!RandomDataset.get_sampling_params@  s    {((((S((((=>>> !D!D!F!FGGQI1C CDDJ~[ABB	Y~[ABB
Z
a+o >??
i
a+o >?? Q''
+q))z!!W	WW:WW   ##9!9 9+69 9  
 	K	
 	
 	
 Y''	:>'UU
i(([1_<(XX)$$Q	(<<$PP;//r1   r   r   r   r   c                   |||z   t          j        |          z   t          |          z                                           }	||	z   }
|t	          |          z   }t          ||
|d| j                  \  }}}t          |          }|||fS )u  
        Returns (prompt, total_input_len).

        NOTE: After decoding the prompt we have to encode and decode it again.
        This is done because in some cases N consecutive tokens
        give a string tokenized into != N number of tokens.
        For example for GPT2Tokenizer:
        [6880, 6881] -> ['Ġcalls', 'here'] ->
        [1650, 939, 486] -> ['Ġcall', 'sh', 'ere']
        To avoid uncontrolled change of the prompt length,
        the encoded sequence is truncated before being decoded again.
        F)r]   r   r   r   r   )r   r   rl   r   r.   r   r   )r<   r]   r   r   r   r   r   r   r   	inner_seqr   r   r!   adjusted_token_sequencer   s                  r2   r   z%RandomDataset.generate_token_sequenceq  s    4 #e^bi	222c.6I6II

&(( 	 *I5 %s9~~5+#-!0#(I   	8' 56666r1   rx   )r'   r(   r)   r*   DEFAULT_PREFIX_LENDEFAULT_RANGE_RATIODEFAULT_INPUT_LENDEFAULT_OUTPUT_LENr>   r   r.   r+   r{   r   r,   r    rb   r   ndarrayr   tupler   r   __classcell__r   s   @r2   r   r     s         < < < < < < "$#,0*,Z Z Z Z 	Z
 Z Z Z Z Z Z 
m	Z Z Z Zx


 
 
c	
 
 
 
 /0/0 /0 	/0
 /0 !/0 
rz2:rz1	2/0 /0 /0 /0b+7 !+7 s)	+7
 +7 +7 +7 +7 +7 
+7 
sC}	+7 +7 +7 +7 +7 +7 +7 +7r1   r   c                   x     e Zd ZdZd fdZdej        ej        ddfded	e	d
e
dede	de	dedee         fdZ xZS )RandomDatasetForRerankingzu
    Random dataset specialized for the needs of scoring:
    - Batches of inputs
    - Inputs composed of pairs
    r8   Nc                 :     t                      j        di | d S r   r   r>   r   s     r2   r>   z"RandomDatasetForReranking.__init__  &    ""6"""""r1   r\   rT   Tr]   r^   r_   r   r   r   is_rerankerc                    t          |          }	|r|dz  |	z
  n|}
|                     d||
d|          \  }}}t          |d                   }|s|dk    r|dk    sJ |dz  }|dz  }|}n||z
  |	z
  }|                     |||d|          \  }}}|j        }|j        }t	          j        |          }t	          j        t          t          |          t          |          z
                      }| 	                    |g d||t          |d                   d|          \  }}}g }t          |          D ]h}| 	                    |g d|t          ||                   t          ||                   |dz   |          \  }}}||z  }|                    ||f           ig }t          d||          D ]}||||z            }|r||	z   t          |          z  n|} |                    t          |gd |D             z   | t          d |D                       z   d|t          ||z            z                        |dk    r1t                               dt%          |          |dk    rd	nd
           |S )N   rT   r   r   c                     g | ]
}|d          S r   r0   rf   s     r2   ri   z4RandomDatasetForReranking.sample.<locals>.<listcomp>  s    ,E,E,ESV,E,E,Er1   c              3   &   K   | ]}|d          V  dS )rT   Nr0   rf   s     r2   r   z3RandomDatasetForReranking.sample.<locals>.<genexpr>  s&      2K2Kc3q62K2K2K2K2K2Kr1   r   r   r   r   )r.   r   r   r   r   r   r   r,   rp   r   rn   rI   rl   r    r   r+   rj   r   r   )!r<   r]   r^   r_   r   r   r   r  r=   n_sep_tokensquery_len_param
query_lens_query_offsets	query_lendoc_len_paramdoc_lensdoc_offsetsr   r   r   r   query_promptquery_input_lenr   rc   rt   r!   r   r   r   r   query_contribs!                                    r2   rb   z RandomDatasetForReranking.sample  s0    ;''=HW9>\99i'+'?'?{OQ	(
 (
$
A} 
1&&	 	A!##	A5ALNI%MM%	1L@M#'#;#;+}a$
 $
 ![ )
%5Yz**
$s:=N9O9O'O"P"PQQ ((#!#%#=+,,- ) 	 	 	<o'; |$$ 	7 	7A6:6R6R#!#%hqk**;q>**!e- 7S 	7 	73FO^ !N2 OOV_56666q,	22 	 	AQY./E %</3u::==$ 
 !!(>,E,Eu,E,E,EE,s2K2KU2K2K2K/K/KK()03qI~3F3FF	       1$$NN ()).22   r1   rx   )r'   r(   r)   r*   r>   r   r   r  r   r.   r+   r   r{   r,   r    rb   r  r  s   @r2   r  r    s         # # # # # # "$*>&8 ^ ^ ^ ^ 	^
 ^ ^ ^ ^ 
m	^ ^ ^ ^ ^ ^ ^ ^r1   r  c                       e Zd ZdZdZdddZdZdZddddZd	Z	d* fdZ
deded
ej        fdZdededed
efdZdeeeef         d
efdZdeeeeef         ef         d
eeeeef         ef         fdZdeeeef         d
eeef         fdZdededeeef         deeeeef         ef         d
eeeeeef         eeeeef         ef         f         f
dZdededeeeeef         ef         deeef         d
eeeeef                  f
dZdd	ej        ej        ej        ej        eeeee	fd e d!ed"ed#e!d$ed%ed&ed'edeeef         dededeeeeef         ef         d(e!d
e"e#         fd)Z$ xZ%S )+RandomMultiModalDatasetu  
    Synthetic multimodal dataset (text + images) that extends RandomDataset.

    Status:
    - Images: supported via synthetic RGB data.
    - Video: supported via synthetic RGB data.
    - Audio: not yet supported.

    Sampling overview:
    1) Number of items per request is sampled uniformly from the integer range
       [floor(n·(1−r)), ceil(n·(1+r))], where n is the base count and r is
       `num_mm_items_range_ratio` in [0, 1]. r=0 keeps it fixed; r=1 allows 0.
       The maximum is further clamped to the sum of per-modality limits.
    2) Each item’s modality and shape is sampled from `bucket_config`, a dict
       mapping (height, width, num_frames) → probability. We treat
       `num_frames`=1 as image and `num_frames` > 1 as video.
       Entries with zero probability are removed and the rest are renormalized
       to sum to 1.
    3) Per-modality hard caps are enforced via `limit_mm_per_prompt`.
       When a modality reaches its cap, all of its buckets are excluded and the
       remaining probabilities are renormalized.

    Example bucket configuration:
    {(256, 256, 1): 0.5, (720, 1280, 1): 0.4, (720, 1280, 16): 0.1}
      - Two image buckets (`num_frames`=1) and one video bucket
      (`num_frames`=16).
    OBS.: Only image sampling is supported for now.
    T   rT   )r   r   r   g      ?))   r!  rT   )     rT   )r"  r#     Fr8   Nc                 :     t                      j        di | d S r   r
  r   s     r2   r>   z RandomMultiModalDataset.__init__>  r  r1   widthheightc                     | j                             dd||dft          j                  }t	          j        |          S )u=  Generate synthetic PIL image with random RGB values.

        NOTE: iid pixel sampling results in worst-case compression
        (good for stressing I/O), but very unlike real photos.
        We could consider a “low-freq” mode (e.g., noise blur)
        to emulate network realism instead of max stress.
        r   r!     dtype)r   r   r   uint8r   	fromarray)r<   r&  r'  random_pixelss       r2   generate_synthetic_imagez0RandomMultiModalDataset.generate_synthetic_imageA  sG     	**UA(	 + 
 
 }---r1   
num_framesc                 ^   ddl }| j                            dd|||dft          j                  } |j        d }d}t          dd	
          5 }|j        }	|                    |	||||f          }
|
	                                st          d          |D ]}|
                    |           |
                                 |                                 t          |	d          5 }|                                }ddd           n# 1 swxY w Y   d|icddd           S # 1 swxY w Y   dS )zGenerate synthetic video with random values.

        Creates a video with random pixel values, encodes it to MP4 format,
        and returns the content as bytes.
        r   Nr!  r)  r*  mp4v   z.mp4F)suffixdelete)fourccfps	frameSizezFailed to create video writerrbr   )cv2r   r   r   r,  VideoWriter_fourccr   nameVideoWriterisOpenedRuntimeErrorwritereleasecloser   read)r<   r&  r'  r0  r:  r.  r6  r7  	temp_file	temp_pathvideo_writerframefvideo_contents                 r2   generate_synthetic_videoz0RandomMultiModalDataset.generate_synthetic_videoQ  s    	


	***(	 + 
 
 ('0ve<<< 	,	!I ??&ceV_ +  L  ((** D"#BCCC& * *""5))))  """OO i&& )! !) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ]++	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	,s7   BD"&D;D"D	D"D	D""D&)D&configc                 ^    |d         dk    rdS |d         dk    rdS t          d|           )z&Map the configuration to the modality.rT   r   r   'Invalid multimodal item configuration: )rq   )r<   rK  s     r2   map_config_to_modalityz.RandomMultiModalDataset.map_config_to_modality}  s=    ":??7BZ!^^7OvOOPPPr1   bucket_configc                 X   t          d |                                D                       rt          d          d |                                D             }|st          d          t	          |                                          fd|                                D             S )zf
        Remove zero probability entries
        and normalize the bucket config to sum to 1.
        c              3   "   K   | ]
}|d k     V  dS r   Nr0   rg   vs     r2   r   zBRandomMultiModalDataset.normalize_bucket_config.<locals>.<genexpr>  s&      55q1u555555r1   z*Bucket config values must be non-negative.c                 &    i | ]\  }}|d k    ||S r  r0   rg   krU  s      r2   
<dictcomp>zCRandomMultiModalDataset.normalize_bucket_config.<locals>.<dictcomp>  s#    III$!Q1q55A555r1   zAGot invalid bucket config. Bucket config values must be non-zero.c                 "    i | ]\  }}||z  S r0   r0   )rg   rX  rU  totals      r2   rY  zCRandomMultiModalDataset.normalize_bucket_config.<locals>.<dictcomp>  s#    ???A1u9???r1   )anyvaluesrq   itemsr   )r<   rP  r[  s     @r2   normalize_bucket_configz/RandomMultiModalDataset.normalize_bucket_config  s     55m224455555 	KIJJJII-*=*=*?*?III 	S   M((**++????)<)<)>)>????r1   mm_item_configc                 T   |                      |          dk    r/t          |                     |d         |d                             S |                      |          dk    r6t          |                     |d         |d         |d                             S t          d|           )z
        Create synthetic images and videos and
        apply process_image/process_video respectively.
        This follows the OpenAI API chat completions
        https://github.com/openai/openai-python
        r   rT   r   r   r  rN  )rO  r   r/  r   rJ  rq   )r<   r`  s     r2   generate_mm_itemz(RandomMultiModalDataset.generate_mm_item  s     &&~66'AA --nQ.?PQARSS   ((88GCC --"1%~a'8.:K    W~WWXXXr1   base_items_per_requestnum_mm_items_range_ratiolimit_mm_per_promptc                 b   
 d|cxk    rdk    sn t          d          |                                D ]E\  }}                     |          }||vr't          d| d|                                           F                     |          }t
                              d|            fd|D             

fd|                                D             }|st          d	          t
                              d
|           t          t          |	                                          t          j        |d|z   z                      }t          dt          j        |d|z
  z                      }	|	|k    rt          d|	 d|           t
                              d|	|           |	|||fS )zG
        Get the sampling parameters for the multimodal items.
        r   r   z+num_mm_items_range_ratio must be in [0, 1].z	Modality z  is not in limit_mm_per_prompt: zNormalized bucket config: %sc                 :    h | ]}                     |          S r0   rO  )rg   cfgr<   s     r2   	<setcomp>zFRandomMultiModalDataset.get_mm_item_sampling_params.<locals>.<setcomp>  s'    XXX3d99#>>XXXr1   c                 $    i | ]\  }}|v 	||S r0   r0   )rg   rX  rU  allowed_modalitiess      r2   rY  zGRandomMultiModalDataset.get_mm_item_sampling_params.<locals>.<dictcomp>  s1     
 
 
QAAS<S<SAq<S<S<Sr1   z8No valid limits for modalities present in bucket_config.zUpdated mm-limit-per-prompt: %srT   r   z/Min num mm items is greater than max mm items: z > z1Sampling number of multimodal items from [%s, %s])rq   r^  rO  keysr_  rj   rk   minr   r]  r   r   r   r   )r<   rc  rd  re  rP  rX  rU  modalitymax_num_mm_itemsmin_num_mm_itemsrl  s   `         @r2   get_mm_item_sampling_paramsz3RandomMultiModalDataset.get_mm_item_sampling_params  sW    /666636666JKKK "'')) 	 	DAq22155H222 4 4 4*//114 4   3 44]CC*	
 	
 	

 YXXX-XXX
 
 
 
06688
 
 
 # 	YWXXX-	
 	
 	
 #**,,--I,4L0LMNN
 

 tz0A8P4PQRR
 
 ...;#; ;(8; ;  
 	?	
 	
 	
 	
 	
r1   rq  rp  c              #      K   t           j                            ||dz                       }|dk    rdS  fd|D             }|                                }t	          |                                          |k     rS j                            t          |                                          t          |                                                    } 	                    |          }	||	         ||	         k     r||	xx         dz  cc<   |V  n|
                                D ]#\  }
} 	                    |
          |	k    rd||
<   $t          d |                                D                       rt                              d|	           dS                      |          }t	          |                                          |k     QdS dS )a~  
        Iterator over the multimodal items for each request
        whose size is between min_num_mm_items and max_num_mm_items.

        Loop over the bucket config and sample a multimodal item.
        Loop until the number of multimodal items sampled is equal to
        request_num_mm_items or limit of multimodal items per prompt
        for all modalities is reached.

        Note:
        - This function operates on a per-request shallow copy of
          `bucket_config` (tuple->float). The original dict passed to
          `sample` is not mutated. If this ever changes, a test
          is implemented and will fail.
        rT   r   Nc                 <    i | ]}                     |          d S r  rh  )rg   rX  r<   s     r2   rY  z@RandomMultiModalDataset.get_mm_item_iterator.<locals>.<dictcomp>  s)    UUU!D77::AUUUr1   )pc              3   "   K   | ]
}|d k    V  dS rS  r0   rT  s     r2   r   z?RandomMultiModalDataset.get_mm_item_iterator.<locals>.<genexpr>3  s&      CC!qAvCCCCCCr1   z-Exhausted all multimodal items of modality %s)r.   r   r   copyr   r]  ro   r,   rm  rO  r^  allrj   r   r_  )r<   rq  rp  rP  re  request_num_mm_itemsmodality_counterbucket_config_copyr`  ro  rX  rU  s   `           r2   get_mm_item_iteratorz,RandomMultiModalDataset.get_mm_item_iterator  s     .  #I/1AA1EFF 
  
  1$$FUUUU}UUU*//11"))++,,/CCC!Y--',,..//48J8Q8Q8S8S3T3T .  N 22>BBH),?,III ***a/***%&&&& /4466 2 2DAq22155AA01*1- CC'9'@'@'B'BCCCCC NNG   E%)%A%ABT%U%U"3 "))++,,/CCCCCCCr1   r\   r]   r^   r_   r`   r   r   r   r   enable_multimodal_chatc                 .                          |||||          \  }}}                     |
||	|          \  }}}	}|j        }t          d |j                                        D                       }t          j        |          }t          j        t          t          |          t          |          z
                      }t                              dt          |          |                                ||          }g }d}t          |          D ]C}                     ||||t!          ||                   t!          ||                   ||          \  }}}||z  }                     ||||	          }t%          t          t&          t(          t*          f                   fd|D                       } |rP|}!                     ||           }!t/          |!|t!          ||                   d |t)          |          z             }"n7t/          ||t!          ||                   | |t)          |          z             }"|                    |"           E|dk    r3|dk    rdnd}#t                              d	t5          |          |#           |S )
Nc              3   .   K   | ]\  }}|j         |V  d S rw   )special)rg   tok_idtokens      r2   r   z1RandomMultiModalDataset.sample.<locals>.<genexpr>e  sB       !
 !
}!
!
 !
 !
 !
 !
 !
r1   z'Sampling from %d out of %d (vocab size)r   r   c                 :    g | ]}                     |          S r0   )rb  )rg   r`  r<   s     r2   ri   z2RandomMultiModalDataset.sample.<locals>.<listcomp>  s7       & )).99  r1   r!   r"   r#   r$   r&   r   r   r   )r   rr  r   r,   added_tokens_decoderr^  r   r   r   rp   rj   debugrl   r   rn   r   r.   r|  r   r/   r+   r   rK   r    rI   r   r   )$r<   r]   r^   r_   r`   r   r   r   r   re  rc  rd  rP  r}  r=   r   r   r   rq  rp  r   r   r   r   r   mm_requestsr   rt   r!   r   r   mm_item_iteratorr?   mm_chat_promptsample_requestr   s$   `                                   r2   rb   zRandomMultiModalDataset.sample;  s   ( ,0+C+C+y*i,
 ,
(
K ,,"$	
 
	
 )
 ! !
 !
!*!?!E!E!G!G!
 !
 !
 
 

 Yz**
$s:=N9O9O'O"P"PQQ5s>7J7JJ	
 	
 	
  ??>:FF |$$ 2	/ 2	/A6:6R6R#!1%%jm,,71:- 7S 	7 	73FO^ !N2 #88  #	    T#s(^$   *:   J &  '-!%!J!JJ" " "/).(+KN(;(;%)03q669" " " "/!.(+KN(;(;%/03q669" " " ~....1$$1A55667DNN ())   r1   rx   )&r'   r(   r)   r*   rz   DEFAULT_LIMIT_MM_PER_PROMPTDEFAULT_BASE_ITEMS_PER_REQUEST DEFAULT_NUM_MM_ITEMS_RANGE_RATIODEFAULT_MM_ITEM_BUCKET_CONFIGDEFAULT_ENABLE_MULTIMODAL_CHATr>   r.   r   r/  r/   rJ  r  r+   rO  r   r_  r   r   rb  rr  r   r|  r   r   r   r  r  r   r{   r,   r    rb   r  r  s   @r2   r  r    s        : M,/!"<"<%&"'*$% %!
 &+"# # # # # #.c .3 .5; . . . . *,*,"%*,36*,	*, *, *, *,XQU3S=-A Qc Q Q Q Q@!%S#"6"=>@	eCcM"E)	*@ @ @ @*Yc3m,Y 
c	Y Y Y Y0I
 #I
 #(I
 "#s(^	I

 E#sC-0%78I
 
sCc3heCcM.BE.I)JJ	KI
 I
 I
 I
V;V;V ;V E#sC-0%78	;V
 "#s(^;V 
%S#&	';V ;V ;V ;VB "$#':*>&8':.I&D*J *'E!x x x x 	x
 x x x x x "#s(^x !$x #(x #sC- %'
x  !%!x$ 
m	%x x x x x x x xr1   r  c                   x     e Zd ZdZd fdZddZ	 	 	 	 	 	 dded	ed
edz  dedz  dedz  de	dede	de
fdZ xZS )ShareGPTDatasetz
    Implements the ShareGPT dataset.  Loads data from a JSON file and generates
    sample requests based on conversation turns.
    r8   Nc                 b     t                      j        di | |                                  d S r   r   r>   rP   r   s     r2   r>   zShareGPTDataset.__init__  6    ""6"""r1   c                 v   | j         t          d          t          | j         d          5 }t          j        |          | _        d d d            n# 1 swxY w Y   d | j        D             | _        t          j        | j                   t          | dd          st          j
        | j                   d S d S )N/dataset_path must be provided for loading data.r   encodingc                 L    g | ]!}d |v t          |d                    dk    |"S )conversationsr  rl   )rg   entrys     r2   ri   z-ShareGPTDataset.load_data.<locals>.<listcomp>  sD     
 
 
%''Co0F,G,G1,L,L ,L,L,Lr1   r7   F)r5   rq   r   jsonloadr;   rW   rm   r6   getattrshuffler<   rH  s     r2   rP   zShareGPTDataset.load_data  s    $NOOO$#g666 	%!	!DI	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	%
 

 
 
	
 	D$%%%t.66 	&N49%%%%%	& 	&s   AAAFr\   r]   r^   rR   rQ   r   r}  r_   r`   c	                    g }
d}| j         D ]T}t          |
          |k    r n=|d         d         d         |d         d         d         }}|                     ||          } ||          j        } ||          j        }t          |          }|t          |          n|}t	          |||d u          s|                    d          x}rt          |          }n)|                    d          x}rt          |          }nd }|r|                     ||          }|
	                    t          ||||||t          |          z   	                     |dz  }V|                     |
|||           |
S )
Nr   r  valuerT   rQ   rR   )r   r   r   )r!   r"   r#   r%   r$   r&   )r;   rl   r[   	input_idsr   getr   r   rK   rI   r    r+   rv   )r<   r]   r^   rR   rQ   r   r}  r_   r`   r=   samplesindr  r!   
completionr%   
prompt_idscompletion_idsr"   new_output_len
image_pathr?   
video_paths                          r2   rb   zShareGPTDataset.sample  s    Y '	 '	E7|||++o&q)'2o&q)'2 F
  77#y 8  L #6**4J&Yz22<NZJ4>4FS000JN$*4D*@   
 "YYw///z "*:66

$yy111 "*:66

!
% WBB6:VVNN!)(6!-%/03s88;  	 	 	 1HCC&&\#4m	
 	
 	
 r1   rx   )NNNFr\   Fr'   r(   r)   r*   r>   rP   r   r.   r+   r{   r,   rb   r  r  s   @r2   r  r    s         
     & & & &( !% $!%',!##9 9 9 9 :	9
 :9 $J9 !%9 9 9 
9 9 9 9 9 9 9 9r1   r  c                       e Zd ZdZddZdS )_ValidateDatasetArgsz@Argparse action to validate dataset name and path compatibility.Nc                     t          || j        |           t          |dd          }t          |dd           }|dk    r||                    d|            d S d S d S )Ndataset_namerW   r5   zCannot use 'random' dataset with --dataset-path. Please specify the appropriate --dataset-name (e.g., 'sharegpt', 'custom', 'sonnet') for your dataset file: )setattrdestr  error)r<   parser	namespacer]  option_stringr  r5   s          r2   __call__z_ValidateDatasetArgs.__call__  s    	49f--- y.(CCy.$?? 8##(@LL"  " "     $#(@(@r1   rw   )r'   r(   r)   r*   r  r0   r1   r2   r  r    s.        JJ     r1   r  r  c                    |                      dt          d           |                      dt          dd           |                      dt          d	t          g d
d           |                      ddd           |                      dt          d t          d           |                      ddd           |                      ddd           |                      ddd           |                     d          }|                     dt          dd           |                     d          }|                     dt          dd            |                     d!t          d d"           |                     d#          }|                     d$t          d%d&           |                     d't          d(d)           |                     d*t          d+d,           |                     d-          }|                     d.t          d d/           |                     d0          }|                     d1t
          d2d3           |                     d4t
          d5d6           |                     d7          }t          |           |                     d8          }t          |           |                     d9          }|                     d:t          d d;           |                     d<t          d d=           |                     d>t          d d?           |                     d@t          d dA           |                     dB          }	|	                     dCt          ddD           |	                     dEt          ddF           |	                     dGt          dHdI           |	                     dJt          dKdL           d S )MNz--seedr   )rB   defaultz--num-promptsi  zNumber of prompts to process.rB   r  helpz--dataset-namerW   )
sharegptburstgptsonnetrW   	random-mmrandom-rerankhfcustomprefix_repetition
spec_benchz$Name of the dataset to benchmark on.)rB   r  actionchoicesr  z--no-stream
store_truez*Do not load the dataset in streaming mode.r  r  z--dataset-pathzWPath to the sharegpt/sonnet dataset. Or the huggingface dataset ID if using HF dataset.)rB   r  r  r  z--no-oversamplezDDo not oversample if the dataset has fewer samples than num-prompts.z--skip-chat-templatezCSkip applying chat template to prompt for datasets that support it.z--disable-shufflez@Disable shuffling of dataset samples for deterministic ordering.zcustom dataset optionsz--custom-output-lenr!  zNumber of output tokens per request. Unless it is set to -1, the value overrides potential output length loaded from the dataset. It is used only for custom dataset.zspec bench dataset optionsz--spec-bench-output-lenzCNum of output tokens per request, used only for spec bench dataset.z--spec-bench-categoryz=Category for spec bench dataset. If None, use all categories.zsonnet dataset optionsz--sonnet-input-len&  zANumber of input tokens per request, used only for sonnet dataset.z--sonnet-output-len   zBNumber of output tokens per request, used only for sonnet dataset.z--sonnet-prefix-len   zBNumber of prefix tokens per request, used only for sonnet dataset.zsharegpt dataset optionsz--sharegpt-output-lenzVOutput length for each request. Overrides the output length from the ShareGPT dataset.zblazedit dataset optionsz--blazedit-min-distancer   z7Minimum distance for blazedit dataset. Min: 0, Max: 1.0z--blazedit-max-distancer   z7Maximum distance for blazedit dataset. Min: 0, Max: 1.0zrandom dataset optionsz>random multimodal dataset options extended from random datasetzhf dataset optionsz--hf-subsetzSubset of the HF dataset.z
--hf-splitzSplit of the HF dataset.z	--hf-namez|Name of the dataset on HuggingFace (e.g., 'lmarena-ai/VisionArena-Chat'). Specify this if your dataset-path is a local path.z--hf-output-lenzYOutput length for each request. Overrides the output lengths from the sampled HF dataset.z!prefix repetition dataset optionsz--prefix-repetition-prefix-lenzMNumber of prefix tokens per request, used only for prefix repetition dataset.z--prefix-repetition-suffix-lenz|Number of suffix tokens per request, used only for prefix repetition dataset. Total input length is prefix_len + suffix_len.z --prefix-repetition-num-prefixesr   z|Number of prefixes to generate, used only for prefix repetition dataset. Prompts per prefix is num_requests // num_prefixes.z--prefix-repetition-output-lenr   zMNumber of output tokens per request, used only for prefix repetition dataset.)add_argumentr.   r+   r  add_argument_groupr   add_random_dataset_base_args"add_random_multimodal_dataset_args)
r  custom_groupspec_bench_groupsonnet_groupsharegpt_groupblazedit_grouprandom_grouprandom_mm_grouphf_groupprefix_repetition_groups
             r2   add_dataset_parserr  %  s'   
sA666
,	     #
 
 
 4#    & 9    
 #=     S    
 R    
 O     ,,-EFFL(	     001MNN!!!R	 "    !!L	 "    ,,-EFFLP	     Q	     Q	     ../IJJN%	      ../IJJN!F	      !F	      ,,-EFFL ...//H O '777(()=>>HC4O     33M     A  	 	 	 '	     %77+  (((	 )    (((M	 )    ((*G	 )    (((	 )     r1   parser_or_groupc                 b   |                      dt          dd           |                      dt          dd           |                      dt          d	d
           |                      dt          dd           |                      dt          dd           |                      ddd           dS )a@  Add CLI arguments for base random dataset options.

    This function adds arguments needed for:
    - random (random dataset)
    - random-mm (random multimodal dataset)
    - random-rerank (random dataset for reranking)

    Args:
        parser_or_group: Either a parser or an argument group to add arguments to.
    z--random-input-lenr}   zBNumber of input tokens per request, used only for random sampling.r  z--random-output-lenr   zCNumber of output tokens per request, used only for random sampling.z--random-range-ratior   zRange ratio for sampling input/output length, used only for random sampling. Must be in the range [0, 1) to define a symmetric sampling range[length * (1 - range_ratio), length * (1 + range_ratio)].z--random-prefix-lenr   zNumber of fixed prefix tokens before the random context in a request. The total input length is the sum of `random-prefix-len` and a random context length sampled from [input_len * (1 - range_ratio), input_len * (1 + range_ratio)].z--random-batch-sizerT   zCBatch size for random sampling. Only used for embeddings benchmark.z--no-rerankerr  zPWhether the model supports reranking natively. Only used for reranker benchmark.r  N)r  r.   r   )r  s    r2   r  r    s      Q	 !      R	 !      D	 !      . !      S	 !      1	 !     r1   c                    |                      dt          t          j        d           |                      dt          t          j        d           |                      dt          j        t          j        d           dt          d	t          t          t          t          t          f         t          f         fd
}|                      d|t          j        d           dS )zAdd CLI arguments for random multimodal dataset options.

    This function adds arguments needed for:
    - random-mm (random multimodal dataset)

    Args:
        parser_or_group: Either a parser or an argument group to add arguments to.
    z"--random-mm-base-items-per-requestzBase number of multimodal items per request for random-mm. Actual per-request count is sampled around this base using --random-mm-num-mm-items-range-ratio.r  z$--random-mm-num-mm-items-range-ratioao  Range ratio r in [0, 1] for sampling items per request. We sample uniformly from the closed integer range [floor(n*(1-r)), ceil(n*(1+r))] where n is the base items per request. r=0 keeps it fixed; r=1 allows 0 items. The maximum is clamped to the sum of per-modality limits from --random-mm-limit-mm-per-prompt. An error is raised if the computed min exceeds the max.z--random-mm-limit-mm-per-prompta.  Per-modality hard caps for items attached per request, e.g. '{"image": 3, "video": 0}'. The sampled per-request item count is clamped to the sum of these limits. When a modality reaches its cap, its buckets are excluded and probabilities are renormalized.OBS.: Only image sampling is supported for now.rU  r8   c                    dt           dt           t          t          t          t          f         t          f         fd}t	          | t                     r ||           S t	          | t
                    rCt          j        |           }t	          |t                     st          d           ||          S t          d          )Ndr8   c           	      0   i }|                                  D ]\  }}|}t          |t                    r@t          t                    5  t          j        |          }d d d            n# 1 swxY w Y   t          |t                    r,t          |          dk    rt          d |D                       st          d|d          t          |          |t          |d                   t          |d                   t          |d                   f<   |S )Nr)  c              3   @   K   | ]}t          |t                    V  d S rw   )rG   r.   )rg   xs     r2   r   ziadd_random_multimodal_dataset_args.<locals>._parse_mm_bucket_config.<locals>.normalize.<locals>.<genexpr>`  s,      <<1Jq#..<<<<<<r1   zInvalid bucket key z. Expected tuple (H, W, T).r   rT   r  )r^  rG   r+   r   	Exceptionastliteral_evalr  rl   rx  rq   r   r.   )r  outrX  valkeys        r2   	normalizezVadd_random_multimodal_dataset_args.<locals>._parse_mm_bucket_config.<locals>.normalizeV  sS   57C'')) J J3c3'' 4!),, 4 4!.s334 4 4 4 4 4 4 4 4 4 4 4 4 4 4 sE**CA<<<<<<< & %NaNNN   @ESzzSQ[[#c!f++s3q6{{;<<Js   A((A,	/A,	z#Bucket config must parse to a dict.z0Unsupported value for --random-mm-bucket-config.)	r/   r  r.   r   rG   r+   r  r  rq   )rU  r  parseds      r2   _parse_mm_bucket_configzCadd_random_multimodal_dataset_args.<locals>._parse_mm_bucket_configT  s    	 	$uS#s]';U'B"C 	 	 	 	$ a 	 9Q<<a 	%%a((Ffd++ H !FGGG9V$$$KLLLr1   z--random-mm-bucket-configa  The bucket config is a dictionary mapping a multimodal itemsampling configuration to a probability.Currently allows for 2 modalities: images and videos. An bucket key is a tuple of (height, width, num_frames)The value is the probability of sampling that specific item. Example: --random-mm-bucket-config {(256, 256, 1): 0.5, (720, 1280, 1): 0.4, (720, 1280, 16): 0.10} First item: images with resolution 256x256 w.p. 0.5Second item: images with resolution 720x1280 w.p. 0.4 Third item: videos with resolution 720x1280 and 16 frames w.p. 0.1OBS.: If the probabilities do not sum to 1, they are normalized.OBS bis.: Only image sampling is supported for now.N)r  r.   r  r  r   r  r  loadsr  objectr/   r  r  )r  r  s     r2   r  r  "  s      ,'F4 ! 	 	 	   .'HF !      )Z'C> !   M6 Md5c33G3N.O M M M M<   #$'EB !     r1   c           
      \
    t           d          sd _         j        dk    rQt           j         j                  }|                     j         j         j	         j         j
                  }n j        dk    rt           j         j                  } j        dk    r=|                     j         j         j         j        d j         j
        	          }nHj        sj        s
J d
            |                     j         j         j         j        d j         j
        	          }n j        dk    r]i } j        t$          j        v s j        t$          j        v rt$          }d _        d  _        n j        t.          j        v s j        t.          j        v rt.          }d _        d  _        nX j        t0          j        v s j        t0          j        v rt0          }d _        n" j        t2          j        v s j        t2          j        v rt2          }d _        n j        t4          j        v s j        t4          j        v r	t4          }n j        t6          j        v s j        t6          j        v r	t6          }n j        t8          j        v s j        t8          j        v rt8          }d _        nX j        t:          j        v s j        t:          j        v rt:          }d _        n" j        t<          j        v s j        t<          j        v rt<          }d _        n j        t>          j        v rt>          }d _         j          j!        d}n j        tD          j        v s j        tD          j        v rtD          }d _        n j        tF          j        v s j        tF          j        v rtF          }d _        d  _        nKtI          d tJ          &                                D                       }tO          d j         d| d          |j(        r! j        dv sd j        v stO          d            | j         j         j         j)         j*         j         j                  j        d& j         j+         j         j
         j	        d|}n fd fd fd fd fd fd fd d!}	  j        d"k    r j        d#vrtO          d$           | j                             }n*# tX          $ r}tO          d% j                   |d }~ww xY w|S )'Nr_   r\   r  )r5   r7   )r^   r]   r   skip_chat_templater_   r`   r  openai-chatF)r^   r   r   r   r]   return_prompt_formattedr_   r`   z;Tokenizer/model must have chat template for sonnet dataset.Tr  train
validation)min_distancemax_distancer  c                 &    g | ]}|j         D ]}|S r0   )SUPPORTED_DATASET_PATHS)rg   clsr  s      r2   ri   zget_samples.<locals>.<listcomp>  sB       (+(C  % !   r1   Unsupported dataset path: zH. Huggingface dataset only supports dataset_path from one of following: z_. Please consider contributing if you would like to add support for additional dataset formats.)r  zopenai-audiozembeddings-zSMulti-modal content is only supported on 'openai-chat' and 'openai-audio' backends.)r5   dataset_subsetdataset_splitr6   	no_streamhf_namer7   )r^   r]   r   r_   r`   r  c                      t           j         j         j                                       j         j         j         j                  S )N)r5   categoryr7   )r^   r]   r   r_   r`   )		SpecBenchr5   spec_bench_categoryr7   rb   num_promptsspec_bench_output_lenr_   r`   argsr]   s   r2   <lambda>zget_samples.<locals>.<lambda>5  s\    )!.1 $ 4# # # f!-#5"&"8"0   	 r1   c                      t           j         j         j                                       j         j         j         j                  S )Nr6   r5   r7   )r]   r^   r   r_   r`   )	r  rm   r5   r7   rb   r  sharegpt_output_lenr_   r`   r  s   r2   r  zget_samples.<locals>.<lambda>@  s[     I!. $ 4! ! ! f#!-3"&"8"0   	 r1   c                      t           j         j         j                                       j         j         j                  S )Nr
  )r]   r^   r_   r`   )BurstGPTDatasetrm   r5   r7   rb   r  r_   r`   r  s   r2   r  zget_samples.<locals>.<lambda>K  sU     I!. $ 4! ! ! f#!-"&"8"0	   	 r1   c                      t           j         j         j                                       j         j         j         j         j	         j
         j         j        	  	        S )Nr
  )	r]   r^   r   r   r   r   r_   r   r`   )r   rm   r5   r7   rb   r  random_prefix_lenrandom_input_lenrandom_output_lenrandom_range_ratior_   random_batch_sizer`   r  s   r2   r  zget_samples.<locals>.<lambda>U  ss    m I!. $ 4   f#!-1/1 3"&"80"0  
 
	 r1   c                      t           j         j         j                                       j         j         j         j         j	         j
         j         j         j         j         j                  S )Nr
  )r]   r^   r   r   r   r   rc  re  rd  rP  r_   r`   )r  rm   r5   r7   rb   r  r  r  r  r   random_mm_base_items_per_requestrandom_mm_limit_mm_per_prompt"random_mm_num_mm_items_range_ratiorandom_mm_bucket_configr_   r`   r  s   r2   r  zget_samples.<locals>.<lambda>d  s    !8 I!. $ 4" " " f#!-1 3/1'+'L$($F)-)P":"&"8"0   	 r1   c            	          t           j         j         j                                       j         j         j         j         j	         j
                   S )Nr
  )r]   r^   r   r   r_   r   r  )r  rm   r5   r7   rb   r  r  r  r_   r  no_rerankerr  s   r2   r  zget_samples.<locals>.<lambda>v  sk    %> I!. $ 4& & & f#!-/ 3"&"80 $ 00   	 r1   c            
          t           j         j         j                                       j         j         j         j         j	         j
         j                  S )Nr
  )r]   r^   r   
suffix_lennum_prefixesr   r_   r`   )PrefixRepetitionRandomDatasetrm   r5   r7   rb   r  prefix_repetition_prefix_lenprefix_repetition_suffix_lenprefix_repetition_num_prefixesprefix_repetition_output_lenr_   r`   r  s   r2   r  zget_samples.<locals>.<lambda>  sn    )F I!. $ 4* * * f#!-<<!@<"&"8"0  	 		 r1   )r  r  r  rW   r  r  r  r  )r  zHMulti-modal content (images) is only supported on 'openai-chat' backend.zUnknown dataset: r0   )-hasattrr_   r  CustomDatasetr5   r7   rb   r  custom_output_lenr  r`   SonnetDatasetbackendsonnet_input_lensonnet_output_lensonnet_prefix_lenchat_templatedefault_chat_templateVisionArenaDatasetr  r  hf_split	hf_subsetMMVUDatasetInstructCoderDatasetMTBenchDatasetMultiModalConversationDatasetConversationDatasetAIMODatasetNextEditPredictionDataset
ASRDatasetBlazeditDatasetblazedit_min_distanceblazedit_max_distanceMLPerfDatasetMMStarDatasetrp   HuggingFaceDataset__subclasses__rq   rz   rm   r  hf_output_lenKeyError)	r  r]   datasetinput_requests	hf_kwargsdataset_classsupported_datasetsdataset_mappingerrs	   ``       r2   get_samplesrH    sz   4,-- $!#H$$*D<P
 
 
 !)-#6"4, ( 
 
 
	h	&	&*D<P
 
 
 <=(($^^!-/11#(-"&"8"0 , 	 	NN * i.M  M M %^^!-/11#(,"&"8"0 , 	 	NN 
	d	"	" 	!3!KKK|1III.M#DM!DNN!DDD|{BBB'M(DM!DNN!5!MMM|3KKK0M#DMM!GGG|~EEE*M#DMM!>!VVV|<TTT9MM!4!LLL|2JJJ/MM!DDD|{BBB'M#DMM!:!RRR|8PPP5M#DMM!CCC|zAAA&M#DMM/"III+M#DM $ : $ : II
 !FFF|}DDD)M#DMM!FFF|}DDD)M!DM!DNN!$ 1@@BB  " " FT-> F F+=F F F   & 		L;;;,, +  *>-	nL 0
 
 
 
 ))"4,#6
 
 
 
*
 
 
 
 

 
 
 
 
	 	 	 	 	        $    " " " " "_]
 ]
~		O K//DL4W4W -   @_T->?AANN 	O 	O 	OD1BDDEE3N	O s   	8T 
T)T$$T)c                   ~     e Zd ZdZd fdZddZ	 	 	 	 	 	 	 dded	ed
edz  dedz  dedz  de	de	dede	de
fdZ xZS )r$  a  
    Implements the Custom dataset.  Loads data from a JSONL file and generates
    sample requests based on conversation turns. E.g.,
    ```
    {"prompt": "What is the capital of India?", "output_tokens": 10}
    {"prompt": "What is the capital of Iran?", "output_tokens": 1520}
    {"prompt": "What is the capital of China?", "output_tokens": 819}
    ```
    Note that 'output_tokens' column is optional and has to be provided only if
    'custom-output-len' argument is None or -1.
    r8   Nc                 b     t                      j        di | |                                  d S r   r  r   s     r2   r>   zCustomDataset.__init__  r  r1   c                    | j         t          d          g | _        | j                             d          rzt	          j        | j         d          }d|j        vrt          d          |                                D ]1\  }}| j                            |	                                           2nt          d          t          j        | j                   t          | dd	          st          j        | j                   d S d S )
Nr  z.jsonlTpath_or_buflinesr!   z*JSONL file must contain a 'prompt' column.z1Only JSONL format is supported for CustomDataset.r7   F)r5   rq   r;   endswithpd	read_jsoncolumnsiterrowsrI   to_dictrN   rW   rm   r6   r  r  )r<   
jsonl_datar  rows       r2   rP   zCustomDataset.load_data  s   $NOOO 	 %%h// 	$2C4PPPJ z111 !MNNN %--// 0 03	  ////0 &C   	D$%%%t.66 	&N49%%%%%	& 	&r1   Fr\   r]   r^   rR   rQ   r   r}  r  r_   r`   c
                    t          | j                  | _        |dk    r"| j        }t                              d|           g }t          | j                  D ]\  }}t          |          |k    r n|d         }|}||dk    r]d|vrt          d          	 t          |d                   }n3# t          t          f$ r}t          d|d          d          |d }~ww xY w|s|	                    d	|d
gdd          }t           ||          j
                  }|                    t          ||||t          |          z                        |                     ||||	           |S )Nr   zHnum_requests is set to 0 or negative, so using all available samples: %dr!   rM  output_tokenszYIf no output length is provided the custom dataset must contain an 'output_tokens' field.z6Invalid value for 'output_tokens' in custom dataset: 'z'. Must be an integer.rC   rD   TFadd_generation_prompttokenizer   )rl   r;   num_available_samplesrj   rk   	enumeraterq   r.   rJ   apply_chat_templater  rI   r    r+   rv   )r<   r]   r^   rR   rQ   r   r}  r  r_   r`   r=   sampled_requestsrt   itemr!   r  er"   s                     r2   rb   zCustomDataset.sample  s    &)^^"15LKK5    ++ &	 &	GAt#$$44(^F'N!Z2%5%5"$..$P  
%(o)>%?%?NN"I.   $J 1J J J   & "66$889*." 7   YYv..899J##!)(603q669	      	&&l,=}	
 	
 	
  s   B..C?CCrx   )NNNFFr\   Fr  r  s   @r2   r$  r$    s       
 
      &  &  &  &L !% $!%',#(!##C  C  C  C  :	C 
 :C  $JC  !%C  !C  C  C  
C  C  C  C  C  C  C  C r1   r$  c                   <     e Zd ZdZd fdZddZdef fdZ xZS )r  z
    Implements the SpecBench dataset: https://github.com/hemingkx/Spec-Bench
    Download the dataset using:
    wget https://raw.githubusercontent.com/hemingkx/Spec-Bench/refs/heads/main/data/spec_bench/question.jsonl
    r8   Nc                     |                     dd           | _         t                      j        di | |                                  d S )Nr  r0   )popr  r   r>   rP   r   s     r2   r>   zSpecBench.__init__+  sK    

:t44""6"""r1   c                    | j         t          d          g | _        t          j        | j         d          }d|j        vrt          d          |                                D ]G\  }}| j        r| j        |d         k    r*|d         d         }| j                            d|i           Ht          j
        | j                   t          | d	d
          st          j        | j                   d S d S )Nr  TrL  turnsz)JSONL file must contain a 'turns' column.r  r   r!   r7   F)r5   rq   r;   rP  rQ  rR  rS  r  rI   rW   rm   r6   r  r  )r<   rU  r  rV  r!   s        r2   rP   zSpecBench.load_data0  s   $NOOO	 \d.?tLLL
 *,,,HIII ))++ 	5 	5FAsM 5t}J'G'GWa	  (F!3444D$%%%t.66 	&N49%%%%%	& 	&r1   c                 6     t                      j        di |S r   )r   rb   r   s     r2   rb   zSpecBench.sampleG  s    uww~'''''r1   rx   )	r'   r(   r)   r*   r>   rP   r,   rb   r  r  s   @r2   r  r  $  s              
& & & &.($ ( ( ( ( ( ( ( ( ( (r1   r  zDSonnetDataset is deprecated and will be removed in a future version.c                   v     e Zd ZdZdZdZdZ	 	 d fdZddZeeed	d
d	fde	de
de
de
de
dedededefdZ xZS )r&  z
    Simplified implementation of the Sonnet dataset.  Loads poem lines from a
    text file and generates sample requests.  Default values here copied from
    `benchmark_serving.py` for the sonnet dataset.
    r  r  r  r8   Nc                 b     t                      j        di | |                                  d S r   r  r   s     r2   r>   zSonnetDataset.__init___  s8     	""6"""r1   c                     | j         st          d          t          | j         d          5 }|                                | _        d d d            d S # 1 swxY w Y   d S )Nzdataset_path must be provided.r   r  )r5   rq   r   	readlinesr;   r  s     r2   rP   zSonnetDataset.load_dataf  s      	?=>>>$#g666 	&!DI	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	&s   AAAFr\   r]   r^   r   r   r   r  r_   r`   c	                    fd| j         D             }
t          d |
D                       t          |
          z  }d}d|dg}                    |dd          }t           |          j                  }||k    rt          d	| d
          t          ||z
  |z            }t          t          ||z
  |z            d          }| j         d |         }g }d}t          |          |k     rt          j	        | j         ||z
            }| d
                    ||z              }d|dg}                    |dd          }t           |          j                  }||k    r?|                    t          |r|n||||t          |          z                        |dz  }t          |          |k     |S )Nc                 0    g | ]} |          j         S r0   )r  )rg   liner]   s     r2   ri   z(SonnetDataset.sample.<locals>.<listcomp>y  s$    KKK99T??4KKKr1   c              3   4   K   | ]}t          |          V  d S rw   r  )rg   tokenss     r2   r   z'SonnetDataset.sample.<locals>.<genexpr>z  s(      @@fc&kk@@@@@@r1   z5Pick as many lines as you can from these poem lines:
rC   rD   TFrY  z8'input_len' must be higher than the base prompt length (z).r   )rX  r\   r   rT   )r;   r   rl   r^  r  rq   roundr   rW   r  joinrI   r    r+   )r<   r]   r^   r   r   r   r  r_   r`   r=   tokenized_linesavg_lenbase_promptbase_msgbase_fmtbase_offsetnum_input_linesnum_prefix_linesprefix_linesr  r  extra_linesr!   msgprompt_formattedr"   s    `                        r2   rb   zSonnetDataset.samplel  sL    LKKKKKK@@@@@@@3CWCWW O#<<=00D5 1 
 
 ))H--788##$$ $ $    [!8G CDDuj;&>'%IJJANNy!2"2!23'll\)) .	_/??  K $JRWW\K-G%H%HJJF"v667C(<<4%  =     YY'788BCCJY&&!3JV//PV#-,6#4s3xx#?	     q' 'll\))( r1   rx   )r'   r(   r)   r*   r   r  r  r>   rP   r   r.   r{   r+   r,   rb   r  r  s   @r2   r&  r&  Q  s           
     & & & & -*,(-!##8 8 8 8 	8
 8 8 "&8 8 8 
8 8 8 8 8 8 8 8r1   r&  c                        e Zd ZdZd fdZd ZdedefdZ	 	 	 	 dd
e	dededz  de
dz  de
dedee         fdZ xZS )r  z
    Implements the BurstGPT dataset.  Loads data from a CSV file and generates
    sample requests based on synthetic prompt generation. Only rows with Model
    "GPT-4" and positive response tokens are used.
    r8   Nc                 b     t                      j        di | |                                  d S r   r  r   s     r2   r>   zBurstGPTDataset.__init__  r  r1   c                     | j         t          d          t          j        | j                   }||d         dk             }||d         dk             }|| _        d S )Nr  ModelzGPT-4zResponse tokensr   )r5   rq   rP  read_csvr;   )r<   dfgpt4_dfs      r2   rP   zBurstGPTDataset.load_data  sa     $NOOO[*++R[G+,'"34q89			r1   r^   c                     |t          | j                  k    r"| j                            || j                  }n"| j                            || j        d          }|j                                        S )N)nrandom_stateT)r  r  replace)rl   r;   rb   r6   r]  r   )r<   r^   r;   s      r2   _sample_loaded_dataz#BurstGPTDataset._sample_loaded_data  sr    3ty>>))9##lAQ#RRDD9##!- $  D {!!###r1   r\   Fr]   rQ   rR   r_   r`   c                    g }|                      |          }	t          |          D ]t          |	         d                   }
t          |	         d                   }|                     ||          }|j        fdt          |
          D             }|                    |          }|                    t          ||
|||t                    z                        |S )N)r^   r  r)  r  c                      g | ]
}|z   z  S r0   r0   )rg   jrt   r   s     r2   ri   z*BurstGPTDataset.sample.<locals>.<listcomp>  s"    HHH!!a%:-HHHr1   )r!   r"   r#   r%   r&   )	r  rn   r.   r[   r   r   rI   r    r+   )r<   r]   r^   rQ   rR   r_   r`   r=   r  r;   r   r   lora_req	token_idsr!   rt   r   s                  @@r2   rb   zBurstGPTDataset.sample  s    ''\'BB|$$ 	 	ADGAJIT!WQZJ33#y 4  H #-J IHHHHuY7G7GHHHI%%i00FNN!((2!)03q669      r1   rx   )NNr\   F)r'   r(   r)   r*   r>   rP   r.   r,   r  r   r+   r{   r    rb   r  r  s   @r2   r  r    s                
$ 
$ 
$ 
$ 
$ 
$  !% $!##        :	 
 :      
m	               r1   r  c                        e Zd ZU dZ e            Zee         eeef         z  e	d<   	 	 	 ddedede
dedz  d	edz  d
df fdZddZ xZS )r=  z.Base class for datasets hosted on HuggingFace.r  FNr5   r  r  r  r  r8   c                      t                      j        dd|i| || _        || _        | | _        |p|| _        |                                  d S )Nr5   r0   )r   r>   r  r  load_streamr  rP   )r<   r5   r  r  r  r  r=   r   s          r2   r>   zHuggingFaceDataset.__init__  sc     	==l=f===*,(=.,r1   c                     t          | j        | j        | j        | j                  | _        t          | dd          s'| j                            | j                  | _        dS dS )z$Load data from HuggingFace datasets.)r<  split	streamingr7   F)rm   N)	r   r5   r  r  r  r;   r  r  r6   rO   s    r2   rP   zHuggingFaceDataset.load_data	  su     $$&	
 
 
	 t.66 	A	))t/?)@@DIII	A 	Ar1   )FNNrx   )r'   r(   r)   r*   rp   r  r+   r/   r   r-   r{   r>   rP   r  r  s   @r2   r=  r=    s         88>AceeSXS(](;;CCC  %)"   	
 d
 t 
     "	A 	A 	A 	A 	A 	A 	A 	Ar1   r=  c                   P    e Zd ZdZdhZdZ	 	 	 	 ddedededz  d	ed
e	dede
fdZdS )r4  z(Dataset for text-only conversation data.z Aeala/ShareGPT_Vicuna_unfilteredFNr\   r]   r^   r   r}  r_   r`   r8   c                    | j                             d           }g }	d}
|d u }|D ] }t          |	          |k    r n	|d         }|d         d         |d         d         }} ||          j        } ||          j        }t          |          }t          |          }|r|n|}t	          |t
                    r|dk    sJ |rt          ||          sd|v rt          |d                   nd }|r|                     ||          }|		                    t          |||||t          |
          z                        |
dz  }
"|                     |	|||           |	S )Nc                 4    t          | d                   dk    S Nr  r  r  r  s    r2   r  z,ConversationDataset.sample.<locals>.<lambda>1	      3q7I3J3Ja3O r1   r   r  r  rT   r   r  r;   filterrl   r  rG   r.   r   r   rK   rI   r    r+   rv   r<   r]   r^   r   r}  r_   r`   r=   filtered_datar_  r  dynamic_outputr`  convr!   r  r  r  r"   completion_lenr?   s                        r2   rb   zConversationDataset.sample&	      	(()O)OPP#t+! 	 	D#$$44(D!%a!14773CJF"6**4J&Yz22<NZJ 00N+9IzJj#..A:>>>A &7
N&S&S 9@DtG}555dJ% W BB6:VV##!)(2%/03s88;     1HCC&&l,=}	
 	
 	
  r1   NFr\   Fr'   r(   r)   r*   r  rz   r   r.   r{   r+   r,   rb   r0   r1   r2   r4  r4  	  s        22 	+ M "&',!##1  1  1  1  $J	1 
 !%1  1  1  
1  1  1  1  1  1 r1   r4  c                   P    e Zd ZdZdhZdZ	 	 	 	 ddeded	edz  d
ede	dede
fdZdS )r3  z)Dataset for multimodal conversation data.zlmms-lab/LLaVA-OneVision-DataTNFr\   r]   r^   r   r}  r_   r`   r8   c                    | j                             d           }g }	d}
|d u }|D ] }t          |	          |k    r n	|d         }|d         d         |d         d         }} ||          j        } ||          j        }t          |          }t          |          }|r|n|}t	          |t
                    r|dk    sJ |rt          ||          sd|v rt          |d                   nd }|r|                     ||          }|		                    t          |||||t          |
          z                        |
dz  }
"|                     |	|||           |	S )Nc                 4    t          | d                   dk    S r  r  r  s    r2   r  z6MultiModalConversationDataset.sample.<locals>.<lambda>m	  r  r1   r   r  r  rT   r   r  r  r  s                        r2   rb   z$MultiModalConversationDataset.sampleb	  r  r1   r  r  r0   r1   r2   r3  r3  Z	  s        33 	( M "&',!##1  1  1  1  $J	1 
 !%1  1  1  
1  1  1  1  1  1 r1   r3  c                   \    e Zd ZdZdZd d dZdZ	 	 	 	 dd
edededz  de	de
de	defdZdS )r-  z
    Vision Arena Dataset.
    r   c                 6    | d         d         d         d         S )Nconversationr   rF   r0   r  s    r2   r  zVisionArenaDataset.<lambda>	  s    >1B11Ea1H1S r1   c                 6    | d         d         d         d         S )Nrf  r   rF   r0   r  s    r2   r  zVisionArenaDataset.<lambda>	  s    '
1a8H8S r1   )zlmarena-ai/VisionArena-Chatz"lmarena-ai/vision-arena-bench-v0.1TNFr\   r]   r^   r   r}  r_   r`   r8   c                 B   ||n| j         }g }t          | j                  D ]\  }	}
t          |          |k    r n| j                            | j                  }|t          d| j                    ||
          }t          |
d         d                   }t           ||          j	                  }|r| 
                    ||          }|                    t          |||||t          |	          z                        |                     ||||           |S )Nr  imagesr   r  )r  r]  r;   rl   r  r  r  rq   r   r  rK   rI   r    r+   rv   r<   r]   r^   r   r}  r_   r`   r=   r_  rt   r`  	parser_fnr!   r?   r"   s                  r2   rb   zVisionArenaDataset.sample	  sZ    $.#9ZZt?V
 ++ 	 	GAt#$$44488FFI  !Ldl!L!LMMMYt__F&tH~a'899JYYv..899J% W BB6:VV##!)(2%/03q669      	&&l,=}	
 	
 	
  r1   r  )r'   r(   r)   r*   r  r  rz   r   r.   r{   r+   r,   rb   r0   r1   r2   r-  r-  	  s          'S'S.S.S  M "&',!##&  &  &  &  $J	& 
 !%&  &  &  
&  &  &  &  &  & r1   r-  c                   T    e Zd ZdZdZdd iZ	 	 	 	 dded	ed
edz  dede	dede
fdZdS )r0  zI
    MMVU Dataset.
    https://huggingface.co/datasets/yale-nlp/MMVU
    r   zyale-nlp/MMVUc                     | d         dz   d                     d | d                                         D                       z   S )Nquestion c              3   *   K   | ]\  }}| d | V  dS ).Nr0   rW  s      r2   r   z'MMVUDataset.<lambda>.<locals>.<genexpr>	  s0      @@41aqJJ1JJ@@@@@@r1   r  )rr  r^  r  s    r2   r  zMMVUDataset.<lambda>	  sF    1Z=
$88@@1Y<+=+=+?+?@@@@@$B r1   NFr\   r]   r^   r   r}  r_   r`   r8   c                 6   ||n| j         }g }t          | j                  D ]\  }	}
t          |          |k    r n| j                            | j                  }|t          d| j                    ||
          }t          |
d                   }t           ||          j	                  }|r| 
                    ||          }|                    t          |||||t          |	          z                        |                     ||||           |S )Nr  r   r  )r  r]  r;   rl   r  r  r  rq   r   r  rK   rI   r    r+   rv   r  s                  r2   rb   zMMVUDataset.sample	  sU    $.#9ZZt?V
 ++ 	 	GAt#$$44488FFI  !Ldl!L!LMMMYt__F&tG}55JYYv..899J% W BB6:VV##!)(2%/03q669      	&&l,=}	
 	
 	
  r1   r  r'   r(   r)   r*   r  r  r   r.   r{   r+   r,   rb   r0   r1   r2   r0  r0  	  s         
  B B "&',!##&  &  &  &  $J	& 
 !%&  &  &  
&  &  &  &  &  & r1   r0  c                   ~    e Zd ZdZdZdhZ	 	 	 	 	 ddeded	edz  d
edede	dede
e         fdZdedee	         fdZdS )r1  a  
    InstructCoder Dataset.
    https://huggingface.co/datasets/likaixin/InstructCoder

    InstructCoder is the dataset designed for general code editing.  It consists
    of 114,239 instruction-input-output triplets, and covers multiple distinct
    code editing scenario.
    r  zlikaixin/InstructCoderNFr\   r]   r^   r   r}  r  r_   r`   r8   c                    ||n| j         }g }	t          |                     |                    D ]v\  }
}|s|                    d|dgdd          }t	           ||          j                  }|	                    t          ||||t          |
          z                        w| 	                    |	|||           |	S )N)r  rC   rD   TFrY  r   )
r  r]  sample_promptsr^  rl   r  rI   r    r+   rv   )r<   r]   r^   r   r}  r  r_   r`   r=   r_  rt   r!   r"   s                r2   rb   zInstructCoderDataset.sample
  s    $.#9ZZt?V
"4#6#6#6#F#FGG 	 	IAv% "66$889*." 7   YYv..899J##!)(203q669	      	&&l,=}	
 	
 	
  r1   r  c              #   t   K   | j                             |          D ]}|d          d|d          d}|V  d S )Ninputz

instructionz6 Just output the code, do not include any explanation.)r;   take)r<   r  r`  r!   s       r2   r  z#InstructCoderDataset.sample_prompts>
  sf      INN1%% 	 	D= < <d=&9 < < <  LLLL	 	r1   NFFr\   F)r'   r(   r)   r*   r  r  r   r.   r{   r+   r,   r    rb   r   r  r0   r1   r2   r1  r1  
  s            "&',#(!##"  "  "  "  $J	" 
 !%"  !"  "  "  
m	"  "  "  " H       r1   r1  c                   V    e Zd ZdZdZdhZ	 	 	 	 	 ddeded	edz  d
edede	dede
fdZdS )r2  a'  
    MT-Bench Dataset.
    https://huggingface.co/datasets/philschmid/mt-bench

    We create a single turn dataset for MT-Bench.
    This is similar to Spec decoding benchmark setup in vLLM
    https://github.com/vllm-project/vllm/blob/9d98ab5ec/examples/offline_inference/eagle.py#L14-L18
    r!  zphilschmid/mt-benchNFr\   r]   r^   r   r}  r  r_   r`   r8   c                    ||n| j         }g }	t          | j                  D ]\  }
}t          |	          |k    r n|d         d         }|s|                    d|dgdd          }t           ||          j                  }|	                    t          ||||t          |
          z                        | 	                    |	|||           |	S )	Nrf  r   rC   rD   TFrY  r   
r  r]  r;   rl   r^  r  rI   r    r+   rv   )r<   r]   r^   r   r}  r  r_   r`   r=   r_  rt   r`  r!   r"   s                 r2   rb   zMTBenchDataset.sample[
  s%    $.#9ZZt?V
 ++ 	 	GAt#$$44']1%F & "66$889*." 7   YYv..899J##!)(203q669	      	&&l,=}	
 	
 	
  r1   r  r  r0   r1   r2   r2  r2  L
  s           "&',#(!##'  '  '  '  $J	' 
 !%'  !'  '  '  
'  '  '  '  '  ' r1   r2  c                   ^    e Zd ZdZdZddhZ	 	 	 	 	 	 dd
edededz  dede	dede
de
defdZdS )r8  z
    Blazedit Dataset.
    https://github.com/ise-uiuc/blazedit

    5k char version: vdaita/edit_5k_char
    10k char version: vdaita/edit_10k_char
    i  zvdaita/edit_5k_charzvdaita/edit_10k_charNFr\   r   r   r]   r^   r   r  r_   r`   r  r  r8   c	                    ||n| j         }g }
t          | j                  D ]\  }}t          |
          |k    r n|d         }|d         }|d         }||k     s||k    r?d| d| d}|s|                    d|dgd	d
          }t           ||          j                  }|
                    t          ||||t          |          z                        | 	                    |
|||           |
S )Ncodechange_requestnorm_distanceziGiven a code file, please apply the change requests and generate the new file.

Original file:
```python
z
```

Change request:
zD

Please generate the new code file in the "New file" section below.rC   rD   TFrY  r   r  )r<   r]   r^   r   r  r_   r`   r  r  r=   r_  rt   r`  r  r  r  r!   r"   s                     r2   rb   zBlazeditDataset.sample
  s    $.#9ZZt?V
 ++ *	 *	GAt#$$44<D!"23N 1M |++}|/K/K
F 	
F 
F 
F 
F 
FF & "66$889*." 7   YYv..899J##!)(203q669	      	&&l,=}	
 	
 	
  r1   )NFr\   Fr   r   )r'   r(   r)   r*   r  r  r   r.   r{   r+   r   r,   rb   r0   r1   r2   r8  r8  
  s           "&#(!##!!>  >  >  >  $J	> 
 !>  >  >  >  >  
>  >  >  >  >  > r1   r8  c                   H    e Zd ZdZh dZ	 	 	 ddedededz  d	ed
ede	fdZ
dS )r5  zO
    Dataset class for processing a AIMO dataset with reasoning questions.
    >   AI-MO/NuminaMath-1.5AI-MO/NuminaMath-CoTAI-MO/aimo-validation-aimeNr\   Fr]   r^   r   r_   r`   r8   c                 
   g }d}|d u }	| j         D ]}
t          |          |k    r n|
d         |
d         }} ||          j        } ||          j        }t          |          }t          |          }|	r|n|}t          |t                    r|dk    sJ |	rt          ||dd          s|                    t          |||d |t          |          z                        |dz  }| 	                    ||||           |S )	Nr   problemsolutionr~   i }  )r   r   r  rT   )
r;   rl   r  rG   r.   r   rI   r    r+   rv   )r<   r]   r^   r   r_   r`   r=   r_  r  r  r`  r!   r  r  r  r"   r  s                    r2   rb   zAIMODataset.sample
  sd    #t+I 	 	D#$$44!%i$z2BJF"6**4J&Yz22<NZJ 00N+9IzJj#..A:>>>A &7N4u' ' '  ##!)(2%)03s88;     1HCC&&l,=}	
 	
 	
  r1   Nr\   F)r'   r(   r)   r*   r  r   r.   r+   r{   r,   rb   r0   r1   r2   r5  r5  
  s            "&!##)  )  )  )  $J	) 
 )  )  
)  )  )  )  )  ) r1   r5  a*  ### Instruction:
You are a code completion assistant and your task is to analyze user edits and then rewrite an excerpt that the user provides, suggesting the appropriate edits within the excerpt, taking into account the cursor location.

### User Edits:

{}

### User Excerpt:

{}

### Response:

<|editable_region_start|>rb   original_start_markerc                     | d         }| d         }| d         }t                               ||          }|                    |          }||d         }|}||dS )a4  Format the zeta prompt for the Next Edit Prediction (NEP) dataset.

    This function formats examples from the NEP dataset
    into prompts and expected outputs. It could be
    further extended to support more NEP datasets.

    Args:
        sample: The dataset sample containing events,
            inputs, and outputs.
        original_start_marker: The marker indicating the
            start of the editable region. Defaults to
            "<|editable_region_start|>".

    Returns:
        A dictionary with the formatted prompts and expected outputs.
    eventsr  outputN)r!   expected_output)zeta_promptr   find)	rb   r  r  r  r  r!   output_start_indexoutput_focused_regionr  s	            r2   _format_zeta_promptr  /  sp    & HF7OEHF..F  %:;;"#5#6#67+OAAAr1   c            	       >    e Zd ZdZdhZdeiZ	 	 ddedede	de
fd	Zd
S )r6  zF
    Dataset class for processing a Next Edit Prediction dataset.
    zzed-industries/zetar\   Fr]   r^   r_   r`   c                    | j                             | j                  }|t          d| j                   g }t	          | j                  D ]\  }}	 ||	          }	|                    t          |	d         t           ||	d                   j	                  t           ||	d                   j	                  |t          |          z                        t          |          |k    r n|                     ||||           |S )Nr  r!   r  r   )MAPPING_PROMPT_FUNCSr  r  rq   r]  r;   rI   r    rl   r  r+   rv   )
r<   r]   r^   r_   r`   r=   formatting_prompt_funcr  rt   rb   s
             r2   rb   z NextEditPredictionDataset.sample\  s4    "&!:!>!>t|!L!L!)H$,HHIII"49-- 	 	IAv++F33FNN!(+"99VH-=#>#>#HII(+!	&):";<<F) )  13q669  	 	 	 7|||++ ,&&\#4m	
 	
 	
 r1   Nry   )r'   r(   r)   r*   r  r  r  r   r.   r+   r{   rb   r0   r1   r2   r6  r6  P  s         
 	 	2 "$#    	
      r1   r6  c                   d    e Zd ZU dZh dZdZdZdZdZe	e
d<   	 	 	 dd
edededz  dede	defdZdS )r7  a  
    Dataset class for processing a ASR dataset for transcription.
    Tested on the following set:

    +----------------+----------------------------------------+--------------------------+-----------------------------+
    | Dataset        | Domain                                 | Speaking Style           | hf-subset                   |
    +----------------+----------------------------------------+--------------------------+-----------------------------+
    | TED-LIUM       | TED talks                              | Oratory                  | release1, release2, release3|
    |                |                                        |                          | release3-speaker-adaptation |
    | VoxPopuli      | European Parliament                    | Oratory                  | en, de, it, fr,  ...        |
    | LibriSpeech    | Audiobook                              | Narrated                 | "LIUM/tedlium"              |
    | GigaSpeech     | Audiobook, podcast, YouTube            | Narrated, spontaneous    | xs, s, m, l, xl, dev, test  |
    | SPGISpeech     | Financial meetings                     | Oratory, spontaneous     | S, M, L, dev, test          |
    | AMI            | Meetings                               | Spontaneous              | ihm, sdm                    |
    +----------------+----------------------------------------+--------------------------+-----------------------------+

    >   LIUM/tedliumedinburghcstr/amikensho/spgispeechfacebook/voxpopulispeechcolab/gigaspeechopenslr/librispeech_asrr   Tz9<|startoftranscript|><|en|><|transcribe|><|notimestamps|>skip_long_audiosNr\   Fr]   r^   r   r_   r`   r8   c                 (   ||n| j         }t          j        }t           ||          j                  }g }	d}
d}| j        D ]}t          |	          |k    r n|d         }|d         |d         }}t          j        ||          }| j        r|dk    r|dz  }Xd||fi}|		                    t          |||||t          |
          z                        |
dz  }
|rt                              d	|           |                     |	|||           |	S )
Nr   audior   sampling_rate)ysrr3  rT   r  z_%d samples discarded from dataset due to their length being greater than what Whisper supports.)r  r7  TRANSCRIPTION_PREAMBLErl   r  r;   r   get_durationr  rI   r    r+   rj   r   rv   )r<   r]   r^   r   r_   r`   r=   r!   r"   r_  r  skippedr`  r  r  r  
duration_sr?   s                     r2   rb   zASRDataset.sample  ss    $.#9ZZt?V
26**455
I 	 	D#$$44ME'NE/$:rA -b999J$ b1!Ar7+J##!)(2%/03s88;     1HCC 	NN* 	   	&&l,=}	
 	
 	
  r1   r  )r'   r(   r)   r*   r  r  rz   r  r  r{   r-   r   r.   r+   r,   rb   r0   r1   r2   r7  r7    s          $   M Y!d!!! "&!##/  /  /  /  $J	/ 
 /  /  
/  /  /  /  /  / r1   r7  c                   T    e Zd ZdZddhZ	 	 	 ddeded	edz  d
edede	e
         fdZdS )r;  a2  
    MLPerf Inference Dataset.

    Dataset on HF:
    https://huggingface.co/datasets/mgoin/mlperf-inference-llama2-data
    https://huggingface.co/datasets/mgoin/mlperf-inference-llama3.1-data

    Each record contains:
      - "system_prompt": system role instruction.
      - "question": user question.
      - "output": reference answer.

    We combine the system prompt and question into a chat-formatted prompt
    (using the tokenizer's chat template) and set the expected output length to
    the tokenized length of the provided reference answer.
    z"mgoin/mlperf-inference-llama2-dataz$mgoin/mlperf-inference-llama3.1-dataNr\   Fr]   r^   r   r_   r`   r8   c                    |d u }g }d}	| j         D ]}
t          |          |k    r n|
d         }|
d         }|
d         }d|dd|dg}|                    |dd	
          }t           ||          j                  }t           ||d	          j                  }|r|n|}t	          ||          s|                    t          ||||t          |	          z                        |	dz  }	|                     ||||           |S )Nr   system_promptr  r  systemrD   rC   TFrY  r   r   rT   )	r;   rl   r^  r  r   rI   r    r+   rv   )r<   r]   r^   r   r_   r`   r=   r  r_  r  r`  r  r  reference_answermessagesr~  r"   ref_out_lenr#   s                      r2   rb   zMLPerfDataset.sample  s    $t+02I $	 $	D#$$44 1MJ'H#H~ "m<<H55H  )<<u  =     YY'788BCCJ 	*uEEEO K 2@"O++Z %Z1DEE ##+)(;03s88;	     1HCC&&l,=}	
 	
 	
  r1   r  )r'   r(   r)   r*   r  r   r.   r+   r{   r,   r    rb   r0   r1   r2   r;  r;    s         $ 	-. "&!##7  7  7  7  $J	7 
 7  7  
m	7  7  7  7  7  7 r1   r;  c                   z     e Zd ZdZdZdZdZ	 	 d fdZeeeeddfd	ed
e	de	de	de	de	de
dedee         fdZ xZS )r  r!  r   r   r8   Nc                      t                      j        di | t          j        | j                   t
          j                            | j                   d S r   )r   r>   rW   rm   r6   r   r   s     r2   r>   z&PrefixRepetitionRandomDataset.__init__9  sR     	""6"""D$%%%
	t'(((((r1   r\   Fr]   r^   r   r  r  r   r_   r`   c	           	         j         ||z  }
|
dk    rt          d| d| d          dt          dt          t                   ffd}g }d}t	          |          D ]} ||          \  }}||z  }t	          |
          D ]c} ||          \  }}||z  }||z   }                    |          }t          |          }|                    t          |||                     d|dk    r3|dk    rd	nd
}t          
                    dt          |          |           t          | dd          st          j        |           |S )Nr   znum_requests (z1) must be greater than or equal to num_prefixes ()target_lengthr8   c                     t           j                            d|                                           }t	          || d          \  }}}||fS )zOGenerate tokens that decode and re-encode to exactly
            target_length.r   r   F)r]   r   r   r   )r   rW   rX   r   r   )r  rp  r  adjusted_tokensr   r]   r   s        r2   _generate_exact_length_tokenszKPrefixRepetitionRandomDataset.sample.<locals>._generate_exact_length_tokensU  sa     Y&&q*=&IIPPRRF1P#%!.#(	2 2 2.A #N22r1   )r!   r"   r#   r   r   r   r7   F)r   rq   r.   r,   rn   r   rl   rI   r    rj   r   r   r  rW   r  )r<   r]   r^   r   r  r  r   r_   r`   r=   prompts_per_prefixr  rc   r   r  prefix_tokensprefix_mismatchsuffix_tokenssuffix_mismatchcombined_tokensr!   r"   r   r   s    `                     @r2   rb   z$PrefixRepetitionRandomDataset.sampleA  s    )
)\9""4 4 4$04 4 4  
	3 	3c 	3 	3 	3 	3 	3 	3 	3  |$$ 	 	A-J-J:-V-V*M? O3 -..  1N1N2 2. %7$"/-"?"))/:: 11
!%#-,6        1$$1A55667DNN ())   t.66 	%N8$$$r1   rx   )r'   r(   r)   r   DEFAULT_SUFFIX_LENDEFAULT_NUM_PREFIXESr  r>   r   r.   r+   r{   r,   r    rb   r  r  s   @r2   r  r  1  s         ) 
) ) ) ) ) ) -,0,!##D D D D 	D
 D D D D D 
m	D D D D D D D Dr1   r  c                   `    e Zd ZdZdZdhZdZ	 	 	 	 dded	ed
edz  de	de
de	dee         fdZdS )r<  z
    Lin-Chen/MMStar: https://huggingface.co/datasets/Lin-Chen/MMStar
    refer to: https://github.com/sgl-project/SpecForge/pull/106
    r   zLin-Chen/MMStarTNFr\   r]   r^   r   r}  r_   r`   r8   c                 F   ||n| j         }g }t          | j                  D ]\  }	}
t          |          |k    r n|
                    dd          }|                    dd          d                                         }t          |
d                   }t           ||          j                  }|r| 	                    ||          }d }n|}|}|
                    t          |||||t          |	          z                        |                     ||||           |S )Nr  r\   zOptions:rT   r   r   r  )r  r]  r;   rl   r  r  stripr   r  rK   rI   r    r+   rv   )r<   r]   r^   r   r}  r_   r`   r=   r_  r  r`  full_qquestion_textr?   r"   r!   mm_for_requests                    r2   rb   zMMStarDataset.sample  sg    $.#9ZZt?V
02"49-- $	 $	IC#$$44 ((:r22F"LLQ77:@@BBM 'tG}55J YY}55?@@J% , BB!:  "& '!+##!)(2%303s88;      	&&l,=}	
 	
 	
  r1   r  )r'   r(   r)   r*   r  r  rz   r   r.   r{   r+   r,   r    rb   r0   r1   r2   r<  r<    s         
 01M "&',!##7  7  7  7  $J	7 
 !%7  7  7  
m	7  7  7  7  7  7 r1   r<  )r|   r}   r~   F)r   FN)r  )lr*   argparser  r   r   r  loggingr   rW   abcr   r   collections.abcr   r   r   
contextlibr   rw  r	   dataclassesr
   	functoolsr   r   tempfiler   typingr   r   numpyr   PILr   typing_extensionsr   vllm.lora.requestr   vllm.lora.utilsr   vllm.multimodalr   vllm.multimodal.imager   vllm.tokenizersr   vllm.utils.import_utilsr   r   r   ImportErrorplaceholder_attrr   rP  r   vllm.utils.argparse_utilsr   r   	getLoggerr'   rj   r    r4   r.   r{   r   r+   rY   r   r/   r-   r   r   r,   	Generatorr  r   r   r  r  r  Actionr  r  _ArgumentGroupr  r  rH  r$  r  r&  r  r=  r4  r3  r-  r0  r1  r2  r8  r5  r  r  r6  r7  r;  r  r<  r0   r1   r2   <module>r'     sf
  
 
 
  



  				     # # # # # # # # 7 7 7 7 7 7 7 7 7 7             ! ! ! ! ! !             ' ' ' ' ' '                   ( ( ( ( ( ( ) ) ) ) ) ) 5 5 5 5 5 5 . . . . . . 4 4 4 4 4 4 ) ) ) ) ) ) 5 5 5 5 5 5=%%%%%%% = = =  ,,H,,^<<LLL=% % % %		8	$	$BBB%+NNNN + + +	**GGG+B@@@@@@@ B B BAAAAAAAAB 
	8	$	$ 
" 
" 
" 
" 
" 
" 
" 
"$c c c c cs c c c\ &+   	
   $ 
   6 0 0 0 0 0 0
 24 d3-. 3 3 3+ +c!2 + + + +\" "c!2 " " " "R $&*42 4242I42 42 	42
 42 
	t	#42 3S	>42 42 42 42xc7 c7 c7 c7 c7$ c7 c7 c7Vh h h h h h h h`_ _ _ _ _m _ _ _NS S S S S& S S Sl    8?   ({5 { { { {|<+h.EE<	< < < <~c+h.EEc	c c c cLV V43F V V V V|v  v  v  v  v $ v  v  v |%( %( %( %( %( %( %( %(Z J P P P P P$ P P PpE E E E E& E E EVA A A A A) A A AN9  9  9  9  9 , 9  9  9 x9  9  9  9  9 $6 9  9  9 B2  2  2  2  2 + 2  2  2 j3  3  3  3  3 $ 3  3  3 v9 9 9 9 9- 9 9 9B6  6  6  6  6 ' 6  6  6 |Q  Q  Q  Q  Q ( Q  Q  Q r4  4  4  4  4 $ 4  4  4 x" 0KB BB),B	B B B BB) ) ) ) ) 2 ) ) )bR  R  R  R  R # R  R  R tN  N  N  N  N & N  N  N lT T T T T$4 T T TxA  A  A  A  A & A  A  A  A  A sH   B %CC
C C"!C"&C+ +C>=C>D	 	DD