Created by: liym27
https://github.com/PaddlePaddle/Paddle/pull/22678
Please review a small PR splited from This PR :1. 本PR支持动态图转静态图:
- 识别:Paddle 动态图API
- 改写:动态图API对应静态图API
2. API改写说明
示例1
- 动态图代码:
@dygraph_to_static_output
def fn_1(input):
# 该节点将被删除
pool2d_max = fluid.dygraph.Pool2D(pool_size=2, pool_type='max', pool_stride=1, global_pooling=False)
pool2d_max(input) # 1.普通表达式。 将被转化
res_1 = pool2d_max(input) # 2.赋值。将被转化
res_2 = pool2d_max(input) + pool2d_max(input) # 3. ast子节点调用动态图接口。将被转化
- 转化后的静态图:
def fn_1(input):
# 1
fluid.layers.pool2d(input, pool_size=2, pool_type='max', pool_stride=1, global_pooling=False)
# 2
res_1 = fluid.layers.pool2d(input, pool_size=2, pool_type='max', pool_stride=1, global_pooling=False)
# 3
res_2 = fluid.layers.pool2d(input, pool_size=2, pool_type='max', pool_stride=1, global_pooling=False) + fluid.layers.pool2d(input, pool_size=2, pool_type='max', pool_stride=1, global_pooling=False)
示例2
- 动态图
class DygraphLayer(fluid.dygraph.Layer):
def __init__(self):
super(DygraphLayer, self).__init__()
self.fc = fluid.dygraph.nn.Linear(
input_dim=10,
output_dim=5,
act='relu',
param_attr=fluid.ParamAttr(initializer=fluid.initializer.Constant(
value=0.99)),
bias_attr=fluid.ParamAttr(initializer=fluid.initializer.Constant(
value=0.5)), )
def forward(self, inputs):
prediction = self.fc(inputs)
return prediction
- 转化后的静态图:
def DygraphLayer_forward(inputs):
prediction = fluid.layers.fc(input, size=5, act='relu', param_attr=fluid.
ParamAttr(initializer=fluid.initializer.Constant(value=0.99)),
bias_attr=fluid.ParamAttr(initializer=fluid.initializer.Constant(
value=0.5)), num_flatten_dims=-1)
return prediction
todo
- 完善所有基本api的测试和转化 https://github.com/PaddlePaddle/Paddle/pull/22678
- 自定义类的转化[进行中]
- 本PR分成小PR