DataStructures

SNode(id, type, upstream, downstream, scope, …)

A dataclass which stores all infos we need about nodes

SMSGraph(graph)

This object describes the simplified MindSpore graph, and it is used for subgraph detection.

Subgraph(pattern, nodes, min_node, …)

The subgraph: Not a subclass of SMSGraph, organized to improve performance

SubgraphCore(nodes)

The growing core of subgraph

SNode

class SubgraphDetection.DataStructure.SNode(id, type, upstream, downstream, scope, level, name)[source]

A dataclass which stores all infos we need about nodes

__repr__()[source]

Return repr(self).

__weakref__

list of weak references to the object (if defined)

SMSGraph

class SubgraphDetection.DataStructure.SMSGraph(graph)[source]

This object describes the simplified MindSpore graph, and it is used for subgraph detection.

__init__(graph)[source]

Init a SMSGraph with a MSGraph object

Parameters

graph (mindinsight.datavisual.data_transform.graph.msgraph.MSGraph) – The MSGraph required to be parsed

__weakref__

list of weak references to the object (if defined)

_check_scope()[source]

Run scope isomorphism check and fill in the scope info (level, type & member)

static frequent_nodes(nodes)[source]

Count the frequent nodes and return a deque of node tuples, which may be used to build subgraph core later Those node whose occurrences less than MIN_SUBGRAPH_INSTANCE_NUMBER will not returned

Parameters

nodes (Dict[int, SubgraphDetection.DataStructure.SimpleMindsporeGraph.snode.SNode]) – Nodes that we are counting

Returns

Each tuple contains same-type nodes

Return type

Deque[Tuple[SubgraphDetection.DataStructure.SimpleMindsporeGraph.snode.SNode, ..]]

get_level_node(level)[source]

Get a projection of a certain level of whole graph

Parameters

level (int) – which level are we dealing with

Returns

Node dict of the certain level

Return type

Dict[int, Union[SubgraphDetection.DataStructure.SimpleMindsporeGraph.snode.SNode, SubgraphDetection.DataStructure.SimpleMindsporeGraph.scope.Scope]]

get_max_level()[source]

Get the max level of the graph scope nodes :returns: max level

Return type

int

get_node_count()[source]

Count the number of nodes

static parse_MSGraph(msgraph)[source]

Parse a MSGraph to SMSGraph

Notes

In this function, We assigned id to every node-like object, even the scope_node & parameter_node.

Parameters

msgraph (mindinsight.datavisual.data_transform.graph.msgraph.MSGraph) – The MSGraph required to be parsed

Returns

all normal(defined by non_normal_node_type) snodes,

and the key is node id, value is SNode object.

scope_node: all name scope nodes that we created,

and the key is scope name, value is Scope object.

parameter_node: all parameter and const nodes,

and the key is node id, value is SNode object.

Return type

normal_node

Subgraph

Warning

May contrary to intuition, but subgraph is not a subclass of SMSGraph, it’s actually efficiently store all infomations about a set of isomorphic subgraphs. See 算法简述-名词解释 for more info.

class SubgraphDetection.DataStructure.Subgraph(pattern, nodes, min_node, min_node_index)[source]

The subgraph: Not a subclass of SMSGraph, organized to improve performance

__init__(pattern, nodes, min_node, min_node_index)[source]

Init a Subgraph with pattern,nodes,and min id nodes info

Parameters
  • pattern (Deque[str]) – The pattern of the subgraph, correspond to the nodes

  • nodes (List[Deque[SubgraphDetection.DataStructure.SimpleMindsporeGraph.snode.SNode]]) – Nodes the make up the subgraph, each tuple hold one place in the pattern

  • min_node (SubgraphDetection.DataStructure.SimpleMindsporeGraph.snode.SNode) – The least id node

  • min_node_index (int) – The index of least id node

Example

suppose we have a subgraph with two instance:
  • Node1(biaAdd)->Node2(Conv2D)

  • Node3(biaAdd)->Node4(Conv2D)

Then the member variables should be:
  • pattern : [‘biaAdd’, ‘Conv2D’]

  • nodes : [ (1,2) , (3,4) ]

  • min_node_id : Node-1

  • min_node_index : (0,0)

  • id : hash(‘1-2’)

__weakref__

list of weak references to the object (if defined)

property id

Get a unique id of a Subgraph

Returns

Hash of a string, which make up by the subgraph instance nodes that contains the smallest id node, nodes are numbered in ascending order, and split by ‘-‘

SubgraphCore

Note

A Subgraph Core won’t transform into a subgraph object untill finish growing.

class SubgraphDetection.DataStructure.SubgraphCore(nodes)[source]

The growing core of subgraph

__init__(nodes)[source]

Init a SubgraphCore with a tuple of Snode.

Parameters

nodes (Optional[Tuple[SubgraphDetection.DataStructure.SimpleMindsporeGraph.snode.SNode, ..]]) – The initial nodes, which should be all in same type

__iter__()[source]

Get a iter of the equivalent nodes in subgraph instance

Returns

self

__next__()[source]

Traverse to get the equivalent-nodes-tuple in subgraph instance

Notes

only these nodes whose id in boundary_nodes will be traversed

Returns

equivalent nodes tuple

Return type

Tuple

__repr__()[source]

Return repr(self).

commit()[source]

Commit the core after finish growing

Returns

self

grow(node_pattern, grow_nodes, keep_instance_index)[source]

Let the core grow

Parameters
  • node_pattern (Deque[str]) – The type of new nodes

  • grow_nodes (Deque[Deque[SubgraphDetection.DataStructure.SimpleMindsporeGraph.snode.SNode]]) – The new nodes

  • keep_instance_index (Tuple[int, ..]) – Which instances is going to keep

Returns

The new core grow from self

property is_valid_for_commit

Check whether if self is valid subgraph