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.

splitter.py 2.9 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. """GraphKernel splitter"""
  16. import os
  17. import json
  18. import json.decoder as jd
  19. import traceback
  20. from mindspore import log as logger
  21. from . import model
  22. from . import utils
  23. def split_with_json(json_str, flags_str):
  24. """Call cost model to split GraphKernel"""
  25. try:
  26. graph_desc = json.loads(json_str)
  27. flags = json.loads(flags_str)
  28. target = graph_desc['process']
  29. comp = model.load_composite(graph_desc)
  30. graph_split, graph_mode = model.split(comp.graph, target, flags)
  31. is_multi_graph = len(graph_split) > 1
  32. graph_list = list(map(comp.dump, graph_split))
  33. _reset_graphmode_for_inplaceassign(graph_list, graph_mode)
  34. result = {"multi_graph": is_multi_graph,
  35. "graph_desc": graph_list,
  36. "graph_mode": graph_mode}
  37. _dump_split_info(flags, json_str, comp.graph, graph_split, graph_mode)
  38. return json.dumps(result)
  39. except jd.JSONDecodeError:
  40. logger.error(traceback.format_exc())
  41. return None
  42. def _reset_graphmode_for_inplaceassign(graph_list, graph_mode):
  43. """Operator with InplaceAssign should always be composite op"""
  44. for i, g in enumerate(graph_list):
  45. if any([op['name'] == 'InplaceAssign' for op in g['op_desc']]):
  46. graph_mode[i] = 'composite'
  47. def _dump_split_info(flags, graph_json, graph_desc, subgraphs, graph_mode):
  48. """Dump split info as text"""
  49. if not flags.get("dump_as_text", False):
  50. return
  51. utils.create_dir(utils.GRAPH_KERNEL_DUMP_PATH)
  52. filename = os.path.join(utils.GRAPH_KERNEL_DUMP_PATH, "graph_kernel_split_mode.txt")
  53. with os.fdopen(os.open(filename, os.O_WRONLY | os.O_CREAT), "a+") as f:
  54. f.write("********** main graph: {} **********\n".format(graph_desc.name))
  55. f.write("input json:\n{}\n".format(graph_json))
  56. f.write("graph desc:\n{}\n".format(str(graph_desc)))
  57. if len(subgraphs) > 1 or subgraphs[0].stitch_info.has_stitch_op():
  58. for i, g in enumerate(subgraphs):
  59. f.write("-------- subgraph {}, mode: {} --------\n".format(i, graph_mode[i]))
  60. f.write("{}\n".format(str(g)))
  61. else:
  62. f.write("Graph unchanged.\n")
  63. f.write("\n")