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.

utils.py 9.4 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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. """Define common utils."""
  16. import json
  17. import os
  18. import stat
  19. from importlib import import_module
  20. from importlib.util import find_spec
  21. from typing import List, Tuple, Mapping
  22. from mindinsight.mindconverter.common.exceptions import ScriptGenerationError, ReportGenerationError, \
  23. UnknownModelError, CheckPointGenerationError, WeightMapGenerationError
  24. from mindinsight.mindconverter.common.log import logger as log
  25. from mindinsight.mindconverter.graph_based_converter.constant import SEPARATOR_IN_ONNX_OP, BINARY_HEADER_PYTORCH_BITS, \
  26. FrameworkType, BINARY_HEADER_PYTORCH_FILE, TENSORFLOW_MODEL_SUFFIX
  27. from mindspore.train.serialization import save_checkpoint
  28. def is_converted(operation: str):
  29. """
  30. Whether convert successful.
  31. Args:
  32. operation (str): Operation name.
  33. Returns:
  34. bool, true or false.
  35. """
  36. return operation and SEPARATOR_IN_ONNX_OP not in operation
  37. def _add_outputs_of_onnx_model(model, output_nodes: List[str]):
  38. """
  39. Add output nodes of onnx model.
  40. Args:
  41. model (ModelProto): ONNX model.
  42. output_nodes (list[str]): Output nodes list.
  43. Returns:
  44. ModelProto, edited ONNX model.
  45. """
  46. onnx = import_module("onnx")
  47. for opt_name in output_nodes:
  48. intermediate_layer_value_info = onnx.helper.ValueInfoProto()
  49. intermediate_layer_value_info.name = opt_name
  50. model.graph.output.append(intermediate_layer_value_info)
  51. return model
  52. def fetch_output_from_onnx_model(model, feed_dict: dict, output_nodes: List[str]):
  53. """
  54. Fetch specific nodes output from onnx model.
  55. Notes:
  56. Only support to get output without batch dimension.
  57. Args:
  58. model (ModelProto): ONNX model.
  59. feed_dict (dict): Feed forward inputs.
  60. output_nodes (list[str]): Output nodes list.
  61. Returns:
  62. dict, nodes' output value.
  63. """
  64. if not isinstance(feed_dict, dict) or not isinstance(output_nodes, list):
  65. raise TypeError("`feed_dict` should be type of dict, and `output_nodes` "
  66. "should be type of List[str].")
  67. edit_model = _add_outputs_of_onnx_model(model, output_nodes)
  68. ort = import_module("onnxruntime")
  69. sess = ort.InferenceSession(path_or_bytes=bytes(edit_model.SerializeToString()))
  70. fetched_res = sess.run(output_names=output_nodes, input_feed=feed_dict)
  71. run_result = dict()
  72. for idx, opt in enumerate(output_nodes):
  73. run_result[opt] = fetched_res[idx]
  74. return run_result
  75. def save_code_file_and_report(model_name: str, code_lines: Mapping[str, Tuple],
  76. out_folder: str, report_folder: str):
  77. """
  78. Save code file and report.
  79. Args:
  80. model_name (str): Model name.
  81. code_lines (dict): Code lines.
  82. out_folder (str): Output folder.
  83. report_folder (str): Report output folder.
  84. """
  85. flags = os.O_WRONLY | os.O_CREAT | os.O_EXCL
  86. modes = stat.S_IRUSR | stat.S_IWUSR
  87. modes_usr = stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR
  88. out_folder = os.path.realpath(out_folder)
  89. if not report_folder:
  90. report_folder = out_folder
  91. else:
  92. report_folder = os.path.realpath(report_folder)
  93. if not os.path.exists(out_folder):
  94. os.makedirs(out_folder, modes_usr)
  95. if not os.path.exists(report_folder):
  96. os.makedirs(report_folder, modes_usr)
  97. for file_name in code_lines:
  98. code, report, trainable_weights, weight_map = code_lines[file_name]
  99. code_file_path = os.path.realpath(os.path.join(out_folder, f"{model_name}.py"))
  100. report_file_path = os.path.realpath(os.path.join(report_folder, f"report_of_{model_name}.txt"))
  101. try:
  102. if os.path.exists(code_file_path):
  103. raise ScriptGenerationError("Code file with the same name already exists.")
  104. with os.fdopen(os.open(code_file_path, flags, modes), 'w') as file:
  105. file.write(code)
  106. except (IOError, FileExistsError) as error:
  107. raise ScriptGenerationError(str(error))
  108. try:
  109. if os.path.exists(report_file_path):
  110. raise ReportGenerationError("Report file with the same name already exists.")
  111. with os.fdopen(os.open(report_file_path, flags, stat.S_IRUSR), "w") as rpt_f:
  112. rpt_f.write(report)
  113. except (IOError, FileExistsError) as error:
  114. raise ReportGenerationError(str(error))
  115. ckpt_file_path = os.path.realpath(os.path.join(out_folder, f"{model_name}.ckpt"))
  116. try:
  117. if os.path.exists(ckpt_file_path):
  118. raise CheckPointGenerationError("Checkpoint file with the same name already exists.")
  119. save_checkpoint(trainable_weights, ckpt_file_path)
  120. except TypeError as error:
  121. raise CheckPointGenerationError(str(error))
  122. weight_map_path = os.path.realpath(os.path.join(out_folder, f"weight_map_of_{model_name}.json"))
  123. try:
  124. if os.path.exists(weight_map_path):
  125. raise WeightMapGenerationError("Weight map file with the same name already exists.")
  126. with os.fdopen(os.open(weight_map_path, flags, stat.S_IRUSR), 'w') as map_f:
  127. weight_map_json = {f"{model_name}": weight_map}
  128. json.dump(weight_map_json, map_f)
  129. except (IOError, FileExistsError) as error:
  130. raise WeightMapGenerationError(str(error))
  131. def onnx_satisfied():
  132. """Validate ONNX , ONNXRUNTIME, ONNXOPTIMIZER installation."""
  133. if not find_spec("onnx") or not find_spec("onnxruntime") or not find_spec("onnxoptimizer"):
  134. return False
  135. return True
  136. def lib_version_satisfied(current_ver: str, mini_ver_limited: str,
  137. newest_ver_limited: str = ""):
  138. """
  139. Check python lib version whether is satisfied.
  140. Notes:
  141. Version number must be format of x.x.x, e.g. 1.1.0.
  142. Args:
  143. current_ver (str): Current lib version.
  144. mini_ver_limited (str): Mini lib version.
  145. newest_ver_limited (str): Newest lib version.
  146. Returns:
  147. bool, true or false.
  148. """
  149. required_version_number_len = 3
  150. if len(list(current_ver.split("."))) != required_version_number_len or \
  151. len(list(mini_ver_limited.split("."))) != required_version_number_len or \
  152. (newest_ver_limited and len(newest_ver_limited.split(".")) != required_version_number_len):
  153. raise ValueError("Version number must be format of x.x.x.")
  154. if current_ver < mini_ver_limited or (newest_ver_limited and current_ver > newest_ver_limited):
  155. return False
  156. return True
  157. def get_dict_key_by_value(val, dic):
  158. """
  159. Return the first appeared key of a dictionary by given value.
  160. Args:
  161. val (Any): Value of the key.
  162. dic (dict): Dictionary to be checked.
  163. Returns:
  164. Any, key of the given value.
  165. """
  166. for d_key, d_val in dic.items():
  167. if d_val == val:
  168. return d_key
  169. return None
  170. def convert_bytes_string_to_string(bytes_str):
  171. """
  172. Convert a byte string to string by utf-8.
  173. Args:
  174. bytes_str (bytes): A bytes string.
  175. Returns:
  176. str, a str with utf-8 encoding.
  177. """
  178. if isinstance(bytes_str, bytes):
  179. return bytes_str.decode('utf-8')
  180. return bytes_str
  181. def get_framework_type(model_path):
  182. """Get framework type."""
  183. if model_path.endswith('.onnx'):
  184. return FrameworkType.PYTORCH.value
  185. try:
  186. with open(model_path, 'rb') as f:
  187. if f.read(BINARY_HEADER_PYTORCH_BITS) == BINARY_HEADER_PYTORCH_FILE:
  188. framework_type = FrameworkType.PYTORCH.value
  189. elif os.path.basename(model_path).split(".")[-1].lower() == TENSORFLOW_MODEL_SUFFIX:
  190. framework_type = FrameworkType.TENSORFLOW.value
  191. else:
  192. framework_type = FrameworkType.UNKNOWN.value
  193. except IOError:
  194. error_msg = "Get UNSUPPORTED model."
  195. error = UnknownModelError(error_msg)
  196. log.error(str(error))
  197. raise error
  198. return framework_type
  199. def reset_init_or_construct(template, variable_slot, new_data, scope):
  200. """Reset init statement."""
  201. template[variable_slot][scope].clear()
  202. template[variable_slot][scope] += new_data
  203. return template
  204. def replace_string_in_list(str_list: list, original_str: str, target_str: str):
  205. """
  206. Replace a string in a list by provided string.
  207. Args:
  208. str_list (list): A list contains the string to be replaced.
  209. original_str (str): The string to be replaced.
  210. target_str (str): The replacement of string.
  211. Returns,
  212. list, the original list with replaced string.
  213. """
  214. return [s.replace(original_str, target_str) for s in str_list]