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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. 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.TensorAdd()
  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.reshape = P.Reshape()
  33. def construct(self, x, y):
  34. add_res1 = self.add(x, 4)
  35. add_res2 = self.add(add_res1, 5)
  36. sub_res = self.sub(y, 3)
  37. mul_res = self.mul(self.sqrt(add_res2), self.sqrt(sub_res))
  38. div_res = self.div(mul_res, self.sqrt(mul_res))
  39. pow_res = self.pow(y, 2)
  40. neg_res = self.neg(self.neg(pow_res))
  41. add_res3 = self.add(neg_res, div_res)
  42. resh_res = self.reshape(add_res3, (2, 12, 3))
  43. return self.reducemin(resh_res, 1)
  44. def test_basic():
  45. input_x = np.random.normal(0, 1, [2, 3, 4, 3]).astype(np.float32)
  46. input_y = np.random.normal(0, 1, [2, 3, 4, 3]).astype(np.float32)
  47. input_y = np.abs(input_y) + 3
  48. add_res = input_x + 9
  49. sub_res = input_y + (-3)
  50. mul_res = np.sqrt(add_res * sub_res)
  51. div_res = np.sqrt(mul_res)
  52. pow_res = input_y * input_y
  53. neg_res = pow_res
  54. add_res3 = neg_res + div_res
  55. expect = np.min(add_res3, (1, 2))
  56. net = Net()
  57. result = net(Tensor(input_x), Tensor(input_y))
  58. res = np.allclose(expect, result.asnumpy(), rtol=1.e-4,
  59. atol=1.e-7, equal_nan=True)
  60. assert res
  61. @pytest.mark.level0
  62. @pytest.mark.platform_x86_gpu_training
  63. @pytest.mark.env_onecard
  64. def test_basic_gpu():
  65. context.set_context(mode=context.GRAPH_MODE, enable_graph_kernel=True, device_target="GPU")
  66. test_basic()
  67. def test_basic_ascend():
  68. context.set_context(mode=context.GRAPH_MODE, enable_graph_kernel=True, device_target="Ascend")
  69. test_basic()