提交 6adc58f0 编写于 作者: Q quyongxiu1 提交者: quyongxiu

a demo case for mapping parse and save

demo save

try if it work

add a run ins

fix bug

fix bug 2

fix bug 3

fix bug 4

fix bug 6

fix bug 6, leave why not convert params, but convert op name

fix bug 7

fix 08

fix 9

fix bug 10, write wrong for json, not gen_explict etc

temp save

pylint fix 01

adjust structure and fix pylint

delete unuse file

delete unused code

add comment and delete the devil figure
上级 a1c35761
此差异已折叠。
# Copyright 2020 Huawei Technologies Co., Ltd
#
# 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.
# ============================================================================
"""Enums."""
from enum import Enum
class RequriedType(Enum):
"""If param is required"""
REQUIRED = 1
UNREQUIRED = 2
# Copyright 2020 Huawei Technologies Co., Ltd
#
# 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.
# ============================================================================
"""funcs for gen_explicit_map"""
from functools import partial
def gen_explicit_map_f_max_pool2d(params_pt, args_pt):
"""
Generate explicit_map for F.MaxPool2d.
Args:
params_pt (dict): Params for APIPt.
args_pt (dict): Args for APIPt.
Returns:
dict, map between frames.
"""
if 'padding' in args_pt:
padding = args_pt['padding']
else:
padding = params_pt['padding']
if padding.strip() in ("0", "(0,0)", "(0, 0)"):
padding = "'valid'"
else:
padding = "'same'"
return {"padding": padding}
def gen_explicit_map_nn_sequential(_, args_pt):
"""
Generate explicit_map for nn.Sequential.
Args:
args_pt (dict): Args for APIPt.
Returns:
dict, map between frames.
"""
args = args_pt['*args']
return {"*args": "[{}]".format(args)}
def gen_explicit_map_one_delta(params_pt, args_pt, k_ms, k_pt):
"""
Generate explicit_map for which include mapping relationship is `1 - k_ms = k_pt`.
Args:
params_pt (dict): Params for APIPt.
args_pt (dict): Args for APIPt.
Returns:
dict, map between frames.
"""
value = args_pt[k_pt] if k_pt in args_pt else params_pt[k_pt]
value = value.strip()
def is_number(string):
try:
float(string)
return True
except ValueError:
return False
if is_number(value):
return {k_ms: str(1 - float(value))}
return {k_ms: "1.0 - " + value}
def gen_explicit_map_nn_maxpool2d(params_pt, args_pt):
"""
Generate explicit_map for nn.MaxPool2d.
Args:
params_pt (dict): Params for APIPt.
args_pt (dict): Args for APIPt.
Returns:
dict, map between frames.
"""
if 'padding' in args_pt:
padding = args_pt['padding']
else:
padding = params_pt['padding']
if padding.strip() in ("0", "(0,0)", "(0, 0)"):
pad_mode = "'valid'"
else:
pad_mode = "'same'"
return {"pad_mode": pad_mode}
tensor_dot_view_gen_explicit_map = lambda params_pt, args_pt: {"shape": "(" + args_pt["*shape"] + ",)"}
tensor_dot_reshape_gen_explicit_map = lambda params_pt, args_pt: {"shape": "(" + args_pt["*shape"] + ",)"}
nn_conv2d_gen_explicit_map = lambda params_pt, args_pt: {"pad_mode": "'pad'"}
nn_batchnorm2d_gen_explicit_map = partial(gen_explicit_map_one_delta, k_ms="momentum", k_pt="momentum")
nn_dropout_gen_explicit_map = partial(gen_explicit_map_one_delta, k_ms="keep_prob", k_pt="p")
{
"F.avg_pool2d": {
"ms_api": [
"P.AvgPool",
{
"ksize": 1,
"strides": 1,
"padding": "valid",
"input": "REQUIRED"
},
[
"ksize",
"strides",
"padding"
]
],
"pt_api": [
"F.avg_pool2d",
{
"input": "REQUIRED",
"kernel_size": "REQUIRED",
"stride": null,
"padding": 0,
"dilation": 1,
"ceil_mode": false,
"return_indices": false
}
],
"ms2pt_mapping": {
"ksize": "kernel_size",
"strides": "stride",
"input": "input"
},
"gen_explicit_map": "gen_explicit_map_f_max_pool2d"
},
"F.max_pool2d": {
"ms_api": [
"P.MaxPool",
{
"ksize": 1,
"strides": 1,
"padding": "valid",
"input": "REQUIRED"
},
[
"ksize",
"strides",
"padding"
]
],
"pt_api": [
"F.max_pool2d",
{
"input": "REQUIRED",
"kernel_size": "REQUIRED",
"stride": null,
"padding": 0,
"dilation": 1,
"ceil_mode": false,
"return_indices": false
}
],
"ms2pt_mapping": {
"ksize": "kernel_size",
"strides": "stride",
"input": "input"
},
"gen_explicit_map": "gen_explicit_map_f_max_pool2d"
},
"F.relu": {
"ms_api": [
"P.ReLU",
{
"input": "REQUIRED"
}
],
"pt_api": [
"F.relu",
{
"input": "REQUIRED",
"inplace": false
}
],
"ms2pt_mapping": {
"input": "input"
},
"gen_explicit_map": null
},
"F.relu6": {
"ms_api": [
"P.ReLU6",
{
"input": "REQUIRED"
}
],
"pt_api": [
"F.relu6",
{
"input": "REQUIRED",
"inplace": false
}
],
"ms2pt_mapping": {
"input": "input"
},
"gen_explicit_map": null
}
}
\ No newline at end of file
{
"nn.Dropout": {
"ms_api": [
"nn.Dropout",
{
"keep_prob": 0.5,
"seed0": 0,
"seed1": 0,
"dtype": "mstype.float32"
}
],
"pt_api": [
"nn.Dropout",
{
"p": 0.5,
"inplace": false
}
],
"ms2pt_mapping": {
"keep_prob": "p"
},
"gen_explicit_map": "nn_dropout_gen_explicit_map"
},
"nn.AvgPool2d": {
"ms_api": [
"nn.AvgPool2d",
{
"kernel_size": 1,
"stride": 1,
"pad_mode": "valid"
}
],
"pt_api": [
"nn.AvgPool2d",
{
"kernel_size": "REQUIRED",
"stride": null,
"padding": 0,
"dilation": 1,
"return_indices": false,
"ceil_mode": "False"
}
],
"ms2pt_mapping": {
"kernel_size": "kernel_size",
"stride": "stride"
},
"gen_explicit_map": "gen_explicit_map_nn_maxpool2d"
},
"nn.MaxPool2d": {
"ms_api": [
"nn.MaxPool2d",
{
"kernel_size": 1,
"stride": 1,
"pad_mode": "valid"
}
],
"pt_api": [
"nn.MaxPool2d",
{
"kernel_size": "REQUIRED",
"stride": null,
"padding": 0,
"dilation": 1,
"return_indices": false,
"ceil_mode": "False"
}
],
"ms2pt_mapping": {
"kernel_size": "kernel_size",
"stride": "stride"
},
"gen_explicit_map": "gen_explicit_map_nn_maxpool2d"
},
"nn.Linear": {
"ms_api": [
"nn.Dense",
{
"in_channels": "REQUIRED",
"out_channels": "REQUIRED",
"weight_init": "normal",
"bias_init": "zeros",
"has_bias": true,
"activation": null
}
],
"pt_api": [
"nn.Linear",
{
"in_features": "REQUIRED",
"out_features": "REQUIRED",
"bias": true
}
],
"ms2pt_mapping": {
"in_channels": "in_features",
"out_channels": "out_features",
"has_bias": "bias"
}
},
"nn.ReLU6": {
"ms_api": [
"nn.ReLU6",
{}
],
"pt_api": [
"nn.ReLU6",
{
"inplace": false
}
],
"ms2pt_mapping": {}
},
"nn.ReLU": {
"ms_api": [
"nn.ReLU",
{}
],
"pt_api": [
"F.relu",
{
"inplace": false
}
],
"ms2pt_mapping": {}
},
"nn.BatchNorm2d": {
"ms_api": [
"nn.BatchNorm2d",
{
"num_features": "REQUIRED",
"eps": 1e-05,
"momentum": 0.9,
"affine": true,
"gamma_init": "ones",
"beta_init": "zeros",
"moving_mean_init": "zeros",
"moving_var_init": "ones",
"use_batch_statistics": true
}
],
"pt_api": [
"nn.BatchNorm2d",
{
"num_features": "REQUIRED",
"eps": 1e-05,
"momentum": 0.1,
"affine": true,
"track_running_stats": true
}
],
"ms2pt_mapping": {
"num_features": "num_features",
"eps": "eps",
"affine": "affine",
"use_batch_statistics": "track_running_stats"
},
"gen_explicit_map": "nn_batchnorm2d_gen_explicit_map"
},
"nn.Conv2d": {
"ms_api": [
"nn.Conv2d",
{
"in_channels": "REQUIRED",
"out_channels": "REQUIRED",
"kernel_size": "REQUIRED",
"stride": 1,
"pad_mode": "same",
"padding": 0,
"dilation": 1,
"group": 1,
"has_bias": false,
"weight_init": "normal",
"bias_init": "zeros"
}
],
"pt_api": [
"nn.Conv2d",
{
"in_channels": "REQUIRED",
"out_channels": "REQUIRED",
"kernel_size": "REQUIRED",
"stride": 1,
"padding": 0,
"dilation": 1,
"groups": 1,
"bias": true,
"padding_mode": "zeros"
}
],
"ms2pt_mapping": {
"in_channels": "in_channels",
"out_channels": "out_channels",
"kernel_size": "kernel_size",
"stride": "stride",
"padding": "padding",
"dilation": "dilation",
"group": "groups",
"has_bias": "bias"
},
"gen_explicit_map": "nn_conv2d_gen_explicit_map"
},
"nn.Sequential": {
"ms_api": [
"nn.SequentialCell",
{
"*args": " REQUIRED"
}
],
"pt_api": [
"nn.Sequential",
{
"*args": " REQUIRED"
}
],
"export_key": false,
"gen_explicit_map": "gen_explicit_map_nn_sequential"
}
}
\ No newline at end of file
{
".view": {
"ms_api": [
"P.Reshape",
{
"x": "REQUIRED",
"shape": "REQUIRED"
}
],
"pt_api": [
".view",
{
"*shape": "REQUIRED"
}
],
"ms2pt_mapping": {
"x": "call_name"
},
"gen_explicit_map": "tensor_dot_view_gen_explicit_map"
},
".size": {
"ms_api": [
"P.Shape",
{
"x": "REQUIRED"
}
],
"pt_api": [
".size",
{
"idx": "REQUIRED"
}
],
"ms2pt_mapping": {
"x": "call_name"
}
},
".flatten": {
"ms_api": [
"P.Flatten",
{
"input": "REQUIRED"
}
],
"pt_api": [
".flatten",
{
"start_dim": 0,
"end_dim": -1
}
],
"ms2pt_mapping": {
"input": "call_name"
}
},
".reshape": {
"ms_api": [
"P.Reshape",
{
"x": "REQUIRED",
"shape": "REQUIRED"
}
],
"pt_api": [
".reshape",
{
"*shape": "REQUIRED"
}
],
"ms2pt_mapping": {
"x": "call_name"
},
"gen_explicit_map": "tensor_dot_reshape_gen_explicit_map"
},
".mean": {
"ms_api": [
"P.ReduceMean",
{
"keep_dims": false,
"input": "REQUIRED",
"axis": []
}
],
"pt_api": [
".mean",
{
"dim": null,
"keepdim": false
}
],
"ms2pt_mapping": {
"keep_dims": "keepdim",
"axis": "dim",
"input": "call_name"
}
},
".squeeze": {
"ms_api": [
"P.ReduceMean",
{
"input": "REQUIRED",
"axis": []
},
[
"axis"
]
],
"pt_api": [
".squeeze",
{
"dim": null
}
],
"ms2pt_mapping": {
"axis": "dim",
"input": "call_name"
}
}
}
\ No newline at end of file
{
"torch.flatten": {
"ms_api": [
"P.Flatten",
{
"input": "REQUIRED"
}
],
"pt_api": [
"torch.flatten",
{
"input": "REQUIRED",
"start_dim": 0,
"end_dim": -1
}
],
"ms2pt_mapping": {
"input": "input"
}
},
"torch.cat": {
"ms_api": [
"P.Concat",
{
"axis": 0,
"input": "REQUIRED"
},
[
"axis"
]
],
"pt_api": [
"torch.cat",
{
"tensors": "REQUIRED",
"dim": 0,
"out": null
}
],
"ms2pt_mapping": {
"input": "tensors",
"axis": "dim"
}
}
}
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册