
    ij
Z                     4   d dl Z d dlZd dlZd dlZd dlZd Zd Zd Zd Zd Z	d Z
	 	 	 ddZd	 Zd
 Zd Zd Zd Zd ZdZdj'                  e      Zde dZdj'                  e      Zdj'                  e      Zd ZddZd Zd Zd ZddddZd dZd Zy)!    Nc                 :   |j                         dvrt        d|       | dz
  |z  dz   } |j                         dk(  r$|t        | |      | z
  n|}| dz
  }| dz
  |z   }||fS |	|| z   dz
  }n| | dz  z   dz
  |z   }t        |dz  |dz  z   | dz
        }||z
  }||fS )a  Convert the padding arguments from Keras to the ones used by JAX.
    JAX starts with an shape of size `(input-1) * stride - kernel_size + 2`,
    then adds `left_pad` on the left, and `right_pad` on the right.
    In Keras, the `padding` argument determines a base shape, to which
    `output_padding` is added on the right. If `output_padding` is None, it will
    be given a default value.
    >   samevalidIThe `padding` argument must be one of 'valid', 'same'. Received: padding=   r      )lower
ValueErrormaxmin)kernel_sizestridedilation_ratepaddingoutput_paddingleft_pad	right_padpad_lens           {/var/www/html/emotional.easysim.app/public_html/venv/lib/python3.12/site-packages/keras/src/backend/common/backend_utils.py6_convert_conv_transpose_padding_args_from_keras_to_jaxr      s     }}//!!(	+
 	
 ?m3a7K}}'!
 % V${2 	
 ?!On4	 Y ! {*Q.G "K!O3a7.HGw!|gk1;?Ch&	Y    c                    |j                         dvrt        d|       | }| dz
  |z  dz   } |j                         dk(  r|t        | |      | z
  n|}d}|}n6||| dz  z
  n|}t        | dz  | z
  |z   dz   d      }d|z  | dz  z   | z
  |z   }|dkD  r*|dkD  r%t        j                  d| d| d	| d
| d| d       ||k\  r,t        j                  d| d|dz
   d| dt
               |dz
  }||fS )a  Convert the padding arguments from Keras to the ones used by Torch.
    Torch starts with an output shape of `(input-1) * stride + kernel_size`,
    then removes `torch_padding` from both sides, and adds
    `torch_output_padding` on the right.
    Because in Torch the output_padding can only be added to the right,
    consistency with Tensorflow is not always possible. In particular this is
    the case when both the Torch padding and output_padding values are
    strictly positive.
    >   r   r   r   r   r   r   r   zbYou might experience inconsistencies across backends when calling conv transpose with kernel_size=z	, stride=z, dilation_rate=z
, padding=z, output_padding=.zHTorch backend requires output_padding < stride. Clamping output_padding z -> z for stride )r	   r
   r   warningswarnUserWarning)r   r   r   r   r   original_kernel_sizetorch_paddingtorch_output_paddings           r   8_convert_conv_transpose_padding_args_from_keras_to_torchr    5   s    }}//!!(	+
 	
 '?m3a7K}}'!
 % V${2 	
 - % [1_$ 	
 Q,~=!CDa
 a/+=N 	 q1A577K6L MX-m_ =i00@C	
 v%'';&<D! M $ 		
  &z...r   c                 0   t        |       dz
  }|d d }g }t        |      D ]r  }	|t        |t              r|n||	   }
t        |t              r|n||	   }t        |t              r|n||	   }t	        ||	   ||||
      \  }}|j                  ||f       t |S )Nr   r   r   r   r   r   lenrange
isinstanceintr   append)input_shapekernel_shapestridesr   r   r   num_spatial_dimskernel_spatial_shapejax_paddingioutput_padding_i	strides_idilation_rate_ipad_left	pad_rights                  r   +compute_conv_transpose_padding_args_for_jaxr6   w   s     ;'!+',K#$ 2 %NC)H " 	
  *'37GWQZ	 -- q! 	 C,Q/)+
	
 	Hi01-20 r   c                 ,   t        |       dz
  }|d d }g }g }	t        |      D ]  }
