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.

expander.py 2.6 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. # ============================================================================
  15. """generate json desc for graph kernel ops"""
  16. import json
  17. import json.decoder as jd
  18. import traceback
  19. from mindspore import log as logger
  20. import mindspore._extends.graph_kernel.expanders as expanders
  21. from mindspore._extends.graph_kernel.model.model import GraphKernelUnsupportedException
  22. def create_expander(expand_info):
  23. """Create an expander according to op name"""
  24. op_name = str(expand_info['name'])
  25. if not hasattr(expanders, op_name):
  26. raise GraphKernelUnsupportedException("Generator do not support op: {}".format(op_name))
  27. expander = getattr(expanders, op_name)
  28. return expander(expand_info)
  29. def extract_expand_info(kernel_info):
  30. """Convert the json into a more friendly format"""
  31. input_desc = []
  32. if 'input_desc' in kernel_info and kernel_info['input_desc']:
  33. for desc in kernel_info['input_desc']:
  34. input_desc += desc
  35. attrs = {}
  36. if 'attr' in kernel_info and kernel_info['attr']:
  37. for attr in kernel_info["attr"]:
  38. attrs[attr["name"]] = attr["value"]
  39. expand_info = {
  40. "name": kernel_info["name"],
  41. "input_desc": input_desc,
  42. "output_desc": kernel_info["output_desc"],
  43. "attr": attrs,
  44. "process": kernel_info["process"],
  45. }
  46. return expand_info
  47. def get_op_expander(json_str: str):
  48. """get op expander by json info"""
  49. try:
  50. kernel_info = json.loads(json_str)
  51. expand_info = extract_expand_info(kernel_info)
  52. expander = create_expander(expand_info)
  53. graph = expander.run()
  54. # dump graph to json desc.
  55. desc = graph.dump()
  56. return json.dumps(desc)
  57. except jd.JSONDecodeError:
  58. logger.error("Failed to generate graph kernel op")
  59. logger.error(traceback.format_exc())
  60. return None
  61. except GraphKernelUnsupportedException as e:
  62. logger.info(e.message)
  63. return ""