
    AVha                    $   d 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 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# 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Z0 e/d#      jc                  e2d#        e/d$d$d%g&      e+jf                  dd'              Z4 e/d(      e+jf                  dd)              Z5 e/d*      e+jf                  dd+              Z6 e/d,g&      e+jf                   e*jn                  dd-d.      dd/                     Z8 e/d,g &      e+jf                  dd0              Z9 e*jt                  d1d2      dd3       Z;ejx                  j                   d4z   e;j                   z   e;_          e*jt                  d5d6       e/d7g&      e+jf                  ejz                  dfd8                     Z>ejx                  j                   e>_          e/d9      e+jf                  d:               Z? e/d;      e+jf                  d<               Z@ e/d=g &      e+jf                  dd>              ZA e/d=g&      e+jf                  dd?              ZBd	dAZC e/dB      e+jf                  ejz                  dfdC              ZD e/dDg &      e+jf                  ddE              ZE e/dDg&      e+jf                  ddF              ZFdd@ejz                  fdGZG e/dH      e+jf                  ddI              ZHd
dJZI e/dK      e+jf                  ddL              ZJ e/dM      e+jf                  	 	 	 	 	 	 	 	 ddN              ZK e/dO      e+jf                  ddP              ZLdQ ZMdR ZNdS ZO ePg eQeReSeTeUeVeWej                  ej                  ej                  ej                  ej                  ej                  ej                  ej                  ej                  ej                  ejz                  ej                  ej                  ej                  ej                  ej                  ej                  ej                  ej                  ej                  ej                  ej                  ej                  ej                        Zoeoj                  ej                         dT ZrddVZs ej                  euevfesdW        e/dX      e+jf                  ddY              Zw e/dZg&      e+jf                  dd[              Zx e/dZg &      e+jf                  dd\              Zy e/d]d]d^g&       e*j                  d^      dd_              Z{ e/d`      e+jf                  ejz                  dfda              Z|ej                  j                   e|_          e/db      e+jf                  ejz                  dfdc              Z}ej                  j                   e}_          e/dd      e+jf                  dde              Z~ e/dfg &      e+jf                  ddg              Z e/dfg&      e+jf                  ddh              Z e/dig dj&      e+jf                   e*j                  dkdl      ddm                     Z e/dndndog&      e+jf                   e*j                  do      	 	 	 	 	 	 ddq                     Z e/drdrdsg&      e+jf                   e*j                  ds      	 	 	 	 ddu                     Z e/dvdvdtg&      e+jf                   e*j                  dt      	 ddw                     Z e/dxdxdyg&      e+jf                   e*j                  dy      	 	 	 ddz                     Zdd{Zd| Z e/d}      e+jf                  eej                  ddfd~                     Z e/dg&      e+j                  e+jf                  dd                     Z e/dg &      e+j                  e+jf                  	 	 	 dd                     Z	 ddZedd       Z e/dg&      e+j                  e+jf                  dd                     Z e/dg &      e+j                  e+jf                  	 	 	 dd                     ZddZ e/d      e+jf                  ej                  ddfd              Z e/dg&      dd       Z e/dg&      dd       Z e/ddg&       e*j                  d      dd              Z e/dg &      e+jf                  dd              Z e/dg&      e+jf                  dd              Zd Z e/d      e+jf                  d               ZdpZdZd Zdej8                  fdZ e/d      e+jf                  dd              Z ej>                  d      dej8                  fd       Z ej>                  d      dej8                  fd       Z ej>                  d      dej8                  fd       Z ej>                  d      dej8                  fd       Z ej>                  d      dej8                  fd       Z e/d      	 	 dd       Z e/ddg&      e+jf                   e*j                  d      	 	 	 dd                     ZejL                  j                   e_          e/ddg &      e+jf                  dd              ZejP                  j                   e_          e/ddg&      e+jf                   e*j                  d      dd                     ZejR                  j                   e_          e/dg &      e+jf                  dd              ZejR                  j                   e_          e/ddg&      e+jf                   e*j                  d      dd                     ZejV                  j                   e_          e/dg &      e+jf                  dd              ZejV                  j                   e_          e/dg&      e+jf                  dd              ZejZ                  j                   e_          e/dg &      e+jf                  dd              Z e/d      e+jf                  	 	 	 	 	 dd              Zd Z e/d      e+jf                  dej                  dfd              Z e/dg&      e+jf                   e*jn                  dd-d      dd                     Z e/dg &      e+jf                  dd              Z e/dg&      e+jf                  dd              Z e/ddg&      e+jf                  ddĄ              Z e/dg&       e*jn                  dddǫ       e*jn                  dddɫ      	 	 	 	 	 ddʄ                     Z e/dg &      e+jf                  	 	 	 dd˄              Z e/dg&      e+jf                   e*jn                  dddΫ      	 	 	 	 d dτ                     Z e/dg &      e+jf                  	 	 	 	 d!dЄ              Zej                   e_          e/dg&      e+jf                   e*jt                  ddӫ      ddԄ                     ZddՄZ e/ddg&      e+jf                   ezd׫      d"d؄                     Z e/dg &      e+jf                  	 d#dل              Zej                   e_         	 d$dڄZ e*j                  d۫       e/dddg&      e+jf                  dd݄                     Z e/dg&      e+jf                   e*jt                  dd߫      	 	 	 	 	 	 d%d                     Zde_          e/dddg&      e+jf                   e*j                  d      	 	 	 	 	 	 d&d                     Z e/dddg&      e+jf                   e*j                  d      ddddUej                  fd                     Zej                  j                   e_          e/d      e+jf                   e*jt                  dd      	 	 	 	 	 	 	 d'd                     Z e/d      e+jf                  	 	 	 	 	 	 	 d'd              Z e/d      e+jf                  dejz                  dfd              Zej                  j                   e_          e/d      e+jf                  dd              Z e/ddg&      e+jf                   e*jn                  ddd      	 	 	 	 	 	 d(d                     Zej                  j                   e_          e/d      e+jf                  d)d              Zejz                  fdZɐd*dZʐddZd Zd Z e/d      e+jf                  dd               Z e/d       e*jt                  dd      dd              Z e/d      e+jf                  dd              Zej                  j                   e_          e+j                  ej                         y(+  z!Support for manipulating tensors.    N)flags)api)context)record)common_shapes)composite_tensor)constant_op)constant_tensor_conversion)dtypes)errors)indexed_slices)ops)sparse_tensor)tensor)tensor_conversion_registry)tensor_shape)tensor_util)constant)array_ops_stack)gen_array_ops)gen_math_ops)
shape_util)tensor_getitem_override)*)
reverse_v2)core)_pywrap_utils)deprecation)dispatch)nest)tf_decorator)	tf_exportnewaxisreshapezmanip.reshape)v1c                 `    t        j                  | ||      }t        j                  ||       |S )a  Reshapes a tensor.

  Given `tensor`, this operation returns a new `tf.Tensor` that has the same
  values as `tensor` in the same order, except with a new shape given by
  `shape`.

  >>> t1 = [[1, 2, 3],
  ...       [4, 5, 6]]
  >>> print(tf.shape(t1).numpy())
  [2 3]
  >>> t2 = tf.reshape(t1, [6])
  >>> t2
  <tf.Tensor: shape=(6,), dtype=int32,
    numpy=array([1, 2, 3, 4, 5, 6], dtype=int32)>
  >>> tf.reshape(t2, [3, 2])
  <tf.Tensor: shape=(3, 2), dtype=int32, numpy=
    array([[1, 2],
           [3, 4],
           [5, 6]], dtype=int32)>

  The `tf.reshape` does not change the order of or the total number of elements
  in the tensor, and so it can reuse the underlying data buffer. This makes it
  a fast operation independent of how big of a tensor it is operating on.

  >>> tf.reshape([1, 2, 3], [2, 2])
  Traceback (most recent call last):
  ...
  InvalidArgumentError: Input to reshape is a tensor with 3 values, but the
  requested shape has 4

  To instead reorder the data to rearrange the dimensions of a tensor, see
  `tf.transpose`.

  >>> t = [[1, 2, 3],
  ...      [4, 5, 6]]
  >>> tf.reshape(t, [3, 2]).numpy()
  array([[1, 2],
         [3, 4],
         [5, 6]], dtype=int32)
  >>> tf.transpose(t, perm=[1, 0]).numpy()
  array([[1, 4],
         [2, 5],
         [3, 6]], dtype=int32)

  If one component of `shape` is the special value -1, the size of that
  dimension is computed so that the total size remains constant.  In particular,
  a `shape` of `[-1]` flattens into 1-D.  At most one component of `shape` can
  be -1.

  >>> t = [[1, 2, 3],
  ...      [4, 5, 6]]
  >>> tf.reshape(t, [-1])
  <tf.Tensor: shape=(6,), dtype=int32,
    numpy=array([1, 2, 3, 4, 5, 6], dtype=int32)>
  >>> tf.reshape(t, [3, -1])
  <tf.Tensor: shape=(3, 2), dtype=int32, numpy=
    array([[1, 2],
           [3, 4],
           [5, 6]], dtype=int32)>
  >>> tf.reshape(t, [-1, 2])
  <tf.Tensor: shape=(3, 2), dtype=int32, numpy=
    array([[1, 2],
           [3, 4],
           [5, 6]], dtype=int32)>

  `tf.reshape(t, [])` reshapes a tensor `t` with one element to a scalar.

  >>> tf.reshape([7], []).numpy().item()
  7

  More examples:

  >>> t = [1, 2, 3, 4, 5, 6, 7, 8, 9]
  >>> print(tf.shape(t).numpy())
  [9]
  >>> tf.reshape(t, [3, 3])
  <tf.Tensor: shape=(3, 3), dtype=int32, numpy=
    array([[1, 2, 3],
           [4, 5, 6],
           [7, 8, 9]], dtype=int32)>

  >>> t = [[[1, 1], [2, 2]],
  ...      [[3, 3], [4, 4]]]
  >>> print(tf.shape(t).numpy())
  [2 2 2]
  >>> tf.reshape(t, [2, 4])
  <tf.Tensor: shape=(2, 4), dtype=int32, numpy=
    array([[1, 1, 2, 2],
           [3, 3, 4, 4]], dtype=int32)>

  >>> t = [[[1, 1, 1],
  ...       [2, 2, 2]],
  ...      [[3, 3, 3],
  ...       [4, 4, 4]],
  ...      [[5, 5, 5],
  ...       [6, 6, 6]]]
  >>> print(tf.shape(t).numpy())
  [3 2 3]
  >>> # Pass '[-1]' to flatten 't'.
  >>> tf.reshape(t, [-1])
  <tf.Tensor: shape=(18,), dtype=int32,
    numpy=array([1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 5, 6, 6, 6],
    dtype=int32)>
  >>> # -- Using -1 to infer the shape --
  >>> # Here -1 is inferred to be 9:
  >>> tf.reshape(t, [2, -1])
  <tf.Tensor: shape=(2, 9), dtype=int32, numpy=
    array([[1, 1, 1, 2, 2, 2, 3, 3, 3],
           [4, 4, 4, 5, 5, 5, 6, 6, 6]], dtype=int32)>
  >>> # -1 is inferred to be 2:
  >>> tf.reshape(t, [-1, 9])
  <tf.Tensor: shape=(2, 9), dtype=int32, numpy=
    array([[1, 1, 1, 2, 2, 2, 3, 3, 3],
           [4, 4, 4, 5, 5, 5, 6, 6, 6]], dtype=int32)>
  >>> # -1 is inferred to be 3:
  >>> tf.reshape(t, [ 2, -1, 3])
  <tf.Tensor: shape=(2, 3, 3), dtype=int32, numpy=
    array([[[1, 1, 1],
            [2, 2, 2],
            [3, 3, 3]],
           [[4, 4, 4],
            [5, 5, 5],
            [6, 6, 6]]], dtype=int32)>

  Args:
    tensor: A `Tensor`.
    shape: A `Tensor`. Must be one of the following types: `int32`, `int64`.
      Defines the shape of the output tensor.
    name: Optional string. A name for the operation.

  Returns:
    A `Tensor`. Has the same type as `tensor`.
  )r   r$   r   maybe_set_static_shape)r   shapenameresults       O/home/dcms/DCMS/lib/python3.12/site-packages/tensorflow/python/ops/array_ops.pyr$   r$   ?   s.    P   5&##FE2	-    fillc                     t        j                  t        j                  || ||      }t	        j
                  ||        |S )a  Creates a tensor filled with a scalar value.

  See also `tf.ones`, `tf.zeros`, `tf.one_hot`, `tf.eye`.

  This operation creates a tensor of shape `dims` and fills it with `value`.

  For example:

  >>> tf.fill([2, 3], 9)
  <tf.Tensor: shape=(2, 3), dtype=int32, numpy=
  array([[9, 9, 9],
         [9, 9, 9]], dtype=int32)>

  `tf.fill` evaluates at graph runtime and supports dynamic shapes based on
  other runtime `tf.Tensors`, unlike `tf.constant(value, shape=dims)`, which
  embeds the value as a `Const` node.

  Args:
    dims: A 1-D sequence of non-negative numbers. Represents the shape of the
      output `tf.Tensor`. Entries should be of type: `int32`, `int64`.
    value: A value to fill the returned `tf.Tensor`.
    name: Optional string. The name of the output `tf.Tensor`.
    layout: Optional, `tf.experimental.dtensor.Layout`. If provided, the result
      is a [DTensor](https://www.tensorflow.org/guide/dtensor_overview) with the
      provided layout.

  Returns:
    A `tf.Tensor` with shape `dims` and the same dtype as `value`.

  Raises:
    InvalidArgumentError: `dims` contains negative entries.
    NotFoundError: `dims` contains non-integer entries.

  @compatibility(numpy)
  Similar to `np.full`. In `numpy`, more parameters are supported. Passing a
  number argument as the shape (`np.full(5, value)`) is valid in `numpy` for
  specifying a 1-D shaped result, while TensorFlow does not support this syntax.
  @end_compatibility
  )layoutdimsvaluer)   )d_apicall_with_layoutr   r-   r   r'   )r0   r1   r)   r/   r*   s        r+   r-   r-      s=    T !!d%d& ##FD1	-r,   identityc                 n   t        | t        j                        r1t        j                  |       st        j                  t        | d      S t        j                         r!t        | d      st        j                  |       } t        j                  | |      }t        | d      r| j                  |_        |S )a  Return a Tensor with the same shape and contents as input.

  The return value is not the same Tensor as the original, but contains the same
  values.  This operation is fast when used on the same device.

  For example:

  >>> a = tf.constant([0.78])
  >>> a_identity = tf.identity(a)
  >>> a.numpy()
  array([0.78], dtype=float32)
  >>> a_identity.numpy()
  array([0.78], dtype=float32)

  Calling `tf.identity` on a variable will make a Tensor that represents the
  value of that variable at the time it is called. This is equivalent to calling
  `<variable>.read_value()`.

  >>> a = tf.Variable(5)
  >>> a_identity = tf.identity(a)
  >>> a.assign_add(1)
  <tf.Variable ... shape=() dtype=int32, numpy=6>
  >>> print(a.numpy())
  6
  >>> print(a_identity.numpy())
  5

  This function can also be used to explicitly transfer tensors between devices.
  For example, to transfer a tensor in GPU memory back to host memory, one can
  use:

  >>> with tf.device("/gpu:0"):
  ...   x_on_gpu = tf.constant(1)
  >>> with tf.device("/cpu:0"):
  ...   x_on_cpu = tf.identity(x_on_gpu)
  >>> x_on_cpu.device
  '/job:localhost/replica:0/task:0/device:CPU:0'

  Args:
    input: A `Tensor`, a `Variable`, a `CompositeTensor` or anything that can be
    converted to a tensor using `tf.convert_to_tensor`.
    name: A name for the operation (optional).

  Returns:
    A `Tensor` or CompositeTensor. Has the same type and contents as `input`.
  Texpand_compositesgraphr)   _handle_data)
isinstancer   CompositeTensorr   IsResourceVariabler    map_structurer4   r   executing_eagerlyhasattrr   convert_to_tensorr   r:   )inputr)   rets      r+   r4   r4      s    d (889

*
*5
1hFF )@ !!%(Eu40#UN#))C	*r,   expand_dimszUse the `axis` argument insteaddimc                 f    t        j                  d|d|      }|t        d      t        | ||      S )a  Returns a tensor with a length 1 axis inserted at index `axis`.

  Given a tensor `input`, this operation inserts a dimension of length 1 at the
  dimension index `axis` of the `input`'s shape. The dimension index follows
  Python
  indexing rules: It's zero-based, a negative index that is counted backward
  from the end.

  This operation is useful to:

  * Add an outer "batch" dimension to a single element.
  * Align axes for broadcasting.
  * To add an inner vector length axis to a tensor of scalars.

  For example:

  If you have a single image of shape `[height, width, channels]`:

  >>> image = tf.zeros([10,10,3])

  You can add an outer `batch` axis by passing `axis=0`:

  >>> tf.expand_dims(image, axis=0).shape.as_list()
  [1, 10, 10, 3]

  The new axis location matches Python `list.insert(axis, 1)`:

  >>> tf.expand_dims(image, axis=1).shape.as_list()
  [10, 1, 10, 3]

  Following standard Python indexing rules, a negative `axis` counts from the
  end so `axis=-1` adds an innermost dimension:

  >>> tf.expand_dims(image, -1).shape.as_list()
  [10, 10, 3, 1]

  This operation requires that `axis` is a valid index for `input.shape`,
  following Python indexing rules:

  ```
  -1-tf.rank(input) <= axis <= tf.rank(input)
  ```

  This operation is related to:

  * `tf.squeeze`, which removes dimensions of size 1.
  * `tf.reshape`, which provides a more flexible reshaping capability.
  * `tf.sparse.expand_dims`, which provides this functionality for
    `tf.SparseTensor`

  Args:
    input: A `Tensor`.
    axis: 0-D (scalar). Specifies the dimension index at which to expand the
      shape of `input`. Must be in the range `[-rank(input) - 1, rank(input)]`.
    name: The name of the output is `Tensor` (optional).
    dim: 0-D (scalar). Equivalent to `axis`, to be deprecated.

  Returns:
    A `Tensor` with the same data as `input`, but its shape has an additional
    dimension of size 1 added.

  Raises:
    ValueError: if either both or neither of `dim` and `axis` are specified.
  axisrE   z1Must specify an axis argument to tf.expand_dims())r   deprecated_argument_lookup
ValueErrorexpand_dims_v2)rB   rG   r)   rE   s       r+   rD   rD   >  s;    H 
	/	/eS	I$	\
H
II	tT	**r,   c                 0    t        j                  | ||      S )a  Returns a tensor with a length 1 axis inserted at index `axis`.

  Given a tensor `input`, this operation inserts a dimension of length 1 at the
  dimension index `axis` of `input`'s shape. The dimension index follows Python
  indexing rules: It's zero-based, and a negative index is counted backward
  from the end.

  This operation is useful to:

  * Add an outer "batch" dimension to a single element.
  * Align axes for broadcasting.
  * To add an inner vector length axis to a tensor of scalars.

  For example:

  If you have a single image of shape `[height, width, channels]`:

  >>> image = tf.zeros([10,10,3])

  You can add an outer `batch` axis by passing `axis=0`:

  >>> tf.expand_dims(image, axis=0).shape.as_list()
  [1, 10, 10, 3]

  The new axis location matches Python `list.insert(axis, 1)`:

  >>> tf.expand_dims(image, axis=1).shape.as_list()
  [10, 1, 10, 3]

  Following standard Python indexing rules, a negative `axis` counts from the
  end so `axis=-1` adds an inner most dimension:

  >>> tf.expand_dims(image, -1).shape.as_list()
  [10, 10, 3, 1]

  This operation requires that `axis` is a valid index for `input.shape`,
  following Python indexing rules:

  ```
  -1-tf.rank(input) <= axis <= tf.rank(input)
  ```

  This operation is related to:

  * `tf.squeeze`, which removes dimensions of size 1.
  * `tf.reshape`, which provides more flexible reshaping capability.
  * `tf.sparse.expand_dims`, which provides this functionality for
    `tf.SparseTensor`

  Args:
    input: A `Tensor`.
    axis: Integer specifying the dimension index at which to expand the
      shape of `input`. Given an input of D dimensions, `axis` must be in range
      `[-(D+1), D]` (inclusive).
    name: Optional string. The name of the output `Tensor`.

  Returns:
    A tensor with the same data as `input`, with an additional dimension
    inserted at the index specified by `axis`.

  Raises:
    TypeError: If `axis` is not specified.
    InvalidArgumentError: If `axis` is out of range `[-(D+1), D]`.
  )r   rD   rB   rG   r)   s      r+   rJ   rJ     s    F 
	"	"5$	55r,   z
2016-11-30zTThis op will be removed after the deprecation date. Please switch to tf.setdiff1d().c                 2    t        j                  | |||      S Nr   	list_diff)xyout_idxr)   s       r+   listdiffrT     s     
	 	 Aw	55r,   
z
2018-11-30zZThis op will be removed after the deprecation date. Please switch to tf.sets.difference().	setdiff1dc                 2    t        j                  | |||      S )a}  Computes the difference between two lists of numbers or strings.

  Given a list x and a list y, this operation returns a list out that
  represents all values that are in x but not in y. The returned list
  out is sorted in the same order that the numbers appear in x
  (duplicates are preserved). This operation also returns a list idx
  that represents the position of each out element in x.

  In other words:

  ```python
  out[i] = x[idx[i]] for i in [0, 1, ..., len(out) - 1]
  ```

  Example usage:

  >>> x = [1, 2, 3, 4, 5, 6]
  >>> y = [1, 3, 5]
  >>> setdiff1d(x,y)
  ListDiff(out=<tf.Tensor: id=2, shape=(3,), dtype=int32,
  numpy=array([2, 4, 6], dtype=int32)>, idx=<tf.Tensor: id=3,
  shape=(3,), dtype=int32, numpy=array([1, 3, 5], dtype=int32)>)

  Args:
    x: A Tensor. 1-D. Values to keep.
    y: A Tensor. Must have the same type as x. 1-D. Values to remove.
    out_idx: An optional tf.DType from: tf.int32, tf.int64. Defaults to
      tf.int32.
    name: A name for the operation (optional).

  Returns:
    A tuple of Tensor objects (out, idx).
    out: A Tensor. Has the same type as x.
    idx: A Tensor of type out_idx.
  rO   )rQ   rR   index_dtyper)   s       r+   rV   rV     s    R 
	 	 A{D	99r,   broadcast_dynamic_shapec                 .    t        j                  | |      S )a  Computes the shape of a broadcast given symbolic shapes.

  When `shape_x` and `shape_y` are Tensors representing shapes (i.e. the result
  of calling tf.shape on another Tensor) this computes a Tensor which is the
  shape of the result of a broadcasting op applied in tensors of shapes
  `shape_x` and `shape_y`.

  This is useful when validating the result of a broadcasting operation when the
  tensors do not have statically known shapes.

  Example:

  >>> shape_x = (1, 2, 3)
  >>> shape_y = (5, 1, 3)
  >>> tf.broadcast_dynamic_shape(shape_x, shape_y)
  <tf.Tensor: shape=(3,), dtype=int32, numpy=array([5, 2, 3], ...>

  Args:
    shape_x: A rank 1 integer `Tensor`, representing the shape of x.
    shape_y: A rank 1 integer `Tensor`, representing the shape of y.

  Returns:
    A rank 1 integer `Tensor` representing the broadcasted shape.

  Raises:
    InvalidArgumentError: If the two shapes are incompatible for
    broadcasting.
  )r   broadcast_argsshape_xshape_ys     r+   rY   rY     s    > 
	%	%gw	77r,   broadcast_static_shapec                 .    t        j                  | |      S )a  Computes the shape of a broadcast given known shapes.

  When `shape_x` and `shape_y` are fully known `TensorShape`s this computes a
  `TensorShape` which is the shape of the result of a broadcasting op applied in
  tensors of shapes `shape_x` and `shape_y`.

  For example, if shape_x is `TensorShape([1, 2, 3])` and shape_y is
  `TensorShape([5, 1, 3])`, the result is a TensorShape whose value is
  `TensorShape([5, 2, 3])`.

  This is useful when validating the result of a broadcasting operation when the
  tensors have statically known shapes.

  Example:

  >>> shape_x = tf.TensorShape([1, 2, 3])
  >>> shape_y = tf.TensorShape([5, 1 ,3])
  >>> tf.broadcast_static_shape(shape_x, shape_y)
  TensorShape([5, 2, 3])

  Args:
    shape_x: A `TensorShape`
    shape_y: A `TensorShape`

  Returns:
    A `TensorShape` representing the broadcasted shape.

  Raises:
    ValueError: If the two shapes can not be broadcasted.
  )r   broadcast_shaper\   s     r+   r_   r_   1  s    B 
	&	&w	88r,   r(   c                     |Mt        j                         j                  j                         rt        j
                  }nt        j                  }t        | ||      S )a  Returns a tensor containing the shape of the input tensor.

  See also `tf.size`, `tf.rank`.

  `tf.shape` returns a 1-D integer tensor representing the shape of `input`.
  For a scalar input, the tensor returned has a shape of (0,) and its value is
  the empty vector (i.e. []).

  For example:

  >>> tf.shape(1.)
  <tf.Tensor: shape=(0,), dtype=int32, numpy=array([], dtype=int32)>

  >>> t = tf.constant([[[1, 1, 1], [2, 2, 2]], [[3, 3, 3], [4, 4, 4]]])
  >>> tf.shape(t)
  <tf.Tensor: shape=(3,), dtype=int32, numpy=array([2, 2, 3], dtype=int32)>

  Note: When using symbolic tensors, such as when using the Keras API,
  tf.shape() will return the shape of the symbolic tensor.

  >>> a = tf.keras.layers.Input((None, 10))
  >>> tf.shape(a)
  <... shape=(3,) dtype=int32...>

  In these cases, using `tf.Tensor.shape` will return more informative results.

  >>> a.shape
  TensorShape([None, None, 10])

  (The first `None` represents the as yet unknown batch size.)

  `tf.shape` and `Tensor.shape` should be identical in eager mode.  Within
  `tf.function` or within a `compat.v1` context, not all dimensions may be
  known until execution time. Hence, when defining custom layers and models
  for graph mode, prefer the dynamic `tf.shape(x)` over the static `x.shape`.

  Args:
    input: A `Tensor` or `SparseTensor`.
    out_type: (Optional) The specified output type of the operation (`int32` or
      `int64`). Defaults to `tf.int32`. (Note: there is an experimental
      flag, `tf_shape_default_int64` that changes the default to `tf.int64`.
      This is an unsupported, experimental setting that causes known breakages.)
    name: A name for the operation (optional).

  Returns:
    A `Tensor` of type `out_type`.
  )r   configtf_shape_default_int64r1   r   int64int32r(   rB   out_typer)   s      r+   shape_v2ri   U  sD    j ||~,,224hh	udH	%%r,   c                     |Mt        j                         j                  j                         rt        j
                  }nt        j                  }t        | |d|      S )a  Returns the shape of a tensor.

  This operation returns a 1-D integer tensor representing the shape of `input`.

  For example:

  ```python
  t = tf.constant([[[1, 1, 1], [2, 2, 2]], [[3, 3, 3], [4, 4, 4]]])
  tf.shape(t)  # [2, 2, 3]
  ```

  Args:
    input: A `Tensor` or `SparseTensor`.
    name: A name for the operation (optional).
    out_type: (Optional) The specified output type of the operation (`int32`
    or `int64`). Defaults to `tf.int32`.

  Returns:
    A `Tensor` of type `out_type`.
  Toptimizerh   )r   rc   rd   r1   r   re   rf   shape_internalrB   r)   rh   s      r+   r(   r(     sE    0 ||~,,224hh	tdX	FFr,   Tc                    t        j                  |d| g      5 }t        | t        j                  t        j
                  f      r;|st        j                  }t        j                  | j                  |      cddd       S t        j                         s|t        j                  |       } | j                         }|rU|j                         rE|st!        j"                  |      cddd       S t%        |j'                         ||      cddd       S |st        j                  }t)        j*                  | ||      cddd       S # 1 sw Y   yxY w)aE  Returns the shape of a tensor.

  If `out_type` is not specified and the shape is fully known, then we look at
  the dimension values to determine whether to return an int32 or int64 tensor.
  If the shape is not fully known, we default to int32.

  Args:
    input: A `Tensor` or `SparseTensor`.
    name: A name for the operation (optional).
    optimize: if true, encode the shape as a constant when possible.
    out_type: (Optional) The specified output type of the operation (`int32` or
      `int64`). Defaults to tf.int32.

  Returns:
    A `Tensor` of type `out_type`.

  ShapeNr9   r)   rh   )r   
