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_zeroslike_op.py 5.8 kB

5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. # Copyright 2019 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. import mindspore.context as context
  18. import mindspore.nn as nn
  19. from mindspore import Tensor
  20. from mindspore.ops import operations as P
  21. from mindspore.ops.operations import _inner_ops as inner
  22. context.set_context(mode=context.PYNATIVE_MODE, device_target="GPU")
  23. class NetZerosLike(nn.Cell):
  24. def __init__(self):
  25. super(NetZerosLike, self).__init__()
  26. self.zeros_like = P.ZerosLike()
  27. def construct(self, x):
  28. return self.zeros_like(x)
  29. @pytest.mark.level0
  30. @pytest.mark.platform_x86_gpu_training
  31. @pytest.mark.env_onecard
  32. def test_ZerosLike():
  33. x0_np = np.random.uniform(-2, 2, (2, 3, 4, 4)).astype(np.float32)
  34. x1_np = np.random.uniform(-2, 2, 1).astype(np.float32)
  35. x0 = Tensor(x0_np)
  36. x1 = Tensor(x1_np)
  37. context.set_context(mode=context.PYNATIVE_MODE, device_target="GPU")
  38. zeros_like = NetZerosLike()
  39. output0 = zeros_like(x0)
  40. expect0 = np.zeros_like(x0_np)
  41. diff0 = output0.asnumpy() - expect0
  42. error0 = np.ones(shape=expect0.shape) * 1.0e-5
  43. assert np.all(diff0 < error0)
  44. assert output0.shape == expect0.shape
  45. output1 = zeros_like(x1)
  46. expect1 = np.zeros_like(x1_np)
  47. diff1 = output1.asnumpy() - expect1
  48. error1 = np.ones(shape=expect1.shape) * 1.0e-5
  49. assert np.all(diff1 < error1)
  50. assert output1.shape == expect1.shape
  51. context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
  52. zeros_like = NetZerosLike()
  53. output0 = zeros_like(x0)
  54. expect0 = np.zeros_like(x0_np)
  55. diff0 = output0.asnumpy() - expect0
  56. error0 = np.ones(shape=expect0.shape) * 1.0e-5
  57. assert np.all(diff0 < error0)
  58. assert output0.shape == expect0.shape
  59. output1 = zeros_like(x1)
  60. expect1 = np.zeros_like(x1_np)
  61. diff1 = output1.asnumpy() - expect1
  62. error1 = np.ones(shape=expect1.shape) * 1.0e-5
  63. assert np.all(diff1 < error1)
  64. assert output1.shape == expect1.shape
  65. class ZerosLikeDynamicNet(nn.Cell):
  66. def __init__(self):
  67. super(ZerosLikeDynamicNet, self).__init__()
  68. self.gpu_convert_to_dynamic_shape = inner.GpuConvertToDynamicShape()
  69. self.zeros_like = P.ZerosLike()
  70. def construct(self, x):
  71. converted_to_dynamic = self.gpu_convert_to_dynamic_shape(x)
  72. return self.zeros_like(converted_to_dynamic)
  73. def zeros_like_dynamic(x):
  74. context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
  75. net = ZerosLikeDynamicNet()
  76. return net(x)
  77. @pytest.mark.level0
  78. @pytest.mark.platform_x86_gpu_training
  79. @pytest.mark.env_onecard
  80. def test_zeros_like_dynamic_bool():
  81. x = Tensor(np.arange(120).reshape(3, 4, 1, 2, 5).astype(np.bool))
  82. output = zeros_like_dynamic(x)
  83. expected = np.zeros([3, 4, 1, 2, 5])
  84. np.testing.assert_array_equal(output.asnumpy(), expected)
  85. @pytest.mark.level0
  86. @pytest.mark.platform_x86_gpu_training
  87. @pytest.mark.env_onecard
  88. def test_zeros_like_dynamic_int8():
  89. x = Tensor(np.arange(24).reshape(1, 4, 1, 6).astype(np.int8))
  90. output = zeros_like_dynamic(x)
  91. expected = np.zeros([1, 4, 1, 6])
  92. print(output)
  93. np.testing.assert_array_equal(output.asnumpy(), expected)
  94. @pytest.mark.level0
  95. @pytest.mark.platform_x86_gpu_training
  96. @pytest.mark.env_onecard
  97. def test_zeros_like_dynamic_uint8():
  98. x = Tensor(np.arange(30).reshape(3, 2, 5).astype(np.uint8))
  99. output = zeros_like_dynamic(x)
  100. expected = np.zeros([3, 2, 5])
  101. np.testing.assert_array_equal(output.asnumpy(), expected)
  102. @pytest.mark.level0
  103. @pytest.mark.platform_x86_gpu_training
  104. @pytest.mark.env_onecard
  105. def test_zeros_like_dynamic_int32():
  106. x = Tensor(np.arange(16).reshape(2, 2, 2, 2).astype(np.int32))
  107. output = zeros_like_dynamic(x)
  108. expected = np.zeros([2, 2, 2, 2])
  109. np.testing.assert_array_equal(output.asnumpy(), expected)
  110. @pytest.mark.level0
  111. @pytest.mark.platform_x86_gpu_training
  112. @pytest.mark.env_onecard
  113. def test_zeros_like_dynamic_float16():
  114. x = Tensor(np.arange(120).reshape(3, 4, 1, 2, 5).astype(np.float16))
  115. output = zeros_like_dynamic(x)
  116. expected = np.zeros([3, 4, 1, 2, 5])
  117. np.testing.assert_array_almost_equal(output.asnumpy(), expected)
  118. @pytest.mark.level0
  119. @pytest.mark.platform_x86_gpu_training
  120. @pytest.mark.env_onecard
  121. def test_zeros_like_dynamic_float32():
  122. x = Tensor(np.arange(63).reshape(3, 7, 3).astype(np.float32))
  123. output = zeros_like_dynamic(x)
  124. expected = np.zeros([3, 7, 3])
  125. np.testing.assert_array_almost_equal(output.asnumpy(), expected)
  126. @pytest.mark.level0
  127. @pytest.mark.platform_x86_gpu_training
  128. @pytest.mark.env_onecard
  129. def test_zeros_like_dynamic_multiple_inputs():
  130. net = ZerosLikeDynamicNet()
  131. x = Tensor(np.arange(4).reshape(4).astype(np.float32))
  132. output = net(x)
  133. expected = np.zeros([4])
  134. np.testing.assert_array_almost_equal(output.asnumpy(), expected)
  135. x = Tensor(np.arange(8).reshape(2, 1, 2, 2).astype(np.uint8))
  136. output = net(x)
  137. expected = np.zeros([2, 1, 2, 2])
  138. np.testing.assert_array_equal(output.asnumpy(), expected)
  139. x = Tensor(np.arange(1).reshape(1).astype(np.float16))
  140. output = net(x)
  141. expected = np.zeros([1])
  142. np.testing.assert_array_almost_equal(output.asnumpy(), expected)