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.

graph_processor.py 5.0 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. # Copyright 2019 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 to process `data_transform.data_manager` to handle graph,
  17. and the status of graph will be checked before calling `Graph` object.
  18. """
  19. from mindinsight.datavisual.common import exceptions
  20. from mindinsight.datavisual.common.enums import PluginNameEnum
  21. from mindinsight.datavisual.common.validation import Validation
  22. from mindinsight.datavisual.processors.base_processor import BaseProcessor
  23. from mindinsight.datavisual.common.exceptions import NodeNotInGraphError
  24. class GraphProcessor(BaseProcessor):
  25. """
  26. This object is to handle `DataManager` object, and process graph object.
  27. Args:
  28. train_id (str): To get train job data by this given id.
  29. data_manager (DataManager): A `DataManager` object.
  30. tag (str): The tag of graph, if tag is None, will load the first graph.
  31. """
  32. def __init__(self, train_id, data_manager, tag=None):
  33. Validation.check_param_empty(train_id=train_id)
  34. super(GraphProcessor, self).__init__(data_manager)
  35. train_job = self._data_manager.get_train_job_by_plugin(train_id, PluginNameEnum.GRAPH.value)
  36. if train_job is None:
  37. raise exceptions.TrainJobNotExistError()
  38. if not train_job['tags'] or (tag is not None and tag not in train_job['tags']):
  39. raise exceptions.GraphNotExistError()
  40. if tag is None:
  41. tag = train_job['tags'][0]
  42. tensors = self._data_manager.list_tensors(train_id, tag=tag)
  43. self._graph = tensors[0].value
  44. def list_nodes(self, scope):
  45. """
  46. Get the nodes of every layer in graph.
  47. Args:
  48. scope (str): The name of a scope.
  49. Returns:
  50. TypedDict('Nodes', {'nodes': list[Node]}), format is {'nodes': [<Node object>]}.
  51. example:
  52. {
  53. "nodes" : [
  54. {
  55. "attr" :
  56. {
  57. "index" : "i: 0\n"
  58. },
  59. "input" : {},
  60. "name" : "input_tensor",
  61. "output" :
  62. {
  63. "Default/TensorAdd-op17" :
  64. {
  65. "edge_type" : "data",
  66. "scope" : "name_scope",
  67. "shape" : [1, 16, 128, 128]
  68. }
  69. },
  70. "output_i" : -1,
  71. "proxy_input" : {},
  72. "proxy_output" : {},
  73. "independent_layout" : False,
  74. "subnode_count" : 0,
  75. "type" : "Data"
  76. }
  77. ]
  78. }
  79. """
  80. if scope and not self._graph.exist_node(scope):
  81. raise NodeNotInGraphError(node_name=scope)
  82. nodes = self._graph.list_node_by_scope(scope=scope)
  83. return {'nodes': nodes}
  84. def search_node_names(self, search_content, offset, limit):
  85. """
  86. Search node names by search content.
  87. Args:
  88. search_content (Any): This content can be the key content of the node to search.
  89. offset (int): An offset for page. Ex, offset is 0, mean current page is 1.
  90. limit (int): The max data items for per page.
  91. Returns:
  92. Dict, the searched nodes.
  93. """
  94. offset = Validation.check_offset(offset=offset)
  95. limit = Validation.check_limit(limit, min_value=1, max_value=1000)
  96. nodes = self._graph.search_nodes_by_pattern(search_content)
  97. real_offset = offset * limit
  98. search_nodes = self._graph.get_nodes(nodes[real_offset:real_offset + limit])
  99. return {"nodes": search_nodes}
  100. def search_single_node(self, name):
  101. """
  102. Search node by node name.
  103. Args:
  104. name (str): The name of node.
  105. Returns:
  106. dict, format is:
  107. item_object = {'nodes': [<Node object>],
  108. 'scope_name': '',
  109. 'children': {<item_object>}}
  110. """
  111. Validation.check_param_empty(name=name)
  112. nodes = self._graph.search_single_node(name)
  113. return nodes