
    2Vh9                        d dl mc mZ d dlmZ d dlmZ d dlm	Z	 d dlm
Z
 d Zd Zd Zd	 Zd
 Zd Zd Zd Zd,dZd Zd Zd-dZ	 	 d.dZd/dZd0dZd0dZ	 	 	 d1dZ	 d0dZd Zd Zd Zd Z d Z!d Z"	 	 	 	 d2dZ#	 	 	 	 d2dZ$	 	 	 	 d2d Z%	 	 	 	 	 d3d!Z&d4d"Z'd4d#Z(d5d$Z)d5d%Z*d6d&Z+d7d'Z,	 d8d(Z-d9d)Z.	 	 	 	 	 d:d*Z/d+ Z0y);    N)Type)backend)OpenVINOKerasTensor)get_ov_outputc                 r    t        |       } t        t        j                  |       j	                  d            S Nr   )r   r   ov_opsetreluoutputxs    M/home/dcms/DCMS/lib/python3.12/site-packages/keras/src/backend/openvino/nn.pyr
   r
   	   +    aAx}}Q/66q9::    c                 v    t        |       } t        t        j                  | dd      j	                  d            S )Ng        g      @r   )r   r   r	   clampr   r   s    r   relu6r      s/    aAx~~ac:AA!DEEr   c                 r    t        |       } t        t        j                  |       j	                  d            S r   )r   r   r	   sigmoidr   r   s    r   r   r      s-    aAx//299!<==r   c                 r    t        |       } t        t        j                  |       j	                  d            S r   )r   r   r	   tanhr   r   s    r   r   r      r   r   c                 r    t        |       } t        t        j                  |       j	                  d            S r   )r   r   r	   softplusr   r   s    r   r   r      -    aAx003::1=>>r   c                 r    t        |       } t        t        j                  |       j	                  d            S r   )r   r   r	   softsignr   r   s    r   r   r   "   r   r   c                     t        |       } t        t        j                  | t        j                  |             j                  d            S r   )r   r   r	   multiplyr   r   r   s    r   silur   '   s?    aA!X--a0188; r   c                     t        d      )Nz4`log_sigmoid` is not supported with openvino backendNotImplementedErrorr   s    r   log_sigmoidr#   .   s    
> r   c                     t        |       } t        j                  || j                               j	                  d      }t        j
                  | |      j	                  d      }t        |      S r   )r   r	   constantget_element_typer   prelur   )r   negative_slopeslope_const
leaky_relus       r   r*   r*   4   s^    aA##**,fQi  ;/66q9Jz**r   c                     t        |       } t        d| j                               }t        d| j                               }t        t        j                  | ||      j                  d            S )NgUUUUUU?g      ?r   )r   r&   r   r	   hard_sigmoidr   )r   alphabetas      r   r,   r,   =   s[    aA)Q%7%7%9:Ea0023Dx44QtDKKANOOr   c                     t        t        |             }t        |       } t        t        j                  | |      j                  d            S r   )r   r,   r   r	   r   r   )r   hard_sigmoid_outputs     r   	hard_silur1   D   sD    'Q8aA!0188; r   c                 t    t        |       } t        t        j                  | |      j	                  d            S r   )r   r   r	   elur   )r   r-   s     r   r3   r3   L   s-    aAx||Au5<<Q?@@r   c                     t        |       } t        || j                               }t        || j                               }t        t        j                  | ||      j                  d            S r   )r   r&   r   r	   selur   )r   r-   scales      r   r5   r5   Q   s[    
 	aA%!3!3!56E%!3!3!56Ex}}Qu=DDQGHHr   c                     t        |       } d}|rd}t        t        j                  | |      j	                  d            S )Nerfr   r   )r   r   r	   gelur   )r   approximateapproximate_modes      r   r9   r9   \   s=    aA!x}}Q0@AHHKLLr   c                    t        |       } |t        j                  |       }t        j                  dgt        j
                        j                  d      }t        j                  | |d      j                  d      }t        j                  |d      j                  d      }t        t        j                  ||d      j                  d            S t        t        j                  | |      j                  d            S Nr   F)
r   r	   shape_ofr%   r   i32r   reshapesoftmaxr   )r   axisx_shapeflatten_shape	flatten_x	softmax_xs         r   rB   rB   d   s    aA|##A& ))2$9@@C$$Qu=DDQG	$$Y299!<	"Y7>>qA
 	
 x//48??BCCr   c                    t        |       } |t        j                  |       }t        j                  dgt        j
                        j                  d      }t        j                  | |d      j                  d      }t        j                  |d      j                  d      }t        t        j                  ||d      j                  d            S t        t        j                  | |      j                  d            S r=   )
r   r	   r?   r%   r   r@   r   rA   log_softmaxr   )r   rC   rD   rE   rF   log_softmax_xs         r   rI   rI   q   s    aA|##A& ))2$9@@C$$Qu=DDQG	 ,,Y:AA!D"]GU;BB1E
 	
 x33At<CCAFGGr   c                     t        d      )Nz1`max_pool` is not supported with openvino backendr!   inputs	pool_sizestridespaddingdata_formats        r   max_poolrR   ~   s     ; r   c                     t        d      )Nz5`average_pool` is not supported with openvino backendr!   rL   s        r   average_poolrT      s     ? r   c                 L    t        | t              r| f|z  n| } t        |       } | S N)
