
    .`iK                      L   d dl mZ d dlZd dlZd dlmZmZ d dlm	Z	 d dl
mZmZ d dlmZ d dlmZmZ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   e	e!          Z" G d de          Z# G d de          Z$ e d           G d de                      Z%dS )    )SetN)ModelConfig
VllmConfig)init_logger)DispatchPoolerPoolingParamsUpdate)PoolerNormalize)EmbeddingPoolerHeadSequencePoolerSequencePoolingMethodSequencePoolingMethodOutputget_seq_pooling_method)pooler_for_token_embed)LlamaForCausalLM)PoolingTask)cached_tokenizer_from_config)PoolingMetadata   )default_pooling_typec                        e Zd ZdZdef fdZ	 	 ddej        dej        ded	edz  d
ef
dZ	dej        d
efdZ
d
ee         fdZded
efdZdej        ded
efdZ xZS )GritLMMeanPoolz8As `MeanPool`, but only includes non-instruction tokens.model_configc                 \    t                                                       | _        t           j                  fddD              _        dt
          t                   dt          j        f fd} |g d           _	         |g d           _
         |g d           _        d S )	Nc                 J    i | ]}|                     |g          d           S )r   )convert_tokens_to_ids).0tok	tokenizers     u/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/vllm/model_executor/models/gritlm.py
<dictcomp>z+GritLMMeanPool.__init__.<locals>.<dictcomp>/   s@     
 
 
 00#77:
 
 
    )<s>   ▁<<|embed><0x0A>usertokensreturnc                 D    t          j        fd| D                       S )Nc                 *    g | ]}j         |         S  )	token_ids)r   tokenselfs     r   
<listcomp>zBGritLMMeanPool.__init__.<locals>.tokens_to_ids.<locals>.<listcomp>5   s     GGGuT^E2GGGr!   )nparray)r*   r1   s    r   tokens_to_idsz.GritLMMeanPool.__init__.<locals>.tokens_to_ids4   s(    8GGGGGGGHHHr!   )r#   r%   r)   r%   r'   r(   )r(   r$   r%   r&   r%   r'   r(   )r#   r%   r&   r%   r'   r(   )super__init__r   r   r/   liststrr3   ndarrayuser_pattern_idsembed_newline_pattern_idsembed_pattern_ids)r1   r   r5   r   	__class__s   `  @r   r7   zGritLMMeanPool.__init__$   s    (01BCC	
 
 
 
P
 
 

	I$s) 	I
 	I 	I 	I 	I 	I 	I !..W.W.W X X)6===*
 *
& "//Y/Y/Y!Z!Zr!   r   Narrtarget	start_idxend_idxr+   c                 z   |dk     rt          d          t          |          dk    st          |          dk    rt          d          t          |          }t          |          }||}t          |t          |||z
  dz                       D ])}||||z            |k                                    r|c S *dS )a  
        Find the first occurrence of `target` in `arr` starting from
        `start_idx`.

        Args:
            arr: The array to search within.
            target: The consecutive subsequence to find.
            start_idx: The starting index to search from (inclusive).
            end_idx: The ending index to search from (exclusive).

        Returns:
            The index of the first occurrence of `target` in `arr`.
        r   z `start_idx` must be non-negativez#Empty `arr` or `target` not allowedNr   )
