Skip to content
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

convert torch_mv #4974

Merged
merged 3 commits into from
Sep 14, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions tools/pnnx/src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,7 @@ set(pnnx_pass_level2_SRCS
pass_level2/torch_mean.cpp
pass_level2/torch_min.cpp
pass_level2/torch_mm.cpp
pass_level2/torch_mv.cpp
pass_level2/torch_narrow.cpp
pass_level2/torch_ne.cpp
pass_level2/torch_norm.cpp
Expand Down
41 changes: 41 additions & 0 deletions tools/pnnx/src/pass_level2/torch_mv.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Tencent is pleased to support the open source community by making ncnn available.
//
// Copyright (C) 2021 THL A29 Limited, a Tencent company. All rights reserved.
//
// Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
// in compliance with the License. You may obtain a copy of the License at
//
// https://opensource.org/licenses/BSD-3-Clause
//
// 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.

#include "pass_level2.h"

namespace pnnx {

class torch_mv : public GraphRewriterPass
{
public:
const char* match_pattern_graph() const
{
return R"PNNXIR(7767517
4 3
pnnx.Input input_0 0 1 input
pnnx.Input input_1 0 1 vec
aten::mv op_1 2 1 input vec output
pnnx.Output output 1 0 output
)PNNXIR";
}

const char* type_str() const
{
return "torch.mv";
}
};

REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(torch_mv, 20)

} // namespace pnnx
1 change: 1 addition & 0 deletions tools/pnnx/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ pnnx_add_test(torch_max)
pnnx_add_test(torch_mean)
pnnx_add_test(torch_min)
pnnx_add_test(torch_mm)
pnnx_add_test(torch_mv)
pnnx_add_test(torch_narrow)
pnnx_add_test(torch_ne)
pnnx_add_test(torch_norm)
Expand Down
54 changes: 54 additions & 0 deletions tools/pnnx/tests/test_torch_mv.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# Tencent is pleased to support the open source community by making ncnn available.
#
# Copyright (C) 2022 THL A29 Limited, a Tencent company. All rights reserved.
#
# Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
# in compliance with the License. You may obtain a copy of the License at
#
# https://opensource.org/licenses/BSD-3-Clause
#
# 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 torch
import torch.nn as nn

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

def forward(self, x, y):
out = torch.mv(x, y)
return out

def test():
net = Model()
net.eval()

torch.manual_seed(0)
x = torch.rand(2, 3)
y = torch.rand(3)

a = net(x, y)

# export torchscript
mod = torch.jit.trace(net, (x, y))
mod.save("test_torch_mv.pt")

# torchscript to pnnx
import os
os.system("../src/pnnx test_torch_mv.pt inputshape=[2,3],[3]")

# pnnx inference
import test_torch_mv_pnnx
b = test_torch_mv_pnnx.test_inference()

return torch.equal(a, b)

if __name__ == "__main__":
if test():
exit(0)
else:
exit(1)