name_scoper;   r   SparseTensorSparseTensorValuer   rf   r   castdense_shaper   r?   rA   	get_shapeis_fully_definedr	   (_tensor_shape_tensor_conversion_functionr   as_listr   r(   )rB   r)   rl   rh   input_shapes        r+   rm   rm     s   & ~~dGeW- F**M,K,KLN<<u00(;F F &&(%%e,oo'446 GGF F  +--/E!F F" <<  THE'F F Fs   AE?A!E*E)EEshape_nc                 2    t        j                  | ||      S )a=  Returns shape of a list of tensors.

  Given a list of tensors, `tf.shape_n` is much faster than applying `tf.shape`
  to each tensor individually.
  >>> a = tf.ones([1, 2])
  >>> b = tf.ones([2, 3])
  >>> c = tf.ones([3, 4])
  >>> tf.shape_n([a, b, c])
  [<tf.Tensor: shape=(2,), dtype=int32, numpy=array([1, 2], dtype=int32)>,
  <tf.Tensor: shape=(2,), dtype=int32, numpy=array([2, 3], dtype=int32)>,
  <tf.Tensor: shape=(2,), dtype=int32, numpy=array([3, 4], dtype=int32)>]

  Args:
    input: A list of at least 1 `Tensor` object with the same dtype.
    out_type: The specified output type of the operation (`int32` or `int64`).
      Defaults to `tf.int32`(optional).
    name: A name for the operation (optional).

  Returns:
    A list of `Tensor` specifying the shape of each input tensor with type of
    `out_type`.
  )rh   r)   )r   r|   rg   s      r+   r|   r|     s    6 
		uxd	CCr,   sizec                     |Mt        j                         j                  j                         rt        j
                  }nt        j                  }t        | ||      S )am  Returns the size of a tensor.

  See also `tf.shape`.

  Returns a 0-D `Tensor` representing the number of elements in `input`
  of type `out_type`. Defaults to tf.int32.

  For example:

  >>> t = tf.constant([[[1, 1, 1], [2, 2, 2]], [[3, 3, 3], [4, 4, 4]]])
  >>> tf.size(t)
  <tf.Tensor: shape=(), dtype=int32, numpy=12>

  Args:
    input: A `Tensor` or `SparseTensor`.
    out_type: (Optional) The specified non-quantized numeric output type of the
      operation. Defaults to `tf.int32`. (Note: there is an experimental
      flag, `tf_shape_default_int64` that changes the default to `tf.int64`.
      This is an unsupported, experimental setting that causes known breakages.)
    name: A name for the operation (optional).

  Returns:
    A `Tensor` of type `out_type`. Defaults to `tf.int32`.

  @compatibility(numpy)
  Equivalent to np.size()
  @end_compatibility
  )r   rc   rd   r1   r   re   rf   r~   rg   s      r+   size_v2r     sD    @ ||~,,224hh	eT8	$$r,   c                     |Mt        j                         j                  j                         rt        j
                  }nt        j                  }t        | |d|      S )a6  Returns the size of a tensor.

  Returns a 0-D `Tensor` representing the number of elements in `input`
  of type `out_type`. Defaults to tf.int32.

  For example:

  ```python
  t = tf.constant([[[1, 1, 1], [2, 2, 2]], [[3, 3, 3], [4, 4, 4]]])
  tf.size(t)  # 12
  ```

  Args:
    input: A `Tensor` or `SparseTensor`.
    name: A name for the operation (optional).
    out_type: (Optional) The specified non-quantized numeric output type of the
      operation. Defaults to `tf.int32`. (Note: there is an experimental
      flag, `tf_shape_default_int64` that changes the default to `tf.int64`.
      This is an unsupported, experimental setting that causes known breakages.)

  Returns:
    A `Tensor` of type `out_type`. Defaults to `tf.int32`.

  @compatibility(numpy)
  Equivalent to np.size()
  @end_compatibility
  Trk   )r   rc   rd   r1   r   re   rf   size_internalrn   s      r+   r~   r~   !  sE    > ||~,,224hh	udTH	EEr,   c                    t        j                         rt        | d      st        | t        j
                  t        j                  f      s]t        j                  |       } |j                  }t        j                  | j                         |      }t        j                  ||      S t        j                  |d| g      5 }t        | t        j
                  t        j                  f      r?t        j                  t        j                  | j                   |      d|      cddd       S t        j                  |       } | j#                         }|rt|j%                         r%t'        |j)                         ||      cddd       S |j*                  r3t-        d |j*                  D              rt'        d||      cddd       S t/        j0                  | ||      cddd       S # 1 sw Y   yxY w)	a  Returns the size of a tensor.

  Args:
    input: A `Tensor` or `SparseTensor`.
    name: A name for the operation (optional).
    optimize: if true, encode the size as a constant when possible.
    out_type: (Optional) The specified non-quantized numeric output type of the
      operation. Defaults to `tf.int32`.

  Returns:
    A `Tensor` of type `out_type`. Defaults to `tf.int32`.
  r8   dtypeSizer   r9   Nc              3   &   K   | ]	  }|d k(    yw)r   N .0rE   s     r+   	<genexpr>z size_internal.<locals>.<genexpr>i  s     #IC1H#Is   rq   )r   r?   r@   r;   r   rs   rt   r   rA   as_numpy_dtypenpprod_shape_tuplerr   r   ru   rv   rw   rx   r   num_elementsr0   anyr   r~   )rB   r)   rl   rh   np_out_typer   r{   s          r+   r   r   H  s    !'%*A

%%}'F'F
GI !!%(E))K775--/{CL  X>>
~~dFUG, E**M,K,KLN


E--x
8!$HE E ##E*eOO%k	'')+224hTJE E #I8H8H#I I!XD1E E D8DE E Es     A G&*AG&5G&G&&G/rankc                     t        | |d      S )a  Returns the rank of a tensor.

  See also `tf.shape`.

  Returns a 0-D `int32` `Tensor` representing the rank of `input`.

  For example:

  ```python
  # shape of tensor 't' is [2, 2, 3]
  t = tf.constant([[[1, 1, 1], [2, 2, 2]], [[3, 3, 3], [4, 4, 4]]])
  tf.rank(t)  # 3
  ```

  **Note**: The rank of a tensor is not the same as the rank of a matrix. The
  rank of a tensor is the number of indices required to uniquely select each
  element of the tensor. Rank is also known as "order", "degree", or "ndims."

  Args:
    input: A `Tensor` or `SparseTensor`.
    name: A name for the operation (optional).

  Returns:
    A `Tensor` of type `int32`.

  @compatibility(numpy)
  Equivalent to np.ndim
  @end_compatibility
  Trl   )rank_internalrB   r)   s     r+   r   r   n  s    B 
udT	22r,   c                    t        j                  |d| g      5 }t        | t        j                  t        j
                  f      r*t        j                  | j                  |      cddd       S t        j                  |       } | j                         }|r;|j                  /t        |j                  t        j                  |      cddd       S t        j                  | |      cddd       S # 1 sw Y   yxY w)zReturns the rank of a tensor.

  Args:
    input: A `Tensor` or `SparseTensor`.
    name: A name for the operation (optional).
    optimize: if true, encode the rank as a constant when possible.

  Returns:
    A `Tensor` of type `int32`.
  Rankr9   N)r   rr   r;   r   rs   rt   r   r~   rv   rA   rw   ndimsr   r   rf   r   )rB   r)   rl   r{   s       r+   r   r     s     ~~dFUG, 	2**M,K,KLN 1 1=	2 	2
 ##E*eOO%k	k''3))6<<dC	2 	2 D1	2 	2 	2s   AC0.AC0C00C9slicec                 4    t        j                  | |||      S )a  Extracts a slice from a tensor.

  See also `tf.strided_slice`.

  This operation extracts a slice of size `size` from a tensor `input_` starting
  at the location specified by `begin`. The slice `size` is represented as a
  tensor shape, where `size[i]` is the number of elements of the 'i'th dimension
  of `input_` that you want to slice. The starting location (`begin`) for the
  slice is represented as an offset in each dimension of `input_`. In other
  words, `begin[i]` is the offset into the i'th dimension of `input_` that you
  want to slice from.

  Note that `tf.Tensor.__getitem__` is typically a more pythonic way to
  perform slices, as it allows you to write `foo[3:7, :-2]` instead of
  `tf.slice(foo, [3, 0], [4, foo.get_shape()[1]-2])`.

  `begin` is zero-based; `size` is one-based. If `size[i]` is -1,
  all remaining elements in dimension i are included in the
  slice. In other words, this is equivalent to setting:

  `size[i] = input_.dim_size(i) - begin[i]`

  This operation requires that:

  `0 <= begin[i] <= begin[i] + size[i] <= Di  for i in [0, n]`

  For example:

  ```python
  t = tf.constant([[[1, 1, 1], [2, 2, 2]],
                   [[3, 3, 3], [4, 4, 4]],
                   [[5, 5, 5], [6, 6, 6]]])
  tf.slice(t, [1, 0, 0], [1, 1, 3])  # [[[3, 3, 3]]]
  tf.slice(t, [1, 0, 0], [1, 2, 3])  # [[[3, 3, 3],
                                     #   [4, 4, 4]]]
  tf.slice(t, [1, 0, 0], [2, 1, 3])  # [[[3, 3, 3]],
                                     #  [[5, 5, 5]]]
  ```

  Args:
    input_: A `Tensor`.
    begin: An `int32` or `int64` `Tensor`.
    size: An `int32` or `int64` `Tensor`.
    name: A name for the operation (optional).

  Returns:
    A `Tensor` the same type as `input_`.
  r9   )r   _slice)input_beginr~   r)   s       r+   r   r     s    h 
		feT	==r,   strided_slicec                    	 t              t        j                  | |

      }|
	d	f
d	}||_        |S )ae  Extracts a strided slice of a tensor (generalized Python array indexing).

  See also `tf.slice`.

  **Instead of calling this op directly most users will want to use the
  NumPy-style slicing syntax (e.g. `tensor[..., 3:4:-1, tf.newaxis, 3]`), which
  is supported via `tf.Tensor.__getitem__` and `tf.Variable.__getitem__`.**
  The interface of this op is a low-level encoding of the slicing syntax.

  Roughly speaking, this op extracts a slice of size `(end-begin)/stride`
  from the given `input_` tensor. Starting at the location specified by `begin`
  the slice continues by adding `stride` to the index until all dimensions are
  not less than `end`.
  Note that a stride can be negative, which causes a reverse slice.

  Given a Python slice `input[spec0, spec1, ..., specn]`,
  this function will be called as follows.

  `begin`, `end`, and `strides` will be vectors of length n.
  n in general is not equal to the rank of the `input_` tensor.

  In each mask field (`begin_mask`, `end_mask`, `ellipsis_mask`,
  `new_axis_mask`, `shrink_axis_mask`) the ith bit will correspond to
  the ith spec.

  If the ith bit of `begin_mask` is set, `begin[i]` is ignored and
  the fullest possible range in that dimension is used instead.
  `end_mask` works analogously, except with the end range.

  `foo[5:,:,:3]` on a 7x8x9 tensor is equivalent to `foo[5:7,0:8,0:3]`.
  `foo[::-1]` reverses a tensor with shape 8.

  If the ith bit of `ellipsis_mask` is set, as many unspecified dimensions
  as needed will be inserted between other dimensions. Only one
  non-zero bit is allowed in `ellipsis_mask`.

  For example `foo[3:5,...,4:5]` on a shape 10x3x3x10 tensor is
  equivalent to `foo[3:5,:,:,4:5]` and
  `foo[3:5,...]` is equivalent to `foo[3:5,:,:,:]`.

  If the ith bit of `new_axis_mask` is set, then `begin`,
  `end`, and `stride` are ignored and a new length 1 dimension is
  added at this point in the output tensor.

  For example,
  `foo[:4, tf.newaxis, :2]` would produce a shape `(4, 1, 2)` tensor.

  If the ith bit of `shrink_axis_mask` is set, it implies that the ith
  specification shrinks the dimensionality by 1, taking on the value at index
  `begin[i]`. `end[i]` and `strides[i]` are ignored in this case. For example in
  Python one might do `foo[:, 3, :]` which would result in `shrink_axis_mask`
  equal to 2.


  NOTE: `begin` and `end` are zero-indexed.
  `strides` entries must be non-zero.


  ```python
  t = tf.constant([[[1, 1, 1], [2, 2, 2]],
                   [[3, 3, 3], [4, 4, 4]],
                   [[5, 5, 5], [6, 6, 6]]])
  tf.strided_slice(t, [1, 0, 0], [2, 1, 3], [1, 1, 1])  # [[[3, 3, 3]]]
  tf.strided_slice(t, [1, 0, 0], [2, 2, 3], [1, 1, 1])  # [[[3, 3, 3],
                                                        #   [4, 4, 4]]]
  tf.strided_slice(t, [1, -1, 0], [2, -3, 3], [1, -1, 1])  # [[[4, 4, 4],
                                                           #   [3, 3, 3]]]
  ```

  Args:
    input_: A `Tensor`.
    begin: An `int32` or `int64` `Tensor`.
    end: An `int32` or `int64` `Tensor`.
    strides: An `int32` or `int64` `Tensor`.
    begin_mask: An `int32` mask.
    end_mask: An `int32` mask.
    ellipsis_mask: An `int32` mask.
    new_axis_mask: An `int32` mask.
    shrink_axis_mask: An `int32` mask.
    var: The variable corresponding to `input_` or None
    name: A name for the operation (optional).

  Returns:
    A `Tensor` the same type as `input`.
  )
rB   r   endstridesr)   
begin_maskend_maskellipsis_masknew_axis_maskshrink_axis_maskc                 H   
 |dz   }j                  
| |	
      S )z=Closure that holds all the arguments to create an assignment._assign)
r   r   r   r1   r)   r   r   r   r   r   )_strided_slice_assign)valr)   r   r   r   r   r   r   parent_namer   r   vars     r+   assignzstrided_slice.<locals>.assignX  sJ     
Y&&&%%+ ' 
- 
-r,   rN   )	ones_liker   r   r   )r   r   r   r   r   r   r   r   r   r   r)   opr   r   s    `````````   @r+   r   r     sh    F _G""
!!'
)" +_- -$ BI	)r,   parallel_stackc           
         t        j                         rt        d      t        j                  |      5  t        j
                  | d         }t        j
                  |      j                         }t        j                  t        |       g      }|j                  |      }t        j                  | D cg c]  }t        |d       c}|      cddd       S c c}w # 1 sw Y   yxY w)a  Stacks a list of rank-`R` tensors into one rank-`(R+1)` tensor in parallel.

  Requires that the shape of inputs be known at graph construction time.

  Packs the list of tensors in `values` into a tensor with rank one higher than
  each tensor in `values`, by packing them along the first dimension.
  Given a list of length `N` of tensors of shape `(A, B, C)`; the `output`
  tensor will have the shape `(N, A, B, C)`.

  For example:

  ```python
  x = tf.constant([1, 4])
  y = tf.constant([2, 5])
  z = tf.constant([3, 6])
  tf.parallel_stack([x, y, z])  # [[1, 4], [2, 5], [3, 6]]
  ```

  The difference between `stack` and `parallel_stack` is that `stack` requires
  all the inputs be computed before the operation will begin but doesn't require
  that the input shapes be known during graph construction.

  `parallel_stack` will copy pieces of the input into the output as they become
  available, in some situations this can provide a performance benefit.

  Unlike `stack`, `parallel_stack` does NOT support backpropagation.

  This is the opposite of unstack.  The numpy equivalent is

      tf.parallel_stack([x, y, z]) = np.asarray([x, y, z])

  @compatibility(eager)
  parallel_stack is not compatible with eager execution.
  @end_compatibility

  Args:
    values: A list of `Tensor` objects with the same shape and type.
    name: A name for this operation (optional).

  Returns:
    output: A stacked `Tensor` with the same type as `values`.

  Raises:
    RuntimeError: if executed in eager mode.
  z;tf.parallel_stack() is not compatible with eager execution.r   r(   N)r   r?   RuntimeErrorr   rr   rA   rw   r   TensorShapelenconcatenater   parallel_concatrD   )valuesr)   value_tvalue_shapeoutput_shaper1   s         r+   r   r   o  s    `  
 * + +
~~d I##F1I.G''0::<K++S[M:L++K8L((,235UA	3<II I 	4I Is   A?C4C
CCC'c                    t        j                         r)t        d | D              rt        j                  | |      S d}g }t        j                  |      5 }t        |       D ]  \  }}t        |t        j                        rN|8|j                  j                  |k7  rt        d|j                   d| d|d      |j                  |       d	}nt        |t        t         f      rDt#        ||t%        |            }t        |t        j                        rd	}|j                  |       |j                  |        |rg }	t        |      D ]a  \  }}t        |t        j                        r|	j                  |       2|	j                  t'        j(                  ||t%        |      
             c t        j                  |	|      cddd       S |cddd       S # 1 sw Y   yxY w)a4  Converts the given list or tuple to a tensor by packing.

  Args:
    list_or_tuple: A (possibly nested) list or tuple containing a tensor.
    dtype: The element type of the returned tensor.
    name: A name for the returned tensor.

  Returns:
    A `tf.Tensor` with value equivalent to `list_or_tuple`.
  c              3   P   K   | ]  }t        |t        j                           y wrN   )r;   r   Tensorr   elems     r+   r   z&_autopacking_helper.<locals>.<genexpr>  s     
CT:dDKK(
Cs   $&r9   FNz3Cannot convert a list containing a tensor of dtype z to z (Tensor is: )Tr   r)   )r   r?   allr   packr   rr   	enumerater;   r   r   r   
base_dtype	TypeErrorappendlisttuple_autopacking_helperstrr	   r   )
list_or_tupler   r)   	must_packconverted_elemsscopeir   converted_elemelems_as_tensorss
             r+   r   r     s      
C]
CCD99)/
~~d u]+ %4	D$++	&!6!6%!?O!ZZLUG=K L Lt$	dT5M*,T5#a&Andkk2)~.t$% / D'!TdDKK(

!
!$
'
 
