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.

message.py 4.0 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. """message"""
  15. import importlib.util
  16. import json
  17. import json.decoder as jd
  18. import logging
  19. import traceback
  20. import os.path
  21. from pathlib import Path
  22. import _akg.tvm
  23. from _akg.utils import validation_check as vc_util
  24. from _akg.utils.dsl_create import TensorUtils
  25. from . import gpu
  26. from . import op_build
  27. @vc_util.check_input_type(str)
  28. def compilewithjson(json_str):
  29. """compile with json."""
  30. try:
  31. kernel_info = json.loads(json_str)
  32. except jd.JSONDecodeError:
  33. logging.error(traceback.format_exc())
  34. return False
  35. op_name = kernel_info['name']
  36. op_func = None
  37. processor = 'aicore'
  38. if 'process' in kernel_info:
  39. processor = kernel_info['process']
  40. # get custom ops implementation first.
  41. if 'impl_path' in kernel_info and kernel_info['impl_path'] is not None:
  42. impl_path = os.path.realpath(kernel_info['impl_path'])
  43. if os.path.isfile(impl_path):
  44. custom_mod_name = Path(impl_path).resolve().stem
  45. mod_spec = importlib.util.spec_from_file_location(custom_mod_name, impl_path)
  46. custom_mod = importlib.util.module_from_spec(mod_spec)
  47. mod_spec.loader.exec_module(custom_mod)
  48. op_func = getattr(custom_mod, op_name, None)
  49. # get built-in ops.
  50. if op_func is None:
  51. if processor == 'cuda':
  52. op_func = getattr(gpu, op_name, None)
  53. if op_func is None:
  54. logging.error("this op not supported, please check op name %s", str(op_name))
  55. return False
  56. args = {}
  57. tsr = []
  58. for input_desc in kernel_info['input_desc']:
  59. if len(input_desc) == 1:
  60. tensor_shape = input_desc[0]['shape']
  61. tensor_shape = (1,) if not tensor_shape else tensor_shape
  62. vc_util.shape_dtype_max_size_check(tensor_shape)
  63. args[input_desc[0]['name']] = _akg.tvm.placeholder(
  64. shape=tensor_shape, name=input_desc[0]['tensor_name'], dtype=input_desc[0]['data_type'])
  65. tsr.append(args[input_desc[0]['name']])
  66. else:
  67. tmp_input = []
  68. for tmp_desc in input_desc:
  69. tensor_shape = tmp_desc['shape']
  70. tensor_shape = (1,) if not tensor_shape else tensor_shape
  71. vc_util.shape_dtype_max_size_check(tensor_shape)
  72. tmp_input.append(_akg.tvm.placeholder(
  73. shape=tensor_shape, name=tmp_desc['tensor_name'], dtype=tmp_desc['data_type']))
  74. args[input_desc[0]['name']] = tmp_input
  75. tsr = tsr + tmp_input
  76. if kernel_info['attr']:
  77. for ext_arg in kernel_info['attr']:
  78. args[ext_arg['name']] = ext_arg['value']
  79. output = op_func(**args)
  80. schedule_func = None
  81. attrs = {}
  82. if isinstance(output, (list, tuple)):
  83. from inspect import isfunction
  84. tmp_outputs = []
  85. for elem in output:
  86. if isfunction(elem):
  87. schedule_func = elem
  88. elif isinstance(elem, dict):
  89. for key, value in elem.items():
  90. if key not in attrs or not attrs[key]:
  91. attrs[key] = value
  92. else:
  93. tmp_outputs.append(elem)
  94. output = tmp_outputs
  95. else:
  96. output = [output]
  97. tsr = tsr + [i for i in output if TensorUtils.is_output_value(i)]
  98. return op_build([op_name], output, tsr, schedule_func, processor, kernel_info['op'], attrs)