
    fPi5                     x    d dl mZ d dlmZmZ d dlmZ d dlmZ d dl	m
Z
  ee          Z G d de          ZdS )	    )	getLogger)AttentionMaskFusionAttention)AttentionMaskFormat)	NodeProto)	OnnxModelc                   V     e Zd ZdZdededef fdZdedeeef         fdZ	d	 Z
 xZS )
FusionAttentionClipzB
    Fuse Attention subgraph of Clip into one Attention node.
    modelhidden_size	num_headsc                     t          |          }t          j        |_        t	                                          ||||ddg           d S )NFSkipLayerNormalization)use_multi_head_attentionsearch_op_types)r   r   NoMaskmask_formatsuper__init__)selfr   r   r   attention_mask	__class__s        /home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/onnxruntime/transformers/fusion_attention_clip.pyr   zFusionAttentionClip.__init__   s]     'u--%8%?"%*56 	 	
 	
 	
 	
 	
    	reshape_qreturnc                 l   | j                             |dd          }|t          |j                  dk    r| j        | j        fS | j                             |j        d                   }|| j        | j        fS t          |          dk    s|d         dk    r| j        | j        fS |d         }| j                             |j        d                   }|| j        | j        fS t          |          dk    s|d         dk    r| j        | j        fS |d         }||z  }| j        dk    r?|| j        k    r4| j        r-t          	                    d| j         d	| d
           d| _        | j        dk    r?|| j        k    r4| j
        r-t          	                    d| j         d	| d
           d| _
        ||fS )zDetect num_heads and hidden_size for ONNX model from MiDaS
        Args:
            reshape_q (NodeProto): reshape node for q
        Returns:
            Tuple[int, int]: num_heads and hidden_size
        Concat   N      r      z--num_heads is z. Detected value is z. Using detected value.Fz--hidden_size is )r   match_parentleninputr   r   get_constant_valuenum_heads_warningloggerwarninghidden_size_warning)r   r   concatnum_head_valuer   head_size_value	head_sizer   s           r   get_num_heads_and_hidden_sizez1FusionAttentionClip.get_num_heads_and_hidden_size&   s    ((Ha@@>S..!33>4#333 66v|AGG!>4#333~!##~a'8A'='=>4#333"1%	*77QHH">4#3331$$(:a(?(?>4#333#A&	)+>A)t~"="=% /wwwU^wwwxxx).&aK43C$C$C' 1r(8rrkrrr   ,1(+%%r   c                    d }d }dD ]$}| j                             |d|          }||}|}%d }||j        d         }n~dD ]w}d }| j                             |d|          }	| j                             |d|          }
|	|	}n|
|
}|H| j                             |d|d          }|h|j        d         }|} |d S | j                             |g dd	|z
  d d dddg          }|>| j                             |g d
