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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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,
  22. enable_graph_kernel=True, device_target="GPU")
  23. class Net(Cell):
  24. def __init__(self):
  25. super(Net, self).__init__()
  26. self.add = P.TensorAdd()
  27. self.sub = P.Sub()
  28. self.mul = P.Mul()
  29. self.div = P.RealDiv()
  30. self.sqrt = P.Sqrt()
  31. self.pow = P.Pow()
  32. self.neg = P.Neg()
  33. self.reducemin = P.ReduceMin()
  34. self.reshape = P.Reshape()
  35. def construct(self, x, y):
  36. add_res1 = self.add(x, 4)
  37. add_res2 = self.add(add_res1, 5)
  38. sub_res = self.sub(y, 3)
  39. mul_res = self.mul(self.sqrt(add_res2), self.sqrt(sub_res))
  40. div_res = self.div(mul_res, self.sqrt(mul_res))
  41. pow_res = self.pow(y, 2)
  42. neg_res = self.neg(self.neg(pow_res))
  43. add_res3 = self.add(neg_res, div_res)
  44. resh_res = self.reshape(add_res3, (2, 12, 3))
  45. return self.reducemin(resh_res, 1)
  46. @pytest.mark.level0
  47. @pytest.mark.platform_x86_gpu_training
  48. @pytest.mark.env_onecard
  49. def test_basic():
  50. input_x = np.random.normal(0, 1, [2, 3, 4, 3]).astype(np.float32)
  51. input_y = np.random.normal(0, 1, [2, 3, 4, 3]).astype(np.float32)
  52. input_y = np.abs(input_y) + 3
  53. add_res = input_x + 9
  54. sub_res = input_y + (-3)
  55. mul_res = np.sqrt(add_res * sub_res)
  56. div_res = np.sqrt(mul_res)
  57. pow_res = input_y * input_y
  58. neg_res = pow_res
  59. add_res3 = neg_res + div_res
  60. expect = np.min(add_res3, (1, 2))
  61. net = Net()
  62. result = net(Tensor(input_x), Tensor(input_y))
  63. res = np.allclose(expect, result.asnumpy(), rtol=1.e-4,
  64. atol=1.e-7, equal_nan=True)
  65. assert res