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.3 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. context.set_context(mode=context.GRAPH_MODE, enable_graph_kernel=True, device_target="GPU")
  22. class Net(Cell):
  23. def __init__(self):
  24. super(Net, self).__init__()
  25. self.add = P.TensorAdd()
  26. self.sub = P.Sub()
  27. self.mul = P.Mul()
  28. self.div = P.RealDiv()
  29. self.sqrt = P.Sqrt()
  30. self.pow = P.Pow()
  31. self.neg = P.Neg()
  32. def construct(self, x, y):
  33. add_res1 = self.add(x, 4)
  34. add_res2 = self.add(add_res1, 5)
  35. sub_res = self.sub(y, 3)
  36. mul_res = self.mul(self.sqrt(add_res2), self.sqrt(sub_res))
  37. div_res = self.div(mul_res, self.sqrt(mul_res))
  38. pow_res = self.pow(y, 2)
  39. neg_res = self.neg(self.neg(pow_res))
  40. return self.add(div_res, neg_res)
  41. @pytest.mark.level0
  42. @pytest.mark.platform_x86_gpu_training
  43. @pytest.mark.env_onecard
  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. expect = div_res + neg_res
  55. net = Net()
  56. result = net(Tensor(input_x), Tensor(input_y))
  57. res = np.allclose(expect, result.asnumpy(), rtol=1.e-4, atol=1.e-7, equal_nan=True)
  58. assert res