o
    soiQ                     @  sT   d dl mZ d dlZd dlZd dlmZ d dlmZ d dl	m
Z
 G dd deZdS )    )annotationsN)Base)expect)_compute_attentionc                   @  sp  e Zd ZedddZedddZedddZedd	d
ZedddZedddZ	edddZ
edddZedddZedddZedddZedddZedddZedddZeddd Zedd!d"Zedd#d$Zedd%d&Zedd'd(Zedd)d*Zedd+d,Zedd-d.Zedd/d0Zedd1d2Zedd3d4Zedd5d6Zedd7d8Zedd9d:Zedd;d<Z edd=d>Z!edd?d@Z"eddAdBZ#eddCdDZ$eddEdFZ%eddGdHZ&eddIdJZ'eddKdLZ(eddMdNZ)eddOdPZ*eddQdRZ+eddSdTZ,eddUdVZ-eddWdXZ.eddYdZZ/edd[d\Z0edd]d^Z1edd_d`Z2eddadbZ3eddcddZ4eddedfZ5eddgdhZ6eddidjZ7eddkdlZ8eddmdnZ9eddodpZ:eddqdrZ;eddsdtZ<eddudvZ=eddwdxZ>eddydzZ?edd{d|Z@edd}d~ZAdS )	AttentionreturnNonec               	   C     t jjdg ddgd} tjddddtj}tjddd	dtj}tjddd	dtj}t|||\}}}}t	| |||g|gd
t j
ddgd d S )Nr   QKVYinputsoutputs               test_attention_4d    r   r   nameopset_importsonnxhelper	make_nodenprandomrandastypefloat32r   r   make_opsetidnoder   r   r   r   _ r*   Y/home/ubuntu/.local/lib/python3.10/site-packages/onnx/backend/test/case/node/attention.pyexport_attention      
zAttention.export_attentionc               	   C  r	   )Nr   r
   r   r   r   r   r   r   r   test_attention_4d_fp16r   r   r   r   r   r    r!   r"   r#   r$   float16r   r   r&   r'   r*   r*   r+   export_attention_fp16!   r-   zAttention.export_attention_fp16c               	   C  s   t jjdg ddgd} tjddddtj}tjdd	d
dtj}tjdd	d
dtj}t|||\}}}}t	| |||g|gdt j
ddgd d S )Nr   r
   r   r   r   	   r   r   r   r   test_attention_4d_gqar   r   r   r   r'   r*   r*   r+   export_attention_gqa3   r-   zAttention.export_attention_gqac               	   C  s   t jjdg ddgd} tjddddtj}tjddd	dtj}tjddd	d
tj}t|||\}}}}t	| |||g|gdt j
ddgd d S )Nr   r
   r   r   r   r   r   r   r   
   "test_attention_4d_diff_heads_sizesr   r   r   r   r'   r*   r*   r+    export_attention_diff_head_sizesE   r-   z*Attention.export_attention_diff_head_sizesc               	   C  s   d} t jjdg ddg| d}tjdddd	tj}tjddd
d	tj}tjddd
d	tj}t|||| d\}}}}t	||||g|gdt j
ddgd d S )N{Gz?r   r
   r   r   r   scaler   r   r   r   r   r:   test_attention_4d_scaledr   r   r   r   r:   r(   r   r   r   r   r)   r*   r*   r+   export_attention_scaledW   $   
z!Attention.export_attention_scaledc               	   C  s   d} t jjdg ddg| d}tjdddd	tj}tjdd
dd	tj}tjdd
dd	tj}t|||| d\}}}}t	||||g|gdt j
ddgd d S )Nr8   r   r
   r   r9   r   r2   r   r   r   r   r;   test_attention_4d_gqa_scaledr   r   r   r   r=   r*   r*   r+   export_attention_gqa_scaledo   r?   z%Attention.export_attention_gqa_scaledc               	   C  s   d} t jjdg ddg| d}tjdddd	tj}tjddd
d	tj}tjddd
dtj}t|||| d\}}}}t	||||g|gdt j
ddgd d S )Nr8   r   r
   r   r9   r   r   r   r   r   r5   r;   )test_attention_4d_diff_heads_sizes_scaledr   r   r   r   r=   r*   r*   r+   'export_attention_diff_head_sizes_scaled   r?   z1Attention.export_attention_diff_head_sizes_scaledc               	   C     t jjdg ddgdd} tjdddd	tj}tjddd
