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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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.relu.attrs["dump"] == "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)