-
Notifications
You must be signed in to change notification settings - Fork 351
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: replace view with reshape during lowering
Signed-off-by: Ruoqian Guo <ruoqiang@nvidia.com>
- Loading branch information
1 parent
09afccb
commit d39b918
Showing
6 changed files
with
74 additions
and
0 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,31 @@ | ||
#include <torch/csrc/jit/passes/subgraph_rewrite.h> | ||
#include "core/util/prelude.h" | ||
|
||
namespace torch_tensorrt { | ||
namespace core { | ||
namespace lowering { | ||
namespace passes { | ||
|
||
void ViewToReshape(std::shared_ptr<torch::jit::Graph>& graph) { | ||
std::string view_pattern = R"IR( | ||
graph(%x, %1): | ||
%out : Tensor = aten::view(%x, %1) | ||
return (%out))IR"; | ||
|
||
std::string reshape_pattern = R"IR( | ||
graph(%x, %1): | ||
%out : Tensor = aten::reshape(%x, %1) | ||
return (%out))IR"; | ||
|
||
// replace aten::view with aten::reshape | ||
torch::jit::SubgraphRewriter map_view_to_reshape; | ||
map_view_to_reshape.RegisterRewritePattern(view_pattern, reshape_pattern); | ||
map_view_to_reshape.runOnGraph(graph); | ||
|
||
LOG_GRAPH("Post lowering of aten::view -> " << *graph); | ||
} | ||
|
||
} // namespace passes | ||
} // namespace lowering | ||
} // namespace core | ||
} // namespace torch_tensorrt |
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,35 @@ | ||
#include <string> | ||
#include "core/compiler.h" | ||
#include "core/lowering/passes/passes.h" | ||
#include "gtest/gtest.h" | ||
#include "tests/util/util.h" | ||
#include "torch/csrc/jit/ir/irparser.h" | ||
#include "torch/csrc/jit/ir/subgraph_matcher.h" | ||
|
||
TEST(LoweringPasses, ViewToReshapeCorrectly) { | ||
std::string source_graph = R"IR( | ||
graph(%x : Tensor, %1, %1.1): | ||
%0 : int = prim::Constant[value=0]() | ||
%2 : Tensor = aten::permute(%x, %1) | ||
%3 : Tensor = aten::contiguous(%2, %0) | ||
%4 : Tensor = aten::view(%3, %1.1) | ||
return (%4))IR"; | ||
std::string target_graph = R"IR( | ||
graph(%x : Tensor, %1, %1.1): | ||
%0 : int = prim::Constant[value=0]() | ||
%2 : Tensor = aten::permute(%x, %1) | ||
%4 : Tensor = aten::reshape(%2, %1.1) | ||
return (%4))IR"; | ||
|
||
torch_tensorrt::core::util::logging::get_logger().set_reportable_log_level( | ||
torch_tensorrt::core::util::logging::LogLevel::kGRAPH); | ||
auto sg = std::make_shared<torch::jit::Graph>(); | ||
torch::jit::parseIR(source_graph, &*sg); | ||
torch_tensorrt::core::lowering::passes::RemoveContiguous(sg); | ||
torch_tensorrt::core::lowering::passes::ViewToReshape(sg); | ||
|
||
auto tg = std::make_shared<torch::jit::Graph>(); | ||
torch::jit::parseIR(target_graph, &*tg); | ||
|
||
ASSERT_TRUE(!torch::jit::findPatternMatches(*tg, *sg).empty()); | ||
} |