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.

test_restful_api.py 32 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701
  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. """
  16. Function:
  17. Test query debugger restful api.
  18. Usage:
  19. pytest tests/st/func/debugger/test_restful_api.py
  20. """
  21. import os
  22. import pytest
  23. from mindinsight.conf import settings
  24. from mindinsight.debugger.common.utils import ServerStatus
  25. from tests.st.func.debugger.conftest import DEBUGGER_BASE_URL
  26. from tests.st.func.debugger.mock_ms_client import MockDebuggerClient
  27. from tests.st.func.debugger.utils import check_state, get_request_result, \
  28. send_and_compare_result
  29. def send_terminate_cmd(app_client):
  30. """Send terminate command to debugger client."""
  31. url = os.path.join(DEBUGGER_BASE_URL, 'control')
  32. body_data = {'mode': 'terminate'}
  33. send_and_compare_result(app_client, url, body_data)
  34. class TestAscendDebugger:
  35. """Test debugger on Ascend backend."""
  36. @classmethod
  37. def setup_class(cls):
  38. """Setup class."""
  39. cls._debugger_client = MockDebuggerClient(backend='Ascend')
  40. @pytest.mark.level0
  41. @pytest.mark.env_single
  42. @pytest.mark.platform_x86_cpu
  43. @pytest.mark.platform_arm_ascend_training
  44. @pytest.mark.platform_x86_gpu_training
  45. @pytest.mark.platform_x86_ascend_training
  46. def test_before_train_begin(self, app_client):
  47. """Test retrieve all."""
  48. url = 'retrieve'
  49. body_data = {'mode': 'all'}
  50. expect_file = 'before_train_begin.json'
  51. send_and_compare_result(app_client, url, body_data, expect_file)
  52. @pytest.mark.level0
  53. @pytest.mark.env_single
  54. @pytest.mark.platform_x86_cpu
  55. @pytest.mark.platform_arm_ascend_training
  56. @pytest.mark.platform_x86_gpu_training
  57. @pytest.mark.platform_x86_ascend_training
  58. @pytest.mark.parametrize("body_data, expect_file", [
  59. ({'mode': 'all'}, 'retrieve_all.json'),
  60. ({'mode': 'node', 'params': {'name': 'Default'}}, 'retrieve_scope_node.json'),
  61. ({'mode': 'node', 'params': {'name': 'Default/optimizer-Momentum/Parameter[18]_7'}},
  62. 'retrieve_aggregation_scope_node.json'),
  63. ({'mode': 'node', 'params': {
  64. 'name': 'Default/TransData-op99',
  65. 'single_node': True}}, 'retrieve_single_node.json'),
  66. ({'mode': 'watchpoint_hit'}, 'retrieve_empty_watchpoint_hit_list')
  67. ])
  68. def test_retrieve_when_train_begin(self, app_client, body_data, expect_file):
  69. """Test retrieve when train_begin."""
  70. url = 'retrieve'
  71. with self._debugger_client.get_thread_instance():
  72. check_state(app_client)
  73. send_and_compare_result(app_client, url, body_data, expect_file)
  74. send_terminate_cmd(app_client)
  75. def test_get_conditions(self, app_client):
  76. """Test get conditions for ascend."""
  77. url = '/v1/mindinsight/conditionmgr/train-jobs/train-id/conditions'
  78. body_data = {}
  79. expect_file = 'get_conditions_for_ascend.json'
  80. with self._debugger_client.get_thread_instance():
  81. check_state(app_client)
  82. send_and_compare_result(app_client, url, body_data, expect_file, method='get', full_url=True)
  83. send_terminate_cmd(app_client)
  84. @pytest.mark.level0
  85. @pytest.mark.env_single
  86. @pytest.mark.platform_x86_cpu
  87. @pytest.mark.platform_arm_ascend_training
  88. @pytest.mark.platform_x86_gpu_training
  89. @pytest.mark.platform_x86_ascend_training
  90. @pytest.mark.parametrize("body_data, expect_file", [
  91. ({'mode': 'all'}, 'multi_retrieve_all.json'),
  92. ({'mode': 'node', 'params': {'name': 'Default', 'graph_name': 'graph_1'}}, 'retrieve_scope_node.json'),
  93. ({'mode': 'node', 'params': {'name': 'graph_0'}}, 'multi_retrieve_scope_node.json'),
  94. ({'mode': 'node', 'params': {'name': 'graph_0/Default/optimizer-Momentum/Parameter[18]_7'}},
  95. 'multi_retrieve_aggregation_scope_node.json'),
  96. ({'mode': 'node', 'params': {
  97. 'name': 'graph_0/Default/TransData-op99',
  98. 'single_node': True}}, 'multi_retrieve_single_node.json'),
  99. ({'mode': 'node', 'params': {
  100. 'name': 'Default/TransData-op99',
  101. 'single_node': True, 'graph_name': 'graph_0'}}, 'retrieve_single_node.json')
  102. ])
  103. def test_multi_retrieve_when_train_begin(self, app_client, body_data, expect_file):
  104. """Test retrieve when train_begin."""
  105. url = 'retrieve'
  106. debugger_client = MockDebuggerClient(backend='Ascend', graph_num=2)
  107. with debugger_client.get_thread_instance():
  108. check_state(app_client)
  109. send_and_compare_result(app_client, url, body_data, expect_file)
  110. send_terminate_cmd(app_client)
  111. @pytest.mark.level0
  112. @pytest.mark.env_single
  113. @pytest.mark.platform_x86_cpu
  114. @pytest.mark.platform_arm_ascend_training
  115. @pytest.mark.platform_x86_gpu_training
  116. @pytest.mark.platform_x86_ascend_training
  117. def test_create_and_delete_watchpoint(self, app_client):
  118. """Test create and delete watchpoint."""
  119. with self._debugger_client.get_thread_instance():
  120. check_state(app_client)
  121. conditions = [
  122. {'id': 'max_gt', 'params': [{'name': 'param', 'value': 1.0}]},
  123. {'id': 'max_lt', 'params': [{'name': 'param', 'value': -1.0}]},
  124. {'id': 'min_gt', 'params': [{'name': 'param', 'value': 1e+32}]},
  125. {'id': 'min_lt', 'params': [{'name': 'param', 'value': -1e+32}]},
  126. {'id': 'max_min_gt', 'params': [{'name': 'param', 'value': 0}]},
  127. {'id': 'max_min_lt', 'params': [{'name': 'param', 'value': 0}]},
  128. {'id': 'mean_gt', 'params': [{'name': 'param', 'value': 0}]},
  129. {'id': 'mean_lt', 'params': [{'name': 'param', 'value': 0}]},
  130. {'id': 'inf', 'params': []},
  131. {'id': 'overflow', 'params': []},
  132. ]
  133. for idx, condition in enumerate(conditions):
  134. create_watchpoint(app_client, condition, idx + 1)
  135. # delete 4-th watchpoint
  136. url = 'delete_watchpoint'
  137. body_data = {'watch_point_id': 4}
  138. get_request_result(app_client, url, body_data)
  139. # test watchpoint list
  140. url = 'retrieve'
  141. body_data = {'mode': 'watchpoint'}
  142. expect_file = 'create_and_delete_watchpoint.json'
  143. send_and_compare_result(app_client, url, body_data, expect_file)
  144. send_terminate_cmd(app_client)
  145. @pytest.mark.level0
  146. @pytest.mark.env_single
  147. @pytest.mark.platform_x86_cpu
  148. @pytest.mark.platform_arm_ascend_training
  149. @pytest.mark.platform_x86_gpu_training
  150. @pytest.mark.platform_x86_ascend_training
  151. def test_update_watchpoint(self, app_client):
  152. """Test retrieve when train_begin."""
  153. watch_point_id = 1
  154. leaf_node_name = 'Default/optimizer-Momentum/Parameter[18]_7/moments.fc3.bias'
  155. with self._debugger_client.get_thread_instance():
  156. check_state(app_client)
  157. condition = {'id': 'inf', 'params': []}
  158. create_watchpoint(app_client, condition, watch_point_id)
  159. # update watchpoint watchpoint list
  160. url = 'update_watchpoint'
  161. body_data = {'watch_point_id': watch_point_id,
  162. 'watch_nodes': [leaf_node_name],
  163. 'mode': 0}
  164. get_request_result(app_client, url, body_data)
  165. # get updated nodes
  166. url = 'search'
  167. body_data = {'name': leaf_node_name, 'watch_point_id': watch_point_id}
  168. expect_file = 'search_unwatched_leaf_node.json'
  169. send_and_compare_result(app_client, url, body_data, expect_file, method='get')
  170. send_terminate_cmd(app_client)
  171. @pytest.mark.level0
  172. @pytest.mark.env_single
  173. @pytest.mark.platform_x86_cpu
  174. @pytest.mark.platform_arm_ascend_training
  175. @pytest.mark.platform_x86_gpu_training
  176. @pytest.mark.platform_x86_ascend_training
  177. def test_watchpoint_hit(self, app_client):
  178. """Test retrieve watchpoint hit."""
  179. with self._debugger_client.get_thread_instance():
  180. create_watchpoint_and_wait(app_client)
  181. # check watchpoint hit list
  182. url = 'retrieve'
  183. body_data = {'mode': 'watchpoint_hit'}
  184. expect_file = 'retrieve_watchpoint_hit.json'
  185. send_and_compare_result(app_client, url, body_data, expect_file)
  186. # check single watchpoint hit
  187. body_data = {
  188. 'mode': 'watchpoint_hit',
  189. 'params': {
  190. 'name': 'Default/TransData-op99',
  191. 'single_node': True,
  192. 'watch_point_id': 1
  193. }
  194. }
  195. expect_file = 'retrieve_single_watchpoint_hit.json'
  196. send_and_compare_result(app_client, url, body_data, expect_file)
  197. send_terminate_cmd(app_client)
  198. @pytest.mark.level0
  199. @pytest.mark.env_single
  200. @pytest.mark.platform_x86_cpu
  201. @pytest.mark.platform_arm_ascend_training
  202. @pytest.mark.platform_x86_gpu_training
  203. @pytest.mark.platform_x86_ascend_training
  204. def test_retrieve_tensor_value(self, app_client):
  205. """Test retrieve tensor value."""
  206. node_name = 'Default/TransData-op99'
  207. with self._debugger_client.get_thread_instance():
  208. check_state(app_client)
  209. # prepare tensor value
  210. url = 'retrieve_tensor_history'
  211. body_data = {'name': node_name}
  212. expect_file = 'retrieve_empty_tensor_history.json'
  213. send_and_compare_result(app_client, url, body_data, expect_file)
  214. # check full tensor history from poll data
  215. res = get_request_result(
  216. app_client=app_client, url='poll_data', body_data={'pos': 0}, method='get')
  217. assert res.get('receive_tensor', {}).get('node_name') == node_name
  218. expect_file = 'retrieve_full_tensor_history.json'
  219. send_and_compare_result(app_client, url, body_data, expect_file)
  220. # check tensor value
  221. url = 'tensors'
  222. body_data = {
  223. 'name': node_name + ':0',
  224. 'detail': 'data',
  225. 'shape': '[1, 1:3]'
  226. }
  227. expect_file = 'retrieve_tensor_value.json'
  228. send_and_compare_result(app_client, url, body_data, expect_file, method='get')
  229. send_terminate_cmd(app_client)
  230. @pytest.mark.level0
  231. @pytest.mark.env_single
  232. @pytest.mark.platform_x86_cpu
  233. @pytest.mark.platform_arm_ascend_training
  234. @pytest.mark.platform_x86_gpu_training
  235. @pytest.mark.platform_x86_ascend_training
  236. def test_compare_tensor_value(self, app_client):
  237. """Test compare tensor value."""
  238. node_name = 'Default/args0'
  239. with self._debugger_client.get_thread_instance():
  240. check_state(app_client)
  241. # prepare tensor values
  242. url = 'control'
  243. body_data = {'mode': 'continue',
  244. 'steps': 2}
  245. get_request_result(app_client, url, body_data)
  246. check_state(app_client)
  247. get_request_result(
  248. app_client=app_client, url='retrieve_tensor_history', body_data={'name': node_name})
  249. res = get_request_result(
  250. app_client=app_client, url='poll_data', body_data={'pos': 0}, method='get')
  251. assert res.get('receive_tensor', {}).get('node_name') == node_name
  252. # get compare results
  253. url = 'tensor-comparisons'
  254. body_data = {
  255. 'name': node_name + ':0',
  256. 'detail': 'data',
  257. 'shape': '[:, :]',
  258. 'tolerance': 1
  259. }
  260. expect_file = 'compare_tensors.json'
  261. send_and_compare_result(app_client, url, body_data, expect_file, method='get')
  262. send_terminate_cmd(app_client)
  263. @pytest.mark.level0
  264. @pytest.mark.env_single
  265. @pytest.mark.platform_x86_cpu
  266. @pytest.mark.platform_arm_ascend_training
  267. @pytest.mark.platform_x86_gpu_training
  268. @pytest.mark.platform_x86_ascend_training
  269. @pytest.mark.parametrize("body_data, expect_file", [
  270. ({'ascend': True}, 'retrieve_node_by_bfs_ascend.json'),
  271. ({'name': 'Default/args0', 'ascend': False}, 'retrieve_node_by_bfs.json')
  272. ])
  273. def test_retrieve_bfs_node(self, app_client, body_data, expect_file):
  274. """Test retrieve bfs node."""
  275. with self._debugger_client.get_thread_instance():
  276. check_state(app_client)
  277. # prepare tensor values
  278. url = 'retrieve_node_by_bfs'
  279. send_and_compare_result(app_client, url, body_data, expect_file, method='get')
  280. send_terminate_cmd(app_client)
  281. @pytest.mark.level0
  282. @pytest.mark.env_single
  283. @pytest.mark.platform_x86_cpu
  284. @pytest.mark.platform_arm_ascend_training
  285. @pytest.mark.platform_x86_gpu_training
  286. @pytest.mark.platform_x86_ascend_training
  287. def test_pause(self, app_client):
  288. """Test pause the training."""
  289. with self._debugger_client.get_thread_instance():
  290. check_state(app_client)
  291. # send run command to execute to next node
  292. url = 'control'
  293. body_data = {'mode': 'continue',
  294. 'steps': -1}
  295. res = get_request_result(app_client, url, body_data)
  296. assert res == {'metadata': {'state': 'running', 'enable_recheck': False}}
  297. # send pause command
  298. url = 'control'
  299. body_data = {'mode': 'pause'}
  300. res = get_request_result(app_client, url, body_data)
  301. assert res == {'metadata': {'state': 'waiting', 'enable_recheck': False}}
  302. send_terminate_cmd(app_client)
  303. @pytest.mark.level0
  304. @pytest.mark.env_single
  305. @pytest.mark.platform_x86_cpu
  306. @pytest.mark.platform_arm_ascend_training
  307. @pytest.mark.platform_x86_gpu_training
  308. @pytest.mark.platform_x86_ascend_training
  309. @pytest.mark.parametrize("url, body_data, enable_recheck", [
  310. ('create_watchpoint',
  311. {'condition': {'id': 'inf', 'params': []},
  312. 'watch_nodes': ['Default']}, True),
  313. ('update_watchpoint',
  314. {'watch_point_id': 1, 'watch_nodes': ['Default/optimizer-Momentum/Parameter[18]_7'],
  315. 'mode': 0}, True),
  316. ('update_watchpoint',
  317. {'watch_point_id': 1, 'watch_nodes': ['Default/optimizer-Momentum'],
  318. 'mode': 1}, True),
  319. ('delete_watchpoint', {}, True)
  320. ])
  321. def test_recheck(self, app_client, url, body_data, enable_recheck):
  322. """Test recheck."""
  323. with self._debugger_client.get_thread_instance():
  324. create_watchpoint_and_wait(app_client)
  325. # create watchpoint
  326. res = get_request_result(app_client, url, body_data, method='post')
  327. assert res['metadata']['enable_recheck'] is enable_recheck
  328. send_terminate_cmd(app_client)
  329. @pytest.mark.level0
  330. @pytest.mark.env_single
  331. @pytest.mark.platform_x86_cpu
  332. @pytest.mark.platform_arm_ascend_training
  333. @pytest.mark.platform_x86_gpu_training
  334. @pytest.mark.platform_x86_ascend_training
  335. def test_recommend_watchpoints(self, app_client):
  336. """Test generating recommended watchpoints."""
  337. original_value = settings.ENABLE_RECOMMENDED_WATCHPOINTS
  338. settings.ENABLE_RECOMMENDED_WATCHPOINTS = True
  339. try:
  340. with self._debugger_client.get_thread_instance():
  341. check_state(app_client)
  342. url = 'retrieve'
  343. body_data = {'mode': 'watchpoint'}
  344. expect_file = 'recommended_watchpoints_at_startup.json'
  345. send_and_compare_result(app_client, url, body_data, expect_file, method='post')
  346. send_terminate_cmd(app_client)
  347. finally:
  348. settings.ENABLE_RECOMMENDED_WATCHPOINTS = original_value
  349. @pytest.mark.level0
  350. @pytest.mark.env_single
  351. @pytest.mark.platform_x86_cpu
  352. @pytest.mark.platform_arm_ascend_training
  353. @pytest.mark.platform_x86_gpu_training
  354. @pytest.mark.platform_x86_ascend_training
  355. @pytest.mark.parametrize("body_data, expect_file", [
  356. ({'tensor_name': 'Default/TransData-op99:0', 'graph_name': 'graph_0'}, 'retrieve_tensor_graph-0.json'),
  357. ({'tensor_name': 'Default/optimizer-Momentum/Parameter[18]_7/moments.fc1.bias:0', 'graph_name': 'graph_0'},
  358. 'retrieve_tensor_graph-1.json')
  359. ])
  360. def test_retrieve_tensor_graph(self, app_client, body_data, expect_file):
  361. """Test retrieve tensor graph."""
  362. url = 'tensor-graphs'
  363. with self._debugger_client.get_thread_instance():
  364. create_watchpoint_and_wait(app_client)
  365. get_request_result(app_client, url, body_data, method='GET')
  366. # check full tensor history from poll data
  367. res = get_request_result(
  368. app_client=app_client, url='poll_data', body_data={'pos': 0}, method='get')
  369. assert res.get('receive_tensor', {}).get('tensor_name') == body_data.get('tensor_name')
  370. send_and_compare_result(app_client, url, body_data, expect_file, method='GET')
  371. send_terminate_cmd(app_client)
  372. class TestGPUDebugger:
  373. """Test debugger on Ascend backend."""
  374. @classmethod
  375. def setup_class(cls):
  376. """Setup class."""
  377. cls._debugger_client = MockDebuggerClient(backend='GPU')
  378. @pytest.mark.level0
  379. @pytest.mark.env_single
  380. @pytest.mark.platform_x86_cpu
  381. @pytest.mark.platform_arm_ascend_training
  382. @pytest.mark.platform_x86_gpu_training
  383. @pytest.mark.platform_x86_ascend_training
  384. def test_next_node_on_gpu(self, app_client):
  385. """Test get next node on GPU."""
  386. gpu_debugger_client = MockDebuggerClient(backend='GPU')
  387. with gpu_debugger_client.get_thread_instance():
  388. check_state(app_client)
  389. # send run command to get watchpoint hit
  390. url = 'control'
  391. body_data = {'mode': 'continue',
  392. 'level': 'node',
  393. 'name': 'Default/TransData-op99'}
  394. res = get_request_result(app_client, url, body_data)
  395. assert res == {'metadata': {'state': 'running', 'enable_recheck': False}}
  396. # get metadata
  397. check_state(app_client)
  398. url = 'retrieve'
  399. body_data = {'mode': 'all'}
  400. expect_file = 'retrieve_next_node_on_gpu.json'
  401. send_and_compare_result(app_client, url, body_data, expect_file)
  402. send_terminate_cmd(app_client)
  403. @pytest.mark.level0
  404. @pytest.mark.env_single
  405. @pytest.mark.platform_x86_cpu
  406. @pytest.mark.platform_arm_ascend_training
  407. @pytest.mark.platform_x86_gpu_training
  408. @pytest.mark.platform_x86_ascend_training
  409. @pytest.mark.parametrize("url, body_data, enable_recheck", [
  410. ('create_watchpoint',
  411. {'condition': {'id': 'inf', 'params': []},
  412. 'watch_nodes': ['Default']}, True),
  413. ('create_watchpoint',
  414. {'condition': {'id': 'inf', 'params': []},
  415. 'watch_nodes': ['Default/TransData-op99']}, True),
  416. ('update_watchpoint',
  417. {'watch_point_id': 1, 'watch_nodes': ['Default/optimizer-Momentum/Parameter[18]_7'],
  418. 'mode': 0}, True),
  419. ('update_watchpoint',
  420. {'watch_point_id': 1, 'watch_nodes': ['Default/optimizer-Momentum'],
  421. 'mode': 1}, True),
  422. ('update_watchpoint',
  423. [{'watch_point_id': 1, 'watch_nodes': ['Default/optimizer-Momentum'],
  424. 'mode': 1},
  425. {'watch_point_id': 1, 'watch_nodes': ['Default/optimizer-Momentum'],
  426. 'mode': 0}
  427. ], True),
  428. ('update_watchpoint',
  429. [{'watch_point_id': 1, 'watch_nodes': ['Default/TransData-op99'],
  430. 'mode': 0},
  431. {'watch_point_id': 1, 'watch_nodes': ['Default/TransData-op99'],
  432. 'mode': 1}
  433. ], True),
  434. ('delete_watchpoint', {'watch_point_id': 1}, True)
  435. ])
  436. def test_recheck_state(self, app_client, url, body_data, enable_recheck):
  437. """Test update watchpoint and check the value of enable_recheck."""
  438. with self._debugger_client.get_thread_instance():
  439. create_watchpoint_and_wait(app_client)
  440. if not isinstance(body_data, list):
  441. body_data = [body_data]
  442. for sub_body_data in body_data:
  443. res = get_request_result(app_client, url, sub_body_data, method='post')
  444. assert res['metadata']['enable_recheck'] is enable_recheck
  445. send_terminate_cmd(app_client)
  446. def test_get_conditions(self, app_client):
  447. """Test get conditions for gpu."""
  448. url = '/v1/mindinsight/conditionmgr/train-jobs/train-id/conditions'
  449. body_data = {}
  450. expect_file = 'get_conditions_for_gpu.json'
  451. with self._debugger_client.get_thread_instance():
  452. check_state(app_client)
  453. send_and_compare_result(app_client, url, body_data, expect_file, method='get', full_url=True)
  454. send_terminate_cmd(app_client)
  455. @pytest.mark.level0
  456. @pytest.mark.env_single
  457. @pytest.mark.platform_x86_cpu
  458. @pytest.mark.platform_arm_ascend_training
  459. @pytest.mark.platform_x86_gpu_training
  460. @pytest.mark.platform_x86_ascend_training
  461. def test_recheck(self, app_client):
  462. """Test recheck request."""
  463. with self._debugger_client.get_thread_instance():
  464. create_watchpoint_and_wait(app_client)
  465. # send recheck when disable to do recheck
  466. get_request_result(app_client, 'recheck', {}, method='post', expect_code=400)
  467. # send recheck when enable to do recheck
  468. create_watchpoint(app_client, {'id': 'inf', 'params': []}, 2)
  469. res = get_request_result(app_client, 'recheck', {}, method='post')
  470. assert res['metadata']['enable_recheck'] is False
  471. send_terminate_cmd(app_client)
  472. @pytest.mark.level0
  473. @pytest.mark.env_single
  474. @pytest.mark.platform_x86_cpu
  475. @pytest.mark.platform_arm_ascend_training
  476. @pytest.mark.platform_x86_gpu_training
  477. @pytest.mark.platform_x86_ascend_training
  478. @pytest.mark.parametrize("filter_condition, expect_file", [
  479. ({'name': 'fc', 'node_category': 'weight'}, 'search_weight.json'),
  480. ({'name': 'fc', 'node_category': 'gradient'}, 'search_gradient.json'),
  481. ({'node_category': 'activation'}, 'search_activation.json')
  482. ])
  483. def test_search_by_category(self, app_client, filter_condition, expect_file):
  484. """Test recheck request."""
  485. with self._debugger_client.get_thread_instance():
  486. check_state(app_client)
  487. send_and_compare_result(app_client, 'search', filter_condition, expect_file,
  488. method='get')
  489. send_terminate_cmd(app_client)
  490. class TestMultiGraphDebugger:
  491. """Test debugger on Ascend backend for multi_graph."""
  492. @classmethod
  493. def setup_class(cls):
  494. """Setup class."""
  495. cls._debugger_client = MockDebuggerClient(backend='Ascend', graph_num=2)
  496. @pytest.mark.level0
  497. @pytest.mark.env_single
  498. @pytest.mark.platform_x86_cpu
  499. @pytest.mark.platform_arm_ascend_training
  500. @pytest.mark.platform_x86_gpu_training
  501. @pytest.mark.platform_x86_ascend_training
  502. @pytest.mark.parametrize("body_data, expect_file", [
  503. ({'mode': 'all'}, 'multi_retrieve_all.json'),
  504. ({'mode': 'node', 'params': {'name': 'Default', 'graph_name': 'graph_1'}}, 'retrieve_scope_node.json'),
  505. ({'mode': 'node', 'params': {'name': 'graph_0'}}, 'multi_retrieve_scope_node.json'),
  506. ({'mode': 'node', 'params': {'name': 'graph_0/Default/optimizer-Momentum/Parameter[18]_7'}},
  507. 'multi_retrieve_aggregation_scope_node.json'),
  508. ({'mode': 'node', 'params': {
  509. 'name': 'graph_0/Default/TransData-op99',
  510. 'single_node': True}}, 'multi_retrieve_single_node.json'),
  511. ({'mode': 'node', 'params': {
  512. 'name': 'Default/TransData-op99',
  513. 'single_node': True, 'graph_name': 'graph_0'}}, 'retrieve_single_node.json')
  514. ])
  515. def test_multi_retrieve_when_train_begin(self, app_client, body_data, expect_file):
  516. """Test retrieve when train_begin."""
  517. url = 'retrieve'
  518. with self._debugger_client.get_thread_instance():
  519. check_state(app_client)
  520. send_and_compare_result(app_client, url, body_data, expect_file)
  521. send_terminate_cmd(app_client)
  522. @pytest.mark.level0
  523. @pytest.mark.env_single
  524. @pytest.mark.platform_x86_cpu
  525. @pytest.mark.platform_arm_ascend_training
  526. @pytest.mark.platform_x86_gpu_training
  527. @pytest.mark.platform_x86_ascend_training
  528. @pytest.mark.parametrize("filter_condition, expect_file", [
  529. ({'name': '', 'node_category': 'weight'}, 'search_weight_multi_graph.json'),
  530. ({'node_category': 'activation'}, 'search_activation_multi_graph.json')
  531. ])
  532. def test_search_by_category_with_multi_graph(self, app_client, filter_condition, expect_file):
  533. """Test search by category request."""
  534. with self._debugger_client.get_thread_instance():
  535. check_state(app_client)
  536. send_and_compare_result(app_client, 'search', filter_condition, expect_file, method='get')
  537. send_terminate_cmd(app_client)
  538. @pytest.mark.level0
  539. @pytest.mark.env_single
  540. @pytest.mark.platform_x86_cpu
  541. @pytest.mark.platform_arm_ascend_training
  542. @pytest.mark.platform_x86_gpu_training
  543. @pytest.mark.platform_x86_ascend_training
  544. @pytest.mark.parametrize("filter_condition, expect_id", [
  545. ({'condition': {'id': 'inf'},
  546. 'watch_nodes': ['Default/optimizer-Momentum/Parameter[18]_7'],
  547. 'graph_name': 'graph_0'}, 1),
  548. ({'condition': {'id': 'inf'},
  549. 'watch_nodes': ['graph_0/Default/optimizer-Momentum/ApplyMomentum[8]_1'],
  550. 'graph_name': None}, 1)
  551. ])
  552. def test_create_watchpoint(self, app_client, filter_condition, expect_id):
  553. """Test create watchpoint with multiple graphs."""
  554. url = 'create_watchpoint'
  555. with self._debugger_client.get_thread_instance():
  556. check_state(app_client)
  557. res = get_request_result(app_client, url, filter_condition)
  558. assert res.get('id') == expect_id
  559. send_terminate_cmd(app_client)
  560. @pytest.mark.level0
  561. @pytest.mark.env_single
  562. @pytest.mark.platform_x86_cpu
  563. @pytest.mark.platform_arm_ascend_training
  564. @pytest.mark.platform_x86_gpu_training
  565. @pytest.mark.platform_x86_ascend_training
  566. @pytest.mark.parametrize("params, expect_file", [
  567. ({'level': 'node'}, 'multi_next_node.json'),
  568. ({'level': 'node', 'node_name': 'graph_0/Default/TransData-op99'}, 'multi_next_node.json'),
  569. ({'level': 'node', 'node_name': 'Default/TransData-op99', 'graph_name': 'graph_0'},
  570. 'multi_next_node.json')
  571. ])
  572. def test_continue_on_gpu(self, app_client, params, expect_file):
  573. """Test get next node on GPU."""
  574. gpu_debugger_client = MockDebuggerClient(backend='GPU', graph_num=2)
  575. original_value = settings.ENABLE_RECOMMENDED_WATCHPOINTS
  576. settings.ENABLE_RECOMMENDED_WATCHPOINTS = True
  577. try:
  578. with gpu_debugger_client.get_thread_instance():
  579. check_state(app_client)
  580. # send run command to get watchpoint hit
  581. url = 'control'
  582. body_data = {'mode': 'continue'}
  583. body_data.update(params)
  584. res = get_request_result(app_client, url, body_data)
  585. assert res == {'metadata': {'state': 'running', 'enable_recheck': False}}
  586. # get metadata
  587. check_state(app_client)
  588. url = 'retrieve'
  589. body_data = {'mode': 'all'}
  590. send_and_compare_result(app_client, url, body_data, expect_file)
  591. send_terminate_cmd(app_client)
  592. finally:
  593. settings.ENABLE_RECOMMENDED_WATCHPOINTS = original_value
  594. @pytest.mark.level0
  595. @pytest.mark.env_single
  596. @pytest.mark.platform_x86_cpu
  597. @pytest.mark.platform_arm_ascend_training
  598. @pytest.mark.platform_x86_gpu_training
  599. @pytest.mark.platform_x86_ascend_training
  600. @pytest.mark.parametrize("body_data, expect_file", [
  601. ({'tensor_name': 'Default/TransData-op99:0', 'graph_name': 'graph_0'}, 'retrieve_tensor_hits-0.json'),
  602. ({'tensor_name': 'Default/optimizer-Momentum/Parameter[18]_7/moments.fc1.bias:0', 'graph_name': 'graph_0'},
  603. 'retrieve_tensor_hits-1.json')
  604. ])
  605. def test_retrieve_tensor_hits(self, app_client, body_data, expect_file):
  606. """Test retrieve tensor graph."""
  607. url = 'tensor-hits'
  608. with self._debugger_client.get_thread_instance():
  609. check_state(app_client)
  610. send_and_compare_result(app_client, url, body_data, expect_file, method='GET')
  611. send_terminate_cmd(app_client)
  612. def create_watchpoint(app_client, condition, expect_id):
  613. """Create watchpoint."""
  614. url = 'create_watchpoint'
  615. body_data = {'condition': condition,
  616. 'watch_nodes': ['Default/optimizer-Momentum/Parameter[18]_7',
  617. 'Default/optimizer-Momentum/Parameter[18]_7/moments.fc3.bias',
  618. 'Default/optimizer-Momentum/Parameter[18]_7/moments.fc1.bias',
  619. 'Default/TransData-op99']}
  620. res = get_request_result(app_client, url, body_data)
  621. assert res.get('id') == expect_id
  622. def create_watchpoint_and_wait(app_client):
  623. """Preparation for recheck."""
  624. check_state(app_client)
  625. create_watchpoint(app_client, condition={'id': 'inf', 'params': []}, expect_id=1)
  626. # send run command to get watchpoint hit
  627. url = 'control'
  628. body_data = {'mode': 'continue',
  629. 'steps': 2}
  630. res = get_request_result(app_client, url, body_data)
  631. assert res == {'metadata': {'state': 'running', 'enable_recheck': False}}
  632. # wait for server has received watchpoint hit
  633. check_state(app_client)
  634. class TestMismatchDebugger:
  635. """Test debugger when Mindinsight and Mindspore is mismatched."""
  636. @classmethod
  637. def setup_class(cls):
  638. """Setup class."""
  639. cls._debugger_client = MockDebuggerClient(backend='Ascend', ms_version='1.0.0')
  640. @pytest.mark.level0
  641. @pytest.mark.env_single
  642. @pytest.mark.platform_x86_cpu
  643. @pytest.mark.platform_arm_ascend_training
  644. @pytest.mark.platform_x86_gpu_training
  645. @pytest.mark.platform_x86_ascend_training
  646. @pytest.mark.parametrize("body_data, expect_file", [
  647. ({'mode': 'all'}, 'version_mismatch.json')
  648. ])
  649. def test_retrieve_when_version_mismatch(self, app_client, body_data, expect_file):
  650. """Test retrieve when train_begin."""
  651. url = 'retrieve'
  652. with self._debugger_client.get_thread_instance():
  653. check_state(app_client, ServerStatus.MISMATCH.value)
  654. send_and_compare_result(app_client, url, body_data, expect_file)
  655. send_terminate_cmd(app_client)