!
!""4u3q6BDD  0u=5 8 9  s   E0GGG%c                     | D ]\  }t        |t        j                        r|j                  j                  c S t        |t
        t        f      sLt        |      }|Z|c S  y)a-  Returns the dtype of any tensor-like object in `list_or_tuple`, if found.

  Args:
    list_or_tuple: A list or tuple representing an object that can be converted
      to a `tf.Tensor`.

  Returns:
    The dtype of any tensor-like object in `list_or_tuple`, or `None` if no
    such object exists.
  N)r;   r   r   r   r   r   r   _get_dtype_from_nested_lists)r   r   maybe_dtypes      r+   r   r     sZ      d$$ZZ"""	D4-	(06k		  
r,   c                       fd}|S )Nc                     t        | t        j                        r/| j                  j                  k7  rt        j                  |       } | S rN   )r;   r   r   r   r   r   ru   )r   r   s    r+   _maybe_castz/_cast_nested_seqs_to_dtype.<locals>._maybe_cast  s:    $$	$**''	'  u-Kr,   r   )r   r   s   ` r+   _cast_nested_seqs_to_dtyper     s     
r,   c                 L    t        d t        j                  |       D              S )Nc              3   >   K   | ]  }t        |      t        v   y wrN   )type_NON_AUTOPACKABLE_TYPESr   s     r+   r   z'_should_not_autopack.<locals>.<genexpr>)  s     OtT$Z22Os   )r   r    flatten)vs    r+   _should_not_autopackr   #  s     
Ot||AO	OOr,   Fc                     |st        |       rt        S t        |       }|t        S ||}n$||k7  rt        j                  t        |      |       } t        | ||xs d      S )z>Tensor conversion function that automatically packs arguments.packed)r   NotImplementedr   r    r>   r   r   )r   r   r)   as_refinferred_dtypes        r+    _autopacking_conversion_functionr   -  si    #A&/2. 
]E5e<a@A	Qt'7x	88r,   c   concatc                 |   t        | t        t        f      s| g} t        |       dk(  rrt	        j
                  |      5 }t	        j                  |dt        j                        j                         j                  d       t        | d   |      cddd       S t        j                  | ||      S # 1 sw Y   !xY w)a  Concatenates tensors along one dimension.

  See also `tf.tile`, `tf.stack`, `tf.repeat`.

  Concatenates the list of tensors `values` along dimension `axis`.  If
  `values[i].shape = [D0, D1, ... Daxis(i), ...Dn]`, the concatenated
  result has shape

      [D0, D1, ... Raxis, ...Dn]

  where

      Raxis = sum(Daxis(i))

  That is, the data from the input tensors is joined along the `axis`
  dimension.

  The number of dimensions of the input tensors must match, and all dimensions
  except the `axis` must be equal.

  For example:

  >>> t1 = [[1, 2, 3], [4, 5, 6]]
  >>> t2 = [[7, 8, 9], [10, 11, 12]]
  >>> tf.concat([t1, t2], 0)
  <tf.Tensor: shape=(4, 3), dtype=int32, numpy=
  array([[ 1,  2,  3],
         [ 4,  5,  6],
         [ 7,  8,  9],
         [10, 11, 12]], dtype=int32)>

  >>> tf.concat([t1, t2], 1)
  <tf.Tensor: shape=(2, 6), dtype=int32, numpy=
  array([[ 1,  2,  3,  7,  8,  9],
         [ 4,  5,  6, 10, 11, 12]], dtype=int32)>

  As in Python, the `axis` could also be negative numbers. Negative `axis`
  are interpreted as counting from the end of the rank, i.e.,
   `axis + rank(values)`-th dimension.

  For example:

  >>> t1 = [[[1, 2], [2, 3]], [[4, 4], [5, 3]]]
  >>> t2 = [[[7, 4], [8, 4]], [[2, 10], [15, 11]]]
  >>> tf.concat([t1, t2], -1)
  <tf.Tensor: shape=(2, 2, 4), dtype=int32, numpy=
    array([[[ 1,  2,  7,  4],
            [ 2,  3,  8,  4]],
           [[ 4,  4,  2, 10],
            [ 5,  3, 15, 11]]], dtype=int32)>

  Note: If you are concatenating along a new axis consider using stack.
  E.g.

  ```python
  tf.concat([tf.expand_dims(t, axis) for t in tensors], axis)
  ```

  can be rewritten as

  ```python
  tf.stack(tensors, axis=axis)
  ```

  Args:
    values: A list of `Tensor` objects or a single `Tensor`.
    axis: 0-D `int32` `Tensor`.  Dimension along which to concatenate. Must be
      in the range `[-rank(values), rank(values))`. As in Python, indexing for
      axis is 0-based. Positive axis in the rage of `[0, rank(values))` refers
      to `axis`-th dimension. And negative axis refers to `axis +
      rank(values)`-th dimension.
    name: A name for the operation (optional).

  Returns:
    A `Tensor` resulting from concatenation of the input tensors.
     
concat_dimr)   r   r   r9   N)r   rG   r)   )r;   r   r   r   r   rr   rA   r   rf   rw   assert_has_rankr4   r   	concat_v2)r   rG   r)   r   s       r+   r   r   E  s    ^ 
FT5M	*XF[A
 
	 ,	
\'ik//!*<fQid+	, ,
 
	 	 T	EE, ,s   AB22B;boolean_maskc                 n   dd}t        j                  || |g      5  t        j                  | d      } t        j                  |d      }|j                         }|j                  }| j                         }|dk(  rt        d      |t        d	      |dn|}t        j                  |      }||}||||z    j                  |       t        j                  t        |       |||z    dg      }	t        | t        t        |       d| |	gt        |       ||z   d gd            } |b||||z    j                         }
| j                  t!        j"                  |d|       j%                  |
g      j%                  |||z   d              t        |d
g      } || ||      cddd       S # 1 sw Y   yxY w)aV  Apply boolean mask to tensor.

  Numpy equivalent is `tensor[mask]`.

  In general, `0 < dim(mask) = K <= dim(tensor)`, and `mask`'s shape must match
  the first K dimensions of `tensor`'s shape.  We then have:
    `boolean_mask(tensor, mask)[i, j1,...,jd] = tensor[i1,...,iK,j1,...,jd]`
  where `(i1,...,iK)` is the ith `True` entry of `mask` (row-major order).
  The `axis` could be used with `mask` to indicate the axis to mask from.
  In that case, `axis + dim(mask) <= dim(tensor)` and `mask`'s shape must match
  the first `axis + dim(mask)` dimensions of `tensor`'s shape.

  See also: `tf.ragged.boolean_mask`, which can be applied to both dense and
  ragged tensors, and can be used if you need to preserve the masked dimensions
  of `tensor` (rather than flattening them, as `tf.boolean_mask` does).

  Examples:

  ```python
  # 1-D example
  tensor = [0, 1, 2, 3]
  mask = np.array([True, False, True, False])
  tf.boolean_mask(tensor, mask)  # [0, 2]

  # 2-D example
  tensor = [[1, 2], [3, 4], [5, 6]]
  mask = np.array([True, False, True])
  tf.boolean_mask(tensor, mask)  # [[1, 2], [5, 6]]
  ```

  Args:
    tensor:  N-D Tensor.
    mask:  K-D boolean Tensor, K <= N and K must be known statically.
    name:  A name for this operation (optional).
    axis:  A 0-D int Tensor representing the axis in `tensor` to mask from. By
      default, axis is 0 which will mask from the first dimension. Otherwise K +
      axis <= N.

  Returns:
    (N-K+1)-dimensional tensor populated by entries in `tensor` corresponding
    to `True` values in `mask`.

  Raises:
    ValueError:  If shapes do not conform.
  Nc                 L    t        t        |      dg      }t        | ||      S )z.Mask tensor along dimension 0 with a 1-D mask.r   rG   )squeezewhere_v2gather)reshaped_tensormaskrG   indicess       r+   _apply_mask_1dz$boolean_mask.<locals>._apply_mask_1d  s#    htnA3/G/766r,   r   r   r9   r   r   zmask cannot be scalar.zNumber of mask dimensions must be specified, even if some dimensions are None.  E.g. shape=[None] is ok, but shape=None is not.rN   )r   rr   rA   rw   r   rI   r   constant_valueassert_is_compatible_withr   r   r(   r$   r   r   	set_shaper   as_shaper   )r   r   r)   rG   r   
shape_mask
ndims_maskshape_tensor
axis_valueleading_size	first_dims              r+   r   r     s   b7
 ~~dFD>2 %.""69F  F3D!J!!J##%LQ/00HI I 1$D++D1Jd4z)*DDZP$$U6]4z8I%JQCPL&M%4 <.&M$+,-
 	F tD:$56CCEi


Ud 3
4
@
@k&;|D:4E4F'GHJ 4"D&$-K%. %. %.s   FF++F4c                     t        | |||      S )a  Apply boolean mask to tensor.

  Numpy equivalent is `tensor[mask]`.

  In general, `0 < dim(mask) = K <= dim(tensor)`, and `mask`'s shape must match
  the first K dimensions of `tensor`'s shape.  We then have:
    `boolean_mask(tensor, mask)[i, j1,...,jd] = tensor[i1,...,iK,j1,...,jd]`
  where `(i1,...,iK)` is the ith `True` entry of `mask` (row-major order).
  The `axis` could be used with `mask` to indicate the axis to mask from.
  In that case, `axis + dim(mask) <= dim(tensor)` and `mask`'s shape must match
  the first `axis + dim(mask)` dimensions of `tensor`'s shape.

  See also: `tf.ragged.boolean_mask`, which can be applied to both dense and
  ragged tensors, and can be used if you need to preserve the masked dimensions
  of `tensor` (rather than flattening them, as `tf.boolean_mask` does).

  Examples:

  >>> tensor = [0, 1, 2, 3]  # 1-D example
  >>> mask = np.array([True, False, True, False])
  >>> tf.boolean_mask(tensor, mask)
  <tf.Tensor: shape=(2,), dtype=int32, numpy=array([0, 2], dtype=int32)>

  >>> tensor = [[1, 2], [3, 4], [5, 6]] # 2-D example
  >>> mask = np.array([True, False, True])
  >>> tf.boolean_mask(tensor, mask)
  <tf.Tensor: shape=(2, 2), dtype=int32, numpy=
  array([[1, 2],
         [5, 6]], dtype=int32)>

  Args:
    tensor:  N-D Tensor.
    mask:  K-D boolean Tensor, K <= N and K must be known statically.
    axis:  A 0-D int Tensor representing the axis in `tensor` to mask from. By
      default, axis is 0 which will mask from the first dimension. Otherwise K +
      axis <= N.
    name:  A name for this operation (optional).

  Returns:
    (N-K+1)-dimensional tensor populated by entries in `tensor` corresponding
    to `True` values in `mask`.

  Raises:
    ValueError:  If shapes do not conform.

  Examples:

  ```python
  # 2-D example
  tensor = [[1, 2], [3, 4], [5, 6]]
  mask = np.array([True, False, True])
  boolean_mask(tensor, mask)  # [[1, 2], [5, 6]]
  ```
  )r   )r   r   rG   r)   s       r+   boolean_mask_v2r
    s    r 
fdD$	//r,   zsparse.masksparse_maskc                    t        j                  |d| |g      5 }| j                  }t        j                  ||      \  }}t        | j                  ||      }t        j                  ||| j                        cddd       S # 1 sw Y   yxY w)a  Masks elements of `IndexedSlices`.

  Given an `IndexedSlices` instance `a`, returns another `IndexedSlices` that
  contains a subset of the slices of `a`. Only the slices at indices not
  specified in `mask_indices` are returned.

  This is useful when you need to extract a subset of slices in an
  `IndexedSlices` object.

  For example:

  ```python
  # `a` contains slices at indices [12, 26, 37, 45] from a large tensor
  # with shape [1000, 10]
  a.indices  # [12, 26, 37, 45]
  tf.shape(a.values)  # [4, 10]

  # `b` will be the subset of `a` slices at its second and third indices, so
  # we want to mask its first and last indices (which are at absolute
  # indices 12, 45)
  b = tf.sparse.mask(a, [12, 45])

  b.indices  # [26, 37]
  tf.shape(b.values)  # [2, 10]
  ```

  Args:
    a: An `IndexedSlices` instance.
    mask_indices: Indices of elements to mask.
    name: A name for the operation (optional).

  Returns:
    The masked `IndexedSlices` instance.
  r  r9   N)
r   rr   r   r   rP   r   r   r   IndexedSlicesrv   )amask_indicesr)   r   out_indices	to_gather
out_valuess          r+   r  r  >  s}    J ~~dMA|+<= PiiG*44WlKK)$7J''
KO	P P Ps   ABBuniquec                 0    t        j                  | ||      S )a   Finds unique elements in a 1-D tensor.

  See also `tf.unique_with_counts`.

  This operation returns a tensor `y` containing all the unique elements
  of `x` sorted in the same order that they occur in `x`. This operation
  also returns a tensor `idx` the same size as `x` that contains the index
  of each value of `x` in the unique output `y`. In other words:


    y[idx[i]] = x[i] for i in [0, 1,...,rank(x) - 1]

  Example usage:

  >>> x = tf.constant([1, 1, 2, 4, 4, 4, 7, 8, 8])
  >>> y, idx = unique(x)
  >>> y
  <tf.Tensor: id=5, shape=(5,), dtype=int32,
  numpy=array([1, 2, 4, 7, 8], dtype=int32)>
  >>> idx
  <tf.Tensor: id=6, shape=(9,), dtype=int32,
  numpy=array([0, 0, 1, 2, 2, 2, 3, 4, 4], dtype=int32)>

  Args:
    x: A Tensor. 1-D.
    out_idx: An optional tf.DType from: tf.int32, tf.int64. Defaults to
      tf.int32.
    name: A name for the operation (optional).

  Returns:
    A tuple of Tensor objects (y, idx).
      y: A Tensor. Has the same type as x.
      idx: A Tensor of type out_idx.

  )r   r  rQ   rS   r)   s      r+   r  r  j  s    T 
		a$	//r,   unique_with_countsc                 0    t        j                  | ||      S )a  Finds unique elements in a 1-D tensor.

  See also `tf.unique`.

  This operation returns a tensor `y` containing all the unique elements
  of `x` sorted in the same order that they occur in `x`. This operation
  also returns a tensor `idx` the same size as `x` that contains the index
  of each value of `x` in the unique output `y`. Finally, it returns a
  third tensor `count` that contains the count of each element of `y`
  in `x`. In other words:

    y[idx[i]] = x[i] for i in [0, 1,...,rank(x) - 1]

  Example usage:

  >>> x = tf.constant([1, 1, 2, 4, 4, 4, 7, 8, 8])
  >>> y, idx, count = unique_with_counts(x)
  >>> y
  <tf.Tensor: id=8, shape=(5,), dtype=int32,
  numpy=array([1, 2, 4, 7, 8], dtype=int32)>
  >>> idx
  <tf.Tensor: id=9, shape=(9,), dtype=int32,
  numpy=array([0, 0, 1, 2, 2, 2, 3, 4, 4], dtype=int32)>
  >>> count
  <tf.Tensor: id=10, shape=(5,), dtype=int32,
  numpy=array([2, 1, 3, 1, 2], dtype=int32)>

  Args:
    x: A Tensor. 1-D.
    out_idx: An optional tf.DType from: tf.int32, tf.int64. Defaults to
      tf.int32.
    name: A name for the operation (optional).

  Returns:
    A tuple of Tensor objects (y, idx, count).
      y: A Tensor. Has the same type as x.
      idx: A Tensor of type out_idx.
      count: A Tensor of type out_idx.

  )r   r  r  s      r+   r  r    s    ^ 
	)	)!Wd	;;r,   splitc                 z   t        |t        j                  t        j                  f      rt        j                  ||| |      S t        j                  |      }|j                         dk(  rt        d|      |'|j                         }|r|d   }|t        d|       t        j                  | ||||      S )a  Splits a tensor `value` into a list of sub tensors.

  See also `tf.unstack`.

  If `num_or_size_splits` is an `int`,  then it splits `value` along the
  dimension `axis` into `num_or_size_splits` smaller tensors. This requires that
  `value.shape[axis]` is divisible by `num_or_size_splits`.

  If `num_or_size_splits` is a 1-D Tensor (or list), then `value` is split into
  `len(num_or_size_splits)` elements. The shape of the `i`-th
  element has the same size as the `value` except along dimension `axis` where
  the size is `num_or_size_splits[i]`.

  For example:

  >>> x = tf.Variable(tf.random.uniform([5, 30], -1, 1))
  >>>
  >>> # Split `x` into 3 tensors along dimension 1
  >>> s0, s1, s2 = tf.split(x, num_or_size_splits=3, axis=1)
  >>> tf.shape(s0).numpy()
  array([ 5, 10], dtype=int32)
  >>>
  >>> # Split `x` into 3 tensors with sizes [4, 15, 11] along dimension 1
  >>> split0, split1, split2 = tf.split(x, [4, 15, 11], 1)
  >>> tf.shape(split0).numpy()
  array([5, 4], dtype=int32)
  >>> tf.shape(split1).numpy()
  array([ 5, 15], dtype=int32)
  >>> tf.shape(split2).numpy()
  array([ 5, 11], dtype=int32)

  Args:
    value: The `Tensor` to split.
    num_or_size_splits: Either an `int` indicating the number of splits
      along `axis` or a 1-D integer `Tensor` or Python list containing the sizes
      of each output tensor along `axis`. If an `int`, then it must evenly
      divide `value.shape[axis]`; otherwise the sum of sizes along the split
      axis must match that of the `value`.
    axis: An `int` or scalar `int32` `Tensor`. The dimension along which
      to split. Must be in the range `[-rank(value), rank(value))`. Defaults to
      0.
    num: Optional, an `int`, used to specify the number of outputs when it
      cannot be inferred from the shape of `size_splits`.
    name: A name for the operation (optional).

  Returns:
    if `num_or_size_splits` is an `int` returns a list of
    `num_or_size_splits` `Tensor` objects; if `num_or_size_splits` is a 1-D
    list or 1-D `Tensor` returns `num_or_size_splits.get_shape[0]`
    `Tensor` objects resulting from splitting `value`.

  Raises:
    ValueError: If `num` is unspecified and cannot be inferred.
    ValueError: If `num_or_size_splits` is a scalar `Tensor`.
  )rG   	num_splitr1   r)   r   zaRank-0 tensors are not supported as the num_or_size_splits argument to split. Argument provided: z'Cannot infer argument `num` from shape )r1   size_splitsrG   r  r)   )r;   numbersIntegralr   	Dimensionr   r  r   rA   _rankrI   r   split_v)r1   num_or_size_splitsrG   numr)   r  size_splits_shapes          r+   r  r    s    t "!!<#9#9:</u4I I %%&89+A
-?	BC C 	[#002a c
{34F3G
HJ J 
		{4
Q Qr,   	transposec                      t        | |||      S )a	  Transposes `a`, where `a` is a Tensor.

  Permutes the dimensions according to the value of `perm`.

  The returned tensor's dimension `i` will correspond to the input dimension
  `perm[i]`. If `perm` is not given, it is set to (n-1...0), where n is the rank
  of the input tensor. Hence, by default, this operation performs a regular
  matrix transpose on 2-D input Tensors.

  If conjugate is `True` and `a.dtype` is either `complex64` or `complex128`
  then the values of `a` are conjugated and transposed.

  @compatibility(numpy)
  In `numpy` transposes are memory-efficient constant time operations as they
  simply return a new view of the same data with adjusted `strides`.

  TensorFlow does not support strides, so `transpose` returns a new tensor with
  the items permuted.
  @end_compatibility

  For example:

  >>> x = tf.constant([[1, 2, 3], [4, 5, 6]])
  >>> tf.transpose(x)
  <tf.Tensor: shape=(3, 2), dtype=int32, numpy=
  array([[1, 4],
         [2, 5],
         [3, 6]], dtype=int32)>

  Equivalently, you could call `tf.transpose(x, perm=[1, 0])`.

  If `x` is complex, setting conjugate=True gives the conjugate transpose:

  >>> x = tf.constant([[1 + 1j, 2 + 2j, 3 + 3j],
  ...                  [4 + 4j, 5 + 5j, 6 + 6j]])
  >>> tf.transpose(x, conjugate=True)
  <tf.Tensor: shape=(3, 2), dtype=complex128, numpy=
  array([[1.-1.j, 4.-4.j],
         [2.-2.j, 5.-5.j],
         [3.-3.j, 6.-6.j]])>

  'perm' is more useful for n-dimensional tensors where n > 2:

  >>> x = tf.constant([[[ 1,  2,  3],
  ...                   [ 4,  5,  6]],
  ...                  [[ 7,  8,  9],
  ...                   [10, 11, 12]]])

  As above, simply calling `tf.transpose` will default to `perm=[2,1,0]`.

  To take the transpose of the matrices in dimension-0 (such as when you are
  transposing matrices where 0 is the batch dimension), you would set
  `perm=[0,2,1]`.

  >>> tf.transpose(x, perm=[0, 2, 1])
  <tf.Tensor: shape=(2, 3, 2), dtype=int32, numpy=
  array([[[ 1,  4],
          [ 2,  5],
          [ 3,  6]],
          [[ 7, 10],
          [ 8, 11],
          [ 9, 12]]], dtype=int32)>

  Note: This has a shorthand `linalg.matrix_transpose`):

  Args:
    a: A `Tensor`.
    perm: A permutation of the dimensions of `a`.  This should be a vector.
    conjugate: Optional bool. Setting it to `True` is mathematically equivalent
      to tf.math.conj(tf.transpose(input)).
    name: A name for the operation (optional).

  Returns:
    A transposed `Tensor`.
  )r  permr)   	conjugate)r$  )r  r&  r'  r)   s       r+   transpose_v2r(  !  s    \ 
QT		BBr,   c                 J   t        j                  |d| g      5 }t        j                  |       st        j                  | d      } |r'| j
                  j                  rt        j                  }nt        j                  }| || ||      cddd       S | j                  j                  }|.t        j                  t        j                  |       dz
  dd      }n*t        j                  |dz
  ddt        j                         } || ||      cddd       S # 1 sw Y   yxY w)a	  Transposes `a`.

  Permutes the dimensions according to `perm`.

  The returned tensor's dimension i will correspond to the input dimension
  `perm[i]`. If `perm` is not given, it is set to (n-1...0), where n is
  the rank of the input tensor. Hence, by default, this operation performs a
  regular matrix transpose on 2-D input Tensors. If conjugate is True and
  `a.dtype` is either `complex64` or `complex128` then the values of `a`
  are conjugated and transposed.

  @compatibility(numpy)
  In `numpy` transposes are memory-efficient constant time operations as they
  simply return a new view of the same data with adjusted `strides`.

  TensorFlow does not support strides, so `transpose` returns a new tensor with
  the items permuted.
  @end_compatibility

  For example:

  ```python
  x = tf.constant([[1, 2, 3], [4, 5, 6]])
  tf.transpose(x)  # [[1, 4]
                   #  [2, 5]
                   #  [3, 6]]

  # Equivalently
  tf.transpose(x, perm=[1, 0])  # [[1, 4]
                                #  [2, 5]
                                #  [3, 6]]

  # If x is complex, setting conjugate=True gives the conjugate transpose
  x = tf.constant([[1 + 1j, 2 + 2j, 3 + 3j],
                   [4 + 4j, 5 + 5j, 6 + 6j]])
  tf.transpose(x, conjugate=True)  # [[1 - 1j, 4 - 4j],
                                   #  [2 - 2j, 5 - 5j],
                                   #  [3 - 3j, 6 - 6j]]

  # 'perm' is more useful for n-dimensional tensors, for n > 2
  x = tf.constant([[[ 1,  2,  3],
                    [ 4,  5,  6]],
                   [[ 7,  8,  9],
                    [10, 11, 12]]])

  # Take the transpose of the matrices in dimension-0
  # (this common operation has a shorthand `linalg.matrix_transpose`)
  tf.transpose(x, perm=[0, 2, 1])  # [[[1,  4],
                                   #   [2,  5],
                                   #   [3,  6]],
                                   #  [[7, 10],
                                   #   [8, 11],
                                   #   [9, 12]]]
  ```

  Args:
    a: A `Tensor`.
    perm: A permutation of the dimensions of `a`.
    name: A name for the operation (optional).
    conjugate: Optional bool. Setting it to `True` is mathematically equivalent
      to tf.math.conj(tf.transpose(input)).

  Returns:
    A transposed `Tensor`.
  r$  r  r9   Nr   r   r   )r   rr   r   
is_tf_typerA   r   
is_complexr   conjugate_transposer$  r(   r   r   _ranger   arangerf   )r  r&  r)   r'  transpose_fnr   s         r+   r$  r$  r  s    H ~~dK!- ,!!!$



,aQWW''"66l",,l!T-, , 77<<D|  !3!3A!6!:BCdYYtaxRrxx8d4d+#, , ,s   A2DA:DD"linalg.matrix_transpose)linalg.transposer0  matrix_transposer2  r1  c                    t        j                  || g      5  t        j                  | d      } | j                         }|j                  }|<|dk  rt        d|  d|       t        t        |dz
              |dz
  gz   |dz
  gz   }n9t        |       }t        t        j                  d	|dz
  d      |dz
  |dz
  gfd	      }t        | ||
      cddd       S # 1 sw Y   yxY w)a  Transposes last two dimensions of tensor `a`.

  For example:

  ```python
  x = tf.constant([[1, 2, 3], [4, 5, 6]])
  tf.linalg.matrix_transpose(x)  # [[1, 4],
                                 #  [2, 5],
                                 #  [3, 6]]

  x = tf.constant([[1 + 1j, 2 + 2j, 3 + 3j],
                   [4 + 4j, 5 + 5j, 6 + 6j]])
  tf.linalg.matrix_transpose(x, conjugate=True)  # [[1 - 1j, 4 - 4j],
                                                 #  [2 - 2j, 5 - 5j],
                                                 #  [3 - 3j, 6 - 6j]]

  # Matrix with two batch dimensions.
  # x.shape is [1, 2, 3, 4]
  # tf.linalg.matrix_transpose(x) is shape [1, 2, 4, 3]
  ```

  Note that `tf.matmul` provides kwargs allowing for transpose of arguments.
  This is done with minimal cost, and is preferable to using this function. E.g.

  ```python
  # Good!  Transpose is taken at minimal additional cost.
  tf.matmul(matrix, b, transpose_b=True)

  # Inefficient!
  tf.matmul(matrix, tf.linalg.matrix_transpose(b))
  ```

  @compatibility(numpy)
  In `numpy` transposes are memory-efficient constant time operations as they
  simply return a new view of the same data with adjusted `strides`.

  TensorFlow does not support strides, `linalg.matrix_transpose` returns a new
  tensor with the items permuted.
  @end_compatibility

  Args:
    a: A `Tensor` with `rank >= 2`.
    name: A name for the operation (optional).
    conjugate: Optional bool. Setting it to `True` is mathematically equivalent
      to tf.math.conj(tf.linalg.matrix_transpose(input)).

  Returns:
    A transposed batch matrix `Tensor`.

  Raises:
    ValueError:  If `a` is determined statically to have `rank < 2`.
  r   r  r9   N   zHArgument `a` should be a (batch) matrix with rank >= 2.  Received `a` = z with shape: r   r   )r&  r'  )r   rr   rA   rw   r   rI   r   ranger   r   r   r-  r$  )r  r)   r'  a_shaper   r&  a_ranks          r+   r2  r2    s    t ~~dA3' 8ac*A kkmGMME	 223M'L M 	M%	"#uqyk1UQYK?dAwfq&1*a06A:vz2J
KQPd QTY7'8 8 8s   B8CC$zlinalg.diagmatrix_diagr   c           	          t        | d      r| j                  dk(  rt        |      }t        j                  | ||||||      S )a  Returns a batched diagonal tensor with given batched diagonal values.

  Returns a tensor with the contents in `diagonal` as `k[0]`-th to `k[1]`-th
  diagonals of a matrix, with everything else padded with `padding`. `num_rows`
  and `num_cols` specify the dimension of the innermost matrix of the output. If
  both are not specified, the op assumes the innermost matrix is square and
  infers its size from `k` and the innermost dimension of `diagonal`. If only
  one of them is specified, the op assumes the unspecified value is the smallest
  possible based on other criteria.

  Let `diagonal` have `r` dimensions `[I, J, ..., L, M, N]`. The output tensor
  has rank `r+1` with shape `[I, J, ..., L, M, num_rows, num_cols]` when only
  one diagonal is given (`k` is an integer or `k[0] == k[1]`). Otherwise, it has
  rank `r` with shape `[I, J, ..., L, num_rows, num_cols]`.

  The second innermost dimension of `diagonal` has double meaning. When `k` is
  scalar or `k[0] == k[1]`, `M` is part of the batch size [I, J, ..., M], and
  the output tensor is:

  ```
  output[i, j, ..., l, m, n]
    = diagonal[i, j, ..., l, n-max(d_upper, 0)] ; if n - m == d_upper
      padding_value                             ; otherwise
  ```

  Otherwise, `M` is treated as the number of diagonals for the matrix in the
  same batch (`M = k[1]-k[0]+1`), and the output tensor is:

  ```
  output[i, j, ..., l, m, n]
    = diagonal[i, j, ..., l, diag_index, index_in_diag] ; if k[0] <= d <= k[1]
      padding_value                                     ; otherwise
  ```
  where `d = n - m`, `diag_index = k[1] - d`, and
  `index_in_diag = n - max(d, 0) + offset`.

  `offset` is zero except when the alignment of the diagonal is to the right.
  ```
  offset = max_diag_len - diag_len(d) ; if (`align` in {RIGHT_LEFT, RIGHT_RIGHT}
                                             and `d >= 0`) or
                                           (`align` in {LEFT_RIGHT, RIGHT_RIGHT}
                                             and `d <= 0`)
           0                          ; otherwise
  ```
  where `diag_len(d) = min(cols - max(d, 0), rows + min(d, 0))`.

  For example:

  ```
  # The main diagonal.
  diagonal = np.array([[1, 2, 3, 4],            # Input shape: (2, 4)
                       [5, 6, 7, 8]])
  tf.linalg.diag(diagonal) ==> [[[1, 0, 0, 0],  # Output shape: (2, 4, 4)
                                 [0, 2, 0, 0],
                                 [0, 0, 3, 0],
                                 [0, 0, 0, 4]],
                                [[5, 0, 0, 0],
                                 [0, 6, 0, 0],
                                 [0, 0, 7, 0],
                                 [0, 0, 0, 8]]]

  # A superdiagonal (per batch).
  diagonal = np.array([[1, 2, 3],  # Input shape: (2, 3)
                       [4, 5, 6]])
  tf.linalg.diag(diagonal, k = 1)
    ==> [[[0, 1, 0, 0],  # Output shape: (2, 4, 4)
          [0, 0, 2, 0],
          [0, 0, 0, 3],
          [0, 0, 0, 0]],
         [[0, 4, 0, 0],
          [0, 0, 5, 0],
          [0, 0, 0, 6],
          [0, 0, 0, 0]]]

  # A tridiagonal band (per batch).
  diagonals = np.array([[[8, 9, 0],  # Input shape: (2, 2, 3)
                         [1, 2, 3],
                         [0, 4, 5]],
                        [[2, 3, 0],
                         [6, 7, 9],
                         [0, 9, 1]]])
  tf.linalg.diag(diagonals, k = (-1, 1))
    ==> [[[1, 8, 0],  # Output shape: (2, 3, 3)
          [4, 2, 9],
          [0, 5, 3]],
         [[6, 2, 0],
          [9, 7, 3],
          [0, 1, 9]]]

  # RIGHT_LEFT alignment.
  diagonals = np.array([[[0, 8, 9],  # Input shape: (2, 2, 3)
                         [1, 2, 3],
                         [4, 5, 0]],
                        [[0, 2, 3],
                         [6, 7, 9],
                         [9, 1, 0]]])
  tf.linalg.diag(diagonals, k = (-1, 1), align="RIGHT_LEFT")
    ==> [[[1, 8, 0],  # Output shape: (2, 3, 3)
          [4, 2, 9],
          [0, 5, 3]],
         [[6, 2, 0],
          [9, 7, 3],
          [0, 1, 9]]]

  # Rectangular matrix.
  diagonal = np.array([1, 2])  # Input shape: (2)
  tf.linalg.diag(diagonal, k = -1, num_rows = 3, num_cols = 4)
    ==> [[0, 0, 0, 0],  # Output shape: (3, 4)
         [1, 0, 0, 0],
         [0, 2, 0, 0]]

  # Rectangular matrix with inferred num_cols and padding_value = 9.
  tf.linalg.diag(diagonal, k = -1, num_rows = 3, padding_value = 9)
    ==> [[9, 9],  # Output shape: (3, 2)
         [1, 9],
         [9, 2]]
  ```

  Args:
    diagonal: A `Tensor` with `rank k >= 1`.
    name: A name for the operation (optional).
    k: Diagonal offset(s). Positive value means superdiagonal, 0 refers to the
      main diagonal, and negative value means subdiagonals. `k` can be a single
      integer (for a single diagonal) or a pair of integers specifying the low
      and high ends of a matrix band. `k[0]` must not be larger than `k[1]`.
    num_rows: The number of rows of the output matrix. If it is not provided,
      the op assumes the output matrix is a square matrix and infers the matrix
      size from `d_lower`, `d_upper`, and the innermost dimension of `diagonal`.
    num_cols: The number of columns of the output matrix. If it is not provided,
      the op assumes the output matrix is a square matrix and infers the matrix
      size from `d_lower`, `d_upper`, and the innermost dimension of `diagonal`.
    padding_value: The value to fill the area outside the specified diagonal
      band with. Default is 0.
    align: Some diagonals are shorter than `max_diag_len` and need to be padded.
      `align` is a string specifying how superdiagonals and subdiagonals should
      be aligned, respectively. There are four possible alignments: "RIGHT_LEFT"
      (default), "LEFT_RIGHT", "LEFT_LEFT", and "RIGHT_RIGHT". "RIGHT_LEFT"
      aligns superdiagonals to the right (left-pads the row) and subdiagonals to
      the left (right-pads the row). It is the packing format LAPACK uses.
      cuSPARSE uses "LEFT_RIGHT", which is the opposite alignment.

  Returns:
    A Tensor. Has the same type as `diagonal`.
  r   bool)diagonalknum_rowsnum_colspadding_valuealignr)   )r@   r   r:  r   matrix_diag_v3)r;  r)   r<  r=  r>  r?  r@  s          r+   r8  r8    sL    x XwHNNf$<'M		%	%	!
 r,   zlinalg.diag_partmatrix_diag_part	diag_partc                     t        | d      r| j                  dk(  rt        |      }t        j                  | ||||      S )a@  Returns the batched diagonal part of a batched tensor.

  Returns a tensor with the `k[0]`-th to `k[1]`-th diagonals of the batched
  `input`.

  Assume `input` has `r` dimensions `[I, J, ..., L, M, N]`.
  Let `max_diag_len` be the maximum length among all diagonals to be extracted,
  `max_diag_len = min(M + min(k[1], 0), N + min(-k[0], 0))`
  Let `num_diags` be the number of diagonals to extract,
  `num_diags = k[1] - k[0] + 1`.

  If `num_diags == 1`, the output tensor is of rank `r - 1` with shape
  `[I, J, ..., L, max_diag_len]` and values:

  ```
  diagonal[i, j, ..., l, n]
    = input[i, j, ..., l, n+y, n+x] ; if 0 <= n+y < M and 0 <= n+x < N,
      padding_value                 ; otherwise.
  ```
  where `y = max(-k[1], 0)`, `x = max(k[1], 0)`.

  Otherwise, the output tensor has rank `r` with dimensions
  `[I, J, ..., L, num_diags, max_diag_len]` with values:

  ```
  diagonal[i, j, ..., l, m, n]
    = input[i, j, ..., l, n+y, n+x] ; if 0 <= n+y < M and 0 <= n+x < N,
      padding_value                 ; otherwise.
  ```
  where `d = k[1] - m`, `y = max(-d, 0) - offset`, and `x = max(d, 0) - offset`.

  `offset` is zero except when the alignment of the diagonal is to the right.
  ```
  offset = max_diag_len - diag_len(d) ; if (`align` in {RIGHT_LEFT, RIGHT_RIGHT}
                                             and `d >= 0`) or
                                           (`align` in {LEFT_RIGHT, RIGHT_RIGHT}
                                             and `d <= 0`)
           0                          ; otherwise
  ```
  where `diag_len(d) = min(cols - max(d, 0), rows + min(d, 0))`.

  The input must be at least a matrix.

  For example:

  ```
  input = np.array([[[1, 2, 3, 4],  # Input shape: (2, 3, 4)
                     [5, 6, 7, 8],
                     [9, 8, 7, 6]],
                    [[5, 4, 3, 2],
                     [1, 2, 3, 4],
                     [5, 6, 7, 8]]])

  # A main diagonal from each batch.
  tf.linalg.diag_part(input) ==> [[1, 6, 7],  # Output shape: (2, 3)
                                  [5, 2, 7]]

  # A superdiagonal from each batch.
  tf.linalg.diag_part(input, k = 1)
    ==> [[2, 7, 6],  # Output shape: (2, 3)
         [4, 3, 8]]

  # A band from each batch.
  tf.linalg.diag_part(input, k = (-1, 2))
    ==> [[[3, 8, 0],  # Output shape: (2, 4, 3)
          [2, 7, 6],
          [1, 6, 7],
          [0, 5, 8]],
         [[3, 4, 0],
          [4, 3, 8],
          [5, 2, 7],
          [0, 1, 6]]]

  # RIGHT_LEFT alignment.
  tf.linalg.diag_part(input, k = (-1, 2), align="RIGHT_LEFT")
    ==> [[[0, 3, 8],  # Output shape: (2, 4, 3)
          [2, 7, 6],
          [1, 6, 7],
          [5, 8, 0]],
         [[0, 3, 4],
          [4, 3, 8],
          [5, 2, 7],
          [1, 6, 0]]]

  # max_diag_len can be shorter than the main diagonal.
  tf.linalg.diag_part(input, k = (-2, -1))
    ==> [[[5, 8],
          [0, 9]],
         [[1, 6],
          [0, 5]]]

  # padding_value = 9
  tf.linalg.diag_part(input, k = (1, 3), padding_value = 9)
    ==> [[[4, 9, 9],  # Output shape: (2, 3, 3)
          [3, 8, 9],
          [2, 7, 6]],
         [[2, 9, 9],
          [3, 4, 9],
          [4, 3, 8]]]

  ```

  Args:
    input: A `Tensor` with `rank k >= 2`.
    name: A name for the operation (optional).
    k: Diagonal offset(s). Positive value means superdiagonal, 0 refers to the
      main diagonal, and negative value means subdiagonals. `k` can be a single
      integer (for a single diagonal) or a pair of integers specifying the low
      and high ends of a matrix band. `k[0]` must not be larger than `k[1]`.
    padding_value: The value to fill the area outside the specified diagonal
      band with. Default is 0.
    align: Some diagonals are shorter than `max_diag_len` and need to be padded.
      `align` is a string specifying how superdiagonals and subdiagonals should
      be aligned, respectively. There are four possible alignments: "RIGHT_LEFT"
      (default), "LEFT_RIGHT", "LEFT_LEFT", and "RIGHT_RIGHT". "RIGHT_LEFT"
      aligns superdiagonals to the right (left-pads the row) and subdiagonals to
      the left (right-pads the row). It is the packing format LAPACK uses.
      cuSPARSE uses "LEFT_RIGHT", which is the opposite alignment.

  Returns:
    A Tensor containing diagonals of `input`. Has the same type as `input`.

  Raises:
    InvalidArgumentError: When `k` is out of bound or when `k[0]>k[1:]`.
  r   r:  )rB   r<  r?  r@  r)   )r@   r   r:  r   matrix_diag_part_v3)rB   r)   r<  r?  r@  s        r+   rB  rB    sD    P UG!6'M		*	*Qm5t
M Mr,   zlinalg.tensor_diag_partc                 0    t        j                  | |      S )a  Returns the diagonal part of the tensor.

  This operation returns a tensor with the `diagonal` part
  of the `input`. The `diagonal` part is computed as follows:

  Assume `input` has dimensions `[D1,..., Dk, D1,..., Dk]`, then the output is a
  tensor of rank `k` with dimensions `[D1,..., Dk]` where:

  `diagonal[i1,..., ik] = input[i1, ..., ik, i1,..., ik]`.

  For a rank 2 tensor, `linalg.diag_part` and `linalg.tensor_diag_part`
  produce the same result. For rank 3 and higher, linalg.diag_part extracts
  the diagonal of each inner-most matrix in the tensor. An example where
  they differ is given below.

  >>> x = [[[[1111,1112],[1121,1122]],
  ...       [[1211,1212],[1221,1222]]],
  ...      [[[2111, 2112], [2121, 2122]],
  ...       [[2211, 2212], [2221, 2222]]]
  ...      ]
  >>> tf.linalg.tensor_diag_part(x)
  <tf.Tensor: shape=(2, 2), dtype=int32, numpy=
  array([[1111, 1212],
         [2121, 2222]], dtype=int32)>
  >>> tf.linalg.diag_part(x).shape
  TensorShape([2, 2, 2])

  Args:
    input: A `Tensor` with rank `2k`.
    name: A name for the operation (optional).

  Returns:
    A Tensor containing diagonals of `input`. Has the same type as `input`, and
    rank `k`.
  r   )r   rC  r   s     r+   tensor_diag_partrG  S	  s    T 
	 	 u4	88r,   zlinalg.set_diagmatrix_set_diagc                 6    t        j                  | ||||      S )a7  Returns a batched matrix tensor with new batched diagonal values.

  Given `input` and `diagonal`, this operation returns a tensor with the
  same shape and values as `input`, except for the specified diagonals of the
  innermost matrices. These will be overwritten by the values in `diagonal`.

  `input` has `r+1` dimensions `[I, J, ..., L, M, N]`. When `k` is scalar or
  `k[0] == k[1]`, `diagonal` has `r` dimensions `[I, J, ..., L, max_diag_len]`.
  Otherwise, it has `r+1` dimensions `[I, J, ..., L, num_diags, max_diag_len]`.
  `num_diags` is the number of diagonals, `num_diags = k[1] - k[0] + 1`.
  `max_diag_len` is the longest diagonal in the range `[k[0], k[1]]`,
  `max_diag_len = min(M + min(k[1], 0), N + min(-k[0], 0))`

  The output is a tensor of rank `k+1` with dimensions `[I, J, ..., L, M, N]`.
  If `k` is scalar or `k[0] == k[1]`:

  ```
  output[i, j, ..., l, m, n]
    = diagonal[i, j, ..., l, n-max(k[1], 0)] ; if n - m == k[1]
      input[i, j, ..., l, m, n]              ; otherwise
  ```

  Otherwise,

  ```
  output[i, j, ..., l, m, n]
    = diagonal[i, j, ..., l, diag_index, index_in_diag] ; if k[0] <= d <= k[1]
      input[i, j, ..., l, m, n]                         ; otherwise
  ```
  where `d = n - m`, `diag_index = k[1] - d`, and
  `index_in_diag = n - max(d, 0) + offset`.

  `offset` is zero except when the alignment of the diagonal is to the right.
  ```
  offset = max_diag_len - diag_len(d) ; if (`align` in {RIGHT_LEFT, RIGHT_RIGHT}
                                             and `d >= 0`) or
                                           (`align` in {LEFT_RIGHT, RIGHT_RIGHT}
                                             and `d <= 0`)
           0                          ; otherwise
  ```
  where `diag_len(d) = min(cols - max(d, 0), rows + min(d, 0))`.

  For example:

  ```
  # The main diagonal.
  input = np.array([[[7, 7, 7, 7],              # Input shape: (2, 3, 4)
                     [7, 7, 7, 7],
                     [7, 7, 7, 7]],
                    [[7, 7, 7, 7],
                     [7, 7, 7, 7],
                     [7, 7, 7, 7]]])
  diagonal = np.array([[1, 2, 3],               # Diagonal shape: (2, 3)
                       [4, 5, 6]])
  tf.matrix_set_diag(input, diagonal)
    ==> [[[1, 7, 7, 7],  # Output shape: (2, 3, 4)
          [7, 2, 7, 7],
          [7, 7, 3, 7]],
         [[4, 7, 7, 7],
          [7, 5, 7, 7],
          [7, 7, 6, 7]]]

  # A superdiagonal (per batch).
  tf.matrix_set_diag(input, diagonal, k = 1)
    ==> [[[7, 1, 7, 7],  # Output shape: (2, 3, 4)
          [7, 7, 2, 7],
          [7, 7, 7, 3]],
         [[7, 4, 7, 7],
          [7, 7, 5, 7],
          [7, 7, 7, 6]]]

  # A band of diagonals.
  diagonals = np.array([[[9, 1, 0],  # Diagonal shape: (2, 4, 3)
                         [6, 5, 8],
                         [1, 2, 3],
                         [0, 4, 5]],
                        [[1, 2, 0],
                         [5, 6, 4],
                         [6, 1, 2],
                         [0, 3, 4]]])
  tf.matrix_set_diag(input, diagonals, k = (-1, 2))
    ==> [[[1, 6, 9, 7],  # Output shape: (2, 3, 4)
          [4, 2, 5, 1],
          [7, 5, 3, 8]],
         [[6, 5, 1, 7],
          [3, 1, 6, 2],
          [7, 4, 2, 4]]]

  # RIGHT_LEFT alignment.
  diagonals = np.array([[[0, 9, 1],  # Diagonal shape: (2, 4, 3)
                         [6, 5, 8],
                         [1, 2, 3],
                         [4, 5, 0]],
                        [[0, 1, 2],
                         [5, 6, 4],
                         [6, 1, 2],
                         [3, 4, 0]]])
  tf.matrix_set_diag(input, diagonals, k = (-1, 2), align="RIGHT_LEFT")
    ==> [[[1, 6, 9, 7],  # Output shape: (2, 3, 4)
          [4, 2, 5, 1],
          [7, 5, 3, 8]],
         [[6, 5, 1, 7],
          [3, 1, 6, 2],
          [7, 4, 2, 4]]]

  ```

  Args:
    input: A `Tensor` with rank `k + 1`, where `k >= 1`.
    diagonal:  A `Tensor` with rank `k`, when `d_lower == d_upper`, or `k + 1`,
      otherwise. `k >= 1`.
    name: A name for the operation (optional).
    k: Diagonal offset(s). Positive value means superdiagonal, 0 refers to the
      main diagonal, and negative value means subdiagonals. `k` can be a single
      integer (for a single diagonal) or a pair of integers specifying the low
      and high ends of a matrix band. `k[0]` must not be larger than `k[1]`.
    align: Some diagonals are shorter than `max_diag_len` and need to be padded.
      `align` is a string specifying how superdiagonals and subdiagonals should
      be aligned, respectively. There are four possible alignments: "RIGHT_LEFT"
      (default), "LEFT_RIGHT", "LEFT_LEFT", and "RIGHT_RIGHT". "RIGHT_LEFT"
      aligns superdiagonals to the right (left-pads the row) and subdiagonals to
      the left (right-pads the row). It is the packing format LAPACK uses.
      cuSPARSE uses "LEFT_RIGHT", which is the opposite alignment.
  )rB   r;  r<  r@  r)   )r   matrix_set_diag_v3)rB   r;  r)   r<  r@  s        r+   rH  rH  	  s%    J 
	)	)HT
C Cr,   c                     	 t        j                  |      dk  rt        j                  t        || |||      S 	 y # t
        t        f$ r Y y w xY w)Ni  )r(   r   r)   )r   r   r2   r3   r   NotImplementedErrorr   )r1   r(   r   r)   r/   s        r+   _constant_if_smallrM  
  s]    		wwu~##
FEe$   
 y	) 			s   6; AAc                 :      fd}t        j                   |      S )z Tags the result of function by setting _is_zeros_tensor attribute.

  This is useful to compute Hessians of fused ops such as cross_entropy.
  c                  &     | i |}d|_         |S NT)_is_zeros_tensor)argskwargsr   funs      r+   wrappedz"_tag_zeros_tensor.<locals>.wrapped
  s    $!&!F"FMr,   )r!   make_decorator)rT  rU  s   ` r+   _tag_zeros_tensorrW  
  s    
 
	$	$S'	22r,   zerosc                    t        j                  |      j                  }t        j                  |d| g      5 }|t         j
                  k(  rd}nS|t         j                  k(  rd}n=|j                  r/t        j                  g       j                  |j                        }nd}t        | t        j                        sZ	 t        j                          st#        || |||      }||cddd       S t%        j&                  t)        j*                  |             } | j9                         st;        | dg      } t=        | t?        ||      ||	      }ddd       j@                  j                  |k(  sJ |S # t,        t.        t0        j2                  f$ r( t        j4                  | t         j6                        } Y w xY w# 1 sw Y   oxY w)
aJ  Creates a tensor with all elements set to zero.

  See also `tf.zeros_like`, `tf.ones`, `tf.fill`, `tf.eye`.

  This operation returns a tensor of type `dtype` with shape `shape` and
  all elements set to zero.

  >>> tf.zeros([3, 4], tf.int32)
  <tf.Tensor: shape=(3, 4), dtype=int32, numpy=
  array([[0, 0, 0, 0],
         [0, 0, 0, 0],
         [0, 0, 0, 0]], dtype=int32)>

  Args:
    shape: A `list` of integers, a `tuple` of integers, or a 1-D `Tensor` of
      type `int32`.
    dtype: The DType of an element in the resulting `Tensor`.
    name: Optional string. A name for the operation.
    layout: Optional, `tf.experimental.dtensor.Layout`. If provided, the result
      is a [DTensor](https://www.tensorflow.org/guide/dtensor_overview) with the
      provided layout.

  Returns:
    A `Tensor` with all elements set to zero.
  rX  F r   r/   Nr   r   r)   r/   )!r   as_dtyper   r   rr   r:  stringis_quantizedr   rX  astyper   r;   
tensor_libr   r   r?   rM  r	   ry   r   r   r   rI   r   UnimplementedErrorrA   rf   r   r$   r-   r   r   )r(   r   r)   r/   zerooutputs         r+   rX  rX  &
  s   : //%
 
+
+%
~~dGeW- Pd	&--	d			XXb\  !5!56ddeZ../A((* &dE5$vN&#P P( DD$$U+-
 ebT"e%$e44OF7P8 
	 	 E	))	)	- V%>%>? A%%e6<<@A-P Ps7   BF;='E2.(E27F;2AF85F;7F88F;;G
zeros_likec                     t        | |||      S )a  Creates a tensor with all elements set to zero.

  See also `tf.zeros`.

  Given a single tensor (`tensor`), this operation returns a tensor of the
  same type and shape as `tensor` with all elements set to zero. Optionally,
  you can use `dtype` to specify a new type for the returned tensor.

  Examples:

    >>> tensor = tf.constant([[1, 2, 3], [4, 5, 6]])
    >>> tf.zeros_like(tensor)
    <tf.Tensor: shape=(2, 3), dtype=int32, numpy=
    array([[0, 0, 0],
           [0, 0, 0]], dtype=int32)>

    >>> tf.zeros_like(tensor, dtype=tf.float32)
    <tf.Tensor: shape=(2, 3), dtype=float32, numpy=
    array([[0., 0., 0.],
           [0., 0., 0.]], dtype=float32)>

  Args:
    tensor: A `Tensor`.
    dtype: A type for the returned `Tensor`. Must be `float16`, `float32`,
      `float64`, `int8`, `uint8`, `int16`, `uint16`, `int32`, `int64`,
      `complex64`, `complex128`, `bool` or `string`. (optional)
    name: A name for the operation (optional).
    optimize: if `True`, attempt to statically determine the shape of `tensor`
      and encode it as a constant. (optional, defaults to `True`)

  Returns:
    A `Tensor` with all elements set to zero.
  zeros_like_implr   r   r)   rl   s       r+   re  re  d
  s    J 
h	77r,   c                 "    t        | ||d|      S )aI  Creates a tensor with all elements set to zero.

  See also `tf.zeros`.

  Given a single tensor or array-like object (`input`), this operation returns
  a tensor of the same type and shape as `input` with all elements set to zero.
  Optionally, you can use `dtype` to specify a new type for the returned tensor.

  Note that the layout of the input tensor is not preserved if the op
  is used inside tf.function. To obtain a tensor with the same layout as the
  input, chain the returned value to a `dtensor.relayout_like`.

  Examples:

    >>> tensor = tf.constant([[1, 2, 3], [4, 5, 6]])
    >>> tf.zeros_like(tensor)
    <tf.Tensor: shape=(2, 3), dtype=int32, numpy=
    array([[0, 0, 0],
           [0, 0, 0]], dtype=int32)>

    >>> tf.zeros_like(tensor, dtype=tf.float32)
    <tf.Tensor: shape=(2, 3), dtype=float32, numpy=
    array([[0., 0., 0.],
           [0., 0., 0.]], dtype=float32)>

    >>> tf.zeros_like([[1, 2, 3], [4, 5, 6]])
    <tf.Tensor: shape=(2, 3), dtype=int32, numpy=
    array([[0, 0, 0],
           [0, 0, 0]], dtype=int32)>

  Args:
    input: A `Tensor` or array-like object.
    dtype: A type for the returned `Tensor`. Must be `float16`, `float32`,
      `float64`, `int8`, `uint8`, `int16`, `uint16`, `int32`, `int64`,
      `complex64`, `complex128`, `bool` or `string` (optional).
    name: A name for the operation (optional).
    layout: Optional, `tf.experimental.dtensor.Layout`. If provided, the result
      is a [DTensor](https://www.tensorflow.org/guide/dtensor_overview) with the
      provided layout.

  Returns:
    A `Tensor` with all elements set to zero.
  Trl   r/   rg  rB   r   r)   r/   s       r+   zeros_like_v2rm  
  s    h 
td6	JJr,   c                     t        j                  |      st        j                  |d      }|j                  }|j
                  }t        j                         r7|||k7  r | t        ||      |||      S t        j                  ||||      S |r4 |j                         r#|t        j                  k7  r | ||xs |||      S |/||k7  r*|t        j                  k7  r | t        ||      |||      S t        j                  ||||      S )z?Internal implementation for ones_like and zeros_like API calls.r   r9   r   r   r)   r/   )r   r*  r   rA   r(   r   r   r?   rm   r2   r3   rx   r   variant)	array_fnarray_like_fnr   r   r)   rl   r/   r   tensor_dtypes	            r+   array_like_implrt  
  s   
 
			'""69F,, Ul2
(
3	  !!-dKK 
',
'
'
)
&..
( E1\V  5L0Ufnn5Lv1	  
		vvD	IIr,   c           
          t        j                  |d| g      5 }t        t        t        j
                  | ||||      cddd       S # 1 sw Y   yxY w)z;Internal implementation for the v1/v2 zeros_like API calls.re  rk  N)r   rr   rt  rX  r   re  )r   r   r)   rl   r/   s        r+   rh  rh  
  sP     ~~dL6(3 	t  	 	 	   $AAr   c                     t        | |||      S )aH  Creates a tensor with all elements set to 1.

  See also `tf.ones`.

  Given a single tensor (`tensor`), this operation returns a tensor of the same
  type and shape as `tensor` with all elements set to 1. Optionally, you can
  specify a new type (`dtype`) for the returned tensor.

  For example:

  ```python
  tensor = tf.constant([[1, 2, 3], [4, 5, 6]])
  tf.ones_like(tensor)  # [[1, 1, 1], [1, 1, 1]]
  ```

  Args:
    tensor: A `Tensor`.
    dtype: A type for the returned `Tensor`. Must be `float32`, `float64`,
      `int8`, `uint8`, `int16`, `uint16`, `int32`, `int64`, `complex64`,
      `complex128` or `bool`.
    name: A name for the operation (optional).
    optimize: if true, attempt to statically determine the shape of 'tensor' and
      encode it as a constant.

  Returns:
    A `Tensor` with all elements set to 1.
  )ones_like_implri  s       r+   r   r   
  s    > 
tX	66r,   c           
          t        j                  |d| g      5 }t        t        t        j
                  | ||d|      cddd       S # 1 sw Y   yxY w)a  Creates a tensor of all ones that has the same shape as the input.

  See also `tf.ones`.

  Given a single tensor (`tensor`), this operation returns a tensor of the
  same type and shape as `tensor` with all elements set to 1. Optionally,
  you can use `dtype` to specify a new type for the returned tensor.

  For example:

  >>> tensor = tf.constant([[1, 2, 3], [4, 5, 6]])
  >>> tf.ones_like(tensor)
  <tf.Tensor: shape=(2, 3), dtype=int32, numpy=
    array([[1, 1, 1],
           [1, 1, 1]], dtype=int32)>

  Note that the layout of the input tensor is not preserved if the op
  is used inside tf.function. To obtain a tensor with the same layout as the
  input, chain the returned value to a `dtensor.relayout_like`.

  Args:
    input: A `Tensor`.
    dtype: A type for the returned `Tensor`. Must be `float16`, `float32`,
      `float64`, `int8`, `uint8`, `int16`, `uint16`, `int32`, `int64`,
      `complex64`, `complex128`, `bool` or `string`.
    name: A name for the operation (optional).
    layout: Optional, `tf.experimental.dtensor.Layout`. If provided, the result
      is a [DTensor](https://www.tensorflow.org/guide/dtensor_overview) with the
      provided layout.

  Returns:
    A `Tensor` with all elements set to one.
  r   Trk  N)r   rr   rt  onesr   r   rl  s       r+   ones_like_v2r{     sQ    T ~~dK%1 	T	 	 	rv  c                 J   t        j                  |d| g      5 }t        j                  | d      } t        | |      }|| j                  }t        ||||      }t        j                         s|j                  | j                                |cddd       S # 1 sw Y   yxY w)z:Internal implementation for the v1/v2 ones_like API calls.r   r   r9   r   Nro  )
r   rr   rA   rm   r   rz  r   r?   r  rw   )r   r   r)   rl   r/   
ones_shaperC   s          r+   rx  rx  V  s    
~~dK&2 	d""69F:J}lle
zT&
AC$$&	mmF$$&'	 	 	s   A6BB"rz  c                 D   t        j                  |      j                  }t        j                  |d| g      5 }|t         j
                  k(  rd}n=|j                  r/t        j                  g       j                  |j                        }nd}t        | t        j                        sZ	 t        j                         st!        || |||      }||cddd       S t#        j$                  t'        j(                  |             } | j3                         st5        | dg      } t7        | t9        ||      ||      }ddd       j:                  j                  |k(  sJ |S # t*        t,        f$ r( t        j.                  | t         j0                        } Y w xY w# 1 sw Y   `xY w)	ar  Creates a tensor with all elements set to one (1).

  See also `tf.ones_like`, `tf.zeros`, `tf.fill`, `tf.eye`.

  This operation returns a tensor of type `dtype` with shape `shape` and
  all elements set to one.

  >>> tf.ones([3, 4], tf.int32)
  <tf.Tensor: shape=(3, 4), dtype=int32, numpy=
  array([[1, 1, 1, 1],
         [1, 1, 1, 1],
         [1, 1, 1, 1]], dtype=int32)>

  Args:
    shape: A `list` of integers, a `tuple` of integers, or a 1-D `Tensor` of
      type `int32`.
    dtype: Optional DType of an element in the resulting `Tensor`. Default is
      `tf.float32`.
    name: Optional string. A name for the operation.
    layout: Optional, `tf.experimental.dtensor.Layout`. If provided, the result
      is a [DTensor](https://www.tensorflow.org/guide/dtensor_overview) with the
      provided layout.

  Returns:
    A `Tensor` with all elements set to one (1).
  rz  Tr   r[  Nr   r   r\  )r   r]  r   r   rr   r:  r_  r   rz  r`  r   r;   ra  r   r   r?   rM  r	   ry   r   r   r   rI   rA   rf   r   r$   r-   r   r   )r(   r   r)   r/   onerd  s         r+   rz  rz  d  sm   : //%
 
+
+%
~~dFUG, Oc			GGBKu334cceZ../A((* &c5%fM&O O" DD$$U+-
 ebT"e%#U3$vNF1O2 
	 	 E	))	)	- $ A%%e6<<@A'O Os6   A.F''E(E 7F4FFFFFplaceholderc                 p    t        j                         rt        d      t        j                  | ||      S )a  Inserts a placeholder for a tensor that will be always fed.

  **Important**: This tensor will produce an error if evaluated. Its value must
  be fed using the `feed_dict` optional argument to `Session.run()`,
  `Tensor.eval()`, or `Operation.run()`.

  For example:

  ```python
  x = tf.compat.v1.placeholder(tf.float32, shape=(1024, 1024))
  y = tf.matmul(x, x)

  with tf.compat.v1.Session() as sess:
    print(sess.run(y))  # ERROR: will fail because x was not fed.

    rand_array = np.random.rand(1024, 1024)
    print(sess.run(y, feed_dict={x: rand_array}))  # Will succeed.
  ```

  Args:
    dtype: The type of elements in the tensor to be fed.
    shape: The shape of the tensor to be fed (optional). If the shape is not
      specified, you can feed a tensor of any shape.
    name: A name for the operation (optional).

  Returns:
    A `Tensor` that may be used as a handle for feeding a value, but not
    evaluated directly.

  Raises:
    RuntimeError: if eager execution is enabled

  @compatibility(TF2)
  This API is not compatible with eager execution and `tf.function`. To migrate
  to TF2, rewrite the code to be compatible with eager execution. Check the
  [migration
  guide](https://www.tensorflow.org/guide/migrate#1_replace_v1sessionrun_calls)
  on replacing `Session.run` calls. In TF2, you can just pass tensors directly
  into ops and layers. If you want to explicitly set up your inputs, also see
  [Keras functional API](https://www.tensorflow.org/guide/keras/functional) on
  how to use `tf.keras.Input` to replace `tf.compat.v1.placeholder`.
  `tf.function` arguments also do the job of `tf.compat.v1.placeholder`.
  For more details please read [Better
  performance with tf.function](https://www.tensorflow.org/guide/function).
  @end_compatibility
  z8tf.placeholder() is not compatible with eager execution.r   r(   r)   )r   r?   r   r   r  r  s      r+   r  r    s:    `  
 * + + 
	"	"e$	GGr,   placeholder_with_defaultc                 0    t        j                  | ||      S )aA  A placeholder op that passes through `input` when its output is not fed.

  @compatibility(TF2)
  This API is strongly discouraged for use with eager execution and
  `tf.function`. The primary use of this API is for testing computation wrapped
  within a `tf.function` where the input tensors might not have statically known
  fully-defined shapes. The same can be achieved by creating a
  [concrete function](
  https://www.tensorflow.org/guide/function#obtaining_concrete_functions)
  from the `tf.function` with a `tf.TensorSpec` input which has partially
  defined shapes. For example, the code

  >>> @tf.function
  ... def f():
  ...   x = tf.compat.v1.placeholder_with_default(
  ...       tf.constant([[1., 2., 3.], [4., 5., 6.]]), [None, 3])
  ...   y = tf.constant([[1.],[2.], [3.]])
  ...   z = tf.matmul(x, y)
  ...   assert z.shape[0] == None
  ...   assert z.shape[1] == 1

  >>> f()

  can easily be replaced by

  >>> @tf.function
  ... def f(x):
  ...   y = tf.constant([[1.],[2.], [3.]])
  ...   z = tf.matmul(x, y)
  ...   assert z.shape[0] == None
  ...   assert z.shape[1] == 1

  >>> g = f.get_concrete_function(tf.TensorSpec([None, 3]))

  You can learn more about `tf.function` at [Better
  performance with tf.function](https://www.tensorflow.org/guide/function).
  @end_compatibility

  Args:
    input: A `Tensor`. The default value to produce when output is not fed.
    shape: A `tf.TensorShape` or list of `int`s. The (possibly partial) shape of
      the tensor.
    name: A name for the operation (optional).

  Returns:
    A `Tensor`. Has the same type as `input`.
  )r   r  )rB   r(   r)   s      r+   r  r    s    b 
	/	/ud	CCr,   zsparse.placeholdersparse_placeholderc           	      @   t        j                         rt        d      ||dz   nd}||dz   nd}|5d}t        t        j
                  |g|      }t        j                  |      }nt        |t        j                        r)|j                         d   }t        j                  |      }nzt        |      }t        j                  t        d |D                    }t        d |D              }t        d	 |D              }t!        j"                  |t        j
                  |
      }t%        ||j&                  |      }t)        j*                  t        | dg||dz   nd      t        t        j
                  d|g||dz   nd      |      }|j-                  |       |S )a	  Inserts a placeholder for a sparse tensor that will be always fed.

  **Important**: This sparse tensor will produce an error if evaluated.
  Its value must be fed using the `feed_dict` optional argument to
  `Session.run()`, `Tensor.eval()`, or `Operation.run()`.

  For example:

  ```python
  x = tf.compat.v1.sparse.placeholder(tf.float32)
  y = tf.sparse.reduce_sum(x)

  with tf.compat.v1.Session() as sess:
    print(sess.run(y))  # ERROR: will fail because x was not fed.

    indices = np.array([[3, 2, 0], [4, 5, 1]], dtype=np.int64)
    values = np.array([1.0, 2.0], dtype=np.float32)
    shape = np.array([7, 9, 2], dtype=np.int64)
    print(sess.run(y, feed_dict={
      x: tf.compat.v1.SparseTensorValue(indices, values, shape)}))  # Will
      succeed.
    print(sess.run(y, feed_dict={
      x: (indices, values, shape)}))  # Will succeed.

    sp = tf.sparse.SparseTensor(indices=indices, values=values,
                                dense_shape=shape)
    sp_value = sp.eval(session=sess)
    print(sess.run(y, feed_dict={x: sp_value}))  # Will succeed.
  ```


  Args:
    dtype: The type of `values` elements in the tensor to be fed.
    shape: The shape of the tensor to be fed (optional). If the shape is not
      specified, you can feed a sparse tensor of any shape.
    name: A name for prefixing the operations (optional).

  Returns:
    A `SparseTensor` that may be used as a handle for feeding a value, but not
    evaluated directly.

  Raises:
    RuntimeError: if eager execution is enabled

  @compatibility(TF2)
  This API is not compatible with eager execution and `tf.function`. To migrate
  to TF2, rewrite the code to be compatible with eager execution. Check the
  [migration
  guide](https://www.tensorflow.org/guide/migrate#1_replace_v1sessionrun_calls)
  on replacing `Session.run` calls. In TF2, you can just pass tensors directly
  into ops and layers. If you want to explicitly set up your inputs, also see
  [Keras functional API](https://www.tensorflow.org/guide/keras/functional) on
  how to use `tf.keras.Input` to replace `tf.compat.v1.sparse_placeholder`.
  `tf.function` arguments also do the job of `tf.compat.v1.sparse_placeholder`.
  For more details please read [Better
  performance with tf.function](https://www.tensorflow.org/guide/function).
  @end_compatibility
  z<`sparse_placeholder` is not compatible with eager execution.Nz/shapez/shape_default)r(   r)   r   c              3   .   K   | ]  }|d k(  rdn|  yw)r   Nr   r   s     r+   r   z%sparse_placeholder.<locals>.<genexpr>Z  s     <sr	s*<s   c              3   F   K   | ]  }t        j                  |        y wrN   )r   dimension_valuer   s     r+   r   z%sparse_placeholder.<locals>.<genexpr>[  s     G#L005Gs   !c              3   (   K   | ]
  }|dn|  y w)Nr   r   r   s     r+   r   z%sparse_placeholder.<locals>.<genexpr>\  s     @3#+B3.@s   r   z/valuesz/indices)r   r   rv   )r   r?   r   r  r   re   r   constant_value_as_shaper;   ra  r   rw   r   r   r   r   r   rA   r  r(   r   rs   r  )	r   r(   r)   
shape_namedefault_shape_namer   rv   dense_shape_defaultr*   s	            r+   r  r  
  s   z  
 * + + %)$4x$*484D//$
]Dfll4&zJK%==kJ%**+__q!d'??FZd )44
<e<
<>GGGe@%@@e##
v||*<>e +U[[z3K %%
%)%5y 4A 
,,t&*&6z!DB 	& 	&'	-r,   padc                      t        | ||||      S )a`  Pads a tensor.

  This operation pads a `tensor` according to the `paddings` you specify.
  `paddings` is an integer tensor with shape `[n, 2]`, where n is the rank of
  `tensor`. For each dimension D of `input`, `paddings[D, 0]` indicates how
  many values to add before the contents of `tensor` in that dimension, and
  `paddings[D, 1]` indicates how many values to add after the contents of
  `tensor` in that dimension. If `mode` is "REFLECT" then both `paddings[D, 0]`
  and `paddings[D, 1]` must be no greater than `tensor.dim_size(D) - 1`. If
  `mode` is "SYMMETRIC" then both `paddings[D, 0]` and `paddings[D, 1]` must be
  no greater than `tensor.dim_size(D)`.

  The padded size of each dimension D of the output is:

  `paddings[D, 0] + tensor.dim_size(D) + paddings[D, 1]`

  For example:

  ```python
  t = tf.constant([[1, 2, 3], [4, 5, 6]])
  paddings = tf.constant([[1, 1,], [2, 2]])
  # 'constant_values' is 0.
  # rank of 't' is 2.
  tf.pad(t, paddings, "CONSTANT")  # [[0, 0, 0, 0, 0, 0, 0],
                                   #  [0, 0, 1, 2, 3, 0, 0],
                                   #  [0, 0, 4, 5, 6, 0, 0],
                                   #  [0, 0, 0, 0, 0, 0, 0]]

  tf.pad(t, paddings, "REFLECT")  # [[6, 5, 4, 5, 6, 5, 4],
                                  #  [3, 2, 1, 2, 3, 2, 1],
                                  #  [6, 5, 4, 5, 6, 5, 4],
                                  #  [3, 2, 1, 2, 3, 2, 1]]

  tf.pad(t, paddings, "SYMMETRIC")  # [[2, 1, 1, 2, 3, 3, 2],
                                    #  [2, 1, 1, 2, 3, 3, 2],
                                    #  [5, 4, 4, 5, 6, 6, 5],
                                    #  [5, 4, 4, 5, 6, 6, 5]]
  ```

  Args:
    tensor: A `Tensor`.
    paddings: A `Tensor` of type `int32`.
    mode: One of "CONSTANT", "REFLECT", or "SYMMETRIC" (case-insensitive)
    constant_values: In "CONSTANT" mode, the scalar pad value to use. Must be
      same type as `tensor`.
    name: A name for the operation (optional).

  Returns:
    A `Tensor`. Has the same type as `tensor`.

  Raises:
    ValueError: When mode is not one of "CONSTANT", "REFLECT", or "SYMMETRIC".
  )r  )r   paddingsmodeconstant_valuesr)   s        r+   pad_v2r  |  s    p 
VXtT?	;;r,   c                    |j                         }|dk(  rxt        j                  |      sIt        j                  |      dk(  r1|t        j
                  |      k(  rt        j                  | ||      }nft        j                  | |||      }nL|dk(  rt        j                  | |d|      }n-|dk(  rt        j                  | |d|      }nt        d|       t        j                         s	t        |      }t        | t        j                         rt#        j$                  | j&                        n"|j(                  j*                  d   j&                  }|j,                  |j&                  j/                         s||zg }t1        ||j3                               D ]J  \  }	}
|	|
t5        d	 |	D              r|j7                  d       .|j7                  t9        |	      |
z          L |j;                  |       |S )
a`  Pads a tensor.

  This operation pads a `tensor` according to the `paddings` you specify.
  `paddings` is an integer tensor with shape `[n, 2]`, where n is the rank of
  `tensor`. For each dimension D of `input`, `paddings[D, 0]` indicates how
  many values to add before the contents of `tensor` in that dimension, and
  `paddings[D, 1]` indicates how many values to add after the contents of
  `tensor` in that dimension. If `mode` is "REFLECT" then both `paddings[D, 0]`
  and `paddings[D, 1]` must be no greater than `tensor.dim_size(D) - 1`. If
  `mode` is "SYMMETRIC" then both `paddings[D, 0]` and `paddings[D, 1]` must be
  no greater than `tensor.dim_size(D)`.

  The padded size of each dimension D of the output is:

  `paddings[D, 0] + tensor.dim_size(D) + paddings[D, 1]`

  For example:

  ```python
  t = tf.constant([[1, 2, 3], [4, 5, 6]])
  paddings = tf.constant([[1, 1,], [2, 2]])
  # 'constant_values' is 0.
  # rank of 't' is 2.
  tf.pad(t, paddings, "CONSTANT")  # [[0, 0, 0, 0, 0, 0, 0],
                                   #  [0, 0, 1, 2, 3, 0, 0],
                                   #  [0, 0, 4, 5, 6, 0, 0],
                                   #  [0, 0, 0, 0, 0, 0, 0]]

  tf.pad(t, paddings, "REFLECT")  # [[6, 5, 4, 5, 6, 5, 4],
                                  #  [3, 2, 1, 2, 3, 2, 1],
                                  #  [6, 5, 4, 5, 6, 5, 4],
                                  #  [3, 2, 1, 2, 3, 2, 1]]

  tf.pad(t, paddings, "SYMMETRIC")  # [[2, 1, 1, 2, 3, 3, 2],
                                    #  [2, 1, 1, 2, 3, 3, 2],
                                    #  [5, 4, 4, 5, 6, 6, 5],
                                    #  [5, 4, 4, 5, 6, 6, 5]]
  ```

  Args:
    tensor: A `Tensor`.
    paddings: A `Tensor` of type `int32`.
    mode: One of "CONSTANT", "REFLECT", or "SYMMETRIC" (case-insensitive)
    name: A name for the operation (optional).
    constant_values: In "CONSTANT" mode, the scalar pad value to use. Must be
      same type as `tensor`.

  Returns:
    A `Tensor`. Has the same type as `tensor`.

  Raises:
    ValueError: When mode is not one of "CONSTANT", "REFLECT", or "SYMMETRIC".
  CONSTANTr   r9   REFLECT)r  r)   	SYMMETRICzhValue of argument `mode` expected to be one of "CONSTANT", "REFLECT", or "SYMMETRIC". Received `mode` = Nc              3   $   K   | ]  }|d u  
 y wrN   r   )r   rQ   s     r+   r   zpad.<locals>.<genexpr>  s     1M!t)1Ms   )upperr   r*  r   ndimre  r   r  r  
mirror_padrI   r   r?   _get_paddings_constantr;   ra  r   r   r   r(   r   inputsr   rx   ziprz   r   r   sumr  )r   r  r  r)   r  r*   paddings_constantr{   	new_shapepaddingrE   s              r+   r  r    s   v 
$	Z ""?3
 A%2==99  =f##
(O$8fy%%yt5F{%%{7F  **.1 2 2
 
	"	"	$.x8 fj//0 	  .6<ii6F6Fq6I6O6O  	%LL))+0A0Mi/1D1D1FG /,'3?ckS1MW1M-N


4
 


3w<#-
.	/
 y!	-r,   c                     t        | t        j                        rt        j                  | d      S t        | t
        t        f      r| D cg c]  }t        |       c}S | S c c}w )a  Helper to get the constant values of the paddings arg to pad().

  Used under V1 graph mode to facilitate computation of the shape of the output
  tensor of `pad()`.

  Args:
    paddings: The same paddings arg as passed to pad(). Can be a Tensor, or
      a nested list or tuple of Tensor and/or numbers.

  Returns:
    A nested list or numbers or `None`, in which `None` indicates unknown
    padding size.
  T)partial)r;   ra  r   r   r   r   r   r  )r  rQ   s     r+   r  r    sV     *++,%%h==(T5M*/78!"1%88O 9s   A#meshgridc                     |j                  dd      }|j                  dd      }|r6t        |j                               d   }t        dj	                  |            |dvrt        d| d	      t        j                  |d|       5 }t        |       }d
|z  }|sg cddd       S g }t        |       D ]B  \  }}	|j                  t        t        j                  |	      |d| dz   ||dz   d z                D | D 	cg c]  }	t        |	       }
}	t        j                  | d         j                   j"                  }|dk(  rL|dkD  rGt        |d   dd
|dz
  z  z         |d<   t        |d   dd
|dz
  z  z         |d<   |
d   |
d   c|
d<   |
d<   t%        |
|      }|D 	cg c]  }	|	|z  	 c}	cddd       S c c}	w c c}	w # 1 sw Y   yxY w)a1  Broadcasts parameters for evaluation on an N-D grid.

  Given N one-dimensional coordinate arrays `*args`, returns a list `outputs`
  of N-D coordinate arrays for evaluating expressions on an N-D grid.

  Notes:

  `meshgrid` supports cartesian ('xy') and matrix ('ij') indexing conventions.
  When the `indexing` argument is set to 'xy' (the default), the broadcasting
  instructions for the first two dimensions are swapped.

  Examples:

  Calling `X, Y = meshgrid(x, y)` with the tensors

  ```python
  x = [1, 2, 3]
  y = [4, 5, 6]
  X, Y = tf.meshgrid(x, y)
  # X = [[1, 2, 3],
  #      [1, 2, 3],
  #      [1, 2, 3]]
  # Y = [[4, 4, 4],
  #      [5, 5, 5],
  #      [6, 6, 6]]
  ```

  Args:
    *args: `Tensor`s with rank 1.
    **kwargs:
      - indexing: Either 'xy' or 'ij' (optional, default: 'xy').
      - name: A name for the operation (optional).

  Returns:
    outputs: A list of N `Tensor`s with rank N.

  Raises:
    TypeError: When no keyword arguments (kwargs) are passed.
    ValueError: When indexing keyword argument is not one of `xy` or `ij`.
  indexingxyr)   r  r   z5'{}' is an invalid keyword argument for this function)r  ijz@Argument `indexing` parameter must be either 'xy' or 'ij', got ''r   Nr   r   )r   r   r4  )r   r   )popr   keysr   formatrI   r   rr   r   r   r   r$   r   stackr~   rA   r   r   rz  )rR  rS  r  r)   keyr  s0rd  r   rQ   shapesoutput_dtype	mult_facts                r+   r  r  1  s   X ZZ
D)(	FJ	'$
v{{}
a
 C
 ((.s5 5 \!
 ++3*A7 8 8 ~~dJ- +t9D	B+ + F$ M1mm
/''*RVe^bQk-I
KMM  $$!d1g$F$((a177BBL4D1H&)Wttax/@%@AfQi&)Wttax/@%@AfQi#AYq	fQi V\*I#)*aA	M*1+ + % +1+ +s2   F>%AF>;F4BF>F9(F>4
F>>Gc                 r  	
 d}d
| ry|u sj                   |u r|S j                   |j                  		|ur{	dk(  r|S |j                  		dkD  rdgnddz
  g	
fd} ||j                  d      } ||j                  d      }||z
  }|dk(  s|dk  	dk  k7  ry|	z  dk7  rdnd}|	z  |z   S |S )z6Computes the size of a single strided slice dimension.Nr   r   r   c                     | u rdkD  r|   S |dz   dz     S | dk  r| z   n| }t        d   t        d   |            S )Nr   r   )maxmin)rQ   cx_fwdr~   strideuse_full_rangevalid_ranges      r+   	canonicalz/_compute_size_of_strided_dim.<locals>.canonical  sZ    	
n	!'!{1~Ia!eq[1IIEqq;q>3{1~u#=>>r,   )r1   stepstartstop)shrinkspecr~   unknownr  r   r   interval_length	remainderr  r  r  s     `      @@@r+   _compute_size_of_strided_dimr    s     '. 	W_

g-N	$99&7{nYYF%z1d)D1H~K? djj!$E
DIIq
!CEkO!1!4&1* E&/14!!i&22Nr,   r   c                    | j                   d   j                         j                  d      }| j                   d   j                         j                  |d         }t        j                  | j                   d         j                  |j
                        }|j
                  t        j                         gS g }t        |j                  |j                        D ]  \  }}|j                  ||z          t        j                  |      gS )z#Shape function for the TileGrad op.r   r   )r  rw   	with_rankr   r  r   r   unknown_shaper  r0   r   r   )r   multiples_shaper{   	multiplesoutput_dimsrE   multiples          r+   _TileGradShaper    s    IIaL**,66q9/		!&&(22?13EF+ 11"))A,?II)__&&())K[--y~~> *X)*$$[122r,   edit_distancec           
         t        | t        j                  t        j                  f      st	        d      t        |t        j                  t        j                  f      st	        d      t        j                  | j                  | j                  | j                  |j                  |j                  |j                  ||      S )a	  Computes the Levenshtein distance between sequences.

  This operation takes variable-length sequences (`hypothesis` and `truth`),
  each provided as a `SparseTensor`, and computes the Levenshtein distance.
  You can normalize the edit distance by length of `truth` by setting
  `normalize` to true.

  For example:

  Given the following input,
  * `hypothesis` is a `tf.SparseTensor` of shape `[2, 1, 1]`
  * `truth` is a `tf.SparseTensor` of shape `[2, 2, 2]`

  >>> hypothesis = tf.SparseTensor(
  ...   [[0, 0, 0],
  ...    [1, 0, 0]],
  ...   ["a", "b"],
  ...   (2, 1, 1))
  >>> truth = tf.SparseTensor(
  ...   [[0, 1, 0],
  ...    [1, 0, 0],
  ...    [1, 0, 1],
  ...    [1, 1, 0]],
  ...    ["a", "b", "c", "a"],
  ...    (2, 2, 2))
  >>> tf.edit_distance(hypothesis, truth, normalize=True)
  <tf.Tensor: shape=(2, 2), dtype=float32, numpy=
  array([[inf, 1. ],
         [0.5, 1. ]], dtype=float32)>

  The operation returns a dense Tensor of shape `[2, 2]` with
  edit distances normalized by `truth` lengths.

  **Note**: It is possible to calculate edit distance between two
  sparse tensors with variable-length values. However, attempting to create
  them while eager execution is enabled will result in a `ValueError`.

  For the following  inputs,

  ```python
  # 'hypothesis' is a tensor of shape `[2, 1]` with variable-length values:
  #   (0,0) = ["a"]
  #   (1,0) = ["b"]
  hypothesis = tf.sparse.SparseTensor(
      [[0, 0, 0],
       [1, 0, 0]],
      ["a", "b"],
      (2, 1, 1))

  # 'truth' is a tensor of shape `[2, 2]` with variable-length values:
  #   (0,0) = []
  #   (0,1) = ["a"]
  #   (1,0) = ["b", "c"]
  #   (1,1) = ["a"]
  truth = tf.sparse.SparseTensor(
      [[0, 1, 0],
       [1, 0, 0],
       [1, 0, 1],
       [1, 1, 0]],
      ["a", "b", "c", "a"],
      (2, 2, 2))

  normalize = True

  # The output would be a dense Tensor of shape `(2,)`, with edit distances
  normalized by 'truth' lengths.
  # output => array([0., 0.5], dtype=float32)
  ```

  Args:
    hypothesis: A `SparseTensor` containing hypothesis sequences.
    truth: A `SparseTensor` containing truth sequences.
    normalize: A `bool`. If `True`, normalizes the Levenshtein distance by
      length of `truth.`
    name: A name for the operation (optional).

  Returns:
    A dense `Tensor` with rank `R - 1`, where R is the rank of the
    `SparseTensor` inputs `hypothesis` and `truth`.

  Raises:
    TypeError: If either `hypothesis` or `truth` are not a `SparseTensor`.
  z"Hypothesis must be a SparseTensor.zTruth must be a SparseTensor.)	normalizer)   )
r;   r   rs   rt   r   r   r  r   r   rv   )
hypothesistruthr  r)   s       r+   r  r    s    l 
!!=#B#BC
E 8
99	m((-*I*IJ
L
3
44		$	$mmll
 r,   FakeQuantWithMinMaxArgsc           
          t        || j                  d   | j                  d      | j                  d      | j                  d      | j                  d            S )z(Gradient for FakeQuantWithMinMaxArgs op.r   r  r  num_bitsnarrow_range)r  r  r  r  )%fake_quant_with_min_max_args_gradientr  get_attrr   grads     r+    _FakeQuantWithMinMaxArgsGradientr  ,  sO     
/
iil
++e

++e
{{:&;;~.
0 0r,   FakeQuantWithMinMaxVarsc           
          t        || j                  d   | j                  d   | j                  d   | j                  d      | j                  d            S )z(Gradient for FakeQuantWithMinMaxVars op.r   r   r4  r  r  r  r  )%fake_quant_with_min_max_vars_gradientr  r  r  s     r+    _FakeQuantWithMinMaxVarsGradientr  8  sM     
/
iiliiliil{{:&;;~.
0 0r,   !FakeQuantWithMinMaxVarsPerChannelc           
          t        || j                  d   | j                  d   | j                  d   | j                  d      | j                  d            S )z2Gradient for FakeQuantWithMinMaxVarsPerChannel op.r   r   r4  r  r  r  )1fake_quant_with_min_max_vars_per_channel_gradientr  r  r  s     r+   *_FakeQuantWithMinMaxVarsPerChannelGradientr  D  sM     
;
iiliiliil{{:&;;~.
0 0r,   QuantizeAndDequantizeV4c           	          t        || j                  d   | j                  d   | j                  d   | j                  d            S )z(Gradient for QuantizeAndDequantizeV4 op.r   r   r4  rG   r   )quantize_and_dequantize_v4_gradr  r  r  s     r+   _QuantizeAndDequantizeV4Gradr  P  sA     
)
iiliiliil;;v
   r,   QuantizeAndDequantizeV4Gradc                     t        | |      S )z,Gradient for QuantizeAndDequantizeV4Grad op.)r  r  s     r+    _QuantizeAndDequantizeV4GradGradr  [  s     
&b$	//r,    required_space_to_batch_paddingsc           	      0   t        j                  |d| |g      5  t        j                  | t        j                  d      } t        j                  |t        j                  d      }|j                         j                          |j                         j                  d       |j                         j                  d   j                  }|dk(  rAt        ddgt        j                        t        ddgt        j                        fcddd       S | j                         j                  |g       |Ht        j                  |t        j                  d	      }|j                         j                  |dg       nt        |dgt        j                        }t        j                  |      }t        j                  |       }t        j                  |      }|
|||}|} |}|dddf   }|dddf   }	| |z   |	z   }
||
|z  z
  |z  }|	|z   }t        j                  t!        |      D cg c]  }||   ||   g c}d
      }t        j                  t!        |      D cg c]	  }d||   g c}d      }||fcddd       S c c}w c c}w # 1 sw Y   yxY w)a  Calculate padding required to make block_shape divide input_shape.

  This function can be used to calculate a suitable paddings argument for use
  with space_to_batch_nd and batch_to_space_nd.

  Args:
    input_shape: int32 Tensor of shape [N].
    block_shape: int32 Tensor of shape [N].
    base_paddings: Optional int32 Tensor of shape [N, 2].  Specifies the minimum
      amount of padding to use.  All elements must be >= 0.  If not specified,
      defaults to 0.
    name: string.  Optional name prefix.

  Returns:
    (paddings, crops), where:

    `paddings` and `crops` are int32 Tensors of rank 2 and shape [N, 2]
    satisfying:

        paddings[i, 0] = base_paddings[i, 0].
        0 <= paddings[i, 1] - base_paddings[i, 1] < block_shape[i]
        (input_shape[i] + paddings[i, 0] + paddings[i, 1]) % block_shape[i] == 0

        crops[i, 0] = 0
        crops[i, 1] = paddings[i, 1] - base_paddings[i, 1]

  Raises: ValueError if called with incompatible shapes.
  r  r{   r   block_shaper   r   r4  Nbase_paddingsr  r9   crops)r   rr   rA   r   rf   rw   assert_is_fully_definedr   r0   r1   rX  r   r   r   r   r  r5  )r{   r  r  r)   num_block_dimsconst_block_shapeconst_input_shapeconst_base_paddings	pad_startorig_pad_endfull_input_shapepad_end_extrapad_endr   result_paddingsresult_cropss                   r+   r  r  a  s~   B ~~d>"K02 +)''6<<m=K''6<<m=K 335++A. **,11!4::NAq66<<(%A*EE+) +) 55~6FG ++
v||/Cm99>1:MN^Q/>m#22;?#22;?%44]C%*;*G'%k%k)m ad#I A&L"Y.= #3k#AA[PM]*G%++-2>-BC)A,
	#CO #(((-n(=>1!]1	>WNLL(W+) +)N 	D 	?U+) +)s1   C-JDJ(J
9%JJ
,J
JJznn.space_to_batchspace_to_batchc                     t        j                  d|d|      }t        | |t        j                  ||gt        j
                        |      }|j                  |j                         j                  d             |S )Nr  
block_sizer   )r  r  r)      )	r   rH   space_to_batch_ndr   arrayre   r  rw   r  )rB   r  r  r)   r  r*   s         r+   r  r    st     55m6A<6@B* ((J
3288D	&
 	6##%//23	-r,   c                     t        | |||      S rN   )r  )rB   r  r  r)   s       r+   space_to_batch_v2r	    s     
5+x	>>r,   znn.space_to_depthspace_to_depthc                 4    t        j                  | |||      S Nr9   r   r
  rB   r  r)   data_formats       r+   r
  r
         
	%	%eZ4	PPr,   c                 4    t        j                  | |||      S r  r  rB   r  r  r)   s       r+   space_to_depth_v2r         
	%	%eZ4	PPr,   znn.depth_to_spacedepth_to_spacec                 4    t        j                  | |||      S r  r   r  r  s       r+   r  r    r  r,   c                 4    t        j                  | |||      S r  r  r  s       r+   depth_to_space_v2r    r  r,   batch_to_spacec                     t        j                  d|d|      }t        | |t        j                  ||gt        j
                        |      }|j                  |j                         j                  d             |S )Nr  r  r   )r  r  r)   r  )	r   rH   batch_to_space_ndr   r  re   r  rw   r  )rB   r  r  r)   r  r*   s         r+   r  r    st     55m6A<6@B* ((J
3288D	&
 	6##%//23	-r,   c                     t        |t              r't        j                  ||gt        j                        }t        | |||      S )am  BatchToSpace for N-D tensors of type T.

  This operation reshapes the "batch" dimension 0 into `M + 1` dimensions of
  shape `block_shape + [batch]`, interleaves these blocks back into the grid
  defined by the spatial dimensions `[1, ..., M]`, to obtain a result with the
  same rank as the input.  The spatial dimensions of this intermediate result
  are then optionally cropped according to `crops` to produce the output.  This
  is the reverse of SpaceToBatch (see `tf.space_to_batch`).

  Args:
    input: A N-D `Tensor` with shape `input_shape = [batch] + spatial_shape +
      remaining_shape`, where `spatial_shape` has M dimensions.
    block_shape: A 1-D `Tensor` with shape [M]. Must be one of the following
      types: `int32`, `int64`. All values must be >= 1. For backwards
      compatibility with TF 1.0, this parameter may be an int, in which case it
      is converted to
      `numpy.array([block_shape, block_shape],
      dtype=numpy.int64)`.
    crops: A  2-D `Tensor` with shape `[M, 2]`. Must be one of the
      following types: `int32`, `int64`. All values must be >= 0.
      `crops[i] = [crop_start, crop_end]` specifies the amount to crop from
      input dimension `i + 1`, which corresponds to spatial dimension `i`.
      It is required that
      `crop_start[i] + crop_end[i] <= block_shape[i] * input_shape[i + 1]`.
      This operation is equivalent to the following steps:
      1. Reshape `input` to `reshaped` of shape: [block_shape[0], ...,
        block_shape[M-1], batch / prod(block_shape), input_shape[1], ...,
        input_shape[N-1]]
      2. Permute dimensions of `reshaped` to produce `permuted` of shape
         [batch / prod(block_shape),  input_shape[1], block_shape[0], ...,
         input_shape[M], block_shape[M-1], input_shape[M+1],
        ..., input_shape[N-1]]
      3. Reshape `permuted` to produce `reshaped_permuted` of shape
         [batch / prod(block_shape), input_shape[1] * block_shape[0], ...,
         input_shape[M] * block_shape[M-1], input_shape[M+1], ...,
         input_shape[N-1]]
      4. Crop the start and end of dimensions `[1, ..., M]` of
         `reshaped_permuted` according to `crops` to produce the output
         of shape:
         [batch / prod(block_shape),  input_shape[1] *
           block_shape[0] - crops[0,0] - crops[0,1], ..., input_shape[M] *
           block_shape[M-1] - crops[M-1,0] - crops[M-1,1],  input_shape[M+1],
           ..., input_shape[N-1]]
    name: A name for the operation (optional).

  Examples:

  1. For the following input of shape `[4, 1, 1, 1]`,
     `block_shape = [2, 2]`, and `crops = [[0, 0], [0, 0]]`:

     ```python
     [[[[1]]],
      [[[2]]],
      [[[3]]],
      [[[4]]]]
     ```

    The output tensor has shape `[1, 2, 2, 1]` and value:

     ```
     x = [[[[1], [2]],
         [[3], [4]]]]
     ```

  2. For the following input of shape `[4, 1, 1, 3]`,
     `block_shape = [2, 2]`, and `crops = [[0, 0], [0, 0]]`:

     ```python
     [[[1,  2,   3]],
      [[4,  5,   6]],
      [[7,  8,   9]],
      [[10, 11, 12]]]
     ```

    The output tensor has shape `[1, 2, 2, 3]` and value:

    ```python
     x = [[[[1, 2, 3], [4,  5,  6 ]],
           [[7, 8, 9], [10, 11, 12]]]]
     ```

  3. For the following
     input of shape `[4, 2, 2, 1]`,
     `block_shape = [2, 2]`, and `crops = [[0, 0], [0, 0]]`:

     ```python
     x = [[[[1], [3]], [[ 9], [11]]],
          [[[2], [4]], [[10], [12]]],
          [[[5], [7]], [[13], [15]]],
          [[[6], [8]], [[14], [16]]]]
     ```

    The output tensor has shape `[1, 4, 4, 1]` and value:

    ```python
     x = [[[1],  [2],  [ 3], [ 4]],
          [[5],  [6],  [ 7], [ 8]],
          [[9],  [10], [11], [12]],
          [[13], [14], [15], [16]]]
     ```

  4. For the following input of shape
      `[8, 1, 3, 1]`,
      `block_shape = [2, 2]`, and `crops = [[0, 0], [2, 0]]`:

      ```python
      x = [[[[0], [ 1], [ 3]]],
           [[[0], [ 9], [11]]],
           [[[0], [ 2], [ 4]]],
           [[[0], [10], [12]]],
           [[[0], [ 5], [ 7]]],
           [[[0], [13], [15]]],
           [[[0], [ 6], [ 8]]],
           [[[0], [14], [16]]]]
      ```

      The output tensor has shape `[2, 2, 4, 1]` and value:

      ```python
      x = [[[[ 1], [ 2], [ 3], [ 4]],
            [[ 5], [ 6], [ 7], [ 8]]],
           [[[ 9], [10], [11], [12]],
            [[13], [14], [15], [16]]]]
      ```

  Returns:
    A `Tensor`. Has the same type as `input`.
  r   rB   r  r  r)   )r;   intr   r  re   r  r  s       r+   batch_to_space_v2r   	  s@    F S!((K5RXXFK	{%d
D Dr,   one_hotc           
         t        j                  |d| |||||g      5 }|du}|du}|rt        j                  ||      }|rt        j                  ||      }|r|j                  j                  nd}	|r|j                  j                  nd}
|s|rM|D|r |	|k7  rt        dj                  |	|            |r9|
|k7  r4t        dj                  |
|            |r|	n|
}n|t        j                  }|st        j                  d|d      }|}	|st        j                  d	|d
      }|}
|	|
k7  rt        dj                  |	|
            t        j                  | |||||      cddd       S # 1 sw Y   yxY w)av  Returns a one-hot tensor.

  See also `tf.fill`, `tf.eye`.

  The locations represented by indices in `indices` take value `on_value`,
  while all other locations take value `off_value`.

  `on_value` and `off_value` must have matching data types. If `dtype` is also
  provided, they must be the same data type as specified by `dtype`.

  If `on_value` is not provided, it will default to the value `1` with type
  `dtype`

  If `off_value` is not provided, it will default to the value `0` with type
  `dtype`

  If the input `indices` is rank `N`, the output will have rank `N+1`. The
  new axis is created at dimension `axis` (default: the new axis is appended
  at the end).

  If `indices` is a scalar the output shape will be a vector of length `depth`

  If `indices` is a vector of length `features`, the output shape will be:

  ```
    features x depth if axis == -1
    depth x features if axis == 0
  ```

  If `indices` is a matrix (batch) with shape `[batch, features]`, the output
  shape will be:

  ```
    batch x features x depth if axis == -1
    batch x depth x features if axis == 1
    depth x batch x features if axis == 0
  ```

  If `indices` is a RaggedTensor, the 'axis' argument must be positive and refer
  to a non-ragged axis. The output will be equivalent to applying 'one_hot' on
  the values of the RaggedTensor, and creating a new RaggedTensor from the
  result.

  If `dtype` is not provided, it will attempt to assume the data type of
  `on_value` or `off_value`, if one or both are passed in. If none of
  `on_value`, `off_value`, or `dtype` are provided, `dtype` will default to the
  value `tf.float32`.

  Note: If a non-numeric data type output is desired (`tf.string`, `tf.bool`,
  etc.), both `on_value` and `off_value` _must_ be provided to `one_hot`.

  For example:

  ```python
  indices = [0, 1, 2]
  depth = 3
  tf.one_hot(indices, depth)  # output: [3 x 3]
  # [[1., 0., 0.],
  #  [0., 1., 0.],
  #  [0., 0., 1.]]

  indices = [0, 2, -1, 1]
  depth = 3
  tf.one_hot(indices, depth,
             on_value=5.0, off_value=0.0,
             axis=-1)  # output: [4 x 3]
  # [[5.0, 0.0, 0.0],  # one_hot(0)
  #  [0.0, 0.0, 5.0],  # one_hot(2)
  #  [0.0, 0.0, 0.0],  # one_hot(-1)
  #  [0.0, 5.0, 0.0]]  # one_hot(1)

  indices = [[0, 2], [1, -1]]
  depth = 3
  tf.one_hot(indices, depth,
             on_value=1.0, off_value=0.0,
             axis=-1)  # output: [2 x 2 x 3]
  # [[[1.0, 0.0, 0.0],   # one_hot(0)
  #   [0.0, 0.0, 1.0]],  # one_hot(2)
  #  [[0.0, 1.0, 0.0],   # one_hot(1)
  #   [0.0, 0.0, 0.0]]]  # one_hot(-1)

  indices = tf.ragged.constant([[0, 1], [2]])
  depth = 3
  tf.one_hot(indices, depth)  # output: [2 x None x 3]
  # [[[1., 0., 0.],
  #   [0., 1., 0.]],
  #  [[0., 0., 1.]]]
  ```

  Args:
    indices: A `Tensor` of indices.
    depth: A scalar defining the depth of the one hot dimension.
    on_value: A scalar defining the value to fill in output when `indices[j]
      = i`. (default: 1)
    off_value: A scalar defining the value to fill in output when `indices[j]
      != i`. (default: 0)
    axis: The axis to fill (default: -1, a new inner-most axis).
    dtype: The data type of the output tensor.
    name: A name for the operation (optional).

  Returns:
    output: The one-hot tensor.

  Raises:
    TypeError: If dtype of either `on_value` or `off_value` don't match `dtype`
    TypeError: If dtype of `on_value` and `off_value` don't match one another
  r!  N)
dtype_hintz8dtype {0} of on_value does not match dtype parameter {1}z9dtype {0} of off_value does not match dtype parameter {1}r   on_valuer9   r   	off_valuez;dtype {0} of on_value does not match dtype {1} of off_value)r   rr   rA   r   r   r   r  r   float32r   r!  )r   depthr$  r%  rG   r   r)   	on_exists
off_existson_dtype	off_dtypes              r+   r!  r!    s   h ~~
IxD%8: ,'=A$I$&J&&xEBh''	eDi,5x~~((4H.8	**dIJ		U* 006x0GI I)u, 006y%0HJ J &9	nne&&q%jAhh''5{Cii9 //5vh	/JL L   %9d!%'W,' ,' ,'s   EE++E4c                    t        | t        j                        rj| j                         j                  Pt        j                  t        j                  | j                         j                        t        j                        S t        | t        j                        r| j                  j                         j                         ri| j                  j                         j                  d   j                   }t        j                  t        j                  |      t        j                        S t#        j$                  dt'        |       d      S )z0Returns a 1D-tensor listing all dimensions in x.r   r   r   )r;   ra  r   rw   r   r	   r   r   r.  r   rf   r   rs   rv   rx   r0   r1   r   r-  r   )rQ   rs     r+   _all_dimensionsr.  6  s     :$$%!++-*=*=*I
		!++-%%&fll< <M../mm002	!&&q)//A		!FLLAA 
		QQ	++r,   sequence_maskc           	      R   t        j                  |d| |g      5  t        j                  |       } |Jt        j                  | t        |             }t        j                  t        d|j                        |      }nt        j                  |      }|j                         j                  =|j                         j                  dk7  r t        d| d|j                          d      t        j                  t        d|j                        |t        d|j                              }t        j                  t        | d      |j                        }||k  }||j                  j                  |      r|cddd       S t        j                  ||      cddd       S # 1 sw Y   yxY w)	a  Returns a mask tensor representing the first N positions of each cell.

  If `lengths` has shape `[d_1, d_2, ..., d_n]` the resulting tensor `mask` has
  dtype `dtype` and shape `[d_1, d_2, ..., d_n, maxlen]`, with

  ```
  mask[i_1, i_2, ..., i_n, j] = (j < lengths[i_1, i_2, ..., i_n])
  ```

  Examples:

  ```python
  tf.sequence_mask([1, 3, 2], 5)  # [[True, False, False, False, False],
                                  #  [True, True, True, False, False],
                                  #  [True, True, False, False, False]]

  tf.sequence_mask([[1, 3],[2,0]])  # [[[True, False, False],
                                    #   [True, True, True]],
                                    #  [[True, True, False],
                                    #   [False, False, False]]]
  ```

  Args:
    lengths: integer tensor, all its values <= maxlen.
    maxlen: scalar integer tensor, size of last dimension of returned tensor.
      Default is the maximum value in `lengths`.
    dtype: output type of the resulting tensor.
    name: name of the op.

  Returns:
    A mask tensor of shape `lengths.shape + (maxlen,)`, cast to specified dtype.
  Raises:
    ValueError: if `maxlen` is not a scalar.
  SequenceMaskNr   zHArgument `maxlen` must be scalar for sequence_mask, received `maxlen` = z with shape 'z	' insteadr   r   )r   rr   rA   r   _maxr.  maximumr   r   rw   r   rI   r-  ru   rD   is_compatible_with)lengthsmaxlenr   r)   
row_vectormatrixr*   s          r+   r/  r/  E  sv   J ~~dNWf,=> .##G,G~  /'*BCf##HQ$=vFf$$V,f+0@0@0B0H0HA0M ..4X 6&&,&6&6&8%9D E E $$FLL!68Av||+DFJ {7B7FF& F}77>3. .6 vu-7. . .s   EF>FF&r   squeeze_dimsc                     t        j                  d|d|      }t        j                  |      r|g}t	        j
                  | ||      S )a  Removes dimensions of size 1 from the shape of a tensor.

  Given a tensor `input`, this operation returns a tensor of the same type with
  all dimensions of size 1 removed. If you don't want to remove all size 1
  dimensions, you can remove specific size 1 dimensions by specifying
  `axis`.

  For example:

  >>> # 't' is a tensor of shape [1, 2, 1, 3, 1, 1]
  >>> t = tf.ones([1, 2, 1, 3, 1, 1])
  >>> print(tf.shape(tf.squeeze(t)).numpy())
  [2 3]

  Or, to remove specific size 1 dimensions:

  >>> # 't' is a tensor of shape [1, 2, 1, 3, 1, 1]
  >>> t = tf.ones([1, 2, 1, 3, 1, 1])
  >>> print(tf.shape(tf.squeeze(t, [2, 4])).numpy())
  [1 2 3 1]

  Note: if `input` is a `tf.RaggedTensor`, then this operation takes `O(N)`
  time, where `N` is the number of elements in the squeezed dimensions.

  Args:
    input: A `Tensor`. The `input` to squeeze.
    axis: An optional list of `ints`. Defaults to `[]`. If specified, only
      squeezes the dimensions listed. The dimension index starts at 0. It is an
      error to squeeze a dimension that is not 1. Must be in the range
      `[-rank(input), rank(input))`. Must be specified if `input` is a
      `RaggedTensor`.
    name: A name for the operation (optional).
    squeeze_dims: Deprecated keyword argument that is now axis.

  Returns:
    A `Tensor`. Has the same type as `input`.
    Contains the same data as `input`, but has one or more dimensions of
    size 1 removed.

  Raises:
    ValueError: When both `squeeze_dims` and `axis` are specified.
  rG   r9  )r   rH   r   isscalarr   r   )rB   rG   r)   r9  s       r+   r   r     sF    ` 
	/	/n0<
>$[[6D			udD	11r,   c                     t        | ||      S )a	  Removes dimensions of size 1 from the shape of a tensor.

  Given a tensor `input`, this operation returns a tensor of the same type with
  all dimensions of size 1 removed. If you don't want to remove all size 1
  dimensions, you can remove specific size 1 dimensions by specifying
  `axis`.

  For example:

  ```python
  # 't' is a tensor of shape [1, 2, 1, 3, 1, 1]
  tf.shape(tf.squeeze(t))  # [2, 3]
  ```

  Or, to remove specific size 1 dimensions:

  ```python
  # 't' is a tensor of shape [1, 2, 1, 3, 1, 1]
  tf.shape(tf.squeeze(t, [2, 4]))  # [1, 2, 3, 1]
  ```

  Unlike the older op `tf.compat.v1.squeeze`, this op does not accept a
  deprecated `squeeze_dims` argument.

  Note: if `input` is a `tf.RaggedTensor`, then this operation takes `O(N)`
  time, where `N` is the number of elements in the squeezed dimensions.

  Note: If squeeze is performed on dimensions of unknown sizes, then the
  returned Tensor will be of unknown shape. A common situation is when the
  first (batch) dimension is of size `None`, `tf.squeeze` returns
  `<unknown>` shape which may be a surprise. Specify the `axis=` argument
  to get the expected result, as illustrated in the following example:

  ```python
  @tf.function
  def func(x):
    print('x.shape:', x.shape)
    known_axes = [i for i, size in enumerate(x.shape) if size == 1]
    y = tf.squeeze(x, axis=known_axes)
    print('shape of tf.squeeze(x, axis=known_axes):', y.shape)
    y = tf.squeeze(x)
    print('shape of tf.squeeze(x):', y.shape)
    return 0

  _ = func.get_concrete_function(tf.TensorSpec([None, 1, 2], dtype=tf.int32))
  # Output is.
  # x.shape: (None, 1, 2)
  # shape of tf.squeeze(x, axis=known_axes): (None, 2)
  # shape of tf.squeeze(x): <unknown>
  ```

  Args:
    input: A `Tensor`. The `input` to squeeze.
    axis: An optional list of `ints`. Defaults to `[]`. If specified, only
      squeezes the dimensions listed. The dimension index starts at 0. It is an
      error to squeeze a dimension that is not 1. Must be in the range
      `[-rank(input), rank(input))`. Must be specified if `input` is a
      `RaggedTensor`.
    name: A name for the operation (optional).

  Returns:
    A `Tensor`. Has the same type as `input`.
    Contains the same data as `input`, but has one or more dimensions of
    size 1 removed.

  Raises:
    ValueError: The input cannot be converted to a tensor, or the specified
      axis cannot be squeezed.
  )r   rL   s      r+   
squeeze_v2r=    s    R 
d	##r,   wherec                 0   |a|_t        j                  |d| g      5 }t        j                  | t        j                  d      } t        j                  | |      cddd       S ||t        j                  | |||      S t        d      # 1 sw Y   yxY w)a  Return the elements, either from `x` or `y`, depending on the `condition`.

  If both `x` and `y` are None, then this operation returns the coordinates of
  true elements of `condition`.  The coordinates are returned in a 2-D tensor
  where the first dimension (rows) represents the number of true elements, and
  the second dimension (columns) represents the coordinates of the true
  elements. Keep in mind, the shape of the output tensor can vary depending on
  how many true values there are in input. Indices are output in row-major
  order.

  If both non-None, `x` and `y` must have the same shape.
  The `condition` tensor must be a scalar if `x` and `y` are scalar.
  If `x` and `y` are tensors of higher rank, then `condition` must be either a
  vector with size matching the first dimension of `x`, or must have the same
  shape as `x`.

  The `condition` tensor acts as a mask that chooses, based on the value at each
  element, whether the corresponding element / row in the output should be taken
  from `x` (if true) or `y` (if false).

  If `condition` is a vector and `x` and `y` are higher rank matrices, then it
  chooses which row (outer dimension) to copy from `x` and `y`. If `condition`
  has the same shape as `x` and `y`, then it chooses which element to copy from
  `x` and `y`.

  Args:
    condition: A `Tensor` of type `bool`
    x: A Tensor which may have the same shape as `condition`. If `condition` is
      rank 1, `x` may have higher rank, but its first dimension must match the
      size of `condition`.
    y: A `tensor` with the same shape and type as `x`.
    name: A name of the operation (optional)

  Returns:
    A `Tensor` with the same type and shape as `x`, `y` if they are non-None.
    Otherwise, a `Tensor` with shape `(num_true, rank(condition))`.

  Raises:
    ValueError: When exactly one of `x` or `y` is non-None.

  @compatibility(TF2)

  This API is compatible with eager execution and `tf.function`. However, this
  is still a legacy API endpoint originally designed for TF1. To migrate to
  fully-native TF2, please replace its usage with `tf.where` instead, which is
  directly backwards compatible with `tf.compat.v1.where`.

  However,`tf.compat.v1.where` is more restrictive than `tf.where`, requiring
  `x` and `y` to have the same shape, and returning a `Tensor` with the same
  type and shape as `x`, `y` (if they are both non-None).

  `tf.where` will accept `x`, `y` that are not the same shape as long as they
  are broadcastable with one another and with `condition`, and will return a
  `Tensor` with shape broadcast from `condition`, `x`, and `y`.

  For example, the following works with `tf.where` but not `tf.compat.v1.where`:

  >>> tf.where([True, False, False, True], [1,2,3,4], [100])
  <tf.Tensor: shape=(4,), dtype=int32, numpy=array([  1, 100, 100,   4],
  dtype=int32)>

  >>> tf.where(True, [1,2,3,4], 100)
  <tf.Tensor: shape=(4,), dtype=int32, numpy=array([1, 2, 3, 4],
  dtype=int32)>

  @end_compatibility
  NWhere	conditionpreferred_dtyper)   rA  r)   rA  rQ   rR   r)   .x and y must both be non-None or both be None.)
r   rr   rA   r   r:  r   r>  r   selectrI   rE  s       r+   r>  r>    s    L Y19	g	{	3 At''
V[[{Di  94@A A 	}a14HH
E
FFA A   =BBr   c                 0   |a|_t        j                  |d| g      5 }t        j                  | t        j                  d      } t        j                  | |      cddd       S ||t        j                  | |||      S t        d      # 1 sw Y   yxY w)a  Returns the indices of non-zero elements, or multiplexes `x` and `y`.

  This operation has two modes:

  1. **Return the indices of non-zero elements** - When only
     `condition` is provided the result is an `int64` tensor where each row is
     the index of a non-zero element of `condition`. The result's shape
     is `[tf.math.count_nonzero(condition), tf.rank(condition)]`.
  2. **Multiplex `x` and `y`** - When both `x` and `y` are provided the
     result has the shape of `x`, `y`, and `condition` broadcast together. The
     result is taken from `x` where `condition` is non-zero
     or `y` where `condition` is zero.

  #### 1. Return the indices of non-zero elements

  Note: In this mode `condition` can have a dtype of `bool` or any numeric
  dtype.

  If `x` and `y` are not provided (both are None):

  `tf.where` will return the indices of `condition` that are non-zero,
  in the form of a 2-D tensor with shape `[n, d]`, where `n` is the number of
  non-zero elements in `condition` (`tf.count_nonzero(condition)`), and `d` is
  the number of axes of `condition` (`tf.rank(condition)`).

  Indices are output in row-major order. The `condition` can have a `dtype` of
  `tf.bool`, or any numeric `dtype`.

  Here `condition` is a 1-axis `bool` tensor with 2 `True` values. The result
  has a shape of `[2,1]`

  >>> tf.where([True, False, False, True]).numpy()
  array([[0],
         [3]])

  Here `condition` is a 2-axis integer tensor, with 3 non-zero values. The
  result has a shape of `[3, 2]`.

  >>> tf.where([[1, 0, 0], [1, 0, 1]]).numpy()
  array([[0, 0],
         [1, 0],
         [1, 2]])

  Here `condition` is a 3-axis float tensor, with 5 non-zero values. The output
  shape is `[5, 3]`.

  >>> float_tensor = [[[0.1, 0], [0, 2.2], [3.5, 1e6]],
  ...                 [[0,   0], [0,   0], [99,    0]]]
  >>> tf.where(float_tensor).numpy()
  array([[0, 0, 0],
         [0, 1, 1],
         [0, 2, 0],
         [0, 2, 1],
         [1, 2, 0]])

  These indices are the same that `tf.sparse.SparseTensor` would use to
  represent the condition tensor:

  >>> sparse = tf.sparse.from_dense(float_tensor)
  >>> sparse.indices.numpy()
  array([[0, 0, 0],
         [0, 1, 1],
         [0, 2, 0],
         [0, 2, 1],
         [1, 2, 0]])

  A complex number is considered non-zero if either the real or imaginary
  component is non-zero:

  >>> tf.where([complex(0.), complex(1.), 0+1j, 1+1j]).numpy()
  array([[1],
         [2],
         [3]])

  #### 2. Multiplex `x` and `y`

  Note: In this mode `condition` must have a dtype of `bool`.

  If `x` and `y` are also provided (both have non-None values) the `condition`
  tensor acts as a mask that chooses whether the corresponding
  element / row in the output should be taken from `x` (if the element in
  `condition` is `True`) or `y` (if it is `False`).

  The shape of the result is formed by
  [broadcasting](https://docs.scipy.org/doc/numpy/reference/ufuncs.html)
  together the shapes of `condition`, `x`, and `y`.

  When all three inputs have the same size, each is handled element-wise.

  >>> tf.where([True, False, False, True],
  ...          [1, 2, 3, 4],
  ...          [100, 200, 300, 400]).numpy()
  array([  1, 200, 300,   4], dtype=int32)

  There are two main rules for broadcasting:

  1. If a tensor has fewer axes than the others, length-1 axes are added to the
     left of the shape.
  2. Axes with length-1 are streched to match the coresponding axes of the other
     tensors.

  A length-1 vector is streched to match the other vectors:

  >>> tf.where([True, False, False, True], [1, 2, 3, 4], [100]).numpy()
  array([  1, 100, 100,   4], dtype=int32)

  A scalar is expanded to match the other arguments:

  >>> tf.where([[True, False], [False, True]], [[1, 2], [3, 4]], 100).numpy()
  array([[  1, 100], [100,   4]], dtype=int32)
  >>> tf.where([[True, False], [False, True]], 1, 100).numpy()
  array([[  1, 100], [100,   1]], dtype=int32)

  A scalar `condition` returns the complete `x` or `y` tensor, with
  broadcasting applied.

  >>> tf.where(True, [1, 2, 3, 4], 100).numpy()
  array([1, 2, 3, 4], dtype=int32)
  >>> tf.where(False, [1, 2, 3, 4], 100).numpy()
  array([100, 100, 100, 100], dtype=int32)

  For a non-trivial example of broadcasting, here `condition` has a shape of
  `[3]`, `x` has a shape of `[3,3]`, and `y` has a shape of `[3,1]`.
  Broadcasting first expands the shape of `condition` to `[1,3]`. The final
  broadcast shape is `[3,3]`. `condition` will select columns from `x` and `y`.
  Since `y` only has one column, all columns from `y` will be identical.

  >>> tf.where([True, False, True],
  ...          x=[[1, 2, 3],
  ...             [4, 5, 6],
  ...             [7, 8, 9]],
  ...          y=[[100],
  ...             [200],
  ...             [300]]
  ... ).numpy()
  array([[ 1, 100, 3],
         [ 4, 200, 6],
         [ 7, 300, 9]], dtype=int32)

  Note that if the gradient of either branch of the `tf.where` generates
  a `NaN`, then the gradient of the entire `tf.where` will be `NaN`. This is
  because the gradient calculation for `tf.where` combines the two branches, for
  performance reasons.

  A workaround is to use an inner `tf.where` to ensure the function has
  no asymptote, and to avoid computing a value whose gradient is `NaN` by
  replacing dangerous inputs with safe inputs.

  Instead of this,

  >>> x = tf.constant(0., dtype=tf.float32)
  >>> with tf.GradientTape() as tape:
  ...   tape.watch(x)
  ...   y = tf.where(x < 1., 0., 1. / x)
  >>> print(tape.gradient(y, x))
  tf.Tensor(nan, shape=(), dtype=float32)

  Although, the `1. / x` values are never used, its gradient is a `NaN` when
  `x = 0`. Instead, we should guard that with another `tf.where`

  >>> x = tf.constant(0., dtype=tf.float32)
  >>> with tf.GradientTape() as tape:
  ...   tape.watch(x)
  ...   safe_x = tf.where(tf.equal(x, 0.), 1., x)
  ...   y = tf.where(x < 1., 0., 1. / safe_x)
  >>> print(tape.gradient(y, x))
  tf.Tensor(0.0, shape=(), dtype=float32)

  See also:

  * `tf.sparse` - The indices returned by the first form of `tf.where` can be
     useful in `tf.sparse.SparseTensor` objects.
  * `tf.gather_nd`, `tf.scatter_nd`, and related ops - Given the
    list of indices returned from `tf.where` the `scatter` and `gather` family
    of ops can be used fetch values or insert values at those indices.
  * `tf.strings.length` - `tf.string` is not an allowed dtype for the
    `condition`. Use the string length instead.

  Args:
    condition: A `tf.Tensor` of dtype bool, or any numeric dtype. `condition`
      must have dtype `bool` when `x` and `y` are provided.
    x: If provided, a Tensor which is of the same type as `y`, and has a shape
      broadcastable with `condition` and `y`.
    y: If provided, a Tensor which is of the same type as `x`, and has a shape
      broadcastable with `condition` and `x`.
    name: A name of the operation (optional).

  Returns:
    If `x` and `y` are provided:
      A `Tensor` with the same type as `x` and `y`, and shape that
      is broadcast from `condition`, `x`, and `y`.
    Otherwise, a `Tensor` with shape `[tf.math.count_nonzero(condition),
    tf.rank(condition)]`.

  Raises:
    ValueError: When exactly one of `x` or `y` is non-None, or the shapes
      are not all broadcastable.
  Nr@  rA  rB  rD  )rA  ter)   rF  )
r   rr   rA   r   r:  r   r>  r   	select_v2rI   rE  s       r+   r   r   \  s    R Y19	g	{	3 At''
V[[{Di  94@A A 	}!!IadKK
E
FFA ArH  reverse_sequencez+seq_dim is deprecated, use seq_axis insteadseq_dimz/batch_dim is deprecated, use batch_axis instead	batch_dimc                     t        j                  d|d|      }t        j                  d|d|      }t        j                  | ||||      S )  Reverses variable length slices.

  This op first slices `input` along the dimension `batch_axis`, and for
  each slice `i`, reverses the first `seq_lengths[i]` elements along the
  dimension `seq_axis`.

  The elements of `seq_lengths` must obey `seq_lengths[i] <=
  input.dims[seq_axis]`, and `seq_lengths` must be a vector of length
  `input.dims[batch_axis]`.

  The output slice `i` along dimension `batch_axis` is then given by
  input slice `i`, with the first `seq_lengths[i]` slices along
  dimension `seq_axis` reversed.

  Example usage:

  >>> seq_lengths = [7, 2, 3, 5]
  >>> input = [[1, 2, 3, 4, 5, 0, 0, 0], [1, 2, 0, 0, 0, 0, 0, 0],
  ...          [1, 2, 3, 4, 0, 0, 0, 0], [1, 2, 3, 4, 5, 6, 7, 8]]
  >>> output = tf.reverse_sequence(input, seq_lengths, seq_axis=1, batch_axis=0)
  >>> output
  <tf.Tensor: shape=(4, 8), dtype=int32, numpy=
  array([[0, 0, 5, 4, 3, 2, 1, 0],
         [2, 1, 0, 0, 0, 0, 0, 0],
         [3, 2, 1, 4, 0, 0, 0, 0],
         [5, 4, 3, 2, 1, 6, 7, 8]], dtype=int32)>

  Args:
    input: A `Tensor`. The input to reverse.
    seq_lengths: A `Tensor`. Must be one of the following types: `int32`,
      `int64`. 1-D with length `input.dims(batch_axis)` and `max(seq_lengths) <=
      input.dims(seq_axis)`
    seq_axis: An `int`. The dimension which is partially reversed.
    batch_axis: An optional `int`. Defaults to `0`. The dimension along which
      reversal is performed.
    name: A name for the operation (optional).

  Returns:
    A Tensor. Has the same type as input.
  seq_axisrN  
batch_axisrO  rB   seq_lengthsrN  rO  r)   )r   rH   r   rM  )rB   rU  rR  rS  r)   rN  rO  s          r+   rM  rM  1  s[    l 33J4=wH(55lJ6A9N*		'	'
 r,   c                 6    t        j                  | ||||      S )rQ  rT  )r   rM  )rB   rU  rR  rS  r)   s        r+   reverse_sequence_v2rW  s  s'    ^ 
	'	'
 r,   r   znThe `validate_indices` argument has no effect. Indices are always validated on CPU and never validated on GPU.)validate_indicesNc                     ~||}t        j                  |      dk7  rt        j                  | ||||      S 	 | j	                  ||      S # t
        $ r t        j                  | |||      cY S w xY w)ae  Gather slices from params axis `axis` according to indices.

  Gather slices from `params` axis `axis` according to `indices`.  `indices`
  must be an integer tensor of any dimension (often 1-D).

  `Tensor.__getitem__` works for scalars, `tf.newaxis`, and
  [python slices](https://numpy.org/doc/stable/reference/arrays.indexing.html#basic-slicing-and-indexing)

  `tf.gather` extends indexing to handle tensors of indices.

  In the simplest case it's identical to scalar indexing:

  >>> params = tf.constant(['p0', 'p1', 'p2', 'p3', 'p4', 'p5'])
  >>> params[3].numpy()
  b'p3'
  >>> tf.gather(params, 3).numpy()
  b'p3'

  The most common case is to pass a single axis tensor of indices (this
  can't be expressed as a python slice because the indices are not sequential):

  >>> indices = [2, 0, 2, 5]
  >>> tf.gather(params, indices).numpy()
  array([b'p2', b'p0', b'p2', b'p5'], dtype=object)

  <div style="width:70%; margin:auto; margin-bottom:10px; margin-top:20px;">
  <img style="width:100%" src="https://www.tensorflow.org/images/Gather.png"
  alt>
  </div>

  The indices can have any shape. When the `params` has 1 axis, the
  output shape is equal to the input shape:

  >>> tf.gather(params, [[2, 0], [2, 5]]).numpy()
  array([[b'p2', b'p0'],
         [b'p2', b'p5']], dtype=object)

  The `params` may also have any shape. `gather` can select slices
  across any axis depending on the `axis` argument (which defaults to 0).
  Below it is used to gather first rows, then columns from a matrix:

  >>> params = tf.constant([[0, 1.0, 2.0],
  ...                       [10.0, 11.0, 12.0],
  ...                       [20.0, 21.0, 22.0],
  ...                       [30.0, 31.0, 32.0]])
  >>> tf.gather(params, indices=[3,1]).numpy()
  array([[30., 31., 32.],
         [10., 11., 12.]], dtype=float32)
  >>> tf.gather(params, indices=[2,1], axis=1).numpy()
  array([[ 2.,  1.],
         [12., 11.],
         [22., 21.],
         [32., 31.]], dtype=float32)

  More generally: The output shape has the same shape as the input, with the
  indexed-axis replaced by the shape of the indices.

  >>> def result_shape(p_shape, i_shape, axis=0):
  ...   return p_shape[:axis] + i_shape + p_shape[axis+1:]
  >>>
  >>> result_shape([1, 2, 3], [], axis=1)
  [1, 3]
  >>> result_shape([1, 2, 3], [7], axis=1)
  [1, 7, 3]
  >>> result_shape([1, 2, 3], [7, 5], axis=1)
  [1, 7, 5, 3]

  Here are some examples:

  >>> params.shape.as_list()
  [4, 3]
  >>> indices = tf.constant([[0, 2]])
  >>> tf.gather(params, indices=indices, axis=0).shape.as_list()
  [1, 2, 3]
  >>> tf.gather(params, indices=indices, axis=1).shape.as_list()
  [4, 1, 2]

  >>> params = tf.random.normal(shape=(5, 6, 7, 8))
  >>> indices = tf.random.uniform(shape=(10, 11), maxval=7, dtype=tf.int32)
  >>> result = tf.gather(params, indices, axis=2)
  >>> result.shape.as_list()
  [5, 6, 10, 11, 8]

  This is because each index takes a slice from `params`, and
  places it at the corresponding location in the output. For the above example

  >>> # For any location in indices
  >>> a, b = 0, 1
  >>> tf.reduce_all(
  ...     # the corresponding slice of the result
  ...     result[:, :, a, b, :] ==
  ...     # is equal to the slice of `params` along `axis` at the index.
  ...     params[:, :, indices[a, b], :]
  ... ).numpy().item()
  True

  ### Batching:

  The `batch_dims` argument lets you gather different items from each element
  of a batch.

  Using `batch_dims=1` is equivalent to having an outer loop over the first
  axis of `params` and `indices`:

  >>> params = tf.constant([
  ...     [0, 0, 1, 0, 2],
  ...     [3, 0, 0, 0, 4],
  ...     [0, 5, 0, 6, 0]])
  >>> indices = tf.constant([
  ...     [2, 4],
  ...     [0, 4],
  ...     [1, 3]])

  >>> tf.gather(params, indices, axis=1, batch_dims=1).numpy()
  array([[1, 2],
         [3, 4],
         [5, 6]], dtype=int32)

  This is equivalent to:

  >>> def manually_batched_gather(params, indices, axis):
  ...   batch_dims=1
  ...   result = []
  ...   for p,i in zip(params, indices):
  ...     r = tf.gather(p, i, axis=axis-batch_dims)
  ...     result.append(r)
  ...   return tf.stack(result)
  >>> manually_batched_gather(params, indices, axis=1).numpy()
  array([[1, 2],
         [3, 4],
         [5, 6]], dtype=int32)

  Higher values of `batch_dims` are equivalent to multiple nested loops over
  the outer axes of `params` and `indices`. So the overall shape function is

  >>> def batched_result_shape(p_shape, i_shape, axis=0, batch_dims=0):
  ...   return p_shape[:axis] + i_shape[batch_dims:] + p_shape[axis+1:]
  >>>
  >>> batched_result_shape(
  ...     p_shape=params.shape.as_list(),
  ...     i_shape=indices.shape.as_list(),
  ...     axis=1,
  ...     batch_dims=1)
  [3, 2]

  >>> tf.gather(params, indices, axis=1, batch_dims=1).shape.as_list()
  [3, 2]

  This comes up naturally if you need to use the indices of an operation like
  `tf.argsort`, or `tf.math.top_k` where the last dimension of the indices
  indexes into the last dimension of input, at the corresponding location.
  In this case you can use `tf.gather(values, indices, batch_dims=-1)`.

  See also:

  * `tf.Tensor.__getitem__`: The direct tensor index operation (`t[]`), handles
    scalars and python-slices `tensor[..., 7, 1:-1]`
  * `tf.scatter`: A collection of operations similar to `__setitem__`
    (`t[i] = x`)
  * `tf.gather_nd`: An operation similar to `tf.gather` but gathers across
    multiple axis at once (it can gather elements of a matrix instead of rows
    or columns)
  * `tf.boolean_mask`, `tf.where`: Binary indexing.
  * `tf.slice` and `tf.strided_slice`: For lower level access to the
    implementation of `__getitem__`'s python-slice handling (`t[1:-1:2]`)

  Args:
    params: The `Tensor` from which to gather values. Must be at least rank
      `axis + 1`.
    indices: The index `Tensor`.  Must be one of the following types: `int32`,
      `int64`. The values must be in range `[0, params.shape[axis])`.
    validate_indices: Deprecated, does nothing. Indices are always validated on
      CPU, never validated on GPU.

      Caution: On CPU, if an out of bound index is found, an error is raised.
      On GPU, if an out of bound index is found, a 0 is stored in the
      corresponding output value.
    axis: A `Tensor`. Must be one of the following types: `int32`, `int64`. The
      `axis` in `params` to gather `indices` from. Must be greater than or equal
      to `batch_dims`.  Defaults to the first non-batch dimension. Supports
      negative indexes.
    batch_dims: An `integer`.  The number of batch dimensions.  Must be less
      than or equal to `rank(indices)`.
    name: A name for the operation (optional).

  Returns:
    A `Tensor`. Has the same type as `params`.
  r   )
batch_dimsr)   r9   )r   r   r   	gather_v2sparse_readAttributeError)paramsr   rX  r)   rG   rZ  s         r+   r   r     s    R 	\D%*""*4A AE gD11	 E""67DtDDEs   A "A10A1c                 $    t        | |||||      S )N)rX  r)   rG   rZ  )r   )r^  r   rX  rG   rZ  r)   s         r+   r[  r[    s#     
'
 r,   batch_gatherz
2017-10-25zg`tf.batch_gather` is deprecated, please use `tf.gather` with `batch_dims=tf.rank(indices) - 1` instead.c                 H   t        j                  |d| |g      5  t        j                  |d      }t        j                  | d      } |j                  j                  t        d      t        | ||j                  j                  dz
        cddd       S # 1 sw Y   yxY w)	zGGather slices from params according to indices with leading batch dims.BatchGatherr   r9   r^  Nz7batch_gather does not allow indices with unknown shape.r   rZ  )r   rr   rA   r(   r   rI   _batch_gather)r^  r   r)   s      r+   r`  r`    s     ~~dMFG+<= N##G)<G""69F}}"
CE EW]]5H5H15LMN N Ns   A4BB!c           	      	   |t        |t              st        d| d      t        j                  |d      }t        j                  | d      } |j
                  j                  }|t        d      ||dz
  }|d	k  r||z  }|d	k  s||k\  rt        d
| d|       | j
                  j                  >|| j
                  j                  k\  r%t        d
| d| j
                  j                         |||k7  rt        |t              s1t        j                  |d	k  |t        j                  |       z   |      }n|d	k  r/| j
                  j                  |t        j                  |       z   }n|| j
                  j                   k  s|| j
                  j                  k\  r>t        d| d| j
                  j                    d| j
                  j                   d      |d	k  r|| j
                  j                  z  }||k  rt        d
| d|       t        t        |            |gt        j                  ||d      t        j                  |dz   t        |       d      g}t!        | t#        |d	            } t%        | ||      }||z   |z
  }t        t        |            t        j                  ||d      t        t        ||            t        j                  |t        |      d      g}t!        |t#        |d	            S t        |      }t        |       }	|}
|j&                  j(                  }t+        d|      }t        j,                  |	|      }t        |d	d      D ]  }||dz
     }|||   z  }t/        d|      }t+        d|      }t        j                  |||      }||z  }t1        j2                  dg|dz
  z  |gz   dg||z
  z  z   d	      }|
t5        ||      z  }
 t5        |
dg      }|	|dz   d }t        j6                  |	d|dz    d	gd      }t5        | t#        |g|gd	            }t9        ||      }t5        |t#        ||gd	            }|j;                         d| j=                  | j;                         d|       }|j?                  |j;                         j@                  |d       }|j?                  | j;                         |dz   d       }|jC                  |       |S )a  Gather slices from params according to indices with leading batch dims.

  This operation assumes that the leading `batch_dims` dimensions of `indices`
  and `params` are batch dimensions; and performs a `tf.gather` operation within
  each batch. (If `batch_dims` is not specified, then it defaults to
  `rank(indices)-1`.)  In the case in which `batch_dims==0`, this operation
  is equivalent to `tf.gather`.

  Args:
    params: A Tensor. The tensor from which to gather values.
    indices: A Tensor. Must be one of the following types: int32, int64. Index
      tensor. Must be in range `[0, params.shape[batch_dims]]`.
    batch_dims: An integer or none.  The number of batch dimensions.  Must be
      less than `rank(indices)`.  Defaults to `rank(indices) - 1` if None.
    axis: A `Tensor`. Must be one of the following types: `int32`, `int64`. The
      `axis` in `params` to gather `indices` from. Must be greater than or equal
      to `batch_dims`.  Defaults to the first non-batch dimension. Supports
      negative indexes.

  Returns:
    A Tensor. Has the same type as `params`.

  Raises:
    ValueError: if `indices` has an unknown shape.
  Nz>Argument `batch_dims` must be an int. Received `batch_dims` = z insteadr   r9   r^  zPtf.gather does not allow indices with unknown rank when batch_dims is specified.r   r   Argument `batch_dims` = % must be less than rank(`indices`) = $ must be less than rank(`params`) = zArgument `axis` = z out of range [z, r   z1 must be less than or equal to argument `axis` = r   rc  )r&  r   r   r   F)"r;   r  r   r   rA   r(   r   rI   tfr>  	array_opsr   r   r5  r   r-  r$  r   rd  r   r   rz  ru   rX  r   r  r$   r   r   rw   
merge_withr   r0   r  )r^  r   rZ  rG   indices_ndimsr&  r*   params_startindices_shapeparams_shapebatch_indicesindices_dtypeaccum_dim_valuecasted_params_shaperE   	dim_valuer  r  dim_indices	dim_shapeflat_indicesouter_shapeflat_inner_shapeflat_paramsflat_resultfinal_shapes                             r+   rd  rd    s.   4 Jz3$?
 //9l(D E E!!'	:'  h7&--%%-
 : ; ;"J!^-J!^z]2
/
| <**7: ; ;\\#
fll6H6H(H
/
| <))/););(<> ? ? 
$*,dC XXdQhy~~f'= =tDd	fll((0INN6**d
&,,$$$
$$&,,2D2D*D-dV 4$ll0001FLL4F4F3GqJ K 	K	"""	
	3J< @??CfF G 	G
 	U:$Ja0D1Hd6lA6D
 vvd34F67zBF !4'*4LU:M<;U:}-.L$v,:	D V&A"677.-v,---**-=1/$)),F:q"% 	5c#C!G,I*3//O"M*E-(D%%eY=K?"K%%	
