parameters: (inp: Tensor, axis: int, index: Tensor, source: Tensor)
return_statement: return inp
docstring: r"""
      Writes all values from the tensor source into input tensor
      at the indices specified in the index tensor.
  
      For each value in source, its output index is specified by its index
      in source for ``axis != dimension`` and by the corresponding value in
      index for ``axis = dimension``.
  
      For a 3-D tensor, input tensor is updated as:
  
      .. code-block::
  
         inp[index[i][j][k]][j][k] = source[i][j][k]  # if axis == 0
         inp[i][index[i][j][k]][k] = source[i][j][k]  # if axis == 1
         inp[i][j][index[i][j][k]] = source[i][j][k]  # if axis == 2
  
      ``inp``, ``index`` and ``source`` should have same number of dimensions.
  
      It is also required that ``source.shape(d) <= inp.shape(d)`` and ``index.shape(d) == source.shape(d)``
      for all dimensions ``d``.
  
      Moreover, the values of index must be between ``0`` and ``inp.shape(axis) - 1`` inclusive.
  
      Note:
          Please notice that, due to performance issues, the result is uncertain on the GPU device
          if scattering different positions from source to the same destination position
          regard to index tensor.
  
          Check the following examples, the oup[0][2] is maybe
          from source[0][2] which value is 0.2256 or source[1][2] which value is 0.5339
          if set the index[1][2] from 1 to 0.
  
      Args:
          inp: inp tensor which to be scattered.
          axis: axis along which to index.
          index: indices of elements to scatter.
          source: source element(s) to scatter.
  
      Return:
          output tensor.
  
      Examples:
  
          .. testcode::
  
              import numpy as np
              import megengine.functional as F
              from megengine import tensor
  
              inp = tensor(np.zeros(shape=(3,5),dtype=np.float32))
              source = tensor([[0.9935,0.9465,0.2256,0.8926,0.4396],[0.7723,0.0718,0.5939,0.357,0.4576]])
              index = tensor([[0,2,0,2,1],[2,0,1,1,2]])
              oup = F.scatter(inp, 0, index,source)
              print(oup.numpy())
  
          Outputs:
  
          .. testoutput::
  
              [[0.9935 0.0718 0.2256 0.     0.    ]
               [0.     0.     0.5939 0.357  0.4396]
               [0.7723 0.9465 0.     0.8926 0.4576]]
docstring_summary: r"""
      Writes all values from the tensor source into input tensor
      at the indices specified in the index tensor.
docstring_tokens: r Writes all values from the tensor source into input tensor at the indices specified in the index tensor .
function: def scatter(inp: Tensor, axis: int, index: Tensor, source: Tensor) -> Tensor:
      # TODO: rewrite doc
      r"""
      Writes all values from the tensor source into input tensor
      at the indices specified in the index tensor.
  
      For each value in source, its output index is specified by its index
      in source for ``axis != dimension`` and by the corresponding value in
      index for ``axis = dimension``.
  
      For a 3-D tensor, input tensor is updated as:
  
      .. code-block::
  
         inp[index[i][j][k]][j][k] = source[i][j][k]  # if axis == 0
         inp[i][index[i][j][k]][k] = source[i][j][k]  # if axis == 1
         inp[i][j][index[i][j][k]] = source[i][j][k]  # if axis == 2
  
      ``inp``, ``index`` and ``source`` should have same number of dimensions.
  
      It is also required that ``source.shape(d) <= inp.shape(d)`` and ``index.shape(d) == source.shape(d)``
      for all dimensions ``d``.
  
      Moreover, the values of index must be between ``0`` and ``inp.shape(axis) - 1`` inclusive.
  
      Note:
          Please notice that, due to performance issues, the result is uncertain on the GPU device
          if scattering different positions from source to the same destination position
          regard to index tensor.
  
          Check the following examples, the oup[0][2] is maybe
          from source[0][2] which value is 0.2256 or source[1][2] which value is 0.5339
          if set the index[1][2] from 1 to 0.
  
      Args:
          inp: inp tensor which to be scattered.
          axis: axis along which to index.
          index: indices of elements to scatter.
          source: source element(s) to scatter.
  
      Return:
          output tensor.
  
      Examples:
  
          .. testcode::
  
              import numpy as np
              import megengine.functional as F
              from megengine import tensor
  
              inp = tensor(np.zeros(shape=(3,5),dtype=np.float32))
              source = tensor([[0.9935,0.9465,0.2256,0.8926,0.4396],[0.7723,0.0718,0.5939,0.357,0.4576]])
              index = tensor([[0,2,0,2,1],[2,0,1,1,2]])
              oup = F.scatter(inp, 0, index,source)
              print(oup.numpy())
  
          Outputs:
  
          .. testoutput::
  
              [[0.9935 0.0718 0.2256 0.     0.    ]
               [0.     0.     0.5939 0.357  0.4396]
               [0.7723 0.9465 0.     0.8926 0.4576]]
      """
      input_shape = inp.shape
      index_shape = index.shape
      source_shape = source.shape
      input_dims = len(input_shape)
      index_dims = len(index_shape)
      source_dims = len(source_shape)
  
      if input_dims != index_dims or input_dims != source_dims:
          raise ValueError("The input, source and index tensor must have same dimensions")
  
      if axis < 0 or axis >= input_dims:
          raise ValueError(
              "Index axis {} is output of bounds, should in range [0 {})".format(
                  axis, input_dims
              )
          )
  
      for i in range(source_dims):
          if source_shape[i] > input_shape[i]:
              raise ValueError(
                  "The each shape size for source {} must be less than or equal to input {} ".format(
                      source_shape, input_shape
                  )
              )
  
      for i in range(index_dims):
          if index_shape[i] != source_shape[i]:
              raise ValueError(
                  "The each shape size for index {} must be equal to source {} ".format(
                      index_shape, source_shape
                  )
              )
  
      for i in range(index_dims):
          if i != axis and index_shape[i] > input_shape[i]:
              raise ValueError(
                  "The index {} must be less than or equal to input {} size apart from axis {}".format(
                      index_shape, input_shape, axis
                  )
              )
  
      idx = _get_idx(index, axis)
      inp[idx] = source.flatten()
      return inp
function_tokens: def scatter ( inp : Tensor , axis : int , index : Tensor , source : Tensor ) -> Tensor : # TODO: rewrite doc input_shape = inp . shape index_shape = index . shape source_shape = source . shape input_dims = len ( input_shape ) index_dims = len ( index_shape ) source_dims = len ( source_shape ) if input_dims != index_dims or input_dims != source_dims : raise ValueError ( "The input, source and index tensor must have same dimensions" ) if axis < 0 or axis >= input_dims : raise ValueError ( "Index axis {} is output of bounds, should in range [0 {})" . format ( axis , input_dims ) ) for i in range ( source_dims ) : if source_shape [ i ] > input_shape [ i ] : raise ValueError ( "The each shape size for source {} must be less than or equal to input {} " . format ( source_shape , input_shape ) ) for i in range ( index_dims ) : if index_shape [ i ] != source_shape [ i ] : raise ValueError ( "The each shape size for index {} must be equal to source {} " . format ( index_shape , source_shape ) ) for i in range ( index_dims ) : if i != axis and index_shape [ i ] > input_shape [ i ] : raise ValueError ( "The index {} must be less than or equal to input {} size apart from axis {}" . format ( index_shape , input_shape , axis ) ) idx = _get_idx ( index , axis ) inp [ idx ] = source . flatten ( ) return inp