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 12 kB

4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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. import uuid
  20. from importlib import import_module
  21. from importlib.util import find_spec
  22. from typing import List, Tuple, Mapping
  23. import numpy as np
  24. from mindinsight.mindconverter.common.log import logger as log
  25. from mindinsight.mindconverter.common.exceptions import ScriptGenerationError, ReportGenerationError, \
  26. CheckPointGenerationError, WeightMapGenerationError, ModelLoadingError, OnnxModelSaveError
  27. from mindinsight.mindconverter.graph_based_converter.constant import SEPARATOR_IN_ONNX_OP, FrameworkType, \
  28. TENSORFLOW_MODEL_SUFFIX, THIRD_PART_VERSION, ONNX_MODEL_SUFFIX, DTYPE_MAP
  29. def is_converted(operation: str):
  30. """
  31. Whether convert successful.
  32. Args:
  33. operation (str): Operation name.
  34. Returns:
  35. bool, true or false.
  36. """
  37. return operation and SEPARATOR_IN_ONNX_OP not in operation
  38. def _add_outputs_of_onnx_model(model, output_nodes: List[str]):
  39. """
  40. Add output nodes of onnx model.
  41. Args:
  42. model (ModelProto): ONNX model.
  43. output_nodes (list[str]): Output nodes list.
  44. Returns:
  45. ModelProto, edited ONNX model.
  46. """
  47. onnx = import_module("onnx")
  48. for opt_name in output_nodes:
  49. intermediate_layer_value_info = onnx.helper.ValueInfoProto()
  50. intermediate_layer_value_info.name = opt_name
  51. model.graph.output.append(intermediate_layer_value_info)
  52. return model
  53. def check_dependency_integrity(*packages):
  54. """Check dependency package integrity."""
  55. try:
  56. for pkg in packages:
  57. import_module(pkg)
  58. return True
  59. except ImportError as e:
  60. log.exception(e)
  61. return False
  62. def build_feed_dict(onnx_model, input_nodes: dict):
  63. """Build feed dict for onnxruntime."""
  64. dtype_mapping = DTYPE_MAP
  65. input_nodes_types = {
  66. node.name: dtype_mapping[node.type.tensor_type.elem_type]
  67. for node in onnx_model.graph.input
  68. }
  69. feed_dict = {
  70. name: np.random.rand(*shape).astype(input_nodes_types[name])
  71. for name, shape in input_nodes.items()
  72. }
  73. return feed_dict
  74. def fetch_output_from_onnx_model(model, model_path: str, feed_dict: dict, output_nodes: List[str]):
  75. """
  76. Fetch specific nodes output from onnx model.
  77. Notes:
  78. Only support to get output without batch dimension.
  79. Args:
  80. model (ModelProto): ONNX model.
  81. model_path (str): ONNX model path.
  82. feed_dict (dict): Feed forward inputs.
  83. output_nodes (list[str]): Output nodes list.
  84. Returns:
  85. dict, nodes' output value.
  86. """
  87. if not isinstance(feed_dict, dict) or not isinstance(output_nodes, list):
  88. raise TypeError("`feed_dict` should be type of dict, and `output_nodes` "
  89. "should be type of List[str].")
  90. edit_model = _add_outputs_of_onnx_model(model, output_nodes)
  91. onnx = import_module("onnx")
  92. ort = import_module("onnxruntime")
  93. try:
  94. dir_path = os.path.dirname(model_path)
  95. stem_name = os.path.splitext(os.path.basename(model_path))[0]
  96. filename = ".~{0}_{1}".format(stem_name, str(uuid.uuid4()))
  97. tmp_file = os.path.join(dir_path, filename)
  98. onnx.save_tensor(edit_model, tmp_file)
  99. except (TypeError, IOError) as error:
  100. if os.path.exists(tmp_file):
  101. os.remove(tmp_file)
  102. raise OnnxModelSaveError("Onnx model save failed, {}".format(str(error)))
  103. try:
  104. sess = ort.InferenceSession(path_or_bytes=tmp_file)
  105. fetched_res = sess.run(output_names=output_nodes, input_feed=feed_dict)
  106. except ModelLoadingError.raise_from() as error:
  107. raise ModelLoadingError("OnnxRuntimeError, {}".format(str(error)))
  108. finally:
  109. if os.path.exists(tmp_file):
  110. os.remove(tmp_file)
  111. run_result = dict()
  112. for idx, opt in enumerate(output_nodes):
  113. run_result[opt] = fetched_res[idx]
  114. return run_result
  115. def save_code_file_and_report(model_name: str, code_lines: Mapping[str, Tuple],
  116. out_folder: str, report_folder: str):
  117. """
  118. Save code file and report.
  119. Args:
  120. model_name (str): Model name.
  121. code_lines (dict): Code lines.
  122. out_folder (str): Output folder.
  123. report_folder (str): Report output folder.
  124. """
  125. flags = os.O_WRONLY | os.O_CREAT | os.O_EXCL
  126. modes = stat.S_IRUSR | stat.S_IWUSR
  127. modes_usr = stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR
  128. out_folder = os.path.realpath(out_folder)
  129. if not report_folder:
  130. report_folder = out_folder
  131. else:
  132. report_folder = os.path.realpath(report_folder)
  133. if not os.path.exists(out_folder):
  134. os.makedirs(out_folder, modes_usr)
  135. if not os.path.exists(report_folder):
  136. os.makedirs(report_folder, modes_usr)
  137. for file_name in code_lines:
  138. code, report, trainable_weights, weight_map = code_lines[file_name]
  139. code_file_path = os.path.realpath(os.path.join(out_folder, f"{model_name}.py"))
  140. report_file_path = os.path.realpath(os.path.join(report_folder, f"report_of_{model_name}.txt"))
  141. try:
  142. if os.path.exists(code_file_path):
  143. raise ScriptGenerationError("Code file with the same name already exists.")
  144. with os.fdopen(os.open(code_file_path, flags, modes), 'w') as file:
  145. file.write(code)
  146. except (IOError, FileExistsError) as error:
  147. raise ScriptGenerationError(str(error))
  148. try:
  149. if os.path.exists(report_file_path):
  150. raise ReportGenerationError("Report file with the same name already exists.")
  151. with os.fdopen(os.open(report_file_path, flags, stat.S_IRUSR), "w") as rpt_f:
  152. rpt_f.write(report)
  153. except (IOError, FileExistsError) as error:
  154. raise ReportGenerationError(str(error))
  155. save_checkpoint = getattr(import_module("mindspore.train.serialization"), "save_checkpoint")
  156. for idx, trainable_weight in enumerate(trainable_weights):
  157. if len(trainable_weights) > 1:
  158. ckpt_file_path = os.path.realpath(os.path.join(out_folder, f"{model_name}_{idx}.ckpt"))
  159. else:
  160. ckpt_file_path = os.path.realpath(os.path.join(out_folder, f"{model_name}.ckpt"))
  161. if os.path.exists(ckpt_file_path):
  162. raise CheckPointGenerationError("Checkpoint file with the same name already exists.")
  163. try:
  164. save_checkpoint(trainable_weight, ckpt_file_path)
  165. except TypeError as error:
  166. raise CheckPointGenerationError(str(error))
  167. weight_map_path = os.path.realpath(os.path.join(report_folder, f"weight_map_of_{model_name}.json"))
  168. try:
  169. if os.path.exists(weight_map_path):
  170. raise WeightMapGenerationError("Weight map file with the same name already exists.")
  171. with os.fdopen(os.open(weight_map_path, flags, stat.S_IRUSR), 'w') as map_f:
  172. weight_map_json = {f"{model_name}": weight_map}
  173. json.dump(weight_map_json, map_f)
  174. except (IOError, FileExistsError) as error:
  175. raise WeightMapGenerationError(str(error))
  176. def onnx_satisfied():
  177. """Validate ONNX , ONNXRUNTIME, ONNXOPTIMIZER installation."""
  178. if not find_spec("onnx") or not find_spec("onnxruntime") or not find_spec("onnxoptimizer"):
  179. return False
  180. return True
  181. def lib_version_satisfied(current_ver: str, mini_ver_limited: str,
  182. newest_ver_limited: str = ""):
  183. """
  184. Check python lib version whether is satisfied.
  185. Notes:
  186. Version number must be format of x.x.x, e.g. 1.1.0.
  187. Args:
  188. current_ver (str): Current lib version.
  189. mini_ver_limited (str): Mini lib version.
  190. newest_ver_limited (str): Newest lib version.
  191. Returns:
  192. bool, true or false.
  193. """
  194. required_version_number_len = 3
  195. if len(list(current_ver.split("."))) != required_version_number_len or \
  196. len(list(mini_ver_limited.split("."))) != required_version_number_len or \
  197. (newest_ver_limited and len(newest_ver_limited.split(".")) != required_version_number_len):
  198. raise ValueError("Version number must be format of x.x.x.")
  199. if current_ver < mini_ver_limited or (newest_ver_limited and current_ver > newest_ver_limited):
  200. return False
  201. return True
  202. def get_dict_key_by_value(val, dic):
  203. """
  204. Return the first appeared key of a dictionary by given value.
  205. Args:
  206. val (Any): Value of the key.
  207. dic (dict): Dictionary to be checked.
  208. Returns:
  209. Any, key of the given value.
  210. """
  211. for d_key, d_val in dic.items():
  212. if d_val == val:
  213. return d_key
  214. return None
  215. def convert_bytes_string_to_string(bytes_str):
  216. """
  217. Convert a byte string to string by utf-8.
  218. Args:
  219. bytes_str (bytes): A bytes string.
  220. Returns:
  221. str, a str with utf-8 encoding.
  222. """
  223. if isinstance(bytes_str, bytes):
  224. return bytes_str.decode('utf-8')
  225. return bytes_str
  226. def get_framework_type(model_path):
  227. """Get framework type."""
  228. model_suffix = os.path.basename(model_path).split(".")[-1].lower()
  229. if model_suffix == ONNX_MODEL_SUFFIX:
  230. framework_type = FrameworkType.ONNX.value
  231. elif model_suffix == TENSORFLOW_MODEL_SUFFIX:
  232. framework_type = FrameworkType.TENSORFLOW.value
  233. else:
  234. framework_type = FrameworkType.UNKNOWN.value
  235. return framework_type
  236. def reset_init_or_construct(template, variable_slot, new_data, scope):
  237. """Reset init statement."""
  238. template[variable_slot][scope].clear()
  239. template[variable_slot][scope] += new_data
  240. return template
  241. def replace_string_in_list(str_list: list, original_str: str, target_str: str):
  242. """
  243. Replace a string in a list by provided string.
  244. Args:
  245. str_list (list): A list contains the string to be replaced.
  246. original_str (str): The string to be replaced.
  247. target_str (str): The replacement of string.
  248. Returns,
  249. list, the original list with replaced string.
  250. """
  251. return [s.replace(original_str, target_str) for s in str_list]
  252. def get_third_part_lib_validation_error_info(lib_list):
  253. """Get error info when not satisfying third part lib validation."""
  254. error_info = None
  255. link_str = ', '
  256. for idx, lib in enumerate(lib_list):
  257. if idx == len(lib_list) - 1:
  258. link_str = ' and '
  259. lib_version_required = THIRD_PART_VERSION[lib]
  260. if len(lib_version_required) == 2:
  261. lib_version_required_min = lib_version_required[0]
  262. lib_version_required_max = lib_version_required[1]
  263. if lib_version_required_min == lib_version_required_max:
  264. info = f"{lib}(=={lib_version_required_min})"
  265. else:
  266. info = f"{lib}(>={lib_version_required_min} and <{lib_version_required_max})"
  267. else:
  268. info = f"{lib}(>={lib_version_required[0]})"
  269. if not error_info:
  270. error_info = info
  271. else:
  272. error_info = link_str.join((error_info, info))
  273. return error_info