
    )`i              	       D   d Z ddlZddlZddlZddlmZ ddlZddlm	c m
c mZ ddlZddlmZ ddlmZmZmZmZ defdZdedej        fd	Zd
 Zej        dej        defd            Z G d de          Zej         dfdee         deeej        f         dedefdZ!dS )a3  
Copyright (c) 2025 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)Union)ir)AddressSpaceNumericPointerTypereturnc                      t           j                            d          d uo t           j                            d          d uS )Ncutlasszcutlass.cute)	importlibutil	find_spec     m/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/flashinfer/cute_dsl/utils.pyis_cute_dsl_availabler      s=      ++47 	AN$$^44D@r   dtypec                     t           j        t           j        t           j        t           j        t           j        t           j        t           j        d}||          S )N)float16bfloat16float32float8_e5m2float8_e4m3fnfloat8_e8m0fnufloat4_e2m1fn)r   Float16BFloat16Float32
Float8E5M2Float8E4M3FNFloat8E8M0FNUFloat4E2M1FN)r   	dtype_maps     r   get_cutlass_dtyper$   $   sC    ?$?) -!/ - I Ur   c                    t          t          | j                                        d          }t          j        t          j        t          j        t          j        t          j        t          j	        t          j
        t          j        t          j        t          j        t          j        t          j        t          j        t          j        i}||                    |           }|t'          |  d          |S )zE
    Return the corresponding torch.dtype per the given DSL type
    Nz is not supported by torch)getattrtorch__name__lowerr   TFloat32r   r   r   r   r   r   r   r   r    r   Float8E4M3B11FNUZfloat8_e4m3fnuzget	TypeError)cutlass_dtypetorch_dtypetorch_type_maps      r   cutlass_to_torch_dtyper2   1   s     %!7!=!=!?!?FFK 	%-%.E-e1!5#8N $((77=DDDEEEr   devicec                 J    t           j                            |           j        S N)r'   cudaget_device_propertiesmulti_processor_count)r3   s    r   
get_num_smr9   H   s     :++F33IIr   c                       e Zd ZdZej        j        dfdej        fdZdefdZ	d Z
d Zd	 Zedej        fd
            Zedee         fd            Zed             ZddddedefdZd ZdefdZd ZdS )_Pointera_  Runtime representation of a pointer that can inter-operate with
    various data structures, including numpy arrays and device memory.

    :param pointer: The pointer to the data
    :type pointer: int or pointer-like object
    :param dtype: Data type of the elements pointed to
    :type dtype: Type
    :param mem_space: Memory space where the pointer resides, defaults generic
    :type mem_space: _cute_ir.AddressSpace, optional
    :param assumed_align: Alignment of input pointer in bytes, defaults None
    :type assumed_align: int, optional

    :ivar _pointer: The underlying pointer
    :ivar _dtype: Data type of the elements
    :ivar _addr_space: Memory space of the pointer
    :ivar _assumed_align: Alignment of the pointer in bytes
    :ivar _desc: C-type descriptor for the pointer
    :ivar _c_pointer: C-compatible pointer representation
    N	mem_spacec                     || _         || _        || _        ||j        dz  | _        n|| _        d | _        d | _        t          | j                   | j        z  dk    sJ d| j         d            d S )N   r   zpointer must be z bytes aligned)_pointer_dtype_addr_spacewidth_assumed_align_desc
_c_pointerint)selfpointerr   r<   assumed_aligns        r   __init__z_Pointer.__init__d   s      $ "'+"2D"/D
4=!!D$771<<<Bt2BBB =<<<<r   r	   c                 r    t          j        t          j        t          | j                                      S r5   )ctypessizeofc_void_prF   r?   rG   s    r   size_in_bytesz_Pointer.size_in_bytesz   s&    }V_S-?-?@@AAAr   c                     | j         gS r5   )	mlir_typerO   s    r   __get_mlir_types__z_Pointer.__get_mlir_types__}   s    r   c                     | j         It          j        t          | j                            | _        t          j        | j                  | _         | j         gS r5   )rE   rL   rN   rF   r?   rD   	addressofrO   s    r   __c_pointers__z_Pointer.__c_pointers__   sD    ?"T]););<<DJ$.tz::DO  r   c                 <    t          |          dk    sJ |d         S )N   r   )len)rG   valuess     r   __new_from_mlir_values__z!_Pointer.__new_from_mlir_values__   s#    6{{aayr   c                 l    t           j                            | j        j        | j        | j                  S r5   )_cute_irPtrTyper-   r@   rR   rA   rC   rO   s    r   rR   z_Pointer.mlir_type   s/    ##K!4#3T5H
 
 	
r   c                     | j         S r5   )r@   rO   s    r   r   z_Pointer.dtype   s
    {r   c                     | j         S r5   )rA   rO   s    r   memspacez_Pointer.memspace   s    r   )locip	min_alignc                     t          d          )Nz!align is not supported in runtime)NotImplementedError)rG   rd   rb   rc   s       r   alignz_Pointer.align   s    !"EFFFr   c                 l    |t           u s(t          |t          j                  r|j        t           u rdS dS )NTF)r   
isinstancer   Valuety)rG   expected_py_types     r   verifyz_Pointer.verify   s<    
 w&&'22 '7G7Jg7U7U4ur   c                 D    dt          | j                  dd| j         dS )NzPtr<0x016x@>)rF   r?   rA   rO   s    r   __str__z_Pointer.__str__   s+    EDM**EEE$2BEEEEr   c                 *    |                                  S r5   )rr   rO   s    r   __repr__z_Pointer.__repr__   s    ||~~r   )r(   
__module____qualname____doc__r]   r   genericrJ   rF   rP   rS   rV   r[   propertyr   r   rR   r   r   ra   r   rg   rm   strrr   rt   r   r   r   r;   r;   O   s        0 ,4+@+H
 
 (	
 
 
 
,Bs B B B B     ! ! !  
 
27 
 
 
 X

 tG}    X     X  ,0D G G Gs GW G G G G
 
 
F F F F F    r   r;   valuer<   c                 ,   t          |t                    r|}njt          |t          j                  r1t          j        |t          j                  j        }|
J d            nt          dt          |                    t          || ||          S )a  Create a pointer from a memory address

    :param dtype: Data type of the pointer elements
    :type dtype: Type[Numeric]
    :param value: Memory address as integer or ctypes pointer
    :type value: Union[int, ctypes._Pointer]
    :param mem_space: Memory address space, defaults to AddressSpace.generic
    :type mem_space: AddressSpace, optional
    :param assumed_align: Alignment in bytes, defaults to None
    :type assumed_align: int, optional
    :return: A pointer object
    :rtype: Pointer

    .. code-block:: python

        import numpy as np
        import ctypes

        from cutlass import Float32
        from cutlass.cute.runtime import make_ptr

        # Create a numpy array
        a = np.random.randn(16, 32).astype(np.float32)

        # Get pointer address as integer
        ptr_address = a.ctypes.data_as(ctypes.POINTER(ctypes.c_float))

        # Create pointer from address
        y = make_ptr(cutlass.Float32, ptr_address)
    NzPointer address is Nonez;Expect int or ctypes.POINTER for value but got type(value)=)rI   )	ri   rF   rL   r;   castrN   r{   r.   type)r   r{   r<   rI   address_values        r   make_ptrr      s    J % 	
	E6?	+	+ 
E6?;;A((*C((((Ld5kkLL
 
 	
 M5)=QQQQr   )"rw   rL   	functoolsimportlib.utilr   typingr   r   cutlass._mlir.dialects.cute_mlirdialectscuter]   r'   cutlass._mlirr   cutlass.cute.typingr   r   r   r   boolr   rz   r   r$   r2   cacher3   rF   r9   r;   rx   r   r   r   r   <module>r      s                     . . . . . . . . . . . .        D D D D D D D D D D D Dt    
S 
W] 
 
 
 
  . Ju| J J J J J] ] ] ] ]w ] ] ]F +2	0R 0R=0Rfo%&0R 0R
 0R 0R 0R 0R 0R 0Rr   