
    `i                        d dl mZ 	 d dlZd dlZn1# e$ r)Zd ee          v rdndZ ede de d          edZ[ww xY wde	d	ej
        fd
Zej        d             Zdej
        dej
        d	dfdZdedej
        d	dfdZdS )    )GuideNnumbanumpyz6To use the kernels in `outlines_core.kernels.numpy`, `z:` must be installed. You can install it with `pip install `
vocab_sizereturnc                 T    t          j        d| dz   dz  fdt           j                  S )N          )dtype)npfullint32)r   s    o/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/outlines_core/kernels/numpy.pyallocate_token_bitmaskr      s4    7	
Z"_#$
h       c                    |j         d         }d|z  }| j         d         |k    r$t          j         | d d |d f<   | d d d |f         } | j         \  }}t          |          D ]`}t          |          D ]N}|||f         }|dz  }	t          d          D ]-}
|	|
z   }||k    r n||
z	  dz  dk    rt          j         | ||f<   .Oad S )Nr
   r   r   )shaper   infrange)logitsmaskmask_lencutoffn_rowsn_colsimimvalbasebitjs               r   #_apply_token_bitmask_inplace_kernelr%      s   z!}H(]F|A fWqqq&''z7F7
#\NFF6]] + +// 
	+ 
	+B2;D7DRyy + +3J;;ES[A%!++$&F7F1a4L
	++ +r   r   r   c                 (   | j         dk    r| nt          j        | d          } |j         dk    r|nt          j        |d          }|j        t          j        k    rt          d|j         d          |j         dk    rt          d|j          d          | j         dk    rt          d	| j          d          |j        d         | j        d         k    r,t          d
|j        d          d| j        d          d          t          | |           dS )ae  
    Apply a logits bitmask inplace, setting the probability of invalid tokens
    to -infinity.

    Arguments:
        logits (np.ndarray): The logits tensor.

        mask (np.ndarray): The token bitmask representing the validity of each
          token in the logits tensor.

    Raises:
        ValueError: If any of the following conditions are not met:
            - `mask.dtype` is not `np.int32`
            - `mask` is not a 2D array
            - `logits` is not a 2D array
            - `mask.shape`shape does not match `logits.shape`

    Returns:
        None: Modifies the mask array in place.
    r
   r   )axis2Invalid mask dtype: Expected `np.int32`, but got ``.   6Invalid mask dimensions: Expected a 2D array, but got D.z8Invalid logits dimensions: Expected a 2D array, but got z.Invalid batch size: Expected `mask.shape[0]` (z) to match `logits.shape[0]` (z).N)ndimr   expand_dimsr   r   
ValueErrorr   r%   )r   r   s     r   apply_token_bitmask_inplacer0   .   sD   * {a''VVR^F-K-K-KF9>>44r~d'C'C'CDzRXOOOO
 
 	
 
aRTYRRR
 
 	
 
		Vv{VVV
 
 	
 
A&,q/	)	)}TZ]}}jpjvwxjy}}}
 
 	
 (55555r   guidec                    |j         t          j        k    rt          d|j          d          |j        dk    rt          d|j         d          |j        d         dk    rt          d|j         d	          |j        d
         st          d          |                     |j        j	        |j
        |j                  S )am  
    Writes a bitmask to represent the tokens permissible by the current state of the `guide`.
    Each bit in the bitmask corresponds to a token ID, with a bit value of 1 indicating that
    the token is allowed and 0 indicating that it is disallowed. This function directly modifies
    the `mask` array in-place.

    Arguments:
        guide (Guide): An instance of the `Guide` class that provides the current guidance state.
        mask (np.ndarray): A 2D tensor of type `torch.int32` where the bitmask will be written.
                             The tensor must be contiguous, have a single batch dimension
                             (shape[0] == 1), and reside on the CPU.

    Raises:
        ValueError: If any of the following conditions are not met:
                    - `mask.dtype` is not `torch.int32`
                    - `mask` is not a 2D tensor
                    - `mask` does not have a single batch dimension (shape[0] != 1)
                    - `mask` is not contiguous in memory
                    - `mask` is not on the CPU device

    Returns:
        None: Modifies the `mask` array in-place.
    r(   r)   r*   r+   r,   r   r
   z_Invalid batch size: Batch mask writes are not supported. Expected shape[0] == 1, but got shape .C_CONTIGUOUSzJMask array must be contiguous in memory. Use `np.ascontiguousarray(mask)`.)r   r   r   r/   r-   r   flagswrite_mask_intoctypesdatasizeitemsize)r1   r   s     r   fill_next_token_bitmaskr;   Y   s    0 zRXOOOO
 
 	
 
aRTYRRR
 
 	
 
A!		{nrnx{{{
 
 	
 Z' 
X
 
 	
   !149dmLLLr   )outlines_corer   r   r   r   ImportErrorestrmissing_depintndarrayr   njitr%   r0   r;    r   r   <module>rE      s_        LLL   $A..''GK
+ 	W  	W  	W  IT  	W  	W  	W s rz     + + +0(6
 (6"* (6 (6 (6 (6 (6V)M5 )M
 )Mt )M )M )M )M )M )Ms    ?$:?