
    Vpf0                   
   U d dl mZ d dlmZmZ d dlZd dlZd dlmZ d dl	Z	d dl
mZ d dlZ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 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 d dl m!Z!m"Z"m#Z# d dl$m%Z% d dl&m'Z' d dl(m)Z)m*Z*m+Z+ d dl,m-Z-m.Z. e-e/cZ/Z0e.e1cZ1Z2 eej3        d          Z4	 ddd"Z5dd'Z6dd*Z7 G d+ d,e          Z8 G d- d.ej9                  Z:d/d/ddd0dd7Z; G d8 d9e          Z<d/d/dd:dd=Z=d/d/dd:dd>Z>d/d/dd:dd?Z?d/d/dd:dd@Z@ ejA                    ZBdAeCdB<   dCd/d/ddDddHZDdI ZEd/d/dd:ddJZFddNZG	 dddVZH	 	 dddYZI	 ddd[ZJ	 ddd]ZKdd^ZLdd_ZMd` ZNda ZOdb ZP e#eNe"dc          ZQ ejR        eQeO           ePejS        eQ<   eQjT        dd             ZUde ZV ejW        eQeV           df ZXdg ZYdh ZZdi Z[dj Z\dk Z]dl Z^dm Z_dn Z` e#eXeYdo e!d           p          ZaeZejb        ea<   e[ejc        ea<   e]ejS        ea<   e^ejd        ea<   e_eje        ea<   e`ejf        ea<   dq Zg ejW        eaeg           dr Zhds Zidt Zjdu Zkdv Zl e#eheidw          Zmejejb        em<   ekejc        em<   elejS        em<   dx Zn ejW        emen           dy Zodz Zpd{ Zqd| Zrd} Zsd~ Ztd Zud Zvd Zwd Zxd Zyd Zzd Z{ e#eteod e!d           p          Z| ej}        e|ewd           exejc        e|<   eyejS        e|<   e{ejf        e|<   ddZ~d Z ejW        e|e           d Zd Zd Zd Zd Zd Zd Z e#eed e!d           p          Zeejb        e<   eejc        e<    eee          ejS        e<    e#eed e!d           p          Zd Z ej}        ed de           eejc        e<    eee          ejS        e<   d Z e#eed e!d           p          Z eee          ejS        e<    eee          ejb        e<    e#eed e!d           p          Z eee          ejS        e<    eee          ejb        e<   d Zd Z e#eed e!d           p          Zeejb        e<   eejc        e<    eee          ejS        e<   d Zd Z ejW        ee            ejW        ee            ejW        ee            ejW        ee            ejW        ee           d Zd Z ejW        eed           ddZdS )    )annotations)CallableSequenceN)partial)
NamedTuple)ad_util)config)core)dispatch)dtypes)source_info_util)util)ad)batching)mlir)partial_eval)lax)_argnum_weak_type_input_dtypestandard_primitive)ir)hlo)Array	ArrayLikeShape)safe_mapsafe_zipT)canonicalizeoperandr   start_indicesSequence[int]limit_indicesstridesSequence[int] | Nonereturnr   c                    t                               | t          |          t          |          |dnt          |                    S )a  Wraps XLA's `Slice
  <https://www.tensorflow.org/xla/operation_semantics#slice>`_
  operator.

  Args:
    operand: an array to slice
    start_indices: a sequence of ``operand.ndim`` start indices.
    limit_indices: a sequence of ``operand.ndim`` limit indices.
    strides: an optional sequence of ``operand.ndim`` strides.

  Returns:
    The sliced array

  Examples:
    Here are some examples of simple two-dimensional slices:

    >>> x = jnp.arange(12).reshape(3, 4)
    >>> x
    Array([[ 0,  1,  2,  3],
           [ 4,  5,  6,  7],
           [ 8,  9, 10, 11]], dtype=int32)

    >>> lax.slice(x, (1, 0), (3, 2))
    Array([[4, 5],
           [8, 9]], dtype=int32)

    >>> lax.slice(x, (0, 0), (3, 4), (1, 2))
    Array([[ 0,  2],
           [ 4,  6],
           [ 8, 10]], dtype=int32)

    These two examples are equivalent to the following Python slicing syntax:

    >>> x[1:3, 0:2]
    Array([[4, 5],
           [8, 9]], dtype=int32)

    >>> x[0:3, 0:4:2]
    Array([[ 0,  2],
           [ 4,  6],
           [ 8, 10]], dtype=int32)

  See Also:
    - :attr:`jax.numpy.ndarray.at`
    - :func:`jax.lax.slice_in_dim`
    - :func:`jax.lax.index_in_dim`
    - :func:`jax.lax.dynamic_slice`
  Nr    r"   r#   )slice_pbindtuple)r   r    r"   r#   s       T/var/www/html/nettyfy-visnx/env/lib/python3.11/site-packages/jax/_src/lax/slicing.pyslicer,   9   sK    f 
gU=-A-A$)-$8$8&-odd5>> 
 
K 
K K    Array | np.ndarray(Array | np.ndarray | Sequence[ArrayLike]slice_sizesr   c                    t          | |          }t          j        j        rt	          j        |          \  }}ng }t          j        |          }t          j	        | g||R dt          |          iS )a\  Wraps XLA's `DynamicSlice
  <https://www.tensorflow.org/xla/operation_semantics#dynamicslice>`_
  operator.

  Args:
    operand: an array to slice.
    start_indices: a list of scalar indices, one per dimension. These values
      may be dynamic.
    slice_sizes: the size of the slice. Must be a sequence of non-negative
      integers with length equal to `ndim(operand)`. Inside a JIT compiled
      function, only static values are supported (all JAX arrays inside JIT
      must have statically known size).

  Returns:
    An array containing the slice.

  Examples:
    Here is a simple two-dimensional dynamic slice:

    >>> x = jnp.arange(12).reshape(3, 4)
    >>> x
    Array([[ 0,  1,  2,  3],
           [ 4,  5,  6,  7],
           [ 8,  9, 10, 11]], dtype=int32)

    >>> dynamic_slice(x, (1, 1), (2, 3))
    Array([[ 5,  6,  7],
           [ 9, 10, 11]], dtype=int32)

    Note the potentially surprising behavior for the case where the requested slice
    overruns the bounds of the array; in this case the start index is adjusted to
    return a slice of the requested size:

    >>> dynamic_slice(x, (1, 1), (2, 4))
    Array([[ 4,  5,  6,  7],
           [ 8,  9, 10, 11]], dtype=int32)

  See Also:
    - :attr:`jax.numpy.ndarray.at`
    - :func:`jax.lax.slice`
    - :func:`jax.lax.dynamic_slice_in_dim`
    - :func:`jax.lax.dynamic_index_in_dim`
  r0   )_dynamic_slice_indicesr	   dynamic_shapesvaluer   _extract_tracers_dyn_shaper
   canonicalize_shapedynamic_slice_pr)   r*   )r   r    r0   dynamic_sizesstatic_sizess        r+   dynamic_slicer:   q   s    ` )-@@-  8"%"@"M"MM<<M*;77L		g 
? 
? 
? 
? 
?*/*=*=
? 
? ?r-   updateArray | Sequence[ArrayLike]c                H    t          | |          }t          j        | |g|R  S )a  Wraps XLA's `DynamicUpdateSlice
  <https://www.tensorflow.org/xla/operation_semantics#dynamicupdateslice>`_
  operator.

  Args:
    operand: an array to slice.
    update: an array containing the new values to write onto `operand`.
    start_indices: a list of scalar indices, one per dimension.

  Returns:
    An array containing the slice.

  Examples:
    Here is an example of updating a one-dimensional slice update:

    >>> x = jnp.zeros(6)
    >>> y = jnp.ones(3)
    >>> dynamic_update_slice(x, y, (2,))
    Array([0., 0., 1., 1., 1., 0.], dtype=float32)

    If the update slice is too large to fit in the array, the start
    index will be adjusted to make it fit

    >>> dynamic_update_slice(x, y, (3,))
    Array([0., 0., 0., 1., 1., 1.], dtype=float32)
    >>> dynamic_update_slice(x, y, (5,))
    Array([0., 0., 0., 1., 1., 1.], dtype=float32)

    Here is an example of a two-dimensional slice update:

    >>> x = jnp.zeros((4, 4))
    >>> y = jnp.ones((2, 2))
    >>> dynamic_update_slice(x, y, (1, 2))
    Array([[0., 0., 0., 0.],
           [0., 0., 1., 1.],
           [0., 0., 1., 1.],
           [0., 0., 0., 0.]], dtype=float32)

  See Also:
    - :attr:`jax.numpy.ndarray.at`
    - :attr:`lax.dynamic_update_index_in_dim`
    - :attr:`lax.dynamic_update_slice_in_dim`
  )r2   dynamic_update_slice_pr)   )r   r;   r    s      r+   dynamic_update_slicer?      s/    Z )-@@-		$Wf	E}	E	E	EEr-   c                  2    e Zd ZU dZded<   ded<   ded<   dS )GatherDimensionNumbersa}  
  Describes the dimension number arguments to an `XLA's Gather operator
  <https://www.tensorflow.org/xla/operation_semantics#gather>`_. See the XLA
  documentation for more details of what the dimension numbers mean.

  Args:
    offset_dims: the set of dimensions in the `gather` output that offset into
      an array sliced from `operand`. Must be a tuple of integers in ascending
      order, each representing a dimension number of the output.
    collapsed_slice_dims: the set of dimensions `i` in `operand` that have
      `slice_sizes[i] == 1` and that should not have a corresponding dimension
      in the output of the gather. Must be a tuple of integers in ascending
      order.
    start_index_map: for each dimension in `start_indices`, gives the
      corresponding dimension in the `operand` that is to be sliced. Must be a
      tuple of integers with size equal to `start_indices.shape[-1]`.

  Unlike XLA's `GatherDimensionNumbers` structure, `index_vector_dim` is
  implicit; there is always an index vector dimension and it must always be the
  last dimension. To gather scalar indices, add a trailing dimension of size 1.
  ztuple[int, ...]offset_dimscollapsed_slice_dimsstart_index_mapN__name__