isinstanceintlist)r   num_spatial_dimss     r   _adjust_strides_dilationr[      s,    
 $.a#51A 	QAHr   c                     t        | t              r| j                         n| } | dk(  rdg g fS | dk(  rdg g fS | dk(  rdg g fS g }g }| D ]*  }|j                  |d          |j                  |d          , d	||fS )
Nsame
SAME_UPPER
same_lower
SAME_LOWERvalidVALIDr      EXPLICIT)rW   strlowerappend)rP   
pads_beginpads_endpadding_pairs       r   _adjust_paddingrk      s     ",GS!9gmmowG&R##	L	 R##	G	BJH ),q/*Q() z8++r   c                     |dk(  r| S |dk(  rg d}n|dk(  rg d}ng d}t        j                  |t        j                        }t        j                  | |      j                  d      S )Nchannels_firstrc   r      rc   ro   )r      rc   ro   )r      rc   ro   rp   r   r	   r%   r   r@   	transposer   )rM   rZ   rQ   permutations       r   _adjust_inputru      sc    &&1	Q	"%##K:Kfk299!<<r   c                     |dk(  rg d}n|dk(  rg d}ng d}t        j                  |t        j                        }t        j                  | |      j                  d      S )Nrc   )ro   rc   r   ro   )rp   ro   r   rc   )rq   rp   r   rc   ro   r   rr   kernelrZ   rt   s      r   _adjust_kernelry      sU    1	Q	"%##K:Kfk299!<<r   c                     |dk(  rg d}n|dk(  rg d}ng d}t        j                  |t        j                        }t        j                  | |      j                  d      S )Nrc   )rc   ro   r   ro   )ro   rp   r   rc   )rp   rq   r   rc   ro   r   rr   rw   s      r   _adjust_depthwise_kernelr{      sW    1	Q	" &##K:Kfk299!<<r   c                     |dk(  r| S |dk(  rg d}n|dk(  rg d}ng d}t        j                  |t        j                        }t        j                  | |      j                  d      S )Nrm   rc   rn   ro   )r   ro   rp   rc   )r   ro   rp   rq   rc   r   rr   )outputsrZ   rQ   rt   s       r   _adjust_outputsr~      sc    &&1	Q	"%##K:Kg{3::1==r   c           	      Z   t        |       } t        |      }t        j                  |      }| j                         j                  j                         dz
  }|dk(  r| j                         d|z   dz
     }n| j                         d   }|j                         d   }t        ||      }t        ||      }t        |      \  }	}
}t        | ||      } t        ||      }|j                         |j                         z  }|dk(  rt        j                  | |||
|||	      }ngt        j                  |       j                  d      }t        j                  |      j                  d      }t        j                  dgt        j                         j                  d      }t        j                  dgt        j                         j                  d      }t        j                  dgt        j                         j                  d      }t        j"                  ||||      j                  d      }t        j"                  ||||      j                  d      }t        j$                  ||      j                  d      }t        j                  dgt        j                         j                  d      }t        j"                  ||||      j                  d      }t        j$                  ||      j                  d      }t        j"                  ||||      j                  d      }t        j&                  |||gd      j                  d      }t        j(                  ||d      j                  d      }t        j*                  | |||
|||	      }t-        |j                  d      ||      }t/        |      S )Nro   channels_lastrc   r   iF)r   r   standardize_data_formatget_partial_shaperank
get_lengthr[   rk   ru   ry   r	   convolutionr?   r   r%   r   r@   slicedivideconcatrA   group_convolutionr~   r   )rM   rx   rO   rP   rQ   dilation_raterZ   inputs_in_channelskernel_in_channelspad_moderh   ri   
num_groupsconvinput_shapefilter_shape
zero_const	one_const	two_const	input_cin
filter_cinint_max_constfilter_coutfilter_new_coutshape_cin_xyfilter_new_shape
new_filters                              r   r   r      sW    6"F6"F11+>K//166AACaGo%#557  1$
 $557:113B7&w0@AG,]<LMM%4W%="Hj(6#3[AFF$45F 	%%'+=+H+H+JJ  Q##
 ''/66q9((077:&&sDHH5<<Q?
%%qc4884;;A>	%%qc4884;;A>	NNIy

&) 	 ^^)Y	

&) 	 __Y
;BB1E
 !))9+txx@GGJnn*i

&) 	 #//+zBII!L~~)]I

&) 	 $??,7

&) 	 %%f.>FMMaP
))
 4;;q>+;[IDt$$r   c           	      @   t        |       } t        |      }t        j                  |      }| j                         j                  j                         dz
  }|dk(  sJ d       t        ||      }t        ||      }t        |      \  }}}	t        | ||      } t        ||      }t        j                  dgt        j                        }
