Skip to content

tests for Normalize subgraph, Slice, Mul and Expand #902

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Sep 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added testdata/dnn/onnx/data/input_expand.npy
Binary file not shown.
Binary file added testdata/dnn/onnx/data/input_expand_identity.npy
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added testdata/dnn/onnx/data/input_slice_neg_starts.npy
Binary file not shown.
Binary file added testdata/dnn/onnx/data/input_split_neg_axis.npy
Binary file not shown.
Binary file added testdata/dnn/onnx/data/output_expand.npy
Binary file not shown.
Binary file added testdata/dnn/onnx/data/output_expand_identity.npy
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added testdata/dnn/onnx/data/output_slice_neg_starts.npy
Binary file not shown.
Binary file added testdata/dnn/onnx/data/output_split_neg_axis.npy
Binary file not shown.
87 changes: 87 additions & 0 deletions testdata/dnn/onnx/generate_onnx_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import numpy as np
import os.path
import onnx
import onnxsim
import google.protobuf.text_format
import io

Expand Down Expand Up @@ -72,6 +73,14 @@ def save_onnx_data_and_model(input, output, name, operation, *args, **kwargs):
model = onnx.helper.make_model(graph, producer_name=name)
onnx.save(model, models_files)

def simplify(name, rename=False, **kwargs):
model, check = onnxsim.simplify(name, **kwargs)
assert check, "couldn't valide"
name = name[:-5]
if rename:
name += '_optimized'
onnx.save(model, name + '.onnx')

torch.manual_seed(0)
np.random.seed(0)

Expand Down Expand Up @@ -412,6 +421,17 @@ def forward(self, x):
save_data_and_model("slice", input, model)
save_data_and_model("slice_opset_11", input, model, version=11)

class SliceStarts(nn.Module):
def __init__(self, *args, **kwargs):
super(SliceStarts, self).__init__()

def forward(self, x):
return x[-1:]

model = SliceStarts()
input_ = Variable(torch.randn(1, 10, dtype=torch.float32))
save_data_and_model("slice_neg_starts", input_, model)

input_2 = Variable(torch.randn(6, 6))
custom_slice_list = [
slice(1, 3, 1),
Expand Down Expand Up @@ -573,6 +593,18 @@ def forward(self, x):
input_ = Variable(torch.tensor(list(range(20)), dtype=torch.float32))
save_data_and_model("split_sizes", input_, model)

class SplitAxis(nn.Module):
def __init__(self, *args, **kwargs):
super(SplitAxis, self).__init__()

def forward(self, x):
tup = torch.split(x, 2, -1)
return torch.cat(tup, 1)

model = SplitAxis()
input_ = Variable(torch.randn(1, 10, dtype=torch.float32))
save_data_and_model("split_neg_axis", input_, model)

class SplitMax(nn.Module):

def __init__(self):
Expand Down Expand Up @@ -840,6 +872,32 @@ def forward(self, x):
output = np.mean(x, axis=2, keepdims=True)
save_onnx_data_and_model(x, output, 'reduce_mean_axis2', 'ReduceMean', axes=(2), keepdims=True)

class Expand(nn.Module):
def __init__(self):
super(Expand, self).__init__()

def forward(self, x):
return x.expand(1, 3, -1, -1, -1)

input = Variable(torch.randn(1, 3, 2, 4))
model = Expand()
model.eval()
save_data_and_model("expand", input, model, export_params=True, version=12)
simplify('models/expand.onnx', False)

class ExpandIdentity(nn.Module):
def __init__(self):
super(ExpandIdentity, self).__init__()

def forward(self, x):
return x.expand(1, 3, -1, -1)

input = Variable(torch.randn(1, 3, 2, 4))
model = ExpandIdentity()
model.eval()
save_data_and_model("expand_identity", input, model, export_params=True, version=12)
simplify('models/expand_identity.onnx', False)

class Expand(nn.Module):
def __init__(self, shape):
super(Expand, self).__init__()
Expand Down Expand Up @@ -908,6 +966,23 @@ def forward(self, x):
x = Variable(torch.randn(1, 2, 3, 4))
save_data_and_model("reduceL2_subgraph_2", x, model)

class reduceL2_subgraph2_2(nn.Module):
def __init__(self):
super(reduceL2_subgraph2_2, self).__init__()
self.size = torch.Size([1, 3, 2, 4])

def forward(self, x):
norm = torch.norm(x, p=2, dim=1, keepdim=True)
clip = torch.clamp(norm, min=0)
expand = clip.expand([1, 3, 2, 4])
return x / expand

input = Variable(torch.randn(1, 3, 2, 4))
model = reduceL2_subgraph2_2()
model.eval()
save_data_and_model("reduceL2_subgraph2_2", input, model, export_params=True, version=12)
simplify('models/reduceL2_subgraph2_2.onnx', False)

from torchvision.ops.misc import *
n = 3
model = FrozenBatchNorm2d(n)
Expand Down Expand Up @@ -1148,6 +1223,18 @@ def forward(self, x0, x1, x2):
input_2 = Variable(torch.ones(2, 1, 4, 1, dtype=torch.float32))
save_data_and_model_multy_inputs("scale_broadcast", model, input_0, input_1, input_2)

class ScaleBroadcastMid(nn.Module):
def __init__(self, *args, **kwargs):
super(ScaleBroadcastMid, self).__init__()

def forward(self, x0, x1):
return torch.mul(x0, x1)

model = ScaleBroadcastMid()
input_0 = Variable(torch.ones(2, 1, 4, dtype=torch.float32))
input_1 = Variable(torch.ones(2, 5, 4, dtype=torch.float32))
save_data_and_model_multy_inputs("scale_broadcast_mid", model, input_0, input_1)

x = Variable(torch.randn(1, 3, 25))
conv1d = nn.Conv1d(3, 2, kernel_size=3, padding=2, stride=2, dilation=2, bias=False)
save_data_and_model("conv1d", x, conv1d)
Expand Down
Binary file added testdata/dnn/onnx/models/expand.onnx
Binary file not shown.
Binary file added testdata/dnn/onnx/models/expand_identity.onnx
Binary file not shown.
Binary file added testdata/dnn/onnx/models/reduceL2_subgraph2_2.onnx
Binary file not shown.
19 changes: 19 additions & 0 deletions testdata/dnn/onnx/models/scale_broadcast_mid.onnx
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
pytorch1.9:t

0
12Mul_0"Multorch-jit-exportZ
0



Z
1



b
2



B
Binary file added testdata/dnn/onnx/models/slice_neg_starts.onnx
Binary file not shown.
22 changes: 22 additions & 0 deletions testdata/dnn/onnx/models/split_neg_axis.onnx
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
pytorch1.9:�
S
tensor12345Split_0"Split*
axis����������*
split@@@@@�
1
1
2
3
4
56Concat_1"Concat*
axis�torch-jit-exportZ
tensor



b
6



B