o
    wio                     @  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                   @  s  e Zd ZedbddZedbddZedbddZedbd	d
ZedbddZedbddZ	edbddZ
edbddZedbddZedbddZedbddZedbddZedbddZedbddZedbdd Zedbd!d"Zedbd#d$Zedbd%d&Zedbd'd(Zedbd)d*Zedbd+d,Zedbd-d.Zedbd/d0Zedbd1d2Zedbd3d4Zedbd5d6Zedbd7d8Zedbd9d:Zedbd;d<Z edbd=d>Z!edbd?d@Z"edbdAdBZ#edbdCdDZ$edbdEdFZ%edbdGdHZ&edbdIdJZ'edbdKdLZ(edbdMdNZ)edbdOdPZ*edbdQdRZ+edbdSdTZ,edbdUdVZ-edbdWdXZ.edbdYdZZ/edbd[d\Z0edbd]d^Z1edbd_d`Z2daS )c	AttentionreturnNonec                  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
d d S )Nr   QKVYinputsoutputs               test_attention_4dr   r   name
onnxhelper	make_nodenprandomrandastypefloat32r   r   noder
   r   r   r   _ r%   b/home/ubuntu/sommelier/.venv/lib/python3.10/site-packages/onnx/backend/test/case/node/attention.pyexport_attention      
zAttention.export_attentionc                  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d d S )Nr   r	   r   r   r   	   r   r   r   r   test_attention_4d_gqar   r   r"   r%   r%   r&   export_attention_gqa    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d d S )Nr   r	   r   r   r   r   r   r   r   
   "test_attention_4d_diff_heads_sizesr   r   r"   r%   r%   r&    export_attention_diff_head_sizes1   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d d S )N{Gz?r   r	   r   r   r   scaler   r   r   r   r   r1   test_attention_4d_scaledr   r   r1   r#   r
   r   r   r   r$   r%   r%   r&   export_attention_scaledB   "   
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d d S )Nr/   r   r	   r   r0   r   r)   r   r   r   r   r2   test_attention_4d_gqa_scaledr   r   r4   r%   r%   r&   export_attention_gqa_scaledY   r6   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d d S )Nr/   r   r	   r   r0   r   r   r   r   r   r,   r2   )test_attention_4d_diff_heads_sizes_scaledr   r   r4   r%   r%   r&   'export_attention_diff_head_sizes_scaledp   r6   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d d S )Nr   r	   r      r   r   	is_causalr   r   r   r   r   r>   test_attention_4d_causalr   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d d S )Nr   r	   r   r<   r=   r   r)   r   r   r   r   r?   test_attention_4d_gqa_causalr   r   r"   r%   r%   r&   export_attention_gqa_causal   rB   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d d S )Nr   r	   r   r<   r=   r   r   r   r   r   r,   r?   )test_attention_4d_diff_heads_sizes_causalr   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d d S )Nr   r
   r   r   	attn_maskr   r   r   r   r   r   r   rK   test_attention_4d_attn_maskr   r   r#   r
   r   r   rK   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	tj}t	||||d
\}}}}t
| ||||g|gdd d S )Nr   rJ   r   r   r   r   r   r   r   rL    test_attention_4d_attn_mask_boolr   )r   r   r   r   r   r   r    r!   boolr   r   rN   r%   r%   r&   export_attention_attn_mask_bool   rP   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
tj}t||||d\}}}}t	| ||||g|gdd d S )Nr   rJ   r   r   r   r)   r   r   r   r   rL   test_attention_4d_gqa_attn_maskr   r   rN   r%   r%   r&   export_attention_gqa_attn_mask  rP   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d d S )Nr   rJ   r   r   r   r   r   r   r   r,   rL   ,test_attention_4d_diff_heads_sizes_attn_maskr   r   rN   r%   r%   r&   *export_attention_diff_head_sizes_attn_mask  rP   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d d S )Nr   r
   r   r   rK   past_key
