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.

test_sequence_mask_op.py 7.5 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. import numpy as np
  16. import pytest
  17. from mindspore import Tensor
  18. from mindspore.ops import composite as C
  19. from mindspore.ops.operations import _inner_ops as inner
  20. import mindspore.nn as nn
  21. import mindspore.context as context
  22. def sequence_mask(x, maxlen):
  23. return C.sequence_mask(Tensor(x.astype(np.int32)), maxlen)
  24. @pytest.mark.level0
  25. @pytest.mark.platform_x86_gpu_training
  26. @pytest.mark.env_onecard
  27. def test_sequence_mask_1d():
  28. context.set_context(mode=context.PYNATIVE_MODE, device_target="GPU")
  29. a = np.array([2, 3, 1])
  30. maxlen = 4
  31. ms_out = sequence_mask(a, maxlen)
  32. expected_out = Tensor(np.array([[True, True, False, False],
  33. [True, True, True, False],
  34. [True, False, False, False]]))
  35. np.testing.assert_array_equal(expected_out.asnumpy(), ms_out.asnumpy())
  36. @pytest.mark.level0
  37. @pytest.mark.platform_x86_gpu_training
  38. @pytest.mark.env_onecard
  39. def test_sequence_mask_2d():
  40. context.set_context(mode=context.PYNATIVE_MODE, device_target="GPU")
  41. a = np.array([[0, 1, 3, 2], [1, 4, 4, 2]])
  42. maxlen = 6
  43. ms_out = sequence_mask(a, maxlen)
  44. expected_out = Tensor(np.array([[[False, False, False, False, False, False],
  45. [True, False, False, False, False, False],
  46. [True, True, True, False, False, False],
  47. [True, True, False, False, False, False]],
  48. [[True, False, False, False, False, False],
  49. [True, True, True, True, False, False],
  50. [True, True, True, True, False, False],
  51. [True, True, False, False, False, False]]]))
  52. np.testing.assert_array_equal(expected_out.asnumpy(), ms_out.asnumpy())
  53. @pytest.mark.level0
  54. @pytest.mark.platform_x86_gpu_training
  55. @pytest.mark.env_onecard
  56. def test_sequence_mask_3d():
  57. context.set_context(mode=context.PYNATIVE_MODE, device_target="GPU")
  58. a = np.array([[[2, 2], [1, 1]],
  59. [[2, 0], [2, 1]],
  60. [[0, 0], [0, 0]]])
  61. maxlen = 2
  62. ms_out = sequence_mask(a, maxlen)
  63. expected_out = Tensor(np.array([[[[True, True], [True, True]], [[True, False], [True, False]]],
  64. [[[True, True], [False, False]], [[True, True], [True, False]]],
  65. [[[False, False], [False, False]], [[False, False], [False, False]]]]))
  66. np.testing.assert_array_equal(expected_out.asnumpy(), ms_out.asnumpy())
  67. @pytest.mark.level0
  68. @pytest.mark.platform_x86_gpu_training
  69. @pytest.mark.env_onecard
  70. def test_sequence_mask_maxlen_1():
  71. context.set_context(mode=context.PYNATIVE_MODE, device_target="GPU")
  72. a = np.array([[[0, 1], [1, 1]],
  73. [[1, 0], [1, 1]],
  74. [[0, 1], [0, 1]]])
  75. maxlen = 1
  76. ms_out = sequence_mask(a, maxlen)
  77. expected_out = Tensor(np.array([[[[False], [True]], [[True], [True,]]],
  78. [[[True], [False]], [[True], [True]]],
  79. [[[False], [True]], [[False], [True]]]]))
  80. np.testing.assert_array_equal(expected_out.asnumpy(), ms_out.asnumpy())
  81. @pytest.mark.level0
  82. @pytest.mark.platform_x86_gpu_training
  83. @pytest.mark.env_onecard
  84. def test_sequence_mask_dynamic():
  85. class SequenceMaskDynamicNet1(nn.Cell):
  86. def __init__(self, maxlen):
  87. super(SequenceMaskDynamicNet1, self).__init__()
  88. self.maxlen = maxlen
  89. self.convert_to_dynamic_shape = inner.GpuConvertToDynamicShape()
  90. def construct(self, x):
  91. converted_to_dynamic_shape = self.convert_to_dynamic_shape(x)
  92. return C.sequence_mask(converted_to_dynamic_shape, self.maxlen)
  93. class SequenceMaskDynamicNet2(nn.Cell):
  94. def __init__(self):
  95. super(SequenceMaskDynamicNet2, self).__init__()
  96. self.convert_to_dynamic_shape = inner.GpuConvertToDynamicShape()
  97. def construct(self, x):
  98. converted_to_dynamic_shape = self.convert_to_dynamic_shape(x)
  99. return C.sequence_mask(converted_to_dynamic_shape)
  100. context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
  101. sequence_mask_net = SequenceMaskDynamicNet1(4)
  102. a = Tensor(np.array([0, 1, 0, 2, 0, 5]))
  103. ms_out = sequence_mask_net(a)
  104. expected_out = Tensor(np.array([[False, False, False, False],
  105. [True, False, False, False],
  106. [False, False, False, False],
  107. [True, True, False, False],
  108. [False, False, False, False],
  109. [True, True, True, True]]))
  110. np.testing.assert_array_equal(expected_out.asnumpy(), ms_out.asnumpy())
  111. a = Tensor(np.array([[4, 3, 0], [0, 1, 3]]))
  112. ms_out = sequence_mask_net(a)
  113. expected_out = Tensor(np.array([[[True, True, True, True],
  114. [True, True, True, False],
  115. [False, False, False, False]],
  116. [[False, False, False, False],
  117. [True, False, False, False],
  118. [True, True, True, False]]]))
  119. np.testing.assert_array_equal(expected_out.asnumpy(), ms_out.asnumpy())
  120. net_without_maxlen = SequenceMaskDynamicNet2()
  121. context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
  122. a = np.array([2, 3, 1])
  123. ms_out = net_without_maxlen(Tensor(a))
  124. expected_out = Tensor(np.array([[True, True, False],
  125. [True, True, True],
  126. [True, False, False]]))
  127. np.testing.assert_array_equal(expected_out.asnumpy(), ms_out.asnumpy())
  128. def sequence_mask_optional(x):
  129. return C.sequence_mask(Tensor(x.astype(np.int32)))
  130. @pytest.mark.level0
  131. @pytest.mark.platform_x86_gpu_training
  132. @pytest.mark.env_onecard
  133. def test_sequence_mask_optional_maxlen():
  134. context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
  135. a = np.array([2, 3, 1])
  136. ms_out = sequence_mask_optional(a)
  137. expected_out = Tensor(np.array([[True, True, False],
  138. [True, True, True],
  139. [True, False, False]]))
  140. np.testing.assert_array_equal(expected_out.asnumpy(), ms_out.asnumpy())
  141. context.set_context(mode=context.PYNATIVE_MODE, device_target="GPU")
  142. a = np.array([2, 3, 1])
  143. ms_out = sequence_mask_optional(a)
  144. expected_out = Tensor(np.array([[True, True, False],
  145. [True, True, True],
  146. [True, False, False]]))
  147. np.testing.assert_array_equal(expected_out.asnumpy(), ms_out.asnumpy())