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.

debugger_api.py 9.4 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. # Copyright 2020 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. """Debugger restful api."""
  16. import json
  17. from flask import Blueprint, jsonify, request
  18. from mindinsight.conf import settings
  19. from mindinsight.debugger.debugger_server import DebuggerServer
  20. from mindinsight.utils.exceptions import ParamValueError
  21. BLUEPRINT = Blueprint("debugger", __name__,
  22. url_prefix=settings.URL_PATH_PREFIX + settings.API_PREFIX)
  23. def _initialize_debugger_server():
  24. """Initialize a debugger server instance."""
  25. enable_debugger = settings.ENABLE_DEBUGGER if hasattr(settings, 'ENABLE_DEBUGGER') else False
  26. server = None
  27. if enable_debugger:
  28. server = DebuggerServer()
  29. return server
  30. def _read_post_request(post_request):
  31. """
  32. Extract the body of post request.
  33. Args:
  34. post_request (object): The post request.
  35. Returns:
  36. dict, the deserialized body of request.
  37. """
  38. body = post_request.stream.read()
  39. try:
  40. body = json.loads(body if body else "{}")
  41. except Exception:
  42. raise ParamValueError("Json data parse failed.")
  43. return body
  44. def _wrap_reply(func, *args, **kwargs):
  45. """Serialize reply."""
  46. reply = func(*args, **kwargs)
  47. return jsonify(reply)
  48. @BLUEPRINT.route("/debugger/poll-data", methods=["GET"])
  49. def poll_data():
  50. """
  51. Wait for data to be updated on UI.
  52. Get data from server and display the change on UI.
  53. Returns:
  54. str, the updated data.
  55. Examples:
  56. >>> Get http://xxxx/v1/mindinsight/debugger/poll-data?pos=xx
  57. """
  58. pos = request.args.get('pos')
  59. reply = _wrap_reply(BACKEND_SERVER.poll_data, pos)
  60. return reply
  61. @BLUEPRINT.route("/debugger/search", methods=["GET"])
  62. def search():
  63. """
  64. Search nodes in specified watchpoint.
  65. Returns:
  66. str, the required data.
  67. Examples:
  68. >>> Get http://xxxx/v1/mindinsight/debugger/search?name=mock_name&watch_point_id=1
  69. """
  70. name = request.args.get('name')
  71. graph_name = request.args.get('graph_name')
  72. watch_point_id = int(request.args.get('watch_point_id', 0))
  73. node_category = request.args.get('node_category')
  74. reply = _wrap_reply(BACKEND_SERVER.search, {'name': name,
  75. 'graph_name': graph_name,
  76. 'watch_point_id': watch_point_id,
  77. 'node_category': node_category})
  78. return reply
  79. @BLUEPRINT.route("/debugger/retrieve_node_by_bfs", methods=["GET"])
  80. def retrieve_node_by_bfs():
  81. """
  82. Search node by bfs.
  83. Returns:
  84. str, the required data.
  85. Examples:
  86. >>> Get http://xxxx/v1/mindinsight/debugger/retrieve_node_by_bfs?name=node_name&ascend=true
  87. """
  88. name = request.args.get('name')
  89. graph_name = request.args.get('graph_name')
  90. ascend = request.args.get('ascend', 'false')
  91. ascend = ascend == 'true'
  92. reply = _wrap_reply(BACKEND_SERVER.retrieve_node_by_bfs, name, graph_name, ascend)
  93. return reply
  94. @BLUEPRINT.route("/debugger/tensor-comparisons", methods=["GET"])
  95. def tensor_comparisons():
  96. """
  97. Get tensor comparisons.
  98. Returns:
  99. str, the required data.
  100. Examples:
  101. >>> Get http://xxxx/v1/mindinsight/debugger/tensor-comparisons
  102. """
  103. name = request.args.get('name')
  104. detail = request.args.get('detail', 'data')
  105. shape = request.args.get('shape')
  106. tolerance = request.args.get('tolerance', '0')
  107. reply = _wrap_reply(BACKEND_SERVER.tensor_comparisons, name, shape, detail, tolerance)
  108. return reply
  109. @BLUEPRINT.route("/debugger/retrieve", methods=["POST"])
  110. def retrieve():
  111. """
  112. Retrieve data according to mode and params.
  113. Returns:
  114. str, the required data.
  115. Examples:
  116. >>> POST http://xxxx/v1/mindinsight/debugger/retrieve
  117. """
  118. body = _read_post_request(request)
  119. mode = body.get('mode')
  120. params = body.get('params')
  121. reply = _wrap_reply(BACKEND_SERVER.retrieve, mode, params)
  122. return reply
  123. @BLUEPRINT.route("/debugger/tensor-history", methods=["POST"])
  124. def retrieve_tensor_history():
  125. """
  126. Retrieve data according to mode and params.
  127. Returns:
  128. str, the required data.
  129. Examples:
  130. >>> POST http://xxxx/v1/mindinsight/debugger/tensor-history
  131. """
  132. body = _read_post_request(request)
  133. name = body.get('name')
  134. graph_name = body.get('graph_name')
  135. reply = _wrap_reply(BACKEND_SERVER.retrieve_tensor_history, name, graph_name)
  136. return reply
  137. @BLUEPRINT.route("/debugger/tensors", methods=["GET"])
  138. def retrieve_tensor_value():
  139. """
  140. Retrieve tensor value according to name and shape.
  141. Returns:
  142. str, the required data.
  143. Examples:
  144. >>> GET http://xxxx/v1/mindinsight/debugger/tensors?name=tensor_name&detail=data&shape=[1,1,:,:]
  145. """
  146. name = request.args.get('name')
  147. detail = request.args.get('detail')
  148. shape = request.args.get('shape')
  149. graph_name = request.args.get('graph_name')
  150. prev = bool(request.args.get('prev') == 'true')
  151. reply = _wrap_reply(BACKEND_SERVER.retrieve_tensor_value, name, detail, shape, graph_name, prev)
  152. return reply
  153. @BLUEPRINT.route("/debugger/create-watchpoint", methods=["POST"])
  154. def create_watchpoint():
  155. """
  156. Create watchpoint.
  157. Returns:
  158. str, watchpoint id.
  159. Raises:
  160. MindInsightException: If method fails to be called.
  161. Examples:
  162. >>> POST http://xxxx/v1/mindinsight/debugger/create-watchpoint
  163. """
  164. params = _read_post_request(request)
  165. params['watch_condition'] = params.pop('condition', None)
  166. reply = _wrap_reply(BACKEND_SERVER.create_watchpoint, params)
  167. return reply
  168. @BLUEPRINT.route("/debugger/update-watchpoint", methods=["POST"])
  169. def update_watchpoint():
  170. """
  171. Update watchpoint.
  172. Returns:
  173. str, reply message.
  174. Raises:
  175. MindInsightException: If method fails to be called.
  176. Examples:
  177. >>> POST http://xxxx/v1/mindinsight/debugger/update-watchpoint
  178. """
  179. params = _read_post_request(request)
  180. reply = _wrap_reply(BACKEND_SERVER.update_watchpoint, params)
  181. return reply
  182. @BLUEPRINT.route("/debugger/delete-watchpoint", methods=["POST"])
  183. def delete_watchpoint():
  184. """
  185. delete watchpoint.
  186. Returns:
  187. str, reply message.
  188. Raises:
  189. MindInsightException: If method fails to be called.
  190. Examples:
  191. >>> POST http://xxxx/v1/mindinsight/debugger/delete-watchpoint
  192. """
  193. body = _read_post_request(request)
  194. watch_point_id = body.get('watch_point_id')
  195. reply = _wrap_reply(BACKEND_SERVER.delete_watchpoint, watch_point_id)
  196. return reply
  197. @BLUEPRINT.route("/debugger/control", methods=["POST"])
  198. def control():
  199. """
  200. Control request.
  201. Returns:
  202. str, reply message.
  203. Raises:
  204. MindInsightException: If method fails to be called.
  205. Examples:
  206. >>> POST http://xxxx/v1/mindinsight/debugger/control
  207. """
  208. params = _read_post_request(request)
  209. reply = _wrap_reply(BACKEND_SERVER.control, params)
  210. return reply
  211. @BLUEPRINT.route("/debugger/recheck", methods=["POST"])
  212. def recheck():
  213. """
  214. Recheck request.
  215. Returns:
  216. str, reply message.
  217. Raises:
  218. MindInsightException: If method fails to be called.
  219. Examples:
  220. >>> POST http://xxxx/v1/mindinsight/debugger/recheck
  221. """
  222. reply = _wrap_reply(BACKEND_SERVER.recheck)
  223. return reply
  224. @BLUEPRINT.route("/debugger/tensor-graphs", methods=["GET"])
  225. def retrieve_tensor_graph():
  226. """
  227. Retrieve tensor value according to name and shape.
  228. Returns:
  229. str, the required data.
  230. Examples:
  231. >>> GET http://xxxx/v1/mindinsight/debugger/tensor-graphs?tensor_name=tensor_name&graph_name=graph_name
  232. """
  233. tensor_name = request.args.get('tensor_name')
  234. graph_name = request.args.get('graph_name')
  235. reply = _wrap_reply(BACKEND_SERVER.retrieve_tensor_graph, tensor_name, graph_name)
  236. return reply
  237. @BLUEPRINT.route("/debugger/tensor-hits", methods=["GET"])
  238. def retrieve_tensor_hits():
  239. """
  240. Retrieve tensor value according to name and shape.
  241. Returns:
  242. str, the required data.
  243. Examples:
  244. >>> GET http://xxxx/v1/mindinsight/debugger/tensor-hits?tensor_name=tensor_name&graph_name=graph_name
  245. """
  246. tensor_name = request.args.get('tensor_name')
  247. graph_name = request.args.get('graph_name')
  248. reply = _wrap_reply(BACKEND_SERVER.retrieve_tensor_hits, tensor_name, graph_name)
  249. return reply
  250. BACKEND_SERVER = _initialize_debugger_server()
  251. def init_module(app):
  252. """
  253. Init module entry.
  254. Args:
  255. app (Flask): The application obj.
  256. """
  257. app.register_blueprint(BLUEPRINT)
  258. if BACKEND_SERVER:
  259. BACKEND_SERVER.start()