InternalGraph¶

class InternalGraph(name, qualname)[源代码]

`InternalGraph` 是 TracedModule 中使用的主要数据结构。它用来表示 Module 的 forward 方法的执行过程。

```import megengine.random as rand
import megengine.functional as F
import megengine.module as M

import megengine.traced_module as tm

class MyModule(M.Module):
def __init__(self):
super().__init__()
self.param = rand.normal(size=(3, 4))
self.linear = M.Linear(4, 5)

def forward(self, x):
return F.relu(self.linear(x + self.param))

net = MyModule()

inp = F.zeros(shape = (3, 4))
traced_module = tm.trace_module(net, inp)
```
Will produce the following `InternalGraph`:

print(traced_module.graph)

```MyModule.Graph (self, x) {
%2:     linear = getattr(self, "linear") -> (Linear)
%3:     param = getattr(self, "param") -> (Tensor)
%5:     linear_out = linear(add_out, )
%6:     relu_out = nn.relu(linear_out, )
return relu_out
}
```

```import megengine.functional as F
import megengine.module as M
import megengine.traced_module as tm

class MyModule(M.Module):
def forward(self, x):
x = x + 1
return x

net = MyModule()

inp = F.zeros(shape = (1, ))
traced_module = tm.trace_module(net, inp)
graph = traced_module.graph
inp_node = graph.inputs[1]
out_node = graph.outputs[0]
out = traced_module(inp)
```

```print(graph)
print(out)
```
```MyModule.Graph (self, x) {
}
((Tensor([1.], device=xpux:0), Tensor([0.], device=xpux:0)), Tensor([1.], device=xpux:0))
```
compile()[源代码]

eval(*inputs)[源代码]

inputs (`Tuple`[`Tensor`]) – 对应于 `graph.inputs[1:]` 的向量。

exprs(recursive=True)[源代码]

recursive – 是否获取子图中的 Exprs 。默认值：True

`ExprFilter` 包含计算图的所有 Exprs.

get_dep_exprs(nodes)[源代码]

nodes (`Sequence`[`Node`]) – 一个 `Node` 列表。

`List`[`Expr`]

get_expr_by_id(expr_id=None, recursive=True)[源代码]

get_function_by_type(func=None, recursive=True)[源代码]

get_method_by_type(method=None, recursive=True)[源代码]

get_module_by_type(module_cls, recursive=True)[源代码]

get_node_by_id(node_id=None, recursive=True)[源代码]

`Node``id` 可以通过下面的代码获得

```# node : Node
print("{:i}".format(node))
print(node.__format__("i"))
# graph : InternalGraph
print("{:i}".format(graph))
print(graph.__format__("i"))
```

get_node_by_name(name=None, ignorecase=True, recursive=True)[源代码]

`Node` 的全名可以通过下面的代码获得

```# node : Node
print("{:p}".format(node))
print(node.__format__("p"))
# graph : InternalGraph
print("{:p}".format(graph))
print(graph.__format__("p"))
```

• name (`Optional`[`str`]) – 一个在 glob 语法中可以包含 `?``*` 来匹配单个或任意字符的字符串。

• ignorecase (`bool`) – 是否忽略这种情况。默认值：True

• recursive – 是否获取子图中的 Nodes. 默认值：True

property inputs

`List`[`Node`]

insert_exprs(expr=None)[源代码]

```with graph.insert_exprs(e): # set the trace mode
... # trace function or module
... # inert exprs into graph and resotre normal mode
```

expr (`Optional`[`Expr`]) – 在哪个 `expr` 之后插入。如果为None，插入位置将根据输入节点自动设置。

property name

`str`

nodes(recursive=True)[源代码]

recursive – 是否获取子图中的 Nodes. 默认值：True

property outputs

`List`[`Node`]

property qualname

```import megengine.module as M
import megengine.traced_module as tm
import megengine as mge

class block(M.Module):
def __init__(self):
super().__init__()
self.relu = M.ReLU()

def forward(self, x):
return self.relu(x)

class module(M.Module):
def __init__(self):
super().__init__()
self.block = block()

def forward(self, x):
x = self.block(x)
return x

net = module()
traced_net = tm.trace_module(net, mge.Tensor([0.]))

qualname = traced_net.block.graph.qualname  # qualname = "module.block"
qualname = qualname.split(".", 1)[-1]  # qualname = "block"

assert qualname in list(map(lambda x: x[0], net.named_modules()))
assert qualname in list(map(lambda x: x[0], traced_net.named_modules()))
```

`str`

replace_node(repl_dict)[源代码]

repl_dict (`Dict`[`Node`, `Node`]) – 指定如何替换节点的 map {old_Node: new_Node} 。

reset_outputs(outputs)[源代码]

outputs – 内部元素为 Node 的对象。支持元组、列表、字典等。

```import megengine.functional as F
import megengine.module as M
import megengine.traced_module as tm

class MyModule(M.Module):
def forward(self, x):
x = x + 1
return x

net = MyModule()

inp = F.zeros(shape = (1, ))
traced_module = tm.trace_module(net, inp)
graph = traced_module.graph
inp_node = graph.inputs[1]
out_node = graph.outputs[0]
graph.reset_outputs((out_node, {"input": inp_node}))
out = traced_module(inp)
```

```print(graph)
print(out)
```
```MyModule.Graph (self, x) {