o
    Xi                     @  s   d dl mZ d dlmZmZ d dlZd dlZd dlm	Z	m
Z
 ejjejjgZeeejf ZG dd de
jZe
e gZe	eZdS )    )annotations)SequenceUnionN)_fusion_utilspatternc                   @  s&   e Zd Zdd Zd
ddZdd Zd	S )FuseBiasMHAc                 C  s   t j||||gdddgd}t j||||gdddgd}t j||||gdddgd}|j|||d d t jdddt jd	ddt jd
dd|t jdddddS )N
has_q_biasTF)tag_var
tag_values
has_k_bias
has_v_biasmask)can_match_nonepast_key
past_valuescalecom.microsoft	num_headsr   _domain)r   OrValueAddMultiHeadAttentionVarAttrVar)selfopquery_matmul
key_matmulvalue_matmulq_biask_biasv_biasr   r   r   r   	query_BSDkey_BSD	value_BSD r&   \/home/ubuntu/.local/lib/python3.10/site-packages/onnxscript/rewriter/ort_fusions/mha_bias.pyr      s8   zFuseBiasMHA.patternreturnpattern.MatchResultc                   s6  t  }	|s|s|s|	dS i  _d fdd	}
|jtvr$|	d
|S |jtvr/|	d|S |jtvr:|	d|S |
|g drK|	d| d|S |
|g dr\|	d| d|S |
|g drm|	d| d|S  jd _ jd _ jd _	t
 jtrt
 jtrt
 j	ts|	dS |	S )Nz)None of query, key, or value have a bias.valir.ValuedimsSequence[str]r(   boolc                   s   t  j| | S )N)r   check_shape_boolbindings)r*   r,   r   r&   r'   no_matchP   s   z#FuseBiasMHA.check.<locals>.no_matchz%Query is not a float or float16 type.z#Key is not a float or float16 type.z%Value is not a float or float16 type.)BSDzShape mismatch: z3 does not match expected dimensions ['B', 'S', 'D'])r3   SkvDkz6 does not match expected dimensions ['B', 'Skv', 'Dk'])r3   r6   Dvz6 does not match expected dimensions ['B', 'Skv', 'Dv']r5   r7   r8   z>Could not determine the hidden sizes of query, key, and value.)r*   r+   r,   r-   r(   r.   )r   MatchResultfailr0   dtypevalid_float_typesgetDh_qDh_kDh_v
isinstanceint)r   contextr   r   r   r   r   r   _check_resultr2   r&   r1   r'   check>   sN   









zFuseBiasMHA.checkc                 K  s   |d u r|j ttj| jf|j dd}|d u r.|j ttj| jf|j dd}|d u rE|j ttj| jf|j dd}|j	|||dd}|j
||||d ||	|
||ddS )N)r;   )valuer   )axisr   r   )Constantirtensornumpyzerosr>   r;   r?   r@   Concatr   )r   r   r   r   r   r    r!   r"   r   r   r   r   r   rD   biasr&   r&   r'   rewritey   s4   zFuseBiasMHA.rewriteN)r(   r)   )__name__
__module____qualname__r   rF   rP   r&   r&   r&   r'   r      s    
,;r   )
__future__r   typingr   r   rL   onnx_irrJ   onnxscript.rewriterr   r   DataTypeFLOATFLOAT16r<   rB   SymbolicDimDimRewriteRuleClassBaser   RewriteRuleSetrulemha_bias_rulesapply_fusion_rulesfuse_mha_biasr&   r&   r&   r'   <module>   s    