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_simplify.py 3.8 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. # Copyright 2020-2021 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. from mindspore import Tensor
  19. from mindspore.nn import Cell
  20. import mindspore.ops.operations as P
  21. class Net(Cell):
  22. def __init__(self):
  23. super(Net, self).__init__()
  24. self.add = P.Add()
  25. self.sub = P.Sub()
  26. self.mul = P.Mul()
  27. self.div = P.RealDiv()
  28. self.sqrt = P.Sqrt()
  29. self.pow = P.Pow()
  30. self.neg = P.Neg()
  31. self.reducemin = P.ReduceMin()
  32. self.reducesum = P.ReduceSum(keep_dims=True)
  33. self.reshape = P.Reshape()
  34. def construct(self, x, y):
  35. add_res1 = self.add(x, 4)
  36. add_res2 = self.add(add_res1, 5)
  37. sub_res = self.sub(y, 3)
  38. mul_res = self.mul(self.sqrt(add_res2), self.sqrt(sub_res))
  39. div_res = self.div(mul_res, self.sqrt(mul_res))
  40. pow_res = self.pow(y, 2)
  41. neg_res = self.neg(self.neg(pow_res))
  42. add_res3 = self.add(neg_res, div_res)
  43. resh_res = self.reshape(add_res3, (2, 12, 3))
  44. neg_res = self.neg(resh_res)
  45. red_res = self.reducesum(neg_res, 0)
  46. return self.reducemin(self.reducemin(red_res, 1), 1)
  47. class EmptyNet(Cell):
  48. def __init__(self):
  49. super(EmptyNet, self).__init__()
  50. self.add = P.Add()
  51. self.neg = P.Neg()
  52. def construct(self, x, y):
  53. add_res1 = self.add(x, y)
  54. neg_res1 = self.neg(x)
  55. add_res2 = self.add(add_res1, neg_res1)
  56. return add_res2
  57. def test_basic():
  58. input_x = np.random.normal(0, 1, [2, 3, 4, 3]).astype(np.float32)
  59. input_y = np.random.normal(0, 1, [2, 3, 4, 3]).astype(np.float32)
  60. input_y = np.abs(input_y) + 3
  61. add_res = input_x + 9
  62. sub_res = input_y + (-3)
  63. mul_res = np.sqrt(add_res * sub_res)
  64. div_res = np.sqrt(mul_res)
  65. pow_res = input_y * input_y
  66. neg_res = pow_res
  67. add_res3 = neg_res + div_res
  68. neg_res = np.negative(add_res3)
  69. red_res = np.sum(neg_res, axis=0, keepdims=True)
  70. expect = np.min(red_res, (1, 2, 3))
  71. net = Net()
  72. result = net(Tensor(input_x), Tensor(input_y))
  73. res = np.allclose(expect, result.asnumpy(), rtol=1.e-4,
  74. atol=1.e-7, equal_nan=True)
  75. assert res
  76. def test_empty_graph():
  77. input_x = np.random.normal(0, 1, [2, 3, 4, 3]).astype(np.float32)
  78. input_y = np.random.normal(0, 1, [2, 3, 4, 3]).astype(np.float32)
  79. expect = input_y
  80. net = EmptyNet()
  81. result = net(Tensor(input_x), Tensor(input_y))
  82. res = np.allclose(expect, result.asnumpy(), rtol=1.e-4,
  83. atol=1.e-7, equal_nan=True)
  84. assert res
  85. @pytest.mark.level0
  86. @pytest.mark.platform_x86_gpu_training
  87. @pytest.mark.env_onecard
  88. def test_basic_gpu():
  89. context.set_context(mode=context.GRAPH_MODE, enable_graph_kernel=True, device_target="GPU")
  90. test_basic()
  91. test_empty_graph()
  92. @pytest.mark.level1
  93. @pytest.mark.platform_arm_ascend_training
  94. @pytest.mark.platform_x86_ascend_training
  95. @pytest.mark.env_onecard
  96. def test_basic_ascend():
  97. context.set_context(mode=context.GRAPH_MODE, enable_graph_kernel=True, device_target="Ascend")
  98. test_basic()
  99. test_empty_graph()