
    2VhB8                         d dl Z d dl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dZ	 	 	 	 dd	Zd
 Zd Zd Zd ZddZ ed      d        Zy)    N)tree)keras_export)canonicalize_axis)to_tuple_or_listc                 $   t        |       } t        |      }| }|}t        |       t        |      kD  rdgt        |       t        |      z
  z  |z   }t        |       t        |      k  rdgt        |      t        |       z
  z  | z   } t        |       }t        t        |             D ]i  }| |   dk(  r	||   ||<   | |   ||   dk(  rdn||   ||<   ,||   dk(  s||   ||   | |   k(  r	| |   ||<   Mt        d| |    d||    d| d| d	       |S )aS  Broadcast input shapes to a unified shape.

    Convert to list for mutability.

    Args:
        shape1: A tuple or list of integers.
        shape2: A tuple or list of integers.

    Returns:
        output_shape (list of integers or `None`): The broadcasted shape.

    Example:
    >>> broadcast_shapes((5, 3), (1, 3))
    [5, 3]
       Nz2Cannot broadcast shape, the failure dim has value z!, which cannot be broadcasted to z. Input shapes are:  and .)listlenrange
ValueError)shape1shape2origin_shape1origin_shape2output_shapeis         M/home/dcms/DCMS/lib/python3.12/site-packages/keras/src/ops/operation_utils.pybroadcast_shapesr      sM     &\F&\FMM
6{S[ Fc&k12V;
6{S[ Fc&k12V;<L3v; !9>$QiLOAY&,Qi1nd&)LOayA~!2fQi6!96L"()Q Hayk!B6!9+ N))6u]O1N      c                 6   t        |       } |t        |       }t        |      }t        |      t        |       z   }|D cg c]  }t        ||       }}t	        |       }t        |      D cg c]  }||v rdn
t        |       }}t        |      S c c}w c c}w )a  Compute the output shape for the `expand_dims` operation.

    Args:
        input_shape: Input shape.
        axis: int or sequence of ints for the axis to expand.

    Returns:
        Tuple of ints: The output shape after the `expand_dims` operation.
    r   )r   r   r   r   iterr   nexttuple)input_shapeaxisout_ndima
shape_iterax	new_shapes          r    compute_expand_dims_output_shaper#   7   s     {#K|;D!D4y3{++H489qa*9D9k"J8=h24R4ZT*--I   :s   B,Bc                    ||n|}t        |       }t        j                  |       } |dk(  r| dd }n| dd }g }t        t	        |            D ]  }||   	d||<   |j                  |         t        j                  |      }|dk(  rWt        j                  ||z
  |z        dz   }	t        t	        |	            D ]!  }||vs|	|   dk  st        d|  d	| d
       n2|dk(  rt        j                  |dz
  |z        dz   }	nt        d|       |	D cg c]  }t        |       }	}|D ]  }d|	|<   	 t        |	      }	|dk(  r|d   f|	z   |d   fz   }
|
S |d   |d   f|	z   }
|
S c c}w )a3  Computes the output shape of pooling operations.

    Args:
        input_shape: Input shape. Must be a tuple of integers.
        pool_size: Size of the pooling operation. Must be a tuple of integers.
        strides: Stride of the pooling operation. Must be a tuple of integers.
            Defaults to `pool_size`.
        padding: Padding method. Available methods are `"valid"` or `"same"`.
            Defaults to `"valid"`.
        data_format: String, either `"channels_last"` or `"channels_first"`.
            The ordering of the dimensions in the inputs. `"channels_last"`
            corresponds to inputs with shape `(batch, height, width, channels)`
            while `"channels_first"` corresponds to inputs with shape
            `(batch, channels, height, weight)`. Defaults to `"channels_last"`.

    Returns:
        Tuple of ints: The output shape of the pooling operation.

    Examples:

    # Basic usage with square pooling on a single image
    >>> compute_pooling_output_shape((1, 4, 4, 1), (2, 2))
    (1, 2, 2, 1)

    # Strided pooling on a single image with strides different from pool_size
    >>> compute_pooling_output_shape((1, 4, 4, 1), (2, 2), strides=(1, 1))
    (1, 3, 3, 1)

    # Pooling on a batch of images
    >>> compute_pooling_output_shape((32, 4, 4, 3), (2, 2))
    (32, 2, 2, 3)
    Nchannels_lastr      validr   z@Computed output size would be negative. Received: `inputs.shape=z` and `pool_size=`.samezGArgument `padding` must be either 'valid' or 'same'. Received: padding=)
