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_dump.py 3.7 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 dump."""
  16. import warnings
  17. import pytest
  18. import mindspore.context as context
  19. import mindspore.nn as nn
  20. import mindspore.ops as ops
  21. from mindspore import set_dump
  22. def test_set_dump_on_cell():
  23. """
  24. Feature: Python API set_dump.
  25. Description: Use set_dump API on Cell instance.
  26. Expectation: Success.
  27. """
  28. class MyNet(nn.Cell):
  29. def __init__(self):
  30. super(MyNet, self).__init__()
  31. self.conv1 = nn.Conv2d(5, 6, 5, pad_mode='valid')
  32. self.relu1 = nn.ReLU()
  33. def construct(self, x):
  34. x = self.conv1(x)
  35. x = self.relu1(x)
  36. return x
  37. net = MyNet()
  38. set_dump(net.relu1)
  39. assert net.relu1.get_flags()["dump"] is True
  40. def test_set_dump_on_primitive():
  41. """
  42. Feature: Python API set_dump.
  43. Description: Use set_dump API on Primitive instance.
  44. Expectation: Success.
  45. """
  46. op = ops.Add()
  47. set_dump(op)
  48. assert op.attrs["dump"] == "true"
  49. def test_input_type_check():
  50. """
  51. Feature: Python API set_dump.
  52. Description: Use set_dump API on unsupported instance.
  53. Expectation: Throw ValueError exception.
  54. """
  55. with pytest.raises(ValueError):
  56. set_dump(1)
  57. @pytest.mark.skip(reason="Warning can only be triggered once, please execute "
  58. "this test case manually.")
  59. def test_set_dump_warning():
  60. """
  61. Feature: Python API set_dump.
  62. Description: Test the warning about device target and mode.
  63. Expectation: Trigger warning message.
  64. """
  65. context.set_context(device_target="CPU")
  66. context.set_context(mode=context.PYNATIVE_MODE)
  67. op = ops.Add()
  68. with warnings.catch_warnings(record=True) as w:
  69. warnings.simplefilter("always")
  70. set_dump(op)
  71. assert "Only Ascend device target is supported" in str(w[-2].message)
  72. assert "Only GRAPH_MODE is supported" in str(w[-1].message)
  73. def test_set_dump_on_cell_with_false():
  74. """
  75. Feature: Python API set_dump on cell with False.
  76. Description: Use set_dump API on Cell instance.
  77. Expectation: Success.
  78. """
  79. class MyNet(nn.Cell):
  80. def __init__(self):
  81. super(MyNet, self).__init__()
  82. self.relu1 = nn.ReLU()
  83. def construct(self, x):
  84. x = self.relu1(x)
  85. return x
  86. net = MyNet()
  87. set_dump(net.relu1)
  88. assert net.relu1.get_flags()["dump"] is True
  89. set_dump(net, False)
  90. assert net.relu1.get_flags()["dump"] is False
  91. def test_set_dump_on_primitive_with_false():
  92. """
  93. Feature: Python API set_dump on primitive with False.
  94. Description: Use set_dump API on Cell instance.
  95. Expectation: Success.
  96. """
  97. class MyNet(nn.Cell):
  98. def __init__(self):
  99. super(MyNet, self).__init__()
  100. self.relu1 = ops.ReLU()
  101. def construct(self, x):
  102. x = self.relu1(x)
  103. return x
  104. net = MyNet()
  105. set_dump(net.relu1)
  106. assert net.relu1.attrs.get("dump") == "true"
  107. set_dump(net, False)
  108. assert net.relu1.attrs.get("dump") == "false"