forked from Tencent/ncnn
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add new pass trans_Stack2Unsqueeze, When using torch.stack with a sin…
…gle input and effectively achieving the same result as torch.unsqueeze
- Loading branch information
sen.li
committed
May 11, 2024
1 parent
4585c6e
commit 0c29d7c
Showing
7 changed files
with
88 additions
and
4 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
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,57 @@ | ||
// 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. | ||
|
||
#include "trans_Stack2Unsqueeze.h" | ||
|
||
#include <algorithm> | ||
#include "pass_level2.h" | ||
|
||
namespace pnnx { | ||
|
||
void trans_Stack2Unsqueeze(Graph& graph) | ||
{ | ||
while (1) | ||
{ | ||
bool matched = false; | ||
|
||
for (size_t i = 0; i < graph.ops.size(); i++) | ||
{ | ||
Operator* op = graph.ops[i]; | ||
|
||
if (op->type != "torch.stack") | ||
continue; | ||
// get input num | ||
if( op->inputs.size() == 1) | ||
{ | ||
op->type = "torch.unsqueeze"; | ||
std::string str = op->name; | ||
std::string from = "torch.stack"; | ||
std::string to = "torch.unsqueeze"; | ||
|
||
// to find sub str | ||
size_t start_pos = str.find(from); | ||
if(start_pos != std::string::npos) { | ||
// replace sub str | ||
str.replace(start_pos, from.length(), to); | ||
} | ||
op->name = str; | ||
} | ||
} | ||
|
||
if (!matched) | ||
break; | ||
} | ||
} | ||
|
||
} // 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,22 @@ | ||
|
||
// 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. | ||
|
||
#include "ir.h" | ||
|
||
namespace pnnx { | ||
|
||
void trans_Stack2Unsqueeze(Graph& graph); | ||
|
||
} // 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