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.

fuzz.py 3.6 kB

5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. """
  15. This utils is used for running fuzz test case more easy and relaxed. even by
  16. python common line. the run command is equal to testarg of testcase
  17. use string test_run function to avoid import it explicitly
  18. -------------------
  19. import fuzz
  20. fuzz.run("add", [(677, 1474), (677, 1474)], ['float32', 'float32'], [], 'add_float32')
  21. """
  22. import os
  23. import time
  24. from akg.utils import kernel_exec as utils
  25. from tests.common.base import TestBase
  26. class TestCase(TestBase):
  27. def setup(self, case):
  28. self.test_arg = case
  29. case_name = "test_akg_fuzz"
  30. case_path = os.getcwd()
  31. # params init
  32. self.params_init(case_name, case_path)
  33. def test_run(self):
  34. self.run_op_build_compile(self.translate_func_name(self.test_arg))
  35. def translate_func_name(self, args):
  36. """
  37. args such as ('reshape', 'reshape.reshape', ([(435, 888)], ['float32'], [[435, 888]], 'reshape_float32'))
  38. """
  39. args_list = [args[0], self.import_get_func(args[1])]
  40. for arg in args[2]:
  41. args_list.append(arg)
  42. return tuple(args_list)
  43. def import_get_func(self, func_name):
  44. """
  45. import get dsl function
  46. """
  47. op_fromlist = ["akg.ops.array.", "akg.ops.nn.", "akg.ops.math.", "akg.ops.optimizers.", "akg.ops.state."]
  48. func_name = func_name.split(".")
  49. for op_from_path in op_fromlist:
  50. try:
  51. op_fromlist = op_from_path + func_name[0]
  52. op_func_py = __import__(op_fromlist, fromlist=func_name[0])
  53. op_func = getattr(op_func_py, func_name[1])
  54. except ImportError:
  55. continue
  56. if op_func is not None:
  57. break
  58. else:
  59. op_fromlist = "test_op." + func_name[0]
  60. op_func_py = __import__(op_fromlist, fromlist=func_name[0])
  61. op_func = getattr(op_func_py, func_name[1])
  62. return op_func
  63. def run_op_build_compile(self, arg):
  64. arg_info = (arg[0], arg[1].__name__, arg[2:])
  65. mod = None
  66. t0 = time.time()
  67. try:
  68. mod = utils.op_build_test(*arg[1:])
  69. except Exception as e:
  70. self._log.error(e)
  71. TestBase.pandora_logger_.traceback()
  72. finally:
  73. if not mod:
  74. self._log.error("run_op_build_compile :: circle {0} fail !".format(arg))
  75. self._log.error("run_op_build_compile :: compile failed !")
  76. result = "fail"
  77. else:
  78. result = "succ"
  79. t1 = time.time()
  80. self._log.info("run_fuzz_result_count func time test: args:%s, result:%s, running:%s seconds",
  81. arg_info, result, str(t1 - t0))
  82. if not result:
  83. assert result
  84. return True
  85. def teardown(self):
  86. self._log.info("============= {0} Teardown============".format(self.casename))
  87. def run(*case):
  88. a = TestCase()
  89. a.setup(case)
  90. a.test_run()
  91. a.teardown()