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 median to pnnx #4934

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions tools/pnnx/src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@ set(pnnx_pass_level2_SRCS
pass_level2/torch_matmul.cpp
pass_level2/torch_max.cpp
pass_level2/torch_mean.cpp
pass_level2/torch_median.cpp
pass_level2/torch_min.cpp
pass_level2/torch_mm.cpp
pass_level2/torch_ne.cpp
Expand Down
41 changes: 41 additions & 0 deletions tools/pnnx/src/pass_level2/torch_mm - 副本.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) 2023 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_median : 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 mat2
aten::mm op_0 2 1 input mat2 out
pnnx.Output output 1 0 out
)PNNXIR";
}

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

REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(torch_median, 20)

} // namespace pnnx
54 changes: 54 additions & 0 deletions tools/pnnx/tests/test_torch_median.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) 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.

import torch
import torch.nn as nn
import torch.nn.functional as F

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

def forward(self, x):
# ¼ÆËãÖÐλÊý
out = torch.median(x, dim=1).values
return out

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

torch.manual_seed(0)
x = torch.rand(3, 5)
a = net(x)

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

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

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

return torch.equal(a, b)

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