
    &VfB                         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d	Z	d
 Z
d Zd ZdZd                    e          Zde dZd                    e          Zd                    e          Zd ZddZd Zd Zd ZddddZdS )    Nc                 >   |                                 dv sJ | dz
  |z  dz   } |                                 dk    r%|t          | |          | z
  n|}| dz
  }| dz
  |z   }n:|	|| 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valid   r   N   )lowermaxmin)kernel_sizestridedilation_ratepaddingoutput_paddingleft_pad	right_padpad_lens           c/var/www/html/software/conda/lib/python3.11/site-packages/keras/src/backend/common/backend_utils.py5_convert_conv_tranpose_padding_args_from_keras_to_jaxr      s     ==??/////?m3a7K}}'!!
 % V$${22 	
 ?!On4		 ! {*Q.GG "K!O3a7.HGw!|gk1;?CCh&	Y    c                    |                                 dv sJ | }| dz
  |z  dz   } |                                 dk    r|t          | |          | z
  n|}d}|}n:||| dz  z
  n|}t          | dz  | z
  |z   dz   d          }d|z  | dz  z   | z
  |z   }|dk    r*|dk    r$t          j        d| d| d	| d
| d| d           ||k    rt	          d| d| d| d| d	          ||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   Nr   r   zbYou might experience inconsistencies across backends when calling conv transpose with kernel_size=z	, stride=z, dilation_rate=z
, padding=z, output_padding=.zThe padding arguments (padding=z) and output_padding=z") lead to a Torch output_padding (z ) that is greater than strides (zm). This is not supported. You can change the padding arguments, kernel or stride, or run on another backend. )r   r	   warningswarn
ValueError)r   r   r   r   r   original_kernel_sizetorch_paddingtorch_output_paddings           r   7_convert_conv_tranpose_padding_args_from_keras_to_torchr   0   s    ==??/////&?m3a7K}}'!!
 % V$${22 	
 - % [1_$$ 	
 Q,~=!CDa
 
 a/+=N 	 q1A55C7KC CC C.;C C C C 2@C C C	
 	
 	
 v%%Pg P P,P P3P P P P P
 
 	
 ...r   c                    t          |           dz
  }|d d         }g }t          |          D ]}	|t          |t                    r|n||	         }
t          |t                    r|n||	         }t          |t                    r|n||	         }t	          ||	         ||||
          \  }}|                    ||f           |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_jaxr3   n   s	    ;''!+',K#$$ 2 2 %NC)H)H% N" 	
  *'377GGGWQZ	 ---"MMq! 	 B,Q/)+
 
 
	
 	Hi01111r   c                    t          |           dz
  }|d d         }g }g }	t          |          D ]}
|t          |t                    r|n||
         }t          |t                    r|n||
         }t          |t                    r|n||
         }t	          ||
         ||||          \  }}|                    |           |	                    |           ||	fS r    )r"   r#   r$   r%   r   r&   )r'   r(   r)   r   r   r   r*   r+   torch_paddingstorch_output_paddingsr-   r.   r/   r0   r   r   s                   r   -compute_conv_transpose_padding_args_for_torchr7      s%    ;''!+',N#$$ ; ; %NC)H)H% N" 	
  *'377GGGWQZ	 ---"MMq! 	 D,Q/)+
 
 
	
  	m,,,$$%9::::000r   c                     | d S |                                 dv sJ |dz
  |z  dz   }|                                 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	   
input_sizer   r)   r   r   r   s         r   "_get_output_shape_given_tf_paddingr;      s     t==??/////?m3a7K}}'!! % W%%33 	
 Q')K7.HH !''Ng-a?.PPr   channels_lastr   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 ]P}|d n||         }t	          |
|         |	|         ||         ||||                   }|                    |           Q|dk    r| d         g|z   |gz   }n| d         |g|z   }|S )Nr   r<   r   r9   r   )r"   r$   r%   r#   r;   r&   )r'   r   filtersr)   r   r   data_formatr   r*   r+   input_spatial_shapeoutput_shaper-   current_output_paddingshape_is                  r   #compute_conv_transpose_output_shaperE      su    ;''!+&.#&& G(*S1E-F-FF'3 0*//-%% <&(+;;o%%)!B$/)!""oL#$$ % %"*DD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_axisrK     s    >$D9''''x''''D    
 
 	
 axxhKr   c                 N    t          | t                    rt          |           n| S )zDStandardize an axis to a tuple if it is a list in the numpy backend.)r$   listtuplerI   s    r   standardize_axis_for_numpyrP     s!    $T400:5;;;d: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.Nz;`value` must be an integer, tuple or list. Received: value=)r$   r%   rN   rM   r   )values    r   to_tuple_or_listrS     si    }ec5$/00 
'$' '
 
 	
 % xLr   z\w+z(?:{0:}(?:,{0:})*)?z\(z\)z{0:}(?:,{0:})*z^{0:}->{0:}$c                     t          j        t          |           st          d|            d |                     d          D             \  }}||fS )Nznot a valid gufunc signature: c              3   ^   K   | ](}d  t          j        t          |          D             V  )dS )c                 \    g | ])}t          t          j        t          |                    *S  )rN   refindall_DIMENSION_NAME).0args     r   