r   nparrayr   r   appendfloorr   intr   )r   	pool_sizestridespaddingdata_formatinput_shape_originspatial_shape	none_dimsr   output_spatial_shaper   s              r   compute_pooling_output_shaper8   N   s   N #?iGk*((;'Ko%#Ab)#ABI3}%&  #  "M!Q  #I'HHmi/7:;a? 	 s/01 	A	!&:1&=&A %%0M1B9+RQ 	 
F	!xx):g(EFJi!
 	
 -AAqCFAA '"&Q' !56o%"$"#!"%'( 	  q!q!
 !!  Bs   E&c           
      P   |dk(  r| dd }|| d   |fz   }n| dd }|| d   |fz   }t        |      t        |       k7  rt        d| d|  d      t        |t              r|ft        |      z  }t        |t              r|ft        |      z  }t        |      t        |      k7  rt        d	| d
|  d      g }	t	        j
                  |      }t        t        |            D ]  }
||
   	d||
<   |	j                  |
         t	        j
                  |dd       }t	        j
                  |      }|dk(  rct	        j                  |||dz
  z  z
  dz
  |z        dz   }t        t        |            D ]$  }
|
|	vs||
   dk  st        d|  d| d| d       n8|dk(  s|dk(  rt	        j                  |dz
  |z        dz   }nt        d| d      |D 
cg c]  }
t        |
       }}
|	D ]  }
d||
<   	 t        |      }|dk(  r| d   f|z   |d   fz   }|S | d   |d   f|z   }|S c c}
w )z%Compute the output shape of conv ops.r%   r   r&   r'   NzNKernel shape must have the same length as input, but received kernel of shape z and input of shape r
   zlDilation must be None, scalar or tuple/list of length of inputs' spatial shape, but received `dilation_rate=z` and input of shape r(   r   z?Computed output size would be negative. Received `inputs shape=z`, `kernel shape=z`, `dilation_rate=r)   r*   causalz9`padding` must be either `'valid'` or `'same'`. Received )
r   r   
isinstancer/   r+   r,   r   r-   r.   r   )r   filterskernel_sizer1   r2   r3   dilation_rater5   kernel_shaper6   r   kernel_spatial_shaper7   r   s                 r   compute_conv_output_shaperB      s    o%#Ab)"k"ow%??#AB"k!ng%>>
<C,,+n -)]!-
 	

 -%&(3}+=='3*s=11
=S//+_ -)]!-
 	
 IHH]+M3}%&  #  "M!Q  88L"$56HH]+M'HH2F2J!KKaO 	 	 s/01 	A	!&:1&=&A %%0M 2%%1N 3&&3_B8 	 
