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_ms_equal.py 3.0 kB

5 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. import numpy as np
  15. from tests.common.gen_random import random_gaussian
  16. from akg.utils import kernel_exec as utils
  17. from akg.utils.result_analysis import gpu_profiling
  18. from akg.utils.format_transform import to_tvm_nd_array
  19. from akg.ops.math_gpu.equal import equal
  20. def gen_data(shapes, dtype):
  21. support_list = {"float16": np.float16, "float32": np.float32}
  22. inputs = []
  23. for i in range(len(shapes)):
  24. shape = shapes[i]
  25. one_input = random_gaussian(shape, miu=1, sigma=0.1).astype(support_list[dtype])
  26. inputs.append(one_input)
  27. if len(inputs) != 2:
  28. raise RuntimeError("inputs num should be 2")
  29. expect = np.equal(inputs[0], inputs[1])
  30. output = np.full(expect.shape, 0, bool)
  31. return inputs, output, expect
  32. def test_ms_equal(shapes, dtype, poly_sch=False):
  33. if poly_sch:
  34. mod = utils.op_build_test(equal, shapes, [dtype, dtype], kernel_name="equal", attrs={"target": "cuda"})
  35. inputs1, output1, expect1 = gen_data(shapes, dtype)
  36. output1 = utils.mod_launch(mod, (*inputs1, output1), expect=expect1)
  37. if shapes[0] == shapes[1]:
  38. inputs2 = []
  39. inputs2.append(inputs1[0])
  40. inputs2.append(inputs1[0])
  41. expect2 = np.equal(inputs2[0], inputs2[1])
  42. output2 = np.full(expect2.shape, 0, bool)
  43. output2 = utils.mod_launch(mod, (*inputs2, output2), expect=expect1)
  44. res = np.allclose(output1, expect1, rtol=5e-03, atol=1.e-8) and np.allclose(output2, expect2, rtol=5e-03, atol=1.e-8)
  45. print("Test {}".format("Pass" if res else "Fail"))
  46. if not res:
  47. print("Error cuda:========================")
  48. print(mod.imported_modules[0].get_source())
  49. raise AssertionError("Test fail")
  50. inputs1 = to_tvm_nd_array(inputs1)
  51. inputs2 = to_tvm_nd_array(inputs2)
  52. expect1 = to_tvm_nd_array(expect1)
  53. expect2 = to_tvm_nd_array(expect2)
  54. gpu_profiling(mod, *inputs1, expect1, *inputs2, expect2, 400)
  55. else:
  56. res = np.allclose(output1, expect1, rtol=5e-03, atol=1.e-8)
  57. print("Test {}".format("Pass" if res else "Fail"))
  58. if not res:
  59. print("Error cuda:========================")
  60. print(mod.imported_modules[0].get_source())
  61. raise AssertionError("Test fail")
  62. inputs1 = to_tvm_nd_array(inputs1)
  63. expect1 = to_tvm_nd_array(expect1)
  64. gpu_profiling(mod, *inputs1, expect1, 400)