sQw9+%}s/B(CC!MIW[)44M	5 -,Z!^_-+!&&|OZ!^'Dqc',. )9(:K'H,-!/ 0+{L1+;{'C! LM&!!#KZ0;;*%'+''(9(9(;(@(@(MN+''(8(8(::>?(KL+;	-r,   	gather_ndzmanip.gather_ndc                 ,   t        j                  |      }|t        |      }|dk(  r|dvrt        j                  | |||      S |dk(  r	 | j	                  ||      S t        | ||||      S # t
        $ r t        j                  | |||      cY S w xY w)a  Gather slices from `params` into a Tensor with shape specified by `indices`.

  `indices` is a `Tensor` of indices into `params`. The index vectors are
  arranged along the last axis of `indices`.

  This is similar to `tf.gather`, in which `indices` defines slices into the
  first dimension of `params`. In `tf.gather_nd`, `indices` defines slices into
  the first `N` dimensions of `params`, where `N = indices.shape[-1]`.

  ## Gathering scalars

  In the simplest case the vectors in `indices` index the full rank of `params`:

  >>> tf.gather_nd(
  ...     indices=[[0, 0],
  ...              [1, 1]],
  ...     params = [['a', 'b'],
  ...               ['c', 'd']]).numpy()
  array([b'a', b'd'], dtype=object)

  In this case the result has 1-axis fewer than `indices`, and each index vector
  is replaced by the scalar indexed from `params`.

  In this case the shape relationship is:

  ```
  index_depth = indices.shape[-1]
  assert index_depth == params.shape.rank
  result_shape = indices.shape[:-1]
  ```

  If `indices` has a rank of `K`, it is helpful to think `indices` as a
  (K-1)-dimensional tensor of indices into `params`.

  ## Gathering slices

  If the index vectors do not index the full rank of `params` then each location
  in the result contains a slice of params. This example collects rows from a
  matrix:

  >>> tf.gather_nd(
  ...     indices = [[1],
  ...                [0]],
  ...     params = [['a', 'b', 'c'],
  ...               ['d', 'e', 'f']]).numpy()
  array([[b'd', b'e', b'f'],
         [b'a', b'b', b'c']], dtype=object)

  Here `indices` contains `[2]` index vectors, each with a length of `1`.
  The index vectors each refer to rows of the `params` matrix. Each
  row has a shape of `[3]` so the output shape is `[2, 3]`.

  In this case, the relationship between the shapes is:

  ```
  index_depth = indices.shape[-1]
  outer_shape = indices.shape[:-1]
  assert index_depth <= params.shape.rank
  inner_shape = params.shape[index_depth:]
  output_shape = outer_shape + inner_shape
  ```

  It is helpful to think of the results in this case as tensors-of-tensors.
  The shape of the outer tensor is set by the leading dimensions of `indices`.
  While the shape of the inner tensors is the shape of a single slice.

  ## Batches

  Additionally, both `params` and `indices` can have `M` leading batch
  dimensions that exactly match. In this case `batch_dims` must be set to `M`.

  For example, to collect one row from each of a batch of matrices you could
  set the leading elements of the index vectors to be their location in the
  batch:

  >>> tf.gather_nd(
  ...     indices = [[0, 1],
  ...                [1, 0],
  ...                [2, 4],
  ...                [3, 2],
  ...                [4, 1]],
  ...     params=tf.zeros([5, 7, 3])).shape.as_list()
  [5, 3]

  The `batch_dims` argument lets you omit those leading location dimensions
  from the index:

  >>> tf.gather_nd(
  ...     batch_dims=1,
  ...     indices = [[1],
  ...                [0],
  ...                [4],
  ...                [2],
  ...                [1]],
  ...     params=tf.zeros([5, 7, 3])).shape.as_list()
  [5, 3]

  This is equivalent to caling a separate `gather_nd` for each location in the
  batch dimensions.


  >>> params=tf.zeros([5, 7, 3])
  >>> indices=tf.zeros([5, 1])
  >>> batch_dims = 1
  >>>
  >>> index_depth = indices.shape[-1]
  >>> batch_shape = indices.shape[:batch_dims]
  >>> assert params.shape[:batch_dims] == batch_shape
  >>> outer_shape = indices.shape[batch_dims:-1]
  >>> assert index_depth <= params.shape.rank
  >>> inner_shape = params.shape[batch_dims + index_depth:]
  >>> output_shape = batch_shape + outer_shape + inner_shape
  >>> output_shape.as_list()
  [5, 3]

  ### More examples

  Indexing into a 3-tensor:

  >>> tf.gather_nd(
  ...     indices = [[1]],
  ...     params = [[['a0', 'b0'], ['c0', 'd0']],
  ...               [['a1', 'b1'], ['c1', 'd1']]]).numpy()
  array([[[b'a1', b'b1'],
          [b'c1', b'd1']]], dtype=object)



  >>> tf.gather_nd(
  ...     indices = [[0, 1], [1, 0]],
  ...     params = [[['a0', 'b0'], ['c0', 'd0']],
  ...               [['a1', 'b1'], ['c1', 'd1']]]).numpy()
  array([[b'c0', b'd0'],
         [b'a1', b'b1']], dtype=object)


  >>> tf.gather_nd(
  ...     indices = [[0, 0, 1], [1, 0, 1]],
  ...     params = [[['a0', 'b0'], ['c0', 'd0']],
  ...               [['a1', 'b1'], ['c1', 'd1']]]).numpy()
  array([b'b0', b'b1'], dtype=object)

  The examples below are for the case when only indices have leading extra
  dimensions. If both 'params' and 'indices' have leading batch dimensions, use
  the 'batch_dims' parameter to run gather_nd in batch mode.

  Batched indexing into a matrix:

  >>> tf.gather_nd(
  ...     indices = [[[0, 0]], [[0, 1]]],
  ...     params = [['a', 'b'], ['c', 'd']]).numpy()
  array([[b'a'],
         [b'b']], dtype=object)



  Batched slice indexing into a matrix:

  >>> tf.gather_nd(
  ...     indices = [[[1]], [[0]]],
  ...     params = [['a', 'b'], ['c', 'd']]).numpy()
  array([[[b'c', b'd']],
         [[b'a', b'b']]], dtype=object)


  Batched indexing into a 3-tensor:

  >>> tf.gather_nd(
  ...     indices = [[[1]], [[0]]],
  ...     params = [[['a0', 'b0'], ['c0', 'd0']],
  ...               [['a1', 'b1'], ['c1', 'd1']]]).numpy()
  array([[[[b'a1', b'b1'],
           [b'c1', b'd1']]],
         [[[b'a0', b'b0'],
           [b'c0', b'd0']]]], dtype=object)


  >>> tf.gather_nd(
  ...     indices = [[[0, 1], [1, 0]], [[0, 0], [1, 1]]],
  ...     params = [[['a0', 'b0'], ['c0', 'd0']],
  ...               [['a1', 'b1'], ['c1', 'd1']]]).numpy()
  array([[[b'c0', b'd0'],
          [b'a1', b'b1']],
         [[b'a0', b'b0'],
          [b'c1', b'd1']]], dtype=object)

  >>> tf.gather_nd(
  ...     indices = [[[0, 0, 1], [1, 0, 1]], [[0, 1, 1], [1, 1, 0]]],
  ...     params = [[['a0', 'b0'], ['c0', 'd0']],
  ...               [['a1', 'b1'], ['c1', 'd1']]]).numpy()
  array([[b'b0', b'b1'],
         [b'd0', b'c1']], dtype=object)


  Examples with batched 'params' and 'indices':

  >>> tf.gather_nd(
  ...     batch_dims = 1,
  ...     indices = [[1],
  ...                [0]],
  ...     params = [[['a0', 'b0'],
  ...                ['c0', 'd0']],
  ...               [['a1', 'b1'],
  ...                ['c1', 'd1']]]).numpy()
  array([[b'c0', b'd0'],
         [b'a1', b'b1']], dtype=object)


  >>> tf.gather_nd(
  ...     batch_dims = 1,
  ...     indices = [[[1]], [[0]]],
  ...     params = [[['a0', 'b0'], ['c0', 'd0']],
  ...               [['a1', 'b1'], ['c1', 'd1']]]).numpy()
  array([[[b'c0', b'd0']],
         [[b'a1', b'b1']]], dtype=object)

  >>> tf.gather_nd(
  ...     batch_dims = 1,
  ...     indices = [[[1, 0]], [[0, 1]]],
  ...     params = [[['a0', 'b0'], ['c0', 'd0']],
  ...               [['a1', 'b1'], ['c1', 'd1']]]).numpy()
  array([[b'c0'],
         [b'b1']], dtype=object)


  See also `tf.gather`.

  Args:
    params: A `Tensor`. The tensor from which to gather values.
    indices: A `Tensor`. Must be one of the following types: `int32`, `int64`.
      Index tensor.
    name: A name for the operation (optional).
    batch_dims: An integer or a scalar 'Tensor'. The number of batch dimensions.
    bad_indices_policy: A string. If `""` or `"DEFAULT"`, the default behavior
      is used (error on CPU and ignore on GPU). If `"IGNORE"`, the bad indices
      are ignored and 0 is stored in the corresponding output value. If
      `"ERROR"`, an error is raised. Accelerators generally don't support
      `"ERROR"`.

  Returns:
    A `Tensor`. Has the same type as `params`.
  r   )rZ  DEFAULT)r)   bad_indices_policyr9   )rZ  r)   r  )r   r   r  r   r}  r]  batch_gather_nd)r^  r   r)   rZ  r  batch_dims_s         r+   r}  r}  #  s    l **:6+[!J1_+?B""d7I  1_ gD11 -   $$
