leozp / Myia-Issues

0 stars 0 forks source link

Relay 流程分析 —— Python #4

Open leozp opened 5 years ago

leozp commented 5 years ago

Python 调用方式

其他框架proto转到Relay

def from_mxnet(symbol, shape=None, dtype="float32", arg_params=None, aux_params=None):

Convert from MXNet"s model into compatible relay Function.


### 通过算子构建
- python/tvm/relay/testing
```python
from __future__ import absolute_import
from tvm import relay
from .init import create_workload

def get_net(batch_size,
            num_classes=10,
            image_shape=(1, 28, 28),
            dtype="float32"):

    data_shape = (batch_size,) + image_shape
    data = relay.var("data",
                     shape=data_shape,
                     dtype=dtype)
    data = relay.nn.batch_flatten(data)
    fc1 = relay.nn.dense(data, relay.var("fc1_weight"), units=128)
    fc1 = relay.nn.bias_add(fc1, relay.var("fc2_bias"))
    act1 = relay.nn.relu(fc1)
    fc2 = relay.nn.dense(act1, relay.var("fc2_weight"), units=64)
    fc2 = relay.nn.bias_add(fc2, relay.var("fc2_bias"))
    act2 = relay.nn.relu(fc2)
    fc3 = relay.nn.dense(act2, relay.var("fc3_weight"), units=num_classes)
    fc3 = relay.nn.bias_add(fc3, relay.var("fc3_bias"))
    mlp = relay.nn.softmax(data=fc3)
    args = relay.ir_pass.free_vars(mlp)
    return relay.Function(args, mlp)

def get_workload(batch_size,
                 num_classes=10,
                 image_shape=(1, 28, 28),
                 dtype="float32"):

    net = get_net(batch_size, num_classes, image_shape, dtype)
    return create_workload(net)

test 用例

leozp commented 5 years ago

Relay 语法前端

配置

Parser流程

除test用例外,没有其他地方调用?Parser当前并没有使用?

leozp commented 5 years ago

流程

Executor 调用流程

Parser流程

Cython build

另行分析

Op register

Test 流程

relay.frontend.from_mxnet/keras
relay.build_config
to_relay.to_relay
relay.build
graph_runtime.create
m.set_input
m.run()
out = m.get_output
leozp commented 5 years ago

文件树

|-- relay
|   |-- __init__.py
|   |-- base.py                 # 定义RelayNode(NodeBase)
|   |-- _expr.py                # 绑定C++文件
|   |-- expr.py                 # 定义expression node
|   |-- expr.pyi                # 和expr.py关系???
|   |-- _ir_pass.py             # 绑定C++文件
|   |-- _ir_pass.pyi                # 定义空函数
|   |-- ir_pass.py              # 定义passes
|   |-- _make.py                # 绑定C++文件
|   |-- _module.py              # 绑定C++文件
|   |-- _module.pyi             # 定义抽象类
|   |-- module.py               # 定义Modeule 
|   |-- _parser.py              # 定义AST解析相关函数
|   |-- parser.py               # 定义AST解析接口
|   |-- build_module.py             # 定义create_executor
|   |-- image.py                # 导出op.image
|   |-- nn.py                   # 导出op.nn
|   |-- scope_builder.py            # 定义ScopeBuilder
|   |-- ty.py                   # 定义type node
|   |-- ty.pyi                  # 和ty.py关系???
|   |-- vision.py               # 导出op.vision
|   |
|   |-- backend                 # 定义后端
|   |   |-- __init__.py
|   |   |-- _backend.py
|   |   |-- compile_engine.py
|   |   |-- graph_runtime_codegen.py
|   |   `-- interpreter.py
|   |
|   |-- frontend                # 定义前端
|   |   |-- __init__.py
|   |   |-- common.py
|   |   |-- mxnet.py
|   |   `-- nnvm_common.py
|   |
|   |-- grammar                 # 定义语法
|   |   |-- Relay.g4
|   |   |-- __init__.py
|   |   |-- py2
|   |   |   `-- __init__.py
|   |   `-- py3
|   |       `-- __init__.py
|   |
|   |-- op                  # 定义算子
|   |   |-- __init__.py
|   |   |-- _make.py
|   |   |-- _reduce.py
|   |   |-- _tensor.py
|   |   |-- _transform.py
|   |   |-- image
|   |   |   |-- __init__.py
|   |   |   |-- _image.py
|   |   |   |-- _make.py
|   |   |   `-- image.py
|   |   |-- nn
|   |   |   |-- __init__.py
|   |   |   |-- _make.py
|   |   |   |-- _nn.py
|   |   |   `-- nn.py
|   |   |-- op.py
|   |   |-- op_attrs.py
|   |   |-- reduce.py
|   |   |-- tensor.py
|   |   |-- transform.py
|   |   `-- vision
|   |       |-- __init__.py
|   |       |-- _make.py
|   |       |-- multibox.py
|   |       `-- nms.py
|   |
|   |-- testing                 # 测试用例
|   |   |-- __init__.py
|   |   |-- config.py
|   |   |-- dcgan.py
|   |   |-- densenet.py
|   |   |-- dqn.py
|   |   |-- inception_v3.py
|   |   |-- init.py
|   |   |-- layers.py
|   |   |-- lstm.py
|   |   |-- mlp.py
|   |   |-- mobilenet.py
|   |   |-- resnet.py
|   |   |-- squeezenet.py
|   |   `-- vgg.py