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 5.0 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. import numpy as np
  2. import pytest
  3. from mindspore import Tensor
  4. from mindspore.ops import operations as P
  5. from mindspore.ops.operations import _inner_ops as inner
  6. import mindspore.nn as nn
  7. import mindspore.context as context
  8. def sequence_mask(x, maxlen):
  9. sequence_mask_op = P.SequenceMask()
  10. return sequence_mask_op(Tensor(x.astype(np.int32)), maxlen)
  11. @pytest.mark.level0
  12. @pytest.mark.platform_x86_gpu_training
  13. @pytest.mark.env_onecard
  14. def test_sequence_mask_1d():
  15. context.set_context(mode=context.PYNATIVE_MODE, device_target="GPU")
  16. a = np.array([2, 3, 1])
  17. maxlen = 4
  18. ms_out = sequence_mask(a, maxlen)
  19. expected_out = Tensor(np.array([[True, True, False, False],
  20. [True, True, True, False],
  21. [True, False, False, False]]))
  22. np.testing.assert_array_equal(expected_out.asnumpy(), ms_out.asnumpy())
  23. @pytest.mark.level0
  24. @pytest.mark.platform_x86_gpu_training
  25. @pytest.mark.env_onecard
  26. def test_sequence_mask_2d():
  27. context.set_context(mode=context.PYNATIVE_MODE, device_target="GPU")
  28. a = np.array([[0, 1, 3, 2], [1, 4, 4, 2]])
  29. maxlen = 6
  30. ms_out = sequence_mask(a, maxlen)
  31. expected_out = Tensor(np.array([[[False, False, False, False, False, False],
  32. [True, False, False, False, False, False],
  33. [True, True, True, False, False, False],
  34. [True, True, False, False, False, False]],
  35. [[True, False, False, False, False, False],
  36. [True, True, True, True, False, False],
  37. [True, True, True, True, False, False],
  38. [True, True, False, False, False, False]]]))
  39. np.testing.assert_array_equal(expected_out.asnumpy(), ms_out.asnumpy())
  40. @pytest.mark.level0
  41. @pytest.mark.platform_x86_gpu_training
  42. @pytest.mark.env_onecard
  43. def test_sequence_mask_3d():
  44. context.set_context(mode=context.PYNATIVE_MODE, device_target="GPU")
  45. a = np.array([[[2, 2], [1, 1]],
  46. [[2, 0], [2, 1]],
  47. [[0, 0], [0, 0]]])
  48. maxlen = 2
  49. ms_out = sequence_mask(a, maxlen)
  50. expected_out = Tensor(np.array([[[[True, True], [True, True]], [[True, False], [True, False]]],
  51. [[[True, True], [False, False]], [[True, True], [True, False]]],
  52. [[[False, False], [False, False]], [[False, False], [False, False]]]]))
  53. np.testing.assert_array_equal(expected_out.asnumpy(), ms_out.asnumpy())
  54. @pytest.mark.level0
  55. @pytest.mark.platform_x86_gpu_training
  56. @pytest.mark.env_onecard
  57. def test_sequence_mask_maxlen_1():
  58. context.set_context(mode=context.PYNATIVE_MODE, device_target="GPU")
  59. a = np.array([[[0, 1], [1, 1]],
  60. [[1, 0], [1, 1]],
  61. [[0, 1], [0, 1]]])
  62. maxlen = 1
  63. ms_out = sequence_mask(a, maxlen)
  64. expected_out = Tensor(np.array([[[[False], [True]], [[True], [True,]]],
  65. [[[True], [False]], [[True], [True]]],
  66. [[[False], [True]], [[False], [True]]]]))
  67. np.testing.assert_array_equal(expected_out.asnumpy(), ms_out.asnumpy())
  68. @pytest.mark.level0
  69. @pytest.mark.platform_x86_gpu_training
  70. @pytest.mark.env_onecard
  71. def test_sequence_mask_dynamic():
  72. class SequenceMaskDynamicNet(nn.Cell):
  73. def __init__(self, maxlen):
  74. super(SequenceMaskDynamicNet, self).__init__()
  75. self.maxlen = maxlen
  76. self.convert_to_dynamic_shape = inner.GpuConvertToDynamicShape()
  77. self.sequence_mask = P.SequenceMask()
  78. def construct(self, x):
  79. converted_to_dynamic_shape = self.convert_to_dynamic_shape(x)
  80. return self.sequence_mask(converted_to_dynamic_shape, self.maxlen)
  81. context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
  82. sequence_mask_net = SequenceMaskDynamicNet(4)
  83. a = Tensor(np.array([0, 1, 0, 2, 0, 5]))
  84. ms_out = sequence_mask_net(a)
  85. expected_out = Tensor(np.array([[False, False, False, False],
  86. [True, False, False, False],
  87. [False, False, False, False],
  88. [True, True, False, False],
  89. [False, False, False, False],
  90. [True, True, True, True]]))
  91. np.testing.assert_array_equal(expected_out.asnumpy(), ms_out.asnumpy())
  92. a = Tensor(np.array([[4, 3, 0], [0, 1, 3]]))
  93. ms_out = sequence_mask_net(a)
  94. expected_out = Tensor(np.array([[[True, True, True, True],
  95. [True, True, True, False],
  96. [False, False, False, False]],
  97. [[False, False, False, False],
  98. [True, False, False, False],
  99. [True, True, True, False]]]))