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_low_precision.py 4.4 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. # Copyright 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 as ops
  21. import mindspore.ops.operations as P
  22. def test_case_1():
  23. class Net1(Cell):
  24. def __init__(self):
  25. super(Net1, self).__init__()
  26. self.sub = ops.Sub()
  27. self.mul = ops.Mul()
  28. self.sum = ops.ReduceSum(keep_dims=False)
  29. self.add = ops.Add()
  30. self.pow = ops.Pow()
  31. def construct(self, x, y, z):
  32. t1 = self.sub(x, y)
  33. t2 = self.mul(t1, x)
  34. t3 = self.add(y, t2)
  35. t4 = self.add(t3, t3)
  36. t5 = z + 1.0
  37. t6 = self.sum(t4)
  38. t7 = self.add(t5, t6)
  39. return t7
  40. def get_output(x, y, z, net, enable_graph_kernel=False):
  41. context.set_context(enable_graph_kernel=enable_graph_kernel)
  42. net_obj = net()
  43. output = net_obj(x, y, z)
  44. return output
  45. N = 8
  46. x = Tensor(np.random.uniform(1, 2, [N, N, N]).astype(np.float32))
  47. y = Tensor(np.random.uniform(1, 2, [N, N, N]).astype(np.float32))
  48. z = Tensor(np.random.uniform(1, 2, [N, N, N]).astype(np.float32))
  49. expect = get_output(x, y, z, Net1, False)
  50. output = get_output(x, y, z, Net1, True)
  51. expect_np = expect.asnumpy().copy()
  52. output_np = output.asnumpy().copy()
  53. assert np.allclose(expect_np, output_np, 1.e-2, 1.e-2)
  54. def test_case_2():
  55. class Net2(Cell):
  56. def __init__(self):
  57. super(Net2, self).__init__()
  58. self.sqrt = P.Sqrt()
  59. self.sum = P.ReduceSum(keep_dims=True)
  60. self.add = P.Add()
  61. self.neg = P.Neg()
  62. def construct(self, x, y):
  63. sqrt_res = self.sqrt(x)
  64. add_res = self.add(y, sqrt_res)
  65. neg_res = self.neg(add_res)
  66. return neg_res
  67. def get_output(x, y, net, enable_graph_kernel=False):
  68. context.set_context(enable_graph_kernel=enable_graph_kernel)
  69. net_obj = net()
  70. output = net_obj(x, y)
  71. return output
  72. N = 16
  73. x = Tensor(np.random.uniform(1, 2, [N, N]).astype(np.float32))
  74. y = Tensor(np.random.uniform(1, 2, [N, N]).astype(np.float32))
  75. expect = get_output(x, y, Net2, False)
  76. output = get_output(x, y, Net2, True)
  77. expect_np = expect[0].asnumpy().copy()
  78. output_np = output[0].asnumpy().copy()
  79. assert np.allclose(expect_np, output_np, 1.e-2, 1.e-2)
  80. @pytest.mark.level0
  81. @pytest.mark.platform_x86_gpu_training
  82. @pytest.mark.env_onecard
  83. def test_gpu_case_1():
  84. context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
  85. context.set_context(graph_kernel_flags="--enable_low_precision=true --disable_pass=highlevelopt2.atomic_clean")
  86. test_case_1()
  87. @pytest.mark.level0
  88. @pytest.mark.platform_x86_gpu_training
  89. @pytest.mark.env_onecard
  90. def test_gpu_case_2():
  91. context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
  92. context.set_context(graph_kernel_flags="--enable_low_precision=true")
  93. test_case_2()
  94. @pytest.mark.level0
  95. @pytest.mark.platform_arm_ascend_training
  96. @pytest.mark.platform_x86_ascend_training
  97. @pytest.mark.env_onecard
  98. def test_ascend_case_1():
  99. context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
  100. context.set_context(graph_kernel_flags="--enable_low_precision=true --disable_pass=highlevelopt2.atomic_clean")
  101. test_case_1()
  102. @pytest.mark.level0
  103. @pytest.mark.platform_arm_ascend_training
  104. @pytest.mark.platform_x86_ascend_training
  105. @pytest.mark.env_onecard
  106. def test_ascend_case_2():
  107. context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
  108. context.set_context(graph_kernel_flags="--enable_low_precision=true")
  109. test_case_2()