o
    X۷i                     @  s  d dl mZ d dlmZ d dlmZ d dlmZ d dlmZ G dd dZ	dd	 Z
d
d Zdd ZG dd dejZG dd dejZG dd deZG dd deZe	eZe	eZG dd dejZeejrjdndZeejrsdndZeejrdZdS dZdS )    )annotations)_cuda_types)_cuda_typerules)_internal_types)runtimec                   @     e Zd Zdd Zdd ZdS )_ClassTemplatec                 C  s   || _ | j j| _d S N)_class_type__doc__)self
class_type r   C/home/ubuntu/vllm_env/lib/python3.10/site-packages/cupyx/jit/cub.py__init__   s   z_ClassTemplate.__init__c                 C  s   t |tr
| j| S | |S r	   )
isinstancetupler
   )r   argsr   r   r   __getitem__   s   


z_ClassTemplate.__getitem__N)__name__
__module____qualname__r   r   r   r   r   r   r   	       r   c                 C  s>   t jr| jd d| j_d S | jd d| j_d| j_d S )Nz#include <hipcub/hipcub.hpp>nvccz%#include <cub/block/block_reduce.cuh>nvrtcF)_runtimeis_hip	generatedadd_codebackendjitify)envr   r   r   _include_cub   s   r"   c                   C     t jrdS dS )Nhipcubcubr   r   r   r   r   r   _get_cub_namespace%      r'   c                   C  r#   )Nr$   cudar&   r   r   r   r   _get_cub_op_namespace)   r(   r*   c                      s&   e Zd Z fddZdddZ  ZS )_TempStorageTypec                   s"   t |tsJ || _t   d S r	   )r   _CubReduceBaseTypeparent_typesuperr   )r   r-   	__class__r   r   r   /   s   z_TempStorageType.__init__returnstrc                 C  s   d| j  dS )Nz	typename z::TempStorage)r-   )r   r   r   r   __str__4   r(   z_TempStorageType.__str__r1   r2   r   r   r   r   r3   __classcell__r   r   r/   r   r+   -   s    r+   c                   @  s4   e Zd Zd
ddZejd
ddZejdd Zd	S )r,   r1   _internal_types.Datac                 C  sH   t | |j| jkrtd|j d| j dt|  d|j d| S )NzInvalid temp_storage type . ( is expected.)())r"   ctypeTempStorage	TypeErrorr   Datacode)r   r!   temp_storager   r   r   _instantiate:   s   
z_CubReduceBaseType._instantiatec                 C  sD   |j | jkrtd|j  d| j dt|j d|j d|j S )NInvalid input type r8   r9   z.Sum(r;   r<   Tr>   r   r?   r@   )r   r!   instanceinputr   r   r   SumB   s   z_CubReduceBaseType.Sumc                 C  sL   |j | jkrtd|j  d| j dt|j d|j d|j d|j S )NrC   r8   r9   z.Reduce(, r;   rD   )r   r!   rF   rG   reduction_opr   r   r   ReduceJ   s   z_CubReduceBaseType.ReduceN)r1   r7   )r   r   r   rB   r   wraps_class_methodrH   rK   r   r   r   r   r,   8   s    
r,   c                      s(   e Zd Zd fddZd	ddZ  ZS )
_WarpReduceTyper1   Nonec                   s$   t || _t| | _t   d S r	   )r   to_ctyperE   r+   r=   r.   r   )r   rE   r/   r   r   r   V   s   
z_WarpReduceType.__init__r2   c                 C  s   t  }| d| j dS )Nz::WarpReduce<>)r'   rE   r   	namespacer   r   r   r3   [   s   z_WarpReduceType.__str__)r1   rN   r4   r5   r   r   r/   r   rM   T   s    rM   c                      s(   e Zd Zd
 fddZddd	Z  ZS )_BlockReduceTypeBLOCK_DIM_Xintr1   rN   c                   s*   t || _|| _t| | _t   d S r	   )r   rO   rE   rT   r+   r=   r.   r   )r   rE   rT   r/   r   r   r   b   s   
z_BlockReduceType.__init__r2   c                 C  s    t  }| d| j d| j dS )Nz::BlockReduce<rI   rP   )r'   rE   rT   rQ   r   r   r   r3   h   s   z_BlockReduceType.__str__)rT   rU   r1   rN   r4   r5   r   r   r/   r   rS   `   s    rS   c                   @  r   )_CubFunctorc                 C  s   t  }| d| d| _d S )Nz::z())r*   fname)r   namerR   r   r   r   r   s   s   z_CubFunctor.__init__c                 C  s   t | jtjddS )Ncub_functor)label)r   r?   rW   r   Unknown)r   r!   r   r   r   
call_constw   s   z_CubFunctor.call_constN)r   r   r   r   r\   r   r   r   r   rV   q   r   rV   rH   z	std::plusMaxmaximumMinminimumN)
__future__r   	cupyx.jitr   r   r   cupy_backends.cuda.apir   r   r   r"   r'   r*   TypeBaser+   r,   rM   rS   
WarpReduceBlockReduceBuiltinFuncrV   r   rH   r]   r_   r   r   r   r   <module>   s&    