__module____qualname____doc____annotations__ r-   r+   rA   rA      sB          * ''''""""""r-   rA   c                      e Zd ZdZ ej                    Z ej                    Z ej                    Ze	dd            Z
dS )GatherScatterModea^  
  Describes how to handle out-of-bounds indices in a gather or scatter.

  Possible values are:

  CLIP:
    Indices will be clamped to the nearest in-range value, i.e., such that the
    entire window to be gathered is in-range.
  FILL_OR_DROP:
    If any part of a gathered window is out of bounds, the entire window
    that is returned, even those elements that were otherwise in-bounds, will be
    filled with a constant.
    If any part of a scattered window is out of bounds, the entire window
    will be discarded.
  PROMISE_IN_BOUNDS:
    The user promises that indices are in bounds. No additional checking will be
    performed. In practice, with the current XLA implementation this means
    that out-of-bounds gathers will be clamped but out-of-bounds scatters will
    be discarded. Gradients will not be correct if indices are out-of-bounds.
  sstr | GatherScatterMode | Nonec                    t          | t                    r| S | dk    rt          j        S | | dk    s| dk    rt          j        S | dk    rt          j        S t          d|  d          )Nclipfilldroppromise_in_boundszUnknown gather mode "")
isinstancerM   CLIPFILL_OR_DROPPROMISE_IN_BOUNDS
ValueError)rN   s    r+   from_anyzGatherScatterMode.from_any  sy    !&'' hF{{##yAKK1;;++003q333444r-   N)rN   rO   )rF   rG   rH   rI   enumautorW   rX   rY   staticmethodr[   rK   r-   r+   rM   rM      sf         ( 
$,dikk
5 
5 
5 <
5 
5 
5r-   rM   Funique_indicesindices_are_sortedmode
fill_valuedimension_numbersr`   boolra   rb   rO   c          
        |t           j        }t                               |          }|t           j        k    r|t	          |           }	t          j        |	t          j                  rt          j	        }nt          j        |	t          j
                  rt          j        |	          j        }nat          j        |	t          j                  rt          j        |	          j        }n(|	t
          j        k    rd}nt!          d|	           nd}t"                              | ||t'          j        |          t+          |          t+          |          ||          S )a~  Gather operator.

  Wraps `XLA's Gather operator
  <https://www.tensorflow.org/xla/operation_semantics#gather>`_.

  The semantics of gather are complicated, and its API might change in the
  future. For most use cases, you should prefer `Numpy-style indexing
  <https://numpy.org/doc/stable/reference/arrays.indexing.html>`_
  (e.g., `x[:, (1,4,7), ...]`), rather than using `gather` directly.

  Args:
    operand: an array from which slices should be taken
    start_indices: the indices at which slices should be taken
    dimension_numbers: a `lax.GatherDimensionNumbers` object that describes
      how dimensions of `operand`, `start_indices` and the output relate.
    slice_sizes: the size of each slice. Must be a sequence of non-negative
      integers with length equal to `ndim(operand)`.
    indices_are_sorted: whether `indices` is known to be sorted. If
      true, may improve performance on some backends.
    unique_indices: whether the elements gathered from ``operand`` are
      guaranteed not to overlap with each other. If ``True``, this may improve
      performance on some backends. JAX does not check this promise: if
      the elements overlap the behavior is undefined.
    mode: how to handle indices that are out of bounds: when set to ``'clip'``,
      indices are clamped so that the slice is within bounds, and when
      set to ``'fill'`` or ``'drop'`` gather returns a slice full of
      ``fill_value`` for the affected slice. The behavior for out-of-bounds
      indices when set to ``'promise_in_bounds'`` is implementation-defined.
    fill_value: the fill value to return for out-of-bounds slices when `mode`
      is ``'fill'``. Ignored otherwise. Defaults to ``NaN`` for inexact types,
      the largest negative value for signed types, the largest positive value
      for unsigned types, and ``True`` for booleans.

  Returns:
    An array containing the gather output.
  NTz(Unsupported dtype for gather fill_value rd   r0   r`   ra   rb   rc   )rM   rY   r[   rX   _dtyper   
issubdtypenpinexactnansignedintegeriinfominunsignedintegermaxbool_rZ   gather_pr)   r
   r6   re   )
r   r    rd   r0   r`   ra   rb   rc   parsed_modedtypes
             r+   gatherrv     sE   X 
\.D!**400+%222Wooe		5"*	-	- 	MV

UB$455 M\%((,

UB$677 M\%((,

FL  

KEKKLLL  J	}0A)+66.))011 
 
 
 r-   c                  2    e Zd ZU dZded<   ded<   ded<   dS )ScatterDimensionNumbersa  
  Describes the dimension number arguments to an `XLA's Scatter operator
  <https://www.tensorflow.org/xla/operation_semantics#scatter>`_. See the XLA
  documentation for more details of what the dimension numbers mean.

  Args:
    update_window_dims: the set of dimensions in the `updates` that are window
      dimensions. Must be a tuple of integers in ascending
      order, each representing a dimension number.
    inserted_window_dims: the set of size 1 window dimensions that must be
      inserted into the shape of `updates`. Must be a tuple of integers in
      ascending order, each representing a dimension number of the output. These
      are the mirror image of `collapsed_slice_dims` in the case of `gather`.
    scatter_dims_to_operand_dims: for each dimension in `scatter_indices`, gives
      the corresponding dimension in `operand`. Must be a sequence of integers
      with size equal to `scatter_indices.shape[-1]`.

  Unlike XLA's `ScatterDimensionNumbers` structure, `index_vector_dim` is
  implicit; there is always an index vector dimension and it must always be the
  last dimension. To scatter scalar indices, add a trailing dimension of size 1.
  r!   update_window_dimsinserted_window_dimsscatter_dims_to_operand_dimsNrE   rK   r-   r+   rx   rx   e  sB          * $###%%%%------r-   rx   ra   r`   rb   scatter_indicesupdatesc                  t          j        t           j        t          j        t          j        | d                              \  }}t
                              | |||||||t                              |          	  	        S )a  Scatter-add operator.

  Wraps `XLA's Scatter operator
  <https://www.tensorflow.org/xla/operation_semantics#scatter>`_, where
  addition is used to combine updates and values from `operand`.

  The semantics of scatter are complicated, and its API might change in the
  future. For most use cases, you should prefer the
  :attr:`jax.numpy.ndarray.at` property on JAX arrays which uses
  the familiar NumPy indexing syntax.

  Args:
    operand: an array to which the scatter should be applied
    scatter_indices: an array that gives the indices in `operand` to which each
      update in `updates` should be applied.
    updates: the updates that should be scattered onto `operand`.
    dimension_numbers: a `lax.ScatterDimensionNumbers` object that describes
      how dimensions of `operand`, `scatter_indices`, `updates` and the output
      relate.
    indices_are_sorted: whether `scatter_indices` is known to be sorted. If
      true, may improve performance on some backends.
    unique_indices: whether the elements to be updated in ``operand`` are
      guaranteed to not overlap with each other. If true, may improve performance on
      some backends. JAX does not check this promise: if the updated elements
      overlap when ``unique_indices`` is ``True`` the behavior is undefined.
    mode: how to handle indices that are out of bounds: when set to 'clip',
      indices are clamped so that the slice is within bounds, and when
      set to 'fill' or 'drop' out-of-bounds updates are dropped. The behavior
      for out-of-bounds indices when set to 'promise_in_bounds' is
      implementation-defined.

  Returns:
    An array containing the sum of `operand` and the scattered updates.
  r   update_jaxprupdate_constsrd   ra   r`   rb   )	r   _reduction_jaxpradd_abstractify_constscatter_add_pr)   rM   r[   	r   r}   r~   rd   ra   r`   rb   jaxprconstss	            r+   scatter_addr     }    N &sw'*'7
7A8N8N'O'OQ Q-%			e.?+N%%d++	 
 
- 
- -r-   c                  t          j        t           j        t          j        t          j        | d                              \  }}t
                              | |||||||t                              |          	  	        S )a  Scatter-multiply operator.

  Wraps `XLA's Scatter operator
  <https://www.tensorflow.org/xla/operation_semantics#scatter>`_, where
  multiplication is used to combine updates and values from `operand`.

  The semantics of scatter are complicated, and its API might change in the
  future. For most use cases, you should prefer the
  :attr:`jax.numpy.ndarray.at` property on JAX arrays which uses
  the familiar NumPy indexing syntax.

  Args:
    operand: an array to which the scatter should be applied
    scatter_indices: an array that gives the indices in `operand` to which each
      update in `updates` should be applied.
    updates: the updates that should be scattered onto `operand`.
    dimension_numbers: a `lax.ScatterDimensionNumbers` object that describes
      how dimensions of `operand`, `start_indices`, `updates` and the output
      relate.
    indices_are_sorted: whether `scatter_indices` is known to be sorted. If
      true, may improve performance on some backends.
    unique_indices: whether the elements to be updated in ``operand`` are
      guaranteed to not overlap with each other. If true, may improve performance on
      some backends. JAX does not check this promise: if the updated elements
      overlap when ``unique_indices`` is ``True`` the behavior is undefined.
    mode: how to handle indices that are out of bounds: when set to 'clip',
      indices are clamped so that the slice is within bounds, and when
      set to 'fill' or 'drop' out-of-bounds updates are dropped. The behavior
      for out-of-bounds indices when set to 'promise_in_bounds' is
      implementation-defined.

  Returns:
    An array containing the sum of `operand` and the scattered updates.
     r   )	r   r   mulr   r   scatter_mul_pr)   rM   r[   r   s	            r+   scatter_mulr     r   r-   c                  t          j        t           j        t          j        t          j        | d                              \  }}t
                              | |||||||t                              |          	  	        S )a  Scatter-min operator.

  Wraps `XLA's Scatter operator
  <https://www.tensorflow.org/xla/operation_semantics#scatter>`_, where
  the `min` function is used to combine updates and values from `operand`.

  The semantics of scatter are complicated, and its API might change in the
  future. For most use cases, you should prefer the
  :attr:`jax.numpy.ndarray.at` property on JAX arrays which uses
  the familiar NumPy indexing syntax.

  Args:
    operand: an array to which the scatter should be applied
    scatter_indices: an array that gives the indices in `operand` to which each
      update in `updates` should be applied.
    updates: the updates that should be scattered onto `operand`.
    dimension_numbers: a `lax.ScatterDimensionNumbers` object that describes
      how dimensions of `operand`, `start_indices`, `updates` and the output
      relate.
    indices_are_sorted: whether `scatter_indices` is known to be sorted. If
      true, may improve performance on some backends.
    unique_indices: whether the elements to be updated in ``operand`` are
      guaranteed to not overlap with each other. If true, may improve performance on
      some backends. JAX does not check this promise: if the updated elements
      overlap when ``unique_indices`` is ``True`` the behavior is undefined.
    mode: how to handle indices that are out of bounds: when set to 'clip',
      indices are clamped so that the slice is within bounds, and when
      set to 'fill' or 'drop' out-of-bounds updates are dropped. The behavior
      for out-of-bounds indices when set to 'promise_in_bounds' is
      implementation-defined.

  Returns:
    An array containing the sum of `operand` and the scattered updates.
  r   r   )	r   r   ro   r   r   scatter_min_pr)   rM   r[   r   s	            r+   scatter_minr     r   r-   c                  t          j        t           j        t          j        t          j        | d                              \  }}t
                              | |||||||t                              |          	  	        S )a  Scatter-max operator.

  Wraps `XLA's Scatter operator
  <https://www.tensorflow.org/xla/operation_semantics#scatter>`_, where
  the `max` function is used to combine updates and values from `operand`.

  The semantics of scatter are complicated, and its API might change in the
  future. For most use cases, you should prefer the
  :attr:`jax.numpy.ndarray.at` property on JAX arrays which uses
  the familiar NumPy indexing syntax.

  Args:
    operand: an array to which the scatter should be applied
    scatter_indices: an array that gives the indices in `operand` to which each
      update in `updates` should be applied.
    updates: the updates that should be scattered onto `operand`.
    dimension_numbers: a `lax.ScatterDimensionNumbers` object that describes
      how dimensions of `operand`, `start_indices`, `updates` and the output
      relate.
    indices_are_sorted: whether `scatter_indices` is known to be sorted. If
      true, may improve performance on some backends.
    unique_indices: whether the elements to be updated in ``operand`` are
      guaranteed to not overlap with each other. If true, may improve performance on
      some backends. JAX does not check this promise: if the updated elements
      overlap when ``unique_indices`` is ``True`` the behavior is undefined.
    mode: how to handle indices that are out of bounds: when set to 'clip',
      indices are clamped so that the slice is within bounds, and when
      set to 'fill' or 'drop' out-of-bounds updates are dropped. The behavior
      for out-of-bounds indices when set to 'promise_in_bounds' is
      implementation-defined.

  Returns:
    An array containing the sum of `operand` and the scattered updates.
  r   r   )	r   r   rq   r   r   scatter_max_pr)   rM   r[   r   s	            r+   scatter_maxr     r   r-   zweakref.WeakKeyDictionary_scatter_apply_cacherK   )update_shapera   r`   rb   funcCallable[[Array], Array]r   c                  t          j        |d| j                  }fd}		 t                              |	          }	n# t
          $ r Y nw xY wt          j        |	t          j        t          j        |                               \  }
}t          
                    | |||
||||t                              |          	  	        S )a  Scatter-apply operator.

  Wraps `XLA's Scatter operator
  <https://www.tensorflow.org/xla/operation_semantics#scatter>`_, where values
  from ``operand`` are replaced with ``func(operand)``, with duplicate indices
  resulting in multiple applications of ``func``.

  The semantics of scatter are complicated, and its API might change in the
  future. For most use cases, you should prefer the
  :attr:`jax.numpy.ndarray.at` property on JAX arrays which uses
  the familiar NumPy indexing syntax.

  Note that in the current implementation, ``scatter_apply`` is not compatible
  with automatic differentiation.

  Args:
    operand: an array to which the scatter should be applied
    scatter_indices: an array that gives the indices in `operand` to which each
      update in `updates` should be applied.
    func: unary function that will be applied at each index.
    dimension_numbers: a `lax.ScatterDimensionNumbers` object that describes
      how dimensions of `operand`, `start_indices`, `updates` and the output
      relate.
    update_shape: the shape of the updates at the given indices.
    indices_are_sorted: whether `scatter_indices` is known to be sorted. If
      true, may improve performance on some backends.
    unique_indices: whether the elements to be updated in ``operand`` are
      guaranteed to not overlap with each other. If true, may improve performance on
      some backends. JAX does not check this promise: if the updated elements
      overlap when ``unique_indices`` is ``True`` the behavior is undefined.
    mode: how to handle indices that are out of bounds: when set to 'clip',
      indices are clamped so that the slice is within bounds, and when
      set to 'fill' or 'drop' out-of-bounds updates are dropped. The behavior
      for out-of-bounds indices when set to 'promise_in_bounds' is
      implementation-defined.

  Returns:
    An array containing the result of applying `func` to `operand` at the given indices.
  r   c                     |           S NrK   )x_r   s     r+   <lambda>zscatter_apply.<locals>.<lambda>n  s    Q r-   r   )r   fullru   r   
setdefault	TypeErrorr   r   _zero	scatter_pr)   rM   r[   )r   r}   r   rd   r   ra   r`   rb   unused_applyr   r   s     `         r+   scatter_applyr   >  s    ^ 8L!W]33&&	!,,T6::FF	 	 	 	D	&vs/?	'@R@R/S/STT-%	U.?+N%%d++	 
 
- 
- -s   ? 
AAc                    |S r   rK   )r   ys     r+   r   r   |  s    a r-   c               x    t                               | ||dd|||t                              |          	  	        S )a  Scatter-update operator.

  Wraps `XLA's Scatter operator
  <https://www.tensorflow.org/xla/operation_semantics#scatter>`_, where updates
  replace values from `operand`.

  If multiple updates are performed to the same index of operand, they may be
  applied in any order.

  The semantics of scatter are complicated, and its API might change in the
  future. For most use cases, you should prefer the
  :attr:`jax.numpy.ndarray.at` property on JAX arrays which uses
  the familiar NumPy indexing syntax.

  Args:
    operand: an array to which the scatter should be applied
    scatter_indices: an array that gives the indices in `operand` to which each
      update in `updates` should be applied.
    updates: the updates that should be scattered onto `operand`.
    dimension_numbers: a `lax.ScatterDimensionNumbers` object that describes
      how dimensions of `operand`, `start_indices`, `updates` and the output
      relate.
    indices_are_sorted: whether `scatter_indices` is known to be sorted. If
      true, may improve performance on some backends.
    unique_indices: whether the elements to be updated in ``operand`` are
      guaranteed to not overlap with each other. If true, may improve performance on
      some backends. JAX does not check this promise: if the updated elements
      overlap when ``unique_indices`` is ``True`` the behavior is undefined.
    mode: how to handle indices that are out of bounds: when set to 'clip',
      indices are clamped so that the slice is within bounds, and when
      set to 'fill' or 'drop' out-of-bounds updates are dropped. The behavior
      for out-of-bounds indices when set to 'promise_in_bounds' is
      implementation-defined.

  Returns:
    An array containing the sum of `operand` and the scattered updates.
  NrK   r   )r   r)   rM   r[   )r   r}   r~   rd   ra   r`   rb   s          r+   scatterr   ~  sE    T 
d*;+N%%d++	 
 
- 
- -r-   srcidxsaxesc           	     0    t          j        d |D             d          }t          j        t          j         fd|D                       t          t          |j        dz
                                }||z  }t           j	                  }|D ]}d||<   t          t          d j        |j	        d         z
  dz                       }t          |t          |          t          |                    }t           ||t          |                    S )Nc                8    g | ]}t          j        |d           S r   )r   expand_dims.0is     r+   
<listcomp>zindex_take.<locals>.<listcomp>  s$    DDD!S_Q55DDDr-   r   c                *    g | ]}j         |         S rK   shape)r   axr   s     r+   r   zindex_take.<locals>.<listcomp>  s    %C%C%Ccim%C%C%Cr-   rB   rC   rD   rd   r0   )r   concatenater   rj   arrayr*   rangendimlistr   rA   rv   )	r   r   r   indicesmax_idxr0   r   rB   dnumss	   `        r+   
index_taker     s   ODDtDDDaHH'OBH%C%C%C%Cd%C%C%CDD!%q(8"9"9::< <'g'SY+  bKOOeAsx'-*::Q>??@@+
  ;;Dkk# # #% 
W!+..
0 
0 
0 0r-   r   start_index
int | Nonelimit_indexstrideintaxisc                   dg| j         z  }t          | j                  }dg| j         z  }| j        |         }|t          j        |          nd}	|t          j        |          n|}
|	dk     r|	|z   }	|
dk     r|
|z   }
t          |          }|	||<   |
||<   t          j        |          ||<   t          | |||          S )a  Convenience wrapper around :func:`lax.slice` applying to only one dimension.

  This is effectively equivalent to ``operand[..., start_index:limit_index:stride]``
  with the indexing applied on the specified axis.

  Args:
    operand: an array to slice.
    start_index: an optional start index (defaults to zero)
    limit_index: an optional end index (defaults to operand.shape[axis])
    stride: an optional stride (defaults to 1)
    axis: the axis along which to apply the slice (defaults to 0)

  Returns:
    An array containing the slice.

  Examples:
    Here is a one-dimensional example:

    >>> x = jnp.arange(4)
    >>> lax.slice_in_dim(x, 1, 3)
    Array([1, 2], dtype=int32)

    Here are some two-dimensional examples:

    >>> x = jnp.arange(12).reshape(4, 3)
    >>> x
    Array([[ 0,  1,  2],
           [ 3,  4,  5],
           [ 6,  7,  8],
           [ 9, 10, 11]], dtype=int32)

    >>> lax.slice_in_dim(x, 1, 3)
    Array([[3, 4, 5],
           [6, 7, 8]], dtype=int32)

    >>> lax.slice_in_dim(x, 1, 3, axis=1)
    Array([[ 1,  2],
           [ 4,  5],
           [ 7,  8],
           [10, 11]], dtype=int32)

  See Also:
    - :attr:`jax.numpy.ndarray.at`
    - :func:`jax.lax.slice`
    - :func:`jax.lax.index_in_dim`
    - :func:`jax.lax.dynamic_slice_in_dim`
  r   r   )r   r   r   r
   _canonicalize_dimensionr   r,   )r   r   r   r   r   r    r"   r#   len_axisstart_index_intlimit_index_ints              r+   slice_in_dimr     s    d #$-w}%%-C',' ]4 (#/ 1+>>>56  $/ 1+>>>5=  q%0Oq%0O	T$'-'-.v66'$-	w}g	>	>>r-   indexkeepdimsc                F   t          j        |          t          |          }}| j        |         }|dk     r||z   n|}d|cxk    r|k     s(n d}t	          |                    |||                    t          | ||dz   d|          }|r|S t          j        ||f          S )a  Convenience wrapper around :func:`lax.slice` to perform int indexing.

  This is effectively equivalent to ``operand[..., start_index:limit_index:stride]``
  with the indexing applied on the specified axis.

  Args:
    operand: an array to index.
    index: integer index
    axis: the axis along which to apply the index (defaults to 0)
    keepdims: boolean specifying whether the output array should preserve the
      rank of the input (default=True)

  Returns:
    The subarray at the specified index.

  Examples:
    Here is a one-dimensional example:

    >>> x = jnp.arange(4)
    >>> lax.index_in_dim(x, 2)
    Array([2], dtype=int32)

    >>> lax.index_in_dim(x, 2, keepdims=False)
    Array(2, dtype=int32)

    Here are some two-dimensional examples:

    >>> x = jnp.arange(12).reshape(3, 4)
    >>> x
    Array([[ 0,  1,  2,  3],
           [ 4,  5,  6,  7],
           [ 8,  9, 10, 11]], dtype=int32)

    >>> lax.index_in_dim(x, 1)
    Array([[4, 5, 6, 7]], dtype=int32)

    >>> lax.index_in_dim(x, 1, axis=1, keepdims=False)
    Array([1, 5, 9], dtype=int32)

  See Also:
    - :attr:`jax.numpy.ndarray.at`
    - :func:`jax.lax.slice`
    - :func:`jax.lax.slice_in_dim`
    - :func:`jax.lax.dynamic_index_in_dim`
  r   z2index {} is out of bounds for axis {} with size {}r   )	r
   r   r   r   
IndexErrorformatr   r   squeeze)r   r   r   r   	axis_sizewrapped_indexmsgresults           r+   index_in_dimr     s    ^ ,U33SYY%mD!)',qyy%)##e-	
m	'	'	'	'i	'	'	'	'
>C
SZZtY77
8
880A1dKK& (M;vw'''r-   
slice_sizec                    t          j        |d          g| j        z  }t          | j                  }t          |          }|||<   t          j        |          ||<   t          | ||          S )a  Convenience wrapper around :func:`lax.dynamic_slice` applied to one dimension.

  This is roughly equivalent to the following Python indexing syntax applied
  along the specified axis: ``operand[..., start_index:start_index + slice_size]``.

  Args:
    operand: an array to slice.
    start_index: the (possibly dynamic) start index
    slice_size: the static slice size
    axis: the axis along which to apply the slice (defaults to 0)

  Returns:
    An array containing the slice.

  Examples:
    Here is a one-dimensional example:

    >>> x = jnp.arange(5)
    >>> dynamic_slice_in_dim(x, 1, 3)
    Array([1, 2, 3], dtype=int32)

    Like `jax.lax.dynamic_slice`, out-of-bound slices will be clipped to the
    valid range:

    >>> dynamic_slice_in_dim(x, 4, 3)
    Array([2, 3, 4], dtype=int32)

    Here is a two-dimensional example:

    >>> x = jnp.arange(12).reshape(3, 4)
    >>> x
    Array([[ 0,  1,  2,  3],
           [ 4,  5,  6,  7],
           [ 8,  9, 10, 11]], dtype=int32)

    >>> dynamic_slice_in_dim(x, 1, 2, axis=1)
    Array([[ 1,  2],
           [ 5,  6],
           [ 9, 10]], dtype=int32)

  See Also:
    - :attr:`jax.numpy.ndarray.at`
    - :func:`jax.lax.slice_in_dim`
    - :func:`jax.lax.dynamic_slice`
    - :func:`jax.lax.dynamic_index_in_dim`
  r   )	r   r   r   r   r   r   r
   r   r:   )r   r   r   r   r    r0   s         r+   dynamic_slice_in_dimr   H  sk    b %(J{A$>$>#?',#N-W]##+	T$#-2:>>+d	w{	;	;;r-   int | Arrayc                Z    t          | |d|          }|r|S t          j        ||f          S )a  Convenience wrapper around dynamic_slice to perform int indexing.

  This is roughly equivalent to the following Python indexing syntax applied
  along the specified axis: ``operand[..., index]``.

  Args:
    operand: an array to slice.
    index: the (possibly dynamic) start index
    axis: the axis along which to apply the slice (defaults to 0)
    keepdims: boolean specifying whether the output should have the same rank as
      the input (default = True)

  Returns:
    An array containing the slice.

  Examples:
    Here is a one-dimensional example:

    >>> x = jnp.arange(5)
    >>> dynamic_index_in_dim(x, 1)
    Array([1], dtype=int32)

    >>> dynamic_index_in_dim(x, 1, keepdims=False)
    Array(1, dtype=int32)

    Here is a two-dimensional example:

    >>> x = jnp.arange(12).reshape(3, 4)
    >>> x
    Array([[ 0,  1,  2,  3],
           [ 4,  5,  6,  7],
           [ 8,  9, 10, 11]], dtype=int32)

    >>> dynamic_index_in_dim(x, 1, axis=1, keepdims=False)
    Array([1, 5, 9], dtype=int32)

  See Also:
    - :attr:`jax.numpy.ndarray.at`
    - :func:`jax.lax.index_in_dim`
    - :func:`jax.lax.dynamic_slice`
    - :func:`jax.lax.dynamic_slice_in_dim`
  r   )r   r   r   )r   r   r   r   r   s        r+   dynamic_index_in_dimr     s9    Z  488& (M;vw'''r-   c                    t          |          }t          j        |d          gt          j        |           z  }|||<   t	          | ||          S )af  Convenience wrapper around :func:`dynamic_update_slice` to update
  a slice in a single ``axis``.

  Args:
    operand: an array to slice.
    update: an array containing the new values to write onto `operand`.
    start_index: a single scalar index
    axis: the axis of the update.

  Returns:
    The updated array

  Examples:

    >>> x = jnp.zeros(6)
    >>> y = jnp.ones(3)
    >>> dynamic_update_slice_in_dim(x, y, 2, axis=0)
    Array([0., 0., 1., 1., 1., 0.], dtype=float32)

    If the update slice is too large to fit in the array, the start
    index will be adjusted to make it fit:

    >>> dynamic_update_slice_in_dim(x, y, 3, axis=0)
    Array([0., 0., 0., 1., 1., 1.], dtype=float32)
    >>> dynamic_update_slice_in_dim(x, y, 5, axis=0)
    Array([0., 0., 0., 1., 1., 1.], dtype=float32)

    Here is an example of a two-dimensional slice update:

    >>> x = jnp.zeros((4, 4))
    >>> y = jnp.ones((2, 4))
    >>> dynamic_update_slice_in_dim(x, y, 1, axis=0)
    Array([[0., 0., 0., 0.],
           [1., 1., 1., 1.],
           [1., 1., 1., 1.],
           [0., 0., 0., 0.]], dtype=float32)

    Note that the shape of the additional axes in ``update`` need not
    match the associated dimensions of the ``operand``:

    >>> y = jnp.ones((2, 3))
    >>> dynamic_update_slice_in_dim(x, y, 1, axis=0)
    Array([[0., 0., 0., 0.],
           [1., 1., 1., 0.],
           [1., 1., 1., 0.],
           [0., 0., 0., 0.]], dtype=float32)

  See Also:
    - :attr:`jax.numpy.ndarray.at`
    - :func:`jax.lax.dynamic_update_slice`
    - :func:`jax.lax.dynamic_update_index_in_dim`
    - :func:`jax.lax.dynamic_slice_in_dim`
  r   )r   r   r   _ndimr?   )r   r;   r   r   r    s        r+   dynamic_update_slice_in_dimr     sO    p 
T$$'J{A$>$>#?#)GBTBT#T-#-	gv}	=	==r-   c                "   t          |          }t          j        |          t          j        |           k    rEt          j        |          dz   t          j        |           k    sJ t          j        ||f          }t	          | |||          S )al  Convenience wrapper around :func:`dynamic_update_slice` to update a slice
  of size 1 in a single ``axis``.

  Args:
    operand: an array to slice.
    update: an array containing the new values to write onto `operand`.
    index: a single scalar index
    axis: the axis of the update.

  Returns:
    The updated array

  Examples:

    >>> x = jnp.zeros(6)
    >>> y = 1.0
    >>> dynamic_update_index_in_dim(x, y, 2, axis=0)
    Array([0., 0., 1., 0., 0., 0.], dtype=float32)

    >>> y = jnp.array([1.0])
    >>> dynamic_update_index_in_dim(x, y, 2, axis=0)
    Array([0., 0., 1., 0., 0., 0.], dtype=float32)

    If the specified index is out of bounds, the index will be clipped to the
    valid range:

    >>> dynamic_update_index_in_dim(x, y, 10, axis=0)
    Array([0., 0., 0., 0., 0., 1.], dtype=float32)

    Here is an example of a two-dimensional dynamic index update:

    >>> x = jnp.zeros((4, 4))
    >>> y = jnp.ones(4)
    >>> dynamic_update_index_in_dim(x, y, 1, axis=0)
    Array([[0., 0., 0., 0.],
          [1., 1., 1., 1.],
          [0., 0., 0., 0.],
          [0., 0., 0., 0.]], dtype=float32)

    Note that the shape of the additional axes in ``update`` need not
    match the associated dimensions of the ``operand``:

    >>> y = jnp.ones((1, 3))
    >>> dynamic_update_index_in_dim(x, y, 1, 0)
    Array([[0., 0., 0., 0.],
           [1., 1., 1., 0.],
           [0., 0., 0., 0.],
           [0., 0., 0., 0.]], dtype=float32)

  See Also:
    - :attr:`jax.numpy.ndarray.at`
    - :func:`jax.lax.dynamic_update_slice`
    - :func:`jax.lax.dynamic_update_index_in_dim`
    - :func:`jax.lax.dynamic_index_in_dim`
  r   )r   r   r   r   r   )r   r;   r   r   s       r+   dynamic_update_index_in_dimr     s    t 
T$Yv#)G,,,,9Vq CIg$6$66666_VdW--F	$WfeT	B	BBr-   c               l   t          j        dd|           t          j        dd|           | j        t          |          k    r*d}t	          |                    || j                            t          |          t          |          k    r%d}t	          |                    ||                    t          t          t          j
        | j        |                    s*d}t	          |                    || j                            t          d |D                       s$d}t	          |                    |                    t          j        j        sMt          t          t          j
        ||                    s%d	}t	          |                    ||                    t          t          t          j        ||                    }|(t          |          d
t          | j                  z  k    r|S t          j        dd|           t          |          | j        k    r*d}t	          |                    || j                            t          d |D                       s$d}t	          |                    |                    t          d t!          ||          D                       S )Nr,   r    r"   z{slice start_indices must have length equal to the number of dimensions of the operand, got indices {} for operand shape {}.zjslice limit_indices must have the same length as start_indices, got start_indices {} and limit_indices {}.zkslice limit_indices must be less than or equal to operand shape, got limit_indices {} for operand shape {}.c              3  "   K   | ]
}|d k    V  dS r   NrK   )r   sis     r+   	<genexpr>z$_slice_shape_rule.<locals>.<genexpr>D  s&      --R1W------r-   zSslice start_indices must be greater than or equal to zero, got start_indices of {}.znslice limit_indices must be greater than or equal to start_indices, got start_indices {} and limit_indices {}.r   r#   zuslice strides must have length equal to the number of dimensions of the operand, got strides {} for operand shape {}.c              3  "   K   | ]
}|d k    V  dS r   rK   r   rN   s     r+   r   z$_slice_shape_rule.<locals>.<genexpr>V  s&      %%Q!V%%%%%%r-   z&slice strides must be positive, got {}c              3  J   K   | ]\  }}t          j        |d |          V  dS )r   )window_sizewindow_strideN)r
   
stride_dim)r   drN   s      r+   r   z$_slice_shape_rule.<locals>.<genexpr>Y  sK       / /1a qaqAAA / / / / / /r-   )r   _check_shapeliker   lenr   r   r   allmapoperatorger	   r3   r4   r*   subzip)r   r    r"   r#   r   diffs         r+   _slice_shape_ruler   5  s   w???w???\S''''MC
CJJ}gm<<
=
==3}----8C
CJJ}m<<
=
==	Sgm];;	<	< >8C
CJJ}gm<<
=
==	--}---	-	- /&C
CJJ}--
.
..			$ @s8;}==>> @:ccjj>>???	s8<>>	?	?$_g$W]1C1C*CCCKw	7333\\W\!!CC
CJJw66
7
77	%%W%%%	%	% )
2C
CJJw''
(
((	 / /tW--/ / / 
/ 
/ /r-   c                  t          j        |          sJ |j        j        }|'t	          j        t	          j        |d                    r5t          |t	          j        ||          dt          |          z            }nt	          j
        |t	          j        t	          j        | j                  dk    dt	          j
        dt	          j        t	          j        | j        d          |                                        }t          |t	          j        ||          t	          j        |d                    }t          j        | t          j        | d          |          }|j        |k    sJ d|j        d|            |gS )Nr   r   r   zresult.shape=z operand_shape=)r   is_undefined_primalavalr   rj   r   equalr   subtractr   r   wherer   multiplyr   padr   )	tr   r    r"   r#   operand_shapepadsreal_limitsr   s	            r+   _slice_transpose_ruler  \  s^   			(	((((,$-_rx3344_}bk-GGc-(((* *DD &hrx  A%qvaR[!%<%<gFFGGI IJ JK }bk-EE{7A&&( (D71cjA&&--&		&	&	&(K6<(K(K=(K(K	&	&	&
