
    )`iN                        d Z ddlZddlmZ ddlZddlmZ ddlmZ ddl	m
Z
mZ ej        d             Z e
d	d
          dej        dedej        fd            Z ed	          dej        dedej        fd            Zeddej        dedej        fd            Ze	 ddej        dej        dedeej        ej        f         fd            ZdS )a3  
Copyright (c) 2024 by FlashInfer team.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

  http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
    N)Tuple   )flashinfer_api)gen_quantization_module)register_custom_opregister_fake_opc                  B    t                                                      S )N)r   build_and_load     k/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/flashinfer/quantization.pyget_quantization_moduler      s    "$$33555r   zflashinfer::packbitsr   )mutates_argsxbitorderreturnc                    | j         }|                     t          j                  } t          j        |                     d          dz   dz  t          j        |          }t                                          | ||           |S Nr         dtypedevice)	r   totorchboolemptysizeuint8r   packbits)r   r   r   ys       r   	_packbitsr"       sj    XF	UZAQVVAYY]q(FKKKA&&q(A666Hr   c                     t          j        |                     d          dz   dz  t           j        | j                  S r   )r   r   r   r   r   r   r   s     r   _fake_packbitsr%   )   s0    ;q		A!+5;qxPPPPr   bigc                 "    t          | |          S )a9  Pack the elements of a binary-valued array into bits in a uint8 array.

    The semantics of this function is the same as `numpy.packbits <https://numpy.org/doc/stable/reference/generated/numpy.packbits.html>`_.

    Parameters
    ----------
    x: torch.Tensor
        The 1D binary-valued array to pack.
    bitorder: str
        The bit-order ("bit"/"little") of the output. Default is "big".

    Returns
    -------
    y: torch.Tensor
        An uint8 packed array, shape ``((x.size(0) + 7) / 8),)``.

    Examples
    --------

    >>> import torch
    >>> from flashinfer import packbits
    >>> x = torch.tensor([1, 0, 1, 1, 0, 0, 1, 1], dtype=torch.bool, device="cuda")
    >>> x_packed = packbits(x)
    >>> list(map(bin, x_packed.tolist()))
    ['0b10110011']

    See Also
    --------
    segment_packbits
    )r"   r$   s     r   r    r    .   s    @ Q!!!r   indptrc                    |dd         |dd         z
  }|dz   dz  }t          j        t          |          |j        |j                  }t          j        |d          |dd<   |d                                         }| j        }|                    t           j                  }|                    t           j                  }t          j	        |t           j
        |          }t                                          | ||||           ||fS )av  Pack a batch of binary-valued segments into bits in a uint8 array.

    For each segment, the semantics of this function is the same as `numpy.packbits <https://numpy.org/doc/stable/reference/generated/numpy.packbits.html>`_.

    Parameters
    ----------
    x: torch.Tensor
        The 1D binary-valued array to pack, shape ``(indptr[-1],)``.
    indptr: torch.Tensor
        The index pointer of each segment in :attr:`x`, shape ``(batch_size + 1,)``.
        The i-th segment in :attr:`x` is ``x[indptr[i]:indptr[i+1]]``.
    bitorder: str
        The bit-order ("bit"/"little") of the output. Default is "big".

    Returns
    -------
    y: torch.Tensor
        An uint8 packed array, shape: ``(new_indptr[-1],)``.
        The ``y[new_indptr[i]:new_indptr[i+1]]`` contains the packed bits ``x[indptr[i]:indptr[i+1]]``.
    new_indptr: torch.Tensor
        The new index pointer of each packed segment in :attr:`y`, shape ``(batch_size + 1,)``.
        It's guaranteed that ``new_indptr[i+1] - new_indptr[i] == (indptr[i+1] - indptr[i] + 7) // 8``.

    Examples
    --------

    >>> import torch
    >>> from flashinfer import segment_packbits
    >>> x = torch.tensor([1, 0, 1, 1, 0, 0, 1, 1, 1, 0, 1], dtype=torch.bool, device="cuda")
    >>> x_packed, new_indptr = segment_packbits(x, torch.tensor([0, 4, 7, 11], device="cuda"), bitorder="big")
    >>> list(map(bin, x_packed.tolist()))
    ['0b10110000', '0b100000', '0b11010000']
    >>> new_indptr
    tensor([0, 1, 2, 3], device='cuda:0')

    Note
    ----
    ``torch.compile`` is not supported for this function because it's data dependent.

    See Also
    --------
    packbits
    r   Nr   r   r   r   )r   zeroslenr   r   cumsumitemr   int32r   r   r   segment_packbits)	r   r(   r   seglen
packed_len
indptr_newoutput_nnzsr   r!   s	            r   r0   r0   Q   s    ^ ABBZ&"+%F1*"JS[[V]SSSJ\*a00JqrrNR.%%''KXFYYu{##Fu{++JKu{6BBBA..q&*hPQRRRj=r   )r&   )__doc__	functoolstypingr   r   api_loggingr   jit.quantizationr   utilsr   r   cacher   Tensorstrr"   r%   r    r0   r   r   r   <module>r>      s                ' ' ' ' ' ' 5 5 5 5 5 5 7 7 7 7 7 7 7 7 6 6 6 *<<<      =< ())Qel Qc Qel Q Q Q *)Q " " " " " " " "D ;@9 9|9"\9589
5<%&9 9 9 9 9 9r   