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.

square_difference_run.py 2.4 kB

5 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. # Copyright 2019 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.tensorio import compare_tensor
  16. from akg.utils import kernel_exec as utils
  17. from tests.common.test_op import square_difference
  18. from tests.common.gen_random import random_gaussian
  19. def square_difference_run(shape1, shape2, dtype, kernel_name, attrs, cce_path="./"):
  20. if 'tuning' in attrs.keys():
  21. t = attrs.get("tuning", False)
  22. kernel_name = attrs.get("kernel_name", False)
  23. mod = utils.op_build_test(square_difference.square_difference, input_shapes=[shape1, shape2],
  24. input_types=[dtype, dtype], kernel_name=kernel_name, attrs=attrs, tuning=t)
  25. if t:
  26. expect, input1, input2, output = gen_data(dtype, shape1, shape2)
  27. return mod, expect, (input1, input2, output)
  28. else:
  29. return mod
  30. else:
  31. mod = utils.op_build_test(square_difference.square_difference, input_shapes=[shape1, shape2],
  32. input_types=[dtype, dtype], kernel_name=kernel_name, attrs=attrs)
  33. expect, input1, input2, output = gen_data(dtype, shape1, shape2)
  34. source_code = mod.imported_modules[0].get_source()
  35. utils.create_code(kernel_name, cce_path, source_code)
  36. output = utils.mod_launch(mod, (input1, input2, output), expect=expect)
  37. return (input1, input2), output, expect, compare_tensor(output, expect, rtol=5e-03, equal_nan=True)
  38. def gen_data(dtype, shape1, shape2):
  39. support_list = {"float16": np.float16, "float32": np.float32}
  40. input1 = random_gaussian(shape1, miu=1, sigma=0.1).astype(support_list[dtype])
  41. input2 = random_gaussian(shape2, miu=1, sigma=0.1).astype(support_list[dtype])
  42. expect = np.square(np.subtract(input1, input2))
  43. out_shape = expect.shape
  44. output = np.full(out_shape, np.nan, dtype)
  45. return expect, input1, input2, output