|t        |t              r|n||
   }t        |t              r|n||
   }t        |t              r|n||
   }t	        ||
   ||||      \  }}|j                  |       |	j                  |        g }t        t        |t        t        f      r|n|g|z  |	      D ]<  \  }}t        d|dz
        }||kD  r|j                  |       ,|j                  |       > |}	||	fS )Nr   r"   r#   r   r   )
r%   r&   r'   r(   r    r)   ziplisttupler   )r*   r+   r,   r   r   r   r-   r.   torch_paddingstorch_output_paddingsr0   r1   r2   r3   r   r   corrected_output_paddingssopmax_alloweds                       r   -compute_conv_transpose_padding_args_for_torchrA      sk    ;'!+',N#$ ; %NC)H " 	
  *'37GWQZ	 -- q! 	 E,Q/)+
	
  	m,$$%9:/;4 !#ge}- 	Y))	 
12 !QUm%,,[9%,,R0
1 6000r   c                 l   t        |       dz
  }|dd }g }t        |      D ]  }	|t        |t              r|n||	   }
t        |t              r|n||	   }t        |t              r|n||	   }t	        ||	   ||||
      \  }}||	   dz
  |z  dz   }|dz
  |z
  }|dz
  |z
  }|j                  ||f        |S )aZ  Per-spatial-dim asymmetric (left, right) crop amounts for torch.

    Torch's `conv_transpose*d` only supports symmetric `padding` plus a
    right-side `output_padding`, which cannot express the asymmetric crops
    that Keras "same" semantics requires when stride > 1 with an odd kernel.
    We work around this by calling torch with `padding=0, output_padding=0`
    (which produces the largest "natural" output of size
    `(input - 1) * stride + effective_kernel`), then asymmetrically slicing
    the spatial dims to the same window JAX would compute.

    The slice on each side is `(effective_kernel - 1) - left_pad` from the
    left and `(effective_kernel - 1) - right_pad` from the right, where
    `left_pad` / `right_pad` come from the existing JAX helper. A negative
    value means we need to extend with zero-padding instead of cropping.
    r   Nr"   r#   r   r$   )r*   r+   r,   r   r   r   r-   r.   cropsr0   r1   r2   r3   r   r   effective_kernel	crop_left
crop_rights                     r   -compute_conv_transpose_output_crops_for_torchrG      s   . ;'!+',E#$ . %NC)H " 	
  *'37GWQZ	 -- q! 	 C,Q/)+
	
 13a7?JQN%)X5	&*i7
i,-3.4 Lr   c                     | y |j                         dvrt        d|       |dz
  |z  dz   }|j                         dk(  r!|t        ||      |z
  n|}| dz
  |z  |z   |z   S || |z  S | dz
  |z  |dz  z   |z   S )N>   r   r   r   r   r   r   )r	   r
   r   
input_sizer   r,   r   r   r   s         r   "_get_output_shape_given_tf_paddingrK     s     }}//!!(	+
 	

 ?m3a7K}}'! % W%3 	
 Q')K7.HH !''Ng-a?.PPr   c           
         t        |       dz
  }|}	t        |t              r|ft        |	      z  }t        |t              r|f|z  }t        |t              r|f|z  }|dk(  r| dd }