past_valuer   present_keypresent_valuer      r   r   r   r   r   rK   rY   rZ   'test_attention_4d_with_past_and_presentr   r   r#   past_sequence_lengthr
   r   r   rK   rY   rZ   r   r\   r]   r$   r%   r%   r&   &export_attention_with_past_and_present:  4   	
z0Attention.export_attention_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| tj}tjdd
|d	tj}tjdd
|d	tj}t||||||d\}}	}
}t	| ||||||g||	|
gdd d S )Nr   rX   r[   r   r^   r   r)   r   r   r   r   r_   +test_attention_4d_gqa_with_past_and_presentr   r   ra   r%   r%   r&   *export_attention_gqa_with_past_and_presentZ  rd   z4Attention.export_attention_gqa_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
| tj}tjdd|d	tj}tjdd|dtj}t||||||d\}}	}
}t	| ||||||g||	|
gdd d S )Nr   rX   r[   r   r^   r   r   r   r   r   r,   r_   2test_attention_4d_diff_heads_with_past_and_presentr   r   ra   r%   r%   r&   6export_attention_diff_head_sizes_with_past_and_presentz  rd   z@Attention.export_attention_diff_head_sizes_with_past_and_presentc                  C  r;   )Nr   r	   r          @r   r   softcapr   r   r   r   r   rk   test_attention_4d_softcapr   r   r"   r%   r%   r&   export_attention_softcap  rB   z"Attention.export_attention_softcapc                  C  rC   )Nr   r	   r   ri   rj   r   r)   r   r   r   r   rl   test_attention_4d_gqa_softcapr   r   r"   r%   r%   r&   export_attention_gqa_softcap  rB   z&Attention.export_attention_gqa_softcapc                  C  rF   )Nr   r	   r   ri   rj   r   r   r   r   r   r,   rl   *test_attention_4d_diff_heads_sizes_softcapr   r   r"   r%   r%   r&   (export_attention_diff_head_sizes_softcap  rI   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
d d S )Nr   r	   r    rt   qk_matmul_outputr   r   r   r   r   r    test_attention_4d_with_qk_matmulr   r   )r#   r
   r   r   r   r$   ru   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d d S )Nr   rJ   rs   r<   r   r   qk_matmul_output_moder   r   r   r   r   rK   ry   %test_attention_4d_with_qk_matmul_biasr   r   r#   r
   r   r   rK   r   r$   ru   r%   r%   r&   $export_attention_with_qk_matmul_bias  .   

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d d S )Nr   rJ   rs   ri   r   )r   r   rk   ry   r   r   r   r   )rK   rk   ry   (test_attention_4d_with_qk_matmul_softcapr   r   r|   r%   r%   r&   'export_attention_with_qk_matmul_softcap  s2   	

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d d S )Nr   rJ   rs   r   rx   r   r   r   r   rz   (test_attention_4d_with_qk_matmul_softmaxr   r   r|   r%   r%   r&   'export_attention_with_qk_matmul_softmax2  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d d S )Nr   rX   r   r\   r]   ru   r<   rx   r^   r   r   r   r   r   )rK   rY   rZ   ry   6test_attention_4d_with_past_and_present_qk_matmul_biasr   r   r#   rb   r
   r   r   rK   rY   rZ   r   r\   r]   ru   r%   r%   r&   5export_attention_with_past_and_present_qk_matmul_biasO  s8   


