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.

dump.py 4.6 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. """Controlling dump behavior."""
  16. from warnings import warn
  17. import mindspore.context as context
  18. from mindspore._c_expression import security
  19. def set_dump(target, enabled=True):
  20. """
  21. Enable or disable dump for the target and its contents.
  22. Target should be an instance of Cell or Primitive. The default enabled
  23. status for a cell or primitive is False. Please note that this API takes
  24. effect only when the dump_mode field in dump config file is 2. See the
  25. `dump document <https://mindspore.cn/docs/programming_guide/zh-CN/master/dump_in_graph_mode.html>`_
  26. for details.
  27. .. warning::
  28. This is an experimental prototype that is subject to change and/or
  29. deletion.
  30. Note:
  31. 1. This API is only effective for GRAPH_MODE with Ascend backend.
  32. 2. When input is a cell, this API is only effective for the members of
  33. the cell instance. If an operator is not a member of the cell
  34. instance, the dump flag will not be set for this operator (e.g.
  35. functional operators used directly in construct method). To make
  36. this API effective, please use self.some_op = SomeOp() in your cell's
  37. __init__ method.
  38. Args:
  39. target (Union[Cell, Primitive]): The Cell instance or Primitive instance
  40. to which the dump flag is set.
  41. enabled (bool): True means enable dump, False means disable dump.
  42. Default: True.
  43. Examples:
  44. >>> from mindspore.nn import Cell
  45. >>> class MyNet(Cell):
  46. ... def __init__(self):
  47. ... super().__init__()
  48. ... self.conv1 = nn.Conv2d(5, 6, 5, pad_mode='valid')
  49. ... self.relu1 = nn.ReLU()
  50. ...
  51. ... def construct(self, x):
  52. ... x = self.conv1(x)
  53. ... x = self.relu1(x)
  54. ... return x
  55. >>> net = MyNet()
  56. >>> set_dump(net.conv1)
  57. """
  58. if security.enable_security():
  59. raise ValueError('The set_dump API is not supported, please recompile '
  60. 'source without "-s on".')
  61. import mindspore.nn as nn # avoid circular import
  62. from mindspore.ops import Primitive
  63. if not isinstance(target, nn.Cell) and not isinstance(target, Primitive):
  64. raise ValueError(f"The \"target\" parameter must be an instance of "
  65. f"Cell or Primitive, "
  66. f"but got an instance of {type(target)}.")
  67. if not isinstance(enabled, bool):
  68. raise ValueError("The \"enabled\" parameter must be bool.")
  69. # Checking for device target and mode.
  70. current_target = context.get_context("device_target")
  71. if current_target != "Ascend":
  72. # We will not return here in case user changed device_target later.
  73. warn("Current device_target is {}, which is not supported by set_dump. "
  74. "Only Ascend device target is supported currently. "
  75. "If you have Ascend device, consider set device_target to Ascend "
  76. "before calling set_dump.".format(current_target))
  77. current_mode = context.get_context("mode")
  78. if current_mode != context.GRAPH_MODE:
  79. # We will not return here in case user changed mode later.
  80. warn(
  81. "Current mode is PYNATIVE_MODE, which is not supported by set_dump. "
  82. "Only GRAPH_MODE is supported currently. "
  83. "Consider set mode to GRAPH_MODE "
  84. "before calling set_dump.")
  85. # The actual set dump logic.
  86. mode = "true" if enabled else "false"
  87. if isinstance(target, nn.Cell):
  88. primitives = getattr(target, "_primitives", {})
  89. for value in primitives.values():
  90. if value:
  91. value.add_prim_attr("dump", mode)
  92. for cell in target.cells():
  93. set_dump(cell, enabled)
  94. return
  95. if isinstance(target, Primitive):
  96. target.add_prim_attr("dump", mode)
  97. return