/r-   c               <   | \  }|\  }t          |          }|                    |d           t          |          }|                    ||j        |                    |d }	n%t          |          }	|	                    |d           t          ||||	          }
|
|fS )Nr   r   )r   insertr   r,   )batched_args
batch_dimsr    r"   r#   r   bdimnew_start_indicesnew_limit_indicesnew_stridesouts              r+   _slice_batching_ruler  n  s    ('
%$=))4###=))4t!4555_KKw--KtQg(*;[II#	dr-   r,   c                    |t          j        t          | |||          S t          t	          j        |          t	          j        |          z
            }t          j        t          | g|R d|iS )Nr'   r0   )r   apply_primitiver(   r*   rj   r   r7   )r   r    r"   r#   r0   s        r+   _slice_implr    s    #q!74 4 4 4 bh}--0G0GGHH+		!/1 
;} 
; 
; 
;.9
; 
; ;r-   c               t    |pdgt          |          z  }| j        \  }t          j        | |||||          gS )Nr   r'   )r   	avals_outr   slice_op)ctxr   r    r"   r#   aval_outs         r+   _slice_lowerr!    sU    /sS///'m)(
-Q&3=Zac c c 
d dr-   c               0   t          j        || j        g          \  }}| j        t          |          k    r*d}t	          |                    || j                            t          |          t          |          k    r2d}t	          |                    t          |          |                    |sWt          t          t          j
        | j        |                    s*d}t	          |                    || j                            |s=t          d |D                       s$d}t	          |                    |                    t          d |D                       rt	          d|           t          t          j        ||                    S )Nzdynamic_slice start_indices must have length equal to the number of dimensions of the operand, got indices {} for operand shape {}.zudynamic_slice slice_sizes must have the same length as start_indices, got start_indices length {} and slice_sizes {}.zgslice slice_sizes must be less than or equal to operand shape, got slice_sizes {} for operand shape {}.c              3  "   K   | ]
}|d k    V  dS r   rK   )r   sszs     r+   r   z,_dynamic_slice_shape_rule.<locals>.<genexpr>  s&      99c999999r-   zOslice slice_sizes must be greater than or equal to zero, got slice_sizes of {}.c              3  ,   K   | ]}|j         d k    V  dS r   r   r   idxs     r+   r   z,_dynamic_slice_shape_rule.<locals>.<genexpr>  (      003Q000000r-   zGstart_indices arguments to dynamic_slice must be scalars,  got indices )r   
split_listr   r   r   r   r   r   r   r   r   anyr*   r   _merge_dyn_shape)r   r0   starts_and_dyn_sizesr    dynr   s         r+   _dynamic_slice_shape_ruler/    s   ';gl^LL-\S''''PC
CJJ}gm<<
=
==3{++++LC
CJJs=11;??
@
@@	 <SX['-EEFF <6C
CJJ{GM::
;
;;	 -S99[99999 -$C
CJJ{++
,
,,00-00000 5
 4$14 4 5 5 5	s#K55	6	66r-   c                  t          j        || j        g          \  }t          fdD                       rAd}t	          |                    d                    d D                                           | j        S )Nc              3     K   | ]?}|j         d          j         k    p$t          j        |j         t          j                   V  @dS r   ru   r   ri   rj   integerr   r   r    s     r+   r   z,_dynamic_slice_dtype_rule.<locals>.<genexpr>  j       	L 	L89 
M!$*	* 
4qw
33	3	L 	L 	L 	L 	L 	Lr-   zKindex arguments to dynamic_slice must be integers of the same type, got: {}, c              3  .   K   | ]}|j         j        V  d S r   ru   namer   s     r+   r   z,_dynamic_slice_dtype_rule.<locals>.<genexpr>  &      (M(M!(M(M(M(M(M(Mr-   )r   r*  r   r+  r   r   joinru   )r   r0   r-  r.  r   r    s        @r+   _dynamic_slice_dtype_ruler<    s    ';gl^LL- 	L 	L 	L 	L=J	L 	L 	L L L PC
CJJtyy(M(M}(M(M(MMMNN
O
OO	r-   c                   |d         }t          |          t          j        urt          j        |g| dd          R d|i}t          j        | d         g| dd          R d|i|fS )Nr   r   r0   )typer   Zeror7   r)   )primalstangentsr0   tangent_outs       r+   _dynamic_slice_jvprC    s{    +	+gl**!&{ZWQRR[ZZZkZZK		gaj	P7122;	P	P	PK	P	PR]	]]r-   c                  t          j        |          sJ t          d |D                       sJ |j        j        |j        j        }}t          |           t          j        u r.t          j        |j                  gd gt          |          z  z   S t          j        |d|          }t          j        || g|R  gd gt          |          z  z   S )Nc              3  @   K   | ]}t          j        |           V  d S r   r   r  r   s     r+   r   z0_dynamic_slice_transpose_rule.<locals>.<genexpr>  0      BBq'***BBBBBBr-   r   )r   r  r   r  r   ru   r>  r   r?  r   r   r   r>   r)   )r
  r   r0   r    r  operand_dtypezeross          r+   _dynamic_slice_transpose_rulerJ    s    			(	((((	BBMBBB	B	BBBB!(!3W\5G-	!WWL&&'4&3}3E3E*EEEH]A}55E#(BMBBBCFS'''( )r-   c                v   t          |           dk    rt          j        g d          d fS t                      }t	          d t          | |          D             |          |u r!t          j        d | D             d          d fS t          j        fdt          | |          D             d          } | dfS )Nr   int32c              3  :   K   | ]\  }}||j         |         V  d S r   r   )r   r   r   s      r+   r   z/_batch_dynamic_slice_indices.<locals>.<genexpr>  s,      KKdaQ]qwqz]]]]KKr-   c                8    g | ]}t          j        |d           S r   )r   	broadcastr   s     r+   r   z0_batch_dynamic_slice_indices.<locals>.<listcomp>  s$    DDDqCM!T22DDDr-   c                P    g | ]"\  }}t          j        |d f|dnd          #S )r   Nr  rK   )broadcast_dimensions)r   broadcast_in_dim)r   r   r   sizes      r+   r   z0_batch_dynamic_slice_indices.<locals>.<listcomp>  sU     & & &	A 	!dAY892P P P & & &r-   r   )	dimension)r   rj   r   objectnextr   r   r   )r   bdimsempty_markerrS  s      @r+   _batch_dynamic_slice_indicesrY    s    \\Q8B  $&&,	KKc'5&9&9KKK
 