n| dd  }
g }t        |      D ]9  }|d n||   }t	        |
|   |	|   ||   ||||         }|j                  |       ; |dk(  r| d   g|z   |gz   }|S | d   |g|z   }|S )Nr   channels_lastr   rI   r   )r%   r'   r(   r&   rK   r)   )r*   r   filtersr,   r   r   data_formatr   r-   r.   input_spatial_shapeoutput_shaper0   current_output_paddingshape_is                  r   #compute_conv_transpose_output_shaperU   ,  s;    ;'!+&.#&(*S1E-FF'3*//-%&(+;;o%)!B/)!"oL#$ %"*Dq0A 	 5*1-,Q/AJ1'*
 	G$% o%#A',6'B  $A0<?r   c                     t        j                  |       } | | cxk  r|k  sn t        d|  d| d      | dk  r| |z   } | S )z?Canonicalize an axis in [-num_dims, num_dims) to [0, num_dims).zaxis z. is out of bounds for an array with dimension r   r   )operatorindexr
   axisnum_dimss     r   canonicalize_axisr\   \  sY    >>$D9'x'D6Gj
 	
 axhKr   c                 r    t        | t        t        f      rt        fd| D              S t        |       fS )zACanonicalize an axis or axes to a tuple of non-negative integers.c              3   6   K   | ]  }t        |        y wN)r\   ).0ar[   s     r   	<genexpr>z$canonicalize_axes.<locals>.<genexpr>l  s     B&q(3Bs   )r'   r:   r9   r\   rY   s    `r   canonicalize_axesrc   i  s2    $&BTBBBdH-//r   c                 <    t        | t              rt        |       S | S )zDStandardize an axis to a tuple if it is a list in the numpy backend.)r'   r9   r:   rZ   s    r   standardize_axis_for_numpyrf   p  s    $T405;:d:r   c           	          |dk(  r| j                   d   n| j                   d   }|j                   d   }t        |t              r,t        |t              r||k7  rt        d| d| d| d      y	y	y	)
u;  Validate a conv input against its kernel shape.

    Used by `conv`, `depthwise_conv`, and `separable_conv` — all share the
    convention that the kernel's input-channel dimension is `kernel.shape[-2]`.
    Produces a clear error message before the backend op raises its own
    implementation-specific one.
    rM   rN   r   r"   ^The number of input channels must match the kernel's input channels. Received: input channels=, kernel input channels=, data_format=''.Nshaper'   r(   r
   inputskernelrP   input_channelskernel_input_channelss        r   check_conv_input_channelsrs   u  s     (?:RQ  #LL, 	>3',c23322@1A B34 5'=,
 	
 4 3 	(r   c           	          |dk(  r| j                   d   n| j                   d   }|j                   d   }t        |t              r,t        |t              r||k7  rt        d| d| d| d      yyy)	a  Validate a conv_transpose input against its kernel shape.

    `conv_transpose` kernels use the layout
    `(spatial..., out_channels, in_channels)`, so the input-channel dimension
    is at `kernel.shape[-1]` (vs. `kernel.shape[-2]` for regular conv).
    rM   rN   r   rh   ri   rj   rk   Nrl   rn   s        r   #check_conv_transpose_input_channelsru     s     (?:RQ  #LL,>3',c23322@1A B34 5'=,
 	
 4 3 	(r   c                     | | S t        | t        t        t        f      st	        d|        t        | t              r| fS | S )z9Convert the non-`None` value to either a tuple or a list.z;`value` must be an integer, tuple or list. Received: value=)r'   r(   r:   r9   r
   )values    r   to_tuple_or_listrx     sQ    }ec5$/0$g'
 	
 %xLr   z\w+z(?:{0:}(?:,{0:})*)?z\(z\)z{0:}(?:,{0:})*z^{0:}->{0:}$c                     t        j                  t        |       st        d|        d | j	                  d      D        \  }}||fS )Nznot a valid gufunc signature: c           
   3      K   | ]M  }t        j                  t        |      D cg c]%  }t        t        j                  t        |            ' c} O y c c}w wr_   )refindall	_ARGUMENTr:   _DIMENSION_NAME)r`   arg_listargs      r   rb   z4_vectorize_parse_gufunc_signature.<locals>.<genexpr>  sM      
  zz)X6	
 "**_c23	
	
s   !A*AAz->)r{   match
_SIGNATUREr
   split)	signatureargsretvalss      r   !_vectorize_parse_gufunc_signaturer     sO     88J	*9)EFF
 "-MD' =r   c           
      (   t        |      }|rt        |      |k  r0t        d| d|       t        |      |k7  rt        d| d|       |r|| d  nd}t        ||      D ].  \  }}|| vr|| |<   || |   k7  st        d| d| d| |           y )	Nzinput with shape z9 does not have enough dimensions for all core dimensions zoutput shape z  does not match core dimensions  z%inconsistent size for core dimension z: z vs )r%   r
   r8   )	dim_sizesrm   	core_dimsis_inputnum_core_dims
