o
    *jm                     @   s   d dl mZ d dlZd dlmZ d dlmZ d dlmZ ee	j
 Zedeed eed gd	gd
gdZedeed eed gd	gd
gdZG dd deZejZG dd deZejZdS )    )PathN)	rearrange)Function)loadscore_computation_cudaz../src/score_computation.cppz"../src/score_computation_kernal.cuz-gz-O2)namesourcesZextra_cflagsZextra_cuda_cflagsvalue_aggregation_cudaz../src/value_aggregation.cppz"../src/value_aggregation_kernel.cuc                   @   $   e Zd Zedd Zedd ZdS )ScoreComputationc                 C   s$   t |||}| ||| |d S )Nr   )r   Zscore_forwardsave_for_backward)ctxquerykeyindexx r   /var/www/html/Deteccion_Ine/venv/lib/python3.10/site-packages/modelscope/ops/quadtree_attention/functions/quadtree_attention.pyforward   s   zScoreComputation.forwardc                 C   s6   | j \}}}| }t||||}|d |d d fS )Nr      )saved_tensors
contiguousr   Zscore_backward)r   grad_outputZinput1Zinput2r   r   r   r   r   backward    s   
zScoreComputation.backwardN__name__
__module____qualname__staticmethodr   r   r   r   r   r   r      s
    
r   c                   @   r
   )value_aggregationc                 C   s|   |  ||| |jd }t|d}t|d}|j\}}}}|jd }	|||||	g }
t||||
 t|
d|d}
|
S )N   b n f K h -> b (n f) K hzb (n f) h d -> b n f h df)r   shaper   	new_zerosr   r	   Zvalue_aggregation_forward)r   scorevaluer   r$   bN_HDoutputr   r   r   r   .   s$   

zvalue_aggregation.forwardc                 C   s~   | j \}}}|jd }t|d}t|d}| }||j }||j }t|||||| t|d|d}||d fS )Nr    r!   zb (n f) K h -> b n f K hr#   )r   r%   r   r   r&   r	   Zvalue_aggregation_backward)r   r   r'   r(   r   r$   Z
grad_scoreZ
grad_valuer   r   r   r   A   s   



zvalue_aggregation.backwardNr   r   r   r   r   r   ,   s
    
r   )pathlibr   ZtorchZeinops.einopsr   Ztorch.autogradr   Ztorch.utils.cpp_extensionr   __file__parentresolveZcur_dirstrr   r	   r   applyZscore_computation_opr   Zvalue_aggregation_opr   r   r   r   <module>   s0   




'