$	\?DDGDDDaHH$NNO& & & &We$$& & & 	  '
 
!r-   c          
     ~   | ^}}|^}}|j         |t          j        u rdndz
  }t          t	          |                    }t          j        ||g          \  }	}
t          j        ||g          \  }}t          |d|          }t          |	|          \  }}t          ||g|
||g|||ddt          j        d           S )Nr   r   rK   r   Trg   )r   r   
not_mappedr*   r   r   r*  rA   rY  _gather_batching_rulerM   rY   )r  r  r0   r   start_indices_and_dyn
operand_bdstart_idx_and_dyn_bdsndimsdimsr    dyn_slice_sizesstart_idx_bdsdyn_slice_size_bdsr   r   
index_bdims                   r+   _dynamic_slice_batching_rulerf    s     %1!'!'1$*$
,zX-@@@!!a
H%	uU||		$#'?3H5'#R#R -&*o6KeW&U&U#-#
 T157 7 7%2=-PP%	e&o&101UDT		,	
? 
? 
? ?r-   c               8   t          j        ||j        g          \  }}|s.|                     t          |g|R t          |                    S t          j        ||          }t          j	        ||j
        d          }t          j        | t          ||g|R d|iS )N)r0   Fr0   )r   r*  r   default_process_primitiver7   dictr   r,  r
   DShapedArrayru   _dyn_shape_staging_rule)tracer   r0   r-  r    r.  r   r  s           r+   _dynamic_slice_staging_rulerm    s    ';afXFF-	 J**?Q<O<O<O+/K+H+H+HJ J J

