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_argmaxwithvalue_op.py 5.0 kB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. # Copyright 2020 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. class NetArgmaxWithValue(nn.Cell):
  22. def __init__(self):
  23. super(NetArgmaxWithValue, self).__init__()
  24. axis1 = 0
  25. axis2 = -1
  26. self.argmax1 = P.ArgMaxWithValue(axis1)
  27. self.argmax2 = P.ArgMaxWithValue(axis2)
  28. self.argmax3 = P.ArgMaxWithValue()
  29. def construct(self, x):
  30. return (self.argmax1(x), self.argmax2(x), self.argmax3(x))
  31. class NetArgmaxWithValueBig(nn.Cell):
  32. def __init__(self, axis=0):
  33. super(NetArgmaxWithValueBig, self).__init__()
  34. self.argmax = P.ArgMaxWithValue(axis)
  35. def construct(self, x):
  36. return self.argmax(x)
  37. def argmaxwithvalue_base(data_type):
  38. x = Tensor(np.array([[1., 20., 5.],
  39. [67., 8., 9.],
  40. [130., 24., 15.],
  41. [0.3, -0.4, -15.]]).astype(data_type))
  42. expect1 = np.array([2, 2, 2]).astype(data_type)
  43. expect2 = np.array([1, 0, 0, 0]).astype(data_type)
  44. expect11 = np.array([130, 24, 15]).astype(data_type)
  45. expect22 = np.array([20, 67, 130, 0.3]).astype(data_type)
  46. context.set_context(mode=context.PYNATIVE_MODE, device_target="GPU")
  47. argmax = NetArgmaxWithValue()
  48. output = argmax(x)
  49. assert (output[0][0].asnumpy() == expect1).all()
  50. assert (output[0][1].asnumpy() == expect11).all()
  51. assert (output[1][0].asnumpy() == expect2).all()
  52. assert (output[1][1].asnumpy() == expect22).all()
  53. assert (output[2][0].asnumpy() == expect1).all()
  54. assert (output[2][1].asnumpy() == expect11).all()
  55. context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
  56. argmax = NetArgmaxWithValue()
  57. output = argmax(x)
  58. assert (output[0][0].asnumpy() == expect1).all()
  59. assert (output[0][1].asnumpy() == expect11).all()
  60. assert (output[1][0].asnumpy() == expect2).all()
  61. assert (output[1][1].asnumpy() == expect22).all()
  62. assert (output[2][0].asnumpy() == expect1).all()
  63. assert (output[2][1].asnumpy() == expect11).all()
  64. def argmaxwithvalue_3d(data_type, shape_x):
  65. np.random.seed(2)
  66. x_np = np.random.random(shape_x).astype(data_type)
  67. x = Tensor(x_np)
  68. argmax = NetArgmaxWithValueBig(0)
  69. output = argmax(x)
  70. expect1 = np.argmax(x_np, axis=0)
  71. expect2 = np.maximum.reduce(x_np, 0)
  72. assert (output[0].asnumpy() == expect1).all()
  73. assert (output[1].asnumpy() == expect2).all()
  74. argmax = NetArgmaxWithValueBig(1)
  75. output = argmax(x)
  76. expect1 = np.argmax(x_np, axis=1)
  77. expect2 = np.maximum.reduce(x_np, 1)
  78. assert (output[0].asnumpy() == expect1).all()
  79. assert (output[1].asnumpy() == expect2).all()
  80. argmax = NetArgmaxWithValueBig(2)
  81. output = argmax(x)
  82. expect1 = np.argmax(x_np, axis=2)
  83. expect2 = np.maximum.reduce(x_np, 2)
  84. assert (output[0].asnumpy() == expect1).all()
  85. assert (output[1].asnumpy() == expect2).all()
  86. @pytest.mark.level0
  87. @pytest.mark.platform_x86_gpu_training
  88. @pytest.mark.env_onecard
  89. def test_argmaxwithvalue_base_float32():
  90. argmaxwithvalue_base(np.float32)
  91. @pytest.mark.level0
  92. @pytest.mark.platform_x86_gpu_training
  93. @pytest.mark.env_onecard
  94. def test_argmaxwithvalue_base_float16():
  95. argmaxwithvalue_base(np.float16)
  96. @pytest.mark.level0
  97. @pytest.mark.platform_x86_gpu_training
  98. @pytest.mark.env_onecard
  99. def test_argmaxwithvalue_3d_float32():
  100. shape_x = (2, 32, 256)
  101. context.set_context(mode=context.PYNATIVE_MODE, device_target="GPU")
  102. argmaxwithvalue_3d(np.float32, shape_x)
  103. context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
  104. argmaxwithvalue_3d(np.float32, shape_x)
  105. @pytest.mark.level0
  106. @pytest.mark.platform_x86_gpu_training
  107. @pytest.mark.env_onecard
  108. def test_argmaxwithvalue_3d_float16():
  109. shape_x = (2, 64, 128)
  110. context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
  111. argmaxwithvalue_3d(np.float16, shape_x)
  112. @pytest.mark.level0
  113. @pytest.mark.platform_x86_gpu_training
  114. @pytest.mark.env_onecard
  115. def test_argmaxwithvalue_3d_big_float32():
  116. shape_x = (128, 1024, 1)
  117. context.set_context(mode=context.PYNATIVE_MODE, device_target="GPU")
  118. argmaxwithvalue_3d(np.float32, shape_x)
  119. context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
  120. argmaxwithvalue_3d(np.float32, shape_x)