g d          }|t
                              d           d S |d         |d         |d         }}}| j                             |g dg d          }|>| j                             |g dg d          }|t
                              d           d S |d         |d         }}d }d }g }| j                             |g dg d|          }|| j                             |ddgddg          }|| j                             |g dg d          }|	|d	         }n| j                             |g dg d          }|i| j                             |g dg d           }|	|d         }ng| j                             |g d!g d"          }|t
                              d#           d S n(t          |          d	k    sJ d	|d         z
  }|d         }|d         }| j                             |g d$g d%          }|G| j                             |g dg d&          }|t
                              d'           d S |d	         }n|d         }|d         |d         }}| j                             |g d(g d)          }|>| j                             |g dg d          }|t
                              d*           d S |d         |d         }}|j        d         |k    s"|j        d         |k    s|j        d         |k    rt
                              d+           d S | 	                    |          \  }} |dk    s| dk    rt
                              d,           d S |}!d-}"d }#d }$||j        d	         d.k    r|j        d	         }"n| j                             |g d/g d0          }%|%|j        d	         }"ng| j                             |g d1|dddddg          }#| j                             |g d2|ddddg          }$|#|$t
                              d3           d S | 
                    d |||||||| ||!j        d         |"d |#d up|$d u4          }&|&t
                              d5           d S | j                            |&           | j        | j        |&j        <   | j                            |!|g           |                     |&j                   d6| _        d S )7N)r   r   r   r   )r   r   AddLayerNormalizationF)r1   MatMulReshape	Transposer4   r3   r   )r1   r3   r4   r5   r3   )r   Nr   r   r   z(fuse_attention: failed to match qkv pathr!   r"   )r4   r5   r4   r1   r3   )r   r   r   r   N)r5   r4   r1   r3   )r   r   r   Nz&fuse_attention: failed to match v path)Softmaxr4   r1   r4   r3   )r   r   r   Nr   )return_indicer8   r3   )r8   r1   Mulr3   )r   r   r   r   )r8   r:   r3   )r   r   r   )Castr;   r8   r1   r:   r3   )r   r   r   r   r   r   )r;   r;   r8   r:   r3   )r   r   r   r   r   z'fuse_attention: failed to match qk path)r4   r5   r4   r:   r1   r3   )r   r   r   r   NN)r   r   r   Nz&fuse_attention: failed to match q path)r5   r4   r5   r4   r1   r3   )r   r   r   r   r   Nz&fuse_attention: failed to match k pathz>fuse_attention: expect to have same input to q, k and v matmulz9fuse_attention: failed to detect num_heads or hidden_size r   )	WhereSubr;   Expand	Unsqueezer@   r4   r4   r;   )	r   r!   r   r   r   r   r   r   r   )r   r?   r@   r@   r=   Less)r?   r@   r@   r=   rA   z4fuse_attention: failed to match causal mask subgraph)
mask_indexq_matmulk_matmulv_matmulq_addk_addv_addr   r   first_inputoutput
add_qk_strscalecausalz+fuse_attention: failed to create fused nodeT)r   r#   rJ   find_first_child_by_typematch_parent_pathr(   debugr$   r%   r/   create_attention_nodenodes_to_addappendthis_graph_namenode_name_to_graph_namenamenodes_to_removeextendincrease_counterop_typeprune_graph)'r   normalize_nodeinput_name_to_nodesoutput_name_to_nodeskip_input_indexnode_before_layer_normiparent
root_inputnode_before_layer_norm_1node_before_layer_norm_2child	qkv_nodesreshape_qkvtranspose_qkv
matmul_qkvv_nodesadd_vmatmul_vcausal_mask_input_indexadd_maskadd_mask_indicesqk_nodes	matmul_qkq_nodesr   add_qmatmul_qk_nodesadd_kmatmul_kr   r   attention_last_nodeadd_qkcausal_mask_nodes_1causal_mask_nodes_2add_qk_nodesnew_nodes'                                          r   fusezFusionAttentionClip.fuseT   s   !% 	0 	0AZ,,^=UWXYYF!#$ )/&
!-/6q9JJ  ( ()-&+/:+B+B>SXZ[+\+\(+/:+B+B>Sgij+k+k(+7 .F**-9 .F*)1
;;*('	  ="\!_
#$ 'J00JJJ!!4q!Q7
 
	
 
44CCC""" I
  GHHHaLaLbM %/] *..@@@
 

 ?j22EEE G EFFF!"+wr{x"&://>>>*	 0 
 
 z33H%A H
 :77
DgDgDgiuiuiuvv''{HH  $z;;JHdHdHdfofofoppH'#':#?#?&OOO...$ $
 $/'/{HH (,z'C'C * L L L /( (H
  (/ &-V W W W &'((A----&'*:1*=&=#{HRL	*..GGG$$$
 

 ?j22DDDooo G EFFF
II
I!"+wr{x*..MMM!!!
 

 ?j22DDDooo G EFFF!"+wr{x>!
**hnQ.?:.M.MQYQ_`aQbfpQpQpLLYZZZF!%!C!CI!N!N	;>>[A--LLTUUUF)""~a $444!*  $z;;
 
 
 0//     +%^A.FF +/**F*F WWW0!Q1a@+ +' +/**F*F MMM0!Q1=+ +' +27J7R%[\\\--#"&-a0't3Y9LTX9X . 
 
  LLFGGGF  ***6:6J$X]3##%8-$HIIIh.///  r   )__name__
__module____qualname____doc__r   intr   r   tupler/   r   __classcell__)r   s   @r   r
   r
      s         

 
 	
 
 
 
 
 
$,&y ,&U3PS8_ ,& ,& ,& ,&\A  A  A  A  A  A  A r   r
   N)loggingr   fusion_attentionr   r   fusion_optionsr   onnxr   
onnx_modelr   r   r(   r
    r   r   <module>r      s   
       ; ; ; ; ; ; ; ; . . . . . .                  	8		F  F  F  F  F / F  F  F  F  F r   