
    )`iq               
          d dl Z d dl mZmZmZmZmZmZmZmZm	Z	 d dl
Z
 G d de j                  Z G d de j                  Z G d de j                  Z ed ee j                            Z G d	 d
e j                  Zdefdefd ed ee                    fge_         ed ee                    d             Z G d d          Zd Zdedededede
j        f
dZdS )    N)		CFUNCTYPEPOINTERc_intc_int64c_size_tc_uint8c_uint16c_void_ppointerc                   $    e Zd ZdefdefdefgZdS )
DLDataTypecodebitslanesN)__name__
__module____qualname__r   r	   _fields_     p/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/flashinfer/comm/dlpack_utils.pyr   r       s-        			(HHHr   r   c                       e Zd ZdefdefgZdS )DLDevicedevice_type	device_idN)r   r   r   r   r   r   r   r   r   r   (   s%        		eHHHr   r   c            	       `    e Zd Zdefdefdefdefd ee          fd ee          fde	fgZ
dS )	DLTensordatadevicendimdtypeshapestridesbyte_offsetN)r   r   r   r
   r   r   r   r   r   r   r   r   r   r   r   r   /   se        		8		*	'''""#GG	
 
!HHHr   r   c                       e Zd ZdS )DLManagedTensorN)r   r   r   r   r   r   r&   r&   E   s        Dr   r&   	dl_tensormanager_ctxdeleterc                     d S )Nr   )dmt_ptrs    r   no_op_deleterr,   Q   s	     	Dr   c                       e Zd ZdZd ZdS )CapsuleWrapperaE  
    A wrapper class that holds references to the PyCapsule and its associated data.

    This class prevents Python's garbage collector from collecting the shape_array and
    managed_tensor objects while the capsule is still in use. It serves as a container
    to maintain the lifecycle of all DLPack-related objects.
    c                 0    || _         || _        || _        dS )aF  
        Initialize the CapsuleWrapper with the necessary objects.

        Parameters:
            capsule: The PyCapsule object that follows the DLPack protocol
            shape_array: The array containing tensor shape information
            managed_tensor: The DLManagedTensor instance that the capsule points to
        N)capsule_shape_array_managed_tensor)selfr0   shape_arraymanaged_tensors       r   __init__zCapsuleWrapper.__init__a   s)      	 ( 	r   N)r   r   r   __doc__r6   r   r   r   r.   r.   X   s-         
 
 
 
 
r   r.   c                 T   d}d}|t           j        t           j        t           j        t           j        t           j        t           j        fv rt          j        |          j        }d}n|t           j	        t           j
        t           j        t           j        fv rt          j        |          j        }d}n[|t           j        t           j        t           j        t           j        fv rt          j        |          j        }d}nt%          |          |dz  }t&          dz  }	 |	|||z            }
 |	||z  d          }t)          d|          }t+          ||d          }t-                      }t/          |           |_        ||_        d|_        ||_        t9          j        |
t=          t&                              |_        t9          j        |t=          t&                              |_         d|_!        tE                      }||_#        d|_$        tJ          |_&        t8          j'        j(        }t.          |_)        t.          t8          j*        t.          g|_+        tY          |          } ||dd          }t9          j        |t8          j-                  j.        }t_          ||
|          }|S )	a  
    Parameters:
      ptr: GPU memory address obtained from cudaMalloc (Python int)
      segment_size: Memory size of each segments in bytes
      segment_stride: Memory stride size between segments in bytes
      num_segments: Number of segments
      torch_dtype: torch dtype
      dev_id: device id.
    Returns:
      A PyCapsule object compliant with DLPack specification, which can be directly converted to a
      tensor using torch.utils.dlpack.from_dlpack
    r            )r   r   )r   r   r   Ns   dltensor)0torchfloat8_e5m2float8_e4m3fnbfloat16float16float32float64finfor   int8int16int32int64iinfouint8uint16uint32uint64NotImplementedErrorr   r   r   r   r
   r   r   r    r!   ctypescastr   r"   r#   r$   r&   r'   r(   r,   r)   	pythonapiPyCapsule_Newrestypec_char_pargtypesr   	py_objectvaluer.   )ptrsegment_sizesegment_stridenum_segmentstorch_dtypedev_idbits_per_elementsdldata_type_codebytes_per_elementShapeArrayTyper4   stride_arrayr   r!   dltensorr5   rQ   managed_tensor_ptrcapsule_ptrr0   capsule_wrappers                        r   create_dlpack_capsulerf   s   sK       "K449	U[%+u{K	K	K!K449	elEL%,O	O	O!K449!+...)Q.q[N .|?P/PQQK!>.4E"EqIIL!v666F,3DANNNEzzHSMMHMHOHMHN[gg.>.>??HN{<1A1ABBHH$&&N'N!%N*N $2M$M&BM 00- 2KFFKk+v'788>G$Wk>JJOr   rW   rX   rY   rZ   r!   c                     t          | |||||          }t          j        j                            |j                  }||_        |S )ax  
    Pack GPU memory into a PyTorch tensor with specified stride.

    Parameters:
        ptr: GPU memory address obtained from cudaMalloc
        segment_size: Memory size of each segment in bytes
        segment_stride: Memory stride size between segments in bytes
        num_segments: Number of segments
        dtype: PyTorch data type for the resulting tensor
        dev_id: CUDA device ID

    Returns:
        PyTorch tensor that references the provided memory

    Note:
        This function creates a new DLPack capsule each time it's called,
        even with the same pointer. Each capsule is consumed only once.
    )rf   r<   utilsdlpackfrom_dlpackr0   _capsule_wrapper)rW   rX   rY   rZ   r!   r\   re   torch_tensors           r   pack_strided_memoryrm      sK    6 ,\>< O ;%11/2IJJL$3L!r   )rN   r   r   r   r   r   r   r	   r
   r   r<   	Structurer   r   r   DLManagedTensorDeleterr&   r   r,   r.   rf   intr!   rm   r   r   r   <module>rq      sW   
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
     !       v       v     #''&/
"
"  	 	 	 	 	f& 	 	 	
 (H		$ 8 899:  4))**	 	 +*	
 
 
 
 
 
 
 
6I I IX 	     	 
 ;           r   