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.

node.py 6.0 kB

5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. This file is used to define the node of graph and associated base types.
  17. """
  18. from enum import Enum
  19. class NodeTypeEnum(Enum):
  20. """Node type enum. The following types are new to our custom."""
  21. NAME_SCOPE = 'name_scope'
  22. POLYMERIC_SCOPE = 'polymeric_scope'
  23. PARAMETER = 'Parameter'
  24. CONST = 'Const'
  25. class Node:
  26. """
  27. Define a node object.
  28. Args:
  29. name (str): Name of new node.
  30. node_id (str): The id of this node, and node id is unique in graph.
  31. """
  32. def __init__(self, name, node_id):
  33. self._node_id = node_id
  34. self._name = name
  35. self._type = ""
  36. self._attr = dict()
  37. self._input = dict()
  38. self._output_i = -1
  39. self._output = {}
  40. self._polymeric_input = {}
  41. self._polymeric_output = {}
  42. self._polymeric_scope_name = ""
  43. self._subnode_count = 0
  44. self._name_scope = ""
  45. self.shape = []
  46. def to_dict(self):
  47. """Converts the node object to dictionary format."""
  48. return {
  49. 'name': self._name,
  50. 'type': self._type,
  51. 'attr': self._attr,
  52. 'input': self._input,
  53. 'output_i': self._output_i,
  54. 'output': self._output,
  55. 'polymeric_input': self._polymeric_input,
  56. 'polymeric_output': self._polymeric_output,
  57. 'subnode_count': self._subnode_count,
  58. 'polymeric_scope_name': self._polymeric_scope_name
  59. }
  60. @property
  61. def node_id(self):
  62. """The id of this node, and id is unique in graph."""
  63. return self._node_id
  64. @property
  65. def name(self):
  66. """Get node name."""
  67. return self._name
  68. @name.setter
  69. def name(self, name):
  70. """Set node name."""
  71. self._name = name
  72. @property
  73. def node_type(self):
  74. """Get node type."""
  75. return self._type
  76. @node_type.setter
  77. def node_type(self, node_type):
  78. """Set node type."""
  79. self._type = node_type
  80. @property
  81. def attr(self):
  82. """Get node attr."""
  83. return self._attr
  84. def update_attr(self, attr_dict):
  85. """
  86. Update node attr.
  87. Args:
  88. attr_dict (dict[str, str]): Format is {'<key>': '<value>'}.
  89. """
  90. self._attr.update(attr_dict)
  91. @property
  92. def input(self):
  93. """
  94. Get all input of current node.
  95. Returns:
  96. dict[str, dict], format is {'<src_name>': {'shape': [], 'edge_type', 'scope'}}.
  97. """
  98. return self._input
  99. def update_input(self, input_dict):
  100. """
  101. Update input.
  102. Args:
  103. input_dict (dict[str, dict]): Format is {'<src_name>': {'shape': [], 'edge_type', 'scope'}}.
  104. """
  105. self._input.update(input_dict)
  106. @property
  107. def output_i(self):
  108. """The memory address of this node when it is in run time."""
  109. return self._output_i
  110. @output_i.setter
  111. def output_i(self, output_i):
  112. """Set memory address."""
  113. self._output_i = output_i
  114. @property
  115. def polymeric_input(self):
  116. """
  117. The polymeric input is the input of the polymeric nodes.
  118. Returns:
  119. dict[str, dict], format is {'<src_name>': {'edge_type': '<value>'}}.
  120. """
  121. return self._polymeric_input
  122. def update_polymeric_input(self, polymeric_input):
  123. """The polymeric input is the input of the polymeric nodes."""
  124. self._polymeric_input.update(polymeric_input)
  125. @property
  126. def output(self):
  127. """The output node of this node."""
  128. return self._output
  129. def update_output(self, output):
  130. """
  131. Update output node.
  132. Args:
  133. output (dict[str, TypedDict('NodeType', {'type': str})]): Format
  134. is {"<node_name>": {"type": "<node type>"}}.
  135. """
  136. self._output.update(output)
  137. @property
  138. def polymeric_output(self):
  139. """Get polymeric output."""
  140. return self._polymeric_output
  141. def update_polymeric_output(self, polymeric_output):
  142. """
  143. Update polymeric output.
  144. Args:
  145. polymeric_output (dict[str, dict): Format is {dst_node.polymeric_scope_name:
  146. {'edge_type': EdgeTypeEnum.data}}).
  147. """
  148. self._polymeric_output.update(polymeric_output)
  149. @property
  150. def polymeric_scope_name(self):
  151. """Get polymeric scope name."""
  152. return self._polymeric_scope_name
  153. @polymeric_scope_name.setter
  154. def polymeric_scope_name(self, name):
  155. """Set polymeric scope name."""
  156. self._polymeric_scope_name = name
  157. @property
  158. def subnode_count(self):
  159. """The sub node count of this node, if this node is a scope node, this count will not be zero."""
  160. return self._subnode_count
  161. @subnode_count.setter
  162. def subnode_count(self, count):
  163. """Set sub node count."""
  164. self._subnode_count = count
  165. @property
  166. def name_scope(self):
  167. """Get name scope of this node."""
  168. return self._name_scope
  169. @name_scope.setter
  170. def name_scope(self, name_scope):
  171. """Set name scope."""
  172. self._name_scope = name_scope
  173. def __str__(self):
  174. return f'<Node, name: {self._name}, type: {self._type}>'

MindInsight为MindSpore提供了简单易用的调优调试能力。在训练过程中,可以将标量、张量、图像、计算图、模型超参、训练耗时等数据记录到文件中,通过MindInsight可视化页面进行查看及分析。

Contributors (1)