t        j                  ||
      }t        j                  | ||||	||      }t!        |j#                  d      ||      }t%        |      S )Nro   r   z@`depthwise_conv` is supported only for channels_last data_formatr   )r   r   r   r   r   r   r[   rk   ru   r{   r	   r%   r   r@   	unsqueezer   r~   r   r   )rM   rx   rO   rP   rQ   r   rZ   r   rh   ri   unsqueeze_dim
group_convs               r   depthwise_convr   :  s!    6"F6"F11+>K//166AACaG/) J) 'w0@AG,]<LMM%4W%="Hj(6#3[AF%f.>?F%%qc4884M6F++X}hJ !!.J z**r   c                     t        d      )Nz7`separable_conv` is not supported with openvino backendr!   )rM   depthwise_kernelpointwise_kernelrO   rP   rQ   r   s          r   separable_convr   ^       A r   c                     t        d      )Nz7`conv_transpose` is not supported with openvino backendr!   )rM   rx   rO   rP   output_paddingrQ   r   s          r   conv_transposer   l  r   r   c                     t        d      )Nz0`one_hot` is not supported with openvino backendr!   r   num_classesrC   dtypesparses        r   one_hotr   z  s    
: r   c                     t        d      )Nz2`multi_hot` is not supported with openvino backendr!   r   s        r   	multi_hotr     s    
< r   c                     t        d      )NzA`categorical_crossentropy` is not supported with openvino backendr!   targetr   from_logitsrC   s       r   categorical_crossentropyr     s    
K r   c                     t        d      )NzH`sparse_categorical_crossentropy` is not supported with openvino backendr!   r   s       r   sparse_categorical_crossentropyr     s    
	  r   c                     t        d      )Nz<`binary_crossentropy` is not supported with openvino backendr!   )r   r   r   s      r   binary_crossentropyr     s    
F r   c                 l   t        |       } t        j                  |t        j                        j                  d      }t        j                  | ||      j                  d      }t        j                  d| j                               j                  d      }t        j                  | |      j                  d      }t        j                  ||      j                  d      }t        j                  |||      }t        |      }t        t        j                  ||      j                  d            }	||	fS )Nr   ro   )r   r	   r%   r   r@   r   reduce_meanr&   powerr   subtract)
r   axeskeepdimssynchronizedmean	const_two	squared_xsquared_meansquared_x_meanvariances
             r   momentsr     s    aAT488,33A6D 4299!<D!!!Q%7%7%9:AA!DIq),33A6I>>$	299!<L)))T8DNt$D".,7>>qAH >r   c                    t        |       } t        |      }t        |      }|t        |      }nRt        j                  |      }|j                         }t        j                  dg|      }	t        j
                  |	|      }|t        |      }nRt        j                  |      }|j                         }t        j                  dg|      }
t        j
                  |
|      }| j                         j                  j                         }|dk  r||z  }|dk7  rwt        t        d|            }||d<   d||<   t        j                  |t        j                        j                  d      }t        j                  | |      j                  d      } t        j                  | |||||      j                  d      }|dk7  rwt        t        d|            }||d<   d||<   t        j                  |t        j                        j                  d      }t        j                  ||      j                  d      }t!        |      S )Nr   rc   )r   r	   r?   r&   r%   	broadcastr   r   r   rY   ranger   r@   r   rs   batch_norm_inferencer   )r   r   r   rC   offsetr6   epsilon
mean_shape	mean_typer   r   x_rankperm_vector
batch_norms                 r   batch_normalizationr     s    	aADX&Hv&&&t,
))+	&&sI6
##J
;e$&&t,
))+	%%qc95	""9j9   "''224Faxqy5F+,AD''TXX>EEaHq+.55a8..	5&$'fQi  qy5F+,AD''TXX>EEaH''
K@GGJ
z**r   c                     t        d      )Nz1`ctc_loss` is not supported with openvino backendr!   )r   r   target_lengthoutput_length
mask_indexs        r   ctc_lossr     s    
; r   c                     t        d      )Nz3`ctc_decode` is not supported with openvino backendr!   )rM   sequence_lengthsstrategy
beam_width	top_pathsmerge_repeatedr   s          r   
ctc_decoder     s     = r   c                     t        d      )Nz-`psnr` is not supported with openvino backendr!   )x1x2max_vals      r   psnrr     s    
M
NNr   )g?)g      ?)g,x?g2֫?)TrV   )Nra   N)rc   ra   Nrc   )rc   ra   NNrc   )r>   float32F)Fr>   )F)FF)NNgMbP?)r   )greedyd   rc   Tr   )1openvino.runtime.opset14runtimeopset14r	   openvinor   	keras.srcr   keras.src.backend.openvino.corer   r   r
   r   r   r   r   r   r   r#   r*   r,   r1   r3   r5   r9   rB   rI   rR   rT   r[   rk   ru   ry   r{   r~   r   r   r   r   r   r   r   r   r   r   r   r   r   r    r   r   <module>r      sK   + +   ? 9;
F
>
;
?
?
+PA ,
+IM
D
H  	" 		,$
===>" N%h !+P " $ ?C(+V Or   