{C
0
0%		5!'5	1	1$		$UOT1 
>&:
> 
> 
>1<
> 
> >r-   c               r   t          j        ||j        j        g          \  }}|s,t	          j        |j        gd |D             R d|i\  }}|g|fS t          j        ||          }d |D             }t          j	        t          |          |j        j        |j        j                  }|gt          j        fS )Nc              3  $   K   | ]}|j         V  d S r   )r  r   r   s     r+   r   z0_dynamic_slice_typecheck_rule.<locals>.<genexpr>  s$      00Q!&000000r-   r0   c                V    g | ]&}t          |          t          j        u r|j        n|'S rK   )r>  r
   Literalvalrp  s     r+   r   z1_dynamic_slice_typecheck_rule.<locals>.<listcomp>	  s0    LLLQ$q''T\11qLLLr-   )r   r*  r  r   r7   abstract_evalr   r,  r
   rj  r*   ru   	weak_type
no_effects)	r   r   r0   r-  r    r.  out_avaleffects	out_shapes	            r+   _dynamic_slice_typecheck_rulerz     s    ';afk]KK-	 
''5	K00-000K K K>IK KHg:w $[#66ILL)LLLI y!1!116<!"!13 3H:t&&r-   c                  t          j        | d|j        g          \  }}}t          j        ||j        g          \  }}	d t          ||	          D             }
t	          j        ||
          }d |D             }t          |||          gS )Nr   c                p    g | ]3\  }}t          |j                  t          j        u r|j        j        n|4S rK   )r>  ru   r
   bintbound)r   ar   s      r+   r   z/_dynamic_slice_padding_rule.<locals>.<listcomp>  sH     
+ 
+ 
+a  ==DI55!'--1 
+ 
+ 
+r-   c                V    g | ]&}t          |          t          j        u r|j        n|'S rK   )r>  r
   DArrayrs  rp  s     r+   r   z/_dynamic_slice_padding_rule.<locals>.<listcomp>  s0    MMM!Q4;..quuAMMMr-   )r   r*  r   r   r   r,  r:   )in_avals	out_avalsr   r0   starts_and_dynx_avalstart_indices_avals	dyn_avalsr    r.  dyn_slice_sizes_	start_idxs                r+   _dynamic_slice_padding_ruler    s    +/?8a[+Q+Q(&
y~x@@-
+ 
+)S))
+ 
+ 
+$%k488,MM}MMM)
9l
3
3	44r-   r:   )weak_type_rulec                   | j         ^}}t          j        ||j        g          \  }}| j        \  }|r)|                    t          j        ||                    }t          j	        | |||          gS )Nr   r    )
avals_inr   r*  r   r  r;   r   r,  r   r:   )	r  r   r0   r-  r  r   r    r.  r   s	            r+   _dynamic_slice_lowerr  "  sw    |*&1';fk]KK-m)( MS%9+s%K%KLLH

S(A]
K
K
K	LLr-   c                *   | j         |j         k    r/d}t          |                    |j        | j                            | j         t	          |          k    r*d}t          |                    || j                            t          t          t          j        | j        |j                            s/d}t          |                    |j        | j                            t          d |D                       rt          d|           | j        S )Nzidynamic_update_slice update must have the same rank as operand, got update shape {} for operand shape {}.zvdynamic_update_slice start_indices must have length equal to the rank of operand, got indices {} for operand shape {}.zodynamic_update_slice update shape must be smaller than operand shape, got update shape {} for operand shape {}.c              3  ,   K   | ]}|j         d k    V  dS r   r&  r'  s     r+   r   z3_dynamic_update_slice_shape_rule.<locals>.<genexpr>B  r)  r-   zMstart_indices arguments to dynamic_update_slice must be scalars, got indices )
r   r   r   r   r   r   r   r   r   r+  r   r;   r    r   s       r+    _dynamic_update_slice_shape_ruler  5  s   \V[  7C
CJJv|W];;
<
<<\S''''CC
CJJ}gm<<
=
==	SgmV\::	;	; =>C
CJJv|W];;
<
<<00-00000 =
 <,9< < = = =	r-   c                    t          j        d| |           t          fdD                       rAd}t          |                    d                    d D                                           | j        S )Nr?   c              3     K   | ]?}|j         d          j         k    p$t          j        |j         t          j                   V  @dS r   r2  r4  s     r+   r   z3_dynamic_update_slice_dtype_rule.<locals>.<genexpr>I  r5  r-   zQindex arguments to dynamic_update_slice must be integers of the same type, got {}r6  c              3  .   K   | ]}|j         j        V  d S r   r8  r   s     r+   r   z3_dynamic_update_slice_dtype_rule.<locals>.<genexpr>M  r:  r-   )r   check_same_dtypesr+  r   r   r;  ru   r  s     ` r+    _dynamic_update_slice_dtype_ruler  G  s    .@@@ 	L 	L 	L 	L=J	L 	L 	L L L PC
CJJtyy(M(M}(M(M(MMMNN
O
OO	r-   c                   | d d         \  }}| dd          }|d d         \  }}t          j        ||g|R  }t          |          t          j        u r;t          |          t          j        u r t          j                            |          }n;t          j        |          }t          j        |          }t          j        ||g|R  }||fS )N   )r>   r)   r>  r   r?  
from_valuer   instantiate_zeros)	r@  rA  r   r;   r    	g_operandg_updateval_outrB  s	            r+   _dynamic_update_slice_jvpr  P  s    BQBK/'6!""+- !)X"'H-HHH'	)__$$h7<)G)G,))'22KK$Y//I#H--H(-iRMRRRK	+	r-   c                   t          d |D                       sJ t          j        |          r|j        j        }n|j        }t          |           t          j        u r_t          j        |          rt          j        |j                  nd }t          j        |          rt          j        |j                  nd }npt          j	        }t          j	        }t          j        | |          }	t          j        |          r
 || |	g|R  nd }t          j        |          r || g|R d|ind }||gd gt          |          z  z   S )Nc              3  @   K   | ]}t          j        |           V  d S r   rF  )r   r   s     r+   r   z7_dynamic_update_slice_transpose_rule.<locals>.<genexpr>^  rG  r-   r   r0   )r   r   r  r  r   r>  r   r?  r>   r)   r7   r   _zerosr   )
r
  r   r;   r    r   	operand_tupdate_tdusdsrI  s
             r+   $_dynamic_update_slice_transpose_ruler  ]  sV   	BBMBBB	B	BBBBF##  ;$LL<L	!WW.0.DW.M.MWW\***SWI,.,B6,J,JTw|FK(((PTHH
 
%C		BJq---E131G1P1PZAu-}----VZIBDBXY_B`B`jrr!>m>>>>>>fjH
X	$#m*<*<!<	<<r-   c           
        | ^}}}|^}}}|t           j        u rt          j        |          n3t	          t          j        t          j        |          |                    }t	          t          t          |                              }	t          |	d|	          }
t          ||          \  }} t          j        t          t          |
ddt          j                  |||fd          |||          dfS )NrK   ry   rz   r{   Trd   ra   r`   rb   r   )in_axesout_axes)r   r[  rj   r   r*   deleter   r   rx   rY  jaxvmapr   r   rM   rW   )r  r  r   r;   r  r^  	update_bdstart_idx_bdr   ra  r   r   re  s                r+   #_dynamic_update_slice_batching_ruler  n  s   
 !-'6I)3&*i,&/83F&F&F"(6"""RYrx'7'7CCDD 	uS&&''	(	($
!T79?CE E E% 39lKK%
Gu#D"') ) ) Y/
 
 

 
( 
(
 *+
+ +r-   r?   c                H    | j         \  }t          j        | ||||          gS )Nr  )r  r   r?   )r  r   r;   r    r   s        r+   _dynamic_update_slice_lowerr    s:    m)(

#C1f2?A A A 
B Br-   c                   t          j        |j        t          j                  st          d          t          j        | j        d          S )N!indices must have an integer typeTallow_extended_dtype)r   ri   ru   rj   r3  rZ   canonicalize_dtype)r   r   rc   kwargss       r+   _gather_dtype_ruler    sB    		7="*	5	5 :
8
9
99		"7=t	L	L	LLr-   c                *    t          | j                  S r   )r   r   )arrs    r+   r   r     s    C	NN r-   c                    t          dt          |                     D ].}| |         | |dz
           k     rt          | d| d|            /d S )Nr    in z op must be sorted; got )r   r   r   )ra  op_namer9  r   s       r+   
_is_sortedr    so    CII L LaAwa!eJJ7JJDJJKKK L Lr-   c                    t          |           dk    rd S d }| d         dk     r	| d         }n| d         |k    r| d         }|rt          d| d| d| d| d	          d S )Nr   zInvalid z set in z op; valid range is [0, z); got: .)r   r   )ra  rankr  r9  invalid_dims        r+   _sorted_dims_in_ranger    s    YY!^^
F+	!Wq[[q'KKBx4r(K 9
 8t 8 8W 8 88 8)48 8 8 9 9 99 9r-   c                    t          t          |                     t          |           k    rt          | d| d|  d          d S )Nr  z op must not repeat; got: r  )r   setr   )ra  r  r9  s      r+   _no_duplicate_dimsr    sN    T^^s4yy  
tKKKKDKKK
L
LL ! r-   c          
        |j         }|j        }	|j        }
t          |          dz
  }t          |          |k     s|dk     r#t	          dt          |           d| d          t          |dd           t          |dd           t          |          }t          |          t          |          z   dz
  }t          |          D ]/}||         }|dk     s||k    rt	          d| d	| d
| d          0t          |
          |j	        |         k    r1t	          dt          |
           d|d|j	        |          d          t          t          |
                    D ]I}|
|         }|dk     s|t          |           k    r&t	          dt          |            d| d| d          Jt          |
dd           t          |	dd           t          |	t          |           dd           t          |	dd           t          |           t          |          k    r/t	          dt          |           dt          |                      t          |          t          |          t          |	          z   k    r&t	          dt          |           d| d|	 d          t          t          |                    D ]<}||         }| j	        |         }|dk    r||k    st	          d| d| d| d          =t          t          |	                    D ]5}||	|                  }|dk    rt	          d| d|	|          d| d          6t          |||          S ) aI  Validates the well-formedness of the arguments to Gather.

  The code implements the checks based on the detailed operation semantics of
  XLA's `Gather <https://www.tensorflow.org/xla/operation_semantics#gather>`_
  operator and following the outline of the implementation of
  ShapeInference::InferGatherShape in TensorFlow.
  r   r   zTGather index leaf dimension must be within [0, rank(indices) + 1). rank(indices) is z$ and gather index leaf dimension is r  rv   rB   zOffset dimension z$ in gather op is out of bounds; got z, but should have been in [0, )zGather op has zI elements in start_index_map and the bound of dimension index_vector_dim= of indices is z". These two numbers must be equal.z'Invalid start_index_map; domain is [0, ), got: ->rD   rC   zTGather op must have one slice size for every input dimension; got: len(slice_sizes)=z, input_shape.rank=zAll components of the offset index in a gather op must either be a offset dimension or explicitly collapsed; got len(slice_sizes)=z, output_slice_sizes=z, collapsed_slice_dims=zSlice size at index z2 in gather op is out of range, must be within [0, z + 1), got zBGather op can only collapse slice dims with bound 1, but bound is z for index z at position )rB   rC   rD   _rankr   r  r  r   r   r   r  _gather_shape_computation)r   r   rd   r0   r`   ra   rb   rc   rB   rC   rD   index_vector_dimoutput_offset_dim_countoutput_shape_rankr   
offset_dimoperand_dim_for_start_index_ir   corresponding_input_sizer~  s                       r+   _gather_shape_ruler    s    "-+*?%5/ 7^^a' 7^^&&&*:Q*>*>
 J7<W~~J J6FJ J J K K K [(M222[(M:::,,+&&w7!;()) 3 3aQJA~~'888 2! 2 2'2 2.2 2 2 3 3 3 9
 	W]+;<<<
 .S%9%9 . .'. . }%56. . . / / / _%%&& @ @a$3A$6!%))%w77 ?"7^^? ?? ?;? ? ? @ @ @ 	8
 _h0ABBB !8-CDDD,eGnnh.0 0 0)85KLLL 7^^s;''''
 98;K8H8H9 9(-g9 9 : : : 	[))C0D,E,EEEE
 /,/,<,</ / +6/ / ,	/ / / 0 0 0 [!!"" , ,aQJ&}Q/!OO$
