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

5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. @ms_function
  37. def construct(self, indice):
  38. return (self.reduce_mean(indice, self.axis0),
  39. self.reduce_mean(indice, self.axis1),
  40. self.reduce_mean(indice, self.axis2),
  41. self.reduce_mean(indice, self.axis3),
  42. self.reduce_mean(indice, self.axis4),
  43. self.reduce_sum(indice, self.axis0),
  44. self.reduce_sum(indice, self.axis2),
  45. self.reduce_max(indice, self.axis0),
  46. self.reduce_max(indice, self.axis2),
  47. self.reduce_max(indice, self.axis5),
  48. self.reduce_max(indice, self.axis6))
  49. @pytest.mark.level0
  50. @pytest.mark.platform_x86_cpu
  51. @pytest.mark.env_onecard
  52. def test_reduce():
  53. reduce = NetReduce()
  54. indice = Tensor(np.array([
  55. [[0., 2., 1., 4., 0., 2.], [3., 1., 2., 2., 4., 0.]],
  56. [[2., 0., 1., 5., 0., 1.], [1., 0., 0., 4., 4., 3.]],
  57. [[4., 1., 4., 0., 0., 0.], [2., 5., 1., 0., 1., 3.]]
  58. ]).astype(np.float32))
  59. output = reduce(indice)
  60. print(output[0])
  61. print(output[1])
  62. print(output[2])
  63. print(output[3])
  64. print(output[4])
  65. print(output[5])
  66. print(output[6])
  67. print(output[7])
  68. print(output[8])
  69. print(output[9])
  70. print(output[10])
  71. expect_0 = np.array([[2., 1., 2., 3., 0., 1], [2., 2., 1., 2., 3., 2.]]).astype(np.float32)
  72. 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(
  73. np.float32)
  74. expect_2 = np.array([[1.5, 2.], [1.5, 2.], [1.5, 2.]]).astype(np.float32)
  75. expect_3 = np.array([2, 1.5, 1.5, 2.5, 1.5, 1.5]).astype(np.float32)
  76. expect_4 = np.array([1.75]).astype(np.float32)
  77. expect_5 = np.array([[6., 3., 6., 9., 0., 3.], [6., 6., 3., 6., 9., 6.]]).astype(np.float32)
  78. expect_6 = np.array([[9., 12.], [9., 12.], [9., 12.]]).astype(np.float32)
  79. expect_7 = np.array([[4., 2., 4., 5., 0., 2.], [3., 5., 2., 4., 4., 3.]]).astype(np.float32)
  80. expect_8 = np.array([[4., 4.], [5., 4.], [4., 5.]]).astype(np.float32)
  81. assert (output[0].asnumpy() == expect_0).all()
  82. assert (output[1].asnumpy() == expect_1).all()
  83. assert (output[2].asnumpy() == expect_2).all()
  84. assert (output[3].asnumpy() == expect_3).all()
  85. assert (output[4].asnumpy() == expect_4).all()
  86. assert (output[5].asnumpy() == expect_5).all()
  87. assert (output[6].asnumpy() == expect_6).all()
  88. assert (output[7].asnumpy() == expect_7).all()
  89. assert (output[8].asnumpy() == expect_8).all()
  90. assert (output[9].asnumpy() == expect_8).all()
  91. assert (output[10].asnumpy() == 5.0).all()
  92. test_reduce()