Elffuss BraidHub

kikocis/ncnn public

master · 4.8 KiBRawPermalink
1// Tencent is pleased to support the open source community by making ncnn available.
2//
3// Copyright (C) 2021 THL A29 Limited, a Tencent company. All rights reserved.
4//
5// Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
6// in compliance with the License. You may obtain a copy of the License at
7//
8// https://opensource.org/licenses/BSD-3-Clause
9//
10// Unless required by applicable law or agreed to in writing, software distributed
11// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
12// CONDITIONS OF ANY KIND, either express or implied. See the License for the
13// specific language governing permissions and limitations under the License.
14
15#include "pass_level1.h"
16
17// #include "../pass_level3/fuse_expression.h"
18
19#include "../utils.h"
20
21namespace pnnx {
22
23class Conv1d : public FuseModulePass
24{
25public:
26 const char* match_type_str() const
27 {
28 return "__torch__.torch.nn.modules.conv.Conv1d";
29 }
30
31 const char* type_str() const
32 {
33 return "nn.Conv1d";
34 }
35
36 void write(Operator* op, const std::shared_ptr<torch::jit::Graph>& graph, const torch::jit::Module& mod) const
37 {
38 // {
39 // pnnx::Graph pnnx_graph;
40 //
41 // pnnx_graph.load(mod, graph);
42 //
43 // pnnx::fuse_expression(pnnx_graph);
44 //
45 // pnnx_graph.save("tmp.param", "tmp.bin");
46 // }
47
48 const torch::jit::Node* convolution = find_node_by_kind(graph, "aten::_convolution");
49 const torch::jit::Node* convolution_mode = find_node_by_kind(graph, "aten::_convolution_mode");
50 const torch::jit::Node* pad = find_node_by_kind(graph, "aten::pad");
51 const torch::jit::Node* reflection_pad1d = find_node_by_kind(graph, "aten::reflection_pad1d");
52 const torch::jit::Node* replication_pad1d = find_node_by_kind(graph, "aten::replication_pad1d");
53
54 if (convolution_mode)
55 {
56 convolution = convolution_mode;
57 }
58
59 const auto& weight = mod.attr("weight").toTensor();
60
61 op->params["groups"] = convolution->namedInput("groups");
62 op->params["in_channels"] = weight.size(1) * op->params["groups"].i;
63 op->params["out_channels"] = weight.size(0);
64 op->params["kernel_size"] = Parameter{weight.size(2)};
65 op->params["stride"] = convolution->namedInput("stride");
66 if (pad)
67 {
68 op->params["padding_mode"] = pad->namedInput("mode");
69 op->params["padding"] = pad->namedInput("pad");
70 std::vector<int>& padding = op->params["padding"].ai;
71 if (padding.size() == 2)
72 {
73 // Conv1d only accepts tuple of one integer
74 if (padding[0] == padding[1])
75 {
76 padding.resize(1);
77 }
78 else if (padding[0] != padding[1])
79 {
80 padding.resize(0);
81 op->params["padding"].s = "same";
82 }
83 }
84 }
85 else if (reflection_pad1d)
86 {
87 op->params["padding_mode"] = "reflect";
88 op->params["padding"] = reflection_pad1d->namedInput("padding");
89 std::vector<int>& padding = op->params["padding"].ai;
90 if (padding.size() == 2)
91 {
92 // Conv1d only accepts tuple of one integer
93 if (padding[0] == padding[1])
94 {
95 padding.resize(1);
96 }
97 else if (padding[0] != padding[1])
98 {
99 padding.resize(0);
100 op->params["padding"].s = "same";
101 }
102 }
103 }
104 else if (replication_pad1d)
105 {
106 op->params["padding_mode"] = "replicate";
107 op->params["padding"] = replication_pad1d->namedInput("padding");
108 std::vector<int>& padding = op->params["padding"].ai;
109 if (padding.size() == 2)
110 {
111 // Conv1d only accepts tuple of one integer
112 if (padding[0] == padding[1])
113 {
114 padding.resize(1);
115 }
116 else if (padding[0] != padding[1])
117 {
118 padding.resize(0);
119 op->params["padding"].s = "same";
120 }
121 }
122 }
123 else
124 {
125 op->params["padding_mode"] = "zeros";
126 op->params["padding"] = convolution->namedInput("padding");
127 }
128 op->params["dilation"] = convolution->namedInput("dilation");
129 op->params["bias"] = mod.hasattr("bias");
130
131 op->attrs["weight"] = weight;
132 if (mod.hasattr("bias"))
133 {
134 op->attrs["bias"] = mod.attr("bias").toTensor();
135 }
136 }
137};
138
139REGISTER_GLOBAL_PNNX_FUSE_MODULE_PASS(Conv1d)
140
141} // namespace pnnx