'9K s   A. ."BBc                 "    t        | ||||      S )N)r)   rZ  r  )r}  )r^  r   rZ  r)   r  s        r+   gather_nd_v2r  4  s     
 
+
 r,   c                    t        j                  |d| |g      5  t        j                  |d      }t        j                  | d      } t        |t              st        d|       |dk  rt        d      | j                  j                  }|j                  j                  }|||k\  rt        d	| d
|       |||k\  rt        d	| d|       |dk(  }|rt        | d      } t        |d      }d}t        |       }t        |      }	|d| }
t        j                  |
dg      }t        |      |z
  dz
  }|	|d }t        j                  |
d      }|D cg c]Y  }t        j                  t        j                   dt        j                  |t"        j$                        d      |j&                        [ }}|rt)        |dding }|D cg c]  }t+        |d       }}t-        t        j.                  |d            }t        |      }t+        |t1        |dd t3        ||j&                        |dd gd            }t1        d|dfd      }t5        ||      }t1        |g|	|d fd      }t+        ||      }t1        ||fd      }t7        j8                  | ||      }t        |      }t+        |t1        |
|dd fd            }|rt;        |d      }ddd       |S c c}w c c}w # 1 sw Y   S xY w)z,gather_nd implementation with batch support.BatchGatherNDr   r9   r^  z*Argument `batch_dims` must be an int; got r   z0tf.gather_nd does not allow negative batch_dims.Nrf  rg  rh  r   r   r   r  r  r  r   r   r  )r  )r  )r   rr   rA   r;   r  r   rI   r(   r   rD   r   r   r   r   unstackru   r-  r   rf   r   r  r$   r$  r  r   rz  tiler   r}  r   )r^  r   rZ  r)   r  params_ndimsrl  expandro  rn  batch_shape
batch_sizeindex_internal_ndimsindices_internal_shapebatch_dim_listrQ   
dim_ranges	mesh_list	flat_list
index_gridindex_grid_shape
tile_shape
flat_shaperw  out	out_shapes                             r+   r  r  E  sM    ~~dOfg->? R!##G)<G""69Fj#&B:,OPPA~IJJ<<%%LMM''M Z=%@1* >66C_F G GJ,$>1* >55AND E E 1_F 6*fG!,gj=L'NM{
+K"";4J=:59*:b9 %,,[qAN  
 	 	<#4#4Q#EqIMM	
J  9C*4t4I2;<Q%(<I<?00CDJ Z( !$)1A1G1GH $
 	

J 5t<1EJjJ7J*}Z['ABKJ7*5Lj,/b9G

!
!,>C
 c
I
#V[)AB-$@qI
JCCa ceR!f 
*W =cR!f 
*s,   D?K1AK'7K1K, C=K1'
K11K;tensor_scatter_updatetensor_scatter_nd_updatec                 4    t        j                  | |||      S )a%  Scatter `updates` into an existing tensor according to `indices`.

  This operation creates a new tensor by applying sparse `updates` to the
  input `tensor`. This is similar to an index assignment.

  ```
  # Not implemented: tensors cannot be updated inplace.
  tensor[indices] = updates
  ```

  If an out of bound index is found on CPU, an error is returned.

  > **WARNING**: There are some GPU specific semantics for this operation.
  >
  > - If an out of bound index is found, the index is ignored.
  > - The order in which updates are applied is nondeterministic, so the output
  >   will be nondeterministic if `indices` contains duplicates.

  This operation is very similar to `tf.scatter_nd`, except that the updates are
  scattered onto an existing tensor (as opposed to a zero-tensor). If the memory
  for the existing tensor cannot be re-used, a copy is made and updated.

  In general:

  * `indices` is an integer tensor - the indices to update in `tensor`.
  * `indices` has **at least two** axes, the last axis is the depth of the
    index vectors.
  * For each index vector in `indices` there is a corresponding entry in
    `updates`.
  * If the length of the index vectors matches the rank of the `tensor`, then
    the index vectors each point to scalars in `tensor` and each update is a
    scalar.
  * If the length of the index vectors is less than the rank of `tensor`, then
    the index vectors each point to the slices of `tensor` and shape of the updates
    must match that slice.

  Overall this leads to the following shape constraints:

  ```
  assert tf.rank(indices) >= 2
  index_depth = indices.shape[-1]
  batch_shape = indices.shape[:-1]
  assert index_depth <= tf.rank(tensor)
  outer_shape = tensor.shape[:index_depth]
  inner_shape = tensor.shape[index_depth:]
  assert updates.shape == batch_shape + inner_shape
  ```

  Typical usage is often much simpler than this general form, and it
  can be better understood starting with simple examples:

  ### Scalar updates

  The simplest usage inserts scalar elements into a tensor by index.
  In this case, the `index_depth` must equal the rank of the
  input `tensor`, slice each column of `indices` is an index into an axis of the
  input `tensor`.

  In this simplest case the shape constraints are:

  ```
  num_updates, index_depth = indices.shape.as_list()
  assert updates.shape == [num_updates]
  assert index_depth == tf.rank(tensor)`
  ```

  For example, to insert 4 scattered elements in a rank-1 tensor with
  8 elements.

  <div style="width:70%; margin:auto; margin-bottom:10px; margin-top:20px;">
  <img style="width:100%"
    src="https://www.tensorflow.org/images/ScatterNd1.png">
  </div>

  This scatter operation would look like this:

  >>> tensor = [0, 0, 0, 0, 0, 0, 0, 0]    # tf.rank(tensor) == 1
  >>> indices = [[1], [3], [4], [7]]       # num_updates == 4, index_depth == 1
  >>> updates = [9, 10, 11, 12]            # num_updates == 4
  >>> print(tf.tensor_scatter_nd_update(tensor, indices, updates))
  tf.Tensor([ 0 9  0 10  11  0  0 12], shape=(8,), dtype=int32)

  The length (first axis) of `updates` must equal the length of the `indices`:
  `num_updates`. This is the number of updates being inserted. Each scalar
  update is inserted into `tensor` at the indexed location.

  For a higher rank input `tensor` scalar updates can be inserted by using an
  `index_depth` that matches `tf.rank(tensor)`:

  >>> tensor = [[1, 1], [1, 1], [1, 1]]    # tf.rank(tensor) == 2
  >>> indices = [[0, 1], [2, 0]]           # num_updates == 2, index_depth == 2
  >>> updates = [5, 10]                    # num_updates == 2
  >>> print(tf.tensor_scatter_nd_update(tensor, indices, updates))
  tf.Tensor(
      [[ 1  5]
       [ 1  1]
       [10  1]], shape=(3, 2), dtype=int32)

  ### Slice updates

  When the input `tensor` has more than one axis scatter can be used to update
  entire slices.

  In this case it's helpful to think of the input `tensor` as being a two level
  array-of-arrays. The shape of this two level array is split into the
  `outer_shape` and the `inner_shape`.

  `indices` indexes into the outer level of the input tensor (`outer_shape`).
  and replaces the sub-array at that location with the corresponding item from
  the `updates` list. The shape of each update is `inner_shape`.

  When updating a list of slices the shape constraints are:

  ```
  num_updates, index_depth = indices.shape.as_list()
  outer_shape = tensor.shape[:index_depth]
  inner_shape = tensor.shape[index_depth:]
  assert updates.shape == [num_updates, inner_shape]
  ```

  For example, to update rows of a `(6, 3)` `tensor`:

  >>> tensor = tf.zeros([6, 3], dtype=tf.int32)

  Use an index depth of one.

  >>> indices = tf.constant([[2], [4]])     # num_updates == 2, index_depth == 1
  >>> num_updates, index_depth = indices.shape.as_list()

  The `outer_shape` is `6`, the inner shape is `3`:

  >>> outer_shape = tensor.shape[:index_depth]
  >>> inner_shape = tensor.shape[index_depth:]

  2 rows are being indexed so 2 `updates` must be supplied.
  Each update must be shaped to match the `inner_shape`.

  >>> # num_updates == 2, inner_shape==3
  >>> updates = tf.constant([[1, 2, 3],
  ...                        [4, 5, 6]])

  Altogether this gives:

  >>> tf.tensor_scatter_nd_update(tensor, indices, updates).numpy()
  array([[0, 0, 0],
         [0, 0, 0],
         [1, 2, 3],
         [0, 0, 0],
         [4, 5, 6],
         [0, 0, 0]], dtype=int32)

  #### More slice update examples

  A tensor representing a batch of uniformly sized video clips naturally has 5
  axes: `[batch_size, time, width, height, channels]`.

  For example:

  >>> batch_size, time, width, height, channels = 13,11,7,5,3
  >>> video_batch = tf.zeros([batch_size, time, width, height, channels])

  To replace a selection of video clips:
    * Use an `index_depth` of 1 (indexing the `outer_shape`: `[batch_size]`)
    * Provide updates each with a shape matching the `inner_shape`:
      `[time, width, height, channels]`.

  To replace the first two clips with ones:

  >>> indices = [[0],[1]]
  >>> new_clips = tf.ones([2, time, width, height, channels])
  >>> tf.tensor_scatter_nd_update(video_batch, indices, new_clips)

  To replace a selection of frames in the videos:

  * `indices` must have an `index_depth` of 2 for the `outer_shape`:
    `[batch_size, time]`.
  * `updates` must be shaped like a list of images.  Each update must have a
    shape, matching the `inner_shape`: `[width, height, channels]`.

  To replace the first frame of the first three video clips:

  >>> indices = [[0, 0], [1, 0], [2, 0]] # num_updates=3, index_depth=2
  >>> new_images = tf.ones([
  ...   # num_updates=3, inner_shape=(width, height, channels)
  ...   3, width, height, channels])
  >>> tf.tensor_scatter_nd_update(video_batch, indices, new_images)

  ### Folded indices

  In simple cases it's convenient to think of `indices` and `updates` as
  lists, but this is not a strict requirement. Instead of a flat `num_updates`,
  the `indices` and `updates` can be folded into a `batch_shape`. This
  `batch_shape` is all axes of the `indices`, except for the innermost
  `index_depth` axis.

  ```
  index_depth = indices.shape[-1]
  batch_shape = indices.shape[:-1]
  ```

  Note: The one exception is that the `batch_shape` cannot be `[]`. You can't
  update a single index by passing indices with shape `[index_depth]`.

  `updates` must have a matching `batch_shape` (the axes before `inner_shape`).

  ```
  assert updates.shape == batch_shape + inner_shape
  ```

  Note: The result is equivalent to flattening the `batch_shape` axes of
  `indices` and `updates`. This generalization just avoids the need
  for reshapes when it is more natural to construct "folded" indices and
  updates.

  With this generalization the full shape constraints are:

  ```
  assert tf.rank(indices) >= 2
  index_depth = indices.shape[-1]
  batch_shape = indices.shape[:-1]
  assert index_depth <= tf.rank(tensor)
  outer_shape = tensor.shape[:index_depth]
  inner_shape = tensor.shape[index_depth:]
  assert updates.shape == batch_shape + inner_shape
  ```

  For example, to draw an `X` on a `(5,5)` matrix start with these indices:

  >>> tensor = tf.zeros([5,5])
  >>> indices = tf.constant([
  ...  [[0,0],
  ...   [1,1],
  ...   [2,2],
  ...   [3,3],
  ...   [4,4]],
  ...  [[0,4],
  ...   [1,3],
  ...   [2,2],
  ...   [3,1],
  ...   [4,0]],
  ... ])
  >>> indices.shape.as_list()  # batch_shape == [2, 5], index_depth == 2
  [2, 5, 2]

  Here the `indices` do not have a shape of `[num_updates, index_depth]`, but a
  shape of `batch_shape+[index_depth]`.

  Since the `index_depth` is equal to the rank of `tensor`:

  * `outer_shape` is `(5,5)`
  * `inner_shape` is `()` - each update is scalar
  * `updates.shape` is `batch_shape + inner_shape == (5,2) + ()`

  >>> updates = [
  ...   [1,1,1,1,1],
  ...   [1,1,1,1,1],
  ... ]

  Putting this together gives:

  >>> tf.tensor_scatter_nd_update(tensor, indices, updates).numpy()
  array([[1., 0., 0., 0., 1.],
         [0., 1., 0., 1., 0.],
         [0., 0., 1., 0., 0.],
         [0., 1., 0., 1., 0.],
         [1., 0., 0., 0., 1.]], dtype=float32)

  Args:
    tensor: Tensor to copy/update.
    indices: Indices to update.
    updates: Updates to apply at the indices.
    name: Optional name for the operation.

  Returns:
    A new tensor with the given shape and updates applied according to the
    indices.
  r   r   updatesr)   )r   r  r  s       r+   r  r    s#    v 
	,	,WgD
B Br,   quantize_v2zN`tf.quantize_v2` is deprecated, please use `tf.quantization.quantize` instead.MIN_COMBINEDc
                    |d}n?|dk  r:| j                   j                  t        d      || j                   j                  z  }|	dk7  rt        j                  | |||||||||	
      S t        j                  | ||||||||	      S )Nr   r   2input should have known rank to use negative axis.{Gz?)Tr  r)   
round_moder  rG   ensure_minimum_range)r  r  r)   r  r  rG   )r(   r   rI   r   r  )
rB   	min_range	max_ranger  r  r)   r  r  rG   r  s
             r+   r  r    s    " 
\Dax{{ KLLEKKDT!$$
!1
3 
3 
	"	"		
 	r,   z.Please use `tf.quantization.quantize` instead.zquantization.quantizequantizec
                 ^    |	dk7  rt        | |||||||||	
      S t        | ||||||||	      S )zQuantize the input tensor.r  )r  r  r)   r  rG   r  )r  r  r)   r  rG   )r  )
rB   r  r  r  r  r  r)   r  rG   r  s
             r+   r  r    sa     T!	!1
3 
3 
	
 	r,   zquantization.dequantize
dequantizec           
         |d}n?|dk  r:| j                   j                  t        d      || j                   j                  z  }|dk\  s|rt        j                  | |||||||      S t        j                  | |||||      S )Nr   r   r  )r  r)   r  rG   r   )r  r)   r   )r(   r   rI   r   r  )rB   r  r  r  r)   rG   r  r   s           r+   r  r    s     
\Dax{{ KLLEKKD	QY,##!  
	!	!Y	4u
F Fr,   z$quantization.quantize_and_dequantizezThis Op has been deprecated, use`quantize_and_dequantize_v2` instead. To To simulate the V1 the behavior of tf.quantization.quantize_and_dequantize(...) use tf.grad_pass_through(tf.quantization.quantize_and_dequantize_v2)(...).c
                     |	d}	n?|	dk  r:| j                   j                  t        d      |	| j                   j                  z  }	t        j                  | ||||||||	|
      S )a  Quantizes then dequantizes a tensor.

  Args:
    input: A `Tensor` to quantize and dequantize.
    input_min: If range_given=True, the minimum input value, that needs to be
      represented in the quantized representation. If axis is specified, this
      should be a vector of minimum values for each slice along axis.
    input_max: If range_given=True, the maximum input value that needs to be
      represented in the quantized representation. If axis is specified, this
      should be a vector of maximum values for each slice along axis.
    signed_input: True if the quantization is signed or unsigned.
    num_bits: The bitwidth of the quantization.
    range_given: If true use `input_min` and `input_max` for the range of the
      input, otherwise determine min and max from the input `Tensor`.
    round_mode: Rounding mode when rounding from float values to quantized ones.
      one of ['HALF_TO_EVEN', 'HALF_UP']
    name: Optional name for the operation.
    narrow_range: If true, then the absolute value of the quantized minimum
      value is the same as the quantized maximum value, instead of 1 greater.
      i.e. for 8 bit quantization, the minimum value is -127 instead of -128.
    axis: Integer. If specified, refers to a dimension of the input tensor, such
      that quantization will be per slice along that dimension.

  Returns:
    A `Tensor`. Each element is the result of quantizing and dequantizing the
    corresponding element of `input`.
  r   r   r  		input_min	input_maxsigned_inputr  range_givenr  r  rG   r)   )r(   r   rI   r   quantize_and_dequantize_v2
rB   r  r  r  r  r  r  r)   r  rG   s
             r+   quantize_and_dequantizer  C  sw    ^ 
\Dax{{ KLLEKKD		1	1

 
r,   z'quantization.quantize_and_dequantize_v2c
                     |	d}	n?|	dk  r:| j                   j                  t        d      |	| j                   j                  z  }	t        j                  | ||||||||	|
      S )aZ
  Quantizes then dequantizes a tensor.

  Updates the gradient definition for quantization that is outside the range to
  be 0.To simulate the V1 the behavior of
  tf.quantization.quantize_and_dequantize(...) use
  tf.grad_pass_through(tf.quantization.quantize_and_dequantize_v2)(...).

  Example usage:

  ```python
  def getQuantizeOp(input):
      input_tensor = tf.placeholder(tf.float32, shape=[4, 4])
      net = tf.quantization.quantize_and_dequantize(input,
                                                    input_min=min_threshold,
                                                    input_max=max_threshold,
                                                    range_given=True)

  To simulate v1 behavior:

  def testDecomposeQuantizeDequantize(self):
      def f(input_tensor):
        return tf.quantization.quantize_and_dequantize_v2(input_tensor,
                                                          input_min = 5.0,
                                                          input_max= -10.0,
                                                          range_given=True)
      input_tensor = tf.placeholder(tf.float32, shape=[4, 4])
      net = tf.grad_pass_through(f)(input_tensor)
  ```

  Args:
    input: A `Tensor` to quantize and dequantize.
    input_min: If range_given=True, the minimum input value, that needs to be
      represented in the quantized representation. If axis is specified, this
      should be a vector of minimum values for each slice along axis.
    input_max: If range_given=True, the maximum input value that needs to be
      represented in the quantized representation. If axis is specified, this
      should be a vector of maximum values for each slice along axis.
    signed_input: True if the quantization is signed or unsigned.
    num_bits: The bitwidth of the quantization.
    range_given: If true use `input_min` and `input_max` for the range of the
      input, otherwise determine min and max from the input `Tensor`.
    round_mode: Rounding mode when rounding from float values to quantized ones.
      one of ['HALF_TO_EVEN', 'HALF_UP']
    name: Optional name for the operation.
    narrow_range: If true, then the absolute value of the quantized minimum
      value is the same as the quantized maximum value, instead of 1 greater.
      i.e. for 8 bit quantization, the minimum value is -127 instead of -128.
    axis: Integer. If specified, refers to a dimension of the input tensor, such
      that quantization will be per slice along that dimension.

  Returns:
    A `Tensor`. Each element is the result of quantizing and dequantizing the
    corresponding element of `input`.
  r   r   r  r  )r(   r   rI   r   quantize_and_dequantize_v4r  s
             r+   r  r    sw    F 
\Dax{{ KLLEKKD		1	1

 
r,   searchsortedleftc                 2   t        |       d   }t        |      d   }t        | d|g      }t        |d|g      }|dk(  rt        j                  ||||      }	n-|dk(  rt        j                  ||||      }	nt        d| d      t        |	t        |            S )a{	  Searches for where a value would go in a sorted sequence.

  This is not a method for checking containment (like python `in`).

  The typical use case for this operation is "binning", "bucketing", or
  "discretizing". The `values` are assigned to bucket-indices based on the
  **edges** listed in `sorted_sequence`. This operation
  returns the bucket-index for each value.

  >>> edges = [-1, 3.3, 9.1, 10.0]
  >>> values = [0.0, 4.1, 12.0]
  >>> tf.searchsorted(edges, values).numpy()
  array([1, 2, 4], dtype=int32)

  The `side` argument controls which index is returned if a value lands exactly
  on an edge:

  >>> seq = [0, 3, 9, 10, 10]
  >>> values = [0, 4, 10]
  >>> tf.searchsorted(seq, values).numpy()
  array([0, 2, 3], dtype=int32)
  >>> tf.searchsorted(seq, values, side="right").numpy()
  array([1, 2, 5], dtype=int32)

  The `axis` is not settable for this operation. It always operates on the
  innermost dimension (`axis=-1`). The operation will accept any number of
  outer dimensions. Here it is applied to the rows of a matrix:

  >>> sorted_sequence = [[0., 3., 8., 9., 10.],
  ...                    [1., 2., 3., 4., 5.]]
  >>> values = [[9.8, 2.1, 4.3],
  ...           [0.1, 6.6, 4.5, ]]
  >>> tf.searchsorted(sorted_sequence, values).numpy()
  array([[4, 1, 2],
         [0, 5, 4]], dtype=int32)

  Note: This operation assumes that `sorted_sequence` **is sorted** along the
  innermost axis, maybe using `tf.sort(..., axis=-1)`. **If the sequence is not
  sorted, no error is raised** and the content of the returned tensor is not well
  defined.

  Args:
    sorted_sequence: N-D `Tensor` containing a sorted sequence.
    values: N-D `Tensor` containing the search values.
    side: 'left' or 'right'; 'left' corresponds to lower_bound and 'right' to
      upper_bound.
    out_type: The output type (`int32` or `int64`).  Default is `tf.int32`.
    name: Optional name for the operation.

  Returns:
    An N-D `Tensor` the size of `values` containing the result of applying
    either lower_bound or upper_bound (depending on side) to each value.  The
    result is not a global index to the entire `Tensor`, but the index in the
    last dimension.

  Raises:
    ValueError: If the last dimension of `sorted_sequence >= 2^31-1` elements.
                If the total size of `values` exceeds `2^31 - 1` elements.
                If the first `N-1` dimensions of the two tensors don't match.
  r   rightr  zFArgument `side` must be either 'right' or 'left'. Received: `side` = 'z'.)rm   r$   r   upper_boundlower_boundrI   )
sorted_sequencer   siderh   r)   sequence_sizevalues_sizesorted_sequence_2d	values_2drd  s
             r+   r  r    s    F !1"5-v&r*+]0CDfr;/0)	W_&&'99h'+-Fv~&&'99h'+-F  ,,065 6 6	/	00r,   zimage.extract_patchesc                 6    t        j                  | |||||      S )a  Extract `patches` from `images`.

  This op collects patches from the input image, as if applying a
  convolution. All extracted patches are stacked in the depth (last) dimension
  of the output.

  Specifically, the op extracts patches of shape `sizes` which are `strides`
  apart in the input image. The output is subsampled using the `rates` argument,
  in the same manner as "atrous" or "dilated" convolutions.

  The result is a 4D tensor which is indexed by batch, row, and column.
  `output[i, x, y]` contains a flattened patch of size `sizes[1], sizes[2]`
  which is taken from the input starting at
  `images[i, x*strides[1], y*strides[2]]`.

  Each output patch can be reshaped to `sizes[1], sizes[2], depth`, where
  `depth` is `images.shape[3]`.

  The output elements are taken from the input at intervals given by the `rate`
  argument, as in dilated convolutions.

  The `padding` argument has no effect on the size of each patch, it determines
  how many patches are extracted. If `VALID`, only patches which are fully
  contained in the input image are included. If `SAME`, all patches whose
  starting point is inside the input are included, and areas outside the input
  default to zero.

  Example:

  ```
    n = 10
    # images is a 1 x 10 x 10 x 1 array that contains the numbers 1 through 100
    images = [[[[x * n + y + 1] for y in range(n)] for x in range(n)]]

    # We generate two outputs as follows:
    # 1. 3x3 patches with stride length 5
    # 2. Same as above, but the rate is increased to 2
    tf.image.extract_patches(images=images,
                             sizes=[1, 3, 3, 1],
                             strides=[1, 5, 5, 1],
                             rates=[1, 1, 1, 1],
                             padding='VALID')

    # Yields:
    [[[[ 1  2  3 11 12 13 21 22 23]
       [ 6  7  8 16 17 18 26 27 28]]
      [[51 52 53 61 62 63 71 72 73]
       [56 57 58 66 67 68 76 77 78]]]]
  ```

  If we mark the pixels in the input image which are taken for the output with
  `*`, we see the pattern:

  ```
     *  *  *  4  5  *  *  *  9 10
     *  *  * 14 15  *  *  * 19 20
     *  *  * 24 25  *  *  * 29 30
    31 32 33 34 35 36 37 38 39 40
    41 42 43 44 45 46 47 48 49 50
     *  *  * 54 55  *  *  * 59 60
     *  *  * 64 65  *  *  * 69 70
     *  *  * 74 75  *  *  * 79 80
    81 82 83 84 85 86 87 88 89 90
    91 92 93 94 95 96 97 98 99 100
  ```

  ```
    tf.image.extract_patches(images=images,
                             sizes=[1, 3, 3, 1],
                             strides=[1, 5, 5, 1],
                             rates=[1, 2, 2, 1],
                             padding='VALID')

    # Yields:
    [[[[  1   3   5  21  23  25  41  43  45]
       [  6   8  10  26  28  30  46  48  50]]

      [[ 51  53  55  71  73  75  91  93  95]
       [ 56  58  60  76  78  80  96  98 100]]]]
  ```

  We can again draw the effect, this time using the symbols `*`, `x`, `+` and
  `o` to distinguish the patches:

  ```
     *  2  *  4  *  x  7  x  9  x
    11 12 13 14 15 16 17 18 19 20
     * 22  * 24  *  x 27  x 29  x
    31 32 33 34 35 36 37 38 39 40
     * 42  * 44  *  x 47  x 49  x
     + 52  + 54  +  o 57  o 59  o
    61 62 63 64 65 66 67 68 69 70
     + 72  + 74  +  o 77  o 79  o
    81 82 83 84 85 86 87 88 89 90
     + 92  + 94  +  o 97  o 99  o
  ```

  Args:
    images: A 4-D Tensor with shape `[batch, in_rows, in_cols, depth]`.
    sizes: The size of the extracted patches. Must be
      `[1, size_rows, size_cols, 1]`.
    strides: A 1-D Tensor of length 4. How far the centers of two consecutive
      patches are in the images. Must be: `[1, stride_rows, stride_cols, 1]`.
    rates: A 1-D Tensor of length 4. Must be: `[1, rate_rows, rate_cols, 1]`.
      This is the input stride, specifying how far two consecutive patch samples
      are in the input. Equivalent to extracting patches with `patch_sizes_eff =
      patch_sizes + (patch_sizes - 1) * (rates - 1)`, followed by subsampling
      them spatially by a factor of `rates`. This is equivalent to `rate` in
      dilated (a.k.a. Atrous) convolutions.
    padding: The type of padding algorithm to use.
    name: A name for the operation (optional).

  Returns:
    A 4-D Tensor of the same type as the input.
  )r   extract_image_patches)imagessizesr   ratesr  r)   s         r+   extract_image_patches_v2r  3  s%    l 
	,	,VUGU-4d
< <r,   zimage.extract_image_patchesr  z'ksizes is deprecated, use sizes insteadksizesc                 f    t        j                  d|d|      }t        j                  | |||||      S )a  Extract patches from images and put them in the "depth" output dimension.

  Args:
    `images`: A `Tensor`. Must be one of the following types: `float32`,
      `float64`, `int32`, `uint8`, `int16`, `int8`, `int64`, `bfloat16`,
      `uint16`, `half`, `uint32`, `uint64`. 4-D Tensor with shape
    `[batch, in_rows, in_cols, depth]`. `ksizes`: A list of `ints` that has
      length `>= 4`. The size of the sliding window for each
    dimension of `images`. `strides`: A list of `ints` that has length `>= 4`.
      1-D of length 4. How far the centers of two consecutive
    patches are in the images. Must be:
    `[1, stride_rows, stride_cols, 1]`. `rates`: A list of `ints`
    that has length `>= 4`. 1-D of length 4. Must be: `[1, rate_rows, rate_cols,
      1]`. This is the input stride, specifying how far two consecutive patch
      samples are in the input. Equivalent to extracting patches with
      `patch_sizes_eff = patch_sizes + (patch_sizes - 1) * (rates - 1)`,
      followed by subsampling them spatially by a factor of `rates`. This is
      equivalent to `rate` in dilated (a.k.a. Atrous) convolutions.
    `padding`: A `string` from: "SAME", "VALID". The type of padding algorithm
      to use.
    We specify the size-related attributes as:  ``` ksizes = [1, ksize_rows,
      ksize_cols, 1] strides = [1, strides_rows, strides_cols, 1] rates = [1,
      rates_rows, rates_cols, 1]
    name: A name for the operation (optional). ```

  Returns:
    A Tensor. Has the same type as images.
  r  r  )r   rH   r   r  )r  r  r   r  r  r)   r  s          r+   r  r    s>    P 11'5(28:&		,	,VVWe-4d
< <r,   fingerprintc                 0    t        j                  | ||      S )a~  Generates fingerprint values.

  Generates fingerprint values of `data`.

  Fingerprint op considers the first dimension of `data` as the batch dimension,
  and `output[i]` contains the fingerprint value generated from contents in
  `data[i, ...]` for all `i`.

  Fingerprint op writes fingerprint values as byte arrays. For example, the
  default method `farmhash64` generates a 64-bit fingerprint value at a time.
  This 8-byte value is written out as an `tf.uint8` array of size 8, in
  little-endian order.

  For example, suppose that `data` has data type `tf.int32` and shape (2, 3, 4),
  and that the fingerprint method is `farmhash64`. In this case, the output
  shape is (2, 8), where 2 is the batch dimension size of `data`, and 8 is the
  size of each fingerprint value in bytes. `output[0, :]` is generated from
  12 integers in `data[0, :, :]` and similarly `output[1, :]` is generated from
  other 12 integers in `data[1, :, :]`.

  Note that this op fingerprints the raw underlying buffer, and it does not
  fingerprint Tensor's metadata such as data type and/or shape. For example, the
  fingerprint values are invariant under reshapes and bitcasts as long as the
  batch dimension remain the same:

  ```python
  tf.fingerprint(data) == tf.fingerprint(tf.reshape(data, ...))
  tf.fingerprint(data) == tf.fingerprint(tf.bitcast(data, ...))
  ```

  For string data, one should expect `tf.fingerprint(data) !=
  tf.fingerprint(tf.string.reduce_join(data))` in general.

  Args:
    data: A `Tensor`. Must have rank 1 or higher.
    method: A `Tensor` of type `tf.string`. Fingerprint method used by this op.
      Currently, available method is `farmhash64`.
    name: A name for the operation (optional).

  Returns:
    A two-dimensional `Tensor` of type `tf.uint8`. The first dimension equals to
    `data`'s first dimension, and the second dimension size depends on the
    fingerprint algorithm.
  )r   r  )datamethodr)   s      r+   r  r    s    ^ 
	"	"4	66r,   c                     t        j                  | ||xs t        j                        } | j                  j
                  r|t        j                  | |      } | S t        d| d|  d| j                         )z.Converts the given value to an integer Tensor.)r)   rC  zArgument `tensor` (name: z/) must be of type integer. Received `tensor` = z of dtype: )	r   rA   r   rf   r   
is_integerr   ru   r   )r   r)   r   s      r+   convert_to_int_tensorr    s      4)>&,,@&\\  /f 
- /v 6,,28;v||nN O Or,   c                    t        | t              s#t        | dt        |       j                         |:d| cxk  r|k  r| S  | | cxk  r	dk  r| |z   S  t        | d|  d|  d| d| 	      | dk  rt        | d|  d| d      | S )	a  Validate an `axis` parameter, and normalize it to be positive.

  If `ndims` is known (i.e., not `None`), then check that `axis` is in the
  range `-ndims <= axis < ndims`, and return `axis` (if `axis >= 0`) or
  `axis + ndims` (otherwise).
  If `ndims` is not known, and `axis` is positive, then return it as-is.
  If `ndims` is not known, and `axis` is negative, then report an error.

  Args:
    axis: An integer constant
    ndims: An integer constant, or `None`
    axis_name: The name of `axis` (for error messages).
    ndims_name: The name of `ndims` (for error messages).

  Returns:
    The normalized `axis` value.

  Raises:
    ValueError: If `axis` is out-of-bounds, or if `axis` is negative and
      `ndims is None`.
  z must be an int; got r   =z out of bounds: expected z<=<z may only be negative if z is statically known.)r;   r  r   r   __name__rI   )rG   r   	axis_name
ndims_names       r+   get_positive_axisr    s    , 
D#	
yk!6tDz7J7J6KL
MM
D5k 
4	!	E\ 
 )AdV ,#$)6("YKqA B Bax
	{!D6 *%,&;= > >	+r,   c           
      ~   d}t        |t              s$t        d| dt        |      j                         t        j                  |d| |g      5  t        j                  | d      } |st        |d      }nt        |dd	      }|j                  j                  d
       t        |       } t        | |j                        }t        || j                  j                  d      }|j                  j                         d
k(  r`t!        |g       }t#        | |d
z         }t%        ||d
z   |      }t'        |d| |||   z  g||d
z   d gd      }t!        ||      cddd       S |j                  j(                  d
k(  r5| j                  j*                  |   j-                  |j                  d          t/        |||   g      }|s|}	|j                  j(                  |d
z   k7  rRt        |      }
t        |      }t'        |d|d
z   |z
   |
