+
    9i                         ^ RI t ^ RIHt ^ RIHt ^ RIHt ^ RIHt ^ RI	H
t
 ^ RIHt ] P                  ! ]4      t ! R R	]4      tR# )
    N)FusionMultiHeadAttentionSam2)FusionLayerNormalizationNCHWFusionOptions)is_installed)
ModelProto)BertOnnxModelc                      a a ] tR t^t oRV3R lV 3R llltR tV 3R ltRV3R lR lltRV3R lR lltRV3R	 lR
 llt	R t
RtVtV ;t# )Sam2OnnxModelc                ,   < V ^8  d   QhRS[ RS[RS[/# )   model	num_headshidden_size)r   int)format__classdict__s   "f/var/www/html/photoedit/myenv/lib/python3.14/site-packages/onnxruntime/transformers/onnx_model_sam2.py__annotate__Sam2OnnxModel.__annotate__   s'     
N 
Nj 
NS 
N3 
N    c                r   < V^ 8X  d   V^ 8X  g   V^ 8  d   W2,          ^ 8X  g   Q h\         SV `  WVR7       R# )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   N)super__init__)selfr   r   r   	__class__s   &&&&r   r   Sam2OnnxModel.__init__   s<     Q;!#3Q;KbfgKghhMr   c                F    V P                  4        V P                  4        R # N)prune_graphremove_unused_constant)r   s   &r   postprocessSam2OnnxModel.postprocess   s    ##%r   c                Z   < \         SV `  4        \        V 4      pVP                  4        R # r   )r   fuse_layer_normr   apply)r   fusionr   s   & r   r%   Sam2OnnxModel.fuse_layer_norm#   s!    !-d3r   c                .   < V ^8  d   QhRS[ R,          /# r   optionsNr   )r   r   s   "r   r   r   )   s      1E r   c                f    \        W P                  V P                  4      pVP                  4        R # r   )r   r   r   r&   )r   r+   
mha_fusions   && r   fuse_multi_head_attention'Sam2OnnxModel.fuse_multi_head_attention)   s$    1$8H8H$..Y
r   c                4   < V ^8  d   QhRS[ R,          RS[/# )r   r+   Nadd_dynamic_axes)r   bool)r   r   s   "r   r   r   -   s      * * 4 *t *r   c                F   \        R 4      '       dU   ^ RIp^ RIHp V! 4       ;_uu_ 4        ^pVP                  \	        V4      ^ RR7      pV P                  W4       RRR4       R# \        P                  R4       V P                  VR4       R#   + '       g   i     R# ; i)tqdmN)logging_redirect_tqdmzsam2 fusion)initialdescz<tqdm is not installed. Run optimization without progress bar)r   r4   tqdm.contrib.loggingr5   range	_optimizeloggerinfo)r   r+   r1   r4   r5   stepsprogress_bars   &&&    r   optimizeSam2OnnxModel.optimize-   sv    B&((#yyuq}yUw5 )(
 KKVWNN7D) )((s   1BB 	c                .   < V ^8  d   QhRS[ R,          /# r*   r   )r   r   s   "r   r   r   :   s     =B =B!5 =Br   c                   Ve#   VP                   '       g   V P                  4        V P                  P                  4        V'       d   VP	                  ^4       V P                  P                  4        V'       d   VP	                  ^4       Ve   VP                  '       d   V P                  4        V'       d   VP	                  ^4       Ve   VP                  '       d   V P                  4        V'       d   VP	                  ^4       V P                  4        V'       d   VP	                  ^4       Ve   VP                  '       d   V P                  V4       V'       d   VP	                  ^4       Ve   VP                  '       d   V P                  4        V'       d   VP	                  ^4       V P                  4        V'       d   VP	                  ^4       V P                  P!                  4        V'       d   VP	                  ^4       Ve   VP"                  '       d   V P%                  4        V'       d   VP	                  ^4       Ve#   VP&                  '       d   V P)                  4        V'       d   VP	                  ^4       V P+                  4        V'       d   VP	                  ^4       \,        P/                  RV P1                  4        24       R # )Nz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"   r;   r<   get_opset_version)r   r+   r>   s   &&&r   r:   Sam2OnnxModel._optimize:   s   )G)G)G((*

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

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

//1"O C C C..0"7#D#D#D##%""od&<&<&>%?@Ar   c                    / p. ROpV F!  pV P                  V4      p\        V4      W&   K#  	  \        P                  RV 24       V# )z(
Returns node count of fused operators.
zOptimized operators:)MultiHeadAttentionLayerNormalizationSkipLayerNormalization)get_nodes_by_op_typelenr;   r<   )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   r"   r%   r.   r?   r:   rb   __static_attributes____classdictcell____classcell__)r   r   s   @@r   r   r      sF     
N 
N& * *=B =B~ r   r   )loggingfusion_attention_sam2r   fusion_layernormr   fusion_optionsr   import_utilsr   onnxr   onnx_model_bertr	   	getLoggerre   r;   r   rd   r   r   <module>rt      s:     > 9 ( %  )			8	$wM wr   