d	tj}tjddd
d	tj}t|||dd\}}}}t	| |||g|gdt j
ddgd d S )Nr   r
   r      r   r   	is_causalr   r   r   r   r   rG   test_attention_4d_causalr   r   r   r   r'   r*   r*   r+   export_attention_causal   "   
z!Attention.export_attention_causalc               	   C     t jjdg ddgdd} tjdddd	tj}tjdd
dd	tj}tjdd
dd	tj}t|||dd\}}}}t	| |||g|gdt j
ddgd d S )Nr   r
   r   rE   rF   r   r2   r   r   r   r   rH   test_attention_4d_gqa_causalr   r   r   r   r'   r*   r*   r+   export_attention_gqa_causal   rK   z%Attention.export_attention_gqa_causalc               	   C     t jjdg ddgdd} tjdddd	tj}tjddd
d	tj}tjddd
dtj}t|||dd\}}}}t	| |||g|gdt j
ddgd d S )Nr   r
   r   rE   rF   r   r   r   r   r   r5   rH   )test_attention_4d_diff_heads_sizes_causalr   r   r   r   r'   r*   r*   r+   'export_attention_diff_head_sizes_causal   ,   
z1Attention.export_attention_diff_head_sizes_causalc               	   C  s   t jjdg ddgd} tjddddtj}tjddd	dtj}tjddd	dtj}tjdd	tj}t||||d
\}}}}t	| ||||g|gdt j
ddgd d S )Nr   r   r   r   	attn_maskr   r   r   r   r   r   r   rT   test_attention_4d_attn_maskr   r   r   r   r(   r   r   r   rT   r   r)   r*   r*   r+   export_attention_attn_mask   ,   

z$Attention.export_attention_attn_maskc               	   C  s   t jjdg ddgd} tjddddtj}tjddd	dtj}tjddd	dtj}tjdd
dd	tj}t||||d\}}}}t	| ||||g|gdt j
ddgd d S )Nr   rS   r   r   r   r   r   r   r   rE   rU   test_attention_4d_attn_mask_3dr   r   r   r   rW   r*   r*   r+   export_attention_attn_3d_mask  ,   

z'Attention.export_attention_attn_3d_maskc               	   C  s   t jjdg ddgdd} tjdddd	tj}tjddd
d	tj}tjddd
d	tj}tjdddd
tj}t||||dd\}}}}t	| ||||g|gdt j
ddgd d S )Nr   rS   r   rE   rF   r   r   r   r   r   rT   rG   %test_attention_4d_attn_mask_3d_causalr   r   r   r   rW   r*   r*   r+   $export_attention_attn_3d_mask_causal!  0   

