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.2 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. # Copyright 2020 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. def get_op_expander(json_str: str):
  22. """get op expander by json info"""
  23. try:
  24. kernel_info = json.loads(json_str)
  25. expand_info = kernel_info['expand_info']
  26. if 'name' not in expand_info:
  27. logger.error("expand info have no op name")
  28. return None
  29. if 'process' not in expand_info:
  30. logger.error("expand info have no processor info")
  31. return None
  32. processor = expand_info['process']
  33. op_name = str(expand_info['name']).lower()
  34. expand_op_func_name = 'expand_' + op_name
  35. if not hasattr(expanders, expand_op_func_name):
  36. logger.error("Generator do not support op: {}".format(op_name))
  37. return None
  38. expand_op_func = getattr(expanders, expand_op_func_name)
  39. # generate graph desc.
  40. graph = expand_op_func(expand_info)
  41. if graph is None:
  42. logger.error("Failed to generate graph of: {}".format(op_name))
  43. return None
  44. graph.set_processor(processor)
  45. # dump graph to json desc.
  46. desc = graph.dump()
  47. return json.dumps(desc)
  48. except jd.JSONDecodeError:
  49. logger.error("Failed to generate graph kernel op")
  50. logger.error(traceback.format_exc())
  51. return None