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.

parallel_estimate.py 2.1 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. # Copyright 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. """estimate parallel case"""
  16. import json
  17. import json.decoder as jd
  18. import traceback
  19. from mindspore import log as logger
  20. from . import model
  21. def estimate_ops(json_str):
  22. """Call cost model to estimate ops."""
  23. try:
  24. json_obj = json.loads(json_str)
  25. graph_descs = json_obj["graph_desc"]
  26. graphs = []
  27. target = None
  28. for gd in graph_descs:
  29. if target is None:
  30. target = gd['process']
  31. elif target != gd['process']:
  32. logger.error("Parallel fusion does not support multi-target({} and {})".format(target, gd['process']))
  33. return None
  34. graphs.append(model.load_composite(gd).graph)
  35. estimation = model.parallel_estimate(graphs, target)
  36. res = (estimation.block_assign, estimation.gain,
  37. estimation.fusion_type, estimation.type_info)
  38. return res
  39. except jd.JSONDecodeError:
  40. logger.error(traceback.format_exc())
  41. return None
  42. def estimate_calculation_amount(json_str):
  43. """Call cost model to estimate calculation amount of op."""
  44. try:
  45. graph_desc = json.loads(json_str)
  46. target = graph_desc['process']
  47. comp = model.load_composite(graph_desc)
  48. estimation = model.parallel_estimate([comp.graph], target)
  49. return estimation.bottleneck
  50. except jd.JSONDecodeError:
  51. logger.error(traceback.format_exc())
  52. return None