z.Attention.export_attention_attn_3d_mask_causalc               	   C  s   t jjdg ddgd} tjddddtj}tjddd	dtj}tjddd	dtj}tjdddd	tj}t||||d
\}}}}t	| ||||g|gdt j
ddgd d S )Nr   rS   r   r   r   r   r   r   r   rU   test_attention_4d_attn_mask_4dr   r   r   r   rW   r*   r*   r+   export_attention_attn_4d_mask?  r\   z'Attention.export_attention_attn_4d_maskc               	   C  s   t jjdg ddgdd} tjdddd	tj}tjddd
d	tj}tjddd
d	tj}tjdddd
tj}t||||dd\}}}}t	| ||||g|gdt j
ddgd d S )Nr   rS   r   rE   rF   r   r   r   r   r   r]   %test_attention_4d_attn_mask_4d_causalr   r   r   r   rW   r*   r*   r+   $export_attention_attn_4d_mask_causal[  r`   z.Attention.export_attention_attn_4d_mask_causalc               	   C  s   t jjdg ddgd} tjddddtj}tjddd	dtj}tjddd	dtj}tjdd	t}t	||||d
\}}}}t
| ||||g|gdt jddgd d S )Nr   rS   r   r   r   r   r   r   r   rU    test_attention_4d_attn_mask_boolr   r   r   r   r   r    r!   r"   r#   r$   r%   boolr   r   r&   rW   r*   r*   r+   export_attention_attn_mask_booly  s,   

z)Attention.export_attention_attn_mask_boolc               	   C  s   t jjdg ddgd} tjddddtj}tjddd	dtj}tjddd	dtj}tjdddd	t}t	||||d
\}}}}t
| ||||g|gdt jddgd d S )Nr   rS   r   r   r   r   r   r   r   rU   #test_attention_4d_attn_mask_bool_4dr   r   r   rf   rW   r*   r*   r+   "export_attention_attn_mask_bool_4d  s,   

z,Attention.export_attention_attn_mask_bool_4dc               	   C  s   t jjdg ddgd} tjddddtj}tjdd	d
dtj}tjdd	d
dtj}tjdd
tj}t||||d\}}}}t	| ||||g|gdt j
ddgd d S )Nr   rS   r   r   r   r2   r   r   r   r   rU   test_attention_4d_gqa_attn_maskr   r   r   r   rW   r*   r*   r+   export_attention_gqa_attn_mask  rY   z(Attention.export_attention_gqa_attn_maskc               	   C  s   t jjdg ddgd} tjddddtj}tjddd	dtj}tjddd	d
tj}tjdd	tj}t||||d\}}}}t	| ||||g|gdt j
ddgd d S )Nr   rS   r   r   r   r   r   r   r   r5   rU   ,test_attention_4d_diff_heads_sizes_attn_maskr   r   r   r   rW   r*   r*   r+   *export_attention_diff_head_sizes_attn_mask  rY   z4Attention.export_attention_diff_head_sizes_attn_maskc               	   C  s
  t jjdg dg dd} d}tjdddd	tj}tjddd
d	tj}tjddd
d	tj}tjdd
| tj}tjdd|d	tj}tjdd|d	tj}t||||||d\}}	}
}t	| ||||||g||	|
gdt j
ddgd d S )Nr   r   r   r   rT   past_key
past_valuer   present_keypresent_valuer      r   r   r   r   r   rT   rp   rq   'test_attention_4d_with_past_and_presentr   r   r   r   r(   past_sequence_lengthr   r   r   rT   rp   rq   r   rs   rt   r)   r*   r*   r+   &export_attention_with_past_and_present  6   	
z0Attention.export_attention_with_past_and_presentc               	   C  
  t jjdg dg dd} d}tjdddd	tj}tjdd
dd	tj}tjdd
dd	tj}tjdd| tj}tjdd
|d	tj}tjdd
|d	tj}t||||||d\}}	}
}t	| ||||||g||	|
gdt j
ddgd d S )Nr   ro   rr   r   ru   r   r2   r   r   r   r   rv   +test_attention_4d_gqa_with_past_and_presentr   r   r   r   rx   r*   r*   r+   *export_attention_gqa_with_past_and_present
  r{   z4Attention.export_attention_gqa_with_past_and_presentc               	   C  r|   )Nr   ro   rr   r   ru   r   r2   r   r   r   r   rv   0test_attention_4d_gqa_with_past_and_present_fp16r   r   r   r/   rx   r*   r*   r+   /export_attention_gqa_with_past_and_present_fp16+  r{   z9Attention.export_attention_gqa_with_past_and_present_fp16c               	   C  s
  t jjdg dg dd} d}tjdddd	tj}tjddd
d	tj}tjddd
dtj}tjdd
| tj}tjdd|d	tj}tjdd|dtj}t||||||d\}}	}
}t	| ||||||g||	|
gdt j
ddgd d S )Nr   ro   rr   r   ru   r   r   r   r   r   r5   rv   2test_attention_4d_diff_heads_with_past_and_presentr   r   r   r   rx   r*   r*   r+   6export_attention_diff_head_sizes_with_past_and_presentL  r{   z@Attention.export_attention_diff_head_sizes_with_past_and_presentc               	   C  s  t jjdg dg dd} d}tjdddd	tj}tjddd
d	tj}tjddd
dtj}tjdddd
| tj}tjdd|d	tj}tjdd|dtj}t||||||d\}}	}
}t	| ||||||g||	|
gdt j
ddgd d S )Nr   ro   rr   r   ru   r   r   r   r   r   r5   rE   rv   9test_attention_4d_diff_heads_with_past_and_present_mask3dr   r   r   r   rx   r*   r*   r+   =export_attention_diff_head_sizes_with_past_and_present_mask3Dm  6   	
zGAttention.export_attention_diff_head_sizes_with_past_and_present_mask3Dc               	   C  s  t jjdg dg dd} d}tjdddd	tj}tjddd
d	tj}tjddd
dtj}tjdddd
| tj}tjdd|d	tj}tjdd|dtj}t||||||d\}}	}
}t	| ||||||g||	|
gdt j
ddgd d S )Nr   ro   rr   r   ru   r   r   r   r   r   r5   rv   9test_attention_4d_diff_heads_with_past_and_present_mask4dr   r   r   r   rx   r*   r*   r+   =export_attention_diff_head_sizes_with_past_and_present_mask4D  r   zGAttention.export_attention_diff_head_sizes_with_past_and_present_mask4Dc               	   C  rD   )Nr   r
   r          @r   r   softcapr   r   r   r   r   r   test_attention_4d_softcapr   r   r   r   r'   r*   r*   r+   export_attention_softcap  rK   z"Attention.export_attention_softcapc               	   C  rL   )Nr   r
   r   r   r   r   r2   r   r   r   r   r   test_attention_4d_gqa_softcapr   r   r   r   r'   r*   r*   r+   export_attention_gqa_softcap  rK   z&Attention.export_attention_gqa_softcapc               	   C  rO   )Nr   r
   r   r   r   r   r   r   r   r   r5   r   *test_attention_4d_diff_heads_sizes_softcapr   r   r   r   r'   r*   r*   r+   (export_attention_diff_head_sizes_softcap  rR   z2Attention.export_attention_diff_head_sizes_softcapc               	   C  s   t jjdg dg dd} tjddddtj}tjddd	dtj}tjddd	dtj}t|||\}}}}t	| |||g||gd
t j
ddgd d S )Nr   r
   r   r   r   qk_matmul_outputr   r   r   r   r   r    test_attention_4d_with_qk_matmulr   r   r   r   )r(   r   r   r   r   r)   r   r*   r*   r+   export_attention_with_qk_matmul  s    
z)Attention.export_attention_with_qk_matmulc               	   C  s   t jjdg dg ddd} tjdddd	tj}tjddd
d	tj}tjddd
d	tj}tjdd
tj}t||||dd\}}}}t	| ||||g||gdt j
ddgd d S )Nr   rS   r   rE   r   r   qk_matmul_output_moder   r   r   r   r   rT   r   %test_attention_4d_with_qk_matmul_biasr   r   r   r   r(   r   r   r   rT   r   r)   r   r*   r*   r+   $export_attention_with_qk_matmul_bias  0   

z.Attention.export_attention_with_qk_matmul_biasc               	   C  s   t jjdg dg dddd} tjdddd	tj}tjddd
d	tj}tjddd
d	tj}tjdd
tj}t||||ddd\}}}}t	| ||||g||gdt j
ddgd d S )Nr   rS   r   r   r   )r   r   r   r   r   r   r   r   )rT   r   r   (test_attention_4d_with_qk_matmul_softcapr   r   r   r   r   r*   r*   r+   'export_attention_with_qk_matmul_softcap-  s4   	

z1Attention.export_attention_with_qk_matmul_softcapc               	   C  s   t jjdg dg ddd} tjddddtj}tjddd	dtj}tjddd	dtj}tjdd	tj}t||||dd
\}}}}t	| ||||g||gdt j
ddgd d S )Nr   rS   r   r   r   r   r   r   r   r   (test_attention_4d_with_qk_matmul_softmaxr   r   r   r   r   r*   r*   r+   'export_attention_with_qk_matmul_softmaxM  r   z1Attention.export_attention_with_qk_matmul_softmaxc               	   C  s  t jjdg dg ddd} d}tjddd	d
tj}tjdddd
tj}tjdddd
tj}tjd	d| tj}tjdd|d
tj}tjdd|d
tj}t||||||dd\}}	}
}t	| ||||||g||	|
|gdt j
ddgd d S )Nr   ro   r   rs   rt   r   rE   r   ru   r   r   r   r   r   rT   rp   rq   r   6test_attention_4d_with_past_and_present_qk_matmul_biasr   r   r   r   r(   ry   r   r   r   rT   rp   rq   r   rs   rt   r   r*   r*   r+   5export_attention_with_past_and_present_qk_matmul_biask  s:   


z?Attention.export_attention_with_past_and_present_qk_matmul_biasc               	   C  s  t jjdg dg ddd} d}tjddd	d
tj}tjdddd
tj}tjdddd
tj}tjddd	d| tj}tjdd|d
tj}tjdd|d
tj}t||||||dd\}}	}
}t	| ||||||g||	|
|gdt j
ddgd d S )Nr   ro   r   rE   r   ru   r   r   r   r   r   r   >test_attention_4d_with_past_and_present_qk_matmul_bias_3d_maskr   r   r   r   r   r*   r*   r+   =export_attention_with_past_and_present_qk_matmul_bias_3d_mask  :   


zGAttention.export_attention_with_past_and_present_qk_matmul_bias_3d_maskc               	   C  s  t jjdg dg ddd} d}tjddd	d
tj}tjdddd
tj}tjdddd
tj}tjddd	d| tj}tjdd|d
tj}tjdd|d
tj}t||||||dd\}}	}
}t	| ||||||g||	|
|gdt j
ddgd d S )Nr   ro   r   rE   r   ru   r   r   r   r   r   r   >test_attention_4d_with_past_and_present_qk_matmul_bias_4d_maskr   r   r   r   r   r*   r*   r+   =export_attention_with_past_and_present_qk_matmul_bias_4d_mask  r   zGAttention.export_attention_with_past_and_present_qk_matmul_bias_4d_maskc               
   C  s  t jjdg dg dddd} d}tjddd	d
tj}tjdddd
tj}tjdddd
tj}tjddd	d| tj}tjdd|d
tj}tjdd|d
tj}t||||||ddd\}}	}
}t	| ||||||g||	|
|gdt j
ddgd d S )Nr   ro   r   rE   r   r   r   rG   ru   r   r   r   r   r   rT   rp   rq   r   rG   Etest_attention_4d_with_past_and_present_qk_matmul_bias_3d_mask_causalr   r   r   r   r   r*   r*   r+   Dexport_attention_with_past_and_present_qk_matmul_bias_3d_mask_causal  >   

zNAttention.export_attention_with_past_and_present_qk_matmul_bias_3d_mask_causalc               
   C  s  t jjdg dg dddd} d}tjddd	d
tj}tjdddd
tj}tjdddd
tj}tjddd	d| tj}tjdd|d
tj}tjdd|d
tj}t||||||ddd\}}	}
}t	| ||||||g||	|
|gdt j
ddgd d S )Nr   ro   r   rE   r   ru   r   r   r   r   r   r   Etest_attention_4d_with_past_and_present_qk_matmul_bias_4d_mask_causalr   r   r   r   r   r*   r*   r+   Dexport_attention_with_past_and_present_qk_matmul_bias_4d_mask_causal  r   zNAttention.export_attention_with_past_and_present_qk_matmul_bias_4d_mask_causalc               	   C  s  t jjdg dg dd} d}tjdddd	tj}tjddd
d	tj}tjddd
d	tj}tjdd
| tj}tjdd|d	tj}tjdd|d	tj}t||||||d\}}	}
}t	| ||||||g||	|
|gdt j
ddgd d S )Nr   ro   r   r   ru   r   r   r   r   r   rv   1test_attention_4d_with_past_and_present_qk_matmulr   r   r   r   r   r*   r*   r+   0export_attention_with_past_and_present_qk_matmul  s6   	

z:Attention.export_attention_with_past_and_present_qk_matmulc               	   C  s   d\} }t jjdg ddg| |d}tjdddtj}tjdd	dtj}tjdd	dtj}t|||| |d
\}}}}t	||||g|gdt j
ddgd d S )Nr   r   r   r
   r   r   r   q_num_headskv_num_headsr   r      r   r   r   test_attention_3dr   r   r   r   r   r   r(   r   r   r   r   r)   r*   r*   r+   export_attention_3d?  2   
zAttention.export_attention_3dc               	   C  s   d\} }t jjdg ddg| |d}tjdddtj}tjdd	d
tj}tjdd	d
tj}t|||| |d\}}}}t	||||g|gdt j
ddgd d S )Nr2   r   r   r
   r   r   r   r   H   r   r   r   test_attention_3d_gqar   r   r   r   r   r*   r*   r+   export_attention_3d_gqa^  r   z!Attention.export_attention_3d_gqac               	   C  s   d\} }t jjdg ddg| |d}tjdddtj}tjdd	dtj}tjdd	d
tj}t|||| |d\}}}}t	||||g|gdt j
ddgd d S )Nr   r   r
   r   r   r   r   r   r      r   "test_attention_3d_diff_heads_sizesr   r   r   r   r   r*   r*   r+   #export_attention_3d_diff_head_sizes}  r   z-Attention.export_attention_3d_diff_head_sizesc            	   	   C  s   d} d\}}t jjdg ddg| ||d}tjddd	tj}tjdd
d	tj}tjdd
d	tj}t|||| ||d\}}}}t	||||g|gdt j
ddgd d S )Nr8   r   r   r
   r   r   r   r:   r   r   r   r   r   r   r:   r   r   test_attention_3d_scaledr   r   r   r   	r:   r   r   r(   r   r   r   r   r)   r*   r*   r+   export_attention_3d_scaled  8   		
z$Attention.export_attention_3d_scaledc            	   	   C  s   d} d\}}t jjdg ddg| ||d}tjddd	tj}tjdd
dtj}tjdd
dtj}t|||| ||d\}}}}t	||||g|gdt j
ddgd d S )Nr8   r   r   r
   r   r   r   r   r   r   r   r   test_attention_3d_gqa_scaledr   r   r   r   r   r*   r*   r+   export_attention_3d_gqa_scaled  r   z(Attention.export_attention_3d_gqa_scaledc            	   	   C  s   d} d\}}t jjdg ddg| ||d}tjddd	tj}tjdd
d	tj}tjdd
dtj}t|||| ||d\}}}}t	||||g|gdt j
ddgd d S )Nr8   r   r   r
   r   r   r   r   r   r   r   r   )test_attention_3d_diff_heads_sizes_scaledr   r   r   r   r   r*   r*   r+   *export_attention_3d_diff_head_sizes_scaled  r   z4Attention.export_attention_3d_diff_head_sizes_scaledc               	   C     d\} }t jjdg ddgd| |d}tjddd	tj}tjdd
d	tj}tjdd
d	tj}t|||d| |d\}}}}t	||||g|gdt j
ddgd d S )Nr   r   r
   r   rE   r   r   rG   r   r   r   r   r   r   rG   r   r   test_attention_3d_causalr   r   r   r   r   r*   r*   r+   export_attention_3d_causal  6   		
z$Attention.export_attention_3d_causalc               	   C     d\} }t jjdg ddgd| |d}tjddd	tj}tjdd
dtj}tjdd
dtj}t|||d| |d\}}}}t	||||g|gdt j
ddgd d S )Nr   r   r
   r   rE   r   r   r   r   r   r   r   test_attention_3d_gqa_causalr   r   r   r   r   r*   r*   r+   export_attention_3d_gqa_causal#  r   z(Attention.export_attention_3d_gqa_causalc               	   C     d\} }t jjdg ddgd| |d}tjddd	tj}tjdd
d	tj}tjdd
dtj}t|||d| |d\}}}}t	||||g|gdt j
ddgd d S )Nr   r   r
   r   rE   r   r   r   r   r   r   r   )test_attention_3d_diff_heads_sizes_causalr   r   r   r   r   r*   r*   r+   *export_attention_3d_diff_head_sizes_causalD  r   z4Attention.export_attention_3d_diff_head_sizes_causalc            	   	   C  s   d\} }t jjdg ddg| |d}tjdddtj}tjdd	dtj}tjdd	dtj}tjdd	tj}t||||| |d
\}}}}t	|||||g|gdt j
ddgd d S )Nr   r   rS   r   r   r   r   r   r   rT   r   r   test_attention_3d_attn_maskr   r   r   r   	r   r   r(   r   r   r   rT   r   r)   r*   r*   r+   export_attention_3d_attn_maske  6   	

z'Attention.export_attention_3d_attn_maskc            	   	   C  s   d\} }t jjdg ddg| |d}tjdddtj}tjdd	d
tj}tjdd	d
tj}tjdd	tj}t||||| |d\}}}}t	|||||g|gdt j
ddgd d S )Nr   r   rS   r   r   r   r   r   r   r   r   test_attention_3d_gqa_attn_maskr   r   r   r   r   r*   r*   r+   !export_attention_3d_gqa_attn_mask  r   z+Attention.export_attention_3d_gqa_attn_maskc            	   	   C  s   d\} }t jjdg ddg| |d}tjdddtj}tjdd	dtj}tjdd	d
tj}tjdd	tj}t||||| |d\}}}}t	|||||g|gdt j
ddgd d S )Nr   r   rS   r   r   r   r   r   r   r   r   ,test_attention_3d_diff_heads_sizes_attn_maskr   r   r   r   r   r*   r*   r+   -export_attention_3d_diff_head_sizes_attn_mask  r   z7Attention.export_attention_3d_diff_head_sizes_attn_maskc               	   C  r   )Nr   r   r
   r         @r   r   r   r   r   r   r   r   r   r   r   r   test_attention_3d_softcapr   r   r   r   r   r*   r*   r+   export_attention_3d_softcap  r   z%Attention.export_attention_3d_softcapc               	   C  r   )Nr   r   r
   r   r   r   r   r   r   r   r   r   test_attention_3d_gqa_softcapr   r   r   r   r   r*   r*   r+   export_attention_3d_gqa_softcap  r   z)Attention.export_attention_3d_gqa_softcapc               	   C  r   )Nr   r   r
   r   r   r   r   r   r   r   r   r   *test_attention_3d_diff_heads_sizes_softcapr   r   r   r   r   r*   r*   r+   +export_attention_3d_diff_head_sizes_softcap
  r   z5Attention.export_attention_3d_diff_head_sizes_softcapc               
   C  s  d\} }t jjdg dg d| |d}d}tjddd	tj}tjdd
d	tj}tjdd
d	tj}tjdd
| tj}tjdd|dtj}tjdd|dtj}	t||||||	| |d\}
}}}t	|||||||	g|
||gdt j
ddgd d S )Nr   r   ro   rr   r   ru   r   r   r   r   r   r   rT   rp   rq   r   r   'test_attention_3d_with_past_and_presentr   r   r   r   r   r   r(   ry   r   r   r   rT   rp   rq   r   rs   rt   r)   r*   r*   r+   )export_attention_3d_with_past_and_present+  @   
z3Attention.export_attention_3d_with_past_and_presentc               
   C  s  d\} }t jjdg dg d| |d}d}tjddd	tj}tjdd
dtj}tjdd
dtj}tjdd
| tj}tjdd|dtj}tjdd|dtj}	t||||||	| |d\}
}}}t	|||||||	g|
||gdt j
ddgd d S )Nr   r   ro   rr   r   ru   r   r   r   r   r   r   r   r   +test_attention_3d_gqa_with_past_and_presentr   r   r   r   r   r*   r*   r+   -export_attention_3d_gqa_with_past_and_presentQ  r   z7Attention.export_attention_3d_gqa_with_past_and_presentc               
   C  s  d\} }t jjdg dg d| |d}d}tjddd	tj}tjdd
d	tj}tjdd
dtj}tjdd
| tj}tjdd|dtj}tjdd|dtj}	t||||||	| |d\}
}}}t	|||||||	g|
||gdt j
ddgd d S )Nr   r   ro   rr   r   ru   r   r   r   r   r   r   r   r5   r   2test_attention_3d_diff_heads_with_past_and_presentr   r   r   r   r   r*   r*   r+   9export_attention_3d_diff_head_sizes_with_past_and_presentw  r   zCAttention.export_attention_3d_diff_head_sizes_with_past_and_presentc               
   C  s  d\} }t jjdg dg d| |d}d}tjddd	tj}tjdd
d	tj}tjdd
d	tj}tjdd
| tj}tjdd|dtj}tjdd|dtj}	t||||||	| |d\}
}}}t	|||||||	g|
|||gdt j
ddgd d S )Nr   r   ro   r   r   ru   r   r   r   r   r   r   r   1test_attention_3d_with_past_and_present_qk_matmulr   r   r   r   r   r   r(   ry   r   r   r   rT   rp   rq   r   rs   rt   r   r*   r*   r+   3export_attention_3d_with_past_and_present_qk_matmul  s@   

z=Attention.export_attention_3d_with_past_and_present_qk_matmulc                  C  s  d\} }t jjdg dg d| |dd}d}tjdd	d
tj}tjddd
tj}tjddd
tj}tjd	d| tj}tjdd|dtj}tjdd|dtj}	t||||||	| |dd	\}
}}}t	|||||||	g|
|||gdt j
ddgd d S )Nr   r   ro   r   rE   r   r   r   r   r   ru   r   r   r   r   r   r   rT   rp   rq   r   r   r   6test_attention_3d_with_past_and_present_qk_matmul_biasr   r   r   r   r   r*   r*   r+   8export_attention_3d_with_past_and_present_qk_matmul_bias  D   	

zBAttention.export_attention_3d_with_past_and_present_qk_matmul_biasc                  C  s  d\} }t jjdg dg d| |ddd}d}tjdd	d
tj}tjddd
tj}tjddd
tj}tjd	d| tj}tjdd|dtj}tjdd|dtj}	t||||||	| |ddd
\}
}}}t	|||||||	g|
|||gdt j
ddgd d S )Nr   r   ro   r   r   r   )r   r   r   r   r   r   ru   r   r   r   r   r   )rT   rp   rq   r   r   r   r   9test_attention_3d_with_past_and_present_qk_matmul_softcapr   r   r   r   r   r*   r*   r+   ;export_attention_3d_with_past_and_present_qk_matmul_softcap  sH   


zEAttention.export_attention_3d_with_past_and_present_qk_matmul_softcapc                  C  s  d\} }t jjdg dg d| |dd}d}tjdd	d
tj}tjddd
tj}tjddd
tj}tjd	d| tj}tjdd|dtj}tjdd|dtj}	t||||||	| |dd	\}
}}}t	|||||||	g|
|||gdt j
ddgd d S )Nr   r   ro   r   r   r   ru   r   r   r   r   r   r   9test_attention_3d_with_past_and_present_qk_matmul_softmaxr   r   r   r   r   r*   r*   r+   ;export_attention_3d_with_past_and_present_qk_matmul_softmax  r   zEAttention.export_attention_3d_with_past_and_present_qk_matmul_softmaxc               	   C  s  d\} }t jjdg ddg| |d}d}d}d}d}| | }|| }tj|||ftjd	}	t| D ]}
|
| }|| }t|
d |	d
dd||f< q1tj|||ftjd	d }tj|||ftjd	d }t	|	||| |d\}}}}t
||	||g|gdt jddgd dS )aa  Test case to verify correct 3D to 4D transpose behavior.

        This test verifies that 3D inputs are correctly reshaped and transposed
        according to the ONNX specification:
        [batch_size, seq_length, hidden_size] ->
        [batch_size, seq_length, num_heads, head_size] ->
        [batch_size, num_heads, seq_length, head_size]
        r   r   r
   r   r   rE   r   r   dtyper   Ng?r   (test_attention_3d_transpose_verificationr   r   r   )r   r   r    r!   zerosr%   rangefloatonesr   r   r&   )r   r   r(   
batch_sizeq_seq_lengthkv_seq_length	head_sizeq_hidden_sizekv_hidden_sizer   head	start_idxend_idxr   r   r   r)   r*   r*   r+   *export_attention_3d_transpose_verification=  sF   
	 
z4Attention.export_attention_3d_transpose_verificationc               	   C  s   t jjdg ddgd} tjddddtj}tjddd	dtj}tjddd	d
tj}tjddddtj}tjddgtj	d}t
|||||d\}}}}t| |||||g|gdt jddgd d S )Nr   )r   r   r   rT   r   r   nonpad_kv_seqlenr   r   r   r   r   r   r   r5   r  )rT   r  -test_attention_4d_diff_heads_mask4d_padded_kvr   r   r   )r   r   r    r!   r"   r#   r$   r%   arrayint64r   r   r&   )r(   r   r   r   rT   r  r   r)   r*   r*   r+   /export_attention_4d_diff_heads_mask4d_padded_kvt  s0   
z9Attention.export_attention_4d_diff_heads_mask4d_padded_kvN)r   r   )B__name__
__module____qualname__staticmethodr,   r1   r4   r7   r>   rA   rC   rJ   rN   rQ   rX   r[   r_   rb   rd   rh   rj   rl   rn   rz   r~   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r  r  r*   r*   r*   r+   r      s          """$$ !!!         %%%%')'6r   )
__future__r   numpyr!   r   onnx.backend.test.case.baser   onnx.backend.test.case.noder   onnx.reference.ops.op_attentionr   r   r*   r*   r*   r+   <module>   s   