
    2Vh$                         d dl mZ d dl mZ d dl mZ d dl mZ d dl mZ d dlmZ d dlm	Z	 d dl
mZ  ed	       G d
 de             Zy)    )backend)constraints)initializers)ops)regularizers)keras_export)	InputSpec)Layerzkeras.layers.GroupNormalizationc                   l     e Zd ZdZ	 	 	 	 	 	 	 	 	 	 	 d
 fd	Z fdZd Zd Zd Zd Z	d Z
 fd	Z xZS )GroupNormalizationa
  Group normalization layer.

    Group Normalization divides the channels into groups and computes
    within each group the mean and variance for normalization.
    Empirically, its accuracy is more stable than batch norm in a wide
    range of small batch sizes, if learning rate is adjusted linearly
    with batch sizes.

    Relation to Layer Normalization:
    If the number of groups is set to 1, then this operation becomes nearly
    identical to Layer Normalization (see Layer Normalization docs for details).

    Relation to Instance Normalization:
    If the number of groups is set to the input dimension (number of groups is
    equal to number of channels), then this operation becomes identical to
    Instance Normalization. You can achieve this via `groups=-1`.

    Args:
        groups: Integer, the number of groups for Group Normalization. Can be in
            the range `[1, N]` where N is the input dimension. The input
            dimension must be divisible by the number of groups.
            Defaults to 32.
        axis: Integer or List/Tuple. The axis or axes to normalize across.
            Typically, this is the features axis/axes. The left-out axes are
            typically the batch axis/axes. -1 is the last dimension in the
            input. Defaults to `-1`.
        epsilon: Small float added to variance to avoid dividing by zero.
            Defaults to 1e-3.
        center: If `True`, add offset of `beta` to normalized tensor.
            If `False`, `beta` is ignored. Defaults to `True`.
        scale: If `True`, multiply by `gamma`. If `False`, `gamma` is not used.
            When the next layer is linear (also e.g. `relu`), this can be
            disabled since the scaling will be done by the next layer.
            Defaults to `True`.
        beta_initializer: Initializer for the beta weight. Defaults to zeros.
        gamma_initializer: Initializer for the gamma weight. Defaults to ones.
        beta_regularizer: Optional regularizer for the beta weight. None by
            default.
        gamma_regularizer: Optional regularizer for the gamma weight. None by
            default.
        beta_constraint: Optional constraint for the beta weight.
            None by default.
        gamma_constraint: Optional constraint for the gamma weight. None by
            default.  Input shape: Arbitrary. Use the keyword argument
            `input_shape` (tuple of integers, does not include the samples
            axis) when using this layer as the first layer in a model.
            Output shape: Same shape as input.
        **kwargs: Base layer keyword arguments (e.g. `name` and `dtype`).

    Reference:

    - [Yuxin Wu & Kaiming He, 2018](https://arxiv.org/abs/1803.08494)
    c                    t        |   di | d| _        || _        || _        || _        || _        || _        t        j                  |      | _
        t        j                  |      | _        t        j                  |      | _        t        j                  |	      | _        t        j                  |
      | _        t        j                  |      | _        y )NT )super__init__supports_maskinggroupsaxisepsiloncenterscaler   getbeta_initializergamma_initializerr   beta_regularizergamma_regularizerr   beta_constraintgamma_constraint)selfr   r   r   r   r   r   r   r   r   r   r   kwargs	__class__s                b/home/dcms/DCMS/lib/python3.12/site-packages/keras/src/layers/normalization/group_normalization.pyr   zGroupNormalization.__init__C   s     	"6" $	
 , 0 01A B!-!1!12C!D , 0 01A B!-!1!12C!D*? +0@ A    c                    || j                      }|t        d| j                    d| d      | j                  dk(  r|| _        || j                  k  rt        d| j                   d| d      || j                  z  dk7  rt        d| j                   d	| d      t        t	        |      | j                   |i
      | _        | j                  r;| j                  |fd| j                  | j                  | j                        | _        nd | _        | j                  r;| j                  |fd| j                  | j                  | j                        | _        nd | _        t"        | I  |       y )NAxis z\ of input tensor should have a defined dimension but the layer received an input with shape .zNumber of groups (z.) cannot be more than the number of channels (z).r   z0) must be a multiple of the number of channels ()ndimaxesgamma)shapenameinitializerregularizer
constraintbeta)r   
ValueErrorr   r	   len
input_specr   
add_weightr   r   r   r)   r   r   r   r   r/   r   build)r   input_shapedimr    s      r!   r4   zGroupNormalization.build`   s   $))$;		{ #H-q"  ;;"DK$T[[M 2''*e2/ 
 !$T[[M 2..1U"6 
 $[!C(8
 ::f 22 2200 ) DJ DJ;;f 11 11// ( DI DIk"r"   c                     | j                  |      }| j                  ||j                        }t        j                  |t        j                  |            S )N)_reshape_into_groups_apply_normalizationr*   r   reshape)r   inputsreshaped_inputsnormalized_inputss       r!   callzGroupNormalization.call   sI    33F; 55V\\
 {{,cii.?@@r"   c                 |   t        j                  |      }t        |j                        }d|d<   t        |dd        D ]  \  }}|	||dz      ||dz   <    || j                     | j
                  z  || j                  <   |j                  | j                  | j
                         t        j                  ||      }|S )Nr&   r      )r   r*   list	enumerater   r   insertr:   )r   r;   r5   group_shapeier<   s          r!   r8   z'GroupNormalization._reshape_into_groups   s    ii'6<<(Ak!"o. 	8DAqy%0Q%7AE"	8 "-TYY!74;;!FDII499dkk2++fk:r"   c                 P   |j                   }t        j                  |d      }t        j                  ||      }t        t        dt        |j                                    }| j                  dk(  rdn| j                  dz
  }|j                  |       | j                  |      }t        j                  ||d      \  }}	t        j                  |	| j                  z         }
| j                  rEt        j                   | j"                  |      }t        j                  ||j                         }|
|z  }
| |
z  }| j$                  rEt        j                   | j&                  |      }t        j                  ||j                         }||z   }||
z  |z   }t        j                  ||      }|S )Nfloat32r@   r&   T)r(   keepdims)dtyper   result_typer   castrA   ranger1   r*   r   pop_create_broadcast_shapemomentsrsqrtr   r   r:   r)   r   r/   )r   r<   r5   inputs_dtypecompute_dtypegroup_reduction_axesr   broadcast_shapemeanvarianceinvr)   resr/   r=   s                  r!   r9   z'GroupNormalization._apply_normalization   s_   &,,++L)D ((?MB#E!S1F1F-G$HIYY"_r$))a-  &66{C"6
h
 ii4<</0::KK

O<EHHUO$9$9:E+Ceck;;;;tyy/:D88D/"7"78D*C+c1C7HH%6E  r"   c                     dgt        |      z  }|| j                     | j                  z  || j                  <   |j                  | j                  | j                         |S )Nr@   )r1   r   r   rC   )r   r5   rV   s      r!   rP   z*GroupNormalization._create_broadcast_shape   sQ    #K 00%0%;t{{%J		"tyy$++6r"   c           	          t        | j                  t              r| j                  g}n| j                  }|D ]=  }|t        |      k\  s|t        |       k  s!t	        d| d| d| j                          |S )Nr$   z" is out of bounds for input shape z. Received: axis=)
isinstancer   intr1   r0   )r   r5   r(   r   s       r!   compute_output_shapez'GroupNormalization.compute_output_shape   s    dii%II;D99D 	Ds;''43{3C2C+C D6 "##.- 0&&*ii[2 	 r"   c                    | j                   | j                  | j                  | j                  | j                  t        j                  | j                        t        j                  | j                        t        j                  | j                        t        j                  | j                        t        j                  | j                        t        j                  | j                        d}t        | A         }i ||S )N)r   r   r   r   r   r   r   r   r   r   r   )r   r   r   r   r   r   	serializer   r   r   r   r   r   r   r   r   
get_config)r   configbase_configr    s      r!   rb   zGroupNormalization.get_config   s    kkII||kkZZ , 6 6t7L7L M!-!7!78N8N!O , 6 6t7L7L M!-!7!78N8N!O*44T5I5IJ + 5 5d6K6K L
 g(*(+(((r"   )    r&   gMbP?TTzerosonesNNNN)__name__
__module____qualname____doc__r   r4   r>   r8   r9   rP   r_   rb   __classcell__)r    s   @r!   r   r      s]    4p   B:3#jA!!F) )r"   r   N)	keras.srcr   r   r   r   r   keras.src.api_exportr   keras.src.layers.input_specr	   keras.src.layers.layerr
   r   r   r"   r!   <module>rq      sA     ! "  " - 1 ( /0d) d) 1d)r"   