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_reduce_op.py 5.4 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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 pytest
  16. import numpy as np
  17. from mindspore import Tensor
  18. from mindspore.ops import operations as P
  19. import mindspore.nn as nn
  20. import mindspore.context as context
  21. from mindspore.common.api import ms_function
  22. context.set_context(device_target="CPU")
  23. class NetReduce(nn.Cell):
  24. def __init__(self):
  25. super(NetReduce, self).__init__()
  26. self.axis0 = 0
  27. self.axis1 = 1
  28. self.axis2 = -1
  29. self.axis3 = (0, 1)
  30. self.axis4 = (0, 1, 2)
  31. self.axis5 = (-1,)
  32. self.axis6 = ()
  33. self.reduce_mean = P.ReduceMean(False)
  34. self.reduce_sum = P.ReduceSum(False)
  35. self.reduce_max = P.ReduceMax(False)
  36. self.reduce_min = P.ReduceMin(False)
  37. @ms_function
  38. def construct(self, indice):
  39. return (self.reduce_mean(indice, self.axis0),
  40. self.reduce_mean(indice, self.axis1),
  41. self.reduce_mean(indice, self.axis2),
  42. self.reduce_mean(indice, self.axis3),
  43. self.reduce_mean(indice, self.axis4),
  44. self.reduce_sum(indice, self.axis0),
  45. self.reduce_sum(indice, self.axis2),
  46. self.reduce_max(indice, self.axis0),
  47. self.reduce_max(indice, self.axis2),
  48. self.reduce_max(indice, self.axis5),
  49. self.reduce_max(indice, self.axis6),
  50. self.reduce_min(indice, self.axis0),
  51. self.reduce_min(indice, self.axis1),
  52. self.reduce_min(indice, self.axis2),
  53. self.reduce_min(indice, self.axis3),
  54. self.reduce_min(indice, self.axis4),
  55. self.reduce_min(indice, self.axis5),
  56. self.reduce_min(indice, self.axis6))
  57. @pytest.mark.level0
  58. @pytest.mark.platform_x86_cpu
  59. @pytest.mark.env_onecard
  60. def test_reduce():
  61. reduce = NetReduce()
  62. indice = Tensor(np.array([
  63. [[0., 2., 1., 4., 0., 2.], [3., 1., 2., 2., 4., 0.]],
  64. [[2., 0., 1., 5., 0., 1.], [1., 0., 0., 4., 4., 3.]],
  65. [[4., 1., 4., 0., 0., 0.], [2., 5., 1., 0., 1., 3.]]
  66. ]).astype(np.float32))
  67. output = reduce(indice)
  68. print(output[0])
  69. print(output[1])
  70. print(output[2])
  71. print(output[3])
  72. print(output[4])
  73. print(output[5])
  74. print(output[6])
  75. print(output[7])
  76. print(output[8])
  77. print(output[9])
  78. print(output[10])
  79. print(output[11])
  80. print(output[12])
  81. print(output[13])
  82. print(output[14])
  83. print(output[15])
  84. print(output[16])
  85. print(output[17])
  86. expect_0 = np.array([[2., 1., 2., 3., 0., 1], [2., 2., 1., 2., 3., 2.]]).astype(np.float32)
  87. expect_1 = np.array([[1.5, 1.5, 1.5, 3., 2., 1.], [1.5, 0., 0.5, 4.5, 2., 2.], [3., 3., 2.5, 0., 0.5, 1.5]]).astype(
  88. np.float32)
  89. expect_2 = np.array([[1.5, 2.], [1.5, 2.], [1.5, 2.]]).astype(np.float32)
  90. expect_3 = np.array([2, 1.5, 1.5, 2.5, 1.5, 1.5]).astype(np.float32)
  91. expect_4 = np.array([1.75]).astype(np.float32)
  92. expect_5 = np.array([[6., 3., 6., 9., 0., 3.], [6., 6., 3., 6., 9., 6.]]).astype(np.float32)
  93. expect_6 = np.array([[9., 12.], [9., 12.], [9., 12.]]).astype(np.float32)
  94. expect_7 = np.array([[4., 2., 4., 5., 0., 2.], [3., 5., 2., 4., 4., 3.]]).astype(np.float32)
  95. expect_8 = np.array([[4., 4.], [5., 4.], [4., 5.]]).astype(np.float32)
  96. expect_9 = np.array([[0., 0., 1., 0., 0., 0.], [1., 0., 0., 0., 1., 0.]]).astype(np.float32)
  97. expect_10 = np.array([[0., 1., 1., 2., 0., 0.], [1., 0., 0., 4., 0., 1.], [2., 1., 1., 0., 0., 0.]]).astype(
  98. np.float32)
  99. expect_11 = np.array([[0., 0.], [0., 0.], [0., 0.]]).astype(np.float32)
  100. expect_12 = np.array([0., 0., 0., 0., 0., 0.]).astype(np.float32)
  101. assert (output[0].asnumpy() == expect_0).all()
  102. assert (output[1].asnumpy() == expect_1).all()
  103. assert (output[2].asnumpy() == expect_2).all()
  104. assert (output[3].asnumpy() == expect_3).all()
  105. assert (output[4].asnumpy() == expect_4).all()
  106. assert (output[5].asnumpy() == expect_5).all()
  107. assert (output[6].asnumpy() == expect_6).all()
  108. assert (output[7].asnumpy() == expect_7).all()
  109. assert (output[8].asnumpy() == expect_8).all()
  110. assert (output[9].asnumpy() == expect_8).all()
  111. assert (output[10].asnumpy() == 5.0).all()
  112. assert (output[11].asnumpy() == expect_9).all()
  113. assert (output[12].asnumpy() == expect_10).all()
  114. assert (output[13].asnumpy() == expect_11).all()
  115. assert (output[14].asnumpy() == expect_12).all()
  116. assert (output[15].asnumpy() == 0.0).all()
  117. assert (output[16].asnumpy() == expect_11).all()
  118. assert (output[17].asnumpy() == 0.0).all()
  119. test_reduce()