You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

array_ops.py 6.4 kB

5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. # Copyright 2020-2021 Huawei Technologies Co., Ltd
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. # ============================================================================
  15. """array Operations."""
  16. from mindspore.ops.composite.multitype_ops import _constexpr_utils as const_utils
  17. from mindspore.common import dtype as mstype
  18. from mindspore.common._register_for_tensor import tensor_operator_registry
  19. from mindspore._checkparam import Validator as validator
  20. from mindspore._checkparam import Rel
  21. from mindspore.ops.primitive import constexpr
  22. from mindspore.ops import functional as F
  23. from .. import operations as P
  24. @constexpr
  25. def _check_is_int(arg_value, arg_name, op_name):
  26. arg_value = validator.check_is_int(arg_value, arg_name, op_name)
  27. return arg_value
  28. @constexpr
  29. def _check_positive_int(arg_value, arg_name, op_name):
  30. arg_value = validator.check_positive_int(arg_value, arg_name, op_name)
  31. return arg_value
  32. @constexpr
  33. def _check_axis_range(arg_value, limit, arg_name, op_name):
  34. arg_value = validator.check_int_range(arg_value, -limit, limit, Rel.INC_LEFT, arg_name, op_name)
  35. return arg_value
  36. @constexpr
  37. def _cal_repeat_dims(x_rank, rep, expand_axis):
  38. rep_dims = [1] * (x_rank + 1)
  39. rep_dims[expand_axis] = rep
  40. return tuple(rep_dims)
  41. @constexpr
  42. def _cal_reshape(x_shape, rep, axis):
  43. x_reshape = list(x_shape)
  44. x_reshape[axis] *= rep
  45. return tuple(x_reshape)
  46. def repeat_elements(x, rep, axis=0):
  47. """
  48. Repeat elements of a tensor along an axis, like np.repeat.
  49. Args:
  50. x (Tensor): The tensor to repeat values for. Must be of type: float16,
  51. float32, int8, uint8, int16, int32, or int64.
  52. rep (int): The number of times to repeat, must be positive, required.
  53. axis (int): The axis along which to repeat, default 0.
  54. Outputs:
  55. One tensor with values repeated along the specified axis. If x has shape
  56. (s1, s2, ..., sn) and axis is i, the output will have shape (s1, s2, ...,
  57. si * rep, ..., sn). The output type will be the same as the type of `x`.
  58. Supported Platforms:
  59. ``Ascend`` ``GPU`` ``CPU``
  60. Examples:
  61. >>> x = Tensor(np.array([[0, 1, 2], [3, 4, 5]]), mindspore.int32)
  62. >>> output = C.repeat_elements(x, rep = 2, axis = 0)
  63. >>> print(output)
  64. [[0 1 2]
  65. [0 1 2]
  66. [3 4 5]
  67. [3 4 5]]
  68. """
  69. const_utils.check_type_valid(F.dtype(x), mstype.number_type, 'input x')
  70. rep = _check_positive_int(rep, "rep", "repeat_elements")
  71. axis = _check_is_int(axis, "axis", "repeat_elements")
  72. shape_op = P.Shape()
  73. rank_op = P.Rank()
  74. tile_op = P.Tile()
  75. expand_dims_op = P.ExpandDims()
  76. reshape_op = P.Reshape()
  77. x_rank = rank_op(x)
  78. axis = _check_axis_range(axis, x_rank, "axis", "repeat_elements")
  79. expand_axis = axis + 1
  80. x_expand = expand_dims_op(x, expand_axis)
  81. rep_dims = _cal_repeat_dims(x_rank, rep, expand_axis)
  82. x_expand = tile_op(x_expand, rep_dims)
  83. x_shape = shape_op(x)
  84. x_reshape = _cal_reshape(x_shape, rep, axis)
  85. x_rep = reshape_op(x_expand, x_reshape)
  86. return x_rep
  87. tensor_operator_registry.register('repeat_elements', repeat_elements)
  88. @constexpr
  89. def _check_sequence_mask_input_len(input_shape):
  90. if not input_shape:
  91. raise ValueError(f"Sequence_mask lengths_shape should be > 0. "
  92. f"Current lengths_shape is {input_shape}.")
  93. # broadcast only supports 7d shape
  94. shape_size = len(input_shape)
  95. if shape_size >= 7:
  96. raise ValueError(f"Sequence_mask lengths_shape's size only support a value less than 7. "
  97. f"Current lengths_shape is {shape_size}d.")
  98. def sequence_mask(lengths, maxlen=None):
  99. """
  100. Returns a mask tensor representing the first N positions of each cell.
  101. If lengths has shape [d_1, d_2, ..., d_n], then the resulting tensor mask has type dtype and shape
  102. [d_1, d_2, ..., d_n, maxlen], with mask[i_1, i_2, ..., i_n, j] = (j < lengths[i_1, i_2, ..., i_n])
  103. Inputs:
  104. - **lengths** (Tensor) - Tensor to calculate the mask for. All values in this tensor should be
  105. less than or equal to `maxlen`. Values greater than `maxlen` will be treated as `maxlen`.
  106. Must be type int32 or int64.
  107. - **maxlen** (int) - size of the last dimension of returned tensor. Must be positive and same
  108. type as elements in `lengths`.
  109. Outputs:
  110. One mask tensor of shape lengths.shape + (maxlen,).
  111. Raises:
  112. TypeError: If `lengths` is not a Tensor.
  113. TypeError: If `maxlen` is not an int.
  114. TypeError: If dtype of `lengths` is neither int32 nor int64.
  115. Supported Platforms:
  116. ``GPU``
  117. Examples:
  118. >>> x = Tensor(np.array([[1, 3], [2, 0]]))
  119. >>> output = C.sequence_mask(x, 3)
  120. >>> print(output)
  121. [[[True, False, False],
  122. [True, True, True]],
  123. [[True, True, False],
  124. [False, False, False]]]
  125. """
  126. argmax_op = P.ArgMaxWithValue()
  127. reshape_op = P.Reshape()
  128. range_op = P.Range()
  129. expand_op = P.ExpandDims()
  130. cast_op = P.Cast()
  131. shape_op = P.Shape()
  132. to_tensor_op = P.ScalarToArray()
  133. const_utils.check_type_valid(F.dtype(lengths), [mstype.int64, mstype.int32], 'lengths')
  134. _check_sequence_mask_input_len(shape_op(lengths))
  135. if maxlen is None:
  136. flatten_data = reshape_op(lengths, (-1,))
  137. flatten_data = cast_op(flatten_data, mstype.float32)
  138. _, value = argmax_op(flatten_data)
  139. maxlen = cast_op(value, mstype.int32)
  140. else:
  141. maxlen = _check_positive_int(maxlen, "maxlen", "sequence_mask")
  142. maxlen = to_tensor_op(maxlen)
  143. range_vector = range_op(to_tensor_op(0), maxlen
  144. , to_tensor_op(1))
  145. mask = expand_op(lengths, -1)
  146. result = range_vector < mask
  147. return result