Elffuss BraidHub

kikocis/ncnn public

master · 1.7 KiBRawPermalink
1// Tencent is pleased to support the open source community by making ncnn available.
2//
3// Copyright (C) 2023 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 "flatten_input.h"
16
17namespace pnnx {
18
19void flatten_input(std::shared_ptr<torch::jit::Graph>& graph)
20{
21 while (1)
22 {
23 bool matched = false;
24
25 for (torch::jit::Node* n : graph->nodes())
26 {
27 if (n->kind() != c10::prim::TupleUnpack && n->kind() != c10::prim::ListUnpack)
28 continue;
29
30 for (size_t i = 1; i < graph->inputs().size(); i++)
31 {
32 if (n->input(0) == graph->inputs()[i])
33 {
34 matched = true;
35
36 for (size_t j = 0; j < n->outputs().size(); j++)
37 {
38 torch::jit::Value* v2 = graph->insertInput(i + 1 + j);
39 n->output(j)->replaceAllUsesWith(v2);
40 }
41 n->destroy();
42 graph->eraseInput(i);
43 break;
44 }
45 }
46
47 if (matched)
48 break;
49 }
50
51 if (!matched)
52 break;
53 }
54}
55
56} // namespace pnnx