gd      }t/        ||      }|j1                  dg|d
z   z         t3        j4                  |t7        |            }t3        j8                  t        j                  dd|j                  	      |      }t;        ||      }t#        | |d
z         }t%        ||d
z   |      }t=        ||      }|dk(  r|}n[t3        j>                  |	t3        j@                  dt        |	      d
            }t'        |d| |g||d
z   d gd      }t!        ||      }| j                  j(                  c|j                  d   dk(  rdnd}|j1                  | j                  d| jC                  |g      jC                  | j                  |d
z   d              |cddd       S t3        jD                  |      }t'        tG        d
|j                        |gd      d   }t3        jH                  ||j                        }tK        ||d|j                        }tM        | ||      cddd       S # 1 sw Y   yxY w)aR  Repeats elements of `data`.

  Args:
    data: An `N`-dimensional tensor.
    repeats: A 1-D integer tensor specifying how many times each element in
      `axis` should be repeated.  `len(repeats)` must equal `data.shape[axis]`.
      Supports broadcasting from a scalar value.
    axis: `int`.  The axis along which to repeat values.  Must be less than
      `max(N, 1)`.
    name: A name for the operation.

  Returns:
    A tensor with `max(N, 1)` dimensions.  Has the same shape as `data`,
    except that dimension `axis` has size `sum(repeats)`.

  Example usage:

  >>> repeat(['a', 'b', 'c'], repeats=[3, 0, 2], axis=0)
  <tf.Tensor: shape=(5,), dtype=string,
  numpy=array([b'a', b'a', b'a', b'c', b'c'], dtype=object)>
  >>> repeat([[1, 2], [3, 4]], repeats=[2, 3], axis=0)
  <tf.Tensor: shape=(5, 2), dtype=int32, numpy=
  array([[1, 2],
         [1, 2],
         [3, 4],
         [3, 4],
         [3, 4]], dtype=int32)>
  >>> repeat([[1, 2], [3, 4]], repeats=[2, 3], axis=1)
  <tf.Tensor: shape=(2, 5), dtype=int32, numpy=
  array([[1, 1, 2, 2, 2],
         [3, 3, 4, 4, 4]], dtype=int32)>

  Fz2Argument `axis` must be an int. Received `axis` = z	 of type Repeatr  r9   repeatsNr   r   )rh   z
