
    Vpf(                        d dl mZ d dlmZ d dlZd dlmZ d dlZd dlm	Z
 d dlmZ d dlmZ eZ	 	 	 	 	 dddZ	 	 	 	 d d!dZdS )"    )annotations)SequenceN)Any)	lax_numpy)lax)convolutionlhsjax.typing.ArrayLikefilter_shapeSequence[int]window_stridespaddingstr | Sequence[tuple[int, int]]lhs_dilationSequence[int] | Nonerhs_dilationdimension_numbers5convolution.ConvGeneralDilatedDimensionNumbers | None	precisionlax.Precision | Nonepreferred_element_typeDType | Nonereturn	jax.Arrayc	                n   t          j        |           }	t          |          }t          j        |	j        d|z   |          }|\  }
}}t          j        |          }|	j        |
d                  }t          j        ||	j	                  
                    |dz            }|
                    |df|z             }t          j        ||fd|j        dz
  z  z             }t          j        |d|d         |d         f          }t          j        |	|||||||dn|t          j        j        f||	
  
        }|S )
a
  Extract patches subject to the receptive field of `conv_general_dilated`.

  Runs the input through a convolution with given parameters. The kernel of the
  convolution is constructed such that the output channel dimension `"C"`
  contains flattened image patches, so instead a single `"C"` dimension
  represents, for example, three dimensions `"chw"` collapsed. The order of
  these dimensions is `"c" + ''.join(c for c in rhs_spec if c not in 'OI')`,
  where `rhs_spec == dimension_numbers[1]`, and the size of this `"C"`
  dimension is therefore the size of each patch, i.e.
  `np.prod(filter_shape) * lhs.shape[lhs_spec.index('C')]`, where
  `lhs_spec == dimension_numbers[0]`.

  Docstring below adapted from `jax.lax.conv_general_dilated`.

  See Also:
    https://www.tensorflow.org/xla/operation_semantics#conv_convolution

  Args:
    lhs: a rank `n+2` dimensional input array.
    filter_shape: a sequence of `n` integers, representing the receptive window
      spatial shape in the order as specified in
      `rhs_spec = dimension_numbers[1]`.
    window_strides: a sequence of `n` integers, representing the inter-window
      strides.
    padding: either the string `'SAME'`, the string `'VALID'`, or a sequence of
      `n` `(low, high)` integer pairs that give the padding to apply before and
      after each spatial dimension.
    lhs_dilation: `None`, or a sequence of `n` integers, giving the
      dilation factor to apply in each spatial dimension of `lhs`. LHS dilation
      is also known as transposed convolution.
    rhs_dilation: `None`, or a sequence of `n` integers, giving the
      dilation factor to apply in each spatial dimension of `rhs`. RHS dilation
      is also known as atrous convolution.
    dimension_numbers: either `None`, or a 3-tuple
      `(lhs_spec, rhs_spec, out_spec)`, where each element is a string
      of length `n+2`. `None` defaults to `("NCHWD..., OIHWD..., NCHWD...")`.
    precision: Optional. Either ``None``, which means the default precision for
      the backend, or a :class:`~jax.lax.Precision` enum value (``Precision.DEFAULT``,
      ``Precision.HIGH`` or ``Precision.HIGHEST``).
    preferred_element_type: Optional. Either ``None``, which means the default
      accumulation type for the input types, or a datatype, indicating to
      accumulate results to and return a result with that datatype.

  Returns:
    A rank `n+2` array containing the flattened image patches in the output
    channel (`"C"`) dimension. For example if
    `dimension_numbers = ("NcHW", "OIwh", "CNHW")`, the output has dimension
    numbers `"CNHW" = "{cwh}NHW"`, with the size of dimension `"C"` equal to
    the size of each patch
    (`np.prod(filter_shape) * lhs.shape[lhs_spec.index('C')]`).

     r   r   )dtype   )r   )r   r   r   N)
r	   rhsr   r   r   r   r   r   feature_group_countr   )jnpasarraytupler   conv_dimension_numbersshapemathprodeyer   reshapetilendimmoveaxisconv_general_dilatedr   	PrecisionDEFAULT)r	   r   r   r   r   r   r   r   r   	lhs_arraylhs_specrhs_specout_specspatial_size
n_channelsr    outs                    R/var/www/html/nettyfy-visnx/env/lib/python3.11/site-packages/jax/_src/lax/other.pyconv_general_dilated_patchesr9      sN   ~ k#)|$$,!8ov,.?A A "3(Hh<((,x{+* 	IO444<<\A=MNN#\1%455#zmdchl&;;<<#S&8A;"<==#(

#)!)	030E0G$3	 	 	# 
*    r    lax.PrecisionLikec	           
        t          j        |           }	t          j        |          }
t	          |
t
                    rt          |
          dk    r|
