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.1 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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. name=node_name&detail=data&shape=[0, 0, :, :]&tolerance=0.5
  98. """
  99. name = request.args.get('name')
  100. detail = request.args.get('detail', 'data')
  101. shape = request.args.get('shape')
  102. tolerance = request.args.get('tolerance', '0')
  103. reply = _wrap_reply(BACKEND_SERVER.tensor_comparisons, name, shape, detail, tolerance)
  104. return reply
  105. @BLUEPRINT.route("/debugger/retrieve", methods=["POST"])
  106. def retrieve():
  107. """
  108. Retrieve data according to mode and params.
  109. Returns:
  110. str, the required data.
  111. Examples:
  112. >>> POST http://xxxx/v1/mindinsight/debugger/retrieve
  113. """
  114. body = _read_post_request(request)
  115. mode = body.get('mode')
  116. params = body.get('params')
  117. reply = _wrap_reply(BACKEND_SERVER.retrieve, mode, params)
  118. return reply
  119. @BLUEPRINT.route("/debugger/retrieve_tensor_history", methods=["POST"])
  120. def retrieve_tensor_history():
  121. """
  122. Retrieve data according to mode and params.
  123. Returns:
  124. str, the required data.
  125. Examples:
  126. >>> POST http://xxxx/v1/mindinsight/debugger/retrieve_tensor_history
  127. """
  128. body = _read_post_request(request)
  129. name = body.get('name')
  130. reply = _wrap_reply(BACKEND_SERVER.retrieve_tensor_history, name)
  131. return reply
  132. @BLUEPRINT.route("/debugger/tensors", methods=["GET"])
  133. def retrieve_tensor_value():
  134. """
  135. Retrieve tensor value according to name and shape.
  136. Returns:
  137. str, the required data.
  138. Examples:
  139. >>> GET http://xxxx/v1/mindinsight/debugger/tensors?name=node_name&detail=data&shape=[1,1,:,:]
  140. """
  141. name = request.args.get('name')
  142. detail = request.args.get('detail')
  143. shape = request.args.get('shape')
  144. reply = _wrap_reply(BACKEND_SERVER.retrieve_tensor_value, name, detail, shape)
  145. return reply
  146. @BLUEPRINT.route("/debugger/create_watchpoint", methods=["POST"])
  147. def create_watchpoint():
  148. """
  149. Create watchpoint.
  150. Returns:
  151. str, watchpoint id.
  152. Raises:
  153. MindInsightException: If method fails to be called.
  154. ParamValueError: If parsing json data search_condition fails.
  155. Examples:
  156. >>> POST http://xxxx/v1/mindinsight/debugger/create_watchpoint
  157. """
  158. body = _read_post_request(request)
  159. condition = body.get('condition')
  160. watch_nodes = body.get('watch_nodes')
  161. watch_point_id = body.get('watch_point_id')
  162. reply = _wrap_reply(BACKEND_SERVER.create_watchpoint, condition, watch_nodes, watch_point_id)
  163. return reply
  164. @BLUEPRINT.route("/debugger/update_watchpoint", methods=["POST"])
  165. def update_watchpoint():
  166. """
  167. Update watchpoint.
  168. Returns:
  169. str, reply message.
  170. Raises:
  171. MindInsightException: If method fails to be called.
  172. ParamValueError: If parsing json data search_condition fails.
  173. Examples:
  174. >>> POST http://xxxx/v1/mindinsight/debugger/update_watchpoint
  175. """
  176. body = _read_post_request(request)
  177. watch_point_id = body.get('watch_point_id')
  178. watch_nodes = body.get('watch_nodes')
  179. mode = body.get('mode')
  180. name = body.get('name')
  181. reply = _wrap_reply(BACKEND_SERVER.update_watchpoint, watch_point_id, watch_nodes, mode, name)
  182. return reply
  183. @BLUEPRINT.route("/debugger/delete_watchpoint", methods=["POST"])
  184. def delete_watchpoint():
  185. """
  186. delete watchpoint.
  187. Returns:
  188. str, reply message.
  189. Raises:
  190. MindInsightException: If method fails to be called.
  191. ParamValueError: If parsing json data search_condition fails.
  192. Examples:
  193. >>> POST http://xxxx/v1/mindinsight/debugger/delete_watchpoint
  194. """
  195. body = _read_post_request(request)
  196. watch_point_id = body.get('watch_point_id')
  197. reply = _wrap_reply(BACKEND_SERVER.delete_watchpoint, watch_point_id)
  198. return reply
  199. @BLUEPRINT.route("/debugger/control", methods=["POST"])
  200. def control():
  201. """
  202. Control request.
  203. Returns:
  204. str, reply message.
  205. Raises:
  206. MindInsightException: If method fails to be called.
  207. ParamValueError: If parsing json data search_condition fails.
  208. Examples:
  209. >>> POST http://xxxx/v1/mindinsight/debugger/control
  210. """
  211. params = _read_post_request(request)
  212. reply = _wrap_reply(BACKEND_SERVER.control, params)
  213. return reply
  214. BACKEND_SERVER = _initialize_debugger_server()
  215. def init_module(app):
  216. """
  217. Init module entry.
  218. Args:
  219. app (Flask): The application obj.
  220. """
  221. app.register_blueprint(BLUEPRINT)
  222. if BACKEND_SERVER:
  223. BACKEND_SERVER.start()