
    h                         S SK r S SKJr  S SKJr  S SKJr  S SKJr  S SK	J
r
  S SKJr  \ R                  " \5      r " S S	\5      rg)
    N)FusionMultiHeadAttentionSam2)FusionLayerNormalizationNCHW)FusionOptions)is_installed)
ModelProto)BertOnnxModelc                      ^  \ rS rSrSS\S\S\4U 4S jjjrS rU 4S jrSS	\	S-  4S
 jjr
SS	\	S-  S\4S jjrSS	\	S-  4S jjrS rSrU =r$ )Sam2OnnxModel   model	num_headshidden_sizec                 Z   > US:X  a  US:X  d  US:  a  X2-  S:X  d   e[         TU ]  XUS9  g)a  Initialize SAM2 ONNX Model.

Args:
    model (ModelProto): the ONNX model
    num_heads (int, optional): number of attention heads. Defaults to 0 (detect the parameter automatically).
    hidden_size (int, optional): hidden dimension. Defaults to 0 (detect the parameter automatically).
r   )r   r   N)super__init__)selfr   r   r   	__class__s       b/var/www/fran/franai/venv/lib/python3.13/site-packages/onnxruntime/transformers/onnx_model_sam2.pyr   Sam2OnnxModel.__init__   s<     Q;!#3Q;KbfgKghhM    c                 D    U R                  5         U R                  5         g N)prune_graphremove_unused_constant)r   s    r   postprocessSam2OnnxModel.postprocess   s    ##%r   c                 X   > [         TU ]  5         [        U 5      nUR                  5         g r   )r   fuse_layer_normr   apply)r   fusionr   s     r   r   Sam2OnnxModel.fuse_layer_norm#   s!    !-d3r   Noptionsc                 d    [        X R                  U R                  5      nUR                  5         g r   )r   r   r   r   )r   r"   
mha_fusions      r   fuse_multi_head_attention'Sam2OnnxModel.fuse_multi_head_attention)   s$    1$8H8H$..Y
r   add_dynamic_axesc                 $   [        S5      (       aH  SS KnSSKJn  U" 5          SnUR                  [	        U5      SSS9nU R                  X5        S S S 5        g [        R                  S5        U R                  US 5        g ! , (       d  f       g = f)Ntqdmr   )logging_redirect_tqdm   zsam2 fusion)initialdescz<tqdm is not installed. Run optimization without progress bar)r   r)   tqdm.contrib.loggingr*   range	_optimizeloggerinfo)r   r"   r'   r)   r*   stepsprogress_bars          r   optimizeSam2OnnxModel.optimize-   sp    B&(#yyuq}yUw5 )(
 KKVWNN7D) )(s   .B
Bc                    Ub!  UR                   (       d  U R                  5         U R                  R                  5         U(       a  UR	                  S5        U R                  R                  5         U(       a  UR	                  S5        Ub  UR                  (       a  U R                  5         U(       a  UR	                  S5        Ub  UR                  (       a  U R                  5         U(       a  UR	                  S5        U R                  5         U(       a  UR	                  S5        Ub  UR                  (       a  U R                  U5        U(       a  UR	                  S5        Ub  UR                  (       a  U R                  5         U(       a  UR	                  S5        U R                  5         U(       a  UR	                  S5        U R                  R!                  5         U(       a  UR	                  S5        Ub  UR"                  (       a  U R%                  5         U(       a  UR	                  S5        Ub!  UR&                  (       a  U R)                  5         U(       a  UR	                  S5        U R+                  5         U(       a  UR	                  S5        [,        R/                  SU R1                  5        35        g )N   zopset version: )enable_shape_inferencedisable_shape_inferenceutilsremove_identity_nodesupdateremove_useless_cast_nodesenable_layer_normr   enable_gelu	fuse_gelufuse_reshapeenable_attentionr%   enable_skip_layer_normfuse_skip_layer_norm
fuse_shaperemove_useless_reshape_nodesenable_bias_skip_layer_normfuse_add_bias_skip_layer_normenable_gelu_approximationgelu_approximationr   r1   r2   get_opset_version)r   r"   r4   s      r   r0   Sam2OnnxModel._optimize:   s   )G)G((*

((*" 	

,,."O 9 9  ""O 3 3NN""O 8 8**73"O > >%%'"" 	

//1"O C C..0"7#D#D##%""od&<&<&>%?@Ar   c                     0 n/ SQnU H!  nU R                  U5      n[        U5      X'   M#     [        R                  SU 35        U$ )z(
Returns node count of fused operators.
)MultiHeadAttentionLayerNormalizationSkipLayerNormalizationzOptimized operators:)get_nodes_by_op_typelenr1   r2   )r   op_countopsopnodess        r   get_fused_operator_statistics+Sam2OnnxModel.get_fused_operator_statisticsy   sR     
 B--b1Eu:HL  	*8*56r    )r   r   r   )NF)NN)__name__
__module____qualname____firstlineno__r   intr   r   r   r   r%   boolr5   r0   rX   __static_attributes____classcell__)r   s   @r   r
   r
      sx    
Nj 
NS 
N3 
N 
N&1E * 4 *t *=B!5 =B~ r   r
   )loggingfusion_attention_sam2r   fusion_layernormr   fusion_optionsr   import_utilsr   onnxr   onnx_model_bertr   	getLoggerr[   r1   r
   rZ   r   r   <module>rk      s:     > 9 ( %  )			8	$wM wr   