-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
445 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// 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. | ||
|
||
#include "pass_level1.h" | ||
|
||
#include "../utils.h" | ||
|
||
namespace pnnx { | ||
|
||
class Fold : public FuseModulePass | ||
{ | ||
public: | ||
const char* match_type_str() const | ||
{ | ||
return "__torch__.torch.nn.modules.fold.Fold"; | ||
} | ||
|
||
const char* type_str() const | ||
{ | ||
return "nn.Fold"; | ||
} | ||
|
||
void write(Operator* op, const std::shared_ptr<torch::jit::Graph>& graph, const torch::jit::Module& mod) const | ||
{ | ||
const torch::jit::Node* col2im = find_node_by_kind(graph, "aten::col2im"); | ||
|
||
op->params["output_size"] = col2im->namedInput("output_size"); | ||
op->params["kernel_size"] = col2im->namedInput("kernel_size"); | ||
op->params["stride"] = col2im->namedInput("stride"); | ||
op->params["padding"] = col2im->namedInput("padding"); | ||
op->params["dilation"] = col2im->namedInput("dilation"); | ||
} | ||
}; | ||
|
||
REGISTER_GLOBAL_PNNX_FUSE_MODULE_PASS(Fold) | ||
|
||
} // namespace pnnx |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// 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. | ||
|
||
#include "pass_level1.h" | ||
|
||
#include "../utils.h" | ||
|
||
namespace pnnx { | ||
|
||
class Unfold : public FuseModulePass | ||
{ | ||
public: | ||
const char* match_type_str() const | ||
{ | ||
return "__torch__.torch.nn.modules.fold.Unfold"; | ||
} | ||
|
||
const char* type_str() const | ||
{ | ||
return "nn.Unfold"; | ||
} | ||
|
||
void write(Operator* op, const std::shared_ptr<torch::jit::Graph>& graph, const torch::jit::Module& mod) const | ||
{ | ||
const torch::jit::Node* im2col = find_node_by_kind(graph, "aten::im2col"); | ||
|
||
op->params["kernel_size"] = im2col->namedInput("kernel_size"); | ||
op->params["stride"] = im2col->namedInput("stride"); | ||
op->params["padding"] = im2col->namedInput("padding"); | ||
op->params["dilation"] = im2col->namedInput("dilation"); | ||
} | ||
}; | ||
|
||
REGISTER_GLOBAL_PNNX_FUSE_MODULE_PASS(Unfold) | ||
|
||
} // namespace pnnx |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// 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. | ||
|
||
#include "pass_level2.h" | ||
|
||
namespace pnnx { | ||
|
||
class F_fold : public GraphRewriterPass | ||
{ | ||
public: | ||
const char* match_pattern_graph() const | ||
{ | ||
return R"PNNXIR(7767517 | ||
8 7 | ||
pnnx.Input input_0 0 1 input | ||
pnnx.Input input_1 0 1 output_size | ||
pnnx.Input input_2 0 1 kernel_size | ||
pnnx.Input input_3 0 1 dilation | ||
pnnx.Input input_4 0 1 padding | ||
pnnx.Input input_5 0 1 stride | ||
aten::col2im op_0 6 1 input output_size kernel_size dilation padding stride out | ||
pnnx.Output output 1 0 out | ||
)PNNXIR"; | ||
} | ||
|
||
const char* type_str() const | ||
{ | ||
return "F.fold"; | ||
} | ||
}; | ||
|
||
REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(F_fold, 10) | ||
|
||
} // namespace pnnx |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// 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. | ||
|
||
#include "pass_level2.h" | ||
|
||
namespace pnnx { | ||
|
||
class F_unfold : public GraphRewriterPass | ||
{ | ||
public: | ||
const char* match_pattern_graph() const | ||
{ | ||
return R"PNNXIR(7767517 | ||
7 6 | ||
pnnx.Input input_0 0 1 input | ||
pnnx.Input input_1 0 1 kernel_size | ||
pnnx.Input input_2 0 1 dilation | ||
pnnx.Input input_3 0 1 padding | ||
pnnx.Input input_4 0 1 stride | ||
aten::im2col op_0 5 1 input kernel_size dilation padding stride out | ||
pnnx.Output output 1 0 out | ||
)PNNXIR"; | ||
} | ||
|
||
const char* type_str() const | ||
{ | ||
return "F.unfold"; | ||
} | ||
}; | ||
|
||
REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(F_unfold, 10) | ||
|
||
} // namespace pnnx |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
# 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 | ||
import torch.nn.functional as F | ||
from packaging import version | ||
|
||
class Model(nn.Module): | ||
def __init__(self): | ||
super(Model, self).__init__() | ||
|
||
def forward(self, x, y, z): | ||
x = F.fold(x, output_size=22, kernel_size=3) | ||
y = F.fold(y, output_size=(17,18), kernel_size=(2,4), stride=(2,1), padding=2, dilation=1) | ||
z = F.fold(z, output_size=(5,11), kernel_size=(1,3), stride=1, padding=(2,4), dilation=1) | ||
|
||
return x, y, z | ||
|
||
def test(): | ||
net = Model() | ||
net.eval() | ||
|
||
torch.manual_seed(0) | ||
x = torch.rand(1, 108, 400) | ||
y = torch.rand(1, 96, 190) | ||
z = torch.rand(1, 33, 153) | ||
|
||
a0, a1, a2 = net(x, y, z) | ||
|
||
# export torchscript | ||
mod = torch.jit.trace(net, (x, y, z)) | ||
mod.save("test_F_fold.pt") | ||
|
||
# torchscript to pnnx | ||
import os | ||
os.system("../src/pnnx test_F_fold.pt inputshape=[1,108,400],[1,96,190],[1,33,153]") | ||
|
||
# pnnx inference | ||
import test_F_fold_pnnx | ||
b0, b1, b2 = test_F_fold_pnnx.test_inference() | ||
|
||
return torch.equal(a0, b0) and torch.equal(a1, b1) and torch.equal(a2, b2) | ||
|
||
if __name__ == "__main__": | ||
if test(): | ||
exit(0) | ||
else: | ||
exit(1) |
Oops, something went wrong.