d         n|
}t          |	|||||||          }t          j	        |	j
        dt          |          z   |          \  }}}|d         g|d         g}}|dd         |dd         fdt          t          t                              fd	          D             t                    ||fff}t          j        ||||
          }t          j        |d|d         |d         f          }|S )a3  General n-dimensional unshared convolution operator with optional dilation.

  Also known as locally connected layer, the operation is equivalent to
  convolution with a separate (unshared) `rhs` kernel used at each output
  spatial location. Docstring below adapted from `jax.lax.conv_general_dilated`.

  See Also:
    https://www.tensorflow.org/xla/operation_semantics#conv_convolution

  Args:
    lhs: a rank `n+2` dimensional input array.
    rhs: a rank `n+2` dimensional array of kernel weights. Unlike in regular
      CNNs, its spatial coordinates (`H`, `W`, ...) correspond to output spatial
      locations, while input spatial locations are fused with the input channel
      locations in the single `I` dimension, in the order of
      `"C" + ''.join(c for c in rhs_spec if c not in 'OI')`, where
      `rhs_spec = dimension_numbers[1]`. For example, if `rhs_spec == "WHIO",
      the unfolded kernel shape is
      `"[output W][output H]{I[receptive window W][receptive window H]}O"`.
    window_strides: a sequence of `n` integers, representing the inter-window
      strides.
    padding: either the string `'SAME'`, the string `'VALID'`, or a sequence of
      `n` `(low, high)` integer pairs that give the padding to apply before and
      after each spatial dimension.
    filter_shape: a sequence of `n` integers, representing the receptive window
      spatial shape in the order as specified in
      `rhs_spec = dimension_numbers[1]`.
    lhs_dilation: `None`, or a sequence of `n` integers, giving the
      dilation factor to apply in each spatial dimension of `lhs`. LHS dilation
      is also known as transposed convolution.
    rhs_dilation: `None`, or a sequence of `n` integers, giving the
      dilation factor to apply in each input spatial dimension of `rhs`.
      RHS dilation is also known as atrous convolution.
    dimension_numbers: either `None`, a `ConvDimensionNumbers` object, or
      a 3-tuple `(lhs_spec, rhs_spec, out_spec)`, where each element is a string
      of length `n+2`.
    precision: Optional. Either ``None``, which means the default precision for
      the backend, a ``lax.Precision`` enum value (``Precision.DEFAULT``,
      ``Precision.HIGH`` or ``Precision.HIGHEST``) or a tuple of two
      ``lax.Precision`` enums indicating precision of ``lhs``` and ``rhs``.

  Returns:
    An array containing the unshared convolution result.

  In the string case of `dimension_numbers`, each character identifies by
  position:

  - the batch dimensions in `lhs`, `rhs`, and the output with the character
    'N',
  - the feature dimensions in `lhs` and the output with the character 'C',
  - the input and output feature dimensions in rhs with the characters 'I'
    and 'O' respectively, and
  - spatial dimension correspondences between `lhs`, `rhs`, and the output using
    any distinct characters.

  For example, to indicate dimension numbers consistent with the `conv` function
  with two spatial dimensions, one could use `('NCHW', 'OIHW', 'NCHW')`. As
  another example, to indicate dimension numbers consistent with the TensorFlow
  Conv2D operation, one could use `('NHWC', 'HWIO', 'NHWC')`. When using the
  latter form of convolution dimension specification, window strides are
  associated with spatial dimension character labels according to the order in
  which the labels appear in the `rhs_spec` string, so that `window_strides[0]`
  is matched with the dimension corresponding to the first character
  appearing in rhs_spec that is not `'I'` or `'O'`.

  If `dimension_numbers` is `None`, the default is `('NCHW', 'OIHW', 'NCHW')`
  (for a 2D convolution).
  r   r   )r	   r   r   r   r   r   r   r   r   r   Nc                     g | ]
}|         S  r>   ).0i
rhs_b_dimss     r8   
<listcomp>z.conv_general_dilated_local.<locals>.<listcomp>   s/     L L L!
1 L L Lr:   c                    |          S )Nr>   )k
lhs_b_dimss    r8   <lambda>z,conv_general_dilated_local.<locals>.<lambda>   s    JqM r:   )key)r   r   ))r"   r#   r   canonicalize_precision
isinstancer$   lenr9   r   r%   r&   sortedrangedot_generalr-   )r	   r    r   r   r   r   r   r   r   r1   c_precisionlhs_precisionpatchesr2   r3   r4   
lhs_c_dims
rhs_c_dimsdnr7   rE   rA   s                       @@r8   conv_general_dilated_localrV   |   s   ^ k#)*955+ [%
(
(-0-=-=-B-B !nn  )
#)	 	 	' "-!Covl 3 335F"H "H(Hh %QK=8A;-j*|*|*L L L LveC
OO.D.D2I2I2I2I(K (K (K L L L*j!!*	Z :z":;"iPPP#S(Xa[(1+$>??#	*r:   )NNNNN)r	   r
   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   )NNNN)r	   r
   r    r
   r   r   r   r   r   r   r   r   r   r   r   r   r   r;   r   r   )
__future__r   collections.abcr   r'   typingr   jaxjax._src.numpyr   r"   jax._src.laxr   r   DTyper9   rV   r>   r:   r8   <module>r^      s    # " " " " " $ $ $ $ $ $        



 + + + + + +       $ $ $ $ $ $ *.)-OS&*+/] ] ] ] ]L *.)-OS#'r r r r r r rr:   