o
    ॵim                     @   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sourcesextra_cflags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   score_forwardsave_for_backward)ctxquerykeyindexx r   r/home/ubuntu/.local/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   score_backward)r   grad_outputinput1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   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   value_aggregation_backward)r   r   r.   r/   r   r*   
grad_score
grad_valuer   r   r   r   A   s   



zvalue_aggregation.backwardNr    r   r   r   r   r%   ,   s
    
r%   )pathlibr   torcheinops.einopsr   torch.autogradr   torch.utils.cpp_extensionr   __file__parentresolvecur_dirstrr   r   r   applyscore_computation_opr%   value_aggregation_opr   r   r   r   <module>   s0   




'