未验证 提交 c05a67a4 编写于 作者: J Jason 提交者: GitHub

Merge pull request #435 from SunAhong1993/paddle-2.0

add pytorch2paddle
......@@ -16,6 +16,7 @@ paddlepaddle >= 1.8.0
tensorflow : tensorflow == 1.14.0
caffe : 无
onnx : onnx >= 1.6.0
pytorch:torch >=1.5.0 (script方式暂不支持1.7.0)
## 安装
### 安装方式一(推荐)
......@@ -45,11 +46,10 @@ x2paddle --framework=caffe --prototxt=deploy.prototxt --weight=deploy.caffemodel
x2paddle --framework=onnx --model=onnx_model.onnx --save_dir=pd_model
```
### Paddle2ONNX
```
# 注意:paddle_infer_model_dir下需包含__model__和__params__两个文件
x2paddle --framework=paddle2onnx --model=paddle_infer_model_dir --save_dir=onnx_model
```
### PyTorch
> PyTorch不支持命令行使用方式,详见[PyTorch2Paddle](pytorch2paddle.md)
### 参数选项
| 参数 | |
|----------|--------------|
......
# PyTorch2Paddle
PyTorch2Paddle支持trace和script两种方式的转换,均是PyTorch动态图到Paddle动态图的转换,转换后的Paddle动态图运用动转静可转换为静态图模型。trace方式生成的代码可读性较强,较为接近原版PyTorch代码的组织结构;script方式不需要知道输入数据的类型和大小即可转换,使用上较为方便,但目前PyTorch支持的script代码方式有所限制,所以支持转换的代码也有所限制。用户可根据自身需求,选择转换方式。
## 环境依赖
python == 2.7 | python >= 3.5
paddlepaddle >= 1.8.0
pytorch:torch >=1.5.0 (script方式暂不支持1.7.0)
**使用trace方式需安装以下依赖**
pandas
treelib
## 使用方式
```
from x2paddle.convert import pytorch2paddle
pytorch2paddle(module=torch_module,
save_dir="./pd_model",
jit_type="trace",
input_examples=[torch_input])
# module (torch.nn.Module): PyTorch的Module。
# save_dir (str): 转换后模型的保存路径。
# jit_type (str): 转换方式。默认为"trace"。
# input_examples (list[torch.tensor]): torch.nn.Module的输入示例,list的长度必须与输入的长度一致。默认为None。
```
**注意:** 当jit_type为"trace"时,input_examples不可为None,转换后自动进行动转静;
当jit_type为"script"时",input_examples不为None时,才可以进行动转静。
## 使用示例
```
import torch
import numpy as np
from torchvision.models import AlexNet
from torchvision.models.utils import load_state_dict_from_url
# 构建输入
input_data = np.random.rand(1, 3, 224, 224).astype("float32")
# 获取PyTorch Module
torch_module = AlexNet()
torch_state_dict = load_state_dict_from_url('https://download.pytorch.org/models/alexnet-owt-4df8aa71.pth')
torch_module.load_state_dict(torch_state_dict)
# 设置为eval模式
torch_module.eval()
# 进行转换
from x2paddle.convert import pytorch2paddle
pytorch2paddle(torch_model,
save_dir="pd_model_trace",
jit_type="trace",
input_examples=[torch.tensor(input_data)])
```
\ No newline at end of file
......@@ -209,7 +209,7 @@ def onnx2paddle(model_path, save_dir, paddle_type, params_merge=False):
mapper.save_inference_model(save_dir, params_merge)
def pytorch2paddle(model_path, save_dir, jit_type, input_files):
def pytorch2paddle(module, save_dir, jit_type="trace", input_examples=None):
# check pytorch installation and version
try:
import torch
......@@ -225,21 +225,22 @@ def pytorch2paddle(model_path, save_dir, jit_type, input_files):
)
return
print("Now translating model from pytorch to paddle.")
from x2paddle.decoder.pytorch_decoder import ScriptDecoder, TraceDecoder
from x2paddle.op_mapper.pytorch2paddle import pytorch_op_mapper
from x2paddle.op_mapper.dygraph.pytorch2paddle.pytorch_op_mapper import PyTorchOpMapper
if jit_type == "trace":
model = TraceDecoder(model_path, input_files)
model = TraceDecoder(module, input_examples)
else:
model = ScriptDecoder(model_path)
mapper = pytorch_op_mapper.PyTorchOpMapper(model)
mapper.graph.build()
model = ScriptDecoder(module, input_examples)
mapper = PyTorchOpMapper(model)
mapper.paddle_graph.build()
print("Model optimizing ...")
from x2paddle.optimizer.pytorch_optimizer.optimizer import GraphOptimizer
graph_opt = GraphOptimizer()
graph_opt.optimize(mapper.graph)
from x2paddle.optimizer.optimizer import GraphOptimizer
graph_opt = GraphOptimizer(source_frame="pytorch", paddle_type="dygraph", jit_type=jit_type)
graph_opt.optimize(mapper.paddle_graph)
print("Model optimized.")
mapper.graph.gen_model(save_dir, jit_type, input_files)
mapper.paddle_graph.gen_model(save_dir, jit_type=jit_type)
def paddle2onnx(model_path, save_dir, opset_version=10):
......@@ -323,10 +324,6 @@ def main():
if args.params_merge:
params_merge = True
onnx2paddle(args.model, args.save_dir, args.paddle_type, params_merge)
elif args.framework == "pytorch":
assert args.model is not None, "--model should be defined while translating pytorch model"
pytorch2paddle(args.model, args.save_dir, args.jit_type, args.input_files)
elif args.framework == "paddle2onnx":
assert args.model is not None, "--model should be defined while translating paddle model to onnx"
paddle2onnx(args.model, args.save_dir, opset_version=args.onnx_opset)
......
......@@ -281,13 +281,14 @@ class PaddleGraph(object):
else:
self.gen_dygraph_code(save_dir)
self.dump_dygraph_parameter(save_dir)
input_shapes = list()
input_types = list()
for input_name in self.inputs:
input_shapes.append(self.inputs_info[input_name][0])
input_types.append(self.inputs_info[input_name][1])
# 如果input_files非空,则导出推理模型;其值类似[[None, 3, 224, 224]]
self.dygraph2static(save_dir, input_shapes, input_types)
# 动转静
if len(self.inputs_info) > 0:
input_shapes = list()
input_types = list()
for input_name in self.inputs:
input_shapes.append(self.inputs_info[input_name][0])
input_types.append(self.inputs_info[input_name][1])
self.dygraph2static(save_dir, input_shapes, input_types)
def gen_static_code(self, code_dir):
def write_code(f, code_list, indent=0):
......@@ -424,9 +425,7 @@ class PaddleGraph(object):
if self.edges_out.get(layer_id, 0) == 0:
for i, output_name in enumerate(layer.outputs):
if ("paddle.nn" in layer.kernel and "functional" not in layer.kernel) or \
(layer.kernel == "paddle.to_tensor" and layer.attrs["data"].startswith("params["))or \
"paddle.fluid.dygraph" in layer.kernel:
if ("paddle.nn" in layer.kernel and "functional" not in layer.kernel):
if i == 0:
continue
if output_name not in self.outputs:
......@@ -512,6 +511,8 @@ class PaddleGraph(object):
return_code = "return {}".format(", ".join(self.outputs))
self.forward_func.extend(gen_codes([return_code], indent=2))
for code_line in self.forward_func:
if "assert [1, 1] == 1 or [1, 1] == [1, 1], 'The [1, 1] must be [1, [1, 1]]!'" in code_line:
continue
f.write(code_line)
for code_line in self.run_func:
f.write(code_line)
......@@ -525,7 +526,6 @@ class PaddleGraph(object):
for layer_id, layer in self.layers.items():
if ("paddle.nn" in layer.kernel and "functional" not in layer.kernel
) or layer.kernel == "paddle.to_tensor" or \
"paddle.fluid.dygraph" in layer.kernel or \
layer.kernel.startswith("custom_layer"):
line = "{}".format(
layer.outputs[0]
......@@ -566,7 +566,7 @@ class PaddleGraph(object):
self.forward_func.extend(gen_codes([line], indent=indent))
elif "prim" in layer.kernel:
func_name = layer.kernel.replace(".", "_")
from x2paddle.op_mapper.dygraph import prim2code
from x2paddle.op_mapper.dygraph.pytorch2paddle import prim2code
if hasattr(prim2code, func_name):
func = getattr(prim2code, func_name)
func(
......@@ -594,7 +594,7 @@ class PaddleGraph(object):
line = line.strip(", ")
line += ")"
if layer.kernel == "self.create_parameter":
self.init_func.extend(gen_codes(["self." + line], indent=indent))
self.init_func.extend(gen_codes(["self." + line], indent=2))
self.forward_func.extend(gen_codes(["{} = self.{}".format(layer.outputs[0],
layer.outputs[0])], indent=indent))
else:
......@@ -614,7 +614,6 @@ class PaddleGraph(object):
from paddle.fluid.dygraph.jit import declarative
sepc_list = list()
for i, name in enumerate(self.inputs):
input_shapes[i][0] = -1
sepc_list.append(
paddle.static.InputSpec(
shape=input_shapes[i], name=name, dtype=input_types[i]))
......@@ -631,4 +630,11 @@ class PaddleGraph(object):
model.set_dict(restore)
model.eval()
static_model = paddle.jit.to_static(model, input_spec=sepc_list)
paddle.jit.save(static_model, osp.join(save_dir, "inference_model/model"))
\ No newline at end of file
try:
paddle.jit.save(static_model, osp.join(save_dir, "inference_model/model"))
except ValueError as e:
if str(e) == "'target_vars' should be a list of Variable.":
print("[DyGraph2StaticGraph Error] Can not convert the dygraph to static! The output of PyTorch mustbe Variable or a list of Variable.")
else:
print(e)
exit(0)
......@@ -41,9 +41,10 @@ class ScriptDecoder(Decoder):
script_path (str): ScriptModule保存路径。
model_path (str): PyTorchModule保存路径。
"""
def __init__(self, script_path=None):
self.script = torch.jit.load(script_path)
def __init__(self, module, input_examples=None):
self.script = torch.jit.script(module)
self.graph = self._optimize_graph(self.script.inlined_graph)
self.input_examples = input_examples
class TraceDecoder(Decoder):
""" PyTorchModule后使用trace方式转换为ScriptModule。
......@@ -53,14 +54,16 @@ class TraceDecoder(Decoder):
input_files (list): 输入网络的numpy,每个numpy保存成.npy文件,
文件路径存储在input_files中。
"""
def __init__(self, model_path, input_files=list()):
# TODO(syf): 传入pytorch的Module(即import),否则出错
model = torch.load(model_path)
model.eval()
input_list = list()
for npy_file in input_files:
input_list.append(torch.tensor(np.load(npy_file)))
self.script = torch.jit.trace(model, input_list, strict=False)
def __init__(self, module, input_examples):
try:
self.script = torch.jit.trace(module, input_examples)
except RuntimeError as e:
if "strict" in str(e):
self.script = torch.jit.trace(module, input_examples, strict=False)
else:
print(e)
exit(0)
self.graph = self._optimize_graph(self.script.inlined_graph)
# print(self.graph)
# print(getattr(getattr(self.script.decoder.block, "5").layer, "2"))
self.input_examples = input_examples
......@@ -1180,7 +1180,7 @@ class OpSet9():
scale=beta)
add_inputs = {"x": val_mm, "y": var_beta}
self.paddle_graph.add_layer(
"paddle.addd",
"paddle.add",
inputs=add_inputs,
outputs=[node.layer_name])
......
......@@ -12,6 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import copy
from x2paddle.core.util import *
from x2paddle.core.program import PaddleGraph
......@@ -37,6 +38,7 @@ def aten_abs(mapper, graph, node):
%n0.3 (Tensor): 绝对值后的Tensor。
%n.3 (Tensor): 绝对值前的Tensor。
"""
scope_name = mapper.normalize_scope_name(node)
output_name = mapper._get_outputs_name(node)[0]
layer_outputs = [output_name]
layer_inputs = {}
......@@ -44,13 +46,13 @@ def aten_abs(mapper, graph, node):
# 获取当前节点输出的list
current_outputs = [output_name]
# 处理输入0,即%n.3
mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs)
mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs, scope_name)
layer_inputs["x"] = inputs_name[0]
# 获取当前节点输入的list
current_inputs = list(layer_inputs.values())
graph.add_layer(
"paddle.fluid.layers.abs", inputs=layer_inputs, outputs=layer_outputs)
"paddle.abs", inputs=layer_inputs, outputs=layer_outputs, scope_name=scope_name)
return current_inputs, current_outputs
......@@ -64,33 +66,40 @@ def aten_adaptive_avg_pool2d(mapper, graph, node):
%x.3 (Tensor): 输入Tensor。
%_output_size.1 (list): 自适应池化后的Tensor的宽、高大小。
"""
scope_name = mapper.normalize_scope_name(node)
op_name = name_generator("pool2d", mapper.nn_name2id)
output_name = mapper._get_outputs_name(node)[0]
layer_outputs = [output_name]
layer_outputs = [op_name, output_name]
layer_inputs = {}
layer_attrs = {}
inputs_name, inputs_node = mapper._get_inputs_name(node)
# 获取当前节点输出的list
current_outputs = [output_name]
# 处理输入0,即%x.3
mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs)
layer_inputs["input"] = inputs_name[0]
mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs, scope_name)
layer_inputs["x"] = inputs_name[0]
# 获取当前节点输入的list
current_inputs = list(layer_inputs.values())
# 处理输入1,即%_output_size.1
if inputs_name[1] in mapper.attrs:
layer_attrs["pool_size"] = mapper.attrs[inputs_name[1]]
layer_attrs["output_size"] = mapper.attrs[inputs_name[1]]
graph.add_layer(
"paddle.nn.AdaptiveAvgPool2D",
inputs=layer_inputs,
outputs=layer_outputs,
scope_name=scope_name,
**layer_attrs)
else:
mapper._check_input(graph, inputs_node[1], inputs_name[1],
current_outputs)
layer_inputs["pool_size"] = inputs_name[1]
current_outputs, scope_name)
layer_inputs["output_size"] = inputs_name[1]
current_inputs.append(inputs_name[1])
layer_attrs["pool_type"] = string("avg")
graph.add_layer(
"fluid.layers.adaptive_pool2d",
inputs=layer_inputs,
outputs=layer_outputs,
**layer_attrs)
graph.add_layer(
"paddle.nn.functional.adaptive_avg_pool2d",
inputs=layer_inputs,
outputs=layer_outputs[1:],
scope_name=scope_name,
**layer_attrs)
return current_inputs, current_outputs
......@@ -107,6 +116,7 @@ def aten_addmm(mapper, graph, node):
%151 (int/float): 输入alpha。
%152 (int/float): 输入beta。
"""
scope_name = mapper.normalize_scope_name(node)
output_name = mapper._get_outputs_name(node)[0]
layer_outputs = [output_name]
layer_inputs = {}
......@@ -116,13 +126,13 @@ def aten_addmm(mapper, graph, node):
current_outputs = [output_name]
# 处理输入0,即%150
mapper._check_input(
graph, inputs_node[0], inputs_name[0], current_outputs, add_dim=True)
graph, inputs_node[0], inputs_name[0], current_outputs, scope_name, add_dim=True)
layer_inputs["input"] = inputs_name[0]
# 处理输入1,即%input.3
mapper._check_input(graph, inputs_node[1], inputs_name[1], current_outputs)
mapper._check_input(graph, inputs_node[1], inputs_name[1], current_outputs, scope_name)
layer_inputs["x"] = inputs_name[1]
# 处理输入2,即%156
mapper._check_input(graph, inputs_node[2], inputs_name[2], current_outputs)
mapper._check_input(graph, inputs_node[2], inputs_name[2], current_outputs, scope_name)
layer_inputs["y"] = inputs_name[2]
# 获取当前节点输入的list
current_inputs = list(layer_inputs.values())
......@@ -131,7 +141,7 @@ def aten_addmm(mapper, graph, node):
layer_attrs["beta"] = mapper.attrs[inputs_name[3]]
else:
mapper._check_input(graph, inputs_node[3], inputs_name[3],
current_outputs)
current_outputs, scope_name)
layer_inputs["beta"] = inputs_name[3]
current_inputs.append(inputs_name[3])
# 处理输入4,即%151
......@@ -139,7 +149,7 @@ def aten_addmm(mapper, graph, node):
layer_attrs["alpha"] = mapper.attrs[inputs_name[4]]
else:
mapper._check_input(graph, inputs_node[4], inputs_name[4],
current_outputs)
current_outputs, scope_name)
layer_inputs["alpha"] = inputs_name[4]
current_inputs.append(inputs_name[4])
......@@ -147,6 +157,7 @@ def aten_addmm(mapper, graph, node):
"paddle.addmm",
inputs=layer_inputs,
outputs=layer_outputs,
scope_name=scope_name,
**layer_attrs)
return current_inputs, current_outputs
......@@ -161,6 +172,7 @@ def aten_add(mapper, graph, node):
%i.12 (-): 输入数值 x。
%288 (-): 输入数值 y。
"""
scope_name = mapper.normalize_scope_name(node)
output_name = mapper._get_outputs_name(node)[0]
layer_outputs = [output_name]
layer_inputs = {}
......@@ -168,16 +180,16 @@ def aten_add(mapper, graph, node):
# 获取当前节点输出的list
current_outputs = [output_name]
# 处理输入0,即%i.12
mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs)
mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs, scope_name)
layer_inputs["x"] = inputs_name[0]
# 处理输入1,即%288
mapper._check_input(
graph, inputs_node[1], inputs_name[1], current_outputs, add_dim=True)
graph, inputs_node[1], inputs_name[1], current_outputs, scope_name, add_dim=True)
layer_inputs["y"] = inputs_name[1]
# 获取当前节点输入的list
current_inputs = list(layer_inputs.values())
graph.add_layer("prim.add", inputs=layer_inputs, outputs=layer_outputs)
graph.add_layer("prim.add", inputs=layer_inputs, outputs=layer_outputs, scope_name=scope_name)
return current_inputs, current_outputs
......@@ -192,6 +204,7 @@ def aten_add_(mapper, graph, node):
%150 (Tensor): 输入Tensor y。
%151 (int/float): 输入alpha。
"""
scope_name = mapper.normalize_scope_name(node)
output_name = mapper._get_outputs_name(node)[0]
layer_outputs = [output_name]
layer_inputs = {}
......@@ -200,11 +213,11 @@ def aten_add_(mapper, graph, node):
# 获取当前节点输出的list
current_outputs = [output_name]
# 处理输入0,即%output.2
mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs)
mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs, scope_name)
layer_inputs["x"] = inputs_name[0]
# 处理输入1,即%150
mapper._check_input(
graph, inputs_node[1], inputs_name[1], current_outputs, add_dim=True)
graph, inputs_node[1], inputs_name[1], current_outputs, scope_name, add_dim=True)
layer_inputs["y"] = inputs_name[1]
# 获取当前节点输入的list
current_inputs = list(layer_inputs.values())
......@@ -213,12 +226,12 @@ def aten_add_(mapper, graph, node):
layer_attrs["alpha"] = mapper.attrs[inputs_name[2]]
else:
mapper._check_input(graph, inputs_node[2], inputs_name[2],
current_outputs)
current_outputs, scope_name)
layer_inputs["alpha"] = inputs_name[2]
current_inputs.append(inputs_name[2])
graph.add_layer(
"prim.add_", inputs=layer_inputs, outputs=layer_outputs, **layer_attrs)
"prim.add_", inputs=layer_inputs, outputs=layer_outputs, scope_name=scope_name, **layer_attrs)
return current_inputs, current_outputs
......@@ -232,6 +245,7 @@ def aten___and__(mapper, graph, node):
%360 (-): 输入 x。
%358 (-): 输入 y。
"""
scope_name = mapper.normalize_scope_name(node)
output_name = mapper._get_outputs_name(node)[0]
layer_outputs = [output_name]
layer_inputs = {}
......@@ -239,15 +253,15 @@ def aten___and__(mapper, graph, node):
# 获取当前节点输出的list
current_outputs = [output_name]
# 处理输入0,即%i.12
mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs)
mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs, scope_name)
layer_inputs["x"] = inputs_name[0]
# 处理输入1,即%288
mapper._check_input(graph, inputs_node[1], inputs_name[1], current_outputs)
mapper._check_input(graph, inputs_node[1], inputs_name[1], current_outputs, scope_name)
layer_inputs["y"] = inputs_name[1]
# 获取当前节点输入的list
current_inputs = list(layer_inputs.values())
graph.add_layer("prim.and", inputs=layer_inputs, outputs=layer_outputs)
graph.add_layer("prim.and", inputs=layer_inputs, outputs=layer_outputs, scope_name=scope_name)
return current_inputs, current_outputs
......@@ -261,21 +275,22 @@ def aten_append(mapper, graph, node):
%_output_size.1 (list): 需要进行append的list。
%v.1 (-): append的元素。
"""
scope_name = mapper.normalize_scope_name(node)
layer_inputs = {}
inputs_name, inputs_node = mapper._get_inputs_name(node)
layer_outputs = [inputs_name[0]]
# 获取当前节点输出的list
current_outputs = [inputs_name[0]]
# 处理输入0,即_output_size.1
mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs)
mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs, scope_name)
layer_inputs["list"] = inputs_name[0]
# 处理输入1,即v.1
mapper._check_input(graph, inputs_node[1], inputs_name[1], current_outputs)
mapper._check_input(graph, inputs_node[1], inputs_name[1], current_outputs, scope_name)
layer_inputs["element"] = inputs_name[1]
# 获取当前节点输入的list
current_inputs = list(layer_inputs.values())
graph.add_layer("prim.append", inputs=layer_inputs, outputs=layer_outputs)
graph.add_layer("prim.append", inputs=layer_inputs, outputs=layer_outputs, scope_name=scope_name)
return current_inputs, current_outputs
......@@ -285,6 +300,7 @@ def aten_arange(mapper, graph, node):
TorchScript示例:
有三种情况,分别处理。
"""
scope_name = mapper.normalize_scope_name(node)
output_name = mapper._get_outputs_name(node)[0]
layer_outputs = [output_name]
layer_inputs = {}
......@@ -301,7 +317,7 @@ def aten_arange(mapper, graph, node):
layer_attrs["end"] = mapper.attrs[inputs_name[0]]
else:
mapper._check_input(graph, inputs_node[0], inputs_name[0],
current_outputs)
current_outputs, scope_name)
layer_inputs["end"] = inputs_name[0]
current_inputs.append(inputs_name[0])
# 处理输入1,即%43,代表dtype
......@@ -317,7 +333,7 @@ def aten_arange(mapper, graph, node):
layer_attrs["start"] = mapper.attrs[inputs_name[0]]
else:
mapper._check_input(graph, inputs_node[0], inputs_name[0],
current_outputs)
current_outputs, scope_name)
layer_inputs["start"] = inputs_name[0]
current_inputs.append(inputs_name[0])
# 处理输入1,即%52,代表end
......@@ -325,7 +341,7 @@ def aten_arange(mapper, graph, node):
layer_attrs["end"] = mapper.attrs[inputs_name[1]]
else:
mapper._check_input(graph, inputs_node[1], inputs_name[1],
current_outputs)
current_outputs, scope_name)
layer_inputs["end"] = inputs_name[1]
current_inputs.append(inputs_name[1])
# 处理输入2,即%43,代表dtype
......@@ -341,7 +357,7 @@ def aten_arange(mapper, graph, node):
layer_attrs["start"] = mapper.attrs[inputs_name[0]]
else:
mapper._check_input(graph, inputs_node[0], inputs_name[0],
current_outputs)
current_outputs, scope_name)
layer_inputs["start"] = inputs_name[0]
current_inputs.append(inputs_name[0])
# 处理输入1,即%52,代表end
......@@ -349,7 +365,7 @@ def aten_arange(mapper, graph, node):
layer_attrs["end"] = mapper.attrs[inputs_name[1]]
else:
mapper._check_input(graph, inputs_node[1], inputs_name[1],
current_outputs)
current_outputs, scope_name)
layer_inputs["end"] = inputs_name[1]
current_inputs.append(inputs_name[1])
# 处理输入2,即%53,代表step
......@@ -357,7 +373,7 @@ def aten_arange(mapper, graph, node):
layer_attrs["step"] = mapper.attrs[inputs_name[2]]
else:
mapper._check_input(graph, inputs_node[2], inputs_name[2],
current_outputs)
current_outputs, scope_name)
layer_inputs["step"] = inputs_name[2]
current_inputs.append(inputs_name[2])
# 处理输入3,即%43,代表dtype
......@@ -373,6 +389,7 @@ def aten_arange(mapper, graph, node):
"paddle.arange",
inputs=layer_inputs,
outputs=layer_outputs,
scope_name=scope_name,
**layer_attrs)
return current_inputs, current_outputs
......@@ -392,20 +409,17 @@ def aten_avg_pool2d(mapper, graph, node):
%272 (bool): 是否在平均池化模式不忽略填充值,False为忽略。
%271 (int): 如果指定,它将用作除数,否则将使用池化区域的大小。
"""
if "pool" in mapper.dygraph_name_id:
mapper.dygraph_name_id["pool"] += 1
else:
mapper.dygraph_name_id["pool"] = 0
pool_name = "pool" + str(mapper.dygraph_name_id["pool"])
scope_name = mapper.normalize_scope_name(node)
op_name = name_generator("pool2d", mapper.nn_name2id)
output_name = mapper._get_outputs_name(node)[0]
layer_outputs = [pool_name, output_name]
layer_outputs = [op_name, output_name]
layer_inputs = {}
layer_attrs = {}
inputs_name, inputs_node = mapper._get_inputs_name(node)
# 获取当前节点输出的list
current_outputs = [output_name]
# 处理输入0,即%x.34
mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs)
mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs, scope_name)
layer_inputs["input"] = inputs_name[0]
# 获取当前节点输入的list
current_inputs = list(layer_inputs.values())
......@@ -424,15 +438,161 @@ def aten_avg_pool2d(mapper, graph, node):
"prim.assert",
inputs={},
outputs=[inputs_name[6] + "_assert"],
scope_name=scope_name if scope_name == "" else scope_name + "_assert",
type="eq",
key=mapper.attrs[inputs_name[6]],
value=None)
# TODO(syf): The op has diff.
# self.paddle_graph.add_layer(
# kernel="paddle.nn.AvgPool2D",
# inputs={"input": input_name},
# outputs=layer_outputs,
# kernel_size=k_size[2:4],
# stride=strides[2:4],
# padding=string(pad_mode))
layer_attrs["pool_type"] = string("avg")
graph.add_layer(
"fluid.layers.pool2d",
inputs=layer_inputs,
outputs=layer_outputs[1:],
scope_name=scope_name,
**layer_attrs)
return current_inputs, current_outputs
def aten_avg_pool3d(mapper, graph, node):
""" 构造最大池化的PaddleLayer。
TorchScript示例:
%branch_pool.2 : Tensor = aten::avg_pool2d(%x.43, %538, %539, %540, %273, %272, %271)
参数含义:
%branch_pool.2 (Tensor): 输出,池化后的结果。
%x.43 (Tensor): 需要池化的Tensor。
%538 (list): 池化kernel的大小。
%539 (list): 步长大小。
%540 (list): 填充大小。
%273 (bool): 是否用ceil函数计算输出高度和宽度。
%272 (bool): 是否在平均池化模式不忽略填充值,False为忽略。
%271 (int): 如果指定,它将用作除数,否则将使用池化区域的大小。
"""
scope_name = mapper.normalize_scope_name(node)
op_name = name_generator("pool2d", mapper.nn_name2id)
output_name = mapper._get_outputs_name(node)[0]
layer_outputs = [op_name, output_name]
layer_inputs = {}
layer_attrs = {}
inputs_name, inputs_node = mapper._get_inputs_name(node)
# 获取当前节点输出的list
current_outputs = [output_name]
# 处理输入0,即%x.34
mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs, scope_name)
layer_inputs["input"] = inputs_name[0]
# 获取当前节点输入的list
current_inputs = list(layer_inputs.values())
# 处理输入1,即%538
layer_attrs["pool_size"] = mapper.attrs[inputs_name[1]]
# 处理输入2,即%539
layer_attrs["pool_stride"] = mapper.attrs[inputs_name[2]]
# 处理输入3,即%540
layer_attrs["pool_padding"] = mapper.attrs[inputs_name[3]]
# 处理输入4,即%273
layer_attrs["ceil_mode"] = mapper.attrs[inputs_name[4]]
# 处理输入5,即%272
layer_attrs["exclusive"] = not mapper.attrs[inputs_name[5]]
# 处理输入6,即%271
graph.add_layer(
"paddle.nn.Pool2D",
"prim.assert",
inputs={},
outputs=[inputs_name[6] + "_assert"],
scope_name=scope_name if scope_name == "" else scope_name + "_assert",
type="eq",
key=mapper.attrs[inputs_name[6]],
value=None)
# TODO(syf): The op has diff.
# self.paddle_graph.add_layer(
# kernel="paddle.nn.AvgPool2D",
# inputs={"input": input_name},
# outputs=layer_outputs,
# kernel_size=k_size[2:4],
# stride=strides[2:4],
# padding=string(pad_mode))
layer_attrs["pool_type"] = string("avg")
graph.add_layer(
"fluid.layers.pool3d",
inputs=layer_inputs,
outputs=layer_outputs,
outputs=layer_outputs[1:],
scope_name=scope_name,
**layer_attrs)
return current_inputs, current_outputs
def aten_avg_pool1d(mapper, graph, node):
""" 构造最大池化的PaddleLayer。
TorchScript示例:
%branch_pool.2 : Tensor = aten::avg_pool1d(%x.43, %538, %539, %540, %273, %272, %271)
参数含义:
%branch_pool.2 (Tensor): 输出,池化后的结果。
%x.43 (Tensor): 需要池化的Tensor。
%538 (list): 池化kernel的大小。
%539 (list): 步长大小。
%540 (list): 填充大小。
%273 (bool): 是否用ceil函数计算输出高度和宽度。
%272 (bool): 是否在平均池化模式不忽略填充值,False为忽略。
%271 (int): 如果指定,它将用作除数,否则将使用池化区域的大小。
"""
scope_name = mapper.normalize_scope_name(node)
op_name = name_generator("pool2d", mapper.nn_name2id)
output_name = mapper._get_outputs_name(node)[0]
layer_outputs = [op_name, output_name]
layer_inputs = {}
layer_attrs = {}
inputs_name, inputs_node = mapper._get_inputs_name(node)
# 获取当前节点输出的list
current_outputs = [output_name]
# 处理输入0,即%x.34
mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs, scope_name)
layer_inputs["input"] = inputs_name[0]
# 获取当前节点输入的list
current_inputs = list(layer_inputs.values())
# 处理输入1,即%538
layer_attrs["pool_size"] = mapper.attrs[inputs_name[1]]
# 处理输入2,即%539
layer_attrs["pool_stride"] = mapper.attrs[inputs_name[2]]
# 处理输入3,即%540
layer_attrs["pool_padding"] = mapper.attrs[inputs_name[3]]
# 处理输入4,即%273
layer_attrs["ceil_mode"] = mapper.attrs[inputs_name[4]]
# 处理输入5,即%272
layer_attrs["exclusive"] = not mapper.attrs[inputs_name[5]]
# 处理输入6,即%271
graph.add_layer(
"prim.assert",
inputs={},
outputs=[inputs_name[6] + "_assert"],
scope_name=scope_name if scope_name == "" else scope_name + "_assert",
type="eq",
key=mapper.attrs[inputs_name[6]],
value=None)
# TODO(syf): The op has diff.
# self.paddle_graph.add_layer(
# kernel="paddle.nn.AvgPool2D",
# inputs={"input": input_name},
# outputs=layer_outputs,
# kernel_size=k_size[2:4],
# stride=strides[2:4],
# padding=string(pad_mode))
layer_attrs["pool_type"] = string("avg")
graph.add_layer(
"fluid.layers.pool1d",
inputs=layer_inputs,
outputs=layer_outputs[1:],
scope_name=scope_name,
**layer_attrs)
return current_inputs, current_outputs
......@@ -455,13 +615,10 @@ def aten_batch_norm(mapper, graph, node):
%766 (float): 为了数值稳定加在分母上的值。
%781 (bool): 是否启用cudnn。
"""
if "batchnorm" in mapper.dygraph_name_id:
mapper.dygraph_name_id["batchnorm"] += 1
else:
mapper.dygraph_name_id["batchnorm"] = 0
batchnorm_name = "batchnorm" + str(mapper.dygraph_name_id["batchnorm"])
scope_name = mapper.normalize_scope_name(node)
op_name = name_generator("batchnorm", mapper.nn_name2id)
output_name = mapper._get_outputs_name(node)[0]
layer_outputs = [batchnorm_name, output_name]
layer_outputs = [op_name, output_name]
layer_inputs = {}
layer_attrs = {}
layer_attrs["is_test"] = True
......@@ -469,27 +626,27 @@ def aten_batch_norm(mapper, graph, node):
# 获取当前节点输出的list
current_outputs = [output_name]
# 处理输入0,即%input.80
mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs)
mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs, scope_name)
layer_inputs["input"] = inputs_name[0]
# 获取当前节点输入、输出的list
current_inputs = list(layer_inputs.values())
# 处理输入1,即%778
weights = mapper.pytorch_params[inputs_name[1]]
mapper.paddle_params[batchnorm_name + ".weight"] = weights
mapper.paddle_params[op_name + ".weight"] = weights
layer_attrs['num_channels'] = weights.shape[0]
# 处理输入2,即%779
if inputs_name[2] in mapper.pytorch_params:
bias = mapper.pytorch_params[inputs_name[2]]
if bias is not None:
mapper.paddle_params[batchnorm_name + ".bias"] = bias
mapper.paddle_params[op_name + ".bias"] = bias
else:
mapper.paddle_params[batchnorm_name + ".bias"] = False
mapper.paddle_params[op_name + ".bias"] = False
# 处理输入3,即%776
mean = mapper.pytorch_params[inputs_name[3]]
mapper.paddle_params[batchnorm_name + "._mean"] = mean
mapper.paddle_params[op_name + "._mean"] = mean
# 处理输入4,即%777
var = mapper.pytorch_params[inputs_name[4]]
mapper.paddle_params[batchnorm_name + "._variance"] = var
mapper.paddle_params[op_name + "._variance"] = var
# 处理输入6,即%exponential_average_factor.23
layer_attrs["momentum"] = mapper.attrs[inputs_name[6]]
# 处理输入7,即%766
......@@ -499,6 +656,7 @@ def aten_batch_norm(mapper, graph, node):
"paddle.nn.BatchNorm",
inputs=layer_inputs,
outputs=layer_outputs,
scope_name=scope_name,
**layer_attrs)
return current_inputs, current_outputs
......@@ -513,6 +671,7 @@ def aten_cat(mapper, graph, node):
%i.12 (list): 需要连接的Tensor组成的list。
%7 (int): 连接的轴。
"""
scope_name = mapper.normalize_scope_name(node)
output_name = mapper._get_outputs_name(node)[0]
layer_outputs = [output_name]
layer_inputs = {}
......@@ -521,8 +680,8 @@ def aten_cat(mapper, graph, node):
# 获取当前节点输出的list
current_outputs = [output_name]
# 处理输入0,即%13
mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs)
layer_inputs["input"] = inputs_name[0]
mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs, scope_name)
layer_inputs["x"] = inputs_name[0]
# 获取当前节点输入的list
current_inputs = list(layer_inputs.values())
# 处理输入1,即%12
......@@ -530,13 +689,14 @@ def aten_cat(mapper, graph, node):
layer_attrs["axis"] = mapper.attrs[inputs_name[1]]
else:
mapper._check_input(graph, inputs_node[1], inputs_name[1],
current_outputs)
current_outputs, scope_name)
layer_inputs["axis"] = inputs_name[1]
current_inputs.append(inputs_name[1])
graph.add_layer(
"fluid.layers.concat",
"paddle.concat",
inputs=layer_inputs,
outputs=layer_outputs,
scope_name=scope_name,
**layer_attrs)
return current_inputs, current_outputs
......@@ -552,6 +712,7 @@ def aten_chunk(mapper, graph, node):
%720 (int): 分割的块数。
%719 (int): 分割的维度。
"""
scope_name = mapper.normalize_scope_name(node)
output_name = mapper._get_outputs_name(node)[0]
layer_outputs = [output_name]
layer_inputs = {}
......@@ -560,8 +721,8 @@ def aten_chunk(mapper, graph, node):
# 获取当前节点输出的list
current_outputs = [output_name]
# 处理输入0,即%input.170
mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs)
layer_inputs["input"] = inputs_name[0]
mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs, scope_name)
layer_inputs["x"] = inputs_name[0]
# 获取当前节点输入的list
current_inputs = list(layer_inputs.values())
# 处理输入1,即%720
......@@ -569,21 +730,22 @@ def aten_chunk(mapper, graph, node):
layer_attrs["num_or_sections"] = mapper.attrs[inputs_name[1]]
else:
mapper._check_input(graph, inputs_node[1], inputs_name[1],
current_outputs)
current_outputs, scope_name)
layer_inputs["num_or_sections"] = inputs_name[1]
current_inputs.append(inputs_name[1])
# 处理输入2,即%719
if inputs_name[2] in mapper.attrs:
layer_attrs["dim"] = mapper.attrs[inputs_name[2]]
layer_attrs["axis"] = mapper.attrs[inputs_name[2]]
else:
mapper._check_input(graph, inputs_node[2], inputs_name[2],
current_outputs)
layer_inputs["dim"] = inputs_name[2]
current_outputs, scope_name)
layer_inputs["axis"] = inputs_name[2]
current_inputs.append(inputs_name[2])
graph.add_layer(
"fluid.layers.split",
"paddle.split",
inputs=layer_inputs,
outputs=layer_outputs,
scope_name=scope_name,
**layer_attrs)
return current_inputs, current_outputs
......@@ -598,6 +760,7 @@ def aten___contains__(mapper, graph, node):
%50 (-): 需对比的输入1。
%name.1 (-): 需对比的输入2。
"""
scope_name = mapper.normalize_scope_name(node)
output_name = mapper._get_outputs_name(node)[0]
layer_outputs = [output_name]
layer_inputs = {}
......@@ -605,15 +768,15 @@ def aten___contains__(mapper, graph, node):
# 获取当前节点输出的list
current_outputs = [output_name]
# 处理输入0,即%50
mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs)
mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs, scope_name)
layer_inputs["input"] = inputs_name[0]
# 处理输入1,即%name.1
mapper._check_input(graph, inputs_node[1], inputs_name[1], current_outputs)
mapper._check_input(graph, inputs_node[1], inputs_name[1], current_outputs, scope_name)
layer_inputs["element"] = inputs_name[1]
# 获取当前节点输入的list
current_inputs = list(layer_inputs.values())
graph.add_layer("prim.contain", inputs=layer_inputs, outputs=layer_outputs)
graph.add_layer("prim.contain", inputs=layer_inputs, outputs=layer_outputs, scope_name=scope_name)
return current_inputs, current_outputs
......@@ -628,88 +791,93 @@ def aten_constant_pad_nd(mapper, graph, node):
%4876 (list): 填充大小。
%42 (-): 填充值。
"""
if "constant_pad" in mapper.dygraph_name_id:
mapper.dygraph_name_id["constant_pad"] += 1
else:
mapper.dygraph_name_id["constant_pad"] = 0
constant_pad_name = "constant_pad" + str(mapper.dygraph_name_id[
"constant_pad"])
scope_name = mapper.normalize_scope_name(node)
op_name = name_generator("pad", mapper.nn_name2id)
output_name = mapper._get_outputs_name(node)[0]
layer_outputs = [constant_pad_name, output_name]
layer_outputs = [op_name, output_name]
layer_inputs = {}
layer_attrs = {}
inputs_name, inputs_node = mapper._get_inputs_name(node)
# 获取当前节点输出的list
current_outputs = [output_name]
# 处理输入0,即%input1.24
mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs)
mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs, scope_name)
layer_inputs["input"] = inputs_name[0]
# 获取当前节点输入的list
current_inputs = list(layer_inputs.values())
# 处理输入1,即%4876
layer_attrs["padding"] = mapper.attrs[inputs_name[1]]
# 处理输入2,即%42
layer_attrs["value"] = mapper.attrs[inputs_name[2]]
layer_attrs["pad_value"] = mapper.attrs[inputs_name[2]]
graph.add_layer(
"fluid.layers.shape",
"prim.shape",
inputs={"input": inputs_name[0]},
outputs=[inputs_name[0] + "_shape"])
outputs=[inputs_name[0] + "_shape"],
scope_name=scope_name)
graph.add_layer(
"prim.len",
inputs={"input": inputs_name[0] + "_shape"},
outputs=[inputs_name[0] + "_len"])
outputs=[inputs_name[0] + "_len"],
scope_name=scope_name)
def add_pad_layers(kernel, dim):
graph.add_layer(
"prim.ne",
inputs={"x": inputs_name[0] + "_len"},
outputs=[inputs_name[0] + "_cond"],
scope_name=scope_name,
y=dim)
graph.add_layer(
"prim.if", {'input': inputs_name[0] + "_cond"},
outputs=[inputs_name[0] + "_if", output_name])
outputs=[inputs_name[0] + "_if", output_name],
scope_name=scope_name)
if_layer = graph.layers[list(graph.layers.keys())[-1]]
block = PaddleGraph(if_layer, graph_type="dygraph")
block = PaddleGraph(parent_layer=if_layer, graph_type="dygraph")
block.add_layer(
"prim.sub",
inputs={"y": inputs_name[0] + "_len"},
outputs=[inputs_name[0] + "_len0"],
scope_name=scope_name,
x=dim)
block.add_layer(
"prim.len2list",
inputs={"len": inputs_name[0] + "_len0"},
outputs=[inputs_name[0] + "_list"])
outputs=[inputs_name[0] + "_list"],
scope_name=scope_name)
block.add_layer(
"paddle.tensor.unsqueeze",
inputs={"x": inputs_name[0],
"axis": inputs_name[0] + "_list"},
outputs=[inputs_name[0] + "_var"])
outputs=[inputs_name[0] + "_var"],
scope_name=scope_name)
block.add_layer(
kernel,
inputs={"input": inputs_name[0] + "_var"},
outputs=layer_outputs,
scope_name=scope_name,
**layer_attrs)
block.add_layer(
"paddle.tensor.squeeze",
inputs={"x": output_name,
"axis": inputs_name[0] + "_list"},
outputs=[output_name])
outputs=[output_name],
scope_name=scope_name)
if_layer.add_block(block)
block = PaddleGraph(if_layer, graph_type="dygraph")
block = PaddleGraph(parent_layer=if_layer, graph_type="dygraph")
layer_inputs["input"] = inputs_name[0]
block.add_layer(
kernel, inputs=layer_inputs, outputs=layer_outputs, **layer_attrs)
kernel, inputs=layer_inputs, outputs=layer_outputs, scope_name=scope_name, **layer_attrs)
if_layer.add_block(block)
if_layer.inputs["input-0"] = inputs_name[0]
if_layer.inputs["input-1"] = inputs_name[0] + "_len"
if len(layer_attrs["padding"]) == 2:
add_pad_layers("paddle.nn.ConstantPad1d", 3)
add_pad_layers("paddle.nn.Pad1D", 3)
elif len(layer_attrs["padding"]) == 4:
add_pad_layers("paddle.nn.ConstantPad2d", 4)
add_pad_layers("paddle.nn.Pad2D", 4)
elif len(layer_attrs["padding"]) == 6:
add_pad_layers("paddle.nn.ConstantPad3d", 5)
add_pad_layers("paddle.nn.Pad3D", 5)
else:
raise Exception("The lenght of padding list must be 2, 4 or 6!")
return current_inputs, current_outputs
......@@ -727,6 +895,7 @@ def aten_contiguous(mapper, graph, node):
【注意】Paddle中无此用法,所以此处翻译成赋值。
"""
scope_name = mapper.normalize_scope_name(node)
output_name = mapper._get_outputs_name(node)[0]
layer_outputs = [output_name]
layer_inputs = {}
......@@ -735,12 +904,12 @@ def aten_contiguous(mapper, graph, node):
# 获取当前节点输出的list
current_outputs = [output_name]
# 处理输入0,即%4058
mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs)
mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs, scope_name)
layer_inputs["input"] = inputs_name[0]
# 获取当前节点输入的list
current_inputs = list(layer_inputs.values())
graph.add_layer("prim.equal", inputs=layer_inputs, outputs=layer_outputs)
graph.add_layer("prim.equal", inputs=layer_inputs, outputs=layer_outputs, scope_name=scope_name)
return current_inputs, current_outputs
......@@ -759,33 +928,30 @@ def aten_conv2d(mapper, graph, node):
%30 (int): 膨胀系数大小。
%26 (int): 卷积的组数。
"""
if "conv" in mapper.dygraph_name_id:
mapper.dygraph_name_id["conv"] += 1
else:
mapper.dygraph_name_id["conv"] = 0
conv2d_name = "conv" + str(mapper.dygraph_name_id["conv"])
scope_name = mapper.normalize_scope_name(node)
op_name = name_generator("conv2d", mapper.nn_name2id)
output_name = mapper._get_outputs_name(node)[0]
layer_outputs = [conv2d_name, output_name]
layer_outputs = [op_name, output_name]
layer_inputs = {}
layer_attrs = {}
inputs_name, inputs_node = mapper._get_inputs_name(node)
# 获取当前节点输出的list
current_outputs = [output_name]
# 处理输入0,即%input.8
mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs)
mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs, scope_name)
layer_inputs["input"] = inputs_name[0]
# 获取当前节点输入的list
current_inputs = list(layer_inputs.values())
# 处理输入1,即%25
weights = mapper.pytorch_params[inputs_name[1]]
mapper.paddle_params[conv2d_name + ".weight"] = weights
mapper.paddle_params[op_name + ".weight"] = weights
layer_attrs["out_channels"] = weights.shape[0]
layer_attrs["kernel_size"] = weights.shape[2:]
# 处理输入2,即%27
if inputs_name[2] in mapper.pytorch_params:
bias = mapper.pytorch_params[inputs_name[2]]
if bias is not None:
mapper.paddle_params[conv2d_name + ".bias"] = bias
mapper.paddle_params[op_name + ".bias"] = bias
else:
layer_attrs["bias_attr"] = False
else:
......@@ -801,9 +967,10 @@ def aten_conv2d(mapper, graph, node):
layer_attrs['in_channels'] = weights.shape[1] * mapper.attrs[inputs_name[6]]
graph.add_layer(
"paddle.nn.Conv2d",
"paddle.nn.Conv2D",
inputs=layer_inputs,
outputs=layer_outputs,
scope_name=scope_name,
**layer_attrs)
return current_inputs, current_outputs
......@@ -812,64 +979,75 @@ def aten__convolution(mapper, graph, node):
""" 构造conv2d的PaddleLayer。
TorchScript示例:
%input.10 : Tensor = aten::_convolution(%input.8, %25, %27, %28, %29, %30, %26)
%input.10 : Tensor = aten::_convolution(%input.1, %18, %10, %19, %20, %21, %13, %22, %12, %13, %13, %15)
参数含义:
%input.10 (Tensor): 输出,卷积后的结果。
%input.8 (Tensor): 需要进行卷积的特征层。
%25 (Tensor): weights。
%27 (Tensor): bias。
%28 (int): 步长大小。
%29 (int): 填充大小。
%30 (int): 膨胀系数大小。
%26 (int): 卷积的组数。
%18 (Tensor): weights。
%10 (Tensor): bias。
%19 (list): 步长大小。
%20 (list): 填充大小。
%21 (list): 膨胀系数大小。
%13 (bool): 是否进行转置卷积。
%22 (list): 输出形状上一侧额外添加的大小。
%12 (int): 卷积的组数。
"""
if "conv" in mapper.dygraph_name_id:
mapper.dygraph_name_id["conv"] += 1
else:
mapper.dygraph_name_id["conv"] = 0
conv2d_name = "conv" + str(mapper.dygraph_name_id["conv"])
scope_name = mapper.normalize_scope_name(node)
op_name = name_generator("conv2d", mapper.nn_name2id)
output_name = mapper._get_outputs_name(node)[0]
layer_outputs = [conv2d_name, output_name]
layer_outputs = [op_name, output_name]
layer_inputs = {}
layer_attrs = {}
inputs_name, inputs_node = mapper._get_inputs_name(node)
# 获取当前节点输出的list
current_outputs = [output_name]
# 处理输入0,即%input.8
mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs)
mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs, scope_name)
layer_inputs["input"] = inputs_name[0]
# 获取当前节点输入的list
current_inputs = list(layer_inputs.values())
# 处理输入1,即%25
# 处理输入1,即%18
weights = mapper.pytorch_params[inputs_name[1]]
mapper.paddle_params[conv2d_name + ".weight"] = weights
layer_attrs["num_filters"] = weights.shape[0]
layer_attrs["filter_size"] = weights.shape[2:]
# 处理输入2,即%27
mapper.paddle_params[op_name + ".weight"] = weights
layer_attrs["out_channels"] = weights.shape[0]
layer_attrs["kernel_size"] = weights.shape[2:]
# 处理输入2,即%10
if inputs_name[2] in mapper.pytorch_params:
bias = mapper.pytorch_params[inputs_name[2]]
if bias is not None:
mapper.paddle_params[conv2d_name + ".bias"] = bias
mapper.paddle_params[op_name + ".bias"] = bias
else:
layer_attrs["bias_attr"] = False
else:
layer_attrs["bias_attr"] = False
# 处理输入3,即%28
# 处理输入3,即%19
layer_attrs["stride"] = mapper.attrs[inputs_name[3]]
# 处理输入4,即%29
# 处理输入4,即%20
layer_attrs["padding"] = mapper.attrs[inputs_name[4]]
# 处理输入5,即%30
# 处理输入5,即%21
layer_attrs["dilation"] = mapper.attrs[inputs_name[5]]
# 处理输入6,即%26
layer_attrs["groups"] = mapper.attrs[inputs_name[6]]
layer_attrs['num_channels'] = weights.shape[1] * mapper.attrs[inputs_name[
6]]
graph.add_layer(
"paddle.nn.Conv2D",
inputs=layer_inputs,
outputs=layer_outputs,
**layer_attrs)
# 处理输入6,即%13
if mapper.attrs[inputs_name[6]]:
# 处理输入7,即%22
layer_attrs["output_padding"] = mapper.attrs[inputs_name[7]]
# 处理输入8,即%12
layer_attrs["groups"] = mapper.attrs[inputs_name[8]]
layer_attrs['in_channels'] = weights.shape[1] * mapper.attrs[inputs_name[
8]]
if mapper.attrs[inputs_name[6]]:
graph.add_layer(
"paddle.nn.Conv2DTranspose",
inputs=layer_inputs,
outputs=layer_outputs,
scope_name=scope_name,
**layer_attrs)
else:
graph.add_layer(
"paddle.nn.Conv2D",
inputs=layer_inputs,
outputs=layer_outputs,
scope_name=scope_name,
**layer_attrs)
return current_inputs, current_outputs
......@@ -882,6 +1060,7 @@ def aten_cos(mapper, graph, node):
%94 (Tensor): 输出,cos之后的结果。
%sinusoid_inp.1 (Tensor): 需要进行shape的Tensor。
"""
scope_name = mapper.normalize_scope_name(node)
output_name = mapper._get_outputs_name(node)[0]
layer_outputs = [output_name]
layer_inputs = {}
......@@ -889,12 +1068,12 @@ def aten_cos(mapper, graph, node):
# 获取当前节点输出的list
current_outputs = [output_name]
# 处理输入0,即%sinusoid_inp.1
mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs)
mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs, scope_name)
layer_inputs["x"] = inputs_name[0]
# 获取当前节点输入、输出的list
current_inputs = list(layer_inputs.values())
graph.add_layer("paddle.cos", inputs=layer_inputs, outputs=layer_outputs)
graph.add_layer("paddle.cos", inputs=layer_inputs, outputs=layer_outputs, scope_name=scope_name)
return current_inputs, current_outputs
......@@ -909,6 +1088,7 @@ def aten_cumsum(mapper, graph, node):
%46 (int): 累加的维度。
%48 (int/None): Tensor的类型。
"""
scope_name = mapper.normalize_scope_name(node)
output_name = mapper._get_outputs_name(node)[0]
layer_outputs = [output_name]
layer_inputs = {}
......@@ -917,7 +1097,7 @@ def aten_cumsum(mapper, graph, node):
# 获取当前节点输出的list
current_outputs = [output_name]
# 处理输入0,即%mask.1
mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs)
mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs, scope_name)
layer_inputs["x"] = inputs_name[0]
# 获取当前节点输入、输出的list
current_inputs = list(layer_inputs.values())
......@@ -926,7 +1106,7 @@ def aten_cumsum(mapper, graph, node):
layer_attrs["axis"] = mapper.attrs[inputs_name[1]]
else:
mapper._check_input(graph, inputs_node[1], inputs_name[1],
current_outputs)
current_outputs, scope_name)
layer_inputs["axis"] = inputs_name[1]
current_inputs.append(inputs_name[1])
# 处理输入1,即%48,代表dtype
......@@ -939,6 +1119,7 @@ def aten_cumsum(mapper, graph, node):
"paddle.cumsum",
inputs=layer_inputs,
outputs=layer_outputs,
scope_name=scope_name,
**layer_attrs)
return current_inputs, current_outputs
......@@ -954,6 +1135,7 @@ def aten_detach(mapper, graph, node):
【注意】由于Paddle无此操作,所以此处制转换为赋值。
"""
scope_name = mapper.normalize_scope_name(node)
output_name = mapper._get_outputs_name(node)[0]
layer_outputs = [output_name]
layer_inputs = {}
......@@ -962,11 +1144,11 @@ def aten_detach(mapper, graph, node):
# 获取当前节点输出的list
current_outputs = [output_name]
# 处理输入0,即%end.1
mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs)
mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs, scope_name)
layer_inputs["input"] = inputs_name[0]
# 获取当前节点输入的list
current_inputs = list(layer_inputs.values())
graph.add_layer("prim.equal", inputs=layer_inputs, outputs=layer_outputs)
graph.add_layer("prim.equal", inputs=layer_inputs, outputs=layer_outputs, scope_name=scope_name)
return current_inputs, current_outputs
......@@ -979,6 +1161,7 @@ def aten_dict(mapper, graph, node):
参数含义:
%features.1: 输出,初始化的dict。
"""
scope_name = mapper.normalize_scope_name(node)
output_name = mapper._get_outputs_name(node)[0]
layer_outputs = [output_name]
layer_inputs = {}
......@@ -986,7 +1169,7 @@ def aten_dict(mapper, graph, node):
# 获取当前节点输出的list
current_outputs = [output_name]
graph.add_layer("prim.dict", inputs=layer_inputs, outputs=layer_outputs)
graph.add_layer("prim.dict", inputs=layer_inputs, outputs=layer_outputs, scope_name=scope_name)
return current_inputs, current_outputs
......@@ -999,6 +1182,7 @@ def aten_dim(mapper, graph, node):
%106 (int): 输出,Tensor的维度。
%101 (Tensor): 输入的Tensor。
"""
scope_name = mapper.normalize_scope_name(node)
output_name = mapper._get_outputs_name(node)[0]
layer_outputs = [output_name]
layer_inputs = {}
......@@ -1006,15 +1190,15 @@ def aten_dim(mapper, graph, node):
# 获取当前节点输出的list
current_outputs = [output_name]
# 处理输入0,即%input.8
mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs)
mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs, scope_name)
layer_inputs["input"] = inputs_name[0]
# 获取当前节点输入的list
current_inputs = list(layer_inputs.values())
graph.add_layer(
"fluid.layers.shape", inputs=layer_inputs, outputs=layer_outputs)
"prim.shape", inputs=layer_inputs, outputs=layer_outputs, scope_name=scope_name)
graph.add_layer(
"prim.len", inputs={"input": output_name}, outputs=layer_outputs)
"prim.len", inputs={"input": output_name}, outputs=layer_outputs, scope_name=scope_name)
return current_inputs, current_outputs
......@@ -1035,15 +1219,15 @@ def aten_div_(mapper, graph, node):
# 获取当前节点输出的list
current_outputs = [output_name]
# 处理输入0,即%124
mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs)
mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs, scope_name)
layer_inputs["x"] = inputs_name[0]
# 处理输入1,即%123
mapper._check_input(graph, inputs_node[1], inputs_name[1], current_outputs)
mapper._check_input(graph, inputs_node[1], inputs_name[1], current_outputs, scope_name)
layer_inputs["y"] = inputs_name[1]
# 获取当前节点输入的list
current_inputs = list(layer_inputs.values())
graph.add_layer("prim.div", inputs=layer_inputs, outputs=layer_outputs)
graph.add_layer("prim.div", inputs=layer_inputs, outputs=layer_outputs, scope_name=scope_name)
return current_inputs, current_outputs
......@@ -1057,6 +1241,7 @@ def aten_div(mapper, graph, node):
%bx_bw.3 (-): 被除数。
%2678 (int): 除数。
"""
scope_name = mapper.normalize_scope_name(node)
output_name = mapper._get_outputs_name(node)[0]
layer_outputs = [output_name]
layer_inputs = {}
......@@ -1064,15 +1249,15 @@ def aten_div(mapper, graph, node):
# 获取当前节点输出的list
current_outputs = [output_name]
# 处理输入0,即%124
mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs)
mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs, scope_name)
layer_inputs["x"] = inputs_name[0]
# 处理输入1,即%123
mapper._check_input(graph, inputs_node[1], inputs_name[1], current_outputs)
mapper._check_input(graph, inputs_node[1], inputs_name[1], current_outputs, scope_name)
layer_inputs["y"] = inputs_name[1]
# 获取当前节点输入的list
current_inputs = list(layer_inputs.values())
graph.add_layer("prim.div", inputs=layer_inputs, outputs=layer_outputs)
graph.add_layer("prim.div", inputs=layer_inputs, outputs=layer_outputs, scope_name=scope_name)
return current_inputs, current_outputs
......@@ -1086,25 +1271,22 @@ def aten_dropout(mapper, graph, node):
%result.3 (Tensor): 输入Tensor。
%118 (bool): 是否是训练阶段。
"""
if "dropout" in mapper.dygraph_name_id:
mapper.dygraph_name_id["dropout"] += 1
else:
mapper.dygraph_name_id["dropout"] = 0
dropout_name = "dropout" + str(mapper.dygraph_name_id["dropout"])
scope_name = mapper.normalize_scope_name(node)
op_name = name_generator("dropout", mapper.nn_name2id)
output_name = mapper._get_outputs_name(node)[0]
layer_outputs = [dropout_name, output_name]
layer_outputs = [op_name, output_name]
layer_inputs = {}
inputs_name, inputs_node = mapper._get_inputs_name(node)
# 获取当前节点输出的list
current_outputs = [output_name]
# 处理输入0,即%119
mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs)
mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs, scope_name)
layer_inputs["input"] = inputs_name[0]
# 获取当前节点输入、输出的list
current_inputs = list(layer_inputs.values())
graph.add_layer(
"paddle.nn.Dropout", inputs=layer_inputs, outputs=layer_outputs, p=0.0)
"paddle.nn.Dropout", inputs=layer_inputs, outputs=layer_outputs, scope_name=scope_name, p=0.0)
return current_inputs, current_outputs
......@@ -1118,25 +1300,22 @@ def aten_dropout_(mapper, graph, node):
%result.3 (Tensor): 输入Tensor。
%118 (bool): 是否是训练阶段。
"""
if "dropout" in mapper.dygraph_name_id:
mapper.dygraph_name_id["dropout"] += 1
else:
mapper.dygraph_name_id["dropout"] = 0
dropout_name = "dropout" + str(mapper.dygraph_name_id["dropout"])
scope_name = mapper.normalize_scope_name(node)
op_name = name_generator("dropout", mapper.nn_name2id)
output_name = mapper._get_outputs_name(node)[0]
layer_outputs = [dropout_name, output_name]
layer_outputs = [op_name, output_name]
layer_inputs = {}
inputs_name, inputs_node = mapper._get_inputs_name(node)
# 获取当前节点输出的list
current_outputs = [output_name]
# 处理输入0,即%119
mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs)
mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs, scope_name)
layer_inputs["input"] = inputs_name[0]
# 获取当前节点输入、输出的list
current_inputs = list(layer_inputs.values())
graph.add_layer(
"paddle.nn.Dropout", inputs=layer_inputs, outputs=layer_outputs, p=0.0)
"paddle.nn.Dropout", inputs=layer_inputs, outputs=layer_outputs, scope_name=scope_name, p=0.0)
return current_inputs, current_outputs
......@@ -1153,13 +1332,10 @@ def aten_embedding(mapper, graph, node):
%46 (bool): scale_grad_by_freq。
%46 (bool): sparse。
"""
if "embedding" in mapper.dygraph_name_id:
mapper.dygraph_name_id["embedding"] += 1
else:
mapper.dygraph_name_id["embedding"] = 0
embedding_name = "embedding" + str(mapper.dygraph_name_id["embedding"])
scope_name = mapper.normalize_scope_name(node)
op_name = name_generator("embedding", mapper.nn_name2id)
output_name = mapper._get_outputs_name(node)[0]
layer_outputs = [embedding_name, output_name]
layer_outputs = [op_name, output_name]
layer_inputs = {}
layer_attrs = {}
inputs_name, inputs_node = mapper._get_inputs_name(node)
......@@ -1167,12 +1343,11 @@ def aten_embedding(mapper, graph, node):
current_outputs = [output_name]
# 处理输入0,即%57
weights = mapper.pytorch_params[inputs_name[0]]
mapper.paddle_params[embedding_name + ".weight"] = weights
# layer_attrs["num_embeddings"] = weights.shape[0]
# layer_attrs["embedding_dim"] = weights.shape[1]
layer_attrs["size"] = weights.shape
mapper.paddle_params[op_name + ".weight"] = weights
layer_attrs["num_embeddings"] = weights.shape[0]
layer_attrs["embedding_dim"] = weights.shape[1]
# 处理输入1,即%input_ids.1
mapper._check_input(graph, inputs_node[1], inputs_name[1], current_outputs)
mapper._check_input(graph, inputs_node[1], inputs_name[1], current_outputs, scope_name)
layer_inputs["input"] = inputs_name[1]
# 获取当前节点输入的list
current_inputs = list(layer_inputs.values())
......@@ -1182,13 +1357,13 @@ def aten_embedding(mapper, graph, node):
else:
layer_attrs["padding_idx"] = mapper.attrs[inputs_name[2]]
# 处理输入4,即%46
# layer_attrs["sparse"] = mapper.attrs[inputs_name[4]]
layer_attrs["is_sparse"] = mapper.attrs[inputs_name[4]]
layer_attrs["sparse"] = mapper.attrs[inputs_name[4]]
graph.add_layer(
"paddle.fluid.dygraph.Embedding",
"paddle.nn.Embedding",
inputs=layer_inputs,
outputs=layer_outputs,
scope_name=scope_name,
**layer_attrs)
return current_inputs, current_outputs
......@@ -1203,6 +1378,7 @@ def aten_eq(mapper, graph, node):
%124 (-): 需对比的输入1。
%123 (-): 需对比的输入2。
"""
scope_name = mapper.normalize_scope_name(node)
output_name = mapper._get_outputs_name(node)[0]
layer_outputs = [output_name]
layer_inputs = {}
......@@ -1210,18 +1386,18 @@ def aten_eq(mapper, graph, node):
# 获取当前节点输出的list
current_outputs = [output_name]
# 处理输入0,即%124
mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs)
mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs, scope_name)
layer_inputs["x"] = inputs_name[0]
x_value = list(node.inputs())[0]
x_type = x_value.type()
# 处理输入1,即%123
mapper._check_input(graph, inputs_node[1], inputs_name[1], current_outputs)
mapper._check_input(graph, inputs_node[1], inputs_name[1], current_outputs, scope_name)
layer_inputs["y"] = inputs_name[1]
y_value = list(node.inputs())[1]
y_type = y_value.type()
# 获取当前节点输入的list
current_inputs = list(layer_inputs.values())
graph.add_layer("prim.eq", inputs=layer_inputs, outputs=layer_outputs)
graph.add_layer("prim.eq", inputs=layer_inputs, outputs=layer_outputs, scope_name=scope_name)
return current_inputs, current_outputs
......@@ -1234,6 +1410,7 @@ def aten_exp(mapper, graph, node):
%55 (Tensor): 输出,运算后的结果。
%54 (Tensor): 需要指数运算的Tensor。
"""
scope_name = mapper.normalize_scope_name(node)
output_name = mapper._get_outputs_name(node)[0]
layer_outputs = [output_name]
layer_inputs = {}
......@@ -1241,13 +1418,13 @@ def aten_exp(mapper, graph, node):
# 获取当前节点输出的list
current_outputs = [output_name]
# 处理输入0,即%result.5
mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs)
mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs, scope_name)
layer_inputs["x"] = inputs_name[0]
# 获取当前节点输入、输出的list
current_inputs = list(layer_inputs.values())
graph.add_layer(
"fluid.layers.exp", inputs=layer_inputs, outputs=layer_outputs)
"paddle.exp", inputs=layer_inputs, outputs=layer_outputs, scope_name=scope_name)
return current_inputs, current_outputs
......@@ -1262,6 +1439,7 @@ def aten_expand(mapper, graph, node):
%1888 (int): 广播的维度。
%1567 (bool): 未使用。
"""
scope_name = mapper.normalize_scope_name(node)
output_name = mapper._get_outputs_name(node)[0]
layer_outputs = [output_name]
layer_inputs = {}
......@@ -1269,66 +1447,72 @@ def aten_expand(mapper, graph, node):
# 获取当前节点输出的list
current_outputs = [output_name]
# 处理输入0,即%1875
mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs)
mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs, scope_name)
layer_inputs["x"] = inputs_name[0]
# 处理输入1,即%1888
mapper._check_input(graph, inputs_node[1], inputs_name[1], current_outputs)
mapper._check_input(graph, inputs_node[1], inputs_name[1], current_outputs, scope_name)
graph.add_layer(
"prim.type",
inputs={"input": inputs_name[0]},
outputs=[inputs_name[0] + "_type"])
outputs=[inputs_name[0] + "_type"],
scope_name=scope_name)
graph.add_layer(
"prim.str",
inputs={"input": inputs_name[0] + "_type"},
outputs=[inputs_name[0] + "_type"])
outputs=[inputs_name[0] + "_type"],
scope_name=scope_name)
graph.add_layer(
"prim.eq",
inputs={"x": inputs_name[0] + "_type"},
outputs=[inputs_name[0] + "_cond"],
scope_name=scope_name,
y=string("VarType.BOOL"))
graph.add_layer(
"prim.if", {'input': inputs_name[0] + "_cond"},
outputs=[inputs_name[0] + "_if1", inputs_name[1] + "_var"])
outputs=[inputs_name[0] + "_if1", inputs_name[1] + "_var"],
scope_name=scope_name)
if_layer = graph.layers[list(graph.layers.keys())[-1]]
block = PaddleGraph(if_layer, graph_type="dygraph")
block = PaddleGraph(parent_layer=if_layer, graph_type="dygraph")
block.add_layer(
"fluid.layers.cast",
"paddle.cast",
inputs={"x": inputs_name[0]},
outputs=[inputs_name[0]],
scope_name=scope_name,
dtype=string("int64"))
block.add_layer(
"fluid.layers.create_global_var",
"self.create_parameter",
inputs={"shape": inputs_name[1]},
outputs=[inputs_name[1] + "_var"],
value=1.0,
scope_name=scope_name,
dtype=string("int64"),
persistable=True)
default_initializer="paddle.nn.initializer.Constant(value=0.0)")
if_layer.add_block(block)
block = PaddleGraph(if_layer, graph_type="dygraph")
block = PaddleGraph(parent_layer=if_layer, graph_type="dygraph")
block.add_layer(
"prim.type",
inputs={"input": inputs_name[0]},
outputs=[inputs_name[0] + "_type"])
outputs=[inputs_name[0] + "_type"],
scope_name=scope_name)
block.add_layer(
"fluid.layers.create_global_var",
"self.create_parameter",
inputs={"shape": inputs_name[1]},
outputs=[inputs_name[1] + "_var"],
value=1.0,
scope_name=scope_name,
dtype=inputs_name[0] + "_type",
persistable=True)
default_initializer="paddle.nn.initializer.Constant(value=0.0)")
if_layer.add_block(block)
if_layer.inputs["input-0"] = inputs_name[0]
if_layer.inputs["input-1"] = inputs_name[1]
layer_inputs["target_tensor"] = inputs_name[1] + "_var"
layer_inputs["y"] = inputs_name[1] + "_var"
current_outputs.append(inputs_name[1] + "_var")
# 获取当前节点输入的list
current_inputs = list(layer_inputs.values())
current_inputs.append(inputs_name[1])
graph.add_layer(
"fluid.layers.expand_as", inputs=layer_inputs, outputs=layer_outputs)
"paddle.expand_as", inputs=layer_inputs, outputs=layer_outputs, scope_name=scope_name)
return current_inputs, current_outputs
......@@ -1342,6 +1526,7 @@ def aten_expand_as(mapper, graph, node):
%1875 (Tensor): 需要广播的Tensor。
%1888 (Tensor): 广播的示例。
"""
scope_name = mapper.normalize_scope_name(node)
output_name = mapper._get_outputs_name(node)[0]
layer_outputs = [output_name]
layer_inputs = {}
......@@ -1349,62 +1534,81 @@ def aten_expand_as(mapper, graph, node):
# 获取当前节点输出的list
current_outputs = [output_name]
# 处理输入0,即%1875
mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs)
mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs, scope_name)
layer_inputs["x"] = inputs_name[0]
# 处理输入1,即%1888
mapper._check_input(graph, inputs_node[1], inputs_name[1], current_outputs)
layer_inputs["target_tensor"] = inputs_name[1]
mapper._check_input(graph, inputs_node[1], inputs_name[1], current_outputs, scope_name)
layer_inputs["y"] = inputs_name[1]
# 获取当前节点输入的list
current_inputs = list(layer_inputs.values())
graph.add_layer(
"prim.type",
inputs={"input": inputs_name[0]},
outputs=[inputs_name[0] + "_type"])
outputs=[inputs_name[0] + "_type"],
scope_name=scope_name)
graph.add_layer(
"prim.str",
inputs={"input": inputs_name[0] + "_type"},
outputs=[inputs_name[0] + "_type"])
outputs=[inputs_name[0] + "_type"],
scope_name=scope_name)
graph.add_layer(
"prim.eq",
inputs={"x": inputs_name[0] + "_type"},
outputs=[inputs_name[0] + "_cond"],
scope_name=scope_name,
y=string("VarType.BOOL"))
graph.add_layer(
"prim.if", {'input': inputs_name[0] + "_cond"},
outputs=[inputs_name[0] + "_if1"])
outputs=[inputs_name[0] + "_if1"],
scope_name=scope_name)
if_layer = graph.layers[list(graph.layers.keys())[-1]]
block = PaddleGraph(if_layer, graph_type="dygraph")
block = PaddleGraph(parent_layer=if_layer, graph_type="dygraph")
block.add_layer(
"prim.type",
inputs={"input": inputs_name[1]},
outputs=[inputs_name[1] + "_type"])
outputs=[inputs_name[1] + "_type"],
scope_name=scope_name)
block.add_layer(
"fluid.layers.cast",
inputs={"x": inputs_name[0]},
outputs=[inputs_name[0]],
scope_name=scope_name,
dtype=inputs_name[1] + "_type")
if_layer.add_block(block)
block = PaddleGraph(if_layer, graph_type="dygraph")
block = PaddleGraph(parent_layer=if_layer, graph_type="dygraph")
if_layer.add_block(block)
if_layer.inputs["input-0"] = inputs_name[0]
if_layer.inputs["input-1"] = inputs_name[1]
graph.add_layer(
"fluid.layers.expand_as", inputs=layer_inputs, outputs=layer_outputs)
"paddle.expand_as", inputs=layer_inputs, outputs=layer_outputs, scope_name=scope_name)
graph.add_layer(
"prim.if", {'input': inputs_name[0] + "_cond"},
outputs=[inputs_name[0] + "_if2"])
outputs=[inputs_name[0] + "_if2"],
scope_name=scope_name)
if_layer = graph.layers[list(graph.layers.keys())[-1]]
block = PaddleGraph(if_layer, graph_type="dygraph")
block = PaddleGraph(parent_layer=if_layer, graph_type="dygraph")
block.add_layer(
"fluid.layers.cast",
inputs={"x": layer_outputs[0]},
outputs=layer_outputs,
outputs=copy.deepcopy(layer_outputs),
scope_name=scope_name,
dtype=string("bool"))
if_layer.add_block(block)
block = PaddleGraph(if_layer, graph_type="dygraph")
if_layer.add_block(block)
if_layer.inputs["input-0"] = layer_outputs[0]
# TODO(syf): check expand_as
# # 处理输入0,即%1875
# mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs, scope_name)
# layer_inputs["x"] = inputs_name[0]
# # 处理输入1,即%1888
# mapper._check_input(graph, inputs_node[1], inputs_name[1], current_outputs, scope_name)
# layer_inputs["y"] = inputs_name[1]
# # 获取当前节点输入的list
# current_inputs = list(layer_inputs.values())
# graph.add_layer(
# "paddle.expand_as", inputs=layer_inputs, outputs=layer_outputs, scope_name=scope_name)
return current_inputs, current_outputs
......@@ -1422,6 +1626,7 @@ def aten_eye(mapper, graph, node):
%67 (str): 设备。
%7 (bool): 是否计算梯度。
"""
scope_name = mapper.normalize_scope_name(node)
output_name = mapper._get_outputs_name(node)[0]
layer_outputs = [output_name]
layer_inputs = {}
......@@ -1430,12 +1635,12 @@ def aten_eye(mapper, graph, node):
# 获取当前节点输出的list
current_outputs = [output_name]
# 处理输入0,即%49
mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs)
mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs, scope_name)
layer_inputs["num_rows"] = inputs_name[0]
if len(inputs_name) > 5:
# 处理输入1,即%_50
mapper._check_input(graph, inputs_node[1], inputs_name[1],
current_outputs)
current_outputs, scope_name)
layer_inputs["num_columns"] = inputs_name[1]
# 获取当前节点输入的list
current_inputs = list(layer_inputs.values())
......@@ -1443,12 +1648,41 @@ def aten_eye(mapper, graph, node):
layer_attrs["dtype"] = dtype_dict[mapper.attrs[inputs_name[-4]]]
graph.add_layer(
"fluid.layers.eye",
"paddle.eye",
inputs=layer_inputs,
outputs=layer_outputs,
scope_name=scope_name,
**layer_attrs)
return current_inputs, current_outputs
def aten_feature_dropout(mapper, graph, node):
""" 构造Dropout的PaddleLayer。
TorchScript示例:
%119 : Tensor = aten::feature_dropout(%result.3, %117, %118)
参数含义:
%119 (Tensor): Dropout后的Tensor。
%result.3 (Tensor): 输入Tensor。
%118 (bool): 是否是训练阶段。
"""
scope_name = mapper.normalize_scope_name(node)
op_name = name_generator("dropout", mapper.nn_name2id)
output_name = mapper._get_outputs_name(node)[0]
layer_outputs = [op_name, output_name]
layer_inputs = {}
inputs_name, inputs_node = mapper._get_inputs_name(node)
# 获取当前节点输出的list
current_outputs = [output_name]
# 处理输入0,即%119
mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs, scope_name)
layer_inputs["input"] = inputs_name[0]
# 获取当前节点输入、输出的list
current_inputs = list(layer_inputs.values())
graph.add_layer(
"paddle.nn.Dropout", inputs=layer_inputs, outputs=layer_outputs, scope_name=scope_name, p=0.0)
return current_inputs, current_outputs
def aten_flatten(mapper, graph, node):
""" 构造flatten的PaddleLayer。
......@@ -1461,41 +1695,31 @@ def aten_flatten(mapper, graph, node):
%4 (int): flatten的开始维度。
%2 (int): flatten的结束维度。
注意:目前flatten只支持第一维的flatten
"""
scope_name = mapper.normalize_scope_name(node)
output_name = mapper._get_outputs_name(node)[0]
layer_outputs = [output_name]
layer_inputs = {}
layer_attrs = {}
inputs_name, inputs_node = mapper._get_inputs_name(node)
# 获取当前节点输出的list
current_outputs = [output_name]
# 处理输入1,即%4
graph.add_layer(
"prim.assert",
inputs={},
outputs=[inputs_name[1] + "_assert"],
type='eq',
key=mapper.attrs[inputs_name[1]],
value=1)
# 处理输入2,即%2
graph.add_layer(
"prim.assert",
inputs={},
outputs=[inputs_name[2] + "_assert"],
type='eq',
key=mapper.attrs[inputs_name[2]],
value=-1)
# 处理输入0,即%x
mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs)
mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs, scope_name)
# 处理输入1,即%4
layer_attrs["start_axis"] = mapper.attrs[inputs_name[1]]
# 处理输入2,即%20
layer_attrs["stop_axis"] = mapper.attrs[inputs_name[2]]
layer_inputs["x"] = inputs_name[0]
# 获取当前节点输入的list
current_inputs = list(layer_inputs.values())
graph.add_layer(
"fluid.layers.flatten",
"paddle.flatten",
inputs=layer_inputs,
outputs=layer_outputs,
axis=1)
scope_name=scope_name,
**layer_attrs)
return current_inputs, current_outputs
......@@ -1508,6 +1732,7 @@ def aten_Float(mapper, graph, node):
%3992 (int): 向上取整后的整数。
%3991 (float): 需要取整的浮点数。
"""
scope_name = mapper.normalize_scope_name(node)
output_name = mapper._get_outputs_name(node)[0]
layer_outputs = [output_name]
layer_inputs = {}
......@@ -1515,12 +1740,12 @@ def aten_Float(mapper, graph, node):
# 获取当前节点输出的list
current_outputs = [output_name]
# 处理输入0,即%3991
mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs)
mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs, scope_name)
layer_inputs["input"] = inputs_name[0]
# 获取当前节点输入的list
current_inputs = list(layer_inputs.values())
graph.add_layer("prim.float", inputs=layer_inputs, outputs=layer_outputs)
graph.add_layer("prim.float", inputs=layer_inputs, outputs=layer_outputs, scope_name=scope_name)
return current_inputs, current_outputs
......@@ -1533,6 +1758,7 @@ def aten_floor(mapper, graph, node):
%3978 (int): 向上取整后的整数。
%scale.18 (float): 需要取整的浮点数。
"""
scope_name = mapper.normalize_scope_name(node)
output_name = mapper._get_outputs_name(node)[0]
layer_outputs = [output_name]
layer_inputs = {}
......@@ -1540,12 +1766,12 @@ def aten_floor(mapper, graph, node):
# 获取当前节点输出的list
current_outputs = [output_name]
# 处理输入0,即%scale.18
mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs)
mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs, scope_name)
layer_inputs["input"] = inputs_name[0]
# 获取当前节点输入的list
current_inputs = list(layer_inputs.values())
graph.add_layer("prim.floor", inputs=layer_inputs, outputs=layer_outputs)
graph.add_layer("prim.floor", inputs=layer_inputs, outputs=layer_outputs, scope_name=scope_name)
return current_inputs, current_outputs
......@@ -1559,6 +1785,7 @@ def aten_floordiv(mapper, graph, node):
%num_channels.2 (-): 被除数。
%2 (int): 除数。
"""
scope_name = mapper.normalize_scope_name(node)
output_name = mapper._get_outputs_name(node)[0]
layer_outputs = [output_name]
layer_inputs = {}
......@@ -1566,15 +1793,15 @@ def aten_floordiv(mapper, graph, node):
# 获取当前节点输出的list
current_outputs = [output_name]
# 处理输入0,即%124
mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs)
mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs, scope_name)
layer_inputs["x"] = inputs_name[0]
# 处理输入1,即%123
mapper._check_input(graph, inputs_node[1], inputs_name[1], current_outputs)
mapper._check_input(graph, inputs_node[1], inputs_name[1], current_outputs, scope_name)
layer_inputs["y"] = inputs_name[1]
# 获取当前节点输入的list
current_inputs = list(layer_inputs.values())
graph.add_layer("prim.floordiv", inputs=layer_inputs, outputs=layer_outputs)
graph.add_layer("prim.floordiv", inputs=layer_inputs, outputs=layer_outputs, scope_name=scope_name)
return current_inputs, current_outputs
......@@ -1588,6 +1815,7 @@ def aten_floor_divide(mapper, graph, node):
%num_channels.2 (-): 被除数。
%2 (int): 除数。
"""
scope_name = mapper.normalize_scope_name(node)
output_name = mapper._get_outputs_name(node)[0]
layer_outputs = [output_name]
layer_inputs = {}
......@@ -1595,15 +1823,15 @@ def aten_floor_divide(mapper, graph, node):
# 获取当前节点输出的list
current_outputs = [output_name]
# 处理输入0,即%124
mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs)
mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs, scope_name)
layer_inputs["x"] = inputs_name[0]
# 处理输入1,即%123
mapper._check_input(graph, inputs_node[1], inputs_name[1], current_outputs)
mapper._check_input(graph, inputs_node[1], inputs_name[1], current_outputs, scope_name)
layer_inputs["y"] = inputs_name[1]
# 获取当前节点输入的list
current_inputs = list(layer_inputs.values())
graph.add_layer("prim.floordiv", inputs=layer_inputs, outputs=layer_outputs)
graph.add_layer("prim.floordiv", inputs=layer_inputs, outputs=layer_outputs, scope_name=scope_name)
return current_inputs, current_outputs
......@@ -1622,6 +1850,7 @@ def aten_full_like(mapper, graph, node):
%65 (bool): 是否计算梯度。
%66 (int): 内存形式。
"""
scope_name = mapper.normalize_scope_name(node)
output_name = mapper._get_outputs_name(node)[0]
layer_outputs = [output_name]
layer_inputs = {}
......@@ -1630,7 +1859,7 @@ def aten_full_like(mapper, graph, node):
# 获取当前节点输出的list
current_outputs = [output_name]
# 处理输入0,即%val_if_large.3
mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs)
mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs, scope_name)
layer_inputs["x"] = inputs_name[0]
# 获取当前节点输入的list
current_inputs = list(layer_inputs.values())
......@@ -1639,7 +1868,7 @@ def aten_full_like(mapper, graph, node):
layer_attrs["fill_value"] = mapper.attrs[inputs_name[1]]
else:
mapper._check_input(graph, inputs_node[1], inputs_name[1],
current_outputs)
current_outputs, scope_name)
layer_inputs["fill_value"] = inputs_name[1]
current_inputs.append(inputs_name[1])
# 处理输入2,即%50,代表dtype
......@@ -1649,6 +1878,7 @@ def aten_full_like(mapper, graph, node):
"paddle.full_like",
inputs=layer_inputs,
outputs=layer_outputs,
scope_name=scope_name,
**layer_attrs)
return current_inputs, current_outputs
......@@ -1664,25 +1894,22 @@ def aten_gelu(mapper, graph, node):
注意: inplace这个参数在paddle中未实现
"""
if "gelu" in mapper.dygraph_name_id:
mapper.dygraph_name_id["gelu"] += 1
else:
mapper.dygraph_name_id["gelu"] = 0
gelu_name = "gelu" + str(mapper.dygraph_name_id["gelu"])
scope_name = mapper.normalize_scope_name(node)
op_name = name_generator("gelu", mapper.nn_name2id)
output_name = mapper._get_outputs_name(node)[0]
layer_outputs = [gelu_name, output_name]
layer_outputs = [op_name, output_name]
layer_inputs = {}
inputs_name, inputs_node = mapper._get_inputs_name(node)
# 获取当前节点输出的list
current_outputs = [output_name]
# 处理输入0,即%result.5
mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs)
mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs, scope_name)
layer_inputs["x"] = inputs_name[0]
# 获取当前节点输入的list
current_inputs = list(layer_inputs.values())
graph.add_layer(
"paddle.nn.GELU", inputs=layer_inputs, outputs=layer_outputs)
"paddle.nn.GELU", inputs=layer_inputs, outputs=layer_outputs, scope_name=scope_name)
return current_inputs, current_outputs
......@@ -1696,6 +1923,7 @@ def aten___getitem__(mapper, graph, node):
%72 (list): 需要获取元素的list。
%88 (int): 索引。
"""
scope_name = mapper.normalize_scope_name(node)
output_name = mapper._get_outputs_name(node)[0]
layer_outputs = [output_name]
layer_inputs = {}
......@@ -1703,15 +1931,15 @@ def aten___getitem__(mapper, graph, node):
# 获取当前节点输出的list
current_outputs = [output_name]
# 处理输入0,即%72
mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs)
mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs, scope_name)
layer_inputs["list"] = inputs_name[0]
# 处理输入1,即%88
mapper._check_input(graph, inputs_node[1], inputs_name[1], current_outputs)
mapper._check_input(graph, inputs_node[1], inputs_name[1], current_outputs, scope_name)
layer_inputs["index"] = inputs_name[1]
# 获取当前节点输入的list
current_inputs = list(layer_inputs.values())
graph.add_layer("prim.getitem", inputs=layer_inputs, outputs=layer_outputs)
graph.add_layer("prim.getitem", inputs=layer_inputs, outputs=layer_outputs, scope_name=scope_name)
return current_inputs, current_outputs
......@@ -1725,6 +1953,7 @@ def aten_gt(mapper, graph, node):
%82 (-): 需对比的输入1。
%78 (-): 需对比的输入2。
"""
scope_name = mapper.normalize_scope_name(node)
output_name = mapper._get_outputs_name(node)[0]
layer_outputs = [output_name]
layer_inputs = {}
......@@ -1732,15 +1961,15 @@ def aten_gt(mapper, graph, node):
# 获取当前节点输出的list
current_outputs = [output_name]
# 处理输入0,即%82
mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs)
mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs, scope_name)
layer_inputs["x"] = inputs_name[0]
# 处理输入1,即%78
mapper._check_input(graph, inputs_node[1], inputs_name[1], current_outputs)
mapper._check_input(graph, inputs_node[1], inputs_name[1], current_outputs, scope_name)
layer_inputs["y"] = inputs_name[1]
# 获取当前节点输入的list
current_inputs = list(layer_inputs.values())
graph.add_layer("prim.gt", inputs=layer_inputs, outputs=layer_outputs)
graph.add_layer("prim.gt", inputs=layer_inputs, outputs=layer_outputs, scope_name=scope_name)
return current_inputs, current_outputs
......@@ -1755,20 +1984,17 @@ def aten_hardtanh_(mapper, graph, node):
%67 (float): hardtanh激活的最小阈值。
%66 (float): hardtanh激活的最大阈值。
"""
if "tanh" in mapper.dygraph_name_id:
mapper.dygraph_name_id["tanh"] += 1
else:
mapper.dygraph_name_id["tanh"] = 0
tanh_name = "tanh" + str(mapper.dygraph_name_id["tanh"])
scope_name = mapper.normalize_scope_name(node)
op_name = name_generator("hardtanh", mapper.nn_name2id)
output_name = mapper._get_outputs_name(node)[0]
layer_outputs = [tanh_name, output_name]
layer_outputs = [op_name, output_name]
layer_inputs = {}
layer_attrs = {}
inputs_name, inputs_node = mapper._get_inputs_name(node)
# 获取当前节点输出的list
current_outputs = [output_name]
# 处理输入0,即%input.20
mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs)
mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs, scope_name)
layer_inputs["x"] = inputs_name[0]
# 获取当前节点输入的list
current_inputs = list(layer_inputs.values())
......@@ -1781,6 +2007,7 @@ def aten_hardtanh_(mapper, graph, node):
'paddle.nn.Hardtanh',
inputs=layer_inputs,
outputs=layer_outputs,
scope_name=scope_name,
**layer_attrs)
return current_inputs, current_outputs
......@@ -1796,6 +2023,7 @@ def aten_index_select(mapper, graph, node):
%320 (int): 维度。
%371 (Tensor): 选择的索引。
"""
scope_name = mapper.normalize_scope_name(node)
output_name = mapper._get_outputs_name(node)[0]
layer_outputs = [output_name]
layer_inputs = {}
......@@ -1804,18 +2032,18 @@ def aten_index_select(mapper, graph, node):
# 获取当前节点输出的list
current_outputs = [output_name]
# 处理输入0,即%x2.3
mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs)
mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs, scope_name)
layer_inputs["x"] = inputs_name[0]
# 处理输入1,即%320
if inputs_name[1] in mapper.attrs:
layer_attrs["axis"] = mapper.attrs[inputs_name[1]]
else:
mapper._check_input(graph, inputs_node[1], inputs_name[1],
current_outputs)
current_outputs, scope_name)
layer_inputs["axis"] = inputs_name[1]
current_inputs.append(inputs_name[1])
# 处理输入2,即%371
mapper._check_input(graph, inputs_node[2], inputs_name[2], current_outputs)
mapper._check_input(graph, inputs_node[2], inputs_name[2], current_outputs, scope_name)
layer_inputs["index"] = inputs_name[2]
# 获取当前节点输入的list
current_inputs = list(layer_inputs.values())
......@@ -1824,6 +2052,7 @@ def aten_index_select(mapper, graph, node):
"prim.index_select",
inputs=layer_inputs,
outputs=current_outputs,
scope_name=scope_name,
**layer_attrs)
return current_inputs, current_outputs
......@@ -1837,6 +2066,7 @@ def aten_Int(mapper, graph, node):
%1739 (int): 输出,int型数据。
%1738 (-): 需要强转的数据。
"""
scope_name = mapper.normalize_scope_name(node)
output_name = mapper._get_outputs_name(node)[0]
layer_outputs = [output_name]
layer_inputs = {}
......@@ -1844,12 +2074,12 @@ def aten_Int(mapper, graph, node):
# 获取当前节点输出的list
current_outputs = [output_name]
# 处理输入0,即%1738
mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs)
mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs, scope_name)
layer_inputs["input"] = inputs_name[0]
# 获取当前节点输入的list
current_inputs = list(layer_inputs.values())
graph.add_layer("prim.int", inputs=layer_inputs, outputs=layer_outputs)
graph.add_layer("prim.int", inputs=layer_inputs, outputs=layer_outputs, scope_name=scope_name)
return current_inputs, current_outputs
......@@ -1863,6 +2093,7 @@ def aten___is__(mapper, graph, node):
%size.122 (-): 需对比的输入1。
%3931 (-): 需对比的输入2。
"""
scope_name = mapper.normalize_scope_name(node)
output_name = mapper._get_outputs_name(node)[0]
layer_outputs = [output_name]
layer_inputs = {}
......@@ -1870,15 +2101,15 @@ def aten___is__(mapper, graph, node):
# 获取当前节点输出的list
current_outputs = [output_name]
# 处理输入0,即%size.122
mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs)
mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs, scope_name)
layer_inputs["x"] = inputs_name[0]
# 处理输入1,即%3931
mapper._check_input(graph, inputs_node[1], inputs_name[1], current_outputs)
mapper._check_input(graph, inputs_node[1], inputs_name[1], current_outputs, scope_name)
layer_inputs["y"] = inputs_name[1]
# 获取当前节点输入的list
current_inputs = list(layer_inputs.values())
graph.add_layer("prim.is", inputs=layer_inputs, outputs=layer_outputs)
graph.add_layer("prim.is", inputs=layer_inputs, outputs=layer_outputs, scope_name=scope_name)
return current_inputs, current_outputs
......@@ -1892,6 +2123,7 @@ def aten___isnot__(mapper, graph, node):
%size.122 (-): 需对比的输入1。
%3931 (-): 需对比的输入2。
"""
scope_name = mapper.normalize_scope_name(node)
output_name = mapper._get_outputs_name(node)[0]
layer_outputs = [output_name]
layer_inputs = {}
......@@ -1899,15 +2131,15 @@ def aten___isnot__(mapper, graph, node):
# 获取当前节点输出的list
current_outputs = [output_name]
# 处理输入0,即%size.122
mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs)
mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs, scope_name)
layer_inputs["x"] = inputs_name[0]
# 处理输入1,即%3931
mapper._check_input(graph, inputs_node[1], inputs_name[1], current_outputs)
mapper._check_input(graph, inputs_node[1], inputs_name[1], current_outputs, scope_name)
layer_inputs["y"] = inputs_name[1]
# 获取当前节点输入的list
current_inputs = list(layer_inputs.values())
graph.add_layer("prim.isnot", inputs=layer_inputs, outputs=layer_outputs)
graph.add_layer("prim.isnot", inputs=layer_inputs, outputs=layer_outputs, scope_name=scope_name)
return current_inputs, current_outputs
......@@ -1925,20 +2157,17 @@ def aten_layer_norm(mapper, graph, node):
%70 (float): 指明在计算过程中是否添加较小的值到方差中以防止除零。
%71 (bool): 是否启用cudnn。
"""
if "layernorm" in mapper.dygraph_name_id:
mapper.dygraph_name_id["layernorm"] += 1
else:
mapper.dygraph_name_id["layernorm"] = 0
layernorm_name = "layernorm" + str(mapper.dygraph_name_id["layernorm"])
scope_name = mapper.normalize_scope_name(node)
op_name = name_generator("layernorm", mapper.nn_name2id)
output_name = mapper._get_outputs_name(node)[0]
layer_outputs = [layernorm_name, output_name]
layer_outputs = [op_name, output_name]
layer_inputs = {}
layer_attrs = {}
inputs_name, inputs_node = mapper._get_inputs_name(node)
# 获取当前节点输出的list
current_outputs = [output_name]
# 处理输入0,即%input.6
mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs)
mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs, scope_name)
layer_inputs["input"] = inputs_name[0]
# 获取当前节点输入、输出的list
current_inputs = list(layer_inputs.values())
......@@ -1946,14 +2175,14 @@ def aten_layer_norm(mapper, graph, node):
layer_attrs["normalized_shape"] = mapper.attrs[inputs_name[1]]
# 处理输入2,即%174
weights = mapper.pytorch_params[inputs_name[2]]
mapper.paddle_params[layernorm_name + ".weight"] = weights
mapper.paddle_params[op_name + ".weight"] = weights
# 处理输入3,即%173
if inputs_name[3] in mapper.pytorch_params:
bias = mapper.pytorch_params[inputs_name[3]]
if bias is not None:
mapper.paddle_params[layernorm_name + ".bias"] = bias
mapper.paddle_params[op_name + ".bias"] = bias
else:
mapper.paddle_params[layernorm_name + ".bias"] = False
mapper.paddle_params[op_name + ".bias"] = False
# 处理输入4,即%70
layer_attrs["epsilon"] = mapper.attrs[inputs_name[4]]
......@@ -1961,6 +2190,7 @@ def aten_layer_norm(mapper, graph, node):
"paddle.nn.LayerNorm",
inputs=layer_inputs,
outputs=layer_outputs,
scope_name=scope_name,
**layer_attrs)
return current_inputs, current_outputs
......@@ -1975,6 +2205,7 @@ def aten_le(mapper, graph, node):
%78 (-): 需对比的输入1。
%79 (-): 需对比的输入2。
"""
scope_name = mapper.normalize_scope_name(node)
output_name = mapper._get_outputs_name(node)[0]
layer_outputs = [output_name]
layer_inputs = {}
......@@ -1982,15 +2213,15 @@ def aten_le(mapper, graph, node):
# 获取当前节点输出的list
current_outputs = [output_name]
# 处理输入0,即%78
mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs)
mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs, scope_name)
layer_inputs["x"] = inputs_name[0]
# 处理输入1,即%79
mapper._check_input(graph, inputs_node[1], inputs_name[1], current_outputs)
mapper._check_input(graph, inputs_node[1], inputs_name[1], current_outputs, scope_name)
layer_inputs["y"] = inputs_name[1]
# 获取当前节点输入的list
current_inputs = list(layer_inputs.values())
graph.add_layer("prim.le", inputs=layer_inputs, outputs=layer_outputs)
graph.add_layer("prim.le", inputs=layer_inputs, outputs=layer_outputs, scope_name=scope_name)
return current_inputs, current_outputs
......@@ -2004,20 +2235,17 @@ def aten_leaky_relu_(mapper, graph, node):
%input.114 (Tensor): 需要leaky relu的Tensor。
%1570 (float): 输入中的元素小于0时的斜率。
"""
if "leaky_relu" in mapper.dygraph_name_id:
mapper.dygraph_name_id["leaky_relu"] += 1
else:
mapper.dygraph_name_id["leaky_relu"] = 0
leaky_relu_name = "leaky_relu" + str(mapper.dygraph_name_id["leaky_relu"])
scope_name = mapper.normalize_scope_name(node)
op_name = name_generator("leakly_relu", mapper.nn_name2id)
output_name = mapper._get_outputs_name(node)[0]
layer_outputs = [leaky_relu_name, output_name]
layer_outputs = [op_name, output_name]
layer_inputs = {}
layer_attrs = {}
inputs_name, inputs_node = mapper._get_inputs_name(node)
# 获取当前节点输出的list
current_outputs = [output_name]
# 处理输入0,即%result.5
mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs)
mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs, scope_name)
layer_inputs["x"] = inputs_name[0]
# 获取当前节点输入、输出的list
current_inputs = list(layer_inputs.values())
......@@ -2028,6 +2256,7 @@ def aten_leaky_relu_(mapper, graph, node):
"paddle.nn.LeakyReLU",
inputs=layer_inputs,
outputs=layer_outputs,
scope_name=scope_name,
**layer_attrs)
return current_inputs, current_outputs
......@@ -2041,6 +2270,7 @@ def aten_len(mapper, graph, node):
%85 (int): 输出,list的长度。
%72 (list): 需要获取长度的list。
"""
scope_name = mapper.normalize_scope_name(node)
output_name = mapper._get_outputs_name(node)[0]
layer_outputs = [output_name]
layer_inputs = {}
......@@ -2048,12 +2278,12 @@ def aten_len(mapper, graph, node):
# 获取当前节点输出的list
current_outputs = [output_name]
# 处理输入0,即%72
mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs)
mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs, scope_name)
layer_inputs["input"] = inputs_name[0]
# 获取当前节点输入的list
current_inputs = list(layer_inputs.values())
graph.add_layer("prim.len", inputs=layer_inputs, outputs=layer_outputs)
graph.add_layer("prim.len", inputs=layer_inputs, outputs=layer_outputs, scope_name=scope_name)
return current_inputs, current_outputs
......@@ -2066,6 +2296,7 @@ def aten_log(mapper, graph, node):
%787 (Tensor): 输出,取log的Tensor。
%786 (Tensor): 需要获取log的Tensor。
"""
scope_name = mapper.normalize_scope_name(node)
output_name = mapper._get_outputs_name(node)[0]
layer_outputs = [output_name]
layer_inputs = {}
......@@ -2073,13 +2304,13 @@ def aten_log(mapper, graph, node):
# 获取当前节点输出的list
current_outputs = [output_name]
# 处理输入0,即%786
mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs)
mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs, scope_name)
layer_inputs["x"] = inputs_name[0]
# 获取当前节点输入的list
current_inputs = list(layer_inputs.values())
graph.add_layer(
"fluid.layers.log", inputs=layer_inputs, outputs=layer_outputs)
"paddle.log", inputs=layer_inputs, outputs=layer_outputs, scope_name=scope_name)
return current_inputs, current_outputs
......@@ -2093,6 +2324,7 @@ def aten_lt(mapper, graph, node):
%78 (-): 需对比的输入1。
%79 (-): 需对比的输入2。
"""
scope_name = mapper.normalize_scope_name(node)
output_name = mapper._get_outputs_name(node)[0]
layer_outputs = [output_name]
layer_inputs = {}
......@@ -2100,15 +2332,15 @@ def aten_lt(mapper, graph, node):
# 获取当前节点输出的list
current_outputs = [output_name]
# 处理输入0,即%78
mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs)
mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs, scope_name)
layer_inputs["x"] = inputs_name[0]
# 处理输入1,即%79
mapper._check_input(graph, inputs_node[1], inputs_name[1], current_outputs)
mapper._check_input(graph, inputs_node[1], inputs_name[1], current_outputs, scope_name)
layer_inputs["y"] = inputs_name[1]
# 获取当前节点输入的list
current_inputs = list(layer_inputs.values())
graph.add_layer("prim.lt", inputs=layer_inputs, outputs=layer_outputs)
graph.add_layer("prim.lt", inputs=layer_inputs, outputs=layer_outputs, scope_name=scope_name)
return current_inputs, current_outputs
......@@ -2123,6 +2355,7 @@ def aten_masked_fill_(mapper, graph, node):
%mask.2 (Tensor): bool型的Tensor,哪些位置需要填充。
%46 (-): 填充的值。
"""
scope_name = mapper.normalize_scope_name(node)
output_name = mapper._get_outputs_name(node)[0]
layer_outputs = [output_name]
layer_inputs = {}
......@@ -2132,45 +2365,52 @@ def aten_masked_fill_(mapper, graph, node):
# 获取当前节点输出的list
current_outputs = [output_name]
# 处理输入0,即%input.4
mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs)
mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs, scope_name)
current_inputs.append(inputs_name[0])
graph.add_layer(
"prim.type",
inputs={"input": inputs_name[0]},
outputs=[inputs_name[0] + "_type"])
outputs=[inputs_name[0] + "_type"],
scope_name=scope_name)
# 处理输入1,即%scores.2
mapper._check_input(graph, inputs_node[1], inputs_name[1], current_outputs)
mapper._check_input(graph, inputs_node[1], inputs_name[1], current_outputs, scope_name)
current_inputs.append(inputs_name[1])
graph.add_layer(
"paddle.logical_not",
inputs={"x": inputs_name[1]},
outputs=[inputs_name[1] + "_not"])
outputs=[inputs_name[1] + "_not"],
scope_name=scope_name)
graph.add_layer(
"fluid.layers.cast",
"paddle.cast",
inputs={"x": inputs_name[1]},
outputs=[inputs_name[1] + "_mask"],
scope_name=scope_name,
dtype=inputs_name[0] + "_type")
graph.add_layer(
"fluid.layers.cast",
"paddle.cast",
inputs={"x": inputs_name[1] + "_not"},
outputs=[inputs_name[1] + "_not_mask"],
scope_name=scope_name,
dtype=inputs_name[0] + "_type")
graph.add_layer(
"paddle.multiply",
inputs={"x": inputs_name[0],
"y": inputs_name[1] + "_not_mask"},
outputs=[inputs_name[0] + "_not_mask"])
outputs=[inputs_name[0] + "_not_mask"],
scope_name=scope_name)
# 处理输入2,即%46
mapper._check_input(graph, inputs_node[2], inputs_name[2], current_outputs)
mapper._check_input(graph, inputs_node[2], inputs_name[2], current_outputs, scope_name)
graph.add_layer(
"prim.eq",
inputs={"x": inputs_name[2]},
outputs=[inputs_name[2] + "_cond1"],
scope_name=scope_name,
y="-float('inf')")
graph.add_layer(
"prim.eq",
inputs={"x": inputs_name[2]},
outputs=[inputs_name[2] + "_cond2"],
scope_name=scope_name,
y="float('inf')")
graph.add_layer(
"prim.or",
......@@ -2178,32 +2418,37 @@ def aten_masked_fill_(mapper, graph, node):
"x": inputs_name[2] + "_cond1",
"y": inputs_name[2] + "_cond2"
},
outputs=[inputs_name[2] + "_cond"])
outputs=[inputs_name[2] + "_cond"],
scope_name=scope_name)
graph.add_layer(
"prim.if", {'input': inputs_name[2] + "_cond"},
outputs=[inputs_name[2] + "_if"])
outputs=[inputs_name[2] + "_if"],
scope_name=scope_name)
if_layer = graph.layers[list(graph.layers.keys())[-1]]
block = PaddleGraph(if_layer, graph_type="dygraph")
block = PaddleGraph(parent_layer=if_layer, graph_type="dygraph")
block.add_layer(
"prim.equal",
inputs={"input": inputs_name[1] + "_mask"},
outputs=[inputs_name[2] + "_1"])
outputs=[inputs_name[2] + "_1"],
scope_name=scope_name)
if_layer.add_block(block)
block = PaddleGraph(if_layer, graph_type="dygraph")
block = PaddleGraph(parent_layer=if_layer, graph_type="dygraph")
block.add_layer(
"prim.mul",
inputs={"x": inputs_name[1] + "_mask",
"y": inputs_name[2]},
outputs=[inputs_name[2] + "_1"])
outputs=[inputs_name[2] + "_1"],
scope_name=scope_name)
if_layer.add_block(block)
if_layer.inputs["input-0"] = inputs_name[1] + "_mask"
if_layer.inputs["input-1"] = inputs_name[2]
if_layer.outputs.append(inputs_name[2] + "_1")
graph.add_layer(
"fluid.layers.elementwise_add",
"paddle.add",
inputs={"x": inputs_name[2] + "_1",
"y": inputs_name[0] + "_not_mask"},
outputs=layer_outputs)
outputs=layer_outputs,
scope_name=scope_name)
return current_inputs, current_outputs
......@@ -2218,6 +2463,7 @@ def aten_masked_fill(mapper, graph, node):
%mask.2 (Tensor): bool型的Tensor,哪些位置需要填充。
%46 (-): 填充的值。
"""
scope_name = mapper.normalize_scope_name(node)
output_name = mapper._get_outputs_name(node)[0]
layer_outputs = [output_name]
layer_inputs = {}
......@@ -2227,45 +2473,52 @@ def aten_masked_fill(mapper, graph, node):
# 获取当前节点输出的list
current_outputs = [output_name]
# 处理输入0,即%input.4
mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs)
mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs, scope_name)
current_inputs.append(inputs_name[0])
graph.add_layer(
"prim.type",
inputs={"input": inputs_name[0]},
outputs=[inputs_name[0] + "_type"])
outputs=[inputs_name[0] + "_type"],
scope_name=scope_name)
# 处理输入1,即%scores.2
mapper._check_input(graph, inputs_node[1], inputs_name[1], current_outputs)
mapper._check_input(graph, inputs_node[1], inputs_name[1], current_outputs, scope_name)
current_inputs.append(inputs_name[1])
graph.add_layer(
"paddle.logical_not",
inputs={"x": inputs_name[1]},
outputs=[inputs_name[1] + "_not"])
outputs=[inputs_name[1] + "_not"],
scope_name=scope_name)
graph.add_layer(
"fluid.layers.cast",
"paddle.cast",
inputs={"x": inputs_name[1]},
outputs=[inputs_name[1] + "_mask"],
scope_name=scope_name,
dtype=inputs_name[0] + "_type")
graph.add_layer(
"fluid.layers.cast",
"paddle.cast",
inputs={"x": inputs_name[1] + "_not"},
outputs=[inputs_name[1] + "_not_mask"],
scope_name=scope_name,
dtype=inputs_name[0] + "_type")
graph.add_layer(
"paddle.multiply",
inputs={"x": inputs_name[0],
"y": inputs_name[1] + "_not_mask"},
outputs=[inputs_name[0] + "_not_mask"])
outputs=[inputs_name[0] + "_not_mask"],
scope_name=scope_name)
# 处理输入2,即%46
mapper._check_input(graph, inputs_node[2], inputs_name[2], current_outputs)
mapper._check_input(graph, inputs_node[2], inputs_name[2], current_outputs, scope_name)
graph.add_layer(
"prim.eq",
inputs={"x": inputs_name[2]},
outputs=[inputs_name[2] + "_cond1"],
scope_name=scope_name,
y="-float('inf')")
graph.add_layer(
"prim.eq",
inputs={"x": inputs_name[2]},
outputs=[inputs_name[2] + "_cond2"],
scope_name=scope_name,
y="float('inf')")
graph.add_layer(
"prim.or",
......@@ -2273,32 +2526,37 @@ def aten_masked_fill(mapper, graph, node):
"x": inputs_name[2] + "_cond1",
"y": inputs_name[2] + "_cond2"
},
outputs=[inputs_name[2] + "_cond"])
outputs=[inputs_name[2] + "_cond"],
scope_name=scope_name)
graph.add_layer(
"prim.if", {'input': inputs_name[2] + "_cond"},
outputs=[inputs_name[2] + "_if"])
outputs=[inputs_name[2] + "_if"],
scope_name=scope_name)
if_layer = graph.layers[list(graph.layers.keys())[-1]]
block = PaddleGraph(if_layer, graph_type="dygraph")
block = PaddleGraph(parent_layer=if_layer, graph_type="dygraph")
block.add_layer(
"prim.equal",
inputs={"input": inputs_name[1] + "_mask"},
outputs=[inputs_name[2] + "_1"])
outputs=[inputs_name[2] + "_1"],
scope_name=scope_name)
if_layer.add_block(block)
block = PaddleGraph(if_layer, graph_type="dygraph")
block = PaddleGraph(parent_layer=if_layer, graph_type="dygraph")
block.add_layer(
"prim.mul",
inputs={"x": inputs_name[1] + "_mask",
"y": inputs_name[2]},
outputs=[inputs_name[2] + "_1"])
outputs=[inputs_name[2] + "_1"],
scope_name=scope_name)
if_layer.add_block(block)
if_layer.inputs["input-0"] = inputs_name[1] + "_mask"
if_layer.inputs["input-1"] = inputs_name[2]
if_layer.outputs.append(inputs_name[2] + "_1")
graph.add_layer(
"fluid.layers.elementwise_add",
"paddle.add",
inputs={"x": inputs_name[2] + "_1",
"y": inputs_name[0] + "_not_mask"},
outputs=layer_outputs)
outputs=layer_outputs,
scope_name=scope_name)
return current_inputs, current_outputs
......@@ -2312,6 +2570,7 @@ def aten_max(mapper, graph, node):
%val_if_large.3 (Tensor): 输入,需要对比的Tensor1。
%159 (Tensor): 输入,需要对比的Tensor2。
"""
scope_name = mapper.normalize_scope_name(node)
output_name = mapper._get_outputs_name(node)[0]
layer_outputs = [output_name]
layer_inputs = {}
......@@ -2322,16 +2581,16 @@ def aten_max(mapper, graph, node):
if str(input_type) == "Tensor":
# 处理输入0,即%val_if_large.3
mapper._check_input(graph, inputs_node[0], inputs_name[0],
current_outputs)
current_outputs, scope_name)
layer_inputs["x"] = inputs_name[0]
# 处理输入1,即%159
mapper._check_input(graph, inputs_node[1], inputs_name[1],
current_outputs)
current_outputs, scope_name)
layer_inputs["y"] = inputs_name[1]
# 获取当前节点输入的list
current_inputs = list(layer_inputs.values())
graph.add_layer(
"paddle.maximum", inputs=layer_inputs, outputs=layer_outputs)
"paddle.maximum", inputs=layer_inputs, outputs=layer_outputs, scope_name=scope_name)
else:
pass
return current_inputs, current_outputs
......@@ -2351,46 +2610,58 @@ def aten_max_pool2d(mapper, graph, node):
%22 (list): 膨胀系数大小。
%19 (bool): 是否用ceil函数计算输出高度和宽度。
"""
if "pool" in mapper.dygraph_name_id:
mapper.dygraph_name_id["pool"] += 1
else:
mapper.dygraph_name_id["pool"] = 0
pool_name = "pool" + str(mapper.dygraph_name_id["pool"])
scope_name = mapper.normalize_scope_name(node)
op_name = name_generator("pool2d", mapper.nn_name2id)
output_name = mapper._get_outputs_name(node)[0]
layer_outputs = [pool_name, output_name]
layer_outputs = [op_name, output_name]
layer_inputs = {}
layer_attrs = {}
layer_attrs_tmp = {}
inputs_name, inputs_node = mapper._get_inputs_name(node)
# 获取当前节点输出的list
current_outputs = [output_name]
# 处理输入0,即%result.11
mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs)
mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs, scope_name)
layer_inputs["input"] = inputs_name[0]
# 获取当前节点输入的list
current_inputs = list(layer_inputs.values())
# 处理输入1,即%20
layer_attrs["pool_size"] = mapper.attrs[inputs_name[1]]
layer_attrs["kernel_size"] = mapper.attrs[inputs_name[1]]
layer_attrs_tmp["pool_size"] = mapper.attrs[inputs_name[1]]
# 处理输入2,即%23
layer_attrs["pool_stride"] = mapper.attrs[inputs_name[2]]
layer_attrs["stride"] = mapper.attrs[inputs_name[2]]
layer_attrs_tmp["pool_stride"] = mapper.attrs[inputs_name[2]]
# 处理输入3,即%21
layer_attrs["pool_padding"] = mapper.attrs[inputs_name[3]]
layer_attrs["padding"] = mapper.attrs[inputs_name[3]]
layer_attrs_tmp["pool_padding"] = mapper.attrs[inputs_name[3]]
# 处理输入4,即%22
graph.add_layer(
"prim.assert",
inputs={},
outputs=[inputs_name[4] + "_assert"],
scope_name=scope_name + "_assert",
type="eq",
key=mapper.attrs[inputs_name[4]],
value=[1, [1, 1]])
# 处理输入5,即%19
layer_attrs["ceil_mode"] = mapper.attrs[inputs_name[5]]
layer_attrs["pool_type"] = string("max")
layer_attrs_tmp["ceil_mode"] = mapper.attrs[inputs_name[5]]
graph.add_layer(
"paddle.nn.Pool2D",
inputs=layer_inputs,
outputs=layer_outputs,
**layer_attrs)
if mapper.attrs[inputs_name[5]] == True:
layer_attrs["pool_type"] = string("max")
graph.add_layer(
"fluid.layers.pool2d",
inputs=layer_inputs,
outputs=layer_outputs[1:],
scope_name=scope_name,
**layer_attrs_tmp)
else:
graph.add_layer(
"paddle.nn.MaxPool2D",
inputs=layer_inputs,
outputs=layer_outputs,
scope_name=scope_name,
**layer_attrs)
return current_inputs, current_outputs
......@@ -2404,6 +2675,7 @@ def aten_matmul(mapper, graph, node):
%101 (Tensor): 矩阵1。
%102 (Tensor): 矩阵2。
"""
scope_name = mapper.normalize_scope_name(node)
output_name = mapper._get_outputs_name(node)[0]
layer_outputs = [output_name]
layer_inputs = {}
......@@ -2411,15 +2683,15 @@ def aten_matmul(mapper, graph, node):
# 获取当前节点输出的list
current_outputs = [output_name]
# 处理输入0,即%101
mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs)
mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs, scope_name)
layer_inputs["x"] = inputs_name[0]
# 处理输入1,即%102
mapper._check_input(graph, inputs_node[1], inputs_name[1], current_outputs)
mapper._check_input(graph, inputs_node[1], inputs_name[1], current_outputs, scope_name)
layer_inputs["y"] = inputs_name[1]
# 获取当前节点输入的list
current_inputs = list(layer_inputs.values())
graph.add_layer("paddle.matmul", inputs=layer_inputs, outputs=layer_outputs)
graph.add_layer("paddle.matmul", inputs=layer_inputs, outputs=layer_outputs, scope_name=scope_name)
return current_inputs, current_outputs
......@@ -2433,6 +2705,7 @@ def aten_min(mapper, graph, node):
%val_if_large.3 (Tensor): 输入,需要对比的Tensor1。
%159 (Tensor): 输入,需要对比的Tensor2。
"""
scope_name = mapper.normalize_scope_name(node)
output_name = mapper._get_outputs_name(node)[0]
layer_outputs = [output_name]
layer_inputs = {}
......@@ -2443,16 +2716,16 @@ def aten_min(mapper, graph, node):
if str(input_type) == "Tensor":
# 处理输入0,即%val_if_large.3
mapper._check_input(graph, inputs_node[0], inputs_name[0],
current_outputs)
current_outputs, scope_name)
layer_inputs["x"] = inputs_name[0]
# 处理输入1,即%159
mapper._check_input(graph, inputs_node[1], inputs_name[1],
current_outputs)
current_outputs, scope_name)
layer_inputs["y"] = inputs_name[1]
# 获取当前节点输入的list
current_inputs = list(layer_inputs.values())
graph.add_layer(
"paddle.minimum", inputs=layer_inputs, outputs=layer_outputs)
"paddle.minimum", inputs=layer_inputs, outputs=layer_outputs, scope_name=scope_name)
else:
pass
return current_inputs, current_outputs
......@@ -2470,6 +2743,7 @@ def aten_mean(mapper, graph, node):
%3 (bool): 是否在输出Tensor中保留减小的维度。
%2 (Tensor): 结果Tensor。
"""
scope_name = mapper.normalize_scope_name(node)
output_name = mapper._get_outputs_name(node)[0]
layer_outputs = [output_name]
layer_inputs = {}
......@@ -2478,30 +2752,31 @@ def aten_mean(mapper, graph, node):
# 获取当前节点输出的list
current_outputs = [output_name]
# 处理输入0,即%result.1
mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs)
layer_inputs["input"] = inputs_name[0]
mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs, scope_name)
layer_inputs["x"] = inputs_name[0]
current_inputs = list(layer_inputs.values())
# 处理输入1,即%4967
if inputs_name[1] in mapper.attrs:
layer_attrs["dim"] = mapper.attrs[inputs_name[1]]
layer_attrs["axis"] = mapper.attrs[inputs_name[1]]
else:
mapper._check_input(graph, inputs_node[1], inputs_name[1],
current_outputs)
layer_inputs["dim"] = inputs_name[1]
current_outputs, scope_name)
layer_inputs["axis"] = inputs_name[1]
current_inputs.append(inputs_name[1])
# 处理输入2,即%3
if inputs_name[1] in mapper.attrs:
layer_attrs["keep_dim"] = mapper.attrs[inputs_name[2]]
layer_attrs["keepdim"] = mapper.attrs[inputs_name[2]]
else:
mapper._check_input(graph, inputs_node[2], inputs_name[2],
current_outputs)
layer_inputs["keep_dim"] = inputs_name[2]
current_outputs, scope_name)
layer_inputs["keepdim"] = inputs_name[2]
current_inputs.append(inputs_name[2])
graph.add_layer(
"fluid.layers.reduce_mean",
"paddle.mean",
inputs=layer_inputs,
outputs=layer_outputs,
scope_name=scope_name,
**layer_attrs)
return current_inputs, current_outputs
......@@ -2516,6 +2791,7 @@ def aten_mul(mapper, graph, node):
%size_prods.38 (-): 数值1。
%114 (-): 数值2。
"""
scope_name = mapper.normalize_scope_name(node)
output_name = mapper._get_outputs_name(node)[0]
layer_outputs = [output_name]
layer_inputs = {}
......@@ -2523,16 +2799,16 @@ def aten_mul(mapper, graph, node):
# 获取当前节点输出的list
current_outputs = [output_name]
# 处理输入0,即%size_prods.38
mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs)
mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs, scope_name)
layer_inputs["x"] = inputs_name[0]
# 处理输入1,即%114
mapper._check_input(graph, inputs_node[1], inputs_name[1], current_outputs)
mapper._check_input(graph, inputs_node[1], inputs_name[1], current_outputs, scope_name)
layer_inputs["y"] = inputs_name[1]
# 获取当前节点输入的list
current_inputs = list(layer_inputs.values())
current_outputs = layer_outputs
graph.add_layer("prim.mul", inputs=layer_inputs, outputs=layer_outputs)
graph.add_layer("prim.mul", inputs=layer_inputs, outputs=layer_outputs, scope_name=scope_name)
return current_inputs, current_outputs
......@@ -2546,6 +2822,7 @@ def aten_mul_(mapper, graph, node):
%size_prods.38 (-): 数值1。
%114 (-): 数值2。
"""
scope_name = mapper.normalize_scope_name(node)
output_name = mapper._get_outputs_name(node)[0]
layer_outputs = [output_name]
layer_inputs = {}
......@@ -2553,16 +2830,16 @@ def aten_mul_(mapper, graph, node):
# 获取当前节点输出的list
current_outputs = [output_name]
# 处理输入0,即%size_prods.38
mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs)
mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs, scope_name)
layer_inputs["x"] = inputs_name[0]
# 处理输入1,即%114
mapper._check_input(graph, inputs_node[1], inputs_name[1], current_outputs)
mapper._check_input(graph, inputs_node[1], inputs_name[1], current_outputs, scope_name)
layer_inputs["y"] = inputs_name[1]
# 获取当前节点输入的list
current_inputs = list(layer_inputs.values())
current_outputs = layer_outputs
graph.add_layer("prim.mul", inputs=layer_inputs, outputs=layer_outputs)
graph.add_layer("prim.mul", inputs=layer_inputs, outputs=layer_outputs, scope_name=scope_name)
return current_inputs, current_outputs
......@@ -2576,6 +2853,7 @@ def aten_ne(mapper, graph, node):
%133 (-): 需对比的输入1。
%132 (-): 需对比的输入2。
"""
scope_name = mapper.normalize_scope_name(node)
output_name = mapper._get_outputs_name(node)[0]
layer_outputs = [output_name]
layer_inputs = {}
......@@ -2583,15 +2861,15 @@ def aten_ne(mapper, graph, node):
# 获取当前节点输出的list
current_outputs = [output_name]
# 处理输入0,即%124
mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs)
mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs, scope_name)
layer_inputs["x"] = inputs_name[0]
# 处理输入1,即%123
mapper._check_input(graph, inputs_node[1], inputs_name[1], current_outputs)
mapper._check_input(graph, inputs_node[1], inputs_name[1], current_outputs, scope_name)
layer_inputs["y"] = inputs_name[1]
# 获取当前节点输入的list
current_inputs = list(layer_inputs.values())
graph.add_layer("prim.ne", inputs=layer_inputs, outputs=layer_outputs)
graph.add_layer("prim.ne", inputs=layer_inputs, outputs=layer_outputs, scope_name=scope_name)
return current_inputs, current_outputs
......@@ -2604,6 +2882,7 @@ def aten_neg(mapper, graph, node):
%909 (int): 取负后结果。
%908 (int): 需取负的输入。
"""
scope_name = mapper.normalize_scope_name(node)
output_name = mapper._get_outputs_name(node)[0]
layer_outputs = [output_name]
layer_inputs = {}
......@@ -2611,12 +2890,12 @@ def aten_neg(mapper, graph, node):
# 获取当前节点输出的list
current_outputs = [output_name]
# 处理输入0,即%124
mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs)
mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs, scope_name)
layer_inputs["input"] = inputs_name[0]
# 获取当前节点输入的list
current_inputs = list(layer_inputs.values())
graph.add_layer("prim.neg", inputs=layer_inputs, outputs=layer_outputs)
graph.add_layer("prim.neg", inputs=layer_inputs, outputs=layer_outputs, scope_name=scope_name)
return current_inputs, current_outputs
......@@ -2629,6 +2908,7 @@ def aten___not__(mapper, graph, node):
%4498 (bool): 取负后结果。
%aux_defined.2 (bool): 需取负的输入。
"""
scope_name = mapper.normalize_scope_name(node)
output_name = mapper._get_outputs_name(node)[0]
layer_outputs = [output_name]
layer_inputs = {}
......@@ -2636,12 +2916,12 @@ def aten___not__(mapper, graph, node):
# 获取当前节点输出的list
current_outputs = [output_name]
# 处理输入0,即%124
mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs)
mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs, scope_name)
layer_inputs["input"] = inputs_name[0]
# 获取当前节点输入的list
current_inputs = list(layer_inputs.values())
graph.add_layer("prim.not", inputs=layer_inputs, outputs=layer_outputs)
graph.add_layer("prim.not", inputs=layer_inputs, outputs=layer_outputs, scope_name=scope_name)
return current_inputs, current_outputs
......@@ -2658,6 +2938,7 @@ def aten_ones(mapper, graph, node):
%4995 (Device): 设备。
%4995 (bool): 是否计算梯度。
"""
scope_name = mapper.normalize_scope_name(node)
output_name = mapper._get_outputs_name(node)[0]
layer_outputs = [output_name]
layer_inputs = {}
......@@ -2671,7 +2952,7 @@ def aten_ones(mapper, graph, node):
layer_attrs["shape"] = mapper.attrs[inputs_name[0]]
else:
mapper._check_input(graph, inputs_node[0], inputs_name[0],
current_outputs)
current_outputs, scope_name)
layer_inputs["shape"] = inputs_name[0]
current_inputs.append(inputs_name[0])
# 处理输入1,即%8,代表dtype
......@@ -2681,6 +2962,7 @@ def aten_ones(mapper, graph, node):
"paddle.ones",
inputs=layer_inputs,
outputs=layer_outputs,
scope_name=scope_name,
**layer_attrs)
return current_inputs, current_outputs
......@@ -2695,6 +2977,7 @@ def aten_permute(mapper, graph, node):
%cls_confs0.2 (Tensor): 需要重排的Tensor。
%2348 (list): 依照此参数进行重排。
"""
scope_name = mapper.normalize_scope_name(node)
output_name = mapper._get_outputs_name(node)[0]
layer_outputs = [output_name]
layer_inputs = {}
......@@ -2703,7 +2986,7 @@ def aten_permute(mapper, graph, node):
# 获取当前节点输出的list
current_outputs = [output_name]
# 处理输入0,即%cls_confs0.2
mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs)
mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs, scope_name)
layer_inputs["x"] = inputs_name[0]
# 获取当前节点输入的list
current_inputs = list(layer_inputs.values())
......@@ -2712,14 +2995,15 @@ def aten_permute(mapper, graph, node):
layer_attrs["perm"] = mapper.attrs[inputs_name[1]]
else:
mapper._check_input(graph, inputs_node[1], inputs_name[1],
current_outputs)
current_outputs, scope_name)
layer_inputs["perm"] = inputs_name[1]
current_inputs.append(inputs_name[1])
graph.add_layer(
"fluid.layers.transpose",
"paddle.transpose",
inputs=layer_inputs,
outputs=layer_outputs,
scope_name=scope_name,
**layer_attrs)
return current_inputs, current_outputs
......@@ -2733,6 +3017,7 @@ def aten_pow(mapper, graph, node):
%x.6 (Tensor): 输出,指数激活后的Tensor。
%4700 (Tensor): 需要指数激活的Tensor。
"""
scope_name = mapper.normalize_scope_name(node)
output_name = mapper._get_outputs_name(node)[0]
layer_outputs = [output_name]
layer_inputs = {}
......@@ -2741,23 +3026,24 @@ def aten_pow(mapper, graph, node):
# 获取当前节点输出的list
current_outputs = [output_name]
# 处理输入0,即%4700
mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs)
mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs, scope_name)
layer_inputs["x"] = inputs_name[0]
# 获取当前节点输入、输出的list
current_inputs = list(layer_inputs.values())
# 处理输入1,即%4703
if inputs_name[1] in mapper.attrs:
layer_attrs["factor"] = mapper.attrs[inputs_name[1]]
layer_attrs["y"] = mapper.attrs[inputs_name[1]]
else:
mapper._check_input(graph, inputs_node[1], inputs_name[1],
current_outputs)
layer_inputs["factor"] = inputs_name[1]
current_outputs, scope_name)
layer_inputs["y"] = inputs_name[1]
current_inputs.append(inputs_name[1])
graph.add_layer(
"fluid.layers.pow",
"paddle.pow",
inputs=layer_inputs,
outputs=layer_outputs,
scope_name=scope_name,
**layer_attrs)
return current_inputs, current_outputs
......@@ -2773,25 +3059,22 @@ def aten_relu(mapper, graph, node):
注意: inplace这个参数在paddle中未实现
"""
if "relu" in mapper.dygraph_name_id:
mapper.dygraph_name_id["relu"] += 1
else:
mapper.dygraph_name_id["relu"] = 0
relu_name = "relu" + str(mapper.dygraph_name_id["relu"])
scope_name = mapper.normalize_scope_name(node)
op_name = name_generator("relu", mapper.nn_name2id)
output_name = mapper._get_outputs_name(node)[0]
layer_outputs = [relu_name, output_name]
layer_outputs = [op_name, output_name]
layer_inputs = {}
inputs_name, inputs_node = mapper._get_inputs_name(node)
# 获取当前节点输出的list
current_outputs = [output_name]
# 处理输入0,即%result.5
mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs)
mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs, scope_name)
layer_inputs["x"] = inputs_name[0]
# 获取当前节点输入的list
current_inputs = list(layer_inputs.values())
graph.add_layer(
"paddle.nn.ReLU", inputs=layer_inputs, outputs=layer_outputs)
"paddle.nn.ReLU", inputs=layer_inputs, outputs=layer_outputs, scope_name=scope_name)
return current_inputs, current_outputs
......@@ -2806,25 +3089,22 @@ def aten_relu_(mapper, graph, node):
注意: inplace这个参数在paddle中未实现
"""
if "relu" in mapper.dygraph_name_id:
mapper.dygraph_name_id["relu"] += 1
else:
mapper.dygraph_name_id["relu"] = 0
relu_name = "relu" + str(mapper.dygraph_name_id["relu"])
scope_name = mapper.normalize_scope_name(node)
op_name = name_generator("relu", mapper.nn_name2id)
output_name = mapper._get_outputs_name(node)[0]
layer_outputs = [relu_name, output_name]
layer_outputs = [op_name, output_name]
layer_inputs = {}
inputs_name, inputs_node = mapper._get_inputs_name(node)
# 获取当前节点输出的list
current_outputs = [output_name]
# 处理输入0,即%result.5
mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs)
mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs, scope_name)
layer_inputs["x"] = inputs_name[0]
# 获取当前节点输入的list
current_inputs = list(layer_inputs.values())
graph.add_layer(
"paddle.nn.ReLU", inputs=layer_inputs, outputs=layer_outputs)
"paddle.nn.ReLU", inputs=layer_inputs, outputs=layer_outputs, scope_name=scope_name)
return current_inputs, current_outputs
......@@ -2839,25 +3119,22 @@ def aten_relu6(mapper, graph, node):
注意: inplace这个参数在paddle中未实现
"""
if "relu6" in mapper.dygraph_name_id:
mapper.dygraph_name_id["relu6"] += 1
else:
mapper.dygraph_name_id["relu6"] = 0
relu6_name = "relu6" + str(mapper.dygraph_name_id["relu6"])
scope_name = mapper.normalize_scope_name(node)
op_name = name_generator("relu6", mapper.nn_name2id)
output_name = mapper._get_outputs_name(node)[0]
layer_outputs = [relu6_name, output_name]
layer_outputs = [op_name, output_name]
layer_inputs = {}
inputs_name, inputs_node = mapper._get_inputs_name(node)
# 获取当前节点输出的list
current_outputs = [output_name]
# 处理输入0,即%result.5
mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs)
mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs, scope_name)
layer_inputs["x"] = inputs_name[0]
# 获取当前节点输入的list
current_inputs = list(layer_inputs.values())
graph.add_layer(
"paddle.nn.ReLU6", inputs=layer_inputs, outputs=layer_outputs)
"paddle.nn.ReLU6", inputs=layer_inputs, outputs=layer_outputs, scope_name=scope_name)
return current_inputs, current_outputs
......@@ -2871,6 +3148,7 @@ def aten_repeat(mapper, graph, node):
%699 (Tensor): 需要复制的Tensor。
%700 (list): 指定每个维度复制的次数。
"""
scope_name = mapper.normalize_scope_name(node)
output_name = mapper._get_outputs_name(node)[0]
layer_outputs = [output_name]
layer_inputs = {}
......@@ -2879,7 +3157,7 @@ def aten_repeat(mapper, graph, node):
# 获取当前节点输出的list
current_outputs = [output_name]
# 处理输入0,即%699
mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs)
mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs, scope_name)
layer_inputs["x"] = inputs_name[0]
# 获取当前节点输入、输出的list
current_inputs = list(layer_inputs.values())
......@@ -2888,7 +3166,7 @@ def aten_repeat(mapper, graph, node):
layer_attrs["repeat_times"] = mapper.attrs[inputs_name[1]]
else:
mapper._check_input(graph, inputs_node[1], inputs_name[1],
current_outputs)
current_outputs, scope_name)
layer_inputs["repeat_times"] = inputs_name[1]
current_inputs.append(inputs_name[1])
......@@ -2896,6 +3174,7 @@ def aten_repeat(mapper, graph, node):
"paddle.tile",
inputs=layer_inputs,
outputs=layer_outputs,
scope_name=scope_name,
**layer_attrs)
return current_inputs, current_outputs
......@@ -2910,6 +3189,7 @@ def aten_reshape(mapper, graph, node):
%4700 (Tensor): 需要reshape的Tensor。
%4703 (list): 形状大小组成的list。
"""
scope_name = mapper.normalize_scope_name(node)
output_name = mapper._get_outputs_name(node)[0]
layer_outputs = [output_name]
layer_inputs = {}
......@@ -2918,7 +3198,7 @@ def aten_reshape(mapper, graph, node):
# 获取当前节点输出的list
current_outputs = [output_name]
# 处理输入0,即%4700
mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs)
mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs, scope_name)
layer_inputs["x"] = inputs_name[0]
# 获取当前节点输入、输出的list
current_inputs = list(layer_inputs.values())
......@@ -2927,56 +3207,16 @@ def aten_reshape(mapper, graph, node):
layer_attrs["shape"] = mapper.attrs[inputs_name[1]]
else:
mapper._check_input(graph, inputs_node[1], inputs_name[1],
current_outputs)
current_outputs, scope_name)
layer_inputs["shape"] = inputs_name[1]
current_inputs.append(inputs_name[1])
graph.add_layer(
"prim.type",
inputs={"input": inputs_name[0]},
outputs=[inputs_name[0] + "_type"])
graph.add_layer(
"prim.str",
inputs={"input": inputs_name[0] + "_type"},
outputs=[inputs_name[0] + "_type"])
graph.add_layer(
"prim.eq",
inputs={"x": inputs_name[0] + "_type"},
outputs=[inputs_name[0] + "_cond"],
y=string("VarType.BOOL"))
graph.add_layer(
"prim.if", {'input': inputs_name[0] + "_cond"},
outputs=[inputs_name[0] + "_if1"])
if_layer = graph.layers[list(graph.layers.keys())[-1]]
block = PaddleGraph(if_layer, graph_type="dygraph")
block.add_layer(
"fluid.layers.cast",
inputs={"x": inputs_name[0]},
outputs=[inputs_name[0]],
dtype=string("int32"))
if_layer.add_block(block)
block = PaddleGraph(if_layer, graph_type="dygraph")
if_layer.add_block(block)
if_layer.inputs["input-0"] = inputs_name[0]
graph.add_layer(
"fluid.layers.reshape",
"paddle.reshape",
inputs=layer_inputs,
outputs=layer_outputs,
scope_name=scope_name,
**layer_attrs)
graph.add_layer(
"prim.if", {'input': inputs_name[0] + "_cond"},
outputs=[inputs_name[0] + "_if2"])
if_layer = graph.layers[list(graph.layers.keys())[-1]]
block = PaddleGraph(if_layer, graph_type="dygraph")
block.add_layer(
"fluid.layers.cast",
inputs={"x": layer_outputs[0]},
outputs=layer_outputs,
dtype=string("bool"))
if_layer.add_block(block)
block = PaddleGraph(if_layer, graph_type="dygraph")
if_layer.add_block(block)
if_layer.inputs["input-0"] = layer_outputs[0]
return current_inputs, current_outputs
......@@ -2991,6 +3231,7 @@ def aten_rsub(mapper, graph, node):
%13 (int/float): 输入数值 y。
%7 (int/float): alpha。
"""
scope_name = mapper.normalize_scope_name(node)
output_name = mapper._get_outputs_name(node)[0]
layer_outputs = [output_name]
layer_inputs = {}
......@@ -2999,18 +3240,18 @@ def aten_rsub(mapper, graph, node):
# 获取当前节点输出的list
current_outputs = [output_name]
# 处理输入0,即%30
mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs)
mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs, scope_name)
layer_inputs["x"] = inputs_name[0]
# 处理输入1,即%13
mapper._check_input(graph, inputs_node[1], inputs_name[1], current_outputs)
mapper._check_input(graph, inputs_node[1], inputs_name[1], current_outputs, scope_name)
layer_inputs["y"] = inputs_name[1]
# 处理输入2,即%7
mapper._check_input(graph, inputs_node[2], inputs_name[2], current_outputs)
mapper._check_input(graph, inputs_node[2], inputs_name[2], current_outputs, scope_name)
layer_inputs["alpha"] = inputs_name[2]
# 获取当前节点输入的list
current_inputs = list(layer_inputs.values())
graph.add_layer("prim.rsub", inputs=layer_inputs, outputs=layer_outputs)
graph.add_layer("prim.rsub", inputs=layer_inputs, outputs=layer_outputs, scope_name=scope_name)
return current_inputs, current_outputs
......@@ -3025,6 +3266,7 @@ def aten_ScalarImplicit(mapper, graph, node):
【注意】由于Paddle无Scalar,所以最后转换为Tensor。
"""
scope_name = mapper.normalize_scope_name(node)
output_name = mapper._get_outputs_name(node)[0]
layer_outputs = [output_name]
layer_inputs = {}
......@@ -3033,14 +3275,14 @@ def aten_ScalarImplicit(mapper, graph, node):
# 获取当前节点输出的list
current_outputs = [output_name]
# 处理输入0,即%end.1
mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs)
mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs, scope_name)
layer_inputs["input"] = inputs_name[0]
input_type = list(node.inputs())[0].type()
# 获取当前节点输入的list
current_inputs = list(layer_inputs.values())
if str(input_type) == "Tensor":
graph.add_layer(
"prim.equal", inputs=layer_inputs, outputs=layer_outputs)
"prim.equal", inputs=layer_inputs, outputs=layer_outputs, scope_name=scope_name)
else:
raise Exception(
"The input type {} of aten::ScalarImplicit is not implemented yet!"
......@@ -3059,6 +3301,7 @@ def aten_select(mapper, graph, node):
%8 (int): select的维度。
%7 (int): select的第n个向量。
"""
scope_name = mapper.normalize_scope_name(node)
output_name = mapper._get_outputs_name(node)[0]
layer_outputs = [output_name]
layer_inputs = {}
......@@ -3067,12 +3310,12 @@ def aten_select(mapper, graph, node):
# 获取当前节点输出的list
current_outputs = [output_name]
# 处理输入0,即%18
mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs)
mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs, scope_name)
layer_inputs["input"] = inputs_name[0]
# 处理输入1,即%8
layer_attrs["dim"] = mapper.attrs[inputs_name[1]]
# 处理输入2,即%75
mapper._check_input(graph, inputs_node[2], inputs_name[2], current_outputs)
mapper._check_input(graph, inputs_node[2], inputs_name[2], current_outputs, scope_name)
layer_inputs["index"] = inputs_name[2]
# 获取当前节点输入的list
current_inputs = list(layer_inputs.values())
......@@ -3081,6 +3324,7 @@ def aten_select(mapper, graph, node):
"prim.select",
inputs=layer_inputs,
outputs=current_outputs,
scope_name=scope_name,
**layer_attrs)
return current_inputs, current_outputs
......@@ -3095,23 +3339,24 @@ def aten__set_item(mapper, graph, node):
%out_name.1 (-): dict的key。
%x.3 (-): dict的value。
"""
scope_name = mapper.normalize_scope_name(node)
layer_inputs = {}
inputs_name, inputs_node = mapper._get_inputs_name(node)
# 获取当前节点输出的list
current_outputs = []
# 处理输入0,即%features.1
mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs)
mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs, scope_name)
layer_inputs["dict"] = inputs_name[0]
# 处理输入1,即%out_name.1
mapper._check_input(graph, inputs_node[1], inputs_name[1], current_outputs)
mapper._check_input(graph, inputs_node[1], inputs_name[1], current_outputs, scope_name)
layer_inputs["key"] = inputs_name[1]
# 处理输入2,即%x.3
mapper._check_input(graph, inputs_node[2], inputs_name[2], current_outputs)
mapper._check_input(graph, inputs_node[2], inputs_name[2], current_outputs, scope_name)
layer_inputs["value"] = inputs_name[2]
# 获取当前节点输入的list
current_inputs = list(layer_inputs.values())
graph.add_layer("prim.set_item", inputs=layer_inputs, outputs=[])
graph.add_layer("prim.set_item", inputs=layer_inputs, outputs=[], scope_name=scope_name)
return current_inputs, current_outputs
......@@ -3124,20 +3369,22 @@ def aten_sigmoid(mapper, graph, node):
%55 (Tensor): 输出,sigmoid后的结果。
%54 (Tensor): 需要tanh的Tensor。
"""
scope_name = mapper.normalize_scope_name(node)
op_name = name_generator("sigmoid", mapper.nn_name2id)
output_name = mapper._get_outputs_name(node)[0]
layer_outputs = [output_name]
layer_outputs = [op_name, output_name]
layer_inputs = {}
inputs_name, inputs_node = mapper._get_inputs_name(node)
# 获取当前节点输出的list
current_outputs = [output_name]
# 处理输入0,即%54
mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs)
mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs, scope_name)
layer_inputs["x"] = inputs_name[0]
# 获取当前节点输入、输出的list
current_inputs = list(layer_inputs.values())
graph.add_layer(
"fluid.layers.sigmoid", inputs=layer_inputs, outputs=layer_outputs)
"paddle.nn.Sigmoid", inputs=layer_inputs, outputs=layer_outputs, scope_name=scope_name)
return current_inputs, current_outputs
......@@ -3150,6 +3397,7 @@ def aten_sin(mapper, graph, node):
%94 (Tensor): 输出,sin之后的结果。
%sinusoid_inp.1 (Tensor): 需要进行shape的Tensor。
"""
scope_name = mapper.normalize_scope_name(node)
output_name = mapper._get_outputs_name(node)[0]
layer_outputs = [output_name]
layer_inputs = {}
......@@ -3157,12 +3405,12 @@ def aten_sin(mapper, graph, node):
# 获取当前节点输出的list
current_outputs = [output_name]
# 处理输入0,即%sinusoid_inp.1
mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs)
mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs, scope_name)
layer_inputs["x"] = inputs_name[0]
# 获取当前节点输入、输出的list
current_inputs = list(layer_inputs.values())
graph.add_layer("paddle.sin", inputs=layer_inputs, outputs=layer_outputs)
graph.add_layer("paddle.sin", inputs=layer_inputs, outputs=layer_outputs, scope_name=scope_name)
return current_inputs, current_outputs
......@@ -3176,6 +3424,7 @@ def aten_size(mapper, graph, node):
%x.12 (Tensor): 需要获取shape的Tensor。
%10 (int): 非必须,代表维度。
"""
scope_name = mapper.normalize_scope_name(node)
output_name = mapper._get_outputs_name(node)[0]
layer_outputs = [output_name]
layer_inputs = {}
......@@ -3184,7 +3433,7 @@ def aten_size(mapper, graph, node):
# 获取当前节点输出的list
current_outputs = [output_name]
# 处理输入0,即%x.12
mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs)
mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs, scope_name)
layer_inputs["input"] = inputs_name[0]
# 获取当前节点输入的list
current_inputs = list(layer_inputs.values())
......@@ -3194,18 +3443,19 @@ def aten_size(mapper, graph, node):
layer_attrs["dim"] = mapper.attrs[inputs_name[1]]
else:
mapper._check_input(graph, inputs_node[1], inputs_name[1],
current_outputs)
current_outputs, scope_name)
layer_inputs["dim"] = inputs_name[1]
current_inputs.append(inputs_name[1])
graph.add_layer(
"prim.shape_dim",
inputs=layer_inputs,
outputs=layer_outputs,
scope_name=scope_name,
**layer_attrs)
return current_inputs, current_outputs
graph.add_layer(
"fluid.layers.shape", inputs=layer_inputs, outputs=layer_outputs)
"prim.shape", inputs=layer_inputs, outputs=layer_outputs, scope_name=scope_name)
return current_inputs, current_outputs
......@@ -3222,6 +3472,7 @@ def aten_slice(mapper, graph, node):
%75 (int): 切分的结束索引。
%77 (int): 切分的步长。
"""
scope_name = mapper.normalize_scope_name(node)
output_name = mapper._get_outputs_name(node)[0]
layer_outputs = [output_name]
layer_inputs = {}
......@@ -3231,8 +3482,8 @@ def aten_slice(mapper, graph, node):
if len(inputs_name) == 5:
# 处理输入0,即%73
mapper._check_input(graph, inputs_node[0], inputs_name[0],
current_outputs)
layer_inputs["input"] = inputs_name[0]
current_outputs, scope_name)
layer_inputs["x"] = inputs_name[0]
# 获取当前节点输入的list
current_inputs = list(layer_inputs.values())
......@@ -3242,14 +3493,16 @@ def aten_slice(mapper, graph, node):
"prim.list",
inputs={},
outputs=[inputs_name[1] + "_list"],
scope_name=scope_name,
input0=mapper.attrs[inputs_name[1]])
else:
mapper._check_input(graph, inputs_node[1], inputs_name[1],
current_outputs)
current_outputs, scope_name)
graph.add_layer(
"prim.list",
inputs={"input0": inputs_name[1]},
outputs=[inputs_name[1] + "_list"])
outputs=[inputs_name[1] + "_list"],
scope_name=scope_name)
current_inputs.append(inputs_name[1])
layer_inputs["axes"] = inputs_name[1] + "_list"
current_inputs.append(inputs_name[1] + "_list")
......@@ -3260,14 +3513,16 @@ def aten_slice(mapper, graph, node):
"prim.list",
inputs={},
outputs=[inputs_name[2] + "_list"],
scope_name=scope_name,
input0=mapper.attrs[inputs_name[2]])
else:
mapper._check_input(graph, inputs_node[2], inputs_name[2],
current_outputs)
current_outputs, scope_name)
graph.add_layer(
"prim.list",
inputs={"input0": inputs_name[2]},
outputs=[inputs_name[2] + "_list"])
outputs=[inputs_name[2] + "_list"],
scope_name=scope_name)
current_inputs.append(inputs_name[2])
layer_inputs["starts"] = inputs_name[2] + "_list"
current_inputs.append(inputs_name[2] + "_list")
......@@ -3278,14 +3533,16 @@ def aten_slice(mapper, graph, node):
"prim.list",
inputs={},
outputs=[inputs_name[3] + "_list"],
scope_name=scope_name,
input0=mapper.attrs[inputs_name[3]])
else:
mapper._check_input(graph, inputs_node[3], inputs_name[3],
current_outputs)
current_outputs, scope_name)
graph.add_layer(
"prim.list",
inputs={"input0": inputs_name[3]},
outputs=[inputs_name[3] + "_list"])
outputs=[inputs_name[3] + "_list"],
scope_name=scope_name)
current_inputs.append(inputs_name[3])
layer_inputs["ends"] = inputs_name[3] + "_list"
current_inputs.append(inputs_name[3] + "_list")
......@@ -3296,45 +3553,48 @@ def aten_slice(mapper, graph, node):
"prim.list",
inputs={},
outputs=[inputs_name[4] + "_list"],
scope_name=scope_name,
input0=mapper.attrs[inputs_name[4]])
else:
mapper._check_input(graph, inputs_node[4], inputs_name[4],
current_outputs)
current_outputs, scope_name)
graph.add_layer(
"prim.list",
inputs={"input0": inputs_name[4]},
outputs=[inputs_name[4] + "_list"])
outputs=[inputs_name[4] + "_list"],
scope_name=scope_name)
current_inputs.append(inputs_name[4])
layer_inputs["strides"] = inputs_name[4] + "_list"
current_inputs.append(inputs_name[4] + "_list")
current_outputs.append(inputs_name[4] + "_list")
graph.add_layer(
"fluid.layers.strided_slice",
"paddle.strided_slice",
inputs=layer_inputs,
outputs=layer_outputs)
outputs=layer_outputs,
scope_name=scope_name)
else:
# 处理输入0,即%73
mapper._check_input(graph, inputs_node[0], inputs_name[0],
current_outputs)
current_outputs, scope_name)
layer_inputs["input"] = inputs_name[0]
# 处理输入1,即%82
mapper._check_input(graph, inputs_node[1], inputs_name[1],
current_outputs)
current_outputs, scope_name)
layer_inputs["start"] = inputs_name[1]
# 处理输入2,即%75
mapper._check_input(graph, inputs_node[2], inputs_name[2],
current_outputs)
current_outputs, scope_name)
layer_inputs["end"] = inputs_name[2]
# 处理输入3,即%77
mapper._check_input(graph, inputs_node[3], inputs_name[3],
current_outputs)
current_outputs, scope_name)
layer_inputs["step"] = inputs_name[3]
# 获取当前节点输入的list
current_inputs = list(layer_inputs.values())
graph.add_layer(
"prim.slice", inputs=layer_inputs, outputs=layer_outputs)
"prim.slice", inputs=layer_inputs, outputs=layer_outputs, scope_name=scope_name)
return current_inputs, current_outputs
......@@ -3349,20 +3609,17 @@ def aten_softmax(mapper, graph, node):
%80 (int): 指定对输入Tensor进行运算的轴。
%72 (str): 类型,默认为None。
"""
if "softmax" in mapper.dygraph_name_id:
mapper.dygraph_name_id["softmax"] += 1
else:
mapper.dygraph_name_id["softmax"] = 0
softmax_name = "softmax" + str(mapper.dygraph_name_id["softmax"])
scope_name = mapper.normalize_scope_name(node)
op_name = name_generator("softmax", mapper.nn_name2id)
output_name = mapper._get_outputs_name(node)[0]
layer_outputs = [softmax_name, output_name]
layer_outputs = [op_name, output_name]
layer_inputs = {}
layer_attrs = {}
inputs_name, inputs_node = mapper._get_inputs_name(node)
# 获取当前节点输出的list
current_outputs = [output_name]
# 处理输入0,即%x.31
mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs)
mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs, scope_name)
layer_inputs["x"] = inputs_name[0]
# 获取当前节点输入的list
current_inputs = list(layer_inputs.values())
......@@ -3372,6 +3629,7 @@ def aten_softmax(mapper, graph, node):
"paddle.nn.Softmax",
inputs=layer_inputs,
outputs=layer_outputs,
scope_name=scope_name,
**layer_attrs)
return current_inputs, current_outputs
......@@ -3387,20 +3645,17 @@ def aten_softplus(mapper, graph, node):
%30 (int): beta。
%29 (int): 阈值。
"""
if "softplus" in mapper.dygraph_name_id:
mapper.dygraph_name_id["softplus"] += 1
else:
mapper.dygraph_name_id["softplus"] = 0
softplus_name = "softplus" + str(mapper.dygraph_name_id["softplus"])
scope_name = mapper.normalize_scope_name(node)
op_name = name_generator("softplus", mapper.nn_name2id)
output_name = mapper._get_outputs_name(node)[0]
layer_outputs = [softplus_name, output_name]
layer_outputs = [op_name, output_name]
layer_inputs = {}
layer_attrs = {}
inputs_name, inputs_node = mapper._get_inputs_name(node)
# 获取当前节点输出的list
current_outputs = [output_name]
# 处理输入0,即%x.31
mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs)
mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs, scope_name)
layer_inputs["x"] = inputs_name[0]
# 获取当前节点输入的list
current_inputs = list(layer_inputs.values())
......@@ -3411,6 +3666,7 @@ def aten_softplus(mapper, graph, node):
"paddle.nn.Softplus",
inputs=layer_inputs,
outputs=layer_outputs,
scope_name=scope_name,
**layer_attrs)
return current_inputs, current_outputs
......@@ -3424,6 +3680,7 @@ def aten_sqrt(mapper, graph, node):
%787 (Tensor): 输出,取sqrt的Tensor。
%786 (Tensor): 需要获取sqrt的Tensor。
"""
scope_name = mapper.normalize_scope_name(node)
output_name = mapper._get_outputs_name(node)[0]
layer_outputs = [output_name]
layer_inputs = {}
......@@ -3431,13 +3688,13 @@ def aten_sqrt(mapper, graph, node):
# 获取当前节点输出的list
current_outputs = [output_name]
# 处理输入0,即%786
mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs)
mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs, scope_name)
layer_inputs["x"] = inputs_name[0]
# 获取当前节点输入的list
current_inputs = list(layer_inputs.values())
graph.add_layer(
"fluid.layers.sqrt", inputs=layer_inputs, outputs=layer_outputs)
"paddle.sqrt", inputs=layer_inputs, outputs=layer_outputs, scope_name=scope_name)
return current_inputs, current_outputs
......@@ -3451,6 +3708,7 @@ def aten_squeeze(mapper, graph, node):
%start_logits.1 (Tensor): 需要删除维度的Tensor。
%4 (int): 维度。
"""
scope_name = mapper.normalize_scope_name(node)
output_name = mapper._get_outputs_name(node)[0]
layer_outputs = [output_name]
layer_inputs = {}
......@@ -3459,7 +3717,7 @@ def aten_squeeze(mapper, graph, node):
# 获取当前节点输出的list
current_outputs = [output_name]
# 处理输入0,即%start_logits.1
mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs)
mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs, scope_name)
layer_inputs["x"] = inputs_name[0]
# 获取当前节点输入的list
current_inputs = list(layer_inputs.values())
......@@ -3468,13 +3726,14 @@ def aten_squeeze(mapper, graph, node):
layer_attrs["axis"] = mapper.attrs[inputs_name[1]]
else:
mapper._check_input(graph, inputs_node[1], inputs_name[1],
current_outputs)
current_outputs, scope_name)
layer_inputs["axis"] = inputs_name[1]
current_inputs.append(inputs_name[1])
graph.add_layer(
"paddle.tensor.squeeze",
inputs=layer_inputs,
outputs=layer_outputs,
scope_name=scope_name,
**layer_attrs)
return current_inputs, current_outputs
......@@ -3489,6 +3748,7 @@ def aten_stack(mapper, graph, node):
%i.12 (Tensor): 需要堆叠的Tensor组成的Tensor。
%7 (int): 堆叠的轴。
"""
scope_name = mapper.normalize_scope_name(node)
output_name = mapper._get_outputs_name(node)[0]
layer_outputs = [output_name]
layer_inputs = {}
......@@ -3497,7 +3757,7 @@ def aten_stack(mapper, graph, node):
# 获取当前节点输出的list
current_outputs = [output_name]
# 处理输入0,即%13
mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs)
mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs, scope_name)
layer_inputs["x"] = inputs_name[0]
# 获取当前节点输入的list
current_inputs = list(layer_inputs.values())
......@@ -3506,13 +3766,14 @@ def aten_stack(mapper, graph, node):
layer_attrs["axis"] = mapper.attrs[inputs_name[1]]
else:
mapper._check_input(graph, inputs_node[1], inputs_name[1],
current_outputs)
current_outputs, scope_name)
layer_inputs["axis"] = inputs_name[1]
current_inputs.append(inputs_name[1])
graph.add_layer(
"paddle.stack",
inputs=layer_inputs,
outputs=layer_outputs,
scope_name=scope_name,
**layer_attrs)
return current_inputs, current_outputs
......@@ -3527,6 +3788,7 @@ def aten_sub(mapper, graph, node):
%839 (-): 输入数值 x。
%836 (-): 输入数值 y。
"""
scope_name = mapper.normalize_scope_name(node)
output_name = mapper._get_outputs_name(node)[0]
layer_outputs = [output_name]
layer_inputs = {}
......@@ -3534,16 +3796,16 @@ def aten_sub(mapper, graph, node):
# 获取当前节点输出的list
current_outputs = [output_name]
# 处理输入0,即%839
mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs)
mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs, scope_name)
layer_inputs["x"] = inputs_name[0]
# 处理输入1,即%836
mapper._check_input(
graph, inputs_node[1], inputs_name[1], current_outputs, add_dim=True)
graph, inputs_node[1], inputs_name[1], current_outputs, scope_name, add_dim=True)
layer_inputs["y"] = inputs_name[1]
# 获取当前节点输入的list
current_inputs = list(layer_inputs.values())
graph.add_layer("prim.sub", inputs=layer_inputs, outputs=layer_outputs)
graph.add_layer("prim.sub", inputs=layer_inputs, outputs=layer_outputs, scope_name=scope_name)
return current_inputs, current_outputs
......@@ -3556,6 +3818,7 @@ def aten_t(mapper, graph, node):
%109 (Tensor): 输出,转置后的矩阵。
%102 (Tensor): 需要转置的Tensor。
"""
scope_name = mapper.normalize_scope_name(node)
output_name = mapper._get_outputs_name(node)[0]
layer_outputs = [output_name]
layer_inputs = {}
......@@ -3563,15 +3826,16 @@ def aten_t(mapper, graph, node):
# 获取当前节点输出的list
current_outputs = [output_name]
# 处理输入0,即%x.12
mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs)
mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs, scope_name)
layer_inputs["x"] = inputs_name[0]
# 获取当前节点输入的list
current_inputs = list(layer_inputs.values())
graph.add_layer(
"fluid.layers.transpose",
"paddle.transpose",
inputs=layer_inputs,
outputs=layer_outputs,
scope_name=scope_name,
perm=[1, 0])
return current_inputs, current_outputs
......@@ -3585,25 +3849,22 @@ def aten_tanh(mapper, graph, node):
%55 (Tensor): 输出,tanh后的结果。
%54 (Tensor): 需要tanh的Tensor。
"""
if "tanh" in mapper.dygraph_name_id:
mapper.dygraph_name_id["tanh"] += 1
else:
mapper.dygraph_name_id["tanh"] = 0
tanh_name = "tanh" + str(mapper.dygraph_name_id["tanh"])
scope_name = mapper.normalize_scope_name(node)
op_name = name_generator("tanh", mapper.nn_name2id)
output_name = mapper._get_outputs_name(node)[0]
layer_outputs = [tanh_name, output_name]
layer_outputs = [op_name, output_name]
layer_inputs = {}
inputs_name, inputs_node = mapper._get_inputs_name(node)
# 获取当前节点输出的list
current_outputs = [output_name]
# 处理输入0,即%result.5
mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs)
mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs, scope_name)
layer_inputs["x"] = inputs_name[0]
# 获取当前节点输入、输出的list
current_inputs = list(layer_inputs.values())
graph.add_layer(
"paddle.nn.Tanh", inputs=layer_inputs, outputs=layer_outputs)
"paddle.nn.Tanh", inputs=layer_inputs, outputs=layer_outputs, scope_name=scope_name)
return current_inputs, current_outputs
......@@ -3618,6 +3879,7 @@ def aten_split(mapper, graph, node):
%135 (int): 分割的数量。
%723 (int): 轴。
"""
scope_name = mapper.normalize_scope_name(node)
output_name = mapper._get_outputs_name(node)[0]
layer_outputs = [output_name]
layer_inputs = {}
......@@ -3626,13 +3888,13 @@ def aten_split(mapper, graph, node):
# 获取当前节点输出的list
current_outputs = [output_name]
# 处理输入0,即%159
mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs)
layer_inputs["input"] = inputs_name[0]
mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs, scope_name)
layer_inputs["x"] = inputs_name[0]
# 处理输入2,即%723
mapper._check_input(graph, inputs_node[2], inputs_name[2], current_outputs)
layer_inputs["dim"] = inputs_name[2]
mapper._check_input(graph, inputs_node[2], inputs_name[2], current_outputs, scope_name)
layer_inputs["axis"] = inputs_name[2]
# 处理输入1,即%135
mapper._check_input(graph, inputs_node[1], inputs_name[1], current_outputs)
mapper._check_input(graph, inputs_node[1], inputs_name[1], current_outputs, scope_name)
input_type = list(node.inputs())[0].type()
if "[]" in str(input_type):
layer_inputs["num_or_sections"] = inputs_name[1]
......@@ -3642,9 +3904,10 @@ def aten_split(mapper, graph, node):
current_inputs = list(layer_inputs.values())
graph.add_layer(
"fluid.layers.split",
"paddle.split",
inputs=layer_inputs,
outputs=layer_outputs,
scope_name=scope_name,
**layer_attrs)
return current_inputs, current_outputs
......@@ -3660,6 +3923,7 @@ def aten_transpose(mapper, graph, node):
%704 (int): 转置的维度1。
%705 (int): 转置的维度2。
"""
scope_name = mapper.normalize_scope_name(node)
output_name = mapper._get_outputs_name(node)[0]
layer_outputs = [output_name]
layer_inputs = {}
......@@ -3668,43 +3932,48 @@ def aten_transpose(mapper, graph, node):
# 获取当前节点输出的list
current_outputs = [output_name]
# 处理输入0,即%x.21
mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs)
mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs, scope_name)
layer_inputs["x"] = inputs_name[0]
# 处理输入1,即%704
mapper._check_input(graph, inputs_node[1], inputs_name[1], current_outputs)
mapper._check_input(graph, inputs_node[1], inputs_name[1], current_outputs, scope_name)
dim1 = inputs_name[1]
# 处理输入2,即%705
mapper._check_input(graph, inputs_node[2], inputs_name[2], current_outputs)
mapper._check_input(graph, inputs_node[2], inputs_name[2], current_outputs, scope_name)
dim2 = inputs_name[2]
# 获取当前节点输入的list
current_inputs = list(layer_inputs.values())
current_inputs = list(layer_inputs.values())
graph.add_layer(
"fluid.layers.shape",
"prim.shape",
inputs={"input": inputs_name[0]},
outputs=[output_name + "_shape"])
outputs=[output_name + "_shape"],
scope_name=scope_name)
current_outputs.append(output_name + "_shape")
graph.add_layer(
"prim.len",
inputs={"input": output_name + "_shape"},
outputs=[output_name + "_len"])
outputs=[output_name + "_len"],
scope_name=scope_name)
current_outputs.append(output_name + "_len")
current_inputs.append(output_name + "_shape")
graph.add_layer(
"prim.len2list",
inputs={"len": output_name + "_len"},
outputs=[output_name + "_list"])
outputs=[output_name + "_list"],
scope_name=scope_name)
current_outputs.append(output_name + "_list")
current_inputs.append(output_name + "_len")
graph.add_layer(
"prim.check_dim",
inputs={"len": output_name + "_len",
"dim": dim1},
outputs=[dim1 + "_new"])
outputs=[dim1 + "_new"],
scope_name=scope_name)
graph.add_layer(
"prim.check_dim",
inputs={"len": output_name + "_len",
"dim": dim2},
outputs=[dim2 + "_new"])
outputs=[dim2 + "_new"],
scope_name=scope_name)
graph.add_layer(
"prim.replaceitem",
inputs={
......@@ -3712,7 +3981,8 @@ def aten_transpose(mapper, graph, node):
"index": dim1 + "_new",
"item": dim2 + "_new"
},
outputs=[])
outputs=[],
scope_name=scope_name)
graph.add_layer(
"prim.replaceitem",
inputs={
......@@ -3720,11 +3990,13 @@ def aten_transpose(mapper, graph, node):
"index": dim2 + "_new",
"item": dim1 + "_new"
},
outputs=[])
outputs=[],
scope_name=scope_name)
graph.add_layer(
"fluid.layers.transpose",
"paddle.transpose",
inputs=layer_inputs,
outputs=layer_outputs,
scope_name=scope_name,
perm=output_name + "_list")
return current_inputs, current_outputs
......@@ -3739,6 +4011,7 @@ def aten_to(mapper, graph, node):
%extended_attention_mask.1 (Tensor): 需要转换的Tensor。
%12 (int): 转换的类型。
"""
scope_name = mapper.normalize_scope_name(node)
output_name = mapper._get_outputs_name(node)[0]
layer_outputs = [output_name]
layer_inputs = {}
......@@ -3747,7 +4020,7 @@ def aten_to(mapper, graph, node):
# 获取当前节点输出的list
current_outputs = [output_name]
# 处理输入0,即%13
mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs)
mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs, scope_name)
layer_inputs["x"] = inputs_name[0]
# 获取当前节点输入的list
current_inputs = list(layer_inputs.values())
......@@ -3758,9 +4031,10 @@ def aten_to(mapper, graph, node):
layer_attrs["dtype"] = dtype_dict[mapper.attrs[inputs_name[1]]]
graph.add_layer(
"fluid.layers.cast",
"paddle.cast",
inputs=layer_inputs,
outputs=layer_outputs,
scope_name=scope_name,
**layer_attrs)
return current_inputs, current_outputs
......@@ -3775,6 +4049,7 @@ def aten_type_as(mapper, graph, node):
%56 (Tensor): 需要改变类型的Tensor。
%mask.1 (Tensor): 转换成与该Tensor相一致的类型。
"""
scope_name = mapper.normalize_scope_name(node)
output_name = mapper._get_outputs_name(node)[0]
layer_outputs = [output_name]
layer_inputs = {}
......@@ -3782,21 +4057,22 @@ def aten_type_as(mapper, graph, node):
# 获取当前节点输出的list
current_outputs = [output_name]
# 处理输入0,即%56
mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs)
mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs, scope_name)
layer_inputs["x"] = inputs_name[0]
# 获取当前节点输入的list
current_inputs = list(layer_inputs.values())
# 处理输入0,即%mask.1
mapper._check_input(graph, inputs_node[1], inputs_name[1], current_outputs)
mapper._check_input(graph, inputs_node[1], inputs_name[1], current_outputs, scope_name)
graph.add_layer(
"prim.type",
inputs={"input": inputs_name[1]},
outputs=[inputs_name[1] + "_type"])
outputs=[inputs_name[1] + "_type"],
scope_name=scope_name)
layer_inputs["dtype"] = inputs_name[1] + "_type"
current_inputs.append(inputs_name[1])
graph.add_layer(
"fluid.layers.cast", inputs=layer_inputs, outputs=layer_outputs)
"paddle.cast", inputs=layer_inputs, outputs=layer_outputs, scope_name=scope_name)
return current_inputs, current_outputs
......@@ -3810,6 +4086,7 @@ def aten_unsqueeze(mapper, graph, node):
%12 (Tensor): 需要插入维度的Tensor。
%7 (int): 维度。
"""
scope_name = mapper.normalize_scope_name(node)
output_name = mapper._get_outputs_name(node)[0]
layer_outputs = [output_name]
layer_inputs = {}
......@@ -3818,7 +4095,7 @@ def aten_unsqueeze(mapper, graph, node):
# 获取当前节点输出的list
current_outputs = [output_name]
# 处理输入0,即%13
mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs)
mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs, scope_name)
layer_inputs["x"] = inputs_name[0]
# 获取当前节点输入的list
current_inputs = list(layer_inputs.values())
......@@ -3827,13 +4104,14 @@ def aten_unsqueeze(mapper, graph, node):
layer_attrs["axis"] = mapper.attrs[inputs_name[1]]
else:
mapper._check_input(graph, inputs_node[1], inputs_name[1],
current_outputs)
current_outputs, scope_name)
layer_inputs["axis"] = inputs_name[1]
current_inputs.append(inputs_name[1])
graph.add_layer(
"paddle.tensor.unsqueeze",
"paddle.unsqueeze",
inputs=layer_inputs,
outputs=layer_outputs,
scope_name=scope_name,
**layer_attrs)
return current_inputs, current_outputs
......@@ -3851,6 +4129,7 @@ def aten_upsample_bilinear2d(mapper, graph, node):
%4995 (float): 高度的乘数因子。
%4995 (float): 宽度的乘数因子。
"""
scope_name = mapper.normalize_scope_name(node)
output_name = mapper._get_outputs_name(node)[0]
layer_outputs = [output_name]
layer_inputs = {}
......@@ -3859,7 +4138,7 @@ def aten_upsample_bilinear2d(mapper, graph, node):
# 获取当前节点输出的list
current_outputs = [output_name]
# 处理输入0,即%x.13
mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs)
mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs, scope_name)
layer_inputs["x"] = inputs_name[0]
# 获取当前节点输入的list
current_inputs = list(layer_inputs.values())
......@@ -3868,25 +4147,29 @@ def aten_upsample_bilinear2d(mapper, graph, node):
layer_attrs["size"] = mapper.attrs[inputs_name[1]]
else:
mapper._check_input(graph, inputs_node[1], inputs_name[1],
current_outputs)
current_outputs, scope_name)
layer_inputs["size"] = inputs_name[1]
current_inputs.append(inputs_name[1])
graph.add_layer(
"prim.isinstance",
inputs={"input": inputs_name[1]},
outputs=[inputs_name[1] + "_isinstance"],
scope_name=scope_name,
cls="paddle.fluid.Variable")
# TODO(syf): paddle.Variable
graph.add_layer(
"prim.if", {"input": inputs_name[1] + "_isinstance"},
outputs=[inputs_name[0] + "_if1"])
outputs=[inputs_name[0] + "_if1"],
scope_name=scope_name)
if_layer = graph.layers[list(graph.layers.keys())[-1]]
block = PaddleGraph(if_layer, graph_type="dygraph")
block = PaddleGraph(parent_layer=if_layer, graph_type="dygraph")
block.add_layer(
"prim.var2list",
inputs={"input": inputs_name[1]},
outputs=[inputs_name[1]])
outputs=[inputs_name[1]],
scope_name=scope_name)
if_layer.add_block(block)
block = PaddleGraph(if_layer, graph_type="dygraph")
block = PaddleGraph(parent_layer=if_layer, graph_type="dygraph")
if_layer.add_block(block)
if_layer.inputs["input-0"] = inputs_name[1]
# 处理输入2,即%5421
......@@ -3894,22 +4177,23 @@ def aten_upsample_bilinear2d(mapper, graph, node):
layer_attrs["align_corners"] = mapper.attrs[inputs_name[2]]
else:
mapper._check_input(graph, inputs_node[2], inputs_name[2],
current_outputs)
current_outputs, scope_name)
layer_inputs["align_corners"] = inputs_name[2]
current_inputs.append(inputs_name[2])
# 处理输入3和4,构造assert
list_layer_inputs = {}
mapper._check_input(graph, inputs_node[3], inputs_name[3], current_outputs)
list_layer_inputs["key"] = inputs_name[3]
current_inputs.append(inputs_name[3])
mapper._check_input(graph, inputs_node[4], inputs_name[4], current_outputs)
list_layer_inputs["value"] = inputs_name[4]
current_inputs.append(inputs_name[4])
graph.add_layer(
"prim.assert",
inputs=list_layer_inputs,
outputs=[output_name + "_assert"],
type="eq")
# # 处理输入3和4,构造assert
# list_layer_inputs = {}
# mapper._check_input(graph, inputs_node[3], inputs_name[3], current_outputs, scope_name)
# list_layer_inputs["key"] = inputs_name[3]
# current_inputs.append(inputs_name[3])
# mapper._check_input(graph, inputs_node[4], inputs_name[4], current_outputs, scope_name)
# list_layer_inputs["value"] = inputs_name[4]
# current_inputs.append(inputs_name[4])
# graph.add_layer(
# "prim.assert",
# inputs=list_layer_inputs,
# outputs=[output_name + "_assert"],
# scope_name=scope_name,
# type="eq")
layer_inputs["scale_factor"] = inputs_name[3]
layer_attrs["align_mode"] = 0
layer_attrs["mode"] = string("bilinear")
......@@ -3917,10 +4201,37 @@ def aten_upsample_bilinear2d(mapper, graph, node):
"paddle.nn.functional.interpolate",
inputs=layer_inputs,
outputs=layer_outputs,
scope_name=scope_name,
**layer_attrs)
return current_inputs, current_outputs
def aten_values(mapper, graph, node):
""" 构造对比大小的PaddleLayer。
TorchScript示例:
%5 : Float(1, *, 1024, 2048)[] = aten::values(%1)
参数含义:
%5 (list): 输出,由字典获取的values的list。
%1 (dict): 字典。
"""
scope_name = mapper.normalize_scope_name(node)
output_name = mapper._get_outputs_name(node)[0]
layer_outputs = [output_name]
layer_inputs = {}
inputs_name, inputs_node = mapper._get_inputs_name(node)
# 获取当前节点输出的list
current_outputs = [output_name]
# 处理输入0,即%78
mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs, scope_name)
layer_inputs["x"] = inputs_name[0]
# 获取当前节点输入的list
current_inputs = list(layer_inputs.values())
graph.add_layer("prim.dict2values", inputs=layer_inputs, outputs=layer_outputs, scope_name=scope_name)
return current_inputs, current_outputs
def aten_view(mapper, graph, node):
""" 构造调整大小的PaddleLayer。
......@@ -3937,6 +4248,7 @@ def aten_view(mapper, graph, node):
此时就不能再调用view函数。因此,需要先使用contiguous来返回一个contiguous copy。
reshape则不需要依赖目标Tensor是否在内存中是连续的。
"""
scope_name = mapper.normalize_scope_name(node)
output_name = mapper._get_outputs_name(node)[0]
layer_outputs = [output_name]
layer_inputs = {}
......@@ -3945,7 +4257,7 @@ def aten_view(mapper, graph, node):
# 获取当前节点输出的list
current_outputs = [output_name]
# 处理输入0,即%x.20
mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs)
mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs, scope_name)
layer_inputs["x"] = inputs_name[0]
# 获取当前节点输入、输出的list
current_inputs = list(layer_inputs.values())
......@@ -3954,55 +4266,15 @@ def aten_view(mapper, graph, node):
layer_attrs["shape"] = mapper.attrs[inputs_name[1]]
else:
mapper._check_input(graph, inputs_node[1], inputs_name[1],
current_outputs)
current_outputs, scope_name)
layer_inputs["shape"] = inputs_name[1]
current_inputs.append(inputs_name[1])
graph.add_layer(
"prim.type",
inputs={"input": inputs_name[0]},
outputs=[inputs_name[0] + "_type"])
graph.add_layer(
"prim.str",
inputs={"input": inputs_name[0] + "_type"},
outputs=[inputs_name[0] + "_type"])
graph.add_layer(
"prim.eq",
inputs={"x": inputs_name[0] + "_type"},
outputs=[inputs_name[0] + "_cond"],
y=string("VarType.BOOL"))
graph.add_layer(
"prim.if", {'input': inputs_name[0] + "_cond"},
outputs=[inputs_name[0] + "_if1"])
if_layer = graph.layers[list(graph.layers.keys())[-1]]
block = PaddleGraph(if_layer, graph_type="dygraph")
block.add_layer(
"fluid.layers.cast",
inputs={"x": inputs_name[0]},
outputs=[inputs_name[0]],
dtype=string("int32"))
if_layer.add_block(block)
block = PaddleGraph(if_layer, graph_type="dygraph")
if_layer.add_block(block)
if_layer.inputs["input-0"] = inputs_name[0]
graph.add_layer(
"fluid.layers.reshape",
"paddle.reshape",
inputs=layer_inputs,
outputs=layer_outputs,
scope_name=scope_name,
**layer_attrs)
graph.add_layer(
"prim.if", {'input': inputs_name[0] + "_cond"},
outputs=[inputs_name[0] + "_if2"])
if_layer = graph.layers[list(graph.layers.keys())[-1]]
block = PaddleGraph(if_layer, graph_type="dygraph")
block.add_layer(
"fluid.layers.cast",
inputs={"x": layer_outputs[0]},
outputs=layer_outputs,
dtype=string("bool"))
if_layer.add_block(block)
block = PaddleGraph(if_layer, graph_type="dygraph")
if_layer.add_block(block)
if_layer.inputs["input-0"] = layer_outputs[0]
return current_inputs, current_outputs
......@@ -4015,6 +4287,7 @@ def aten_warn(mapper, graph, node):
%3 (str): warning的提示字符串。
%2 (int): warning的stacklevel。
"""
scope_name = mapper.normalize_scope_name(node)
output_name = mapper._get_outputs_name(node)[0]
layer_outputs = [output_name]
layer_inputs = {}
......@@ -4023,7 +4296,7 @@ def aten_warn(mapper, graph, node):
# 获取当前节点输出的list
current_outputs = [output_name]
# 处理输入0,即%3
mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs)
mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs, scope_name)
layer_inputs["input"] = inputs_name[0]
# 获取当前节点输入的list
current_inputs = list(layer_inputs.values())
......@@ -4032,7 +4305,7 @@ def aten_warn(mapper, graph, node):
layer_attrs["stacklevel"] = mapper.attrs[inputs_name[1]]
else:
mapper._check_input(graph, inputs_node[1], inputs_name[1],
current_outputs)
current_outputs, scope_name)
layer_inputs["stacklevel"] = inputs_name[1]
current_inputs.append(inputs_name[1])
......@@ -4040,6 +4313,7 @@ def aten_warn(mapper, graph, node):
"prim.warnings",
inputs=layer_inputs,
outputs=layer_outputs,
scope_name=scope_name,
**layer_attrs)
return current_inputs, current_outputs
......@@ -4055,6 +4329,7 @@ def aten_where(mapper, graph, node):
%w0.2 (Tensor): 输入数值 x。
%210 (Tensor): 输入数值 y。
"""
scope_name = mapper.normalize_scope_name(node)
output_name = mapper._get_outputs_name(node)[0]
layer_outputs = [output_name]
layer_inputs = {}
......@@ -4062,18 +4337,18 @@ def aten_where(mapper, graph, node):
# 获取当前节点输出的list
current_outputs = [output_name]
# 处理输入0,即%209
mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs)
mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs, scope_name)
layer_inputs["condition"] = inputs_name[0]
# 处理输入1,即%w0.2
mapper._check_input(graph, inputs_node[1], inputs_name[1], current_outputs)
mapper._check_input(graph, inputs_node[1], inputs_name[1], current_outputs, scope_name)
layer_inputs["x"] = inputs_name[1]
# 处理输入1,即%w0.2
mapper._check_input(graph, inputs_node[2], inputs_name[2], current_outputs)
mapper._check_input(graph, inputs_node[2], inputs_name[2], current_outputs, scope_name)
layer_inputs["y"] = inputs_name[2]
# 获取当前节点输入的list
current_inputs = list(layer_inputs.values())
graph.add_layer("paddle.where", inputs=layer_inputs, outputs=layer_outputs)
graph.add_layer("paddle.where", inputs=layer_inputs, outputs=layer_outputs, scope_name=scope_name)
return current_inputs, current_outputs
......@@ -4090,6 +4365,7 @@ def aten_zeros(mapper, graph, node):
%4995 (Device): 设备。
%4995 (bool): 是否计算梯度。
"""
scope_name = mapper.normalize_scope_name(node)
output_name = mapper._get_outputs_name(node)[0]
layer_outputs = [output_name]
layer_inputs = {}
......@@ -4103,7 +4379,7 @@ def aten_zeros(mapper, graph, node):
layer_attrs["shape"] = mapper.attrs[inputs_name[0]]
else:
mapper._check_input(graph, inputs_node[0], inputs_name[0],
current_outputs)
current_outputs, scope_name)
layer_inputs["shape"] = inputs_name[0]
current_inputs.append(inputs_name[0])
# 处理输入1,即%8,代表dtype
......@@ -4113,6 +4389,7 @@ def aten_zeros(mapper, graph, node):
"paddle.zeros",
inputs=layer_inputs,
outputs=layer_outputs,
scope_name=scope_name,
**layer_attrs)
return current_inputs, current_outputs
......@@ -4131,6 +4408,7 @@ def aten_zeros_like(mapper, graph, node):
%671 (bool): 是否计算梯度。
%672 (memory_format): 存储类型。
"""
scope_name = mapper.normalize_scope_name(node)
output_name = mapper._get_outputs_name(node)[0]
layer_outputs = [output_name]
layer_inputs = {}
......@@ -4139,7 +4417,7 @@ def aten_zeros_like(mapper, graph, node):
# 获取当前节点输出的list
current_outputs = [output_name]
# 处理输入0,即%n.2
mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs)
mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs, scope_name)
layer_inputs["x"] = inputs_name[0]
# 获取当前节点输入的list
current_inputs = list(layer_inputs.values())
......@@ -4150,5 +4428,6 @@ def aten_zeros_like(mapper, graph, node):
"paddle.zeros_like",
inputs=layer_inputs,
outputs=layer_outputs,
scope_name=scope_name,
**layer_attrs)
return current_inputs, current_outputs
......@@ -25,6 +25,7 @@ def prim_Constant(mapper, graph, node):
参数含义:
%2 (常量类型由赋值类型定义,该示例中为int型): 常量赋值结果输出。
"""
scope_name = mapper.normalize_scope_name(node)
output_name = mapper._get_outputs_name(node)[0]
output = list(node.outputs())[0]
value = output.toIValue()
......@@ -32,7 +33,10 @@ def prim_Constant(mapper, graph, node):
if isinstance(value, str):
value = string(value)
if str(output_type) == "Tensor":
tensor_value = value
value = "{}".format(value)
if "tensor" in value:
mapper.pytorch_params[output_name] = tensor_value.cpu().detach().numpy()
if "inf" in str(value):
t = str(type(value)).split("'")[1]
......@@ -45,7 +49,7 @@ def prim_Constant(mapper, graph, node):
value = int(math.pow(2, 31) - 1)
mapper.attrs[output_name] = value
graph.add_layer(
"prim.constant", inputs={}, outputs=[output_name], value=value)
"prim.constant", inputs={}, outputs=[output_name], scope_name=scope_name, value=value)
return [], [output_name]
......@@ -60,6 +64,7 @@ def prim_data(mapper, graph, node):
【注意】Paddle中无此用法,所以此处翻译成赋值。
"""
scope_name = mapper.normalize_scope_name(node)
output_name = mapper._get_outputs_name(node)[0]
layer_outputs = [output_name]
layer_inputs = {}
......@@ -68,15 +73,53 @@ def prim_data(mapper, graph, node):
# 获取当前节点输出的list
current_outputs = [output_name]
# 处理输入0,即%4336
mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs)
mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs, scope_name)
layer_inputs["input"] = inputs_name[0]
# 获取当前节点输入的list
current_inputs = list(layer_inputs.values())
graph.add_layer("prim.equal", inputs=layer_inputs, outputs=layer_outputs)
graph.add_layer("prim.equal", inputs=layer_inputs, outputs=layer_outputs, scope_name=scope_name)
return current_inputs, current_outputs
def prim_DictConstruct(mapper, graph, node):
""" 构建dict。
TorchScript示例:
%32 : Dict(str, Tensor) = prim::DictConstruct(%30, %23, %31, %29)
参数含义:
%32 (dict): 组成的字典。
%30 (str): key。
%23 (-): value。
%31 (str): key。
%29 (-): value。
"""
scope_name = mapper.normalize_scope_name(node)
output_name = mapper._get_outputs_name(node)[0]
layer_outputs = [output_name]
layer_inputs = {}
layer_attrs = {}
inputs_name, inputs_node = mapper._get_inputs_name(node)
# 获取当前节点输出的list
current_outputs = [output_name]
# 处理每个输入
for i, input_name in enumerate(inputs_name):
if i%2 == 0:
layer_attrs["key{}".format(int(i/2))] = mapper.attrs[input_name]
else:
layer_inputs["value{}".format(int(i/2))] = input_name
# 获取当前节点输入的list
current_inputs = list(layer_inputs.values())
graph.add_layer("prim.dict_construct",
inputs=layer_inputs,
outputs=layer_outputs,
scope_name=scope_name,
**layer_attrs)
return current_inputs, current_outputs
def prim_GetAttr(mapper, graph, node):
""" 获取attribute信息。
......@@ -86,6 +129,7 @@ def prim_GetAttr(mapper, graph, node):
%7 (Tensor): 输入Tensor。
%27 (Tensor): 输入Tensor。
"""
scope_name = mapper.normalize_scope_name(node)
current_node = node
field_name_list = [node.s('name')]
while True:
......@@ -102,7 +146,7 @@ def prim_GetAttr(mapper, graph, node):
if hasattr(part_script, field_name):
param = getattr(part_script, field_name)
if isinstance(param, torch.Tensor):
param = param.detach().numpy()
param = param.cpu().detach().numpy()
if len(param.shape) == 0:
param = np.reshape(param, 1)
if str(param.dtype) == "uint8":
......@@ -129,14 +173,15 @@ def prim_If(mapper, graph, node):
%107 (bool): if判断条件。
%input.5 (Tensor): if控制流的输出,与%output.4对应。
"""
scope_name = mapper.normalize_scope_name(node)
outputs_name = mapper._get_outputs_name(node)
node_outputs = outputs_name.copy()
current_outputs = outputs_name.copy()
input_node = list(node.inputs())[0].node()
script_input_unique_id = list(node.inputs())[0].unique()
input_node_name = mapper.outputs_info[script_input_unique_id]
mapper._check_input(graph, input_node, input_node_name, current_outputs)
graph.add_layer("prim.if", {'input': input_node_name}, node_outputs)
mapper._check_input(graph, input_node, input_node_name, current_outputs, scope_name)
graph.add_layer("prim.if", inputs={'input': input_node_name}, outputs=node_outputs, scope_name=scope_name)
current_layer = list(graph.layers.values())[-1]
block0 = list(node.blocks())[0]
block0_graph, graph_inputs0 = mapper.traverse(block0, current_layer)
......@@ -163,6 +208,7 @@ def prim_ListConstruct(mapper, graph, node):
%84 (int/其他): list第一个元素信息。
%85 (int/其他): list第二个元素信息。
"""
scope_name = mapper.normalize_scope_name(node)
output_name = mapper._get_outputs_name(node)[0]
layer_outputs = [output_name]
layer_inputs = {}
......@@ -175,7 +221,7 @@ def prim_ListConstruct(mapper, graph, node):
# 获取当前节点输入的list
current_inputs = list(layer_inputs.values())
graph.add_layer("prim.list", inputs=layer_inputs, outputs=layer_outputs)
graph.add_layer("prim.list", inputs=layer_inputs, outputs=layer_outputs, scope_name=scope_name)
return current_inputs, current_outputs
......@@ -189,6 +235,7 @@ def prim_ListUnpack(mapper, graph, node):
%x2.4 (Tensor): 输出,list的第二个元素。
%4354 (list): 列表。
"""
scope_name = mapper.normalize_scope_name(node)
outputs_name = mapper._get_outputs_name(node)
layer_outputs = outputs_name.copy()
layer_inputs = {}
......@@ -196,13 +243,13 @@ def prim_ListUnpack(mapper, graph, node):
# 获取当前节点输出的list
current_outputs = layer_outputs.copy()
# 处理输入0,即%4354
mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs)
mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs, scope_name)
layer_inputs["input"] = inputs_name[0]
# 获取当前节点输入的list
current_inputs = list(layer_inputs.values())
graph.add_layer(
"prim.list_unpack", inputs=layer_inputs, outputs=layer_outputs)
"prim.list_unpack", inputs=layer_inputs, outputs=layer_outputs, scope_name=scope_name)
mapper.split_len[list(layer_inputs.values())[0]] = len(layer_outputs)
return current_inputs, current_outputs
......@@ -223,6 +270,7 @@ def prim_Loop(mapper, graph, node):
%x.3 (Tensor): 循环中修改的Tensor。
%x (Tensor): loop循环的输出,与%x.5对应。
"""
scope_name = mapper.normalize_scope_name(node)
node_outputs = mapper._get_outputs_name(node)
loop_inputs = {}
block = list(node.blocks())[0]
......@@ -242,7 +290,7 @@ def prim_Loop(mapper, graph, node):
loop_input_node_name = mapper.outputs_info[
script_loop_input_unique_id]
mapper._check_input(graph, loop_input_node, loop_input_node_name,
node_outputs)
node_outputs, scope_name)
loop_inputs['input'] = loop_input_node_name
loop_outputs.append(block_input_node_name)
node_outputs.append(block_input_node_name)
......@@ -252,14 +300,15 @@ def prim_Loop(mapper, graph, node):
loop_input_node_name = mapper.outputs_info[
script_loop_input_unique_id]
mapper._check_input(graph, loop_input_node, loop_input_node_name,
node_outputs)
node_outputs, scope_name)
graph.add_layer(
"prim.equal",
inputs={'input': loop_input_node_name},
outputs=[block_input_node_name])
outputs=[block_input_node_name],
scope_name=scope_name)
node_outputs.append(block_input_node_name)
graph.add_layer("prim.loop", inputs=loop_inputs, outputs=loop_outputs)
graph.add_layer("prim.loop", inputs=loop_inputs, outputs=loop_outputs, scope_name=scope_name)
current_layer = list(graph.layers.values())[-1]
block_graph, graph_inputs = mapper.traverse(block, current_layer)
for i, input_name in enumerate(graph_inputs):
......@@ -279,6 +328,7 @@ def prim_min(mapper, graph, node):
%86 (list): 输入。
%87 (int): 输出。
"""
scope_name = mapper.normalize_scope_name(node)
output_name = mapper._get_outputs_name(node)[0]
layer_outputs = [output_name]
layer_inputs = {}
......@@ -286,12 +336,12 @@ def prim_min(mapper, graph, node):
# 获取当前节点输出的list
current_outputs = [output_name]
# 处理输入0,即%86
mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs)
mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs, scope_name)
layer_inputs["input"] = inputs_name[0]
# 获取当前节点输入的list
current_inputs = list(layer_inputs.values())
graph.add_layer("prim.min", inputs=layer_inputs, outputs=layer_outputs)
graph.add_layer("prim.min", inputs=layer_inputs, outputs=layer_outputs, scope_name=scope_name)
return current_inputs, current_outputs
......@@ -304,6 +354,7 @@ def prim_NumToTensor(mapper, graph, node):
%other.2 (Tensor): 输出。
%1736 (-): 输入。
"""
scope_name = mapper.normalize_scope_name(node)
output_name = mapper._get_outputs_name(node)[0]
layer_outputs = [output_name]
layer_inputs = {}
......@@ -312,25 +363,26 @@ def prim_NumToTensor(mapper, graph, node):
# 获取当前节点输出的list
current_outputs = [output_name]
# 处理输入0,即%86
mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs)
if inputs_node[0].kind() == "aten::size":
mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs, scope_name)
inputs_inputs_name, inputs_inputs_node = mapper._get_inputs_name(inputs_node[0])
if inputs_node[0].kind() == "aten::size" and len(inputs_inputs_name) > 1:
layer_inputs["input"] = inputs_name[0]
# 获取当前节点输入的list
current_inputs = list(layer_inputs.values())
graph.add_layer(
"prim_equal", inputs=layer_inputs, outputs=layer_outputs)
"prim_equal", inputs=layer_inputs, outputs=layer_outputs, scope_name=scope_name)
else:
layer_inputs["value"] = inputs_name[0]
layer_inputs["fill_value"] = inputs_name[0]
# 获取当前节点输入的list
current_inputs = list(layer_inputs.values())
input_type = list(node.inputs())[0].type()
layer_attrs["dtype"] = input_type
layer_attrs["persistable"] = True
layer_attrs["shape"] = [1]
graph.add_layer(
"fluid.layers.create_global_var",
"paddle.full",
inputs=layer_inputs,
outputs=layer_outputs,
scope_name=scope_name,
**layer_attrs)
return current_inputs, current_outputs
......@@ -343,6 +395,7 @@ def prim_RaiseException(mapper, graph, node):
参数含义:
%76 (str): 异常信息。
"""
scope_name = mapper.normalize_scope_name(node)
output_name = mapper._get_outputs_name(node)[0]
layer_outputs = [output_name]
layer_inputs = {}
......@@ -350,13 +403,13 @@ def prim_RaiseException(mapper, graph, node):
# 获取当前节点输出的list
current_outputs = [output_name]
# 处理输入0,即%76
mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs)
mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs, scope_name)
layer_inputs["input"] = inputs_name[0]
# 获取当前节点输入的list
current_inputs = list(layer_inputs.values())
graph.add_layer(
"prim.exception", inputs=layer_inputs, outputs=layer_outputs)
"prim.exception", inputs=layer_inputs, outputs=layer_outputs, scope_name=scope_name)
return current_inputs, current_outputs
......@@ -369,6 +422,7 @@ def prim_requires_grad(mapper, graph, node):
%356 (bool): 输出,当前Tensor是否计算梯度。
%tensor.31 (Tensor): 输入的Tensor。
"""
scope_name = mapper.normalize_scope_name(node)
output_name = mapper._get_outputs_name(node)[0]
layer_outputs = [output_name]
layer_inputs = {}
......@@ -376,13 +430,13 @@ def prim_requires_grad(mapper, graph, node):
# 获取当前节点输出的list
current_outputs = [output_name]
# 处理输入0,即%86
mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs)
mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs, scope_name)
layer_inputs["input"] = inputs_name[0]
# 获取当前节点输入的list
current_inputs = list(layer_inputs.values())
graph.add_layer(
"prim.requires_grad", inputs=layer_inputs, outputs=layer_outputs)
"prim.requires_grad", inputs=layer_inputs, outputs=layer_outputs, scope_name=scope_name)
return current_inputs, current_outputs
......@@ -395,6 +449,7 @@ def prim_SetAttr(mapper, graph, node):
%260 (-): 属性名前缀。
%277 (-): 需要设置的值。
"""
scope_name = mapper.normalize_scope_name(node)
output_name = mapper._get_outputs_name(node)[0]
field_name_list = []
tmp_node = node
......@@ -416,7 +471,8 @@ def prim_SetAttr(mapper, graph, node):
graph.add_layer(
"prim.set_attr",
inputs={"input": inputs_name[1]},
outputs=["self." + ".".join(field_name_list).replace(".", "_")])
outputs=["self." + ".".join(field_name_list).replace(".", "_")],
scope_name=scope_name)
return [], [output_name]
......@@ -429,6 +485,7 @@ def prim_shape(mapper, graph, node):
%4701 (list): 输出,shape信息。
%result.1 (Tensor): 需要获取shape的值。
"""
scope_name = mapper.normalize_scope_name(node)
output_name = mapper._get_outputs_name(node)[0]
layer_outputs = [output_name]
layer_inputs = {}
......@@ -436,13 +493,13 @@ def prim_shape(mapper, graph, node):
# 获取当前节点输出的list
current_outputs = [output_name]
# 处理输入0,即%input.8
mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs)
mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs, scope_name)
layer_inputs["input"] = inputs_name[0]
# 获取当前节点输入的list
current_inputs = list(layer_inputs.values())
graph.add_layer(
"fluid.layers.shape", inputs=layer_inputs, outputs=layer_outputs)
"paddle.shape", inputs=layer_inputs, outputs=layer_outputs, scope_name=scope_name)
return current_inputs, current_outputs
......@@ -456,6 +513,7 @@ def prim_TupleConstruct(mapper, graph, node):
%x.46 (Tensor/其他): tuple第一个元素信息。
%aux (Tensor/其他): tuple第二个元素信息。
"""
scope_name = mapper.normalize_scope_name(node)
output_name = mapper._get_outputs_name(node)[0]
layer_outputs = [output_name]
layer_inputs = {}
......@@ -468,7 +526,7 @@ def prim_TupleConstruct(mapper, graph, node):
# 获取当前节点输入的list
current_inputs = list(layer_inputs.values())
graph.add_layer("prim.tuple", inputs=layer_inputs, outputs=layer_outputs)
graph.add_layer("prim.tuple", inputs=layer_inputs, outputs=layer_outputs, scope_name=scope_name)
return current_inputs, current_outputs
......@@ -482,6 +540,7 @@ def prim_TupleUnpack(mapper, graph, node):
%aux.3 (Tensor/其他): 输出,tuple第二个元素信息。
%4492 (tuple): 需要获取元素的tuple。
"""
scope_name = mapper.normalize_scope_name(node)
outputs_name = mapper._get_outputs_name(node)
layer_outputs = outputs_name
layer_inputs = {}
......@@ -493,7 +552,7 @@ def prim_TupleUnpack(mapper, graph, node):
current_inputs = list(layer_inputs.values())
graph.add_layer(
"prim.tuple_unpack", inputs=layer_inputs, outputs=layer_outputs)
"prim.tuple_unpack", inputs=layer_inputs, outputs=layer_outputs, scope_name=scope_name)
return current_inputs, current_outputs
......@@ -508,6 +567,7 @@ def prim_unchecked_cast(mapper, graph, node):
【注意】Paddle中无此用法,所以此处翻译成赋值。
"""
scope_name = mapper.normalize_scope_name(node)
output_name = mapper._get_outputs_name(node)[0]
layer_outputs = [output_name]
layer_inputs = {}
......@@ -516,12 +576,12 @@ def prim_unchecked_cast(mapper, graph, node):
# 获取当前节点输出的list
current_outputs = [output_name]
# 处理输入0,即%size.63
mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs)
mapper._check_input(graph, inputs_node[0], inputs_name[0], current_outputs, scope_name)
layer_inputs["input"] = inputs_name[0]
# 获取当前节点输入的list
current_inputs = list(layer_inputs.values())
graph.add_layer("prim.equal", inputs=layer_inputs, outputs=layer_outputs)
graph.add_layer("prim.equal", inputs=layer_inputs, outputs=layer_outputs, scope_name=scope_name)
return current_inputs, current_outputs
......@@ -533,9 +593,10 @@ def prim_Uninitialized(mapper, graph, node):
参数含义:
%345 (bool): 输出,为赋值的bool。
"""
scope_name = mapper.normalize_scope_name(node)
output_name = mapper._get_outputs_name(node)[0]
output = list(node.outputs())[0]
mapper.attrs[output_name] = None
graph.add_layer(
"prim.constant", inputs={}, outputs=[output_name], value=None)
"prim.constant", inputs={}, outputs=[output_name], scope_name=scope_name, value=None)
return [], [output_name]
......@@ -12,7 +12,8 @@
# See the License for the specific language governing permissions and
# limitations under the License.
NO_OUTPUT_COUNT = 0
def gen_codes(code_list, indent=0):
indent_blank = " " * indent
codes = []
......@@ -24,43 +25,61 @@ def gen_codes(code_list, indent=0):
return codes
def get_value(layer, key):
def get_value(layer, key, layer_id=None, different_attrs=None):
""" 进行optimizer后可能把inputs的value直接用数值代替(ConstantFuser),
会把input换成attr,所以需要此处的操作。
"""
if key in layer.inputs:
return layer.inputs[key]
else:
return str(layer.attrs[key])
def prim_add(layer, indent=1, init_func=[], forward_func=[]):
if different_attrs is None:
return str(layer.attrs[key])
else:
key_name = "{}_{}".format(layer.outputs[0], key)
if key_name in different_attrs:
return key_name
else:
if layer_id is None:
return str(layer.attrs[key])
key_name = "{}_{}".format("layer_id/{}".format(layer_id), key)
if key_name in different_attrs:
new_key_name = "attr_{}".format(NO_OUTPUT_COUNT)
NO_OUTPUT_COUNT += 1
diff_index = different_attrs.index(key_name)
different_attrs[diff_index] = new_key_name
return new_key_name
else:
return str(layer.attrs[key])
def prim_add(layer, indent=1, init_func=[], forward_func=[], layer_id=None, different_attrs=None):
line = "{} = {} + {}".format(layer.outputs[0],
get_value(layer, "x"), get_value(layer, "y"))
get_value(layer, "x", different_attrs), get_value(layer, "y", different_attrs))
forward_func.extend(gen_codes([line], indent=indent))
def prim_add_(layer, indent=1, init_func=[], forward_func=[]):
def prim_add_(layer, indent=1, init_func=[], forward_func=[], layer_id=None, different_attrs=None):
line = "{} = {} + {} * {}".format(layer.outputs[0],
get_value(layer, "x"),
get_value(layer, "x", different_attrs),
layer.attrs["alpha"],
get_value(layer, "y"))
get_value(layer, "y", different_attrs))
forward_func.extend(gen_codes([line], indent=indent))
def prim_and(layer, indent=1, init_func=[], forward_func=[]):
def prim_and(layer, indent=1, init_func=[], forward_func=[], layer_id=None, different_attrs=None):
line = "{} = {} and {}".format(layer.outputs[0],
get_value(layer, "x"), get_value(layer, "y"))
get_value(layer, "x", different_attrs), get_value(layer, "y", different_attrs))
forward_func.extend(gen_codes([line], indent=indent))
def prim_append(layer, indent=1, init_func=[], forward_func=[]):
def prim_append(layer, indent=1, init_func=[], forward_func=[], layer_id=None, different_attrs=None):
line = "{}.append({})".format(
get_value(layer, "list"), get_value(layer, "element"))
get_value(layer, "list", layer_id, different_attrs),
get_value(layer, "element", layer_id, different_attrs))
forward_func.extend(gen_codes([line], indent=indent))
def prim_assert(layer, indent=1, init_func=[], forward_func=[]):
def prim_assert(layer, indent=1, init_func=[], forward_func=[], layer_id=None, different_attrs=None):
if layer.attrs["type"] == "eq":
values = get_value(layer, "key")
if "value" in layer.attrs:
......@@ -83,93 +102,119 @@ def prim_assert(layer, indent=1, init_func=[], forward_func=[]):
forward_func.extend(gen_codes([line], indent=indent))
def prim_check_dim(layer, indent=1, init_func=[], forward_func=[]):
def prim_check_dim(layer, indent=1, init_func=[], forward_func=[], layer_id=None, different_attrs=None):
lines = []
lines.append("if {} < 0:".format(get_value(layer, "dim")))
dim = get_value(layer, "dim", different_attrs)
lines.append("if {} < 0:".format(dim))
lines.append(" {} = {} + {}".format(layer.outputs[
0], get_value(layer, "dim"), get_value(layer, "len")))
0], dim, get_value(layer, "len", different_attrs)))
lines.append("else:")
lines.append(" {} = {}".format(layer.outputs[0], get_value(layer,
"dim")))
lines.append(" {} = {}".format(layer.outputs[0], dim))
forward_func.extend(gen_codes(lines, indent=indent))
def prim_constant(layer, indent=1, init_func=[], forward_func=[]):
def prim_constant(layer, indent=1, init_func=[], forward_func=[], layer_id=None, different_attrs=None):
line = "{} = {}".format(layer.outputs[0], layer.attrs["value"])
forward_func.extend(gen_codes([line], indent=indent))
def prim_contain(layer, indent=1, init_func=[], forward_func=[]):
def prim_contain(layer, indent=1, init_func=[], forward_func=[], layer_id=None, different_attrs=None):
line = "{} = {} in {}".format(layer.outputs[0],
get_value(layer, "element"),
get_value(layer, "input"))
get_value(layer, "element", different_attrs),
get_value(layer, "input", different_attrs))
forward_func.extend(gen_codes([line], indent=indent))
def prim_dict(layer, indent=1, init_func=[], forward_func=[]):
def prim_dict(layer, indent=1, init_func=[], forward_func=[], layer_id=None, different_attrs=None):
line = "{} = dict()".format(layer.outputs[0])
forward_func.extend(gen_codes([line], indent=indent))
def prim_dict_construct(layer, indent=1, init_func=[], forward_func=[], layer_id=None, different_attrs=None):
lines = list()
line = "{} = dict()".format(layer.outputs[0])
lines.append(line)
for i in range(len(layer.inputs)):
line = "{}[{}] = {}".format(layer.outputs[0],
get_value(layer, "key{}".format(i), different_attrs),
get_value(layer, "value{}".format(i), different_attrs))
lines.append(line)
forward_func.extend(gen_codes(lines, indent=indent))
def prim_dict2values(layer, indent=1, init_func=[], forward_func=[], layer_id=None, different_attrs=None):
line = "{} = list({}.values())".format(layer.outputs[0],
get_value(layer, "x", different_attrs))
forward_func.extend(gen_codes([line], indent=indent))
def prim_div(layer, indent=1, init_func=[], forward_func=[]):
def prim_div(layer, indent=1, init_func=[], forward_func=[], layer_id=None, different_attrs=None):
line = "{} = {} / {}".format(layer.outputs[0],
get_value(layer, "x"), get_value(layer, "y"))
get_value(layer, "x", different_attrs),
get_value(layer, "y", different_attrs))
forward_func.extend(gen_codes([line], indent=indent))
def prim_eq(layer, indent=1, init_func=[], forward_func=[]):
def prim_eq(layer, indent=1, init_func=[], forward_func=[], layer_id=None, different_attrs=None):
line = "{} = {} == {}".format(layer.outputs[0],
get_value(layer, "x"), get_value(layer, "y"))
get_value(layer, "x", different_attrs),
get_value(layer, "y", different_attrs))
forward_func.extend(gen_codes([line], indent=indent))
def prim_equal(layer, indent=1, init_func=[], forward_func=[]):
line = "{} = {}".format(layer.outputs[0], get_value(layer, "input"))
def prim_equal(layer, indent=1, init_func=[], forward_func=[], layer_id=None, different_attrs=None):
line = "{} = {}".format(layer.outputs[0], get_value(layer, "input", different_attrs))
forward_func.extend(gen_codes([line], indent=indent))
def prim_exception(layer, indent=1, init_func=[], forward_func=[]):
line = "raise RaiseException({})".format(get_value(layer, "input"))
def prim_exception(layer, indent=1, init_func=[], forward_func=[], layer_id=None, different_attrs=None):
line = "raise RaiseException({})".format(get_value(layer, "input", different_attrs))
forward_func.extend(gen_codes([line], indent=indent))
def prim_float(layer, indent=1, init_func=[], forward_func=[]):
line = "{} = float({})".format(layer.outputs[0], get_value(layer, "input"))
def prim_float(layer, indent=1, init_func=[], forward_func=[], layer_id=None, different_attrs=None):
line = "{} = float({})".format(layer.outputs[0], get_value(layer, "input", different_attrs))
forward_func.extend(gen_codes([line], indent=indent))
def prim_floor(layer, indent=1, init_func=[], forward_func=[]):
def prim_floor(layer, indent=1, init_func=[], forward_func=[], layer_id=None, different_attrs=None):
line = "{} = math.floor({})".format(layer.outputs[0],
get_value(layer, "input"))
get_value(layer, "input", different_attrs))
forward_func.extend(gen_codes([line], indent=indent))
def prim_floordiv(layer, indent=1, init_func=[], forward_func=[]):
def prim_floordiv(layer, indent=1, init_func=[], forward_func=[], layer_id=None, different_attrs=None):
line = "{} = {} // {}".format(layer.outputs[0],
get_value(layer, "x"), get_value(layer, "y"))
get_value(layer, "x", different_attrs),
get_value(layer, "y", different_attrs))
forward_func.extend(gen_codes([line], indent=indent))
def prim_getitem(layer, indent=1, init_func=[], forward_func=[]):
def prim_getitem(layer, indent=1, init_func=[], forward_func=[], layer_id=None, different_attrs=None):
line = "{} = {}[{}]".format(layer.outputs[0],
get_value(layer, "list"),
get_value(layer, "index"))
get_value(layer, "list", different_attrs),
get_value(layer, "index", different_attrs))
forward_func.extend(gen_codes([line], indent=indent))
def prim_gt(layer, indent=1, init_func=[], forward_func=[]):
def prim_gt(layer, indent=1, init_func=[], forward_func=[], layer_id=None, different_attrs=None):
line = "{} = {} > {}".format(layer.outputs[0],
get_value(layer, "x"), get_value(layer, "y"))
get_value(layer, "x", different_attrs),
get_value(layer, "y", different_attrs))
forward_func.extend(gen_codes([line], indent=indent))
def prim_if(layer, indent=1, init_func=[], forward_func=[]):
line = "if {} :".format(get_value(layer, "input"))
def prim_if(layer, indent=1, init_func=[], forward_func=[], layer_id=None, different_attrs=None):
line = "if {} :".format(get_value(layer, "input", different_attrs))
forward_func.extend(gen_codes([line], indent=indent))
block = layer.blocks[0]
b_init_lines, b_forward_lines = block.gen_dygraph_code(indent=indent + 1)
init_func.extend(b_init_lines)
forward_func.extend(b_forward_lines)
if len(block.layers) == 0:
line = "pass"
forward_func.extend(gen_codes([line], indent=indent + 1))
else:
b_init_lines, b_forward_lines = block.gen_dygraph_code(indent=indent + 1)
init_func.extend(b_init_lines)
forward_func.extend(b_forward_lines)
block = layer.blocks[1]
if len(block.layers) > 0:
b_init_lines, b_forward_lines = block.gen_dygraph_code(
......@@ -181,73 +226,76 @@ def prim_if(layer, indent=1, init_func=[], forward_func=[]):
forward_func.extend(b_forward_lines)
def prim_int(layer, indent=1, init_func=[], forward_func=[]):
line = "{} = int({})".format(layer.outputs[0], get_value(layer, "input"))
def prim_int(layer, indent=1, init_func=[], forward_func=[], layer_id=None, different_attrs=None):
line = "{} = int({})".format(layer.outputs[0], get_value(layer, "input", different_attrs))
forward_func.extend(gen_codes([line], indent=indent))
def prim_is(layer, indent=1, init_func=[], forward_func=[]):
def prim_is(layer, indent=1, init_func=[], forward_func=[], layer_id=None, different_attrs=None):
line = "{} = {} is {}".format(layer.outputs[0],
get_value(layer, "x"), get_value(layer, "y"))
get_value(layer, "x", different_attrs),
get_value(layer, "y", different_attrs))
forward_func.extend(gen_codes([line], indent=indent))
def prim_isinstance(layer, indent=1, init_func=[], forward_func=[]):
def prim_isinstance(layer, indent=1, init_func=[], forward_func=[], layer_id=None, different_attrs=None):
line = "{} = isinstance({}, {})".format(layer.outputs[0],
get_value(layer, "input"),
get_value(layer, "input", different_attrs),
layer.attrs["cls"])
forward_func.extend(gen_codes([line], indent=indent))
def prim_isnot(layer, indent=1, init_func=[], forward_func=[]):
def prim_isnot(layer, indent=1, init_func=[], forward_func=[], layer_id=None, different_attrs=None):
line = "{} = {} is not {}".format(layer.outputs[0],
get_value(layer, "x"),
get_value(layer, "y"))
get_value(layer, "x", different_attrs),
get_value(layer, "y", different_attrs))
forward_func.extend(gen_codes([line], indent=indent))
def prim_le(layer, indent=1, init_func=[], forward_func=[]):
def prim_le(layer, indent=1, init_func=[], forward_func=[], layer_id=None, different_attrs=None):
line = "{} = {} <= {}".format(layer.outputs[0],
get_value(layer, "x"), get_value(layer, "y"))
get_value(layer, "x", different_attrs),
get_value(layer, "y", different_attrs))
forward_func.extend(gen_codes([line], indent=indent))
def prim_len(layer, indent=1, init_func=[], forward_func=[]):
line = "{} = len({})".format(layer.outputs[0], get_value(layer, "input"))
def prim_len(layer, indent=1, init_func=[], forward_func=[], layer_id=None, different_attrs=None):
line = "{} = len({})".format(layer.outputs[0], get_value(layer, "input", different_attrs))
forward_func.extend(gen_codes([line], indent=indent))
def prim_len2list(layer, indent=1, init_func=[], forward_func=[]):
def prim_len2list(layer, indent=1, init_func=[], forward_func=[], layer_id=None, different_attrs=None):
lines = []
lines.append("{} = []".format(layer.outputs[0]))
lines.append("for i in range({}):".format(get_value(layer, "len")))
lines.append("for i in range({}):".format(get_value(layer, "len", different_attrs)))
lines.append(" {}.append(i)".format(layer.outputs[0]))
forward_func.extend(gen_codes(lines, indent=indent))
def prim_lt(layer, indent=1, init_func=[], forward_func=[]):
def prim_lt(layer, indent=1, init_func=[], forward_func=[], layer_id=None, different_attrs=None):
line = "{} = {} < {}".format(layer.outputs[0],
get_value(layer, "x"), get_value(layer, "y"))
get_value(layer, "x", different_attrs),
get_value(layer, "y", different_attrs))
forward_func.extend(gen_codes([line], indent=indent))
def prim_list(layer, indent=1, init_func=[], forward_func=[]):
def prim_list(layer, indent=1, init_func=[], forward_func=[], layer_id=None, different_attrs=None):
input_len = len(layer.inputs) + len(layer.attrs)
inputs_list = list()
for i in range(input_len):
inputs_list.append(get_value(layer, "input{}".format(i)))
inputs_list.append(get_value(layer, "input{}".format(i), different_attrs))
inputs_str = ', '.join(inputs_list)
line = "{} = [{}]".format(layer.outputs[0], inputs_str)
forward_func.extend(gen_codes([line], indent=indent))
def prim_list_unpack(layer, indent=1, init_func=[], forward_func=[]):
line = "{} = {}".format(", ".join(layer.outputs), get_value(layer, "input"))
def prim_list_unpack(layer, indent=1, init_func=[], forward_func=[], layer_id=None, different_attrs=None):
line = "{} = {}".format(", ".join(layer.outputs), get_value(layer, "input", different_attrs))
forward_func.extend(gen_codes([line], indent=indent))
def prim_loop(layer, indent=1, init_func=[], forward_func=[]):
loop_range = get_value(layer, "input")
def prim_loop(layer, indent=1, init_func=[], forward_func=[], layer_id=None, different_attrs=None):
loop_range = get_value(layer, "input", different_attrs)
line = "for {} in range({}):".format(layer.outputs[1], loop_range)
forward_func.extend(gen_codes([line], indent=indent))
block = layer.blocks[0]
......@@ -256,137 +304,149 @@ def prim_loop(layer, indent=1, init_func=[], forward_func=[]):
forward_func.extend(b_forward_lines)
def prim_min(layer, indent=1, init_func=[], forward_func=[]):
line = "{} = min({})".format(layer.outputs[0], get_value(layer, "input"))
def prim_min(layer, indent=1, init_func=[], forward_func=[], layer_id=None, different_attrs=None):
line = "{} = min({})".format(layer.outputs[0], get_value(layer, "input", different_attrs))
forward_func.extend(gen_codes([line], indent=indent))
def prim_mul(layer, indent=1, init_func=[], forward_func=[]):
def prim_mul(layer, indent=1, init_func=[], forward_func=[], layer_id=None, different_attrs=None):
line = "{} = {} * {}".format(layer.outputs[0],
get_value(layer, "x"), get_value(layer, "y"))
get_value(layer, "x", different_attrs),
get_value(layer, "y", different_attrs))
forward_func.extend(gen_codes([line], indent=indent))
def prim_ne(layer, indent=1, init_func=[], forward_func=[]):
def prim_ne(layer, indent=1, init_func=[], forward_func=[], layer_id=None, different_attrs=None):
line = "{} = {} != {}".format(layer.outputs[0],
get_value(layer, "x"), get_value(layer, "y"))
get_value(layer, "x", different_attrs),
get_value(layer, "y", different_attrs))
forward_func.extend(gen_codes([line], indent=indent))
def prim_neg(layer, indent=1, init_func=[], forward_func=[]):
line = "{} = -{}".format(layer.outputs[0], get_value(layer, "input"))
def prim_neg(layer, indent=1, init_func=[], forward_func=[], layer_id=None, different_attrs=None):
line = "{} = -{}".format(layer.outputs[0], get_value(layer, "input", different_attrs))
forward_func.extend(gen_codes([line], indent=indent))
def prim_not(layer, indent=1, init_func=[], forward_func=[]):
line = "{} = not {}".format(layer.outputs[0], get_value(layer, "input"))
def prim_not(layer, indent=1, init_func=[], forward_func=[], layer_id=None, different_attrs=None):
line = "{} = not {}".format(layer.outputs[0], get_value(layer, "input", different_attrs))
forward_func.extend(gen_codes([line], indent=indent))
def prim_or(layer, indent=1, init_func=[], forward_func=[]):
def prim_or(layer, indent=1, init_func=[], forward_func=[], layer_id=None, different_attrs=None):
line = "{} = {} or {}".format(layer.outputs[0],
get_value(layer, "x"), get_value(layer, "y"))
get_value(layer, "x", different_attrs),
get_value(layer, "y", different_attrs))
forward_func.extend(gen_codes([line], indent=indent))
def prim_replaceitem(layer, indent=1, init_func=[], forward_func=[]):
def prim_replaceitem(layer, indent=1, init_func=[], forward_func=[], layer_id=None, different_attrs=None):
line = "{}[{}] = {}".format(
get_value(layer, "list"),
get_value(layer, "index"), get_value(layer, "item"))
get_value(layer, "list", layer_id, different_attrs),
get_value(layer, "index", layer_id, different_attrs),
get_value(layer, "item", layer_id, different_attrs))
forward_func.extend(gen_codes([line], indent=indent))
def prim_requires_grad(layer, indent=1, init_func=[], forward_func=[]):
def prim_requires_grad(layer, indent=1, init_func=[], forward_func=[], layer_id=None, different_attrs=None):
line = "{} = not {}.stop_gradient".format(layer.outputs[0],
get_value(layer, "input"))
get_value(layer, "input", different_attrs))
forward_func.extend(gen_codes([line], indent=indent))
def prim_rsub(layer, indent=1, init_func=[], forward_func=[]):
def prim_rsub(layer, indent=1, init_func=[], forward_func=[], layer_id=None, different_attrs=None):
line = "{} = {} - {} * {}".format(layer.outputs[0],
get_value(layer, "y"),
get_value(layer, "x"),
get_value(layer, "alpha"))
get_value(layer, "y", different_attrs),
get_value(layer, "x", different_attrs),
get_value(layer, "alpha", different_attrs))
forward_func.extend(gen_codes([line], indent=indent))
def prim_select(layer, indent=1, init_func=[], forward_func=[]):
line = "{} = {}[".format(layer.outputs[0], get_value(layer, "input"))
def prim_select(layer, indent=1, init_func=[], forward_func=[], layer_id=None, different_attrs=None):
line = "{} = {}[".format(layer.outputs[0], get_value(layer, "input", different_attrs))
for dim in range(layer.attrs["dim"]):
line += ":, "
line += (get_value(layer, "index") + "]")
line += (get_value(layer, "index", different_attrs) + "]")
forward_func.extend(gen_codes([line], indent=indent))
def prim_set_attr(layer, indent=1, init_func=[], forward_func=[]):
line = "{} = {}".format(layer.outputs[0], get_value(layer, "input"))
def prim_set_attr(layer, indent=1, init_func=[], forward_func=[], layer_id=None, different_attrs=None):
line = "{} = {}".format(layer.outputs[0], get_value(layer, "input", different_attrs))
forward_func.extend(gen_codes([line], indent=indent))
def prim_set_item(layer, indent=1, init_func=[], forward_func=[]):
def prim_set_item(layer, indent=1, init_func=[], forward_func=[], layer_id=None, different_attrs=None):
line = "{}[{}] = {}".format(
get_value(layer, "dict"),
get_value(layer, "key"), get_value(layer, "value"))
get_value(layer, "dict", different_attrs),
get_value(layer, "key", different_attrs), get_value(layer, "value", different_attrs))
forward_func.extend(gen_codes([line], indent=indent))
def prim_shape(layer, indent=1, init_func=[], forward_func=[], layer_id=None, different_attrs=None):
line = "{} = {}.shape".format(layer.outputs[0],
get_value(layer, "input", different_attrs))
forward_func.extend(gen_codes([line], indent=indent))
def prim_shape_dim(layer, indent=1, init_func=[], forward_func=[]):
line = "{} = fluid.layers.shape({})[{}]".format(layer.outputs[0],
get_value(layer, "input"),
get_value(layer, "dim"))
def prim_shape_dim(layer, indent=1, init_func=[], forward_func=[], layer_id=None, different_attrs=None):
line = "{} = {}.shape[{}]".format(layer.outputs[0],
get_value(layer, "input", different_attrs),
get_value(layer, "dim", different_attrs))
forward_func.extend(gen_codes([line], indent=indent))
def prim_slice(layer, indent=1, init_func=[], forward_func=[]):
def prim_slice(layer, indent=1, init_func=[], forward_func=[], layer_id=None, different_attrs=None):
line = "{} = {}[{}: {}: {}]".format(layer.outputs[0],
get_value(layer, "input"),
get_value(layer, "start"),
get_value(layer, "end"),
get_value(layer, "step"))
get_value(layer, "input", different_attrs),
get_value(layer, "start", different_attrs),
get_value(layer, "end", different_attrs),
get_value(layer, "step", different_attrs))
forward_func.extend(gen_codes([line], indent=indent))
def prim_str(layer, indent=1, init_func=[], forward_func=[]):
line = "{} = str({})".format(layer.outputs[0], get_value(layer, "input"))
def prim_str(layer, indent=1, init_func=[], forward_func=[], layer_id=None, different_attrs=None):
line = "{} = str({})".format(layer.outputs[0], get_value(layer, "input", different_attrs))
forward_func.extend(gen_codes([line], indent=indent))
def prim_sub(layer, indent=1, init_func=[], forward_func=[]):
def prim_sub(layer, indent=1, init_func=[], forward_func=[], layer_id=None, different_attrs=None):
line = "{} = {} - {}".format(layer.outputs[0],
get_value(layer, "x"), get_value(layer, "y"))
get_value(layer, "x", different_attrs),
get_value(layer, "y", different_attrs))
forward_func.extend(gen_codes([line], indent=indent))
def prim_tuple(layer, indent=1, init_func=[], forward_func=[]):
def prim_tuple(layer, indent=1, init_func=[], forward_func=[], layer_id=None, different_attrs=None):
input_len = len(layer.inputs) + len(layer.attrs)
inputs_list = list()
for i in range(input_len):
inputs_list.append(get_value(layer, "input{}".format(i)))
inputs_list.append(get_value(layer, "input{}".format(i), different_attrs))
inputs_str = ', '.join(inputs_list)
line = "{} = ({})".format(layer.outputs[0], inputs_str)
forward_func.extend(gen_codes([line], indent=indent))
def prim_tuple_unpack(layer, indent=1, init_func=[], forward_func=[]):
def prim_tuple_unpack(layer, indent=1, init_func=[], forward_func=[], layer_id=None, different_attrs=None):
outputs_str = ', '.join(layer.outputs)
line = "{} = {}".format(outputs_str, get_value(layer, "input"))
line = "{} = {}".format(outputs_str, get_value(layer, "input", different_attrs))
forward_func.extend(gen_codes([line], indent=indent))
def prim_type(layer, indent=1, init_func=[], forward_func=[]):
line = "{} = {}.dtype".format(layer.outputs[0], get_value(layer, "input"))
def prim_type(layer, indent=1, init_func=[], forward_func=[], layer_id=None, different_attrs=None):
line = "{} = {}.dtype".format(layer.outputs[0], get_value(layer, "input", different_attrs))
forward_func.extend(gen_codes([line], indent=indent))
def prim_var2list(layer, indent=1, init_func=[], forward_func=[]):
def prim_var2list(layer, indent=1, init_func=[], forward_func=[], layer_id=None, different_attrs=None):
line = "{} = {}.numpy().tolist()".format(layer.outputs[0],
get_value(layer, "input"))
get_value(layer, "input", different_attrs))
forward_func.extend(gen_codes([line], indent=indent))
def prim_warnings(layer, indent=1, init_func=[], forward_func=[]):
def prim_warnings(layer, indent=1, init_func=[], forward_func=[], layer_id=None, different_attrs=None):
lines = ["import warnings"]
line = "warnings.warn({}, stacklevel={})".format(
get_value(layer, "input"), layer.attrs["stacklevel"])
get_value(layer, "input", different_attrs), layer.attrs["stacklevel"])
lines.append(line)
forward_func.extend(gen_codes(lines, indent=indent))
......@@ -17,24 +17,29 @@ import numpy as np
from x2paddle.core.op_mapper import OpMapper
from x2paddle.core.util import *
from x2paddle.core.program import PaddleGraph
from x2paddle.op_mapper.pytorch2paddle import prim
from x2paddle.op_mapper.pytorch2paddle import aten
from x2paddle.op_mapper.dygraph.pytorch2paddle import prim
from x2paddle.op_mapper.dygraph.pytorch2paddle import aten
class PyTorchOpMapper(OpMapper):
def __init__(self, decoder):
super(PyTorchOpMapper, self).__init__()
self.script = decoder.script
self.input_examples = decoder.input_examples
self.paddle_params = dict()
self.outputs_info = {} # key为output unique id,value为当前节点的输出名字
self.pytorch_params = {} # key为节点名,value为参数
self.attrs = {} # key为节点名,value为属性值
self.output_index = 0
self.dygraph_name_id = {} # 动态图__init__输出名字中的id,key为kernel类型,value为id
self.nn_name2id = {} # 动态图__init__输出名字中的id,key为kernel类型,value为id
self.split_len = {} # split的长度
self.scope_name_list = list()
self.scope_name2id = dict()
self.inputs_info = dict()
# 转换
self.check_op(decoder.graph)
self.graph, _ = self.traverse(decoder.graph)
self.paddle_graph, _ = self.traverse(decoder.graph)
self.paddle_graph.set_inputs_info(self.inputs_info)
def check_op(self, script_graph):
def _update_op_list(graph):
......@@ -72,17 +77,21 @@ class PyTorchOpMapper(OpMapper):
current_node_outputs.extend(outputs)
# 初始化
graph = PaddleGraph(parent_layer, graph_type="dygraph")
graph = PaddleGraph(parent_layer=parent_layer, graph_type="dygraph")
if "TopLevelTracedModule" in str(type(self.script)):
graph.set_script(self.script)
current_node_outputs = []
graph_inputs = []
# 转换输入节点
if isinstance(script_graph, torch._C.Graph):
input_ct = 0
for i, ivalue in enumerate(script_graph.inputs()):
node = ivalue.node()
if str(ivalue.type()) != "Tensor":
if str(ivalue.type()) not in ["Tensor", "Dict[str, Tensor]"]:
graph.set_name(str(ivalue.type()).split(".")[-1])
continue
inputs, outputs = self.data(graph, node, ivalue.unique())
inputs, outputs = self.data(graph, node, ivalue.unique(), input_ct)
input_ct += 1
# 转换中间节点
for node in script_graph.nodes():
kind = node.kind()
......@@ -120,7 +129,7 @@ class PyTorchOpMapper(OpMapper):
graph.outputs = inputs_name
# 更新split参数
for layer in graph.layers.values():
if layer.kernel == "fluid.layers.split" and "num_or_sections" in layer.attrs:
if layer.kernel == "paddle.split" and "num_or_sections" in layer.attrs:
layer.attrs["num_or_sections"] = self.split_len[layer.outputs[
0]]
return graph, graph_inputs
......@@ -151,6 +160,7 @@ class PyTorchOpMapper(OpMapper):
node,
output_name,
node_outputs,
scope_name,
add_dim=False):
if node.kind() == "prim::GetAttr":
param = self.pytorch_params[output_name]
......@@ -159,10 +169,13 @@ class PyTorchOpMapper(OpMapper):
param = param[np.newaxis, :]
self.paddle_params[output_name] = param
graph.add_layer(
"fluid.dygraph.base.to_variable",
"self.create_parameter",
inputs={},
outputs=[output_name],
value="params[{}]".format(string(output_name)))
scope_name=scope_name,
dtype=string(str(param.dtype)),
shape = param.shape,
default_initializer="paddle.nn.initializer.Constant(value=0.0)")
else:
if isinstance(param, dict) and "Tensor" in param and \
"parent_layer_id" in param:
......@@ -183,11 +196,13 @@ class PyTorchOpMapper(OpMapper):
param = param[np.newaxis, :]
self.paddle_params[output_name] = param
graph.add_layer(
"fluid.dygraph.base.to_variable",
"self.create_parameter",
inputs={},
outputs=[output_name],
value="params[{}]".format(
string(output_name)))
scope_name=scope_name,
dtype=string(str(param.dtype)),
shape = param.shape,
default_initializer="paddle.nn.initializer.Constant(value=0.0)")
node_outputs.append(output_name)
return
# 若if-else外,则可直接引用if-else中的赋值结果
......@@ -195,16 +210,30 @@ class PyTorchOpMapper(OpMapper):
"prim.constant",
inputs={},
outputs=[output_name],
scope_name=scope_name,
value=param["Tensor"])
else:
graph.add_layer(
"prim.constant",
inputs={},
outputs=[output_name],
scope_name=scope_name,
value=string(param)
if isinstance(param, str) else param)
node_outputs.append(output_name)
elif node.kind() == "prim::Constant" and output_name in self.pytorch_params:
param = self.pytorch_params[output_name]
self.paddle_params[output_name] = param
graph.add_layer(
"self.create_parameter",
inputs={},
outputs=[output_name],
scope_name=scope_name,
dtype=string(str(param.dtype)),
shape = param.shape,
default_initializer="paddle.nn.initializer.Constant(value=0.0)")
def _get_inputs_name(self, node):
inputs_name = []
inputs_node = []
......@@ -215,8 +244,10 @@ class PyTorchOpMapper(OpMapper):
inputs_node.append(script_input_node)
inputs_name.append(input_name)
return inputs_name, inputs_node
def data(self, graph, node, uid):
def data(self, graph, node, uid, input_ct):
scope_name = self.normalize_scope_name(node)
for output_ivalue in node.outputs():
script_unique_id = output_ivalue.unique()
if script_unique_id in self.outputs_info or script_unique_id != uid:
......@@ -226,13 +257,18 @@ class PyTorchOpMapper(OpMapper):
self.output_index += 1
output_name = self.outputs_info[uid]
graph.add_layer(
"fluid.dygraph.base.to_variable",
"paddle.to_tensor",
inputs={},
outputs=[node_name],
value=output_name)
scope_name=scope_name,
data=output_name)
if self.input_examples is not None:
input_np = self.input_examples[input_ct].detach().numpy()
self.inputs_info[output_name] = [list(input_np.shape), str(input_np.dtype)]
return [], [output_name]
def equal(self, graph, node, uid=None, parent_layer=None, index=None):
scope_name = self.normalize_scope_name(node)
if parent_layer is not None and index is not None:
# block的输出
input_node_name = self.outputs_info[uid]
......@@ -241,9 +277,61 @@ class PyTorchOpMapper(OpMapper):
control_output_id = index - 1
output_node_name = parent_layer.outputs[control_output_id]
current_outputs = [output_node_name]
self._check_input(graph, node, input_node_name, current_outputs)
self._check_input(graph, node, input_node_name, current_outputs, scope_name)
graph.add_layer(
"prim.equal",
inputs={'input': input_node_name},
outputs=[output_node_name])
outputs=[output_node_name],
scope_name=scope_name)
return [input_node_name], current_outputs
def normalize_scope_name(self, node):
""" 对scope的名字进行标准化。
"""
scope_name = node.scopeName()
if scope_name == "":
return scope_name
scope_name_part = scope_name.split("/")
for index in range(len(scope_name_part) - 1):
if scope_name_part[index] in scope_name_part[index + 1]:
continue
last_name_segments = scope_name_part[index].split(".")
name_segments = scope_name_part[index + 1].split(".")
for j, name in enumerate(last_name_segments):
name_segments[j] = name
scope_name_part[index + 1] = ".".join(name_segments)
last_name = scope_name_part[-1]
name_segments = last_name.split(".")
for i, ns in enumerate(name_segments):
if i not in self.scope_name2id:
self.scope_name2id[i] = dict()
if ns not in self.scope_name2id[i]:
self.scope_name2id[i][ns] = 0
real_scope_name = "/".join(name_segments[1:])
real_father_scope_name = "/".join(name_segments[1:-1])
for i, ns in enumerate(name_segments):
if i == 0:
continue
if self.scope_name2id[i][ns] != 0:
name_segments[i] = name_segments[i] + \
"__{}".format(self.scope_name2id[i][ns])
prefix_scope_name = "/".join(name_segments[1 :i + 1])
is_found = False
for j in range(len(self.scope_name_list)):
last_scope_name = self.scope_name_list[-1-j]
if last_scope_name.startswith(prefix_scope_name + "/") \
or last_scope_name == prefix_scope_name:
if j != 0: # and i != len(name_segments) - 1:
is_found = True
origin_name_segment_i = name_segments[i].split("__")[0]
self.scope_name2id[i][origin_name_segment_i] += 1
name_segments[i] = origin_name_segment_i + \
"__" + str(self.scope_name2id[i][origin_name_segment_i])
break
if is_found:
break
real_scope_name = "/".join(name_segments[1:])
self.scope_name_list.append(real_scope_name)
return real_scope_name
\ No newline at end of file
# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from x2paddle.optimizer.code_optimizer.hierachical_tree import HierarchicalTree
\ No newline at end of file
# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import copy
import os.path as osp
from treelib import Tree
from queue import Queue
from x2paddle.optimizer.code_optimizer.layer_code_generator import gen_layer_code, rename_layers, NN_KERNEL_WITH_PARAMS, NN_KERNEL_NAME
from x2paddle.optimizer.code_optimizer.subgraphs_union import distinguish_sequential, get_inputs_outputs
from x2paddle.core.program import PaddleLayer
from x2paddle.optimizer.code_optimizer.parameter_tree import PamareterNode, PamareterTree
SEPARATOR_IN_SCOPE = "/"
class HierarchicalTree(Tree):
""" 定义层次树。
"""
def __init__(self, pd_graph):
super(HierarchicalTree, self).__init__()
self.pd_graph = pd_graph
self.script = pd_graph.script
self.create_node("Module", self.pd_graph.name) # create root
self._hierarchical_order = dict()
self.codes = list()
self.identifier_idx = dict()
self.param_tree = PamareterTree()
self.module_name2count = dict()
def insert(self, layer):
""" 往层次树中插入节点。
Args:
layer (PaddleLayer): 需要插入的节点。
"""
scope_name = layer.scope_name
if scope_name == "":
if layer.kernel == "prim.tuple" or layer.kernel == "prim.tuple_unpack":
layer_id = layer.id
layer_id_list = list()
for input_layer_id in self.pd_graph.edges_in[layer_id]:
layer_id_list.append(int(input_layer_id))
layer_id_list = list(set(layer_id_list))
layer_id_list.sort(reverse=True)
for input_layer_id in layer_id_list:
input_layer_id_str = str(input_layer_id)
if self.pd_graph.layers[input_layer_id_str].scope_name != "":
scope_name = self.pd_graph.layers[input_layer_id_str].scope_name
break
layer.scope_name = scope_name
else:
self.create_node(tag=layer.id,
identifier="no_scope_" + layer.id,
parent=self.pd_graph.name,
data=layer)
return
scopes = scope_name.split(SEPARATOR_IN_SCOPE)
for idx, scope in enumerate(scopes):
parent = SEPARATOR_IN_SCOPE.join(scopes[:idx])#.lower()
identifier = SEPARATOR_IN_SCOPE.join(scopes[:idx + 1])#.lower()
if self.contains(identifier):
if idx != len(scopes) - 1:
parent_node = self.parent(identifier)
self.move_node(identifier, parent_node.identifier)
continue
else:
if self.get_node(identifier).data is None:
data = layer
if identifier not in self.identifier_idx:
self.identifier_idx[identifier] = 0
else:
self.identifier_idx[identifier] += 1
identifier_name = identifier + SEPARATOR_IN_SCOPE + str(self.identifier_idx[identifier])
self.create_node(tag=scopes[idx],
identifier=identifier_name,
parent=identifier,
data=data)
data.scope_name = identifier_name
continue
else:
data = self[identifier].data
self[identifier].data = None
parent_node = self.parent(identifier)
self.move_node(identifier, parent_node.identifier)
if identifier not in self.identifier_idx:
self.identifier_idx[identifier] = 0
else:
self.identifier_idx[identifier] += 1
self.create_node(tag=scopes[idx],
identifier=identifier + SEPARATOR_IN_SCOPE + str(self.identifier_idx[identifier]),
parent=identifier,
data=data)
self.identifier_idx[identifier] += 1
data = layer
self.create_node(tag=scopes[idx],
identifier=identifier + SEPARATOR_IN_SCOPE + str(self.identifier_idx[identifier]),
parent=identifier,
data=data)
continue
if idx == 0 and not self.contains(identifier):
data = layer if idx == len(scopes) - 1 else None
self.create_node(tag=scopes[idx],
identifier=identifier,
parent=self.pd_graph.name,
data=data)
else:
if idx == len(scopes) - 1:
if parent == "":
childs = self.children(self.pd_graph.name)
parent = self.pd_graph.name
else:
childs = self.children(parent)
prefix = identifier
identifiers = list()
for child in childs:
child_identifier = child.identifier
if child_identifier.startswith(prefix) and child_identifier != prefix:
identifiers.append(child_identifier)
if len(identifiers) == 0:
identifier = prefix + "_0"
else:
identifier_ids = list()
for id_obj in identifiers:
identifier_ids.append(int(id_obj.split("_")[-1]))
identifier_ids.sort()
identifier = prefix + "_{}".format(identifier_ids[-1] + 1)
data = layer if idx == len(scopes) - 1 else None
self.create_node(tag=scopes[idx],
identifier=identifier,
parent=parent,
data=data)
def update_hierarchical_order(self):
""" 更新层次排序,使用一个字典存储该信息,
关键字为当前层次,值为节点名字。
"""
hierarchical_order = dict()
queue = Queue()
queue.put(item=(self.pd_graph.name, 0), block=False)
while not queue.empty():
node_name, cur_level = queue.get(block=False)
node_inst = self[node_name]
if cur_level not in hierarchical_order:
hierarchical_order[cur_level] = []
hierarchical_order[cur_level].append(node_name)
for successor_name in node_inst.successors(self.identifier):
queue.put(item=(successor_name, cur_level + 1), block=False)
self._hierarchical_order = hierarchical_order
def analyze_attrs_table(self, attrs_table):
""" 分析属性表格,哪些属性取值不一致。
"""
diff_attrs_column = list()
for column in list(attrs_table.columns):
elements = list(attrs_table.get(column))
base = elements[0]
for element in elements[1:]:
if isinstance(base, str) and "'" not in base:
break
if element != base:
diff_attrs_column.append(column)
break
return diff_attrs_column
def merge_node(self, sub_layers_list, attrs_table, node_name2sub_layers, module_name):
""" 将一个scope的节点合成一个Module(Class),并将对应的Class代码
放到code字符串中。
"""
def get_node_name(sub_layers):
for k, v in node_name2sub_layers.items():
if v == sub_layers:
node_name = k
break
return node_name
sub_layers = sub_layers_list[0]
node_name = get_node_name(sub_layers)
sub_layers, _, _ = rename_layers(sub_layers)
diff_attrs_column = self.analyze_attrs_table(attrs_table)
if module_name is None:
module_name = node_name.replace("/", "_") #node_name.split("/")[-1]
module_name = module_name[0].upper() + module_name[1:]
if module_name in self.module_name2count:
module_name = module_name + "_0"
code_str = gen_layer_code(self.pd_graph, sub_layers, module_name,
different_attrs=diff_attrs_column)
# print(code_str)
self.codes.append(code_str)
for sub_layers in sub_layers_list:
inputs, outputs = get_inputs_outputs(self.pd_graph, sub_layers)
inputs_dict = dict()
for i, input in enumerate(inputs):
inputs_dict["input_{}".format(i)] = input
if module_name in self.module_name2count:
self.module_name2count[module_name] += 1
else:
self.module_name2count[module_name] = 0
if module_name.lower() in NN_KERNEL_NAME.values():
mn = module_name.lower() + "__"
else:
mn = module_name.lower()
outputs = ["{}/{}".format(mn, self.module_name2count[module_name])] + outputs
node_name = get_node_name(sub_layers)
diff_attrs = dict()
for column in diff_attrs_column:
diff_attrs[column] = attrs_table.get(column).loc[node_name]
node_name_seg = node_name.split(SEPARATOR_IN_SCOPE)
node_name_seg[-1] = module_name.lower()
new_node_name = SEPARATOR_IN_SCOPE.join(node_name_seg)
new_layer = PaddleLayer(id=list(sub_layers.keys())[-1],
kernel="module",
inputs=inputs_dict,
outputs=outputs,
scope_name=new_node_name,
module=module_name,
**diff_attrs)
_, nn_param_nodes, _ = rename_layers(sub_layers, self.param_tree)
param_node = PamareterNode(old_name=outputs[0])
for node in nn_param_nodes:
param_node.add_child(node)
self.param_tree.add_node(param_node)
for i, (layer_id, layer) in enumerate(sub_layers.items()):
if i == len(sub_layers) - 1:
self.pd_graph.layers[layer_id] = new_layer
else:
self.pd_graph.layers.pop(layer_id)
self.pd_graph.build()
self[node_name].data = new_layer
def find_subgraph_diff(self, module_name2sub_layers, module_name2sub_identifiers, node_name2sub_layers, name):
""" 查找子图的diff,主要是输入参数的diff。
"""
sub_layers = module_name2sub_layers[name]
sub_identifiers = module_name2sub_identifiers[name]
new_sub_layers, new_sub_sequentials, sequentials2attrs_table = distinguish_sequential(self.pd_graph,
name,
sub_layers,
sub_identifiers,
node_name2sub_layers)
module_name2sub_layers.pop(name)
module_name2sub_identifiers.pop(name)
for k, v in new_sub_layers.items():
module_name2sub_layers[k] = v
module_name2sub_identifiers[k] = new_sub_sequentials[k]
return sequentials2attrs_table
def convert_subgraph_to_layer(self):
"""
1. 根据_hierarchical_order,从最深的层次开始将
子图合并成layer(即合成节点)。
2. 根据参数名新旧对应关系,更新参数名。
"""
depths = sorted(list(self._hierarchical_order.keys()), reverse=True)
all_name_old2new = dict()
for depth in depths[1:]:
# Module的名字与子图的对应关系
module_name2sub_layers = dict()
# Module的名字与子图中layer命名的对应关系
module_name2sub_identifiers = dict()
# 层次树中包含子树的节点,其节点名与子图对用关系
node_name2sub_layers = dict()
for node_name in self._hierarchical_order[depth]:
node_inst = self[node_name]
if node_inst.data is None:
sub_layers = dict()
sub_identifiers = dict()
for successor_name in node_inst.successors(self.identifier):
sub_layers[self[successor_name].data.id] = self[successor_name].data
sub_identifiers[self[successor_name].data.id] = self[successor_name].data.scope_name.split("/")[-1]
node_name2sub_layers[node_name] = sub_layers
node_name_segs = node_name.split("/")
# 获取Module的名字
module = self.script
is_largest_module = False # 当前module是否是最外层的Module
for name_id, name in enumerate(node_name_segs):
name = name.split("__")[0]
if not hasattr(module, name):
is_largest_module = True
break
module = getattr(module, name)
if is_largest_module:
if name_id == 0:
module_name = name
else:
module_name = "_".join(node_name_segs)
else:
module_name = module._get_name()
if module_name in module_name2sub_layers:
module_name2sub_layers[module_name].append(sub_layers)
module_name2sub_identifiers[module_name].append(sub_identifiers)
else:
module_name2sub_layers[module_name] = [sub_layers]
module_name2sub_identifiers[module_name] = [sub_identifiers]
module_names = list(module_name2sub_layers.keys())
for module_name in module_names:
sequentials2attrs_table = self.find_subgraph_diff(module_name2sub_layers,
module_name2sub_identifiers,
node_name2sub_layers,
module_name)
for name in sequentials2attrs_table.keys():
if name.startswith("Sequential"):
# 若Module的名字为Sequential,则以scope_name的名字来命名,在merge_node中实现
module_name = None
else:
module_name = name
self.merge_node(module_name2sub_layers[name],
sequentials2attrs_table[name],
node_name2sub_layers,
module_name)
def update_parameters(self):
""" 更新参数。
"""
self.param_tree.traverse()
full_old_name_list = copy.deepcopy(list(self.pd_graph.parameters.keys()))
for old_name, new_name in self.param_tree.old2new.items():
for full_old_name in full_old_name_list:
if full_old_name.startswith("{}.".format(old_name)):
full_new_name = full_old_name.replace("{}.".format(old_name), "{}.".format(new_name))
params = self.pd_graph.parameters.pop(full_old_name)
self.pd_graph.parameters[full_new_name] = params
if full_old_name == old_name:
full_new_name = full_old_name.replace(old_name, new_name)
params = self.pd_graph.parameters.pop(full_old_name)
self.pd_graph.parameters[full_new_name] = params
def save_source_files(self, save_dir):
def gen_main_code():
input_data_name = ', '.join(self.pd_graph.inputs)
run_func_list = list()
run_func_list.append("def main({}):".format(input_data_name))
run_func_list.append(" # 共{}个输入".format(len(self.pd_graph.inputs_info)))
for k, v in self.pd_graph.inputs_info.items():
run_func_list.append(" # {}: 形状为{},类型为{}。".format(k, v[0], v[1]))
run_func_list.extend(
[" paddle.disable_static()",
" params, _ = fluid.load_dygraph('{}/model')".format(save_dir),
" model = {}()".format(self.pd_graph.name),
" model.set_dict(params)",
" model.eval()",
" out = model({})".format(input_data_name),
" return out"])
return "\n".join(run_func_list)
self.update_hierarchical_order()
self.convert_subgraph_to_layer()
self.update_parameters()
import_list = ["import paddle",
"import paddle.fluid as fluid",
"",]
import_str = "\n".join(import_list)
if not osp.exists(save_dir):
os.makedirs(save_dir)
f = open(osp.join(save_dir, 'x2paddle_code.py'), 'w')
f.write(import_str)
for code in self.codes:
f.write(code)
f.write("\n")
run_func = gen_main_code()
f.write(run_func)
f.close()
# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import copy
from x2paddle.optimizer.code_optimizer.parameter_tree import PamareterNode
NN_KERNEL_NAME = {"paddle.nn.BatchNorm": "bn",
"paddle.nn.LayerNorm": "layernorm",
"paddle.nn.Conv2D": "conv",
"paddle.nn.Embedding": "embedding",
"paddle.nn.Linear": "linear",
"paddle.nn.ReLU": "relu",
"paddle.nn.ReLU6": "relu",
"paddle.nn.Softmax": "softmax",
"paddle.nn.Softplus": "softplus",
"paddle.nn.Tanh": "tanh",
"paddle.nn.AvgPool2D": "pool",
"paddle.nn.MaxPool2D": "pool",
"paddle.nn.Pad1d": "pad",
"paddle.nn.Pad2d": "pad",
"paddle.nn.Pad3d": "pad",
"paddle.nn.Dropout": "dropout",
"paddle.nn.GELU": "gelu",
"paddle.nn.Hardtanh": "tanh",
"paddle.nn.LeakyReLU": "leakly_relu"}
NN_KERNEL_WITH_PARAMS = list(NN_KERNEL_NAME.keys())[:5]
def rename_layers(layers, param_tree=None):
""" 对子模块的输入输出等进行重命名。
"""
layers_cp = copy.deepcopy(layers)
name_dict = dict()
nn_param_nodes = list()
count = 0
nn_count_dict = dict()
module_count_dict = dict()
new_names = list()
for kernel in NN_KERNEL_NAME.keys():
nn_count_dict[kernel] = 0
def rename_sub_layers(sub_layers, count, is_block=False):
for layer_id, layer in sub_layers.items():
# 对输入重命名
for input_k, input_v in layer.inputs.items():
if input_v in name_dict:
layer.inputs[input_k] = name_dict[input_v]
else:
new_name = "x{}".format(count)
count += 1
layer.inputs[input_k] = new_name
name_dict[input_v] = new_name
# 对block重命名
for block in layer.blocks:
count = rename_sub_layers(block.layers,
count, is_block=True)
# 对输出重命名
if len(layer.outputs) == 0 and not is_block:
new_names.append("layer_id/{}".format(layer_id))
for i, output_v in enumerate(layer.outputs):
if output_v in name_dict:
layer.outputs[i] = name_dict[output_v]
if i == 0 and not is_block:
new_names.append(name_dict[output_v])
else:
if i == 0 and layer.kernel in NN_KERNEL_NAME.keys():
new_name = NN_KERNEL_NAME[layer.kernel] + str(nn_count_dict[layer.kernel])
param_node = PamareterNode(old_name=layer.outputs[0],
new_name=new_name)
nn_param_nodes.append(param_node)
if param_tree is not None:
param_tree.add_node(param_node)
layer.outputs[0] = new_name
nn_count_dict[layer.kernel] += 1
elif i == 0 and layer.kernel == "module":
old_name = layer.outputs[0].split("/")[0]
if old_name not in nn_count_dict:
nn_count_dict[old_name] = 0
else:
nn_count_dict[old_name] += 1
new_name = old_name + str(nn_count_dict[old_name])
if param_tree is not None:
param_node = param_tree.get_node(layer.outputs[0])
nn_param_nodes.append(param_node)
param_node.new_name = new_name
layer.outputs[0] = new_name
else:
old_name = layer.outputs[i]
new_name = "x{}".format(count)
count += 1
layer.outputs[i] = new_name
name_dict[output_v] = new_name
if layer.kernel == "self.create_parameter":
param_node = PamareterNode(old_name=old_name,
new_name=new_name)
nn_param_nodes.append(param_node)
if param_tree is not None:
param_tree.add_node(param_node)
if i == 0 and not is_block:
new_names.append(new_name)
# 对layer的attr进行重命名
for attr_k, attr_v in layer.attrs.items():
if isinstance(attr_v, str) and "'" not in attr_v \
and attr_v in name_dict:
layer.attrs[attr_k] = name_dict[attr_v]
return count
rename_sub_layers(layers_cp, count)
return layers_cp, nn_param_nodes, new_names
def gen_layer_code(graph, sub_layers, sub_layers_name, different_attrs=list()):
""" 根据sub_layers生成对应的Module代码。
Args:
graph (x2paddle.core.program.PaddleGraph): 整个Paddle图。
sub_layers (dict): 子图的id和其对应layer组成的字典。
sub_layers_name (str): 子图的名字。
different_attrs (list): 属性列表,这些属性表明在被调用时赋予不同值。
"""
def gen_codes(code_list, indent=0):
""" 根据code_list生成代码段。
Args:
code_list (list): 代码行组成的list。
indent (int): 每行空格的数量。
Returns:
str: 代码段。
"""
indent_blank = " " * indent
codes = []
for code_line in code_list:
if code_line.strip() == "":
codes.append('\n')
else:
codes.append(indent_blank + code_line + '\n')
return codes
def gen_head(inputs, different_attrs):
# 生成Layer的头部代码
head = gen_codes(["class {}(paddle.nn.Layer):".format(sub_layers_name)], indent=0)
# 生成init函数的头部代码
attrs_str = ", ".join(different_attrs)
init_func_head = \
gen_codes(["def __init__(self, {}):".format(attrs_str)], indent=1) + \
gen_codes(["super({}, self).__init__()".format(sub_layers_name)], indent=2)
# 生成forward函数的头部代码
input_data_name = ", ".join(inputs)
forward_func_head = \
gen_codes(["def forward(self, {}):".format(input_data_name)], indent=1)
return head, init_func_head, forward_func_head
init_func = []
forward_func = []
cur_outputs = list()
inputs = list()
outputs = list()
param_prefix_list = list()
input_id = 0
for layer_id, layer in sub_layers.items():
if layer_id not in graph.edges_out:
for index, output_name in enumerate(layer.outputs):
if layer.kernel.startswith("paddle.nn") and index == 0:
continue
if not output_name.startswith("x") or output_name in outputs \
or layer.kernel == "prim.assert" or \
layer.kernel == "prim.if" or layer.kernel == "prim.loop":
continue
elif output_name not in outputs:
outputs.append(output_name)
continue
for out_layer_id in graph.edges_out[layer_id]:
if out_layer_id not in sub_layers:
for index, output_name in enumerate(layer.outputs):
if layer.kernel.startswith("paddle.nn") and index == 0 and "functional" not in layer.kernel:
continue
if not output_name.startswith("x") or output_name in outputs \
or layer.kernel == "prim.assert" or \
layer.kernel == "prim.if" or layer.kernel == "prim.loop":
continue
else:
outputs.append(output_name)
no_output_count = 0
for i, (layer_id, layer) in enumerate(sub_layers.items()):
if ("paddle.nn" in layer.kernel and "functional" not in layer.kernel):
line = "self.{} = {}(".format(layer.outputs[0], layer.kernel)
for k, v in layer.attrs.items():
key_name = "{}_{}".format(layer.outputs[0], k)
if key_name in different_attrs:
line += "{}={}, ".format(k, key_name)
else:
line += "{}={}, ".format(k, v)
line = line.strip(", ")
line += ")"
init_func.extend(gen_codes([line], indent=2))
if len(layer.outputs) == 1:
line = layer.outputs[0]
elif len(layer.outputs) == 2:
line = layer.outputs[1]
else:
line = ','.join(layer.outputs[1:])
line += " = self.{}(".format(layer.outputs[0])
for k, v in layer.inputs.items():
if v not in cur_outputs and v not in inputs:
inputs.append(v)
line += "{}, ".format(v)
line = line.strip(", ")
line += ")"
forward_func.extend(gen_codes([line], indent=2))
if len(layer.outputs) == 1:
cur_outputs.append(layer.outputs[0])
else:
cur_outputs.extend(layer.outputs[1:])
elif "prim" in layer.kernel:
func_name = layer.kernel.replace(".", "_")
from x2paddle.op_mapper.dygraph.pytorch2paddle import prim2code
if hasattr(prim2code, func_name):
for k, v in layer.inputs.items():
if v not in cur_outputs and v not in inputs:
inputs.append(v)
func = getattr(prim2code, func_name)
func(
layer,
indent=2,
init_func=init_func,
forward_func=forward_func,
layer_id=layer_id,
different_attrs=different_attrs)
cur_outputs.extend(layer.outputs)
else:
raise Exception(
"The kind {} in paddle model is not supported yet.".
format(layer.kernel))
elif layer.kernel == "module":
line = "self.{} = {}(".format(layer.outputs[0], layer.attrs["module"])
layer.attrs.pop("module")
for k, v in layer.attrs.items():
key_name = "{}_{}".format(layer.outputs[0], k)
if key_name in different_attrs:
line += "{}={}, ".format(k, key_name)
else:
line += "{}={}, ".format(k, v)
line = line.strip(", ")
line += ")"
init_func.extend(gen_codes([line], indent=2))
if len(layer.outputs) == 2:
line = layer.outputs[1]
else:
line = ','.join(layer.outputs[1:])
line += " = self.{}(".format(layer.outputs[0])
for k, v in layer.inputs.items():
if v not in cur_outputs and v not in inputs:
inputs.append(v)
line += "{}, ".format(v)
line = line.strip(", ")
line += ")"
forward_func.extend(gen_codes([line], indent=2))
cur_outputs.extend(layer.outputs[1:])
else:
if layer.kernel == "paddle.to_tensor":
v = layer.attrs["data"]
if v not in cur_outputs and v not in inputs:
inputs.append(v)
if len(layer.outputs) == 1:
line = layer.outputs[0]
else:
line = ','.join(layer.outputs)
line += " = {}(".format(layer.kernel)
for k, v in layer.inputs.items():
if isinstance(v, list):
line += "{}=[{}], ".format(k, ", ".join(v))
for lv in v:
if lv not in cur_outputs and lv not in inputs:
inputs.append(lv)
else:
if v not in cur_outputs and v not in inputs:
inputs.append(v)
line += "{}={}, ".format(k, v)
for k, v in layer.attrs.items():
key_name = "{}_{}".format(layer.outputs[0], k)
if key_name in different_attrs:
line += "{}=self.{}, ".format(k, key_name)
init_func.extend(gen_codes(["self.{} = {}".format(key_name, key_name)], indent=2))
else:
line += "{}={}, ".format(k, v)
line = line.strip(", ")
line += ")"
if layer.kernel == "self.create_parameter":
init_func.extend(gen_codes(["self." + line], indent=2))
forward_func.extend(gen_codes(["{} = self.{}".format(layer.outputs[0],
layer.outputs[0])], indent=2))
else:
forward_func.extend(gen_codes([line], indent=2))
cur_outputs.extend(layer.outputs)
head, init_func_head, forward_func_head = gen_head(inputs, different_attrs)
output_data_name = ", ".join(outputs)
code_list = head + init_func_head + init_func + \
forward_func_head + forward_func + \
gen_codes(["return {}".format(output_data_name)], indent=2)
code_str = "".join(code_list)
return code_str
\ No newline at end of file
# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
class PamareterNode(object):
def __init__(self, old_name=None, new_name=None):
self.old_name = old_name
self.new_name = new_name
self.childs = list()
def add_child(self, child):
self.childs.append(child)
def has_child(self):
if len(self.childs) == 0:
return False
else:
return True
class PamareterTree(object):
def __init__(self):
self.nodes = list()
self.old2new = dict()
def add_node(self, node):
self.nodes.append(node)
def traverse(self):
tmp = list()
def recurs(node, prefix_name):
for child in node.childs:
child_prefix_name = prefix_name + "." + child.new_name
if child.has_child():
recurs(child, child_prefix_name)
else:
self.old2new[child.old_name] = child_prefix_name[1:]
recurs(self.nodes[-1], "")
def get_node(self, old_name):
for node in self.nodes:
if node.old_name == old_name:
return node
\ No newline at end of file
# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import copy
import pandas as pd
from x2paddle.optimizer.code_optimizer.layer_code_generator import rename_layers
def construct_attrs_table(sub_layers_list, node_name2sub_layers):
""" 构造不同属性的表格。
"""
def get_node_name(sub_layers):
for k, v in node_name2sub_layers.items():
if v == sub_layers:
node_name = k
break
return node_name
sub_layers = sub_layers_list[0]
_, _, new_names = rename_layers(sub_layers)
table = list()
node_names = list()
for sub_layers in sub_layers_list:
attrs = dict()
node_names.append(get_node_name(sub_layers))
for i, (layer_id, layer) in enumerate(sub_layers.items()):
for k, v in layer.attrs.items():
attrs[new_names[i] + "_{}".format(k)] = v
table.append(attrs)
pd_table = pd.DataFrame(table, index=node_names)
return pd_table
def get_inputs_outputs(pd_graph, layers):
inputs = list()
outputs = list()
cur_outputs = list()
layer_ids = list(layers.keys())
for layer_id, layer in layers.items():
# 获取输出节点名字
if layer_id not in pd_graph.edges_out:
for output_name in layer.outputs:
if not output_name.startswith("x") or output_name in outputs \
or layer.kernel == "prim.assert" or \
layer.kernel == "prim.if" or layer.kernel == "prim.loop":
continue
elif output_name not in outputs:
outputs.append(output_name)
else:
for out_layer_id in pd_graph.edges_out[layer_id]:
if out_layer_id not in layer_ids:
for output_name in layer.outputs:
if not output_name.startswith("x") or output_name in outputs \
or layer.kernel == "prim.assert" or \
layer.kernel == "prim.if" or layer.kernel == "prim.loop":
continue
else:
outputs.append(output_name)
# 获取输入节点名字
for k, v in layer.inputs.items():
if v not in cur_outputs and v not in inputs:
inputs.append(v)
if ("paddle.nn" in layer.kernel and "functional" not in layer.kernel):
cur_outputs.extend(layer.outputs[1:])
else:
cur_outputs.extend(layer.outputs)
return inputs, outputs
def get_inputs_count(pd_graph, sub_layers):
input_ct2sub_layer_id = dict()
for i, sub_layer in enumerate(sub_layers):
inputs, outputs = get_inputs_outputs(pd_graph, sub_layer)
if len(inputs) not in input_ct2sub_layer_id:
input_ct2sub_layer_id[len(inputs)] = [i]
else:
input_ct2sub_layer_id[len(inputs)].append(i)
return input_ct2sub_layer_id
def distinguish_sequential(pd_graph, module_name, sub_layers, sub_identifiers, node_name2sub_layers):
""" 获取不同的layers组成的序列
"""
def distinguish_sequential_by_inputs(part_layers, part_identifiers, part_module_name):
new_sub_layers = dict()
new_sub_sequentials = dict()
sequentials2attrs_table = dict()
input_ct2sub_layer_id = get_inputs_count(pd_graph, part_layers)
if len(input_ct2sub_layer_id) == 1:
new_sub_layers["{}".format(part_module_name)] = part_layers
new_sub_sequentials["{}".format(part_module_name)] = part_identifiers
sequentials2attrs_table["{}".format(part_module_name)] = construct_attrs_table(part_layers, node_name2sub_layers)
else:
for i, (k, indexes) in enumerate(input_ct2sub_layer_id.items()):
new_sub_layers["{}__{}".format(part_module_name, i)] = list()
new_sub_sequentials["{}__{}".format(part_module_name, i)] = list()
for index in indexes:
new_sub_layers["{}__{}".format(part_module_name, i)].append(part_layers[index])
new_sub_sequentials["{}__{}".format(part_module_name, i)].append(part_identifiers[index])
sequentials2attrs_table["{}__{}".format(part_module_name, i)] = \
construct_attrs_table(new_sub_layers["{}__{}".format(part_module_name, i)], node_name2sub_layers)
return new_sub_layers, new_sub_sequentials, sequentials2attrs_table
new_sub_layers = dict()
new_sub_sequentials = dict()
sequentials2attrs_table = dict()
identifiers_str_list = list()
for identifiers in sub_identifiers:
identifiers_str_list.append(", ".join(list(identifiers.values())))
identifiers_str_set = list(set(identifiers_str_list))
if len(identifiers_str_set) == 1:
return distinguish_sequential_by_inputs(sub_layers, sub_identifiers, module_name)
else:
for i in range(len(identifiers_str_set)):
new_sub_layers["{}{}".format(module_name, i)] = list()
new_sub_sequentials["{}{}".format(module_name, i)] = list()
no_same_module_count = 0
for j, identifiers in enumerate(sub_identifiers):
identifiers_str = identifiers_str_list[j]
for i in range(len(identifiers_str_set)):
if identifiers_str_set[i] == identifiers_str:
is_diff = False
if identifiers_str_set[i].replace(", ", "").isdigit() or module_name == "ModuleList":
new_sub_layers["{}{}".format(module_name, len(identifiers_str_set) + no_same_module_count)] = [sub_layers[j]]
new_sub_sequentials["{}{}".format(module_name, len(identifiers_str_set) + no_same_module_count)] = [identifiers]
no_same_module_count += 1
else:
new_sub_layers["{}{}".format(module_name, i)].append(sub_layers[j])
new_sub_sequentials["{}{}".format(module_name, i)].append(identifiers)
break
new_new_sub_layers = dict()
new_new_sub_sequentials = dict()
for k, v in new_sub_layers.items():
part_sub_layers, part_sub_sequentials, part_sequentials2attrs_table = \
distinguish_sequential_by_inputs(v, new_sub_sequentials[k], k)
new_new_sub_layers.update(part_sub_layers)
new_new_sub_sequentials.update(part_sub_sequentials)
sequentials2attrs_table.update(part_sequentials2attrs_table)
return new_new_sub_layers, new_new_sub_sequentials, sequentials2attrs_table
\ No newline at end of file
......@@ -32,3 +32,5 @@ from .reshape_fuser import DygraphReshapeFuser
from .reshape_fuse_pass import DygraphReshapeFusePass
from .tf_batchnorm_fuser import DygraphTFBatchNormFuser
from .tf_batchnorm_fuse_pass import DygraphTFBatchNormFusePass
from .trace_fc_fuser import TraceFcFuser
from .trace_fc_fuse_pass import TraceFcFusePass
......@@ -13,6 +13,7 @@
# limitations under the License.
import numpy as np
import copy
from x2paddle.optimizer.pattern_matcher import FuseBase
from x2paddle.core.program import PaddleGraph, PaddleLayer
from x2paddle.core.util import *
......@@ -21,11 +22,13 @@ from x2paddle.core.util import *
class DygraphAdaptivePool2dFuser(FuseBase):
def __init__(self):
super(DygraphAdaptivePool2dFuser, self).__init__(graph_type="dygraph")
self.patterns = list()
def build_pattern(self):
""" 描述需要替换的adaptive pool2d图结构。
adaptive pool2d层模式python实现代码示例:
x68 = fluid.layers.shape(input=x60)
模式一:
x68 = prim.shape(input=x60)
x69 = len(x68)
x70 = x69 <= 2
if x70 :
......@@ -38,57 +41,64 @@ class DygraphAdaptivePool2dFuser(FuseBase):
for _x79 in range(x77):
x80 = [6, 6][_x79]
x73.append(x80)
x81 = fluid.layers.adaptive_pool2d(input=x60, pool_size=x73, pool_type='avg')
x81 = paddle.nn.functional.adaptive_avg_pool2d(input=x60, pool_size=x73, pool_type='avg')
模式二:
x64 = x60.shape
x65 = len(x64)
x66 = x65 > 2
if x66 :
pass
else:
raise RaiseException('AssertionError: ')
x69 = self.pool2d3(x60)
"""
def gen_name(id):
return "x" + str(id)
self.pattern.add_layer(
"fluid.layers.shape",
# 模式一:
pattern = PaddleGraph(graph_type="dygraph")
pattern.add_layer(
"prim.shape",
inputs={'input': "pool-input-0"},
outputs=[gen_name(1)])
self.pattern.add_layer(
pattern.add_layer(
"prim.len", inputs={"input": gen_name(1)}, outputs=[gen_name(6)])
self.pattern.add_layer(
pattern.add_layer(
"prim.le", inputs={"x": gen_name(6)}, outputs=[gen_name(8)], y=2)
self.pattern.add_layer("prim.if", {'input': gen_name(8)}, [gen_name(9)])
if_layer = self.pattern.layers[list(self.pattern.layers.keys())[-1]]
pattern_block0 = PaddleGraph(if_layer, graph_type="dygraph")
pattern.add_layer("prim.if", {'input': gen_name(8)}, [gen_name(9)])
if_layer = pattern.layers[list(pattern.layers.keys())[-1]]
pattern_block0 = PaddleGraph(parent_layer=if_layer, graph_type="dygraph")
pattern_block0.add_layer(
"prim.exception",
inputs={},
outputs=[gen_name(9)],
input="Exception")
if_layer.add_block(pattern_block0)
pattern_block1 = PaddleGraph(if_layer, graph_type="dygraph")
pattern_block1 = PaddleGraph(parent_layer=if_layer, graph_type="dygraph")
if_layer.add_block(pattern_block1)
self.pattern.add_layer("prim.list", inputs={}, outputs=[gen_name(10)])
self.pattern.add_layer(
pattern.add_layer("prim.list", inputs={}, outputs=[gen_name(10)])
pattern.add_layer(
"prim.slice",
inputs={"input": gen_name(1), },
outputs=[gen_name(12)],
start=-1,
end=100,
step=1)
self.pattern.add_layer(
outputs=[gen_name(12)])
pattern.add_layer(
"prim.len", inputs={"input": gen_name(12)}, outputs=[gen_name(14)])
self.pattern.add_layer(
pattern.add_layer(
"prim.list",
inputs={"input1": gen_name(14)},
outputs=[gen_name(15)],
input0=2)
self.pattern.add_layer(
outputs=[gen_name(15)])
pattern.add_layer(
"prim.min", inputs={"input": gen_name(15)}, outputs=[gen_name(16)])
self.pattern.add_layer("prim.loop", {'input': gen_name(16)},
pattern.add_layer("prim.loop", {'input': gen_name(16)},
[gen_name(17), gen_name(18)])
loop_layer = self.pattern.layers[list(self.pattern.layers.keys())[-1]]
loop_layer = pattern.layers[list(pattern.layers.keys())[-1]]
pattern_block = PaddleGraph(loop_layer, graph_type="dygraph")
pattern_block.add_layer(
"prim.getitem",
inputs={"index": gen_name(18)},
outputs=[gen_name(19)],
list=[6, 6])
outputs=[gen_name(19)])
pattern_block.add_layer(
"prim.append",
inputs={"list": gen_name(10),
......@@ -97,14 +107,45 @@ class DygraphAdaptivePool2dFuser(FuseBase):
loop_layer.inputs["input-0"] = gen_name(10)
loop_layer.add_block(pattern_block)
pool_attrs = {'pool_type': string("avg")}
self.pattern.add_layer(
"fluid.layers.adaptive_pool2d",
pattern.add_layer(
"paddle.nn.functional.adaptive_avg_pool2d",
inputs={'input': "pool-input-0",
"pool_size": gen_name(10)},
outputs=[gen_name(21)],
**pool_attrs)
self.pattern.build(inputs={"input-0": "pool-input-0", })
pattern.build(inputs={"input-0": "pool-input-0", })
self.patterns.append(pattern)
# 模式二:
pattern = PaddleGraph(graph_type="dygraph")
pattern.add_layer(
"prim.shape",
inputs={'input': "pool-input-0"},
outputs=[gen_name(0)])
pattern.add_layer(
"prim.len", inputs={"input": gen_name(0)}, outputs=[gen_name(1)])
pattern.add_layer(
"prim.gt", inputs={"x": gen_name(1)}, outputs=[gen_name(2)], y=2)
pattern.add_layer("prim.if", {'input': gen_name(2)}, [gen_name(3)])
if_layer = pattern.layers[list(pattern.layers.keys())[-1]]
pattern_block0 = PaddleGraph(parent_layer=if_layer, graph_type="dygraph")
if_layer.add_block(pattern_block0)
pattern_block1 = PaddleGraph(parent_layer=if_layer, graph_type="dygraph")
pattern_block1.add_layer(
"prim.exception",
inputs={},
outputs=[gen_name(4)],
input="Exception")
if_layer.add_block(pattern_block1)
pattern.add_layer(
"paddle.nn.AdaptiveAvgPool2D",
inputs={"input": "pool-input-0"},
outputs=["pool1", gen_name(5)])
pattern.build(inputs={"input-0": "pool-input-0",
"input-1": "pool-input-0",})
self.patterns.append(pattern)
def insert_new_layer(self, graph, parameters, matches):
parameters = graph.parameters
new_layer = self.gen_new_layer(parameters, matches)
......@@ -114,20 +155,21 @@ class DygraphAdaptivePool2dFuser(FuseBase):
def gen_new_layer(self, parameters, matches):
layers_id = list(matches.keys())
layer = matches[layers_id[11]]
pool_size = layer.attrs["list"]
layer = matches[layers_id[0]]
input_name = layer.inputs["input"]
layer = matches[layers_id[-1]]
output_name = layer.outputs[0]
pool_type = layer.attrs["pool_type"]
attrs = dict()
attrs["pool_size"] = pool_size
attrs["pool_type"] = pool_type
new_layer = PaddleLayer(
layers_id[0],
"fluid.layers.adaptive_pool2d",
inputs={"input": input_name},
outputs=[output_name],
**attrs)
if matches[layers_id[-1]].kernel == "paddle.nn.functional.adaptive_avg_pool2d":
layer = matches[layers_id[11]]
pool_size = layer.attrs["list"]
layer = matches[layers_id[0]]
input_name = layer.inputs["input"]
layer = matches[layers_id[-1]]
output_name = layer.outputs[0]
attrs = dict()
attrs["output_size"] = pool_size
new_layer = PaddleLayer(
layers_id[0],
"paddle.nn.functional.adaptive_avg_pool2d",
inputs={"input": input_name},
outputs=[output_name],
**attrs)
else:
new_layer = copy.deepcopy(matches[layers_id[-1]])
return new_layer
......@@ -25,33 +25,33 @@ class DygraphBatchNorm2dFuser(FuseBase):
def build_pattern(self):
""" 描述需要替换的batchnorm2d图结构。
batchnorm2d层模式python实现代码示例:
x336 = fluid.layers.shape(input=x334)
x336 = len(x336)
x337 = x336 != 4
if x337 :
x2011 = x2009.shape
x2011 = len(x2011)
x2012 = x2011 != 4
if x2012 :
raise RaiseException('Exception')
if False :
x351 = fluid.layers.shape(input=x334)
x352 = x351[0]
x353 = len(x351)
x354 = x353 - 2
x357 = x352
for _x356 in range(x354):
x358 = _x356 + 2
x359 = x351[x358]
x360 = x357 * x359
x355 = x360
x361 = x355 == 1
if x361 :
x2026 = x2009.shape
x2027 = x2026[0]
x2028 = len(x2026)
x2029 = x2028 - 2
x2032 = x2027
for _x2031 in range(x2029):
x2033 = _x2031 + 2
x2034 = x2026[x2033]
x2035 = x2032 * x2034
x2030 = x2035
x2036 = x2030 == 1
if x2036 :
raise RaiseException('Exception')
x364 = self.batchnorm7(x334)
x2039 = self.batchnorm50(x2009)
"""
def gen_name(id):
return "x" + str(id)
self.pattern.add_layer(
"fluid.layers.shape",
"prim.shape",
inputs={'input': "bn-input-0"},
outputs=[gen_name(0)])
self.pattern.add_layer(
......@@ -60,20 +60,20 @@ class DygraphBatchNorm2dFuser(FuseBase):
"prim.ne", inputs={"x": gen_name(0)}, outputs=[gen_name(1)], y=4)
self.pattern.add_layer("prim.if", {'input': gen_name(1)}, [gen_name(2)])
if_layer1 = self.pattern.layers[list(self.pattern.layers.keys())[-1]]
pattern_block0 = PaddleGraph(if_layer1, graph_type="dygraph")
pattern_block0 = PaddleGraph(parent_layer=if_layer1, graph_type="dygraph")
pattern_block0.add_layer(
"prim.exception",
inputs={},
outputs=[gen_name(3)],
input="Exception")
if_layer1.add_block(pattern_block0)
pattern_block1 = PaddleGraph(if_layer1, graph_type="dygraph")
pattern_block1 = PaddleGraph(parent_layer=if_layer1, graph_type="dygraph")
if_layer1.add_block(pattern_block1)
self.pattern.add_layer("prim.if", {}, [gen_name(4)], input=False)
if_layer2 = self.pattern.layers[list(self.pattern.layers.keys())[-1]]
pattern_block0 = PaddleGraph(if_layer2, graph_type="dygraph")
pattern_block0 = PaddleGraph(parent_layer=if_layer2, graph_type="dygraph")
pattern_block0.add_layer(
"fluid.layers.shape",
"prim.shape",
inputs={'input': "bn-input-0"},
outputs=[gen_name(5)])
pattern_block0.add_layer(
......@@ -93,7 +93,7 @@ class DygraphBatchNorm2dFuser(FuseBase):
outputs=[gen_name(8.1), gen_name(10)])
loop_layer = pattern_block0.layers[list(pattern_block0.layers.keys())[
-1]]
pattern_block0_block0 = PaddleGraph(loop_layer, graph_type="dygraph")
pattern_block0_block0 = PaddleGraph(parent_layer=loop_layer, graph_type="dygraph")
pattern_block0_block0.add_layer(
"prim.add", inputs={"x": gen_name(10)}, outputs=[gen_name(11)], y=2)
pattern_block0_block0.add_layer(
......@@ -119,27 +119,24 @@ class DygraphBatchNorm2dFuser(FuseBase):
"prim.if", inputs={"input": gen_name(14)}, outputs=[gen_name(15)])
if_layer21 = pattern_block0.layers[list(pattern_block0.layers.keys())[
-1]]
pattern_block0_block0 = PaddleGraph(if_layer21, graph_type="dygraph")
pattern_block0_block0 = PaddleGraph(parent_layer=if_layer21, graph_type="dygraph")
pattern_block0_block0.add_layer(
"prim.exception",
inputs={},
outputs=[gen_name(15)],
input="Exception")
if_layer21.add_block(pattern_block0_block0)
pattern_block0_block1 = PaddleGraph(if_layer21, graph_type="dygraph")
pattern_block0_block1 = PaddleGraph(parent_layer=if_layer21, graph_type="dygraph")
if_layer21.add_block(pattern_block0_block1)
if_layer2.add_block(pattern_block0)
pattern_block1 = PaddleGraph(if_layer2, graph_type="dygraph")
pattern_block1 = PaddleGraph(parent_layer=if_layer2, graph_type="dygraph")
if_layer2.add_block(pattern_block1)
if_layer2.inputs["input-0"] = "bn-input-0"
self.pattern.add_layer(
"paddle.nn.BatchNorm",
inputs={"input": "bn-input-0"},
outputs=[gen_name(16), gen_name(17)],
is_test=True,
num_channels=160,
momentum=0.1,
epsilon=0.001)
is_test=True)
self.pattern.build(inputs={"input-0": "bn-input-0"})
def insert_new_layer(self, graph, parameters, matches):
......@@ -148,9 +145,6 @@ class DygraphBatchNorm2dFuser(FuseBase):
graph.layers[new_layer_id] = new_layer
matches.pop(new_layer_id)
# for layer in matches.values():
# print(layer.outputs)
# print("-------")
def gen_new_layer(self, parameters, matches):
layers_id = list(matches.keys())
......
......@@ -31,14 +31,14 @@ class DygraphFcFuser(FuseBase):
x134 = x133 == 2
if x134 :
classifier_6_weight = self.classifier_6_weight
x136 = fluid.layers.transpose(x=classifier_6_weight, perm=[1, 0])
x136 = paddle.transpose(x=classifier_6_weight, perm=[1, 0])
classifier_6_bias = self.classifier_6_bias
x137 = paddle.addmm(input=classifier_6_bias, x=x128, y=x136, beta=1, alpha=1)
x135 = x137
else:
classifier_6_weight = self.classifier_6_weight
x138 = fluid.layers.transpose(x=classifier_6_weight, perm=[1, 0])
x139 = fluid.layers.matmul(x=x128, y=x138)
x138 = paddle.transpose(x=classifier_6_weight, perm=[1, 0])
x139 = paddle.matmul(x=x128, y=x138)
classifier_6_bias = self.classifier_6_bias
x140 = x139 + 1 * classifier_6_bias
x135 = x140
......@@ -48,7 +48,7 @@ class DygraphFcFuser(FuseBase):
return "x" + str(id)
self.pattern.add_layer(
"fluid.layers.shape",
"prim.shape",
inputs={'input': "fc-input-0"},
outputs=[gen_name(2)])
self.pattern.add_layer(
......@@ -61,22 +61,20 @@ class DygraphFcFuser(FuseBase):
self.pattern.add_layer("prim.if", {'input': gen_name(3)}, [gen_name(4)])
self.pattern.outputs.append(gen_name(4))
if_layer1 = self.pattern.layers[list(self.pattern.layers.keys())[-1]]
pattern_block0 = PaddleGraph(if_layer1, graph_type="dygraph")
pattern_block0 = PaddleGraph(parent_layer=if_layer1, graph_type="dygraph")
pattern_block0.add_layer(
"fluid.dygraph.base.to_variable",
"self.create_parameter",
inputs={},
outputs=[gen_name(5)],
value="params[{}]".format(string(gen_name(5))))
outputs=[gen_name(5)])
pattern_block0.add_layer(
"fluid.layers.transpose",
"paddle.transpose",
inputs={"x": gen_name(5)},
outputs=[gen_name(6)],
perm=[1, 0])
pattern_block0.add_layer(
"fluid.dygraph.base.to_variable",
"self.create_parameter",
inputs={},
outputs=[gen_name(7)],
value="params[{}]".format(string(gen_name(7))))
outputs=[gen_name(7)])
pattern_block0.add_layer(
"paddle.addmm",
inputs={"input": gen_name(7),
......@@ -90,14 +88,13 @@ class DygraphFcFuser(FuseBase):
pattern_block0.add_layer(
"prim.equal", inputs={'input': gen_name(8)}, outputs=[gen_name(4)])
if_layer1.add_block(pattern_block0)
pattern_block1 = PaddleGraph(if_layer1, graph_type="dygraph")
pattern_block1 = PaddleGraph(parent_layer=if_layer1, graph_type="dygraph")
pattern_block1.add_layer(
"fluid.dygraph.base.to_variable",
"self.create_parameter",
inputs={},
outputs=[gen_name(5)],
value="params[{}]".format(string(gen_name(5))))
outputs=[gen_name(5)])
pattern_block1.add_layer(
"fluid.layers.transpose",
"paddle.transpose",
inputs={"x": gen_name(5)},
outputs=[gen_name(6)],
perm=[1, 0])
......@@ -108,10 +105,9 @@ class DygraphFcFuser(FuseBase):
outputs=[gen_name(9)])
if_layer1.inputs["input-1"] = "fc-input-0"
pattern_block1.add_layer(
"fluid.dygraph.base.to_variable",
"self.create_parameter",
inputs={},
outputs=[gen_name(12)],
value="params[{}]".format(string(gen_name(12))))
outputs=[gen_name(12)])
pattern_block1.add_layer(
"prim.add_",
inputs={"x": gen_name(9),
......@@ -137,9 +133,9 @@ class DygraphFcFuser(FuseBase):
layer = matches[layers_id[3]]
output_name = layer.outputs[0]
layer = matches[layers_id[4]]
weight_name = layer.attrs["value"][8:-2]
weight_name = layer.outputs[0]
layer = matches[layers_id[6]]
bias_name = layer.attrs["value"][8:-2]
bias_name = layer.outputs[0]
attrs = dict()
attrs["in_features"] = parameters[weight_name].shape[1]
attrs["out_features"] = parameters[weight_name].shape[0]
......
......@@ -12,6 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import copy
import numpy as np
from x2paddle.optimizer.pattern_matcher import FuseBase
from x2paddle.core.program import PaddleGraph, PaddleLayer
......@@ -32,135 +33,40 @@ class DygraphInterpolateBilinearFuser(FuseBase):
def build_pattern(self):
""" 描述需要替换的双线性插值图结构。
interpolate_bilinear层模式python实现代码示例:
x3016 = fluid.layers.shape(input=x3005)
x3016 = len(x3016)
x3017 = x3016 - 2
x3018 = []
for _x3020 in range(x3017):
x3018.append(None)
x3021 = (x3005, x8, None, None)
x3022 = fluid.layers.shape(input=x3005)
x3022 = len(x3022)
x3023 = x3022 == 3
if x3023 :
x2195 = x2181.shape
x2195 = len(x2195)
x2196 = x2195 - 2
x2197 = []
for _x2199 in range(x2196):
x2197.append(None)
x2200 = (x2181, x8, None, None)
...
x2267 = x2266 == 3
if x2267 :
raise RaiseException('Exception')
x3024 = None
x2268 = None
else:
x3026 = fluid.layers.shape(input=x3005)
x3026 = len(x3026)
x3027 = x3026 == 4
if x3027 :
x3044, x3045, x3046, x3047 = x3021
x3048 = x3045 is None
if x3048 :
x3051 = x3046 is None
x3049 = x3051
x3050 = x3045
else:
x3052 = x3045
x3049 = False
x3050 = x3052
if x3049 :
raise RaiseException('Exception')
x3055 = x3050 is not None
if x3055 :
x3058 = x3050
x3059 = x3046 is not None
x3056 = x3059
x3057 = x3058
else:
x3056 = False
x3057 = x3050
if x3056 :
raise RaiseException('Exception')
x3060 = None
x3061 = None
else:
x3060 = x3046
x3061 = x3057
x3063 = x3060 is not None
if x3063 :
x3065 = x3060
x3066 = len(x3065)
x3067 = x3066 != 2
if x3067 :
raise RaiseException('Exception')
x3064 = x3065
else:
x3064 = x3060
x3070 = x3061 is not None
if x3070 :
x3072 = x3061
x3071 = x3072
else:
x3071 = None
if x3070 :
x3073 = x3071
else:
x3074 = x3064 is not None
if x3074 :
x3076 = x3064
x3075 = x3076
else:
raise RaiseException('Exception')
x3075 = None
x3078 = x3047 is None
if x3078 :
x3080 = len(x3075)
x3081 = x3080 > 0
x3086 = 0
for x3083 in range(2147483647):
x3087 = x3075[x3086]
x3088 = math.floor(x3087)
x3089 = x3088 != x3087
if x3089 :
x3090 = False
x3091 = x3089
else:
x3090 = None
x3091 = None
if x3089 :
x3092 = x3090
x3093 = x3091
else:
x3092 = True
x3093 = x3089
x3094 = x3086 + 1
x3095 = x3094 < x3080
x3096 = x3095 and x3092
x3082 = x3093
x3083 = x3094
if x3082 :
import warnings
warnings.warn('The default behavior for interpolate/upsample with float scale_factor will change in 1.6.0 to align with other frameworks/libraries, and use scale_factor directly, instead of relying on the computed output size. If you wish to keep the old behavior, please set recompute_scale_factor=True. See the documentation of nn.Upsample for details. ', stacklevel=2)
x3099 = []
for _x3101 in range(2):
x3102 = _x3101 + 2
x3103 = fluid.layers.shape(x3044)[x3102]
x3104 = float(x3103)
x3105 = x3075[_x3101]
x3106 = x3104 * x3105
x3107 = math.floor(x3106)
x3099.append(x3107)
x3073 = x3099
x3108 = x3018[0]
x3109 = x3018[1]
x3073_isinstance = isinstance(x3073, paddle.fluid.Variable)
if x3073_isinstance :
x3073 = x3073.numpy().tolist()
assert x3108 == x3109, 'The x3108 must be x3109!'
x3110 = paddle.nn.functional.interpolate(x=x3005, size=x3073, scale_factor=x3108, align_corners=False, align_mode=0)
x3028 = x3110
x2270 = x2181.shape
x2270 = len(x2270)
x2271 = x2270 == 4
if x2271 :
x2274 = x2197[0]
x2275 = x2197[1]
x2233_isinstance = isinstance(x2233, paddle.fluid.Variable)
if x2233_isinstance :
x2233 = x2233.numpy().tolist()
x2276 = paddle.nn.functional.interpolate(x=x2181, size=x2233, scale_factor=x2274, align_corners=False, align_mode=0, mode='bilinear')
x2272 = x2276
else:
x3111 = fluid.layers.shape(input=x3005)
x3111 = len(x3111)
x3112 = x3111 == 5
if x3112 :
x2277 = x2181.shape
x2277 = len(x2277)
x2278 = x2277 == 5
if x2278 :
raise RaiseException('Exception')
else:
raise RaiseException('Exception')
x3028 = None
x3024 = x3028
x2272 = None
x2268 = x2272
"""
def gen_name(id):
......@@ -168,7 +74,7 @@ class DygraphInterpolateBilinearFuser(FuseBase):
if self.version_gt_150:
self.pattern.add_layer(
"fluid.layers.shape",
"prim.shape",
inputs={"input": "interpolate-input-0"},
outputs=[gen_name(9)])
self.pattern.add_layer(
......@@ -200,686 +106,36 @@ class DygraphInterpolateBilinearFuser(FuseBase):
"prim.tuple",
inputs={
"input0": "interpolate-input-0",
"input1": "interpolate-input-1",
"input1": "interpolate-input-4",
},
outputs=[gen_name(13)],
outputs=[gen_name(12)],
input2=None,
input3=None)
self.pattern.add_layer(
"fluid.layers.shape",
inputs={"input": "interpolate-input-0"},
outputs=[gen_name(14)])
self.pattern.add_layer(
"prim.len",
inputs={"input": gen_name(14)},
outputs=[gen_name(14)])
self.pattern.add_layer(
"prim.eq",
inputs={"x": gen_name(14)},
outputs=[gen_name(15)],
inputs={"x": "interpolate-input-2"},
outputs=[gen_name(10.1)],
y=3)
self.pattern.add_layer(
"prim.if",
inputs={"input": gen_name(15)},
outputs=[gen_name(16)])
if_layer1 = self.pattern.layers[list(self.pattern.layers.keys())[
-1]]
pattern_block = PaddleGraph(if_layer1, graph_type="dygraph")
pattern_block.add_layer(
"prim.exception",
inputs={},
outputs=[gen_name(17)],
input="Exception")
pattern_block.add_layer(
"prim.equal", inputs={}, outputs=[gen_name(16)], input=None)
if_layer1.add_block(pattern_block)
pattern_block = PaddleGraph(if_layer1, graph_type="dygraph")
pattern_block.add_layer(
"fluid.layers.shape",
inputs={"input": "interpolate-input-0"},
outputs=[gen_name(18)])
pattern_block.add_layer(
"prim.len",
inputs={"input": gen_name(18)},
outputs=[gen_name(18)])
pattern_block.add_layer(
"prim.eq",
inputs={"x": gen_name(18)},
outputs=[gen_name(19)],
y=4)
pattern_block.add_layer(
"prim.if",
inputs={"input": gen_name(19)},
outputs=[gen_name(20)])
if_layer2 = pattern_block.layers[list(pattern_block.layers.keys())[
-1]]
pattern_block_block = PaddleGraph(if_layer2, graph_type="dygraph")
pattern_block_block.add_layer(
"prim.tuple_unpack",
inputs={"input": gen_name(13)},
outputs=[
gen_name(34), gen_name(35), gen_name(36), gen_name(37)
])
pattern_block_block.add_layer(
"prim.is",
inputs={"x": gen_name(35)},
outputs=[gen_name(38)],
y=None)
pattern_block_block.add_layer(
"prim.if",
inputs={"input": gen_name(38)},
outputs=[gen_name(39), gen_name(40)])
if_layer3 = pattern_block_block.layers[list(
pattern_block_block.layers.keys())[-1]]
pattern_block_block_block = PaddleGraph(
if_layer3, graph_type="dygraph")
pattern_block_block_block.add_layer(
"prim.is",
inputs={"x": gen_name(36)},
outputs=[gen_name(41)],
y=None)
pattern_block_block_block.add_layer(
"prim.equal",
inputs={"input": gen_name(41)},
outputs=[gen_name(39)])
pattern_block_block_block.add_layer(
"prim.equal",
inputs={"input": gen_name(35)},
outputs=[gen_name(40)])
if_layer3.add_block(pattern_block_block_block)
pattern_block_block_block = PaddleGraph(
if_layer3, graph_type="dygraph")
pattern_block_block_block.add_layer(
"prim.equal",
inputs={"input": gen_name(35)},
outputs=[gen_name(42)])
pattern_block_block_block.add_layer(
"prim.equal", inputs={}, outputs=[gen_name(39)], input=False)
pattern_block_block_block.add_layer(
"prim.equal",
inputs={"input": gen_name(35)},
outputs=[gen_name(40)])
if_layer3.add_block(pattern_block_block_block)
if_layer3.inputs.update({
"input-0": gen_name(36),
'input-1': gen_name(35),
'input-2': gen_name(35),
})
pattern_block_block.add_layer(
"prim.if",
inputs={"input": gen_name(39)},
outputs=[gen_name(43)])
if_layer4 = pattern_block_block.layers[list(
pattern_block_block.layers.keys())[-1]]
pattern_block_block_block = PaddleGraph(
if_layer4, graph_type="dygraph")
pattern_block_block_block.add_layer(
"prim.exception",
inputs={},
outputs=[gen_name(44)],
input="Exception")
if_layer4.add_block(pattern_block_block_block)
pattern_block_block_block = PaddleGraph(
if_layer4, graph_type="dygraph")
if_layer4.add_block(pattern_block_block_block)
pattern_block_block.add_layer(
"prim.isnot",
inputs={"x": gen_name(40)},
outputs=[gen_name(45)],
y=None)
pattern_block_block.add_layer(
"prim.if",
inputs={"input": gen_name(45)},
outputs=[gen_name(46), gen_name(47)])
if_layer5 = pattern_block_block.layers[list(
pattern_block_block.layers.keys())[-1]]
pattern_block_block_block = PaddleGraph(
if_layer5, graph_type="dygraph")
pattern_block_block_block.add_layer(
"prim.equal",
inputs={"input": gen_name(40)},
outputs=[gen_name(48)])
pattern_block_block_block.add_layer(
"prim.isnot",
inputs={"x": gen_name(36)},
outputs=[gen_name(49)],
y=None)
pattern_block_block_block.add_layer(
"prim.equal",
inputs={"input": gen_name(49)},
outputs=[gen_name(46)])
pattern_block_block_block.add_layer(
"prim.equal",
inputs={"input": gen_name(48)},
outputs=[gen_name(47)])
if_layer5.add_block(pattern_block_block_block)
pattern_block_block_block = PaddleGraph(
if_layer5, graph_type="dygraph")
pattern_block_block_block.add_layer(
"prim.equal", inputs={}, outputs=[gen_name(46)], input=False)
pattern_block_block_block.add_layer(
"prim.equal",
inputs={"input": gen_name(40)},
outputs=[gen_name(47)])
if_layer5.add_block(pattern_block_block_block)
if_layer5.inputs.update({
"input-0": gen_name(40),
"input-1": gen_name(36),
"input-3": gen_name(40)
})
pattern_block_block.add_layer(
"prim.if",
inputs={"input": gen_name(46)},
outputs=[gen_name(50), gen_name(51)])
if_layer6 = pattern_block_block.layers[list(
pattern_block_block.layers.keys())[-1]]
pattern_block_block_block = PaddleGraph(
if_layer6, graph_type="dygraph")
pattern_block_block_block.add_layer(
"prim.exception",
inputs={},
outputs=[gen_name(52)],
input="Exception")
pattern_block_block_block.add_layer(
"prim.equal", inputs={}, outputs=[gen_name(50)], input=None)
pattern_block_block_block.add_layer(
"prim.equal", inputs={}, outputs=[gen_name(51)], input=None)
if_layer6.add_block(pattern_block_block_block)
pattern_block_block_block = PaddleGraph(
if_layer6, graph_type="dygraph")
pattern_block_block_block.add_layer(
"prim.equal",
inputs={"input": gen_name(36)},
outputs=[gen_name(50)])
pattern_block_block_block.add_layer(
"prim.equal",
inputs={"input": gen_name(47)},
outputs=[gen_name(51)])
if_layer6.add_block(pattern_block_block_block)
if_layer6.inputs.update({
"input-0": gen_name(36),
"input-1": gen_name(47)
})
pattern_block_block.add_layer(
"prim.isnot",
inputs={"x": gen_name(50)},
outputs=[gen_name(53)],
y=None)
pattern_block_block.add_layer(
"prim.if",
inputs={"input": gen_name(53)},
outputs=[gen_name(54)])
if_layer7 = pattern_block_block.layers[list(
pattern_block_block.layers.keys())[-1]]
pattern_block_block_block = PaddleGraph(
if_layer7, graph_type="dygraph")
pattern_block_block_block.add_layer(
"prim.equal",
inputs={"input": gen_name(50)},
outputs=[gen_name(55)])
pattern_block_block_block.add_layer(
"prim.len",
inputs={"input": gen_name(55)},
outputs=[gen_name(56)])
pattern_block_block_block.add_layer(
"prim.ne",
inputs={"x": gen_name(56)},
outputs=[gen_name(57)],
y=2)
pattern_block_block_block.add_layer(
"prim.if",
inputs={"input": gen_name(57)},
outputs=[gen_name(58)])
if_layer8 = pattern_block_block_block.layers[list(
pattern_block_block_block.layers.keys())[-1]]
pattern_block_block_block_block = PaddleGraph(
if_layer8, graph_type="dygraph")
pattern_block_block_block_block.add_layer(
"prim.exception",
inputs={},
outputs=[gen_name(59)],
input="Exception")
if_layer8.add_block(pattern_block_block_block_block)
pattern_block_block_block_block = PaddleGraph(
if_layer8, graph_type="dygraph")
if_layer8.add_block(pattern_block_block_block_block)
pattern_block_block_block.add_layer(
"prim.equal",
inputs={"input": gen_name(55)},
outputs=[gen_name(54)])
if_layer7.add_block(pattern_block_block_block)
pattern_block_block_block = PaddleGraph(
if_layer7, graph_type="dygraph")
pattern_block_block_block.add_layer(
"prim.equal",
inputs={"input": gen_name(50)},
outputs=[gen_name(54)])
if_layer7.add_block(pattern_block_block_block)
if_layer7.inputs.update({
"input-0": gen_name(50),
"input-1": gen_name(50)
})
pattern_block_block.add_layer(
"prim.isnot",
inputs={"x": gen_name(51)},
outputs=[gen_name(60)],
y=None)
pattern_block_block.add_layer(
"prim.if",
inputs={"input": gen_name(60)},
outputs=[gen_name(61)])
if_layer9 = pattern_block_block.layers[list(
pattern_block_block.layers.keys())[-1]]
pattern_block_block_block = PaddleGraph(
if_layer9, graph_type="dygraph")
pattern_block_block_block.add_layer(
"prim.equal",
inputs={"input": gen_name(51)},
outputs=[gen_name(62)])
pattern_block_block_block.add_layer(
"prim.equal",
inputs={"input": gen_name(62)},
outputs=[gen_name(61)])
if_layer9.add_block(pattern_block_block_block)
pattern_block_block_block = PaddleGraph(
if_layer9, graph_type="dygraph")
pattern_block_block_block.add_layer(
"prim.isnot",
inputs={"x": gen_name(54)},
outputs=[gen_name(64)],
y=None)
pattern_block_block_block.add_layer(
"prim.if",
inputs={"input": gen_name(64)},
outputs=[gen_name(65)])
if_layer11 = pattern_block_block_block.layers[list(
pattern_block_block_block.layers.keys())[-1]]
pattern_block_block_block_block = PaddleGraph(
if_layer11, graph_type="dygraph")
pattern_block_block_block_block.add_layer(
"prim.equal",
inputs={"input": gen_name(54)},
outputs=[gen_name(66)])
pattern_block_block_block_block.add_layer(
"prim.equal",
inputs={"input": gen_name(66)},
outputs=[gen_name(65)])
if_layer11.add_block(pattern_block_block_block_block)
pattern_block_block_block_block = PaddleGraph(
if_layer11, graph_type="dygraph")
pattern_block_block_block_block.add_layer(
"prim.exception",
inputs={},
outputs=[gen_name(67)],
input="Exception")
pattern_block_block_block_block.add_layer(
"prim.equal", inputs={}, outputs=[gen_name(65)], input=None)
if_layer11.add_block(pattern_block_block_block_block)
if_layer11.inputs.update({"input-0": gen_name(54), })
pattern_block_block_block.add_layer(
"prim.is",
inputs={"x": gen_name(37)},
outputs=[gen_name(68)],
y=None)
pattern_block_block_block.add_layer(
"prim.if",
inputs={"input": gen_name(68)},
outputs=[gen_name(69)])
if_layer12 = pattern_block_block_block.layers[list(
pattern_block_block_block.layers.keys())[-1]]
pattern_block_block_block_block = PaddleGraph(
if_layer12, graph_type="dygraph")
pattern_block_block_block_block.add_layer(
"prim.len",
inputs={"input": gen_name(65)},
outputs=[gen_name(70)])
pattern_block_block_block_block.add_layer(
"prim.gt",
inputs={"x": gen_name(70)},
outputs=[gen_name(71)],
y=0)
pattern_block_block_block_block.add_layer(
"prim.equal", inputs={}, outputs=[gen_name(72)], input=0)
pattern_block_block_block_block.add_layer(
"prim.loop",
inputs={},
outputs=[gen_name(74), gen_name(75), gen_name(76.1)],
input=2147483647)
loop_layer = pattern_block_block_block_block.layers[list(
pattern_block_block_block_block.layers.keys())[-1]]
pattern_loop_block = PaddleGraph(loop_layer, graph_type="dygraph")
pattern_loop_block.add_layer(
"prim.getitem",
inputs={"list": gen_name(65),
"element": gen_name(72)},
outputs=[gen_name(74.1)])
pattern_loop_block.add_layer(
"prim.floor",
inputs={"input": gen_name(74.1)},
outputs=[gen_name(75.1)])
pattern_loop_block.add_layer(
"prim.ne",
inputs={"x": gen_name(75.1),
"y": gen_name(74.1)},
outputs=[gen_name(76)])
pattern_loop_block.add_layer(
"prim.if",
inputs={"input": gen_name(76)},
outputs=[gen_name(77)])
if_layer13 = pattern_loop_block.layers[list(
pattern_loop_block.layers.keys())[-1]]
pattern_loop_block_block = PaddleGraph(
if_layer13, graph_type="dygraph")
pattern_loop_block_block.add_layer(
"prim.equal", inputs={}, outputs=[gen_name(77)], input=False)
if_layer13.add_block(pattern_loop_block_block)
pattern_loop_block_block = PaddleGraph(
if_layer13, graph_type="dygraph")
pattern_loop_block_block.add_layer(
"prim.equal", inputs={}, outputs=[gen_name(77)], input=True)
if_layer13.add_block(pattern_loop_block_block)
pattern_loop_block.add_layer(
"prim.add",
inputs={"x": gen_name(72)},
outputs=[gen_name(81)],
y=1)
pattern_loop_block.add_layer(
"prim.lt",
inputs={"x": gen_name(81),
"y": gen_name(70)},
outputs=[gen_name(82)])
pattern_loop_block.add_layer(
"prim.and",
inputs={"x": gen_name(82),
"y": gen_name(77)},
outputs=[gen_name(83)])
pattern_loop_block.add_layer(
"prim.equal",
inputs={"input": gen_name(76)},
outputs=[gen_name(74)])
pattern_loop_block.add_layer(
"prim.equal",
inputs={"input": gen_name(81)},
outputs=[gen_name(75)])
loop_layer.add_block(pattern_loop_block)
loop_layer.inputs.update({
"input-0": gen_name(65),
"input-1": gen_name(72),
"input-2": gen_name(72),
"input-3": gen_name(70)
})
pattern_block_block_block_block.add_layer(
"prim.if",
inputs={"input": gen_name(74)},
outputs=[gen_name(84)])
if_layer15 = pattern_block_block_block_block.layers[list(
pattern_block_block_block_block.layers.keys())[-1]]
pattern_block_block_block_block_block = PaddleGraph(
if_layer15, graph_type="dygraph")
pattern_block_block_block_block_block.add_layer(
"prim.warnings",
inputs={},
outputs=[gen_name(85)],
stacklevel=2,
input="...")
if_layer15.add_block(pattern_block_block_block_block_block)
pattern_block_block_block_block_block = PaddleGraph(
if_layer15, graph_type="dygraph")
if_layer15.add_block(pattern_block_block_block_block_block)
if_layer12.add_block(pattern_block_block_block_block)
pattern_block_block_block_block = PaddleGraph(
if_layer12, graph_type="dygraph")
if_layer12.add_block(pattern_block_block_block_block)
if_layer12.inputs.update({
"input-0": gen_name(65),
"input-1": gen_name(65),
})
pattern_block_block_block.add_layer(
"prim.list", inputs={}, outputs=[gen_name(86)])
pattern_block_block_block.add_layer(
"prim.loop",
inputs={},
outputs=[gen_name(87), gen_name(88)],
input=2)
loop_layer = pattern_block_block_block.layers[list(
pattern_block_block_block.layers.keys())[-1]]
pattern_loop_block = PaddleGraph(loop_layer, graph_type="dygraph")
pattern_loop_block.add_layer(
"prim.add",
inputs={"x": gen_name(88)},
outputs=[gen_name(89)],
y=2)
pattern_loop_block.add_layer(
"prim.shape_dim",
inputs={"input": gen_name(34),
"dim": gen_name(89)},
outputs=[gen_name(90)])
pattern_loop_block.add_layer(
"prim.float",
inputs={"input": gen_name(90)},
outputs=[gen_name(91)])
pattern_loop_block.add_layer(
"prim.getitem",
inputs={"list": gen_name(65),
"element": gen_name(88)},
outputs=[gen_name(92)])
pattern_loop_block.add_layer(
"prim.mul",
inputs={"x": gen_name(91),
"y": gen_name(92)},
outputs=[gen_name(93)])
pattern_loop_block.add_layer(
"prim.floor",
inputs={"input": gen_name(93)},
outputs=[gen_name(94)])
pattern_loop_block.add_layer(
"prim.append",
inputs={"list": gen_name(86),
"element": gen_name(94)},
outputs=[])
loop_layer.add_block(pattern_loop_block)
loop_layer.inputs.update({
"input-0": gen_name(34),
"input-1": gen_name(65),
"input-2": gen_name(86)
})
pattern_block_block_block.add_layer(
"prim.equal",
inputs={"input": gen_name(86)},
outputs=[gen_name(61)])
if_layer9.add_block(pattern_block_block_block)
if_layer9.inputs.update({
"input-0": gen_name(51),
"input-1": gen_name(54),
"input-2": gen_name(54),
"input-3": gen_name(37),
"input-4": gen_name(34)
})
pattern_block_block.add_layer(
"prim.getitem",
inputs={"list": gen_name(11)},
outputs=[gen_name(95)],
element=0)
pattern_block_block.add_layer(
"prim.getitem",
inputs={"list": gen_name(11)},
outputs=[gen_name(96)],
element=1)
pattern_block_block.add_layer(
"prim.isinstance",
inputs={"input": gen_name(61)},
outputs=["interpolate-input-0_isinstance"],
cls="paddle.fluid.Variable")
pattern_block_block.add_layer(
"prim.if", {"input": "interpolate-input-0_isinstance"},
outputs=["interpolate-input-0_if1"])
if_layer_isinstance = pattern_block_block.layers[list(
pattern_block_block.layers.keys())[-1]]
pattern_block_block_block = PaddleGraph(
if_layer_isinstance, graph_type="dygraph")
pattern_block_block_block.add_layer(
"prim.var2list",
inputs={"input": gen_name(61)},
outputs=[gen_name(61)])
if_layer_isinstance.add_block(pattern_block_block_block)
pattern_block_block_block = PaddleGraph(
if_layer_isinstance, graph_type="dygraph")
if_layer_isinstance.add_block(pattern_block_block_block)
if_layer_isinstance.inputs["input-0"] = gen_name(61)
pattern_block_block.add_layer(
"prim.assert",
inputs={"key": gen_name(95),
"value": gen_name(96)},
outputs=[gen_name(97) + "_assert"],
type="eq")
pattern_block_block.add_layer(
"paddle.nn.functional.interpolate",
inputs={
"input": "interpolate-input-0",
"size": gen_name(61),
"scale_factor": gen_name(95)
},
outputs=[gen_name(97)],
align_corners=False,
align_mode=0)
pattern_block_block.add_layer(
"prim.equal",
inputs={"input": gen_name(97)},
outputs=[gen_name(20)])
if_layer2.add_block(pattern_block_block)
pattern_block_block = PaddleGraph(if_layer2, graph_type="dygraph")
pattern_block_block.add_layer(
"fluid.layers.shape",
inputs={"input": "interpolate-input-0"},
outputs=[gen_name(98)])
pattern_block_block.add_layer(
"prim.len",
inputs={"input": gen_name(98)},
outputs=[gen_name(98)])
pattern_block_block.add_layer(
"prim.eq",
inputs={"x": gen_name(98)},
outputs=[gen_name(99)],
y=5)
pattern_block_block.add_layer(
"prim.if",
inputs={"input": gen_name(99)},
outputs=[gen_name(100)])
if_layer16 = pattern_block_block.layers[list(
pattern_block_block.layers.keys())[-1]]
pattern_block_block_block = PaddleGraph(
if_layer16, graph_type="dygraph")
pattern_block_block_block.add_layer(
"prim.exception",
inputs={},
outputs=[gen_name(101)],
input="Exception")
if_layer16.add_block(pattern_block_block_block)
pattern_block_block_block = PaddleGraph(
if_layer16, graph_type="dygraph")
pattern_block_block_block.add_layer(
"prim.exception",
inputs={},
outputs=[gen_name(102)],
input="Exception")
if_layer16.add_block(pattern_block_block_block)
pattern_block_block.add_layer(
"prim.equal", inputs={}, outputs=[gen_name(20)], input=None)
if_layer2.add_block(pattern_block_block)
if_layer2.inputs.update({
"input-0": gen_name(13),
"input-1": gen_name(13),
"input-2": "interpolate-input-0",
"input-3": gen_name(11),
"input-5": gen_name(11),
})
pattern_block.add_layer(
"prim.equal",
inputs={"input": gen_name(20)},
outputs=[gen_name(16)])
if_layer1.add_block(pattern_block)
if_layer1.inputs.update({
"input-2": "interpolate-input-0",
"input-4": gen_name(13),
"input-7": gen_name(11),
"input-9": gen_name(11),
"input-11": "interpolate-input-0",
"input-12": "interpolate-input-0",
})
self.pattern.build(inputs={
"input-0": "interpolate-input-0",
"input-1": "interpolate-input-1"
})
else:
self.pattern.add_layer(
"fluid.layers.shape",
inputs={"input": "interpolate-input-0"},
outputs=[gen_name(9)])
self.pattern.add_layer(
"prim.len",
inputs={"input": gen_name(9)},
outputs=[gen_name(9)])
self.pattern.add_layer(
"prim.sub",
inputs={"x": gen_name(9)},
outputs=[gen_name(10)],
y=2)
self.pattern.add_layer(
"prim.list", inputs={}, outputs=[gen_name(11)])
self.pattern.add_layer(
"prim.loop",
inputs={"input": gen_name(10)},
outputs=[gen_name(12.1), gen_name(12.2)])
loop_layer = self.pattern.layers[list(self.pattern.layers.keys())[
-1]]
pattern_block = PaddleGraph(loop_layer, graph_type="dygraph")
pattern_block.add_layer(
"prim.append",
inputs={"list": gen_name(11)},
outputs=[],
element=None)
loop_layer.inputs["input-0"] = gen_name(11)
loop_layer.add_block(pattern_block)
self.pattern.add_layer(
"prim.tuple",
inputs={
"input0": "interpolate-input-0",
"input1": "interpolate-input-1",
},
outputs=[gen_name(13)],
input2=None,
input3=None)
self.pattern.add_layer(
"fluid.layers.shape",
inputs={"input": "interpolate-input-0"},
outputs=[gen_name(14)])
self.pattern.add_layer(
"prim.len",
inputs={"input": gen_name(14)},
inputs={"input": gen_name(10.1)},
outputs=[gen_name(14)])
self.pattern.add_layer(
"prim.eq",
inputs={"x": gen_name(14)},
outputs=[gen_name(15)],
y=3)
self.pattern.add_layer(
"prim.if",
inputs={"input": gen_name(15)},
outputs=[gen_name(16)])
if_layer1 = self.pattern.layers[list(self.pattern.layers.keys())[
-1]]
pattern_block = PaddleGraph(if_layer1, graph_type="dygraph")
pattern_block = PaddleGraph(parent_layer=if_layer1, graph_type="dygraph")
pattern_block.add_layer(
"prim.exception",
inputs={},
outputs=[gen_name(17)],
outputs=[gen_name(15)],
input="Exception")
pattern_block.add_layer(
"prim.equal", inputs={}, outputs=[gen_name(16)], input=None)
"prim.equal", inputs={}, outputs=[gen_name(14)], input=None)
if_layer1.add_block(pattern_block)
pattern_block = PaddleGraph(if_layer1, graph_type="dygraph")
pattern_block = PaddleGraph(parent_layer=if_layer1, graph_type="dygraph")
pattern_block.add_layer(
"fluid.layers.shape",
"prim.shape",
inputs={"input": "interpolate-input-0"},
outputs=[gen_name(18)])
pattern_block.add_layer(
......@@ -891,524 +147,28 @@ class DygraphInterpolateBilinearFuser(FuseBase):
inputs={"x": gen_name(18)},
outputs=[gen_name(19)],
y=4)
pattern_block.add_layer(
"prim.if",
inputs={"input": gen_name(19)},
outputs=[gen_name(20)])
if_layer2 = pattern_block.layers[list(pattern_block.layers.keys())[
-1]]
pattern_block_block = PaddleGraph(if_layer2, graph_type="dygraph")
pattern_block_block.add_layer(
"prim.tuple_unpack",
inputs={"input": gen_name(13)},
outputs=[
gen_name(34), gen_name(35), gen_name(36), gen_name(37)
])
pattern_block_block.add_layer(
"prim.is",
inputs={"x": gen_name(35)},
outputs=[gen_name(38)],
y=None)
pattern_block_block.add_layer(
"prim.if",
inputs={"input": gen_name(38)},
outputs=[gen_name(39), gen_name(40)])
if_layer3 = pattern_block_block.layers[list(
pattern_block_block.layers.keys())[-1]]
pattern_block_block_block = PaddleGraph(
if_layer3, graph_type="dygraph")
pattern_block_block_block.add_layer(
"prim.is",
inputs={"x": gen_name(36)},
outputs=[gen_name(41)],
y=None)
pattern_block_block_block.add_layer(
"prim.equal",
inputs={"input": gen_name(41)},
outputs=[gen_name(39)])
pattern_block_block_block.add_layer(
"prim.equal",
inputs={"input": gen_name(35)},
outputs=[gen_name(40)])
if_layer3.add_block(pattern_block_block_block)
pattern_block_block_block = PaddleGraph(
if_layer3, graph_type="dygraph")
pattern_block_block_block.add_layer(
"prim.equal",
inputs={"input": gen_name(35)},
outputs=[gen_name(42)])
pattern_block_block_block.add_layer(
"prim.equal", inputs={}, outputs=[gen_name(39)], input=False)
pattern_block_block_block.add_layer(
"prim.equal",
inputs={"input": gen_name(35)},
outputs=[gen_name(40)])
if_layer3.add_block(pattern_block_block_block)
if_layer3.inputs.update({
"input-0": gen_name(36),
'input-1': gen_name(35),
'input-2': gen_name(35),
})
pattern_block_block.add_layer(
"prim.if",
inputs={"input": gen_name(39)},
outputs=[gen_name(43)])
if_layer4 = pattern_block_block.layers[list(
pattern_block_block.layers.keys())[-1]]
pattern_block_block_block = PaddleGraph(
if_layer4, graph_type="dygraph")
pattern_block_block_block.add_layer(
"prim.exception",
inputs={},
outputs=[gen_name(44)],
input="Exception")
if_layer4.add_block(pattern_block_block_block)
pattern_block_block_block = PaddleGraph(
if_layer4, graph_type="dygraph")
if_layer4.add_block(pattern_block_block_block)
pattern_block_block.add_layer(
"prim.isnot",
inputs={"x": gen_name(40)},
outputs=[gen_name(45)],
y=None)
pattern_block_block.add_layer(
"prim.if",
inputs={"input": gen_name(45)},
outputs=[gen_name(46), gen_name(47)])
if_layer5 = pattern_block_block.layers[list(
pattern_block_block.layers.keys())[-1]]
pattern_block_block_block = PaddleGraph(
if_layer5, graph_type="dygraph")
pattern_block_block_block.add_layer(
"prim.equal",
inputs={"input": gen_name(40)},
outputs=[gen_name(48)])
pattern_block_block_block.add_layer(
"prim.isnot",
inputs={"x": gen_name(36)},
outputs=[gen_name(49)],
y=None)
pattern_block_block_block.add_layer(
"prim.equal",
inputs={"input": gen_name(49)},
outputs=[gen_name(46)])
pattern_block_block_block.add_layer(
"prim.equal",
inputs={"input": gen_name(48)},
outputs=[gen_name(47)])
if_layer5.add_block(pattern_block_block_block)
pattern_block_block_block = PaddleGraph(
if_layer5, graph_type="dygraph")
pattern_block_block_block.add_layer(
"prim.equal", inputs={}, outputs=[gen_name(46)], input=False)
pattern_block_block_block.add_layer(
"prim.equal",
inputs={"input": gen_name(40)},
outputs=[gen_name(47)])
if_layer5.add_block(pattern_block_block_block)
if_layer5.inputs.update({
"input-0": gen_name(40),
"input-1": gen_name(36),
"input-3": gen_name(40)
})
pattern_block_block.add_layer(
"prim.if",
inputs={"input": gen_name(46)},
outputs=[gen_name(50), gen_name(51)])
if_layer6 = pattern_block_block.layers[list(
pattern_block_block.layers.keys())[-1]]
pattern_block_block_block = PaddleGraph(
if_layer6, graph_type="dygraph")
pattern_block_block_block.add_layer(
"prim.exception",
inputs={},
outputs=[gen_name(52)],
input="Exception")
pattern_block_block_block.add_layer(
"prim.equal", inputs={}, outputs=[gen_name(50)], input=None)
pattern_block_block_block.add_layer(
"prim.equal", inputs={}, outputs=[gen_name(51)], input=None)
if_layer6.add_block(pattern_block_block_block)
pattern_block_block_block = PaddleGraph(
if_layer6, graph_type="dygraph")
pattern_block_block_block.add_layer(
"prim.equal",
inputs={"input": gen_name(36)},
outputs=[gen_name(50)])
pattern_block_block_block.add_layer(
"prim.equal",
inputs={"input": gen_name(47)},
outputs=[gen_name(51)])
if_layer6.add_block(pattern_block_block_block)
if_layer6.inputs.update({
"input-0": gen_name(36),
"input-1": gen_name(47)
})
pattern_block_block.add_layer(
"prim.isnot",
inputs={"x": gen_name(50)},
outputs=[gen_name(53)],
y=None)
pattern_block_block.add_layer(
"prim.if",
inputs={"input": gen_name(53)},
outputs=[gen_name(54)])
if_layer7 = pattern_block_block.layers[list(
pattern_block_block.layers.keys())[-1]]
pattern_block_block_block = PaddleGraph(
if_layer7, graph_type="dygraph")
pattern_block_block_block.add_layer(
"prim.equal",
inputs={"input": gen_name(50)},
outputs=[gen_name(55)])
pattern_block_block_block.add_layer(
"prim.len",
inputs={"input": gen_name(55)},
outputs=[gen_name(56)])
pattern_block_block_block.add_layer(
"prim.ne",
inputs={"x": gen_name(56)},
outputs=[gen_name(57)],
y=2)
pattern_block_block_block.add_layer(
"prim.if",
inputs={"input": gen_name(57)},
outputs=[gen_name(58)])
if_layer8 = pattern_block_block_block.layers[list(
pattern_block_block_block.layers.keys())[-1]]
pattern_block_block_block_block = PaddleGraph(
if_layer8, graph_type="dygraph")
pattern_block_block_block_block.add_layer(
"prim.exception",
inputs={},
outputs=[gen_name(59)],
input="Exception")
if_layer8.add_block(pattern_block_block_block_block)
pattern_block_block_block_block = PaddleGraph(
if_layer8, graph_type="dygraph")
if_layer8.add_block(pattern_block_block_block_block)
pattern_block_block_block.add_layer(
"prim.equal",
inputs={"input": gen_name(55)},
outputs=[gen_name(54)])
if_layer7.add_block(pattern_block_block_block)
pattern_block_block_block = PaddleGraph(
if_layer7, graph_type="dygraph")
pattern_block_block_block.add_layer(
"prim.equal",
inputs={"input": gen_name(50)},
outputs=[gen_name(54)])
if_layer7.add_block(pattern_block_block_block)
if_layer7.inputs.update({
"input-0": gen_name(50),
"input-1": gen_name(50)
})
pattern_block_block.add_layer(
"prim.isnot",
inputs={"x": gen_name(51)},
outputs=[gen_name(60)],
y=None)
pattern_block_block.add_layer(
"prim.if",
inputs={"input": gen_name(60)},
outputs=[gen_name(61)])
if_layer9 = pattern_block_block.layers[list(
pattern_block_block.layers.keys())[-1]]
pattern_block_block_block = PaddleGraph(
if_layer9, graph_type="dygraph")
pattern_block_block_block.add_layer(
"prim.equal",
inputs={"input": gen_name(51)},
outputs=[gen_name(62)])
pattern_block_block_block.add_layer(
"prim.equal",
inputs={"input": gen_name(62)},
outputs=[gen_name(61)])
if_layer9.add_block(pattern_block_block_block)
pattern_block_block_block = PaddleGraph(
if_layer9, graph_type="dygraph")
pattern_block_block_block.add_layer(
"prim.equal", inputs={}, outputs=[gen_name(61)], input=None)
if_layer9.add_block(pattern_block_block_block)
if_layer9.inputs.update({"input-0": gen_name(51)})
pattern_block_block.add_layer(
"prim.if",
inputs={"input": gen_name(60)},
outputs=[gen_name(63)])
if_layer10 = pattern_block_block.layers[list(
pattern_block_block.layers.keys())[-1]]
pattern_block_block_block = PaddleGraph(
if_layer10, graph_type="dygraph")
pattern_block_block_block.add_layer(
"prim.equal",
inputs={"input": gen_name(61)},
outputs=[gen_name(63)])
if_layer10.add_block(pattern_block_block_block)
pattern_block_block_block = PaddleGraph(
if_layer10, graph_type="dygraph")
pattern_block_block_block.add_layer(
"prim.isnot",
inputs={"x": gen_name(54)},
outputs=[gen_name(64)],
y=None)
pattern_block_block_block.add_layer(
"prim.if",
inputs={"input": gen_name(64)},
outputs=[gen_name(65)])
if_layer11 = pattern_block_block_block.layers[list(
pattern_block_block_block.layers.keys())[-1]]
pattern_block_block_block_block = PaddleGraph(
if_layer11, graph_type="dygraph")
pattern_block_block_block_block.add_layer(
"prim.equal",
inputs={"input": gen_name(54)},
outputs=[gen_name(66)])
pattern_block_block_block_block.add_layer(
"prim.equal",
inputs={"input": gen_name(66)},
outputs=[gen_name(65)])
if_layer11.add_block(pattern_block_block_block_block)
pattern_block_block_block_block = PaddleGraph(
if_layer11, graph_type="dygraph")
pattern_block_block_block_block.add_layer(
"prim.exception",
inputs={},
outputs=[gen_name(67)],
input="Exception")
pattern_block_block_block_block.add_layer(
"prim.equal", inputs={}, outputs=[gen_name(65)], input=None)
if_layer11.add_block(pattern_block_block_block_block)
if_layer11.inputs.update({"input-0": gen_name(54), })
pattern_block_block_block.add_layer(
"prim.is",
inputs={"x": gen_name(37)},
outputs=[gen_name(68)],
y=None)
pattern_block_block_block.add_layer(
"prim.if",
inputs={"input": gen_name(68)},
outputs=[gen_name(69)])
if_layer12 = pattern_block_block_block.layers[list(
pattern_block_block_block.layers.keys())[-1]]
pattern_block_block_block_block = PaddleGraph(
if_layer12, graph_type="dygraph")
pattern_block_block_block_block.add_layer(
"prim.len",
inputs={"input": gen_name(65)},
outputs=[gen_name(70)])
pattern_block_block_block_block.add_layer(
"prim.gt",
inputs={"x": gen_name(70)},
outputs=[gen_name(71)],
y=0)
pattern_block_block_block_block.add_layer(
"prim.equal", inputs={}, outputs=[gen_name(72)], input=0)
pattern_block_block_block_block.add_layer(
"prim.loop",
inputs={},
outputs=[gen_name(74), gen_name(75), gen_name(76.1)],
input=2147483647)
loop_layer = pattern_block_block_block_block.layers[list(
pattern_block_block_block_block.layers.keys())[-1]]
pattern_loop_block = PaddleGraph(loop_layer, graph_type="dygraph")
pattern_loop_block.add_layer(
"prim.getitem",
inputs={"list": gen_name(65),
"element": gen_name(72)},
outputs=[gen_name(74.1)])
pattern_loop_block.add_layer(
"prim.floor",
inputs={"input": gen_name(74.1)},
outputs=[gen_name(75.1)])
pattern_loop_block.add_layer(
"prim.ne",
inputs={"x": gen_name(75.1),
"y": gen_name(74.1)},
outputs=[gen_name(76)])
pattern_loop_block.add_layer(
"prim.if",
inputs={"input": gen_name(76)},
outputs=[gen_name(77), gen_name(78)])
if_layer13 = pattern_loop_block.layers[list(
pattern_loop_block.layers.keys())[-1]]
pattern_loop_block_block = PaddleGraph(
if_layer13, graph_type="dygraph")
pattern_loop_block_block.add_layer(
"prim.equal", inputs={}, outputs=[gen_name(77)], input=False)
pattern_loop_block_block.add_layer(
"prim.equal",
inputs={"input": gen_name(76)},
outputs=[gen_name(78)])
if_layer13.add_block(pattern_loop_block_block)
pattern_loop_block_block = PaddleGraph(
if_layer13, graph_type="dygraph")
pattern_loop_block_block.add_layer(
"prim.equal", inputs={}, outputs=[gen_name(77)], input=None)
pattern_loop_block_block.add_layer(
"prim.equal", inputs={}, outputs=[gen_name(78)], input=None)
if_layer13.add_block(pattern_loop_block_block)
if_layer13.inputs.update({"input-0": gen_name(76), })
pattern_loop_block.add_layer(
"prim.if",
inputs={"input": gen_name(76)},
outputs=[gen_name(79), gen_name(80)])
if_layer14 = pattern_loop_block.layers[list(
pattern_loop_block.layers.keys())[-1]]
pattern_loop_block_block = PaddleGraph(
if_layer14, graph_type="dygraph")
pattern_loop_block_block.add_layer(
"prim.equal",
inputs={"input": gen_name(77)},
outputs=[gen_name(79)])
pattern_loop_block_block.add_layer(
"prim.equal",
inputs={"input": gen_name(78)},
outputs=[gen_name(80)])
if_layer14.add_block(pattern_loop_block_block)
pattern_loop_block_block = PaddleGraph(
if_layer14, graph_type="dygraph")
pattern_loop_block_block.add_layer(
"prim.equal", inputs={}, outputs=[gen_name(79)], input=True)
pattern_loop_block_block.add_layer(
"prim.equal",
inputs={"input": gen_name(76)},
outputs=[gen_name(80)])
if_layer14.add_block(pattern_loop_block_block)
if_layer14.inputs.update({
"input-0": gen_name(77),
"input-1": gen_name(78),
"input-2": gen_name(76)
})
pattern_loop_block.add_layer(
"prim.add",
inputs={"x": gen_name(72)},
outputs=[gen_name(81)],
y=1)
pattern_loop_block.add_layer(
"prim.lt",
inputs={"x": gen_name(81),
"y": gen_name(70)},
outputs=[gen_name(82)])
pattern_loop_block.add_layer(
"prim.and",
inputs={"x": gen_name(82),
"y": gen_name(79)},
outputs=[gen_name(83)])
pattern_loop_block.add_layer(
"prim.equal",
inputs={"input": gen_name(80)},
outputs=[gen_name(74)])
pattern_loop_block.add_layer(
"prim.equal",
inputs={"input": gen_name(81)},
outputs=[gen_name(75)])
loop_layer.add_block(pattern_loop_block)
loop_layer.inputs.update({
"input-0": gen_name(65),
"input-1": gen_name(72),
"input-2": gen_name(72),
"input-3": gen_name(70)
})
pattern_block_block_block_block.add_layer(
"prim.if",
inputs={"input": gen_name(74)},
outputs=[gen_name(84)])
if_layer15 = pattern_block_block_block_block.layers[list(
pattern_block_block_block_block.layers.keys())[-1]]
pattern_block_block_block_block_block = PaddleGraph(
if_layer15, graph_type="dygraph")
pattern_block_block_block_block_block.add_layer(
"prim.warnings",
inputs={},
outputs=[gen_name(85)],
stacklevel=2,
input="...")
if_layer15.add_block(pattern_block_block_block_block_block)
pattern_block_block_block_block_block = PaddleGraph(
if_layer15, graph_type="dygraph")
if_layer15.add_block(pattern_block_block_block_block_block)
if_layer12.add_block(pattern_block_block_block_block)
pattern_block_block_block_block = PaddleGraph(
if_layer12, graph_type="dygraph")
if_layer12.add_block(pattern_block_block_block_block)
if_layer12.inputs.update({
"input-0": gen_name(65),
"input-1": gen_name(65),
})
pattern_block_block_block.add_layer(
"prim.list", inputs={}, outputs=[gen_name(86)])
pattern_block_block_block.add_layer(
"prim.loop",
inputs={},
outputs=[gen_name(87), gen_name(88)],
input=2)
loop_layer = pattern_block_block_block.layers[list(
pattern_block_block_block.layers.keys())[-1]]
pattern_loop_block = PaddleGraph(loop_layer, graph_type="dygraph")
pattern_loop_block.add_layer(
"prim.add",
inputs={"x": gen_name(88)},
outputs=[gen_name(89)],
y=2)
pattern_loop_block.add_layer(
"prim.shape_dim",
inputs={"input": gen_name(34),
"dim": gen_name(89)},
outputs=[gen_name(90)])
pattern_loop_block.add_layer(
"prim.float",
inputs={"input": gen_name(90)},
outputs=[gen_name(91)])
pattern_loop_block.add_layer(
"prim.getitem",
inputs={"list": gen_name(65),
"element": gen_name(88)},
outputs=[gen_name(92)])
pattern_loop_block.add_layer(
"prim.mul",
inputs={"x": gen_name(91),
"y": gen_name(92)},
outputs=[gen_name(93)])
pattern_loop_block.add_layer(
"prim.floor",
inputs={"input": gen_name(93)},
outputs=[gen_name(94)])
pattern_loop_block.add_layer(
"prim.append",
inputs={"list": gen_name(86),
"element": gen_name(94)},
outputs=[])
loop_layer.add_block(pattern_loop_block)
loop_layer.inputs.update({
"input-0": gen_name(34),
"input-1": gen_name(65),
"input-2": gen_name(86)
})
pattern_block_block_block.add_layer(
"prim.equal",
inputs={"input": gen_name(86)},
outputs=[gen_name(63)])
if_layer10.add_block(pattern_block_block_block)
if_layer10.inputs.update({
"input-0": gen_name(61),
"input-1": gen_name(54),
"input-2": gen_name(54),
"input-3": gen_name(37),
"input-4": gen_name(34)
})
pattern_block_block = PaddleGraph(parent_layer=if_layer2, graph_type="dygraph")
pattern_block_block.add_layer(
"prim.getitem",
inputs={"list": gen_name(11)},
outputs=[gen_name(95)],
outputs=[gen_name(21)],
element=0)
pattern_block_block.add_layer(
"prim.getitem",
inputs={"list": gen_name(11)},
outputs=[gen_name(96)],
outputs=[gen_name(22)],
element=1)
pattern_block_block.add_layer(
"prim.isinstance",
inputs={"input": gen_name(63)},
inputs={"input": "interpolate-input-3"},
outputs=["interpolate-input-0_isinstance"],
cls="paddle.fluid.Variable")
pattern_block_block.add_layer(
......@@ -1420,133 +180,127 @@ class DygraphInterpolateBilinearFuser(FuseBase):
if_layer_isinstance, graph_type="dygraph")
pattern_block_block_block.add_layer(
"prim.var2list",
inputs={"input": gen_name(63)},
outputs=[gen_name(63)])
inputs={"input": "interpolate-input-3"},
outputs=["interpolate-input-3"])
if_layer_isinstance.add_block(pattern_block_block_block)
pattern_block_block_block = PaddleGraph(
if_layer_isinstance, graph_type="dygraph")
if_layer_isinstance.add_block(pattern_block_block_block)
if_layer_isinstance.inputs["input-0"] = gen_name(63)
pattern_block_block.add_layer(
"prim.assert",
inputs={"key": gen_name(95),
"value": gen_name(96)},
outputs=[gen_name(97) + "_assert"],
type="eq")
if_layer_isinstance.inputs["input-0"] = "interpolate-input-3"
pattern_block_block.add_layer(
"paddle.nn.functional.interpolate",
inputs={
"input": "interpolate-input-0",
"size": gen_name(63),
"scale_factor": gen_name(95)
"size": "interpolate-input-3",
"scale_factor": gen_name(21)
},
outputs=[gen_name(97)],
align_corners=False,
align_mode=0)
outputs=[gen_name(23)])
pattern_block_block.add_layer(
"prim.equal",
inputs={"input": gen_name(97)},
inputs={"input": gen_name(23)},
outputs=[gen_name(20)])
if_layer2.add_block(pattern_block_block)
pattern_block_block = PaddleGraph(if_layer2, graph_type="dygraph")
pattern_block_block.add_layer(
"fluid.layers.shape",
"prim.shape",
inputs={"input": "interpolate-input-0"},
outputs=[gen_name(98)])
outputs=[gen_name(24)])
pattern_block_block.add_layer(
"prim.len",
inputs={"input": gen_name(98)},
outputs=[gen_name(98)])
inputs={"input": gen_name(24)},
outputs=[gen_name(24)])
pattern_block_block.add_layer(
"prim.eq",
inputs={"x": gen_name(98)},
outputs=[gen_name(99)],
inputs={"x": gen_name(24)},
outputs=[gen_name(25)],
y=5)
pattern_block_block.add_layer(
"prim.if",
inputs={"input": gen_name(99)},
outputs=[gen_name(100)])
if_layer16 = pattern_block_block.layers[list(
inputs={"input": gen_name(25)},
outputs=[gen_name(26)])
if_layer3 = pattern_block_block.layers[list(
pattern_block_block.layers.keys())[-1]]
pattern_block_block_block = PaddleGraph(
if_layer16, graph_type="dygraph")
parent_layer=if_layer3, graph_type="dygraph")
pattern_block_block_block.add_layer(
"prim.exception",
inputs={},
outputs=[gen_name(101)],
outputs=[gen_name(27)],
input="Exception")
if_layer16.add_block(pattern_block_block_block)
if_layer3.add_block(pattern_block_block_block)
pattern_block_block_block = PaddleGraph(
if_layer16, graph_type="dygraph")
parent_layer=if_layer3, graph_type="dygraph")
pattern_block_block_block.add_layer(
"prim.exception",
inputs={},
outputs=[gen_name(102)],
outputs=[gen_name(28)],
input="Exception")
if_layer16.add_block(pattern_block_block_block)
if_layer3.add_block(pattern_block_block_block)
pattern_block_block.add_layer(
"prim.equal", inputs={}, outputs=[gen_name(20)], input=None)
if_layer2.add_block(pattern_block_block)
if_layer2.inputs.update({
"input-0": gen_name(13),
"input-1": gen_name(13),
"input-2": "interpolate-input-0",
"input-0": "interpolate-input-0",
"input-1": "interpolate-input-3",
"input-2": "interpolate-input-3",
"input-3": gen_name(11),
"input-5": gen_name(11),
})
pattern_block.add_layer(
"prim.equal",
inputs={"input": gen_name(20)},
outputs=[gen_name(16)])
outputs=[gen_name(14)])
if_layer1.add_block(pattern_block)
if_layer1.inputs.update({
"input-2": "interpolate-input-0",
"input-4": gen_name(13),
"input-7": gen_name(11),
"input-9": gen_name(11),
"input-11": "interpolate-input-0",
"input-12": "interpolate-input-0",
'input-2': 'interpolate-input-0',
'input-4': gen_name(11),
'input-6': gen_name(11),
'input-8': 'interpolate-input-0',
'input-9': 'interpolate-input-3',
'input-10': 'interpolate-input-0'
})
self.pattern.build(inputs={
"input-0": "interpolate-input-0",
"input-1": "interpolate-input-1"
"input-1": "interpolate-input-1",
"input-2": "interpolate-input-2",
"input-3": "interpolate-input-3",
"input-4": "interpolate-input-4"
})
def insert_new_layer(self, graph, parameters, matches):
new_layers = self.gen_new_layer(parameters, matches)
new_layer_id = list(matches.keys())[0]
graph.layers[new_layer_id] = new_layers[0]
matches.pop(new_layer_id)
new_layer_id = list(matches.keys())[0]
graph.layers[new_layer_id] = new_layers[1]
block_layer = new_layers[1].blocks[0].layers.pop(
list(new_layers[1].blocks[0].layers.keys())[-1])
new_layers[1].blocks[0].layers[new_layer_id + ".0.0"] = block_layer
matches.pop(new_layer_id)
new_layer_id = list(matches.keys())[0]
graph.layers[new_layer_id] = new_layers[2]
matches.pop(new_layer_id)
new_layer = self.gen_new_layer(parameters, matches)
global_layers = graph.get_global_layers()
new_matches = dict()
is_match = False
for layer_id, layer in global_layers.items():
if layer_id == list(matches.keys())[0] and not is_match:
new_matches[layer_id] = layer
is_match = True
if is_match:
new_matches[layer_id] = layer
if layer_id == list(matches.keys())[-1]:
break
new_layer_id = new_layer.layer_id
graph.layers[new_layer_id] = new_layer
new_matches.pop(new_layer_id)
matches.clear()
for layer_id, layer in new_matches.items():
matches[layer_id] = layer
def gen_new_layer(self, parameters, matches):
layers = list()
layers_id = list(matches.keys())
layer = matches[layers_id[6]]
size = layer.inputs["input1"]
layer = matches[layers_id[92]]
layer.inputs["input"] = size
layers.append(layer)
layer = matches[layers_id[93]]
block_layer = layer.blocks[0].layers[list(layer.blocks[0].layers.keys())
[0]]
block_layer.inputs["input"] = size
block_layer.outputs[0] = size
layer.inputs["input-0"] = size
layers.append(layer)
layer = matches[layers_id[-1]]
outputs = layer.outputs
layer = matches[layers_id[96]]
layer.inputs.pop("scale_factor")
layer.inputs["size"] = size
layer.outputs = outputs
layers.append(layer)
return layers
layer = matches[layers_id[19]]
new_layer = copy.deepcopy(layer)
layer = matches[layers_id[9]]
new_layer.outputs[0] = layer.outputs[0]
new_layer.layer_id = layers_id[7]
new_layer.inputs.pop("scale_factor")
new_layer.inputs["size"] = size
return new_layer
# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from x2paddle.optimizer.pass_ import Pass
from x2paddle.optimizer.fusion.dygraph import TraceFcFuser
from x2paddle.optimizer.pass_manager import pass_register
@pass_register
class TraceFcFusePass(Pass):
name = "trace_fc_fuse_pass"
def __init__(self):
Pass.__init__(self)
def apply(self, graph):
fuser = TraceFcFuser()
fuser.operate(graph, match_kind="topo")
# 用于注册
trace_fc_fuse_pass = TraceFcFusePass()
\ No newline at end of file
# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import numpy as np
from x2paddle.optimizer.pattern_matcher import FuseBase
from x2paddle.core.program import PaddleGraph, PaddleLayer
from x2paddle.core.util import *
class TraceFcFuser(FuseBase):
def __init__(self):
self.linear_index = 0
super(TraceFcFuser, self).__init__(graph_type="dygraph")
self.patterns = list()
def build_pattern(self):
""" 描述需要替换的fc图结构。
fc层模式python实现代码示例:
模式一:
encoder_layer_8_attention_self_key_weight = self.encoder_layer_8_attention_self_key_weight
x748 = paddle.transpose(x=encoder_layer_8_attention_self_key_weight, perm=[1, 0])
x749 = paddle.matmul(x=x732, y=x748)
encoder_layer_8_attention_self_key_bias = self.encoder_layer_8_attention_self_key_bias
x750 = x749 + 1 * encoder_layer_8_attention_self_key_bias
模式二:
x13 = self.x13
x14 = paddle.transpose(x=x13, perm=[1, 0])
x15 = self.x15
x16 = paddle.addmm(input=x15, x=x12, y=x14, beta=1, alpha=1)
"""
def gen_name(id):
return "x" + str(id)
pattern = PaddleGraph(graph_type="dygraph")
pattern.add_layer(
"self.create_parameter",
inputs={},
outputs=[gen_name(0)])
pattern.add_layer(
"fluid.layers.transpose",
inputs={"x": gen_name(0)},
outputs=[gen_name(1)],
perm=[1, 0])
pattern.add_layer(
"paddle.matmul",
inputs={"x": "fc-input-0",
"y": gen_name(1)},
outputs=[gen_name(2)])
pattern.add_layer(
"self.create_parameter",
inputs={},
outputs=[gen_name(3)])
pattern.add_layer(
"prim.add_",
inputs={"x": gen_name(2),
"y": gen_name(3)},
outputs=[gen_name(4)],
alpha=1)
pattern.build(inputs={"input-0": "fc-input-0"})
self.patterns.append(pattern)
pattern = PaddleGraph(graph_type="dygraph")
pattern.add_layer(
"self.create_parameter",
inputs={},
outputs=[gen_name(0)])
pattern.add_layer(
"paddle.transpose",
inputs={"x": gen_name(0)},
outputs=[gen_name(1)],
perm=[1, 0])
pattern.add_layer(
"self.create_parameter",
inputs={},
outputs=[gen_name(2)])
pattern.add_layer(
"paddle.addmm",
inputs={"input": gen_name(2),
"x": "fc-input-0",
"y": gen_name(1)},
outputs=[gen_name(4)],
alpha=1,
beta=1)
pattern.build(inputs={"input-0": "fc-input-0"})
self.patterns.append(pattern)
def insert_new_layer(self, graph, parameters, matches):
new_layer = self.gen_new_layer(parameters, matches)
new_layer_id = list(matches.keys())[0]
graph.layers[new_layer_id] = new_layer
matches.pop(new_layer_id)
def gen_new_layer(self, parameters, matches):
layers_id = list(matches.keys())
if len(layers_id) == 5:
layer = matches[layers_id[2]]
else:
layer = matches[layers_id[-1]]
input_name = layer.inputs["x"]
scope_name = layer.scope_name
layer = matches[layers_id[-1]]
output_name = layer.outputs[0]
layer = matches[layers_id[0]]
weight_name = layer.outputs[0]
layer = matches[layers_id[-2]]
bias_name = layer.outputs[0]
attrs = dict()
attrs["in_features"] = parameters[weight_name].shape[1]
attrs["out_features"] = parameters[weight_name].shape[0]
linear_name = "linear{}".format(self.linear_index)
self.linear_index += 1
parameters["{}.weight".format(linear_name)] = parameters[
weight_name].transpose((1, 0))
parameters["{}.bias".format(linear_name)] = np.squeeze(parameters[
bias_name])
new_layer = PaddleLayer(
layers_id[0],
"paddle.nn.Linear",
inputs={"input": input_name},
outputs=[linear_name, output_name],
scope_name=scope_name,
**attrs)
return new_layer
......@@ -18,14 +18,20 @@ from x2paddle.optimizer.fusion.static import *
from x2paddle.optimizer.elimination.dygraph import *
class GraphOptimizer(object):
def __init__(self, source_frame, paddle_type="dygraph"):
def __init__(self, source_frame, paddle_type="dygraph", jit_type="trace"):
if source_frame == "pytorch":
self.passes = [
"dygraph_constant_fuse_pass", "dygraph_batchnorm2d_fuse_pass",
"dygraph_interpolate_bilinear_fuse_pass", "dygraph_fc_fuse_pass",
"dygraph_adaptive_pool2d_fuse_pass", "dygraph_reshape_fuse_pass",
"dygraph_dropout_fuse_pass"
]
if jit_type == "trace":
self.passes = ["trace_fc_fuse_pass"]
else:
self.passes = [
"dygraph_constant_fuse_pass",
"dygraph_batchnorm2d_fuse_pass",
"dygraph_interpolate_bilinear_fuse_pass",
"dygraph_fc_fuse_pass",
"dygraph_adaptive_pool2d_fuse_pass",
"dygraph_reshape_fuse_pass",
"dygraph_dropout_fuse_pass"
]
elif source_frame == "caffe":
if paddle_type == "dygraph":
self.passes = ["dygraph_bn_scale_fuse_pass"]
......@@ -38,8 +44,7 @@ class GraphOptimizer(object):
"transpose_eliminate_pass"
]
else:
# TODO
pass
self.passes = []
def optimize(self, graph):
for pass_name in self.passes:
......
......@@ -85,7 +85,11 @@ class PatternMatcher(object):
# 判断subgraph中的节点是否被外部图使用到(如若被使用到则无效)
if layer_id in graph.edges_out:
if pattern_layer_id not in pattern.edges_out:
if not set(pattern_layer.outputs).issubset(
if "paddle.nn" in layer.kernel and "functional" not in layer.kernel:
pattern_layer_opt = pattern_layer.outputs[1:]
else:
pattern_layer_opt = pattern_layer.outputs
if not set(pattern_layer_opt).issubset(
pattern.outputs):
# 若pattern当前layer的输出是pattern的输出,则是正确的
if pattern_index == 0 or is_subblock:
......@@ -97,7 +101,11 @@ class PatternMatcher(object):
if len(graph.edges_out[layer_id]) != len(
pattern.edges_out[pattern_layer_id]):
# 如果在每个节点edges_in相同的情况下,edges_out数目相同则说明无节点在subgraph外被用到
if not set(pattern_layer.outputs).issubset(
if "paddle.nn" in layer.kernel and "functional" not in layer.kernel:
pattern_layer_opt = pattern_layer.outputs[1:]
else:
pattern_layer_opt = pattern_layer.outputs
if not set(pattern_layer_opt).issubset(
pattern.outputs):
# 若pattern当前layer的输出是pattern的输出,则是正确的
if pattern_index == 0 or is_subblock:
......@@ -105,6 +113,7 @@ class PatternMatcher(object):
else:
subgraph_id2layers.pop(layer_id)
continue
# 当为控制流时的处理
if layer.kernel == "prim.if" or layer.kernel == "prim.loop":
if len(pattern_layer.blocks) != len(layer.blocks):
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册