<listcomp>z?_vectorize_parse_gufunc_signature.<locals>.<genexpr>.<listcomp>7  s<     	
 	
 	
 "*_c2233	
 	
 	
r   N)rX   rY   	_ARGUMENT)r[   arg_lists     r   	<genexpr>z4_vectorize_parse_gufunc_signature.<locals>.<genexpr>6  s[        
 		
 	
z)X66	
 	
 	
     r   z->)rX   match
_SIGNATUREr   split)	signatureargsretvalss      r   !_vectorize_parse_gufunc_signaturerg   1  sl     8J	** GE)EEFFF 
 "--  MD' =r   Tc           
      |   t          |          }|r)t          |          |k     rt          d| d|           n(t          |          |k    rt          d| d|           |r|| d          nd}t          ||          D ]9\  }}|| vr|| |<   || |         k    rt          d| d| d| |                    :d S )	Nzinput with shape z9 does not have enough dimensions for all core dimensions zoutput shape z  does not match core dimensions rW   z%inconsistent size for core dimension z: z vs )r"   r   zip)	dim_sizesshape	core_dimsis_inputnum_core_dims
core_shapedimsizes           r   _vectorize_update_dim_sizesrr   @  sL   	NNM u::%%*E * *'* *   & u::&&5 5 5)25 5  
 ,5<~''"JJ//  	Ti!IcNNYs^##. . .. .&s^. .   $ r   c                    ddl m} t          |           t          |          k    r/t          dt          |           dt          |                      g }i }t	          | |          D ]V\  }}t          ||j        |d           |j        t          |          z
  }|                    |j        d |                    W|d         }|D ]}	|	                    ||	          }||fS )Nr   opsz/wrong number of positional arguments: expected , got Trm   )
	keras.srcru   r"   	TypeErrorri   rr   rk   ndimr&   broadcast_shapes)
re   input_core_dimsru   shapesrj   r\   rl   rz   broadcast_shapess
             r   !_vectorize_parse_input_dimensionsr   [  s.    
4yyC((((@O,,@ @47II@ @
 
 	
 F "IdO44 ( (Y#sy)d	
 	
 	
 	
 x#i..(ci&''''QiO C C..BBI%%r   c                 *     ddl m  fd}|S )Nr   rt   c                  *    	|  }t          |t          t          f          r
fd|D             }n|j        g}dgt	          |          z  }n}t	          |          dk    r't          |t                    st          d|           t	          |          t	          |          k    r/t          dt	          |           dt	          |                     t                    }t          ||          D ]\  }}t          |||d           |S )	Nc                 :    g | ]}                     |          S rW   )rk   )r[   xru   s     r   r]   zA_vectorize_check_output_dims.<locals>.wrapped.<locals>.<listcomp>~  s#    4441#))A,,444r   rW   r   z@output must be a tuple when multiple outputs are expected, got: z+wrong number of output arguments: expected rv   Frw   )	r$   rM   rN   rk   r"   ry   dictri   rr   )re   out
out_shapesoutput_core_dimssizesrk   rl   rj   expected_output_core_dimsfuncru   s          r   wrappedz-_vectorize_check_output_dims.<locals>.wrapped{  s_   dDkcD%=)) 	%4444444JJ)J$, "tc*oo58#$$q((C1G1G(0*-0 0   :#&6"7"777O #$4 5 5O O=@__O O  
 Y #J0@ A A 	Q 	QE9'ui%PPPPP
r   )rx   ru   )r   rj   r   r   ru   s   ``` @r   _vectorize_check_output_dimsr   t  sI    
        8 Nr   c                 2    s |fS fdt                    D             }fd|                                D             }fdt          d D                       D             fd|                                D              fd}|||fS )Nc                 "    g | ]\  }}|v	|S rW   rW   )r[   r-   r\   excludeds      r   r]   z-_vectorize_apply_excluded.<locals>.<listcomp>  s'    KKKFAs(9J9JC9J9J9Jr   c                 $    i | ]\  }}|v	||S rW   rW   r[   keyvalr   s      r   
<dictcomp>z-_vectorize_apply_excluded.<locals>.<dictcomp>  s0       S#3h3F3FS3F3F3Fr   c                 J    g | ]}|t                    k     ||         f S rW   )r"   )r[   r-   re   s     r   r]   z-_vectorize_apply_excluded.<locals>.<listcomp>  s8       s4yy== 
DG==r   c              3   D   K   | ]}t          |t                    |V  d S N)r$   r%   )r[   es     r   r`   z,_vectorize_apply_excluded.<locals>.<genexpr>  s1      BBaz!S/A/ABBBBBBBr   c                 $    i | ]\  }}|v 	||S rW   rW   r   s      r   r   z-_vectorize_apply_excluded.<locals>.<dictcomp>  s$    PPP(#sxS#r   c                  r    t          |           } D ]\  }}|                     ||            | i |S r   )rM   insert)re   kwargsr-   r\   r   static_argsstatic_kwargss       r   new_funcz+_vectorize_apply_excluded.<locals>.new_func  sR    Dzz! 	  	 FAsKK3tT5V5}555r   )	enumerateitemssorted)	r   r   re   r   dynamic_argsdynamic_kwargsr   r   r   s	   ```    @@r   _vectorize_apply_excludedr     s     "T6!!KKKKiooKKKL   !'  N   BB8BBBBB  K
 QPPPfllnnPPPM6 6 6 6 6 6 6 \>11r   )r   rd   c                ~     ddl m t                      t          j                    fd            }|S )z*Implementation adapted from JAX and NumPy.r   rt   Nc                     t          | |          \  }} }t                    \  }ndgt          |           z  d }d t          |           D             t	                    rat	          fdD                       rt          d d           t          || i           \  }} }fdt                    D             t          t          j        |                     } t          |           \  }}t          |||          }g }g }	t          |           D ]\  }
}|
j        d |
j        t          |          z
           }t          |          t          |          z
  }|dz  |z   }|	                    |d d d                    t          d	 t          |          D                       }                    |