22 +Q + +,D+ +'+ + + , , , 3
 )**++ E Ea,Q/0Ezz D&+D D-a0D D?@D D D E E E 
 
#7,={	K	KKr-   c                  	
 |j         	|j        t          	          t          |           z   dz
  }t          |           dz
  }t	          | j                  }t          |          |k    r|                    d           |                    |           t          |          fdt          |          D             
t          	
fdt          |          D                       }|S )Nr   c              3  *   K   | ]\  }}|v	|V  d S r   rK   )r   r   rN   rC   s      r+   r   z,_gather_shape_computation.<locals>.<genexpr>(  s>       7 741a!555 55557 7r-   c              3  \   K   | ]&}|v rt                    nt                    V  'd S r   )rV  )r   r   indices_shape_genrB   slice_sizes_gens     r+   r   z,_gather_shape_computation.<locals>.<genexpr>*  s`       N N/0 ()K'7'7d?###)**N N N N N Nr-   )rB   rC   r   r  r   r   appendpopiter	enumerater*   r   )r   rd   r0   r  r  expanded_indices_shapeansrC   r  rB   r  s          @@@@r+   r  r    s,   !-+*?+&&w7!;7^^a'.. 		  $444!!!$$$-...1227 7 7 79[#9#9 7 7 7/ N N N N N N49:K4L4LN N N 	N 	N#	*r-   c                  |}t          t          j        t          j                  }	t	          j        | j                  }
t	          j        |t          j                  }t          |j                  dz
  }|
 |	|j	                           t	          j        |           |	|j	                           z
  }t	          j
        t	          j        |t          j        d                    t	          j        |t	          j        |t          t          |                                                  }t	          j        ||g          }|t          |j                  z   }t          j        t          j        |          |j                  }t)          | ||||t*          j                  }t	          j        t	          j        |||          |t	          j        ||                    S )zHLowers a FILL_OR_DROP gather as a PROMISE_IN_BOUNDS gather with masking.ru   r   r   )ra   rb   )rc   )r   rj   r   int64r   shape_as_valuer   convert_element_typer   rD   bitwise_andr   ler   r*   r   _reduce_andrB   r  arangerv   rM   rY   selectrR  	full_like)r   r   rd   r0   r`   ra   rc   output_shaper   intarrayoperand_dimsnum_batch_dimsupper_boundmaskoutput_ndimsbatch_dims_in_output
gather_outs                    r+   _gather_fillr  /  s    %RXRX...(#GM22,$Wbh77'w}%%). 88E1223	%%hhu/D&E&EFG  
	fWbhqkk""	fWcok5~9N9N3O3OPPQQ
S 
S$ 
/	0	0$  #e&7"8"88,29\#:#:#(#46 6
 gw{);,>@ @ @* 
|-ABBjZ@@@
B 
B Br-   c          
     0    t          | ||||||d          S )Nr   r_   )rv   )	gr   r   rd   r0   r`   ra   rb   rc   s	            r+   _gather_jvp_ruler  Q  s/     
7-{-#5D
 
 
 r-   c          	     t   t          j        |          sJ |j        j        }	t	          |           t
          j        u rt          j        |j                  }
n^t          j        |	t          j	        |                     }t          |j        |j        |j                  }t          ||| ||||          }
|
d gS )Nr  )r`   ra   rb   )r   r  r  r   r>  r   r?  r   r   r   rx   rB   rC   rD   r   )r
  r   r   rd   r0   r`   ra   rb   rc   r  r  rI  scatter_dnumss                r+   _gather_transpose_ruler   Y  s     
		(	((((,$-	!WW
,w|
$
$CCH]CIaLL11E+*6,A#4#DF F FM eWa%3);! ! !C tr-   c          
        | ^}}	}
|^}}}d |
D             }||t          j        ||d          \  }}|j        d         f|z   }dt          t	          j        d|j                            z   }t          t	          j        d|j                            }t          t	          j        d|j                            }t          |||          }t          |t           j                  rt          j        ||          }t          t          j        ||
          |          D ]@\  }}t          |t           j                  r!t#          j        ||j                  st(          At          j        |j        t/          |	||                    }n|}t1          ||	|t          j        ||          ||||          |fS |k|it          j        |	|d          }	t          d |j        D                       }t          ||j        |j                  }t1          ||	||dd||          dfS t          j        ||d          }t          j        |	|d          }	t#          j        |j        d         d          rt7          t#          j        |j        dd          |j                  t#          j        |	j        dd          t=          j        |	j                            ||||||          }t          j         d|z   t          j!        |                    dfS tE          |	j        |	j        d                   }tG          |	j                  }d|d	<   t          j$        |t          |          d          }t          j%        ||	&                    |          gtO          |          dz
            }	d
|z   }dt          t	          j        d|j                            z   }t          t	          j        d|j                            }dt          t	          j        d|j                            z   }t          |||          }t1          ||	||||||          dfS )Nc                &    g | ]}|j         j        S rK   )ru   r~  )r   bs     r+   r   z)_gather_batching_rule.<locals>.<listcomp>q  s    BBBQ17=BBBr-   r   r  r   r   rg   c              3      K   | ]	}d |z   V  
dS )r   NrK   rp  s     r+   r   z(_gather_batching_rule.<locals>.<genexpr>  s&      EE!AEEEEEEr-   Fr  r   )(r   move_stacked_axisr   r*   rj   r   rB   rC   rD   rA   rV   
RaggedAxisbdim_as_shaper   r   r,  IndexedAxisSizer
   same_referentlengthsNotImplementedErrorshape_as_bdimstacked_axisr  rv   moveaxisdefinitely_equalr  ShapedArrayru   r   r  r   r   _promote_dtype_for_sizer   broadcasted_iotar   astyper   )r  r  rd   r0   r`   ra   rb   rc   r   r   rb  operand_bdimindices_bdimrd  dyn_slice_size_boundsrB   rC   rD   r   ragged_slice_sizesorig
fabricatedbdim_outr  index_dtypecount_shapecountss                              r+   r\  r\  l  s    (4$'7_4>1,1BB/BBB,"6$6waPPG\=#%3Krva):)FGGHHHK +<+Q!R!RSSBF1&7&GHHIIO"1') ) )E , 344 #1,LL!

{O
<
<
  & &
$
 j(":;; 		&#D**<== & &%'

#
#GU4F
G
GI Ihh hE(6KLL%-D  
 !)) )  8q99GEE'8'DEEEEEK".C)9; ; ;E '7e)%%**N N NOPQ Q q99Gq99G W]1-q11 B'

7=,gm
<
<

7=,!4W]CCE E-;'<N
, , ,l Xd\)39W+=+=>>AA *'-q9IJJKw}%%KKO!+u[/A/A1EEFovw~~k'B'BC!+..24 4G $K%q2C2X(Y(Y"Z"ZZq"3"?@@AAKU26!->-N#O#OPPPO"1') ) )E '7e).%7d') ) ) +,, ,r-   c                   t          j        | t          j                  s| S 	 t	          |          }n:#  t          j        |           j        dk    r| nt          j        d          cY S xY w|t          j        |           j        k    r| S |t          j        t          j	                  j        k    rt          j        d          S t          j
        t          j                  S )N    rL  )r   ri   rj   r3  r   rn   bitsru   rq   rL  r  r  )ru   rS  s     r+   r  r    s    		5"*	-	- LFt99DDFHUOO(B..55BHW4E4EEEE	RXe__   Lrx!!%%%8G$RX...s	   3 5A*c                   | \  }
}t          d |
j        D                       rt          |t          j        k    rt          t          ||||||	          gS )Nc              3  J   K   | ]}t          |t          j                  V  d S r   )rV   peBoundedAxisSizerp  s     r+   r   z#_gather_pad_rule.<locals>.<genexpr>  s/      GGqAr)	*	*GGGGGGr-   rd   r0   rb   rc   )r+  r   r  rM   rY   rv   )r  r  r   r   rd   r0   r`   ra   rb   rc   operand_avalindices_avals               r+   _gather_pad_ruler(    sz      (,GGL4FGGGGG 
	000

'5F(t
L L L 
M Mr-   rv   ir.Valuec          
        | j         \  }	}
| j        \  |	j        j                            |	j                  j        }fdt          t          |                    D             }|                    g |j	        |R           }g ||R }t          t          ||||||          }t          j        | |||t          j        |	          |
gt          j                  g          \  }|S )Nc                $    g | ]}j         |z   S rK   r&  )r   r   aval_ys     r+   r   z(_gather_lower_opaque.<locals>.<listcomp>  s    IIIa&+/IIIr-   )rB   rg   r  r  )r  r  ru   _rulesphysical_element_avalr   r   r   _replacerB   r   _gather_lowerr   delegate_loweringr
   physical_aval)r  r   r   rd   r0   r`   ra   rb   rc   aval_xaval_indices	elt_shapetrailing_offset_dimsgather_lowerresr,  s                  @r+   _gather_lower_opaquer:    s    &,M'&l!77EEK)IIII5Y3H3HIII'00I%1I4HII 1 K K*+*	**+'8n+$:O O O, 
		<'"6**L9#F++,
. 
. 
.$# 
*r-   c                  | j         \  }	t          j        |	j        t          j                  rt          | ||||||||	  	        gS |t          j        k    r4t          j	        t          d          }
 |
| ||||||||	j        	  	        S |t          j        t          j        fv s
J |            t          j                            t#          |j                  g g t'          | j        d         j                  dz
  t#          |j                  t#          |j                            }t/          j        |          sxt          j        | |          }t          j        |	          g}|||g}|t6          j                            |          d}t          j                            |||          j        S t          j         |||t          j!        |          t6          j                            |          	          gS )
Nrg   Fmultiple_results)rd   r0   r`   ra   rc   r  r   )rC   operand_batching_dimsstart_indices_batching_dimsr  rB   rD   )rd   ra   )resultsoperands
attributes)ra   )"r  r   ri   ru   extendedr:  rM   rX   r   	lower_funr  r   rY   rW   r   rA   getr   rC   r   r  rB   rD   r
   is_constant_shapeeval_dynamic_shape_as_tensoraval_to_ir_typer   BoolAttrDynamicGatherOpbuild_genericr@  rv   dense_int_array)r  r   r   rd   r0   r`   ra   rb   rc   r   gather_fill_fnr   r@  rA  rB  s                  r+   r1  r1  
  s7    m)(x~v77   Wg1B-D	       
+++^L5IIIN>Wg+%:LHN	< < < < 
#5#(* 
* 
* 
*+/
* 
* 
*

$
(
( 1 FGG"$3<?011A5(455,<== )  % 
		,	, A3CEEK #H--.G+.H" koo.@AA J ,,(z - C CCJK J[));??+=>>@ @ @ A Ar-   c                    t          j        |j        t          j                  st          d          t          j        d| |           t          j        | j        d          S )Nr  r   Tr  )	r   ri   ru   rj   r3  rZ   r   r  r  )r   r   r~   r  s       r+   _scatter_dtype_rulerO  @  sW    		7="*	5	5 :
8
9
99	7G444		"7=t	L	L	LLr-   c          
         |j         |j        |j        }	t          |          dz
  }
t          |          |
k     s|
dk     r#t	          dt          |           d|
 d          t          |j                  }t          |          |
k    r|                    d           t          |          dz
  t                    z   }t          |          |k    r#t	          d| dt          |           d          t          dd	           t          dd	           t          t          |          dd	           t          dd
           t          dd
           t          t                     dd
           t                    t                    z   }t                     |k    r#t	          d| dt                      d          t          |	          |j        |
         k    r1t	          dt          |	           d|
d|j        |
          d          t          t          |	                    D ]I}|	|         }|dk     s|t                     k    r&t	          dt                      d| d| d          Jt          |	dd            fdt          t           j                            D             }t          t                              D ]K}|         }||         |j        |         k     r*t	          d| d|j        |          d||          d          Lfdt          t          |                    D             }d}|D ]b}||
k    r|dz  }t          j        |j        |         ||                   s*t	          d| d|j        |          d||          d          |dz  }c j        S )ad  Validates the well-formedness of the ``dimension_numbers`` argument to
  Scatter.

  The code implements the checks based on the detailed operation semantics of
  XLA's `Scatter <https://www.tensorflow.org/xla/operation_semantics#scatter>`_
  operator and following the outline of the implementation of
  ShapeInference::InferScatterShape in TensorFlow.
  r   r   zUScatter index leaf dimension must be within [0, rank(indices) + 1). rank(indices) is z% and scatter index leaf dimension is r  zUpdates tensor must be of rank z; got r   ry   rz   zScatter op has window of size z ; doesn't match operand of rank zScatter op has zV elements in scatter_dims_to_operand_dims and the bound of dimension index_vector_dim=r  z!. These two numbers must be equalz<Invalid scatter_dims_to_operand_dims mapping; domain is [0, r  r  r{   c                L    g | ] }|t                    vj        |         !S rK   )r  r   )r   r   rz   r   s     r+   r   z'_scatter_shape_rule.<locals>.<listcomp>  s@     C C C#$,@(A(A#A#A $M!,#A#A#Ar-   zBounds of the window dimensions of updates must not exceed the bounds of the corresponding dimensions of operand. For dimension z, updates bound is z, operand bound is c                6    g | ]}|t                    v|S rK   )r  )r   dimry   s     r+   r   z'_scatter_shape_rule.<locals>.<listcomp>  sB     2 2 2/00B1 B1 B1 B1 B1r-   zBounds of the scatter dimensions of updates must be the same as the bounds of the corresponding dimensions of scatter indices. For scatter dimension z, indices bound is )ry   rz   r{   r  r   r   r   r   r  r  r  r  r   r
   r  )r   r   r~   r   r   rd   ra   r`   rb   r{   r  r  expected_updates_rankr   r   rS  max_update_slice_sizesupdate_window_dimupdate_scatter_dimsscatter_dims_seenrz   ry   s   `                   @@r+   _scatter_shape_rulerY  F  sn    );*?!2!O 7^^a' 7^^&&&*:Q*>*>
 O<A'NNO O;KO O O P P P  .. 		  $444!!!$$$566:1223 7^^,,,
 -6K - - >>- - - . . . ,@AAA'4HIII*E'NNI,. . . !9.DEEE)96LMMM,eGnni.0 0 0 &''#.B*C*CC+
