|
| 1 | +// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +#include <memory> |
| 16 | +#include "gtest/gtest.h" |
| 17 | +#include "paddle/fluid/framework/details/computation_op_handle.h" |
| 18 | +#include "paddle/fluid/framework/details/eager_deletion_op_handle.h" |
| 19 | +#include "paddle/fluid/framework/ir/graph.h" |
| 20 | +#include "paddle/fluid/framework/ir/memory_optimize_pass/memory_optimization_var_info.h" |
| 21 | +#include "paddle/fluid/framework/ir/pass.h" |
| 22 | +#include "paddle/fluid/framework/paddle2cinn/build_cinn_pass.h" |
| 23 | +#include "paddle/fluid/framework/paddle2cinn/cinn_compiler.h" |
| 24 | +#include "paddle/fluid/framework/parallel_executor.h" |
| 25 | +#include "paddle/fluid/framework/program_desc.h" |
| 26 | + |
| 27 | +USE_OP(mul); |
| 28 | +USE_OP(cinn_launch); |
| 29 | +USE_OP(elementwise_add); |
| 30 | +namespace paddle::framework { |
| 31 | + |
| 32 | +using Name2VarInfoMap = |
| 33 | + std::unordered_map<std::string, std::shared_ptr<ir::MemOptVarInfo>>; |
| 34 | + |
| 35 | +static ProgramDesc BuildProgramInsideCinnLaunchOp() { |
| 36 | + ProgramDesc program; |
| 37 | + auto* block = program.MutableBlock(0); |
| 38 | + block->Var("var1"); |
| 39 | + block->Var("var2"); |
| 40 | + block->Var("var3"); |
| 41 | + block->Var("var4"); |
| 42 | + block->Var("var5"); |
| 43 | + |
| 44 | + auto add_op = std::unique_ptr<OpDesc>( |
| 45 | + new OpDesc("elementwise_add", {{"X", {"var1"}}, {"Y", {"var2"}}}, |
| 46 | + {{"Out", {"var3"}}}, {})); |
| 47 | + block->AppendAllocatedOp(std::move(add_op)); |
| 48 | + auto mul_op = std::unique_ptr<OpDesc>(new OpDesc( |
| 49 | + "mul", {{"X", {"var3"}}, {"Y", {"var4"}}}, {{"Out", {"var5"}}}, {})); |
| 50 | + block->AppendAllocatedOp(std::move(mul_op)); |
| 51 | + return program; |
| 52 | +} |
| 53 | + |
| 54 | +static ProgramDesc BuildProgramWithCinnLaunchOp( |
| 55 | + const std::string& compilation_key) { |
| 56 | + // create a cinn_launch op |
| 57 | + ProgramDesc program; |
| 58 | + auto* block = program.MutableBlock(0); |
| 59 | + block->Var("var1"); |
| 60 | + block->Var("var2"); |
| 61 | + block->Var("var4"); |
| 62 | + block->Var("var5"); |
| 63 | + |
| 64 | + auto cinn_launch_op = std::unique_ptr<OpDesc>( |
| 65 | + new OpDesc("cinn_launch", {{"X", {"var1", "var2", "var4"}}}, |
| 66 | + {{"Out", {"var5"}}}, {{"compilation_key", compilation_key}})); |
| 67 | + block->AppendAllocatedOp(std::move(cinn_launch_op)); |
| 68 | + return program; |
| 69 | +} |
| 70 | + |
| 71 | +struct TestPassContext { |
| 72 | + explicit TestPassContext(const ProgramDesc& program) { |
| 73 | + graph = std::make_unique<ir::Graph>(program); |
| 74 | + details::BuildStrategy build_strategy; |
| 75 | + details::ExecutionStrategy exec_strategy; |
| 76 | + exec_strategy.use_device_ = paddle::platform::kCUDA; |
| 77 | + executor.reset(new ParallelExecutor(platform::CUDAPlace(0), &scope, |
| 78 | + exec_strategy, build_strategy, |
| 79 | + graph.get())); |
| 80 | + } |
| 81 | + |
| 82 | + Scope scope; |
| 83 | + std::unique_ptr<ir::Graph> graph; |
| 84 | + std::unique_ptr<ParallelExecutor> executor; |
| 85 | +}; |
| 86 | + |
| 87 | +TEST(ShareMemInfoToSubGraphPassTest, test_main_graph_share_varinfo) { |
| 88 | + // add a subgraph to CinnCompiler |
| 89 | + auto subgraph = std::make_unique<ir::Graph>(BuildProgramInsideCinnLaunchOp()); |
| 90 | + subgraph->GetOrInit<Name2VarInfoMap>( |
| 91 | + paddle2cinn::kMemOptVarInfoFromMainGraph); |
| 92 | + std::string compilation_key = |
| 93 | + paddle2cinn::CinnCompiler::GetInstance()->AddGraph(std::move(subgraph)); |
| 94 | + |
| 95 | + // build test data and apply pass |
| 96 | + auto context = std::make_unique<TestPassContext>( |
| 97 | + BuildProgramWithCinnLaunchOp(compilation_key)); |
| 98 | + |
| 99 | + // check result |
| 100 | + const ir::Graph& result_subgraph = |
| 101 | + paddle2cinn::CinnCompiler::GetInstance()->FindGraph(compilation_key); |
| 102 | + const auto& dst_varinfo_map = result_subgraph.Get<Name2VarInfoMap>( |
| 103 | + paddle2cinn::kMemOptVarInfoFromMainGraph); |
| 104 | + ASSERT_EQ(dst_varinfo_map.size(), 4); |
| 105 | + EXPECT_EQ(dst_varinfo_map.count("var1"), 1); |
| 106 | + EXPECT_EQ(dst_varinfo_map.count("var5"), 1); |
| 107 | + EXPECT_EQ(dst_varinfo_map.at("var1").use_count(), 2); |
| 108 | + EXPECT_EQ(dst_varinfo_map.at("var5").use_count(), 2); |
| 109 | +} |
| 110 | + |
| 111 | +TEST(ShareMemInfoToSubGraphPassTest, test_subgraph_take_varinfo) { |
| 112 | + // build test data and apply pass |
| 113 | + auto context = |
| 114 | + std::make_unique<TestPassContext>(BuildProgramInsideCinnLaunchOp()); |
| 115 | + auto& varinfo_map_shared = context->graph->GetOrInit<Name2VarInfoMap>( |
| 116 | + paddle2cinn::kMemOptVarInfoFromMainGraph); |
| 117 | + varinfo_map_shared = { |
| 118 | + {"var1", std::make_shared<ir::MemOptVarInfo>("var1", 1)}, |
| 119 | + {"var2", std::make_shared<ir::MemOptVarInfo>("var2", 2)}, |
| 120 | + }; |
| 121 | + |
| 122 | + ir::MemOptVarInfoMapList varinfo_maps(1); |
| 123 | + auto& dst_varinfo_map = varinfo_maps.front(); |
| 124 | + dst_varinfo_map = {{"var1", std::make_shared<ir::MemOptVarInfo>("var1", 1)}, |
| 125 | + {"var2", std::make_shared<ir::MemOptVarInfo>("var2", 1)}, |
| 126 | + {"var3", std::make_shared<ir::MemOptVarInfo>("var3", 1)}, |
| 127 | + {"var4", std::make_shared<ir::MemOptVarInfo>("var4", 1)}, |
| 128 | + {"var5", std::make_shared<ir::MemOptVarInfo>("var5", 1)}}; |
| 129 | + auto share_pass = |
| 130 | + ir::PassRegistry::Instance().Get("share_varinfo_into_cinn_pass"); |
| 131 | + share_pass->SetNotOwned(ir::kMemOptVarInfoMapList, &varinfo_maps); |
| 132 | + share_pass->Apply(context->graph.get()); |
| 133 | + |
| 134 | + // check result |
| 135 | + ASSERT_NE(dst_varinfo_map.at("var1")->ParentHolder(), nullptr); |
| 136 | + ASSERT_NE(dst_varinfo_map.at("var2")->ParentHolder(), nullptr); |
| 137 | + ASSERT_EQ(dst_varinfo_map.at("var3")->ParentHolder(), nullptr); |
| 138 | + ASSERT_EQ(dst_varinfo_map.at("var4")->ParentHolder(), nullptr); |
| 139 | + ASSERT_EQ(dst_varinfo_map.at("var5")->ParentHolder(), nullptr); |
| 140 | +} |
| 141 | + |
| 142 | +} // namespace paddle::framework |
0 commit comments