core_shapedimsizes           r   _vectorize_update_dim_sizesr     s    	NMu:%#E7 +'[*  u:&w '))25 
 ,5~'"JJ/ 	Ti!IcNYs^#7uB&Ys^,. 	r   c                    ddl m} t        |       t        |      k7  r#t        dt        |       dt        |              g }i }t	        | |      D ]T  \  }}t        ||j                  |d       |j                  t        |      z
  }|j                  |j                  d |        V |d   }|D ]  }	|j                  ||	      } ||fS )Nr   opsz/wrong number of positional arguments: expected , got Tr   )
	keras.srcr   r%   	TypeErrorr8   r   rm   ndimr)   broadcast_shapes)
r   input_core_dimsr   shapesr   r   r   r   broadcast_shaper>   s
             r   !_vectorize_parse_input_dimensionsr     s     
4yC((O,-VCI;@
 	
 FIdO4 (Y#syy)d	
 xx#i.(cii&'( QiO C..BCI%%r   c                 *     ddl m  fd}|S )Nr   r   c                      
|  }t        |t        t        f      r|D cg c]  }j                  |       }}n|j                  g}	dgt	        |      z  }nh	}t	        |      dkD  rt        |t              st        d|       t	        |      t	        |      k7  r#t        dt	        |       dt	        |             t              }t        ||      D ]  \  }}t        |||d        |S c c}w )Nr   r   z@output must be a tuple when multiple outputs are expected, got: z+wrong number of output arguments: expected r   Fr   )	r'   r9   r:   rm   r%   r   dictr8   r   )r   outx
out_shapesoutput_core_dimssizesrm   r   r   expected_output_core_dimsfuncr   s           r   wrappedz-_vectorize_check_output_dims.<locals>.wrapped
  s   DkcD%=)0341#))A,4J4))J$, "tc*o58#$q(C1G**-0  :#&6"77  #$4 56fS_<MO 
 Y #J0@ A 	QE9'ui%P	Q 
