o
    ۾i                  
   @   s   d dl mZ z
d dlZd dlZW n" ey2 Z zdeev r dndZede de dedZ[ww de	d	ej
fd
dZejdd Zdej
dej
d	dfddZdedej
d	dfd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                 C   s   t jd| d d fdt jdS )N          )dtype)npfullint32)r    r   O/home/ubuntu/.local/lib/python3.10/site-packages/outlines_core/kernels/numpy.pyallocate_token_bitmask   s
   r   c                 C   s   |j d }d| }| j d |kr&tj | d d |d f< | d d d |f } | j \}}t|D ]4}t|D ]-}|||f }|d }	tdD ]}
|	|
 }||krQ n||
? d@ dkratj | ||f< qEq5q/d S )Nr   r
   r   )shaper   infrange)logitsmaskmask_lencutoffn_rowsn_colsimimvalbasebitjr   r   r   #_apply_token_bitmask_inplace_kernel   s&   

r"   r   r   c                 C   s   | j dkr| ntj| dd} |j dkr|ntj|dd}|jtjkr+td|j d|j dkr9td|j  d| j dkrGtd	| j  d|jd | jd krbt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   r   r   r   apply_token_bitmask_inplace.   s&   

r,   guidec                 C   s   |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
 s6t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)r-   r   r   r   r   fill_next_token_bitmaskY   s"   

r6   )outlines_corer   r   r   r   ImportErrorestrmissing_depintndarrayr   njitr"   r,   r6   r   r   r   r   <module>   s$    
+