rank(data))r  r   r   rc  r   r   r  )r  rh   )'r;   r  r   r   r  r   rr   rA   r  r(   with_rank_at_most_with_nonzero_rankr   r  r   r   r$   rD   tile_one_dimensionr   r   r0   r   broadcast_tor  r   r2  r.  r3  r/  r   _sumr-  r   cumsumrX  r5  r  r   )r  r  rG   r)   $use_optimized_non_xla_implementation
data_shapeexpandedtiledresult_shaperepeats_originalrepeats_shaperepeats_ndimsra   
max_repeatr   maskedr*   repeated_dim_sizenew_axis_sizerepeats_scanoutput_sizeoutput_indicesgather_indicess                          r+   repeat_with_axisr  J  sE   L */&	D#	
 ))-iT
8K8K7LN O O ~~dHtWo6 h5  F3D 0%gI>g%gITJgMM##A& d#Dtgmm4J T4::??|LD }}!!#q($gT4!8,h 4!8W=e
Ud
g
4(889:dQhi;P "#$l UL)9h5 h5> }}a
jjood55gmmA6FG7Z%5$67G
 0  
		q	(gW 1M12MBLw84&D1H-.
  $$Wog.FGj''


j6F6F
G
j 7J/d T4!8,h 4!8Z@e E4(f 
(--$$Q-=(>BD !2 3Zq	5JK . 
			%$]]1-2ET*66O([D1HI)>?	A qh5 h5| "((1l E!<+=+=>M !##%'k $))+W]]Kn#
Ww}}NnD.t4Qh5 h5 h5s   DP3"H:P3&BP33P<c                     | j                   j                   dg| j                   j                  z  }|||<   n=t        t        |       t        j
                        }t        |d| |g||dz   d gd      }t        | |      S )z%Tiles a single dimension of a tensor.Nr   r   r   )r(   r   rz  r   r   rf   r   r  )r  rG   r  r  