/ 5s   C3)r   r   )r   r   r   r   r   s   ``` @r   _vectorize_check_output_dimsr     s    
 8 Nr   c                     |s ||fS t        |      D cg c]  \  }}||vs| }}}|j                         D ci c]  \  }}||vs|| }	}}t        d |D              D cg c]  }|t        |      k  r|||   f c}|j                         D ci c]  \  }}||v s|| c}} fd}
|
||	fS c c}}w c c}}w c c}w c c}}w )Nc              3   B   K   | ]  }t        |t              s|  y wr_   )r'   r(   )r`   es     r   rb   z,_vectorize_apply_excluded.<locals>.<genexpr>3  s     Baz!S/ABs   c                  f    t        |       } D ]  \  }}| j                  ||         | i |S r_   )r9   insert)r   kwargsr0   r   r   static_argsstatic_kwargss       r   new_funcz+_vectorize_apply_excluded.<locals>.new_func8  sB    Dz! 	 FAsKK3	 T5V5}55r   )	enumerateitemssortedr%   )r   excludedr   r   r0   r   dynamic_argskeyvaldynamic_kwargsr   r   r   s   `          @@r   _vectorize_apply_excludedr   )  s    T6!!&/oKFAs(9JCKLK!'S#3h3FSN 
 B8BBs4y= 
DGK
 /5llnP(#sxS#XPM6 \>11# L
 Qs(   B=B= CC)C	C(C)r   r   c                r     ddl m t               t        j                          fd       }|S )z*Implementation adapted from JAX and NumPy.r   r   c                     t        | |      \  }} }t              \  }ndgt        |       z  d }t        |       D ch c]
  \  }}|	| }}}t	        |      rYt	        fd|D              rt        d| d       t        ||| i       \  }} }t              D cg c]  \  }}||vr| c}}t        t        j                  |             } t        |       \  }	}
t        ||
|      }g }g }t        |       D ]  \  }}|j                  d |j                  t        |      z
   }t        |	      t        |      z
  }|dz  |z   }|j                  |d d d          t        d t        |      D              }j                  ||      }|j                  |        |}g t        t        |       D ]S  \  }}t        d	 |D              }t!        d
 |D              r!j                  t        |	      dz
  |z
         K ||      }U  || }s|S t#        |t              rt        fd|D              S j%                  |      S c c}}w c c}}w )Nr   c              3   .   K   | ]  }|   d k7    yw)r   Nr   )r`   r0   r   s     r   rb   z2vectorize_impl.<locals>.wrapped.<locals>.<genexpr>X  s     ??1%+?s   zCannot pass None at locations z with signature=)r   rN   c              3   2   K   | ]  \  }}|d k(  s|  yw)r   Nr   )r`   r0   r   s      r   rb   z2vectorize_impl.<locals>.wrapped.<locals>.<genexpr>w  s      $aTQY$s   re   c              3   .   K   | ]  }|d k(  rdnd  yw)r   Nr   r   )r`   r   s     r   rb   z2vectorize_impl.<locals>.wrapped.<locals>.<genexpr>  s     LDAID14Ls   c              3   $   K   | ]  }|d u  
 y wr_   r   )r`   rZ   s     r   rb   z2vectorize_impl.<locals>.wrapped.<locals>.<genexpr>  s     4D44<4s   r   c              3   D   K   | ]  }j                  |         yw)re   N)expand_dims)r`   rdims_to_expandr   s     r   rb   z2vectorize_impl.<locals>.wrapped.<locals>.<genexpr>  s#      <=7s    )r   r   r%   r   anyr
   r:   mapconvert_to_tensorr   r   r8   rm   r   r)   squeezeallr'   r   ) r   r   excluded_funcr   r0   r   	none_args_r   r   r   checked_funcsqueezed_argsrev_filled_shapesr   noncore_shapepad_ndimfilled_shapesqueeze_indicessqueezed_argvectorized_funcnegdim
axis_sizesin_axesresultr   r   r   r   pyfuncr   vmap_fns                             @@r   r   zvectorize_impl.<locals>.wrappedH  s   &?HdF'
#tV  1)< .O-  "dSY.O#%.t_D61cQD	Dy>?Y?? 4YK @&&/[2  &?y$&"M4
 (8AsI% O S..56%F/&
" 49&6
 !$8 	/NCII&A3y>(ABM?+c-.@@H#d?]:L$$\$B$%78# $!*=!9$ O ;;s;AL  .	/ '"+C1B,C"D 	DFJLLLG4G44%%c/&:Q&>&GH")/7"C	D !-0M& AG   ??6???q Es   
II(I)r   r   set	functoolswraps)r   r   r   r   r   r   s   ```` @r   vectorize_implr   A  s6     uH__VE@ E@N Nr   c                     |dk\  rt        d      g|z  t        |||      gz   }n&t        t        |||      gt        d      gd|z
  z  z   }| t        |         S )z$Slice a Tensor along the given axis.r   NrN   )sliceEllipsisr:   )r   startstopsteprZ   slicess         r   slice_along_axisr     si     qy+%udD)A(BBE%t45tI9
 
 U6]r   c                 D    t        j                  | |z        }|dz   }||fS )z8Compute small and big window sizes for adaptive pooling.r   )mathceil)	input_dim
output_dimsmallbigs       r   %compute_adaptive_pooling_window_sizesr     s(    IIi*,-E
!)C#:r   )NrM   r   )T)r   Nr   r   ) r   r   rW   r{   r   r   r    r6   rA   rG   rK   rU   r\   rc   rf   rs   ru   rx   r~   format_CORE_DIMENSION_LISTr}   _ARGUMENT_LISTr   r   r   r   r   r   r   r   r   r   r   r   <module>r      s       	 *Z?/D$N61r5pQF -`
0;

6
0" ,33OD &'r*	!((3"">2
6&2#L20 15 Od	r   