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_debug_info.py 2.8 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. # Copyright 2021 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. """ test_parse_numpy """
  16. import os
  17. import shutil
  18. import subprocess
  19. import pytest
  20. import numpy as np
  21. import mindspore as ms
  22. from mindspore import nn
  23. from mindspore import context
  24. from mindspore import ms_function
  25. from mindspore import Tensor
  26. from tests.security_utils import security_off_wrap
  27. def find_files(file, para):
  28. output = subprocess.check_output(
  29. ["grep '%s' %s | wc -l" % (para, file)],
  30. shell=True)
  31. out = str(output, 'utf-8').strip()
  32. return out
  33. def remove_path(path):
  34. if os.path.exists(path):
  35. shutil.rmtree(path)
  36. @security_off_wrap
  37. def test_ms_function():
  38. @ms_function
  39. def add(x):
  40. return x + 1
  41. context.set_context(mode=context.GRAPH_MODE)
  42. context.set_context(save_graphs=True, save_graphs_path="ir_dump_path")
  43. input1 = np.random.randn(5, 5)
  44. add(Tensor(input1, ms.float32))
  45. result = find_files("./ir_dump_path/*validate*.ir", "test_debug_info.py(46)/ return x + 1/")
  46. assert result == '2'
  47. remove_path("./ir_dump_path/")
  48. context.set_context(save_graphs=False)
  49. @security_off_wrap
  50. def test_cell_ms_function():
  51. class Net(nn.Cell):
  52. @ms_function
  53. def construct(self, x):
  54. return x
  55. context.set_context(mode=context.GRAPH_MODE)
  56. context.set_context(save_graphs=True, save_graphs_path="ir_dump_path")
  57. input1 = np.random.randn(5, 5)
  58. net = Net()
  59. net(Tensor(input1, ms.float32))
  60. result = find_files("./ir_dump_path/*validate*.ir", "test_debug_info.py(64)/ return x/")
  61. assert result == '1'
  62. remove_path("./ir_dump_path/")
  63. context.set_context(save_graphs=False)
  64. def test_parse_slice_location():
  65. """
  66. Feature: parse location.
  67. Description: Test Slice node will be parsed with correct location.
  68. Expectation: TypeError.
  69. """
  70. class Net(nn.Cell):
  71. def construct(self, x):
  72. return x[1.2:]
  73. context.set_context(mode=context.GRAPH_MODE)
  74. input1 = Tensor((1, 2, 3))
  75. net = Net()
  76. with pytest.raises(TypeError) as ex:
  77. net(input1)
  78. assert "return x[1.2:]" in str(ex.value)