ValueErrorlenrangeminall)r1   r?   r@   rA   rB   arr_len
target_lenis           r   _find_arrayzGritLMMeanPool._find_array=   s    ( q==?@@@s88q==CKK1,,BCCCc(([[
?Gy#gw/Ca/G"H"HII 	 	AAJ&'616688  rr!   prompt_token_idsc                 j   d}|d         | j         d         k    rt                              d           |S | j        }|                     || j        dd          dk    r| j        }|                     ||d          }|dk    r|t          |          z   }nt                              d	           d}|S )
aF  
        Get the length of the instruction in the prompt.

        We do a pattern matching to find the instruction in the prompt,
        and then return the length of the instruction.

        The pattern matching is done using integers instead of strings
        because the prompt is given as a list of token IDs.
        r   r"   zlBOS token not found in prompt, thus using empty string for instruction. GritLM requires BOS token in prompt.r      )rA   rB   )rA   rD   zQuery instruction not found in prompt, thus using BOS token as instruction instead. GritLM requires query instruction in prompt.)r/   loggerwarningr=   rM   r;   r<   rF   )r1   rN   instruction_lenr=   found_embed_pattern_idxs        r   _get_instruction_lenz#GritLMMeanPool._get_instruction_lenb   s      A$."777NN7  
 #" !2 $"71a     
 !% > #'"2"2/1 #3 #
 #
 #b((5<M8N8NNOONN?  
  Or!   c                     dhS )Nr&   r.   )r1   s    r   get_supported_tasksz"GritLMMeanPool.get_supported_tasks   s
    yr!   taskc                 "    t          d          S )NT)requires_token_ids)r   )r1   rX   s     r   get_pooling_updatesz"GritLMMeanPool.get_pooling_updates   s    "d;;;;r!   hidden_statespooling_metadatac                 x    |j         }t          j         fd|                                D             d          }d}t	          t          j                             }t          ||          D ]L\  }}|                    |||z   ||z                                dt          j	                             ||z  }M|S )Nc                     g | ];}                     |                                                                          <S r.   )rU   cpunumpy)r   r/   r1   s     r   r2   z*GritLMMeanPool.forward.<locals>.<listcomp>   sK        )))--//*?*?*A*ABB  r!   r`   )devicer   )dimdtype)
prompt_lenstorchtensorget_prompt_token_idsr8   Tensorzipappendmeanfloat32)	r1   r\   r]   re   
instr_lensoffsetpooled_data
prompt_len	instr_lens	   `        r   forwardzGritLMMeanPool.forward   s    
 '2\   !1!F!F!H!H   
 
 

 5<(**%(j%A%A 	! 	!!J	fy06J3FFGLL M    
 j FFr!   )r   N)__name__
__module____qualname____doc__r   r7   r3   r:   intrM   rU   r   r   rW   r   r[   rf   ri   r   r   rs   __classcell__r>   s   @r   r   r   !   sE       BB[[ [ [ [ [ [ [: "# #Z# 
# 	#
 t# 
# # # #J/RZ /C / / / /bS%5    < <8K < < < <| * 
%	       r!   r   c                   $     e Zd Zdef fdZ xZS )GritLMPoolerr   c                     |j         }|J t                                          |j        dk    rt	          |          nt          |j                  t          |j        t                                           d S )NMEAN)
head_dtype
activation)poolinghead)	pooler_configr6   r7   seq_pooling_typer   r   r
   r   r	   )r1   r   r   r>   s      r   r7   zGritLMPooler.__init__   s    $2((( !1V;; |,,,+M,JKK$'2*,,   	 
	
 
	
 
	
 
	
 
	
r!   )rt   ru   rv   r   r7   ry   rz   s   @r   r|   r|      sD        
[ 
 
 
 
 
 
 
 
 
 
r!   r|   r~   )r   c                   8     e Zd ZdZdZ	 d	dededdf fdZ xZS )
GritLMaq  This class implements the embedding model for parasail-ai/GritLM-7B-vllm.

    The class inherits from LlamaForCausalLM and provides a custom pooling
    layer.

    The main difference between the pooling layer in GritLM and the one in
    LlamaForCausalLM is that GritLM ignores the query instruction in the prompt
    when pooling the hidden states.

    Embedding prompts should be in the following format:
    - With instruction: "<|user|>
INSTRUCTION
<|embed|>
PROMPT".
    - Without instruction: "<|embed|>
PROMPT".

    Generation prompts should be in the following format:
    - "<|user|>
PROMPT
<|assistant|>
"
    T vllm_configprefixr+   Nc                 8   |j         j        dk    r&|j         j        }d|_        d |j        _        d |_         t                      j        d||d| |j         j        }|8t          t          |          t          |j                   d          | _        d S d S )Nr   F)r   r   )token_embedr&   r.   )r   runner_type	hf_config	is_causalcache_configsliding_windowr6   r7   r   r   r   r|   pooler)r1   r   r   kwargsr   r   r>   s         r   r7   zGritLM.__init__   s     #/9<<#0:I"'I6:K$3'+I$J[JJ6JJJ#0>$(#9-#H#H)+*BCC  DKKK %$r!   )r   )	rt   ru   rv   rw   is_pooling_modelr   r9   r7   ry   rz   s   @r   r   r      su         " 
   
 
         r!   r   )&collections.abcr   ra   r3   rf   vllm.configr   r   vllm.loggerr   !vllm.model_executor.layers.poolerr   r   -vllm.model_executor.layers.pooler.activationsr	   )vllm.model_executor.layers.pooler.seqwiser
   r   r   r   r   )vllm.model_executor.layers.pooler.tokwiser    vllm.model_executor.models.llamar   
vllm.tasksr   vllm.tokenizersr   vllm.v1.pool.metadatar   interfaces_baser   rt   rQ   r   r|   r   r.   r!   r   <module>r      s               / / / / / / / / # # # # # #        J I I I I I              M L L L L L = = = = = = " " " " " " 8 8 8 8 8 8 1 1 1 1 1 1 1 1 1 1 1 1	X		P P P P P* P P Pf
 
 
 
 
> 
 
 
$ v...+ + + + + + + /.+ + +r!   