ones_values        r+   r  r    s~     
ZZ!djj&&&IIdOd4j&,,/J
5D)H:z$()7LMI	dI	r,   c                     | j                   j                  1| j                   j                  dk(  rt        j                  | g      S | S t        |       }t	        |       }t        | t        dg|gd      | d       S )zBIf `data` is scalar, then add a dimension; otherwise return as-is.Nr   r   r   )r(   r   r   r  r   r$   r   )r  r  
data_ndimss      r+   r  r    sr    	ZZ!zz1""D6**ktJdJ4!j 1:J;<HIIr,   repeatc                 @    |t        | dg      } d}t        | |||      S )a  Repeat elements of `input`.

  See also `tf.concat`, `tf.stack`, `tf.tile`.

  Args:
    input: An `N`-dimensional Tensor.
    repeats: An 1-D `int` Tensor. The number of repetitions for each element.
      repeats is broadcasted to fit the shape of the given axis. `len(repeats)`
      must equal `input.shape[axis]` if axis is not None.
    axis: An int. The axis along which to repeat values. By default, (axis=None),
      use the flattened input array, and return a flat output array.
    name: A name for the operation.

  Returns:
    A Tensor which has the same shape as `input`, except along the given axis.
      If axis is None then the output array is flattened to match the flattened
      input array.

  Example usage:

  >>> repeat(['a', 'b', 'c'], repeats=[3, 0, 2], axis=0)
  <tf.Tensor: shape=(5,), dtype=string,
  numpy=array([b'a', b'a', b'a', b'c', b'c'], dtype=object)>

  >>> repeat([[1, 2], [3, 4]], repeats=[2, 3], axis=0)
  <tf.Tensor: shape=(5, 2), dtype=int32, numpy=
  array([[1, 2],
         [1, 2],
         [3, 4],
         [3, 4],
         [3, 4]], dtype=int32)>

  >>> repeat([[1, 2], [3, 4]], repeats=[2, 3], axis=1)
  <tf.Tensor: shape=(2, 5), dtype=int32, numpy=
  array([[1, 1, 2, 2, 2],
         [3, 3, 4, 4, 4]], dtype=int32)>

  >>> repeat(3, repeats=4)
  <tf.Tensor: shape=(4,), dtype=int32, numpy=array([3, 3, 3, 3], dtype=int32)>

  >>> repeat([[1,2], [3,4]], repeats=2)
  <tf.Tensor: shape=(8,), dtype=int32,
  numpy=array([1, 1, 2, 2, 3, 3, 4, 4], dtype=int32)>

  r   r   )r$   r  )rB   r  rG   r)   s       r+   r  r    s.    ` 
\EB4 ED	%$	55r,   guarantee_constzNot for public use.c                 0    t        j                  | |      S )a4  Promise to the TF runtime that the input tensor is a constant.

  The runtime is then free to make optimizations based on this.

  Returns the input tensor without modification.

  Args:
    input: A `Tensor`.
    name: A name for this operation.

  Returns:
    A `Tensor`. Has the same dtype as `input`.
  r   )r   r  r   s     r+   r  r  1  s      
	&	&U	>>r,   stop_gradientc                    t        | t        j                        r1t        j                  |       st        j                  t        | d      S t        j                         5  t        j                  | |      cddd       S # 1 sw Y   yxY w)a\  Stops gradient computation.

  NOTE: This docstring is patched out below. See
  tensorflow/core/api_def/base_api/api_def_StopGradient.pbtxt for the full
  docstring. That file determines the public documentation page.

  Args:
    input: A `Tensor`.
    name: A name for this operation.

  Returns:
    A `Tensor`. Has the same dtype as `input`.
  Tr6   r9   N)r;   r   r<   r   r=   r    r>   r  r   stop_recordingr   r   s     r+   r  r  D  sk    $ (889

*
*5
1mUdKK  9&&u489 9 9s    BB
rN   )NN)NNN)NTNrP  )Nr   r   r   r   r   NN)r   )NNF)r   )r   N)Nr   )r   Nr  )NFr$  )Nr$  F)r2  F)diagr   r   r   r   
RIGHT_LEFT)rC  r   r   r  )set_diagr   r  )NNT)TN)r  r   N)r  Nr   )Tr  )NNHWC)r  N)NNNNN)NNNr   )NNr   N)Nr   rZ  )r   NrZ  )NrZ  )r  NHALF_AWAY_FROM_ZEROFNr  )r  r  NFNr  )T   FHALF_TO_EVENNFN)NNNNNN)
farmhash64N)rG   r   )__doc__r  numpyr   tensorflow.core.configr   tensorflow.dtensor.pythonr   r2   tensorflow.python.eagerr   r   tensorflow.python.frameworkr   r   r	   r
   r   r   r   r   r   r   ra  r   r   r   'tensorflow.python.framework.constant_opr   tensorflow.python.opsr   r   r   r   r   #tensorflow.python.ops.gen_array_opsr   reversetensorflow.python.typesr   tensorflow.python.utilr   r   r   r    r!    tensorflow.python.util.tf_exportr"   r#   export_constantr  add_dispatch_supportr$   r-   r4   deprecated_argsrD   rJ   
deprecatedrT   rP   rf   rV   rY   r_   ri   r(   rm   r|   r   r~   r   r   r   r   r   r   r   r   r   setr  floatcomplexr:  bytesr   
memoryviewbool_	complex64clongdouble
complex128float16r&  float64
longdoubleint8int16re   longlongtimedelta64
datetime64object_bytes_str_uint8uint16uint32uint64	ulonglongvoidr   addndarrayr   r   #register_tensor_conversion_functionr   r   r   r   r
  deprecated_endpointsr  r  r  r  r(  r$  r2  r8  rB  rG  rH  rM  rW  rX  register_unary_elementwise_apire  rm  rt  rh  r   r{  rx  rz  r  r  r  r  r  r  r  NEW_AXISSHRINK_AXISr  	Operationr  r  RegisterGradientr  r  r  r  r  r  r  r	  r  r
  r  r  r  r  r   r!  r.  r/  r   r=  r>  r   rM  rW  r   r[  r`  rd  r}  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  check_numericsr   r,   r+   <module>r;     se    (   ( 2 + * 5 8 3 B . . 6 + 5 < B 4 3 < 1 / . , 9 2 E ( 0 . + ' / 6  	)  $ $Xy 9 9)_56	H  7HV 6	,  ,^ :	;  ;~ }o	T#DeLD+ M  D+N =R 	A6  !A6R ;<6<6 !**22T9H<L<LL  AB {m	 &4 $:  B
$:N "++33	  $%	8  &8@ #$	9  %9D 7r	8&  8&v wi	G  G<&FR 9	"LLt D  D8 6b	#%  #%L vh	"F  "FJ #TFLL #EL 6	3  3D22 7	2>  2>l ?	  ! !#$G  GT 	9I  9Iz.b*    	    		 
 
      HH  LL  NN  MM  JJ  JJ  JJ  MM   GG! " HH# $ HH% & HH' ( KK) * NN+ , MM- . JJ/ 0 II1 2 GG3 4 HH5 6 II7 8 II9 : II; < LL= > GG?    B   BJJ 'P9( ?  > >	5M3R9 8	ZF  ZFz ~	Y.   Y.x >b!	70  "70t =m];<!!!-0'P 1 ='PT 8	ll (0  (0V %%--  	"(,,T -<  !-<` +==EE   7	MQ  MQ` ;2	LC  LC^ {m	S,  S,n JL 
!!!"46HIH8 J L
H8V =m];<	!!!-0"c 1  =cL #57I"JK	!!!"45 

IM 6  LIMX #<k"JL	!!!+. 
&9 / L&9R "35F!GH	!!!"34 

CC 5  ICCR	3 7	~~D 8   8v |n	((	"8  ) "8J <B	((	 		1K  )  1Kj IM)JX   {m	((	7  ) 7> ;2	((	 		0  ) 0f 6	nn4 6  6r }o3H 3Hl )*+0D ,0Df #%9:;!!!"67k 8 <k` 5R	6<  6<r ug	_  _D, :	M+  M+` 
%P3s}} 3$ ?	d  dN /00 0 10 /00 0 10 9:03== 0 ;0 /0 S]]   1  340 0 50
 -. 48*.K) /K)\ "$456	!!!"23 	 4  7$ '55==  0R8	?  9? *;;CC   "$456	!!!"23Q 4  7Q '55==  2&	Q  'Q *88@@   "$456	!!!"23Q 4  7Q '55==  2&	Q  'Q *88@@    !	
  "
 '55==  #	ED  $EDP 9	 ^'  ^'B, ?	"&fkk >.  >.B yk	T#D+-02-  02f 9	G$  G$T wi	LG  LG^ 7
|$	OG  %OGf !"#TJ&( TN(*
 # $!#8*( $8v "%	 "&#'!	2  &2n xj	T2 8	9 !NE9  NEb 8	  $   NN	  ~	 67N7   
Nxv {-./	'(K )  0K\ ;2	AC	  	 !((  @BWt "!!"9:
"$;<> 
WB > ;
WBz }o	 
	$	'	  'T K  "(?'LM	!!!*- 
$		! .  N!H $*C*6*8 9	!!!,/
 
		
..F 0 9F< #--55
  12	LM 		7M  37t 45	
 		R  6Rj >	  ,,	N1  N1b !,,44  "#	u<  $u<p ,.EFG	T#L%' 
	
'<'  H'<T !. C C K K   =	-7  -7` /5ll 
#ZT5n

J 8	16  16h 34? 5 ?" ?	9  9> &33;;  ( ' '(D(D Er,   