Elffuss BraidHub

kikocis/ncnn public

master · 2.1 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 "convert_torch_chunk.h"
16
17namespace pnnx {
18
19namespace ncnn {
20
21void convert_torch_chunk(Graph& graph)
22{
23 int op_index = 0;
24
25 for (Operator* op : graph.ops)
26 {
27 if (op->type != "torch.chunk")
28 continue;
29
30 op->type = "Slice";
31 op->name = std::string("chunk_") + std::to_string(op_index++);
32
33 const int batch_index = op->inputs[0]->params["__batch_index"].i;
34
35 int axis = op->params.at("dim").i;
36 if (axis == batch_index)
37 {
38 fprintf(stderr, "chunk along batch axis %d is not supported\n", batch_index);
39 continue;
40 }
41
42 if (axis < 0)
43 {
44 int input_rank = op->inputs[0]->shape.size();
45 axis = input_rank + axis;
46 }
47
48 int chunks = op->params.at("chunks").i;
49
50 if (!op->inputs[0]->shape.empty())
51 {
52 int size = op->inputs[0]->shape[axis];
53 if (size % chunks != 0)
54 {
55 fprintf(stderr, "chunk with non-perfect divided size %d / %d is not supported\n", size, chunks);
56 }
57 }
58
59 if (axis > batch_index)
60 axis -= 1;
61
62 op->params["0"].type = 5;
63 op->params["0"].ai.resize(chunks, -233);
64
65 op->params["1"] = axis;
66
67 op->params.erase("chunks");
68 op->params.erase("dim");
69 }
70}
71
72} // namespace ncnn
73
74} // namespace pnnx