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

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