7^^{""
 ?[ ? ?-27^^? ? ? @ @ @ 

&''m$%& &
 -c*F&G&G - -$4- - #*-0@"A- - - . . . 12233 E Ea
&q
)C
Qww#w'' D %gD D89D D=@D D D E E E ( 1935 5 5C C C C CeC<N<N6O6O C C C '(()) @ @a*1-a 7=1B#CCC ?0A? ? #*-0A"B? ? #9";	? ? ? @ @ @ D2 2 2 2eGnn(=(= 2 2 2  	 	a,,,1 q!13IJ[3\]] G FCDF F #*-"2F F 00AB	F F F G G G
 	r-   c          
     
    g d}t          t           j                            D ]Q}||j        v r                    d           !                    |j        |j        |                             |dz  }Rt           fd|j        D                       }t          j	        |          }t          j
        |t          j        t          j        t          j        |j                  j                  t          j                            }t          j        ||j        t          |j                  dz
  f          }t          j        t          j        d          t          j        |t          j                  |          S )z.Clamps `indices` to be in-range for a scatter.r   r   c              3  D   K   | ]}j         |         |         z
  V  d S r   r   )r   r   r   r0   s     r+   r   z)_clamp_scatter_indices.<locals>.<genexpr>  sO       #P #P'( $+=#3k!n#D #P #P #P #P #P #Pr-   )r   r   r   rz   r  ry   r*   r{   r   r  ro   r  rj   uint64rn   ru   rq   r  rR  clamp)	r   r   r~   r   posr   upper_boundsr  r0   s	   `       @r+   _clamp_scatter_indicesr`    s   +	#W]##$$  aE&&&u'?'DEFFF	Qhcc" #P #P #P #P #P,1,N#P #P #P P P, "<00+028GM;R;R;V1W1W24(< <= =+ $['-&)'-&8&81&<%>@ @+	28A;; 8"( K K
  
   r-   c                  | \  }}	}
|\  }}}~t                               ||	|
||||||	  	        }t          |          t          j        u r;t          |          t          j        u r t          j                            |          }nKt          j        |          }t          j        |          }t                               ||	|||||||	  	        }||fS )Nr   )r   r)   r>  r   r?  r  r   r  )r@  rA  r   r   rd   ra   r`   rb   r   r   r~   r  	g_indices	g_updatesr  rB  s                   r+   _scatter_add_jvprd    s     &'7G$,!)Y	wl!5F+N	   '
 
)__$$iGL)H)H,))'22KK$Y//I$Y//I$$7IL#7H-n	 %  K
 
+	r-   c               (   t          j        |          rJ t          j        |          r|j        j        }
n|j        }
t	          |           t
          j        u r_t          j        |          rt          j        |j                  nd }t          j        |          rt          j        |j                  nd }nd x}}t          j        |          r| }t          j        |          rt          |j        |j	        |j
                  }g }d}t          t          | j                            D ]L}||j	        v r|                    d           !|                    |
|j        |                             |dz  }Mt          | ||||	d          }|d |gS )Nr   r   r   r%  )r   r  r  r   r>  r   r?  rA   ry   rz   r{   r   r   r  rv   r
  r   r   r~   r   r   rd   ra   r`   rb   updates_shaper  r  gather_dnumsr0   r^  r   s                    r+   _scatter_add_transpose_ruleri    s    #G,,,,,G$$ "L&MMMM	!WW.0.DW.M.MWW\***SWI-/-CG-L-LVw|GL)))RVHHI	g&& i	g&& J+%8.C)FH H Hl kcS\\""  !!666


Q






]+<+OPS+TU
V
V
V
(##7l$/dqJ J Jh
T8	$$r-   c          	        t          j        |          rJ t          j        |          r|j        j        }
n|j        }
t	          |           t
          j        u r`t          j        |          rt          j        |j                  nd }t          j        |          rt          j        |j                  nd }nd x}}t          j        |          rt          | ||||||	          }t          j        |          r|st          d          t          |j
        |j        |j                  }g }d}t          t          | j                            D ]L}||j        v r|                    d           !|                    |
|j
        |                             |dz  }Mt!          t#          j        | |          ||||	d          }|d |gS )Nr  Cscatter_mul gradients are only implemented if `unique_indices=True`r   r   r   r%  )r   r  r  r   r>  r   r?  r   r  rA   ry   rz   r{   r   r   r  rv   r   r   rf  s                    r+   _scatter_mul_transpose_rulerl     s    #G,,,,,G$$ "L&MMMM	!WW.0.DW.M.MWW\***SWI-/-CG-L-LVw|GL)))RVHHI	g&& 
Wg1B/  i 
g&& 1 Q!
OQ Q 	Q+%8.C)FH H Hl kcS\\""  !!666


Q






]+<+OPS+TU
V
V
V
(##7++W*6K!a1 1 1h T8	$$r-   c               r   |\  }	}
}|\  }}}t          d t          ||          D                       }t          j        |	||          }	d}t          j        |||          }|t	          t          j        d|j                            }dt	          t          j        d|j                            z   }t	          t          j        d|j	                            }t          |||          }|                     |	|
|||||||	  	        dfS t          j        |
||          }
t          |
j                  }d|d<   t          j        |
j        t	          |          d          }t          j        ||
gt%          |          dz
            }
t	          t          j        d|j                            }dt	          t          j        d|j                            z   }dt	          t          j        d|j	                            z   }t          |||          }|                     |	|
|||||||	  	        dfS )Nc              3  :   K   | ]\  }}||j         |         V  d S r   r   )r   r   r   s      r+   r   z)_scatter_batching_rule.<locals>.<genexpr>0  s9       ! !ea gbk! !r-   r   r   r  r  )rd   ra   r`   rb   r   r   r  )rV  r   r   bdim_at_frontr*   rj   r   rz   ry   r{   rx   r)   r   r   r   r  ru   r   r   )
scatter_opr  r  r   r   rd   ra   r`   rb   r   r   r~   r  r  updates_bdimrS  rz   ry   r{   r   r  r  s                         r+   _scatter_batching_rulerr  (  s{    +'7G-7*,l 
 ! !s<'D'D ! ! ! 
! 
!$"7L$??',"7L$??' +<+Q!R!RSSbfQ0A0T&U&U V VV#(3D3a)b)b#c#c #-1%AC C CE ??w5+Nl-  I I KLL L "7L$??'W]##++b/u[/A/A1EE&OVW-s;/?/?!/CDD'RVA'8'KLLMMbfQ0A0V&W&W X XX!%bfQ8I8f.g.g(h(h!h
!+/#?A A A% 
w5+Nl- 
 
I 
I KL
L Lr-   zscatter-addzscatter-mulc                   |st          d          t          j        |t          t	          j        |          || ||||                    S )Nrk  r  )r  r   r   r   r   zeros_like_jaxval)	r  r   r   r   rd   ra   r`   rb   kws	            r+   _scatter_mul_jvp_rhsrv  f  sg    	 M
KM M M	K""Aq<M+N   
 
 r-   c                *    t          j        | ||fi |S r   )r   r)   )r  r   r   r   ru  s        r+   r   r   q  s    =#5aA#D#D#D#D r-   c	                l   |\  }	}
}|\  }}}|}|j         }|                     |	|
|||||||	  	        }t          |          t          j        u r<t          |          t          j        u r!t          j                            |          }nt          j        |          }t          j        |          }t          |j	        |j
        |j                  }g }d}t          t          |	j                             D ]L}||j
        v r|                    d           !|                    ||j	        |                             |dz  }Mt          |	|
|t!          j        |                    }t          ||
|t!          j        |                    }||k    }||k    }t          t%          t'          j        |	          |
t'          j        |t'          j        |          t'          j        |                    |          |
|t!          j        |                    }t          t%          t'          j        |	          |
t'          j        |          |          |
|t!          j        |                    }t'          j        |d|dz   z  d|z            }t'          j        ||t'          j        |                    }t'          j        |d|dz   z  t'          j        |                    }d|z   |z  } t          ||
|t!          j        |                    }!|!| z  ||z  z   }"t%          ||
|"||||          }||fS )Nr   r   r   r   g      ?g      r|   )r   r)   r>  r   r?  r  r   r  rA   ry   rz   r{   r   r   r  rv   rj   r   r   r   r  r  _ones)#rp  r@  rA  r   r   rd   ra   r`   rb   r   r   r~   r  rb  rc  r  rg  r  rB  rh  r0   r^  r   initial_valstarget_valssuccessful_updatesretained_valuesnum_updatesnum_refsupdates_normalizerupdates_coefoperand_normalizeroperand_coeftarget_tangentstangent_updatess#                                      r+   _scatter_extremal_jvpr  x  s    &'7G$,!)Y	#---OOwl!]+#$	  0 0' 
)__$$iGL)H)H,))'22KK$Y//I$Y//I *!4*?%BD D DL
 K
C3w}%%&&  	
m0	0	01=)I#)NOPPPq ,(=(=? ?L ,(=(=? ?K "[0#{2OJwJ)39W+=+=z'**, ,		 	
 	
 K CJw''Ig&&!	# 	# 	
 H O$';?$;$'+$57 7 :00!j113 3L O$';?$;$'J{$;$;= = --9L 7L"(;*?*?A AO '5 </0O i%-+1C-;#') ) )K 
+	r-   zscatter-minzscatter-maxc               "   |t          d          | \  }}	}
|\  }}}|}t          |          t          j        u r_t          |          t          j        u rDt                              ||	|
||||||	  	        }|t          j                            |          fS t          j        |          }t          j        |          }|rJt                              ||	|
||||d|	  	        }t                              ||	|||||d|	  	        }||fS t          |
j
                  }|j        D ]}d||<   t          j        |          }t          j        |          rC|dz   t!          j        t           j                  j        k     rt           j        nt           j        }nt           j        }t+          j        t+          j        t+          j        ||          |          t+          j        |
|                    }t5          t+          j        |j
        d|          |	|||||          }t9          |j        |j        |j                  }g }d}t?          tA          |j
                            D ]Q}||j        v r|!                    d           !|!                    |
j
        |j        |                             |dz  }RtE          ||	||	          }t+          j#        t+          j$        |t+          j%        |                    |t+          j%        |                    }t+          j#        t+          j$        ||          |
t+          j%        |
                    }t+          j#        t+          j$        |t+          j%        |                    |t+          j%        |                    }t+          j#        t+          j$        ||          |t+          j%        |                    }tM          ||	|||||
          }tM          ||	|||||
          }||fS )Nz!scatter_apply JVP not implementedr   Tr   r  r   r|   r   r   r  )'r  r>  r   r?  r   r)   r  r   r  r   r   ry   mathprodr
   is_constant_dimrj   rn   uint32rq   r\  r   r   reshapeiotary  r   r   rA   rz   r{   r   r   r  rv   r  eqr  r   ) r@  rA  r   r   rd   ra   r`   rb   r   r   r~   r  rb  rc  r   r  rB  	ids_shape
update_dimnum_idsid_dtype
update_idsscattered_idsrh  r0   r^  r   gathered_update_idsmasked_operandmasked_updatesmasked_g_operandmasked_g_updatess                                    r+   _scatter_jvpr    s:    
A
B
BB%'7G$,!)Y	
%	)__$$iGL)H)Hnnwl!U+N	   G
 GL++G4444"9--)"9--)   nnwl!U+Dt  M MG ..),!U+Dt ! M MK K  7=!!),  jIjIi  '	'"" $q[BHRY,?,?,CCCryyHHyHws{38Hg#>#>	JJy999; ;* #(7=!X>>!:u-?)7dD D D- ((368 8 8, +	#]())**  aE&&&u'?'DEFFF	Qhcc}g1=+68 8 8
 :cf]CJ}4M4MNN%sz'':':< <.:cfZ2EFF%sz'':':< <.Z}cj6O6O P P )3:i+@+@B BZz3F G G )3:i+@+@B B */+='5DB B B' ,g7G.3/A+9F F F+ 
+	r-   c          	        |st          d          t          j        |          rJ t          j        |          r|j        j        }
n|j        }
t          |           t          j        u r`t          j        |          rt          j        |j                  nd }t          j        |          rt          j        |j                  nd }nd x}}t          j        |          r0t          | |t          j
        |