|
          }|                    |           |}g t          t          |	           D ]l\  }}t          d |D                       }t!          d |D                       r)                    t          |          dz
  |z
             ` ||          }m || }s|S t#          |t                    rt          fd|D                       S                     |
          S )NrW   c                     h | ]	\  }}||
S r   rW   )r[   r-   r\   s      r   	<setcomp>z2vectorize_impl.<locals>.wrapped.<locals>.<setcomp>  s    DDD61cQr   c              3   0   K   | ]}|         d k    V  dS )rW   NrW   )r[   r-   r|   s     r   r`   z2vectorize_impl.<locals>.wrapped.<locals>.<genexpr>  s-      ???1%+??????r   zCannot pass None at locations z with signature=c                 "    g | ]\  }}|v	|S rW   rW   )r[   r-   rp   	none_argss      r   r]   z3vectorize_impl.<locals>.wrapped.<locals>.<listcomp>  s2       AsI%% %%%r   )r   r>   c              3   ,   K   | ]\  }}|d k    |V  dS )r   NrW   )r[   r-   rq   s      r   r`   z2vectorize_impl.<locals>.wrapped.<locals>.<genexpr>  s3       $ $aTQYYYYYY$ $r   rO   c              3   *   K   | ]}|d k    rdndV  dS )r   Nr   rW   )r[   rq   s     r   r`   z2vectorize_impl.<locals>.wrapped.<locals>.<genexpr>  s.      LLDAIIDD1LLLLLLr   c              3      K   | ]}|d u V  	d S r   rW   )r[   rI   s     r   r`   z2vectorize_impl.<locals>.wrapped.<locals>.<genexpr>  s&      44D44<444444r   r   c              3   F   K   | ]}                     |           V  dS )rO   N)expand_dims)r[   rdims_to_expandru   s     r   r`   z2vectorize_impl.<locals>.wrapped.<locals>.<genexpr>  sD        <=77     r   )r   rg   r"   r   anyr   rN   mapconvert_to_tensorr   r   ri   rk   rz   r&   squeezeallr$   r   )re   r   excluded_funcr   _r~   rj   checked_funcsqueezed_argsrev_filled_shapesr\   rl   noncore_shapepad_ndimfilled_shapesqueeze_indicessqueezed_argvectorized_funcnegdim
axis_sizesin_axesresultr   r|   r   r   ru   pyfuncrd   vmap_fns                         @@@r   r   zvectorize_impl.<locals>.wrapped  s   &?HdF'
 '
#tV  1)<< .O--  "dSYY.O#DDYt__DDD	y>> 	????Y?????  2Y 2 2&/2 2   &?y$& &"M4   '88  O S.5566%F/&
 &
" 49&6
 
 !$88 	/ 	/NCI&A3y>>(A&ABM?++c-.@.@@H#d?]:L$$\$$B$%7888# $ $!*=!9!9$ $ $  O ;;s;AAL  ....&"+C1B,C"D"D 	D 	DFJLLLLLLLG44G44444 D%%c/&:&:Q&>&GHHHH")'/7"C"C -0 	@M&& 	@     AG      ??6????r   )rx   ru   set	functoolswraps)r   r   r   rd   r   ru   s   ```` @r   vectorize_implr     s     uuH_VE@ E@ E@ E@ E@ E@ E@ E@ E@N Nr   )Nr<   r   )T)r   rG   rX   r   r   r   r3   r7   r;   rE   rK   rP   rS   rZ   format_CORE_DIMENSION_LISTr^   _ARGUMENT_LISTrb   rg   rr   r   r   r   r   rW   r   r   <module>r      s        				 & & &R;/ ;/ ;/|$ $ $N&1 &1 &1RQ Q Q> - - - -`
 
 
; ; ;
  " ,33ODD *&***	!((33"">22
     6& & &2# # #L2 2 20 15 O O O O O O Or   