F	g1!xx):g(EFJGiq
 	
 -AAqCFAA '"&Q' !56o%^ 44R8H7JJ 	
  $AR(89<PP Bs   H#c                 @   t        |       dk(  rd| d   f} t        |      dk(  r|d   df}| d   "|d   | d   |d   k7  rt        d|  d| d      t        | dd |dd       }| d   |d   g}||z   }t        |       dk(  r|d= t        |      dk(  r|d= t        |      S )	zCompute the output shape of a `matmul` operation.

    Args:
        shape1: Shape of the left operand.
        shape2: Shape of the right operand.

    Returns:
        Tuple of ints: The output shape for the `matmul` operation.
    r   r   r&   Nr:   z[Inner dimensions (`x1.shape[-1]` and `x2.shape[-2]`) must be equal, but received `x1.shape=z` and `x2.shape=r)   )r   r   r   r   )r   r   leading_shapelast_2_dims_shaper   s        r   compute_matmul_output_shaperF      s     6{aVAY
6{a)Qr
2J"2J&*$--3H 5$
 	
 %VCR[&"+>MVBZ0 #44L
6{a
6{ar   c           
         |j                  d      }|dkD  rt        d| d| d| d      d| v rt        d |D              S t        j                  |       }|d	k(  r.|t        j                  |      k7  rt        d
|  d| d|       |S d}d}t        |      D ]  \  }}|dk(  r|}||z  } |d	k(  s||z  d	k7  rt        d| d|  d| d|       t        |      }	||z  |	|<   t        |	      S )zConverts `-1` in `newshape` to either an actual dimension or `None`.

    This utility does not special case the 0th dimension (batch size).
    r&   r   z4There must be at most one unknown dimension (-1) in z. Received: =r
   Nc              3   .   K   | ]  }|d k7  r|nd  yw)r&   N ).0dims     r   	<genexpr>z/compute_reshape_output_shape.<locals>.<genexpr>(  s     DCC2IS4/Ds   r   zFThe total size of the tensor must be unchanged. Received: input_shape=z, zyThe total size of the tensor must be unchanged, however, the input size cannot by divided by the specified dimensions in z. Received: input_shape=)countr   r   mathprod	enumerater   )
r   newshapenewshape_arg_nameunknown_dim_count
input_sizeknown_output_sizeunknown_dim_indexindexrL   r   s
             r   compute_reshape_output_shaperY     sj   
 !r*1B !.?-@(1N
 	
 {D8DDD;'JA8,,*m2.?-@(M   ) %
s"9 %$	% A.?!?1!DK !!9+b !8*.
 	
 >L&04E&EL"#r   c                      t                |t         ddd         S t        |      t               k7  r$t        dt                dt        |       d      t         fd|D              S )a  Compute the output shape for the `transpose` operation.

    Args:
        input_shape: Input shape.
        axes: Permutation of the dimensions for the `transpose` operation.

    Returns:
        Tuple of ints: The output shape after the `transpose` operation.
    Nr&   zDaxis must be a list of the same length as the input shape, expected z, but received r
   c              3   (   K   | ]	  }|     y wNrJ   )rK   r!   r   s     r   rM   z1compute_transpose_output_shape.<locals>.<genexpr>]  s     0RR0s   )r   r   r   r   )r   axess   ` r   compute_transpose_output_shaper^   J  s{     {#K|[2&''
4yC$$K()T1F
 	
 04000r   c                     t        |       } t        |      }|&d | v rd gnt        t        j                  |             g} t	        |       t	        |      k7  rt        d|  d| d      ||   | |<   t        | |      }|S )NzM`x` and `indices` must have the same number of dimensions, but receive shape r	   r
   )r   r/   r+   rP   r   r   r   )r   indices_shaper   r   s       r   $compute_take_along_axis_output_shapera   `  s    {#K'M|k)TFBGGK4H0I/J 	 ;3}--!!,U=/D
 	

 &d+K#K?Lr   c                     t        |       } |&|rt        | D cg c]  }d c}      S t        g       S |r|D ]  }d| |<   	 t        |       S t        |d      D ]  }| |=  t        |       S c c}w )Nr   T)reverse)r   r   sorted)shaper   keepdims_r!   s        r   reduce_shaperh   s  s    KE|U+!+,,9 	BE"I	U|t, 	Bb		U| ,s   	A-zkeras.utils.get_source_inputsc                 p   t        | d      s| S | j                  \  }}}|r|j                  s| gS |j                  |   }|j                  rt	        j
                  |j                        S g }|j                  D ]:  } t        |       }|D ](  t        fd|D              s|j                         * < |S )zReturns the list of input tensors necessary to compute `tensor`.

    Output will always be a list of tensors
    (potentially with 1 element).

    Args:
        tensor: The tensor to start from.

    Returns:
        List of input tensors.
    _keras_historyc              3   &   K   | ]  }|u 
 y wr\   rJ   )rK   txs     r   rM   z$get_source_inputs.<locals>.<genexpr>  s     >!1A:>s   )hasattrrj   _inbound_nodesis_inputr   flattenoutput_tensorsinput_tensorsget_source_inputsallr-   )tensor	operation
node_indexrg   nodesource_tensorsprevious_sourcesrm   s          @r   rt   rt     s     6+,%44Iz1I44x''
3==<< 3 344N,, 1#4V#< ) 1A>~>>&--a011 "!r   )r(   r%   )r   r(   r%   r   )NF)rO   numpyr+   	keras.srcr   keras.src.api_exportr   &keras.src.backend.common.backend_utilsr   r   r   r#   r8   rB   rF   rY   r^   ra   rh   rt   rJ   r   r   <module>r      s{       - D C)X6 Vz M` F-`1,&$ -." /"r   