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 5.7 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. # Copyright 2019-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. """message"""
  15. import importlib.util
  16. import json
  17. import json.decoder as jd
  18. import logging
  19. import traceback
  20. import os
  21. from pathlib import Path
  22. import akg.tvm
  23. from akg.utils import kernel_exec as utils
  24. from akg.utils import validation_check as vc_util
  25. from akg import composite
  26. from . import cce
  27. from . import gpu
  28. from . import op_build
  29. @vc_util.check_input_type(str)
  30. def _compilewithjson_to_module(json_str):
  31. """compile with json."""
  32. try:
  33. kernel_info = json.loads(json_str)
  34. except jd.JSONDecodeError:
  35. logging.error(traceback.format_exc())
  36. return False
  37. supported_processors = ['cuda', 'aicore']
  38. processor = 'cuda'
  39. if 'process' in kernel_info:
  40. processor = kernel_info['process']
  41. if processor not in supported_processors:
  42. logging.error("supported processors: {}, current processor: {}".format(supported_processors, processor))
  43. return False
  44. if 'composite' in kernel_info and kernel_info['composite'] is True:
  45. try:
  46. composite.build(json_str)
  47. return True
  48. except Exception:
  49. logging.error(traceback.format_exc())
  50. return False
  51. op_name = kernel_info['name']
  52. op_func = None
  53. # get custom ops implementation first.
  54. if 'impl_path' in kernel_info and kernel_info['impl_path'] is not None:
  55. impl_path = os.path.realpath(kernel_info['impl_path'])
  56. if os.path.isfile(impl_path):
  57. custom_mod_name = Path(impl_path).resolve().stem
  58. mod_spec = importlib.util.spec_from_file_location(
  59. custom_mod_name, impl_path)
  60. custom_mod = importlib.util.module_from_spec(mod_spec)
  61. mod_spec.loader.exec_module(custom_mod)
  62. op_func = getattr(custom_mod, op_name, None)
  63. # get built-in ops.
  64. if op_func is None:
  65. if processor == 'cuda':
  66. op_func = getattr(gpu, op_name, None)
  67. if op_func is not None:
  68. input_shapes = []
  69. input_types = []
  70. for input_desc in kernel_info['input_desc']:
  71. input_shapes.append(input_desc[0]['shape'])
  72. input_types.append(input_desc[0]['data_type'])
  73. op_attrs = []
  74. if kernel_info['attr']:
  75. for ext_arg in kernel_info['attr']:
  76. op_attrs.append(ext_arg['value'])
  77. dump_ir = os.getenv('MS_AKG_DUMP_IR') == "on"
  78. dump_code = os.getenv('MS_AKG_DUMP_CODE') == "on"
  79. utils.op_build(op_func, input_shapes, input_types, op_attrs, kernel_info['op'], dump_ir=dump_ir,
  80. dump_code=dump_code)
  81. return True
  82. else:
  83. op_func = getattr(cce, op_name, None)
  84. if op_func is None:
  85. logging.error(
  86. "this op not support by akg, please check op name %s", str(op_name))
  87. return False
  88. args = {}
  89. tsr = []
  90. for input_desc in kernel_info['input_desc']:
  91. if len(input_desc) == 1:
  92. tensor_shape = input_desc[0]['shape']
  93. tensor_shape = (1,) if not tensor_shape else tensor_shape
  94. vc_util.shape_dtype_max_size_check(
  95. tensor_shape, input_desc[0]['data_type'])
  96. args[input_desc[0]['name']] = akg.tvm.placeholder(
  97. shape=tensor_shape, name=input_desc[0]['tensor_name'], dtype=input_desc[0]['data_type'])
  98. tsr.append(args[input_desc[0]['name']])
  99. else:
  100. tmp_input = []
  101. for tmp_desc in input_desc:
  102. tensor_shape = tmp_desc['shape']
  103. tensor_shape = (1,) if not tensor_shape else tensor_shape
  104. vc_util.shape_dtype_max_size_check(
  105. tensor_shape, tmp_desc['data_type'])
  106. tmp_input.append(akg.tvm.placeholder(
  107. shape=tensor_shape, name=tmp_desc['tensor_name'], dtype=tmp_desc['data_type']))
  108. args[input_desc[0]['name']] = tmp_input
  109. tsr = tsr + tmp_input
  110. if kernel_info['attr']:
  111. for ext_arg in kernel_info['attr']:
  112. args[ext_arg['name']] = ext_arg['value']
  113. output = op_func(**args)
  114. schedule_func = None
  115. attrs = {}
  116. if isinstance(output, (list, tuple)):
  117. from inspect import isfunction
  118. tmp_outputs = []
  119. for elem in output:
  120. if isfunction(elem):
  121. schedule_func = elem
  122. elif isinstance(elem, dict):
  123. for key, value in elem.items():
  124. if key not in attrs or not attrs[key]:
  125. attrs[key] = value
  126. else:
  127. tmp_outputs.append(elem)
  128. output = tmp_outputs
  129. else:
  130. output = [output]
  131. tsr = tsr + [i for i in output if utils.TensorUtils.is_output_value(i)]
  132. build_res = op_build([op_name], output, tsr, schedule_func, processor, kernel_info['op'], attrs)
  133. if not build_res:
  134. return False
  135. return True
  136. def compilewithjson(json_str):
  137. return _compilewithjson_to_module(json_str)

AKG(Auto Kernel Generator)对深度神经网络中的算子进行优化,并提供特定模式下的算子自动融合功能。AKG与MindSpore的图算融合功能协同工作,可提升在不同硬件后端上运行网络的性能。