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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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. port = settings.DEBUGGER_PORT if hasattr(settings, 'DEBUGGER_PORT') else None
  26. enable_debugger = settings.ENABLE_DEBUGGER if hasattr(settings, 'ENABLE_DEBUGGER') else False
  27. server = None
  28. if port and enable_debugger:
  29. server = DebuggerServer(port)
  30. return server
  31. def _read_post_request(post_request):
  32. """
  33. Extract the body of post request.
  34. Args:
  35. post_request (object): The post request.
  36. Returns:
  37. dict, the deserialized body of request.
  38. """
  39. body = post_request.stream.read()
  40. try:
  41. body = json.loads(body if body else "{}")
  42. except Exception:
  43. raise ParamValueError("Json data parse failed.")
  44. return body
  45. def _wrap_reply(func, *args, **kwargs):
  46. """Serialize reply."""
  47. reply = func(*args, **kwargs)
  48. return jsonify(reply)
  49. @BLUEPRINT.route("/debugger/poll_data", methods=["GET"])
  50. def poll_data():
  51. """
  52. Wait for data to be updated on UI.
  53. Get data from server and display the change on UI.
  54. Returns:
  55. str, the updated data.
  56. Examples:
  57. >>> Get http://xxxx/v1/mindinsight/debugger/poll_data?pos=xx
  58. """
  59. pos = request.args.get('pos')
  60. reply = _wrap_reply(BACKEND_SERVER.poll_data, pos)
  61. return reply
  62. @BLUEPRINT.route("/debugger/search", methods=["GET"])
  63. def search():
  64. """
  65. Search nodes in specified watchpoint.
  66. Returns:
  67. str, the required data.
  68. Examples:
  69. >>> Get http://xxxx/v1/mindinsight/debugger/retrive?mode=all
  70. """
  71. name = request.args.get('name')
  72. watch_point_id = int(request.args.get('watch_point_id', 0))
  73. reply = _wrap_reply(BACKEND_SERVER.search, name, watch_point_id)
  74. return reply
  75. @BLUEPRINT.route("/debugger/retrieve_node_by_bfs", methods=["GET"])
  76. def retrieve_node_by_bfs():
  77. """
  78. Search node by bfs.
  79. Returns:
  80. str, the required data.
  81. Examples:
  82. >>> Get http://xxxx/v1/mindinsight/debugger/retrieve_node_by_bfs?name=node_name&ascend=true
  83. """
  84. name = request.args.get('name')
  85. ascend = request.args.get('ascend', 'false')
  86. ascend = ascend == 'true'
  87. reply = _wrap_reply(BACKEND_SERVER.retrieve_node_by_bfs, name, ascend)
  88. return reply
  89. @BLUEPRINT.route("/debugger/tensor-comparisons", methods=["GET"])
  90. def tensor_comparisons():
  91. """
  92. Get tensor comparisons.
  93. Returns:
  94. str, the required data.
  95. Examples:
  96. >>> Get http://xxxx/v1/mindinsight/debugger/tensor-comparisons
  97. """
  98. name = request.args.get('name')
  99. detail = request.args.get('detail', 'data')
  100. shape = request.args.get('shape')
  101. tolerance = request.args.get('tolerance', '0')
  102. reply = _wrap_reply(BACKEND_SERVER.tensor_comparisons, name, shape, detail, tolerance)
  103. return reply
  104. @BLUEPRINT.route("/debugger/retrieve", methods=["POST"])
  105. def retrieve():
  106. """
  107. Retrieve data according to mode and params.
  108. Returns:
  109. str, the required data.
  110. Examples:
  111. >>> POST http://xxxx/v1/mindinsight/debugger/retrieve
  112. """
  113. body = _read_post_request(request)
  114. mode = body.get('mode')
  115. params = body.get('params')
  116. reply = _wrap_reply(BACKEND_SERVER.retrieve, mode, params)
  117. return reply
  118. @BLUEPRINT.route("/debugger/retrieve_tensor_history", methods=["POST"])
  119. def retrieve_tensor_history():
  120. """
  121. Retrieve data according to mode and params.
  122. Returns:
  123. str, the required data.
  124. Examples:
  125. >>> POST http://xxxx/v1/mindinsight/debugger/retrieve_tensor_history
  126. """
  127. body = _read_post_request(request)
  128. name = body.get('name')
  129. reply = _wrap_reply(BACKEND_SERVER.retrieve_tensor_history, name)
  130. return reply
  131. @BLUEPRINT.route("/debugger/tensors", methods=["GET"])
  132. def retrieve_tensor_value():
  133. """
  134. Retrieve tensor value according to name and shape.
  135. Returns:
  136. str, the required data.
  137. Examples:
  138. >>> GET http://xxxx/v1/mindinsight/debugger/tensors?name=node_name&detail=data&shape=[1,1,:,:]
  139. """
  140. name = request.args.get('name')
  141. detail = request.args.get('detail')
  142. shape = request.args.get('shape')
  143. reply = _wrap_reply(BACKEND_SERVER.retrieve_tensor_value, name, detail, shape)
  144. return reply
  145. @BLUEPRINT.route("/debugger/create_watchpoint", methods=["POST"])
  146. def create_watchpoint():
  147. """
  148. Create watchpoint.
  149. Returns:
  150. str, watchpoint id.
  151. Raises:
  152. MindInsightException: If method fails to be called.
  153. ParamValueError: If parsing json data search_condition fails.
  154. Examples:
  155. >>> POST http://xxxx/v1/mindinsight/debugger/create_watchpoint
  156. """
  157. body = _read_post_request(request)
  158. condition = body.get('condition')
  159. watch_nodes = body.get('watch_nodes')
  160. watch_point_id = body.get('watch_point_id')
  161. reply = _wrap_reply(BACKEND_SERVER.create_watchpoint, condition, watch_nodes, watch_point_id)
  162. return reply
  163. @BLUEPRINT.route("/debugger/update_watchpoint", methods=["POST"])
  164. def update_watchpoint():
  165. """
  166. Update watchpoint.
  167. Returns:
  168. str, reply message.
  169. Raises:
  170. MindInsightException: If method fails to be called.
  171. ParamValueError: If parsing json data search_condition fails.
  172. Examples:
  173. >>> POST http://xxxx/v1/mindinsight/debugger/update_watchpoint
  174. """
  175. body = _read_post_request(request)
  176. watch_point_id = body.get('watch_point_id')
  177. watch_nodes = body.get('watch_nodes')
  178. mode = body.get('mode')
  179. name = body.get('name')
  180. reply = _wrap_reply(BACKEND_SERVER.update_watchpoint, watch_point_id, watch_nodes, mode, name)
  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. ParamValueError: If parsing json data search_condition fails.
  191. Examples:
  192. >>> POST http://xxxx/v1/mindinsight/debugger/delete_watchpoint
  193. """
  194. body = _read_post_request(request)
  195. watch_point_id = body.get('watch_point_id')
  196. reply = _wrap_reply(BACKEND_SERVER.delete_watchpoint, watch_point_id)
  197. return reply
  198. @BLUEPRINT.route("/debugger/control", methods=["POST"])
  199. def control():
  200. """
  201. Control request.
  202. Returns:
  203. str, reply message.
  204. Raises:
  205. MindInsightException: If method fails to be called.
  206. ParamValueError: If parsing json data search_condition fails.
  207. Examples:
  208. >>> POST http://xxxx/v1/mindinsight/debugger/control
  209. """
  210. params = _read_post_request(request)
  211. reply = _wrap_reply(BACKEND_SERVER.control, params)
  212. return reply
  213. BACKEND_SERVER = _initialize_debugger_server()
  214. def init_module(app):
  215. """
  216. Init module entry.
  217. Args:
  218. app (Flask): The application obj.
  219. """
  220. app.register_blueprint(BLUEPRINT)
  221. if BACKEND_SERVER:
  222. BACKEND_SERVER.start()