d| j                  ||d|	          }t          j        |          rt          |j        |j        |j                  }g }d}t!          t#          | j                            D ]L}||j        v r|                    d           !|                    |
|j        |                             |dz  }Mt'          | ||||	d          }|d |gS )	Nz>scatter transpose is only implemented whereunique_indices=Truer   r  Tr  r   r   r%  )r  r   r  r  r   r>  r   r?  r   r   r   ru   rA   ry   rz   r{   r   r   r  rv   rf  s                    r+   _scatter_transpose_ruler  P	  s    
 5
 4 5 5 5#G,,,,,G$$ "L&MMMM	!WW.0.DW.M.MWW\***SWI-/-CG-L-LVw|GL)))RVHHI	g&& :!Wch}aqw&O&O&O,=-?)-D: : :i
 
g&& &+%8.C)FH H Hl kcS\\""  !!666


Q






]+<+OPS+TU
V
V
V
(##7l$/d#$& & &h T8	$$r-   r   c                  | j         \  }
}| j        \  }|
j        j                            |
j                  j        }fdt          t          |                    D             }|                    g |j	        |R           }t          t          ||||||	          }t          j        | ||||t          j        |
          |t          j                  gt          j        |          g          \  }|S )Nc                $    g | ]}j         |z   S rK   r&  )r   r   aval_updatess     r+   r   z)_scatter_lower_opaque.<locals>.<listcomp>	  s!    OOOA,+a/OOOr-   )ry   r   r   rd   r`   ra   rb   r-  )r  r  ru   r.  r/  r   r   r   r0  ry   r   _scatter_lowerr   r2  r
   r3  )r  r   r   r~   r   r   rd   r`   ra   rb   r4  r5  r,  r6  trailing_window_dimsscatter_lowerr9  r  s                    @r+   _scatter_lower_opaquer  	  s!    (+|$&,M'&l!77EEK)OOOOs9~~9N9NOOO'001,? 1/1 1 1 2 2 <}).+$8 8 8- 
		='7G"6**L"<002#F++,	
. 
. 
.$#
 
*r-   c                  |F|rJ | j         d         j        }
t          j        t          t          j        d|
                    \  }}| j        \  }t          j	        |j        t          j
                  rt          | |||||||||	
  
        gS |	t          j        k    rAt          j        t           d          } ||                     d           ||||          \  }|}t$          j                            t+          |j                  t+          |j                  g g t+          |j                  t3          | j         d         j                  dz
  	          }t          j        |          }|g}|g}t%          j        |f||||t:          j                            |          t:          j                            |          
          }t          j        t          j        d|j                            }|j        j         !                    ||          }t;          j"        |          5  tG          j$                    }|j%        rtM          d          t          j'        | j(        ||t          j)                    ||j*        d         |j*        d         | j+                  \  }}t%          j,        t          j-        |                     d d d            n# 1 swxY w Y   |j.        S )Nr   rK   r  Fr<  r  r   r   ry   rz   input_batching_dimsscatter_indices_batching_dimsscattered_dims_to_operand_dimsr  ra   r`   z!Cannot lower effectful `scatter`.)dim_var_values)/r  ru   r   r   _scatter_reduction_computationr
   r  r  r   ri   rC  r  rM   rW   r   rD  r`  replacer   rx   rE  r   ry   rz   r{   r   r   rH  	ScatterOpr   rI  update_computationblocksr  InsertionPointr   new_name_stackrx  r  jaxpr_subcompmodule_contextTokenSet	argumentsr  return_flatten_ir_valuesr@  )r  r   r   r~   r   r   rd   ra   r`   rb   rH  r   clip_fnr   r  r   opscalar_typer;   
name_stack	out_nodesr   s                         r+   r  r  	  s    LO)M"%"6&(8](K(K#M #ML- m)(x~v77 ;!Wgw!+N-D	: : : ; ; 
###n3eLLLGt44gw!):< < <JW %-11e677 :;;$&%)%*L%M%M3<?011A5 2  - ))&I'I'
}i);<<[__^446 6 6" $T%5b(.%I%IJJ+ '..{KHH&	   3 3!022J E CDDD%L*dmoov'*F,<Q,?)+ + +LIq K&y112223 3 3 3 3 3 3 3 3 3 3 3 3 3 3 
s   .B KK!Kc                4    t          j        |           j        S r   )rj   finforu   r  s    r+   _real_dtyper  	  s    rx44r-   c                  | j         \  }
}}|
j        t          j        k    rt	          | ||||||	
  
        S |	t
          j        k    rAt          j        t          d          } || 
                    d           |||          \  | j        \  }|}t          j                            t          |j                  t          |j                  g g t          |j                  t'          | j         d         j                  dz
            t+          |j                  t          j        t/          j        |j                            fd} |t          j        |          t          j        |                    } |t          j        |          t          j        |                    }t          j        ||          gS )	Nr   Fr<  r  r  r   r  c                   | g} |g}t          j        f| |
t          j                                      t          j                                                }t          j        t          j        d	                    }|j	        d         j
                            ||          }t          j        |          5  t          j        |j         j        }t          j        |g           d d d            n# 1 swxY w Y   |j        S )Nr  rK   r   )r   r  r   rI  rE  r   rH  r
   r  regionsr  r  r  AddOpr  r   r  )operand_partupdates_partr   r  reducerr   r   ra   operand_type_part
real_dtyper  r`   s         r+   _scatterz(_scatter_add_lower_gpu.<locals>._scatter	  s.    >L >Lm	;??+=>>{~668 8 8G &t'7J'G'GHHKoa '..{KHHG		7	#	#  Iw()0c	k3%               >s   ?.C99C= C=)r  ru   rj   
complex128r  rM   rW   r   rD  r`  r  r  r   rx   rE  r   ry   rz   r{   r   r   r  rH  r
   r  realimagcomplex)r  r   r   r~   r   r   rd   ra   r`   rb   operand_aval_inr   updates_aval_inr  r   r   r  r  r  r  r  r  s     `    ``          @@@r+   _scatter_add_lower_gpur  	  s    ),%/1obm++#w'3(5,=-?)7dD D D D 
###n3eLLLGws{{T{22GWg.0 0 0HG m)(
%-11e677 :;;$&%)%*L%M%M3<?011A5 2  - 8>****
x~z224 4         & 
#(7##SXg%6%6	7	7$	#(7##SXg%6%6	7	7$
+dD
!
!	""r-   gpu)platformlist[ArrayLike]c           
        t          |          | j        k    r7d}t          |                    t          |          | j                            t          |t          t          f          sA|j        dk    r't          d                    |j                            t          |          }g }t          || j                  D ]\  }}t          j
        t          |          t          j                  r|                    |           Ht          |t          t          j        f          rUt#          j        |          rA|                    t'          j        |dk     r||z   n|t          |                               t#          j        |          }t          |t          t          j        f          rB|                    |dk     r%|t'          j        |t          |                    z   n|           5t'          j        |t          |                    }|                    t'          j        |dk     ||z   |                     |S )NzJLength of slice indices must match number of operand dimensions ({} vs {})r   z+Slice indices must be a 1D sequence, got {}r   )r   r   rZ   r   r   rV   r*   r   r   r   ri   rh   rj   rp   r  r   r3  r
   r  r   r  dimension_as_valuer  )r   r    r   r   r   r   d_arrs          r+   r2   r2   
  s   
 	7<''C
SZZM 2 2GMBB
C
CC	ME4=	1	1 (QD}2335 5 5''M&-// 3 3daB$677 mmA!c2:&'' D,@,C,C mmC,a!eeQUUF1IINNOOO""A!c2:&'' mm!a%%A0F1II>>>>QOOO$Qq		22E
MM#*QUAIq112222	-r-   r   )
r   r   r    r!   r"   r!   r#   r$   r%   r   )r   r.   r    r/   r0   r   r%   r   )r   r.   r;   r   r    r<   r%   r   )r   r   r    r   rd   rA   r0   r   r`   re   ra   re   rb   rO   r%   r   )r   r   r}   r   r~   r   rd   rx   ra   re   r`   re   rb   rO   r%   r   )r   r   r}   r   r   r   rd   rx   r   r   ra   re   r`   re   rb   rO   r%   r   )r   r   r   r   r   r!   r%   r   )r   r   )r   r.   r   r   r   r   r   r   r   r   r%   r   )r   T)
r   r.   r   r   r   r   r   re   r%   r   r  )
r   r.   r   r   r   r   r   r   r%   r   )
r   r.   r   r   r   r   r   re   r%   r   )
r   r.   r;   r   r   r   r   r   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   r\   r   	functoolsr   r  typingr   weakrefnumpyrj   r  jax._srcr   r	   r
   r   r   r   r   jax._src.interpretersr   r   r   r   r#  jax._src.laxr   jax._src.lax.utilsr   r   r   jax._src.lib.mlirr   jax._src.lib.mlir.dialectsr   jax._src.typingr   r   r   jax._src.utilr   r   r   
unsafe_mapr   
unsafe_zipru   rh   r,   r:   r?   rA   EnumrM   rv   rx   r   r   r   r   WeakKeyDictionaryr   rJ   r   r  r   r   r   r   r   r   r   r   r   r  r  r(   
deflinear2primitive_batchersdef_implr  r!  register_loweringr/  r<  rC  rJ  rY  rf  rm  rz  r  r7   primitive_jvpsprimitive_transposescustom_staging_rulescustom_typecheckspadding_rulesr  r  r  r  r  r  r>   r  r  r  r  r  r  r  r  r  r  r   r\  r  r(  rs   defjvpr:  r1  rO  rY  r`  rd  ri  rl  rr  r   r   rv  r  r   r   r  r  r   r  r  r  r  r2   rK   r-   r+   <module>r     s    # " " " " " " . . . . . . . .                     



                               % % % % % %       $ $ $ $ $ $ * * * * * * & & & & & & 4 4 4 4 4 4               
 !           * * * * * * 3 3 3 3 3 3 3 3 3 3 , , , , , , , ,CZCZ	D	1	1	1
 +/5K 5K 5K 5K 5Kp7? 7? 7? 7?t.F .F .F .Fb# # # # #Z # # #6$5 $5 $5 $5 $5	 $5 $5 $5V #(&+26D D D D D DN. . . . .j . . .: #5)-	-- -- -- -- -- --d #5)-	-- -- -- -- -- --d #5)-	-- -- -- -- -- --d #5)-	-- -- -- -- -- --` 3L'2K2M2M  M M M M "5)-;- ;- ;- ;- ;- ;-| "0 
 #5)-	.- .- .- .- .- .-`0 0 0 0* /0H? H? H? H? H?V GH"&9( 9( 9( 9( 9(| 787< 7< 7< 7< 7<x :>1( 1( 1( 1( 1(h;> ;> ;> ;>|>C >C >C >CB%/ %/ %/N  $  ( 
.g
F
F g, - - -'; G $ 	; ; ;d d d  w - - -7 7 70  ^ ^ ^	) 	) 	)  ? ? ?,	> 	> 	>' ' '5 5 5 %$8/$$Q'') ) ) &8 / "+H  (/K O ,+F  (*G  '$?  !M M M  (< = = =  $    = = ="+ + +, ,+$&F   -F ( )(  . / ( 	 2 3B B B
  -/J K K KM M M
 	#"L L L

9 
9 
9M M McL cL cLL  0B B BD    &d, d, d,L/ / /
M 
M 
M *H$$Q'') ) ) 
	($d + + +$:  !(= H %-     (2A 2A 2Ah  x / / /M M Mf f fR     2  .% % %B%% %% %%P0L 0L 0Ld #",m$$Q'') ) ) $4 -  )D  &	'
 -00 	 M * #",m$$Q'') ) )   
	-
D
D

      *E  &	'
 -00 	 M *c c cJ #",m$$Q'') ) ) 
'
 -00 	 M *#*7+@-#P#P -  "",m$$Q'') ) ) 
'
 -00 	 M *#*7+@-#P#P -  c c cJ(% (% (%T ,i$$Q'') ) )	  , ) %< 	 "	'
 ),, 	 I &  ,5 5 5n  y. 1 1 1  }n 5 5 5  }n 5 5 5  }n 5 5 5  }n 5 5 5 5 4 44# 4# 4#l  }&<u M M M M     r-   