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.

lineage_api.py 6.1 kB

5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. # Copyright 2019 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. """Lineage restful api."""
  16. import json
  17. import os
  18. from flask import Blueprint, jsonify, request
  19. from mindinsight.conf import settings
  20. from mindinsight.datavisual.utils.tools import get_train_id
  21. from mindinsight.lineagemgr import filter_summary_lineage, get_summary_lineage
  22. from mindinsight.lineagemgr.common.validator.validate import validate_path
  23. from mindinsight.utils.exceptions import MindInsightException, ParamValueError
  24. BLUEPRINT = Blueprint("lineage", __name__, url_prefix=settings.URL_PREFIX.rstrip("/"))
  25. @BLUEPRINT.route("/models/model_lineage", methods=["POST"])
  26. def search_model():
  27. """
  28. Get model lineage info.
  29. Get model info by summary base dir return a model lineage information list of dict
  30. contains model's all kinds of param and count of summary log.
  31. Returns:
  32. str, the model lineage information.
  33. Raises:
  34. MindInsightException: If method fails to be called.
  35. ParamValueError: If parsing json data search_condition fails.
  36. Examples:
  37. >>> POST http://xxxx/v1/mindinsight/models/model_lineage
  38. """
  39. search_condition = request.stream.read()
  40. try:
  41. search_condition = json.loads(search_condition if search_condition else "{}")
  42. except Exception:
  43. raise ParamValueError("Json data parse failed.")
  44. model_lineage_info = _get_lineage_info(
  45. lineage_type="model",
  46. search_condition=search_condition
  47. )
  48. return jsonify(model_lineage_info)
  49. @BLUEPRINT.route("/datasets/dataset_lineage", methods=["POST"])
  50. def get_datasets_lineage():
  51. """
  52. Get dataset lineage.
  53. Returns:
  54. str, the dataset lineage information.
  55. Raises:
  56. MindInsightException: If method fails to be called.
  57. ParamValueError: If parsing json data search_condition fails.
  58. Examples:
  59. >>> POST http://xxxx/v1/minddata/datasets/dataset_lineage
  60. """
  61. search_condition = request.stream.read()
  62. try:
  63. search_condition = json.loads(search_condition if search_condition else "{}")
  64. except Exception:
  65. raise ParamValueError("Json data parse failed.")
  66. dataset_lineage_info = _get_lineage_info(
  67. lineage_type="dataset",
  68. search_condition=search_condition
  69. )
  70. return jsonify(dataset_lineage_info)
  71. def _get_lineage_info(lineage_type, search_condition):
  72. """
  73. Get lineage info for dataset or model.
  74. Args:
  75. lineage_type (str): Lineage type, 'dataset' or 'model'.
  76. search_condition (dict): Search condition.
  77. Returns:
  78. dict, lineage info.
  79. Raises:
  80. MindInsightException: If method fails to be called.
  81. """
  82. if 'lineage_type' in search_condition:
  83. raise ParamValueError("Lineage type does not need to be assigned in a specific interface.")
  84. if lineage_type == 'dataset':
  85. search_condition.update({'lineage_type': 'dataset'})
  86. summary_base_dir = str(settings.SUMMARY_BASE_DIR)
  87. try:
  88. lineage_info = filter_summary_lineage(
  89. summary_base_dir, search_condition)
  90. lineages = lineage_info['object']
  91. summary_base_dir = os.path.realpath(summary_base_dir)
  92. length = len(summary_base_dir)
  93. for lineage in lineages:
  94. summary_dir = lineage['summary_dir']
  95. summary_dir = os.path.realpath(summary_dir)
  96. if summary_base_dir == summary_dir:
  97. relative_dir = './'
  98. else:
  99. relative_dir = os.path.join(os.curdir, summary_dir[length+1:])
  100. lineage['summary_dir'] = relative_dir
  101. except MindInsightException as exception:
  102. raise MindInsightException(exception.error, exception.message, http_code=400)
  103. return lineage_info
  104. @BLUEPRINT.route("/datasets/dataset_graph", methods=["GET"])
  105. def get_dataset_graph():
  106. """
  107. Get dataset graph.
  108. Returns:
  109. str, the dataset graph information.
  110. Raises:
  111. MindInsightException: If method fails to be called.
  112. ParamValueError: If summary_dir is invalid.
  113. Examples:
  114. >>> GET http://xxxx/v1/mindinsight/datasets/dataset_graph?train_id=xxx
  115. """
  116. summary_base_dir = str(settings.SUMMARY_BASE_DIR)
  117. summary_dir = get_train_id(request)
  118. if summary_dir.startswith('/'):
  119. validate_path(summary_dir)
  120. elif summary_dir.startswith('./'):
  121. summary_dir = os.path.join(summary_base_dir, summary_dir[2:])
  122. summary_dir = validate_path(summary_dir)
  123. else:
  124. raise ParamValueError(
  125. "Summary dir should be absolute path or "
  126. "relative path that relate to summary base dir."
  127. )
  128. try:
  129. dataset_graph = get_summary_lineage(
  130. summary_dir=summary_dir,
  131. keys=['dataset_graph']
  132. )
  133. except MindInsightException as exception:
  134. raise MindInsightException(exception.error, exception.message, http_code=400)
  135. if dataset_graph:
  136. summary_dir_result = dataset_graph.get('summary_dir')
  137. base_dir_len = len(summary_base_dir)
  138. if summary_base_dir == summary_dir_result:
  139. relative_dir = './'
  140. else:
  141. relative_dir = os.path.join(
  142. os.curdir, summary_dir[base_dir_len + 1:]
  143. )
  144. dataset_graph['summary_dir'] = relative_dir
  145. return jsonify(dataset_graph)
  146. def init_module(app):
  147. """
  148. Init module entry.
  149. Args:
  150. app (Flask): The application obj.
  151. """
  152. app.register_blueprint(BLUEPRINT)

MindInsight为MindSpore提供了简单易用的调优调试能力。在训练过程中,可以将标量、张量、图像、计算图、模型超参、训练耗时等数据记录到文件中,通过MindInsight可视化页面进行查看及分析。

Contributors (1)