z?Attention.export_attention_with_past_and_present_qk_matmul_biasc                  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d d S )Nr   rX   r   r   r^   r   r   r   r   r   r_   1test_attention_4d_with_past_and_present_qk_matmulr   r   r   r%   r%   r&   0export_attention_with_past_and_present_qk_matmulq  s4   	

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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&   export_attention_3d  0   
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d d S )Nr)   r   r   r	   r   r   r   r   H   r   r   r   test_attention_3d_gqar   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d d S )Nr   r   r	   r   r   r   r   r   r      r   "test_attention_3d_diff_heads_sizesr   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d d S )Nr/   r   r   r	   r   r   r   r1   r   r   r   r   r   r   r1   r   r   test_attention_3d_scaledr   r   	r1   r   r   r#   r
   r   r   r   r$   r%   r%   r&   export_attention_3d_scaled  6   		
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d d S )Nr/   r   r   r	   r   r   r   r   r   r   r   r   test_attention_3d_gqa_scaledr   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d d S )Nr/   r   r   r	   r   r   r   r   r   r   r   r   )test_attention_3d_diff_heads_sizes_scaledr   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d d S )Nr   r   r	   r   r<   r   r   r>   r   r   r   r   r   r   r>   r   r   test_attention_3d_causalr   r   r   r%   r%   r&   export_attention_3d_causalN  4   		
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d d S )Nr   r   r	   r   r<   r   r   r   r   r   r   r   test_attention_3d_gqa_causalr   r   r   r%   r%   r&   export_attention_3d_gqa_causaln  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d d S )Nr   r   r	   r   r<   r   r   r   r   r   r   r   )test_attention_3d_diff_heads_sizes_causalr   r   r   r%   r%   r&   *export_attention_3d_diff_head_sizes_causal  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d d S )Nr   r   rJ   r   r   r   r   r   r   rK   r   r   test_attention_3d_attn_maskr   r   	r   r   r#   r
   r   r   rK   r   r$   r%   r%   r&   export_attention_3d_attn_mask  4   	

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d d S )Nr   r   rJ   r   r   r   r   r   r   r   r   test_attention_3d_gqa_attn_maskr   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d d S )Nr   r   rJ   r   r   r   r   r   r   r   r   ,test_attention_3d_diff_heads_sizes_attn_maskr   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   rk   r   r   r   r   r   r   rk   r   r   test_attention_3d_softcapr   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&   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&   +export_attention_3d_diff_head_sizes_softcapN  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d d S )Nr   r   rX   r[   r   r^   r   r   r   r   r   r   rK   rY   rZ   r   r   'test_attention_3d_with_past_and_presentr   r   r   r   r#   rb   r
   r   r   rK   rY   rZ   r   r\   r]   r$   r%   r%   r&   )export_attention_3d_with_past_and_presentn  >   
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d d S )Nr   r   rX   r[   r   r^   r   r   r   r   r   r   r   r   +test_attention_3d_gqa_with_past_and_presentr   r   r   r%   r%   r&   -export_attention_3d_gqa_with_past_and_present  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d d S )Nr   r   rX   r[   r   r^   r   r   r   r   r   r   r   r,   r   2test_attention_3d_diff_heads_with_past_and_presentr   r   r   r%   r%   r&   9export_attention_3d_diff_head_sizes_with_past_and_present  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d d S )Nr   r   rX   r   r   r^   r   r   r   r   r   r   r   1test_attention_3d_with_past_and_present_qk_matmulr   r   r   r   r#   rb   r
   r   r   rK   rY   rZ   r   r\   r]   ru   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d d S )Nr   r   rX   r   r<   r   r   r   r   ry   r^   r   r   r   r   r   r   rK   rY   rZ   r   r   ry   6test_attention_3d_with_past_and_present_qk_matmul_biasr   r   r   r%   r%   r&   8export_attention_3d_with_past_and_present_qk_matmul_bias  B   	

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d d S )Nr   r   rX   r   ri   r   )r   r   r   r   rk   ry   r^   r   r   r   r   r   )rK   rY   rZ   r   r   rk   ry   9test_attention_3d_with_past_and_present_qk_matmul_softcapr   r   r   r%   r%   r&   ;export_attention_3d_with_past_and_present_qk_matmul_softcap)  sF   


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d d S )Nr   r   rX   r   r   r   r^   r   r   r   r   r   r   9test_attention_3d_with_past_and_present_qk_matmul_softmaxr   r   r   r%   r%   r&   ;export_attention_3d_with_past_and_present_qk_matmul_softmaxR  r   zEAttention.export_attention_3d_with_past_and_present_qk_matmul_softmaxN)r   r   )3__name__
__module____qualname__staticmethodr'   r+   r.   r5   r8   r:   rA   rE   rH   rO   rS   rU   rW   rc   rf   rh   rn   rp   rr   rw   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    !   $$$$&(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   