diff --git a/paddle/fluid/framework/details/build_strategy.cc b/paddle/fluid/framework/details/build_strategy.cc index e44edfca1bdf0c..79578e5653a227 100644 --- a/paddle/fluid/framework/details/build_strategy.cc +++ b/paddle/fluid/framework/details/build_strategy.cc @@ -61,8 +61,6 @@ class ParallelExecutorPassBuilder : public ir::PassBuilder { // Note: This is a trick to support 0D-Tensor for CINN. This pass will be // removed in the near future. AppendPass("cinn_zero_tensor_trick_pass"); - // Note: This pass is used to enable cinn. - AppendPass("build_cinn_pass"); AppendPrintGraphPass("graph_viz_pass", "_build_cinn_graph"); } #endif diff --git a/paddle/fluid/framework/ir/memory_optimize_pass/eager_deletion_pass.cc b/paddle/fluid/framework/ir/memory_optimize_pass/eager_deletion_pass.cc index 0398117e08b8fb..796e9a5e7f0a90 100644 --- a/paddle/fluid/framework/ir/memory_optimize_pass/eager_deletion_pass.cc +++ b/paddle/fluid/framework/ir/memory_optimize_pass/eager_deletion_pass.cc @@ -305,13 +305,6 @@ void EagerDeletionPass::ApplyImpl(ir::Graph *graph) const { auto recurrent_op_eager_deletion_pass = ir::PassRegistry::Instance().Get("recurrent_op_eager_deletion_pass"); recurrent_op_eager_deletion_pass->Apply(graph); - -#ifdef PADDLE_WITH_CINN - auto share_varinfo_into_cinn_pass = - ir::PassRegistry::Instance().Get("share_varinfo_into_cinn_pass"); - share_varinfo_into_cinn_pass->SetNotOwned(kMemOptVarInfoMapList, &var_infos); - share_varinfo_into_cinn_pass->Apply(graph); -#endif } } // namespace ir diff --git a/paddle/fluid/framework/ir/memory_optimize_pass/share_varinfo_into_cinn_pass.cc b/paddle/fluid/framework/ir/memory_optimize_pass/share_varinfo_into_cinn_pass.cc index f48897674143af..4dde29316d23ee 100644 --- a/paddle/fluid/framework/ir/memory_optimize_pass/share_varinfo_into_cinn_pass.cc +++ b/paddle/fluid/framework/ir/memory_optimize_pass/share_varinfo_into_cinn_pass.cc @@ -20,7 +20,7 @@ #include "paddle/fluid/framework/ir/memory_optimize_pass/memory_optimization_var_info.h" #include "paddle/fluid/framework/paddle2cinn/build_cinn_pass.h" #include "paddle/fluid/framework/paddle2cinn/cinn_compiler.h" -#include "paddle/fluid/operators/cinn/cinn_launch_op.h" +#include "paddle/fluid/operators/cinn/cinn_op_helper.h" #include "paddle/fluid/platform/enforce.h" #include "paddle/utils/string/string_helper.h" diff --git a/paddle/fluid/framework/paddle2cinn/build_cinn_pass.cc b/paddle/fluid/framework/paddle2cinn/build_cinn_pass.cc index 90567bd728cd5a..ce4304b4ec2287 100644 --- a/paddle/fluid/framework/paddle2cinn/build_cinn_pass.cc +++ b/paddle/fluid/framework/paddle2cinn/build_cinn_pass.cc @@ -36,7 +36,7 @@ limitations under the License. */ #include "paddle/fluid/framework/op_proto_maker.h" #include "paddle/fluid/framework/paddle2cinn/cinn_compiler.h" #include "paddle/fluid/framework/paddle2cinn/cinn_subgraph_detector.h" -#include "paddle/fluid/operators/cinn/cinn_launch_op.h" +#include "paddle/fluid/operators/cinn/cinn_op_helper.h" #include "paddle/fluid/platform/enforce.h" #include "paddle/fluid/platform/errors.h" diff --git a/paddle/fluid/inference/api/paddle_pass_builder.cc b/paddle/fluid/inference/api/paddle_pass_builder.cc index 7b399d67d3557f..495ff86bfee8f4 100644 --- a/paddle/fluid/inference/api/paddle_pass_builder.cc +++ b/paddle/fluid/inference/api/paddle_pass_builder.cc @@ -228,7 +228,6 @@ const std::vector kCINNCompilerPasses{ "gpu_cpu_map_matmul_v2_to_mul_pass", "gpu_cpu_map_matmul_v2_to_matmul_pass", "gpu_cpu_map_matmul_to_mul_pass", - "build_cinn_pass", }; const std::vector CpuBasicPasses{ diff --git a/paddle/fluid/operators/cinn/cinn_instruction_run_op.cc b/paddle/fluid/operators/cinn/cinn_instruction_run_op.cc deleted file mode 100644 index 5d4e3ae331596b..00000000000000 --- a/paddle/fluid/operators/cinn/cinn_instruction_run_op.cc +++ /dev/null @@ -1,126 +0,0 @@ -// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// 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 "paddle/fluid/operators/cinn/cinn_instruction_run_op.h" - -#include "paddle/fluid/framework/paddle2cinn/cinn_compiler.h" -#include "paddle/fluid/operators/cinn/cinn_launch_context.h" -#include "paddle/fluid/platform/enforce.h" - -namespace paddle::operators { - -class CinnInstructionRunOp : public framework::OperatorWithKernel { - public: - using framework::OperatorWithKernel::OperatorWithKernel; - - void InferShape(framework::InferShapeContext* ctx) const override { - // The cinn-graph may hasn't input for CINN now support fill_constant, - // and its all inputs may generated by fill_constant instead of by fetch. - // OP_INOUT_CHECK(ctx->HasInputs(kX), "Input", kX, "CinnInstructionRun"); - OP_INOUT_CHECK( - ctx->HasOutputs(kOutputs), "Output", kOutputs, "CinnInstructionRun"); - const CinnCompiledObject& compiled_object = - CinnCompiler::GetInstance()->GetCompiledObject( - ctx->Attrs().Get(kCachedIndex)); - - details::CinnLaunchContext* launch_context = - compiled_object.launch_context.get(); - std::vector output_args = ctx->Outputs(kOutputs); - std::vector output_dims(output_args.size()); - std::transform(output_args.begin(), - output_args.end(), - output_dims.begin(), - [launch_context](const std::string& var_name) { - cinn_buffer_t* buffer = - launch_context->GetCinnBufferOfVar(var_name); - return framework::DDim(buffer->dims, buffer->dimensions); - }); - ctx->SetOutputsDim(kOutputs, output_dims); - } - - protected: - /* [Why use single type kernel]: - * - * Whether the kernel data type is int, float or other type, - * which has no effect on its execution logic, so directly - * specified a data type here. - * - */ - phi::KernelKey GetExpectedKernelType( - const framework::ExecutionContext& ctx) const override { - return phi::KernelKey(framework::proto::VarType::FP32, ctx.GetPlace()); - } -}; - -class CinnInstructionRunOpMaker : public framework::OpProtoAndCheckerMaker { - public: - void Make() override { - AddInput(kX, - "(vector)" - "which are the input arguments of this cinn instruction") - .AsDuplicable(); - AddOutput(kOutputs, - "(vector)" - "which are the output arguments of this cinn instruction") - .AsDuplicable(); - AddAttr( - kCachedIndex, - "(int64_t)" - "the stored index of the cached compilation result in CinnCompiler," - "which is used to fetch the CinnCompiledObject where this cinn " - "instruction is included"); - AddAttr( - kInstructionIndex, - "(int64_t)" - "the index of this instruction to the cinn runtime program"); - AddComment(R"DOC( -CinnInstructionRun Operator. - -This operator is used to launch a -CINN(https://github.com/PaddlePaddle/CINN/blob/develop/README.md) instruction execution - -Both the input and output of this operator are a set of variables -which are the input and output arguments of the bound cinn instruction respectively. -In addition, there is an attribute named 'cached_index' should be -set necessarily to get the CinnCompiledObject where the instruction is included -and 'instruction_index' is fetch the instruction object from complied runtime program. - -It accomplishes the execution of the instruction according to the following steps: - 0. Set the shapes ot the output variables at InferShape function with - compilation result. - 1. Fetch the cinn instruction bound to this operator by 'cached_index' - and 'instruction_index' from CinnCompiler. - 2. Prepare the input and output variables of the instruction in Paddle and share - their buffers to CINN by setting 'memory' of according cinn_buffer_t. - 3. Launch CINN runtime to execute the instruction. - -)DOC"); - } -}; - -} // namespace paddle::operators - -namespace ops = paddle::operators; -REGISTER_OPERATOR( - cinn_instruction_run, - ops::CinnInstructionRunOp, - ops::CinnInstructionRunOpMaker, - paddle::framework::EmptyGradOpMaker, - paddle::framework::EmptyGradOpMaker); - -PD_REGISTER_STRUCT_KERNEL(cinn_instruction_run, - CPU, - ALL_LAYOUT, - ops::CinnInstructionRunOpKernel, - float) {} diff --git a/paddle/fluid/operators/cinn/cinn_instruction_run_op.cu.cc b/paddle/fluid/operators/cinn/cinn_instruction_run_op.cu.cc deleted file mode 100644 index d5d236dbd65293..00000000000000 --- a/paddle/fluid/operators/cinn/cinn_instruction_run_op.cu.cc +++ /dev/null @@ -1,25 +0,0 @@ -/* Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -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 "paddle/fluid/operators/cinn/cinn_instruction_run_op.h" - -#include "paddle/fluid/framework/op_registry.h" - -namespace ops = paddle::operators; -/* see [Why use single type kernel] */ -PD_REGISTER_STRUCT_KERNEL(cinn_instruction_run, - GPU, - ALL_LAYOUT, - ops::CinnInstructionRunOpKernel, - float) {} diff --git a/paddle/fluid/operators/cinn/cinn_instruction_run_op.h b/paddle/fluid/operators/cinn/cinn_instruction_run_op.h deleted file mode 100644 index 766e8e6d2620db..00000000000000 --- a/paddle/fluid/operators/cinn/cinn_instruction_run_op.h +++ /dev/null @@ -1,83 +0,0 @@ -// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// 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. - -#pragma once - -#include -#include -#include -#include - -#include "paddle/cinn/hlir/framework/graph_compiler.h" -#include "paddle/cinn/hlir/framework/instruction.h" -#include "paddle/fluid/framework/op_registry.h" -#include "paddle/fluid/framework/operator.h" -#include "paddle/fluid/framework/paddle2cinn/cinn_compiler.h" -#include "paddle/fluid/framework/paddle2cinn/transform_type.h" -#include "paddle/fluid/operators/cinn/cinn_launch_context.h" -#include "paddle/fluid/operators/cinn/cinn_op_helper.h" - -namespace paddle::operators { - -using CinnInstruction = ::cinn::hlir::framework::Instruction; -using CinnCompiledObject = framework::paddle2cinn::CinnCompiledObject; -using CinnCompiler = framework::paddle2cinn::CinnCompiler; - -template -class CinnInstructionRunOpKernel : public framework::OpKernel { - public: - void Compute(const framework::ExecutionContext& ctx) const override { - // step 1: fetch the cinn instruction bound to this operator - auto cached_index = ctx.template Attr(kCachedIndex); - auto ins_index = ctx.template Attr(kInstructionIndex); - const CinnCompiledObject& compiled_object = - CinnCompiler::GetInstance()->GetCompiledObject(cached_index); - const std::vector>& instructions = - compiled_object.runtime_program->GetRunInstructions(); - PADDLE_ENFORCE_LT( - ins_index, - instructions.size(), - phi::errors::InvalidArgument("Index(%ld) > instructions.size(%ld).", - ins_index, - instructions.size())); - auto&& instruction = instructions.at(ins_index); - - // step 2: prepare the input and output arguments of the instruction - details::CinnLaunchContext* launch_context = - compiled_object.launch_context.get(); - auto share_argument_buffer_fn = [launch_context, - &ctx](const std::string& var_name) { - cinn_buffer_t* buffer = launch_context->GetCinnBufferOfVar(var_name); - std::string revise_var_name = launch_context->RedirectVarName(var_name); - framework::Variable* var = ctx.scope().GetVar(revise_var_name); - auto* tensor = var->template GetMutable(); - buffer->memory = reinterpret_cast(tensor->mutable_data( - ctx.GetPlace(), - framework::paddle2cinn::TransToPaddleDataType(buffer->type))); - }; - std::vector in_args = ctx.InputNames(kX); - std::for_each(in_args.begin(), in_args.end(), share_argument_buffer_fn); - std::vector out_args = ctx.OutputNames(kOutputs); - std::for_each(out_args.begin(), out_args.end(), share_argument_buffer_fn); - - // step 3: launch CINN runtime to execute the instruction - // TODO(CtfGo): simplify format of arguments package as a vector in CINN - // and update this usage call - instruction->Run(&launch_context->FinalizeArguments(), - false, - details::GetStream(ctx)); - } -}; - -} // namespace paddle::operators diff --git a/paddle/fluid/operators/cinn/cinn_launch_op.cc b/paddle/fluid/operators/cinn/cinn_launch_op.cc deleted file mode 100644 index a9154910acf11f..00000000000000 --- a/paddle/fluid/operators/cinn/cinn_launch_op.cc +++ /dev/null @@ -1,210 +0,0 @@ -// Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// 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 "paddle/fluid/operators/cinn/cinn_launch_op.h" - -#include -#include - -#include "paddle/cinn/common/target.h" -#include "paddle/cinn/hlir/framework/graph_compiler.h" -#include "paddle/cinn/runtime/cinn_runtime.h" -#include "paddle/cinn/runtime/flags.h" -#include "paddle/common/flags.h" -#include "paddle/phi/core/generator.h" -#include "paddle/utils/string/string_helper.h" - -#if defined(PADDLE_WITH_CUDA) -COMMON_DECLARE_bool(cudnn_deterministic); -#endif - -namespace paddle { -namespace operators { - -namespace details { - -const ::cinn::common::Target& PlaceToCinnTarget(const platform::Place& place) { - if (platform::is_cpu_place(place)) { - return ::cinn::common::DefaultHostTarget(); - } else if (platform::is_gpu_place(place)) { - return ::cinn::common::DefaultNVGPUTarget(); - } - - PADDLE_THROW(phi::errors::InvalidArgument( - "CINN is not supported on current place:%s", place)); - return ::cinn::common::UnkTarget(); -} - -void DebugCinnCompiledResult(const CinnCompiledObject& result) { - if (!VLOG_IS_ON(4)) { - return; - } - const auto& cinn_runtime_program = result.runtime_program; - const auto& cinn_scope = *(result.scope); - const auto& paddle2cinn_varmap = result.paddle2cinn_varmap; - - VLOG(4) << "Compiled runtime_program instruction size:[" - << cinn_runtime_program->size() << "]"; - - std::vector infos; - auto cinn_var_names = cinn_scope.var_names(); - infos.reserve(cinn_var_names.size()); - std::transform(cinn_var_names.begin(), - cinn_var_names.end(), - std::back_inserter(infos), - [](const auto& name_view) { return name_view.data(); }); - VLOG(4) << "Compiled scope variable names:[" - << string::join_strings(infos, ',') << "]"; - - infos.clear(); - infos.reserve(paddle2cinn_varmap.size()); - std::transform(paddle2cinn_varmap.begin(), - paddle2cinn_varmap.end(), - std::back_inserter(infos), - [](const auto& paddle2cinn) { - return paddle2cinn.first + "->" + paddle2cinn.second; - }); - VLOG(4) << "Compiled paddle2cinn_varmap:[" << string::join_strings(infos, ',') - << "]"; -} - -void LaunchCinnExecution(const CinnCompiledObject& compiled_obj, - const CinnLaunchContext& context, - void* stream) { - compiled_obj.runtime_program->Execute(&context.FinalizeArguments(), stream); -} - -void SetCinnRuntimeFlags() { -#if defined(PADDLE_WITH_CUDA) - VLOG(4) << "Set FLAGS_cinn_cudnn_deterministic to " - << FLAGS_cudnn_deterministic; - ::cinn::runtime::SetCinnCudnnDeterministic(FLAGS_cudnn_deterministic); -#endif -} - -template <> -void SetCinnRandomSeed() { - auto seed = phi::DefaultCPUGenerator()->GetCurrentSeed(); - ::cinn::runtime::RandomSeed::GetOrSet(seed); -} - -void SetCinnTarget(const ::cinn::common::Target& target) { - VLOG(4) << "Set CINN compile target to " << target; - ::cinn::runtime::CurrentTarget::SetCurrentTarget(target); -} - -} // namespace details - -class CinnLaunchOp : public framework::OperatorWithKernel { - public: - using framework::OperatorWithKernel::OperatorWithKernel; - - void InferShape(framework::InferShapeContext* ctx) const override { - // The cinn-graph may hasn't input for CINN now support fill_constant, - // and its all inputs may generated by fill_constant instead of by fetch. - // OP_INOUT_CHECK(ctx->HasInputs(kX) || ctx->HasInputs(kNoNeedBufferX), - // "Input", string::format_string("%s|%s", kX, - // kNoNeedBufferX), - // "CinnLaunchOp"); - OP_INOUT_CHECK( - ctx->HasOutputs(kOutputs), "Output", kOutputs, "CinnLaunchOp"); - } - - protected: - /* [Why use single type kernel]: - * - * This op is similar to a control flow op, it doses not need - * a op kernel, but in order to make it execute under dynamic - * graph mode, implement it with op kernel. - * - * So whether the kernel data type is int, float or other type, - * which has no effect on its execution logic, so directly - * specified a data type here. - * - * Of course, the data type here is also not important. - */ - - phi::KernelKey GetExpectedKernelType( - const framework::ExecutionContext& ctx) const override { - return phi::KernelKey(framework::proto::VarType::FP32, ctx.GetPlace()); - } -}; - -class CinnLaunchOpMaker : public framework::OpProtoAndCheckerMaker { - public: - void Make() override { - AddInput(kX, - "(vector)" - "which are the input of graph inside the CinnLaunchOp" - "excluding kNoNeedBufferX.") - .AsDuplicable(); - AddInput(kNoNeedBufferX, - "(vector)" - "which are the input of graph inside the CinnLaunchOp but" - "their buffer are not needed.") - .AsDuplicable() - .AsDispensable(); - AddOutput(kOutputs, - "(vector)" - "which are the output of graph inside the CinnLaunchOp.") - .AsDuplicable(); - AddAttr( - kCompilationKey, - "(string)" - "a hash key used to get the graph object or its computation result."); - AddComment(R"DOC( -CinnLaunch Operator. - -This operator is used to launch CINN(https://github.com/PaddlePaddle/CINN/blob/develop/README.md) -to compile a graph and execute the compiled object. - -Both input and output of this operator are a set of variables -which are input and output of the graph respectively that will be -compiled and executed in this operator. -In addition, there is an attribute named 'compilation_key' should be -set necessarily to get corresponding ir::Graph object of the graph -or its computation result. - -It accomplishes the computation of graph following several steps: - 1. Fetch ir::Graph object from CinnCompiler using kCompilationKey - 2. Compile the graph to a compiled object, and insert it to the - global cache so that we can directly query it from this cache next time - when shape of input variables are not changed at all. - 3. Create and instantiate all variables used to execute compiled runtime program - if necessary according to the info(type,shape) included in the return scope. - 4. Pack each tensor buffer of all above variables as execution arguments. - 5. Launch execution of the runtime program with above arguments, then - the result would be output by writing value on underlying buffer address. - -)DOC"); - } -}; - -DECLARE_NO_NEED_BUFFER_VARS_INFERER(CinnLaunchOpNoBufVarsInferer, - kNoNeedBufferX); - -} // namespace operators -} // namespace paddle - -namespace ops = paddle::operators; -REGISTER_OPERATOR( - cinn_launch, - ops::CinnLaunchOp, - ops::CinnLaunchOpMaker, - ops::CinnLaunchOpNoBufVarsInferer, - paddle::framework::EmptyGradOpMaker, - paddle::framework::EmptyGradOpMaker); -/* see [Why use single type kernel] */ -PD_REGISTER_STRUCT_KERNEL( - cinn_launch, CPU, ALL_LAYOUT, ops::CinnLaunchOpKernel, float) {} diff --git a/paddle/fluid/operators/cinn/cinn_launch_op.cu.cc b/paddle/fluid/operators/cinn/cinn_launch_op.cu.cc deleted file mode 100644 index a7ff605dca9b9d..00000000000000 --- a/paddle/fluid/operators/cinn/cinn_launch_op.cu.cc +++ /dev/null @@ -1,41 +0,0 @@ -/* Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -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 "paddle/fluid/operators/cinn/cinn_launch_op.h" - -#include "paddle/cinn/runtime/flags.h" -#include "paddle/fluid/framework/operator.h" -#include "paddle/phi/core/generator.h" - -namespace paddle { -namespace operators { - -namespace details { - -template <> -void SetCinnRandomSeed() { - auto seed = phi::DefaultCUDAGenerator(0)->GetCurrentSeed(); - ::cinn::runtime::RandomSeed::GetOrSet(seed); -} - -} // namespace details -} // namespace operators -} // namespace paddle - -/* see [Why use single type kernel] */ -PD_REGISTER_STRUCT_KERNEL(cinn_launch, - GPU, - ALL_LAYOUT, - paddle::operators::CinnLaunchOpKernel, - float) {} diff --git a/paddle/fluid/operators/cinn/cinn_launch_op.h b/paddle/fluid/operators/cinn/cinn_launch_op.h deleted file mode 100644 index 4398c970f33ca2..00000000000000 --- a/paddle/fluid/operators/cinn/cinn_launch_op.h +++ /dev/null @@ -1,185 +0,0 @@ -// Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// 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. - -#pragma once - -#include -#include -#include -#include -#include - -#include "paddle/cinn/common/target.h" -#include "paddle/common/flags.h" -#include "paddle/fluid/framework/data_type.h" -#include "paddle/fluid/framework/new_executor/interpretercore.h" -#include "paddle/fluid/framework/op_registry.h" -#include "paddle/fluid/framework/operator.h" -#include "paddle/fluid/framework/paddle2cinn/cinn_compiler.h" -#include "paddle/fluid/operators/cinn/cinn_launch_context.h" -#include "paddle/fluid/operators/cinn/cinn_op_helper.h" -#include "paddle/fluid/platform/profiler.h" -#include "paddle/pir/include/core/program.h" -#include "paddle/pir/include/core/value.h" - -COMMON_DECLARE_bool(enable_pe_launch_cinn); -COMMON_DECLARE_bool(enable_interpretercore_launch_cinn); -namespace paddle { -namespace operators { - -using CinnCompiler = framework::paddle2cinn::CinnCompiler; -using CinnCompiledObject = framework::paddle2cinn::CinnCompiledObject; - -namespace details { - -// Transform Paddle place to CINN target -const ::cinn::common::Target& PlaceToCinnTarget(const platform::Place& place); - -// Print detailed compilation result of graph for debug -void DebugCinnCompiledResult(const CinnCompiledObject& result); - -// Launch cinn to execute compiled executable program and wait done -void LaunchCinnExecution(const CinnCompiledObject& compiled_obj, - const CinnLaunchContext& context, - void* stream); - -// Set cinn FLAGS (such as FLAGS_cinn_cudnn_deterministic) with paddle's FLAGS. -void SetCinnRuntimeFlags(); - -// set CINN global random seed -template -void SetCinnRandomSeed(); - -// set CINN compile target -void SetCinnTarget(const ::cinn::common::Target& target); - -} // namespace details - -template -class CinnLaunchOpKernel : public framework::OpKernel { - public: - void Compute(const framework::ExecutionContext& ctx) const override { - const auto& scope = ctx.scope(); - const auto& place = ctx.GetPlace(); - void* stream = details::GetStream(ctx); - platform::RecordEvent record_event_1( - "Step 1. Find graph object and prepare input"); - // Step 1. Find graph object and prepare input - PADDLE_ENFORCE_EQ(ctx.HasAttr(kCompilationKey), - true, - phi::errors::NotFound( - "No Attribute(%s) found for CinnLaunchOp operator.", - kCompilationKey)); - const auto& compilation_key = ctx.template Attr(kCompilationKey); - VLOG(4) << "CinnLaunchOp attribute(" << kCompilationKey << ") " - << "value:\n" - << CinnCompiler::GetInstance()->ReadableKey(compilation_key); - - std::map inputs_name2tensor; - std::vector input_x_variable_names; - std::vector input_no_need_buffer_variable_names; - auto add_name2tensor_fn = - [&inputs_name2tensor]( - const std::vector& variable_names, - const std::vector& tensors) { - std::transform( - variable_names.begin(), - variable_names.end(), - tensors.begin(), - std::inserter(inputs_name2tensor, inputs_name2tensor.end()), - [](const std::string& name, const phi::DenseTensor* tensor) { - return std::make_pair(name, tensor); - }); - }; - - auto input_x_tensors = ctx.MultiInput(kX); - if (!input_x_tensors.empty()) { - input_x_variable_names = std::move(ctx.InputNames(kX)); - add_name2tensor_fn(input_x_variable_names, input_x_tensors); - } - auto input_no_need_buffer_tensors = - ctx.MultiInput(kNoNeedBufferX); - if (!input_no_need_buffer_tensors.empty()) { - input_no_need_buffer_variable_names = - std::move(ctx.InputNames(kNoNeedBufferX)); - add_name2tensor_fn(input_no_need_buffer_variable_names, - input_no_need_buffer_tensors); - } - - platform::RecordEvent record_event_2( - "Step 2. Get compilation result of the graph"); - // Step 2. Get compilation result of the graph - auto target = details::PlaceToCinnTarget(place); - details::SetCinnTarget(target); - using ClockType = std::chrono::steady_clock; - std::chrono::time_point start_t, end_t; - if (VLOG_IS_ON(1)) { - VLOG(1) << "Starts to compile at thread " << std::this_thread::get_id(); - start_t = ClockType::now(); - } - const auto& cinn_compiled_object = CinnCompiler::GetInstance()->Compile( - compilation_key, inputs_name2tensor, target, stream); - if (VLOG_IS_ON(1)) { - end_t = ClockType::now(); - auto time_sec = std::chrono::duration_cast( - end_t - start_t); - VLOG(1) << "Ends to compile at thread " << std::this_thread::get_id() - << " , time cost : " << time_sec.count() << " ms"; - - const auto& visible_names = - cinn_compiled_object.launch_context->GetVisibleVarNames(); - VLOG(1) << "These CINN variable can visible by Paddle: " - << string::join_strings(visible_names, ", "); - } - details::DebugCinnCompiledResult(cinn_compiled_object); - auto* launch_context = cinn_compiled_object.launch_context.get(); - - platform::RecordEvent record_event_3("Step 3. Set CINN runtime FLAGS."); - // Step 3. Set CINN runtime FLAGS, such as FLAGS_cinn_cudnn_deterministic. - details::SetCinnRuntimeFlags(); - - // set CINN global random seed - details::SetCinnRandomSeed(); - - // Step 4. Execute the compiled CINN instructions by a PE or - // by the CINN compiled program in sequential order - if (FLAGS_enable_pe_launch_cinn) { - if (FLAGS_enable_interpretercore_launch_cinn) { - platform::RecordEvent record_event_4( - "Step 4. Execute the runtime program by InterpreterCore."); - VLOG(4) << "Execute the runtime program by InterpreterCore"; - auto* interpreter_core = launch_context->InitializeInterpreterCore( - place, const_cast(&scope)); - interpreter_core->Run({}, false); - } else { - platform::RecordEvent record_event_4( - "Step 4. Execute the runtime graph by PE."); - VLOG(4) << "Execute the runtime graph by PE"; - framework::Scope& exec_scope = scope.NewScope(); - auto* pe = launch_context->InitializePE(place, &exec_scope); - pe->RunWithoutFetch(launch_context->GetSkipEagerVars()); - } - } else { - platform::RecordEvent record_event_4( - "Step 4. Execute the compiled executable program."); - VLOG(4) << "Execute the compiled executable program"; - launch_context->UpdateCapturedEnv(scope, place); - LaunchCinnExecution(cinn_compiled_object, *launch_context, stream); - } - VLOG(4) << "CinnLaunchOp launch execution done."; - } -}; - -} // namespace operators -} // namespace paddle diff --git a/test/cpp/fluid/cinn/CMakeLists.txt b/test/cpp/fluid/cinn/CMakeLists.txt index 35d095fc90bda6..a3a7eab8b107b1 100644 --- a/test/cpp/fluid/cinn/CMakeLists.txt +++ b/test/cpp/fluid/cinn/CMakeLists.txt @@ -8,15 +8,4 @@ if(WITH_TESTING) "OMP_NUM_THREADS=1;runtime_include_dir=${CINN_INCLUDE_DIR}/paddle/cinn/runtime/cuda/" ) - paddle_test(cinn_instruction_run_op_test SRCS cinn_instruction_run_op_test.cc) - target_link_libraries(cinn_instruction_run_op_test ${PYTHON_LIBRARIES}) - - get_property( - env - TEST cinn_instruction_run_op_test - PROPERTY ENVIRONMENT) - set_property(TEST cinn_instruction_run_op_test - PROPERTY ENVIRONMENT "${CINN_RUN_ENVIRONMENT}" ${env}) - set_tests_properties(cinn_instruction_run_op_test PROPERTIES LABELS - "RUN_TYPE=CINN") endif() diff --git a/test/cpp/fluid/cinn/cinn_instruction_run_op_test.cc b/test/cpp/fluid/cinn/cinn_instruction_run_op_test.cc deleted file mode 100644 index 36086a0bd1fab3..00000000000000 --- a/test/cpp/fluid/cinn/cinn_instruction_run_op_test.cc +++ /dev/null @@ -1,130 +0,0 @@ -/* Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -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 - -#include - -#include "gtest/gtest.h" -#include "paddle/fluid/framework/op_registry.h" -#include "paddle/fluid/framework/paddle2cinn/cinn_compiler.h" -#include "paddle/fluid/framework/scope.h" -#include "paddle/fluid/platform/cpu_helper.h" -#include "paddle/fluid/platform/enforce.h" -#include "paddle/fluid/platform/init.h" -#include "paddle/phi/core/kernel_registry.h" -#include "test/cpp/fluid/cinn/test_helper.h" - -USE_OP_ITSELF(cinn_launch); -USE_OP_ITSELF(cinn_instruction_run); -USE_OP_ITSELF(elementwise_add); - -PD_DECLARE_KERNEL(cinn_launch, CPU, ALL_LAYOUT); -PD_DECLARE_KERNEL(cinn_instruction_run, CPU, ALL_LAYOUT); -PD_DECLARE_KERNEL(add, CPU, ALL_LAYOUT); -#ifdef PADDLE_WITH_CUDA -PD_DECLARE_KERNEL(cinn_launch, GPU, ALL_LAYOUT); -PD_DECLARE_KERNEL(cinn_instruction_run, GPU, ALL_LAYOUT); -PD_DECLARE_KERNEL(add, KPS, ALL_LAYOUT); -#endif - -namespace paddle::operators { - -using framework::paddle2cinn::CinnCompiler; - -class TestCinnInstructionRunOp : public ::testing::Test { - public: - const char* test_op_out_name = "test_op_out"; - const char* add_op_out_name = "add_op_out"; - std::unique_ptr cinn_launch_op; - std::unique_ptr cinn_instruction_run_op; - std::unique_ptr elementwise_add_op; - - void SetUp() override { - auto compilation_key = CinnCompiler::GetInstance()->AddGraph( - CreateOnlyElementwiseAddGraph("x", "y", test_op_out_name)); - - // create necessary ops - cinn_launch_op = paddle::framework::OpRegistry::CreateOp( - "cinn_launch", - {{"X", {"x", "y"}}}, - {{"Out", {test_op_out_name}}}, - {{"compilation_key", compilation_key}}); - cinn_instruction_run_op = paddle::framework::OpRegistry::CreateOp( - "cinn_instruction_run", - {{"X", {"x", "y"}}}, - {{"Out", {test_op_out_name}}}, - {{"cached_index", 0}, {"instruction_index", 0}}); - elementwise_add_op = - paddle::framework::OpRegistry::CreateOp("elementwise_add", - {{"X", {"x"}}, {"Y", {"y"}}}, - {{"Out", {add_op_out_name}}}, - {{}}); - } - - void Compile(const platform::Place& place) { - // check case: a compiled object not cached before cinn_launch_op run, - // so a cinn_instruction_run_op will throw an error - framework::Scope scope; - InitVariablesWithRandomValue({"x", "y"}, {10, 20}, place, &scope); - scope.Var(test_op_out_name)->GetMutable(); - ASSERT_THROW(cinn_instruction_run_op->Run(scope, place), - paddle::platform::EnforceNotMet); - - // run cinn_launch_op firstly to launch the compilation - // of the above graph and cache two compiled results - // of both type float and int - cinn_launch_op->Run(scope, place); - scope.EraseVars({"x", "y", test_op_out_name}); - scope.Var(test_op_out_name)->GetMutable(); - InitVariablesWithRandomValue({"x", "y"}, {30, 40}, place, &scope); - cinn_launch_op->Run(scope, place); - } - - void RunAndCheck(const platform::Place& place, framework::Scope* scope) { - // Run ops and check the computation results - InitVariablesWithRandomValue({"x", "y"}, {10, 20}, place, scope); - scope->Var(test_op_out_name)->GetMutable(); - scope->Var(add_op_out_name)->GetMutable(); - elementwise_add_op->Run(*scope, place); - cinn_launch_op->Run(*scope, place); - CompareOpResult(scope->GetVar(test_op_out_name), - scope->GetVar(add_op_out_name)); - } - - void TearDown() override { CinnCompiler::GetInstance()->Clear(); } -}; - -TEST_F(TestCinnInstructionRunOp, CPU) { - platform::CPUPlace place; - Compile(place); - framework::Scope scope1; - RunAndCheck(place, &scope1); - // the second run on the same place is to check the cache logic - framework::Scope scope2; - RunAndCheck(place, &scope2); -} - -#ifdef PADDLE_WITH_CUDA -TEST_F(TestCinnInstructionRunOp, GPU) { - platform::CUDAPlace place; - Compile(place); - framework::Scope scope1; - RunAndCheck(place, &scope1); - framework::Scope scope2; - RunAndCheck(place, &scope2); -} -#endif - -} // namespace paddle::operators diff --git a/test/cpp/fluid/cinn/cinn_launch_context_test.cc b/test/cpp/fluid/cinn/cinn_launch_context_test.cc index b11436c15b00d7..f2d9097e75e0e3 100644 --- a/test/cpp/fluid/cinn/cinn_launch_context_test.cc +++ b/test/cpp/fluid/cinn/cinn_launch_context_test.cc @@ -39,12 +39,6 @@ limitations under the License. */ #include "paddle/pir/include/core/program.h" #include "paddle/pir/include/core/value.h" -USE_OP_ITSELF(cinn_instruction_run); -PD_DECLARE_KERNEL(cinn_instruction_run, CPU, ALL_LAYOUT); -#ifdef PADDLE_WITH_CUDA -PD_DECLARE_KERNEL(cinn_instruction_run, GPU, ALL_LAYOUT); -#endif - namespace paddle { namespace operators::details { @@ -225,46 +219,6 @@ TEST_F(CinnLaunchContextTest, TestCheckTensorEquivalent) { paddle::platform::EnforceNotMet); } -TEST_F(CinnLaunchContextTest, TestBuildCompiledProgram) { - platform::CPUPlace place; - framework::Scope scope; - ParallelExecutor* pe = nullptr; - ASSERT_NO_THROW((pe = launch_context->InitializePE(place, &scope))); - - // check details of program build by compiled instructions - const ProgramDesc& program = pe->Graph().OriginProgram(); - ASSERT_EQ(program.Size(), 1); - const auto& block = program.Block(0); - // vars - std::set var_names = block.LocalVarNames(); - ASSERT_EQ(var_names.size(), 5); - for (auto&& var_name : var_names) { - auto* var = block.FindVar(var_name); - ASSERT_NE(var, nullptr); - auto* buffer = launch_context->GetCinnBufferOfVar(var_name); - ASSERT_EQ(framework::DDim(buffer->dims, buffer->dimensions), - common::make_ddim(var->GetShape())); - } - ASSERT_TRUE(block.FindVar("var1")->Persistable()); - ASSERT_FALSE(block.FindVar("var5")->Persistable()); - ASSERT_TRUE(block.FindVar("var5")->IsParameter()); - ASSERT_FALSE(block.FindVar("var1")->IsParameter()); - // ops - ASSERT_EQ(block.OpSize(), 3); - auto* op1 = block.Op(0); - ASSERT_EQ(op1->Type(), "cinn_instruction_run"); - ASSERT_EQ(op1->Input(kX), std::vector({"var1", "var2"})); - ASSERT_EQ(op1->Output(kOutputs), std::vector({"var3"})); - ASSERT_EQ(op1->GetAttrIfExists(kCachedIndex), 110); - ASSERT_EQ(op1->GetAttrIfExists(kInstructionIndex), 0); - auto* op3 = block.Op(2); - ASSERT_EQ(op3->Type(), "cinn_instruction_run"); - ASSERT_EQ(op3->Input(kX), std::vector({"var3", "cinn_var4"})); - ASSERT_EQ(op3->Output(kOutputs), std::vector({"var5"})); - ASSERT_EQ(op3->GetAttrIfExists(kCachedIndex), 110); - ASSERT_EQ(op3->GetAttrIfExists(kInstructionIndex), 2); -} - // DEPRECATED(CtfGo): following test of callback assignment // will be deprecated after we switch to pe TEST_F(CinnLaunchContextTest, TestCallbackAssignment) { diff --git a/test/cpp/fluid/cinn/cinn_launch_op_test.cc b/test/cpp/fluid/cinn/cinn_launch_op_test.cc deleted file mode 100644 index edbfab53f07b51..00000000000000 --- a/test/cpp/fluid/cinn/cinn_launch_op_test.cc +++ /dev/null @@ -1,161 +0,0 @@ -/* Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -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 "paddle/fluid/operators/cinn/cinn_launch_op.h" - -#include - -#include -#include -#include - -#include "gtest/gtest.h" -#include "paddle/common/ddim.h" -#include "paddle/common/flags.h" -#include "paddle/fluid/framework/op_registry.h" -#include "paddle/fluid/framework/paddle2cinn/cinn_compiler.h" -#include "paddle/fluid/framework/scope.h" -#include "paddle/fluid/platform/cpu_helper.h" -#include "paddle/fluid/platform/init.h" -#include "paddle/phi/core/kernel_registry.h" -#include "test/cpp/fluid/cinn/test_helper.h" - -USE_OP_ITSELF(cinn_launch); -USE_OP_ITSELF(cinn_instruction_run); -USE_OP_ITSELF(elementwise_add); -COMMON_DECLARE_double(eager_delete_tensor_gb); -COMMON_DECLARE_bool(enable_pe_launch_cinn); -COMMON_DECLARE_bool(enable_interpretercore_launch_cinn); -COMMON_DECLARE_bool(enable_cinn_auto_tune); - -PD_DECLARE_KERNEL(cinn_launch, CPU, ALL_LAYOUT); -PD_DECLARE_KERNEL(cinn_instruction_run, CPU, ALL_LAYOUT); -PD_DECLARE_KERNEL(add, CPU, ALL_LAYOUT); -#ifdef PADDLE_WITH_CUDA -PD_DECLARE_KERNEL(cinn_launch, GPU, ALL_LAYOUT); -PD_DECLARE_KERNEL(cinn_instruction_run, GPU, ALL_LAYOUT); -PD_DECLARE_KERNEL(add, KPS, ALL_LAYOUT); -#endif - -namespace paddle::operators { - -using framework::paddle2cinn::CinnCompiler; - -class TestCinnLaunchOp : public ::testing::Test { - public: - const char* test_op_out_name = "test_op_out"; - const char* add_op_out_name = "add_op_out"; - std::unique_ptr cinn_launch_op; - std::unique_ptr elementwise_add_op; - - void SetUp() override { - paddle::framework::InitDevices(); - platform::SetNumThreads(1); - // cache test graph into CinnCompiler - auto compilation_key = CinnCompiler::GetInstance()->AddGraph( - CreateOnlyElementwiseAddGraph("x", "y", test_op_out_name)); - - // create cinn_launch_op and elementwise_add op - cinn_launch_op = paddle::framework::OpRegistry::CreateOp( - "cinn_launch", - {{"X", {"x", "y"}}}, - {{"Out", {test_op_out_name}}}, - {{"compilation_key", compilation_key}}); - elementwise_add_op = - paddle::framework::OpRegistry::CreateOp("elementwise_add", - {{"X", {"x"}}, {"Y", {"y"}}}, - {{"Out", {add_op_out_name}}}, - {{}}); - } - - void RunAndCheck(const platform::Place& place, framework::Scope* scope) { - // Run ops and check the computation results - InitVariablesWithRandomValue({"x", "y"}, {10, 20}, place, scope); - scope->Var(test_op_out_name)->GetMutable(); - scope->Var(add_op_out_name)->GetMutable(); - elementwise_add_op->Run(*scope, place); - cinn_launch_op->Run(*scope, place); - CompareOpResult(scope->GetVar(test_op_out_name), - scope->GetVar(add_op_out_name)); - } - - void TearDown() override { CinnCompiler::GetInstance()->Clear(); } -}; - -TEST_F(TestCinnLaunchOp, TestRunCPUInstructionByPE) { - framework::Scope scope1; - RunAndCheck(platform::CPUPlace(), &scope1); - // the second run on the same place is to check the cache logic - framework::Scope scope2; - RunAndCheck(platform::CPUPlace(), &scope2); -} - -#ifdef PADDLE_WITH_CUDA -TEST_F(TestCinnLaunchOp, TestRunGPUInstructionByPE) { - framework::Scope scope1; - RunAndCheck(platform::CUDAPlace(), &scope1); - framework::Scope scope2; - RunAndCheck(platform::CUDAPlace(), &scope2); -} -#endif - -TEST_F(TestCinnLaunchOp, TestRunCPUInstructionByCinnProgram) { - // set FLAGS_enable_pe_launch_cinn=false to switch to use - // default scheduler of CINN to execute the compiled program - FLAGS_enable_pe_launch_cinn = false; - FLAGS_enable_interpretercore_launch_cinn = false; - framework::Scope scope1; - RunAndCheck(platform::CPUPlace(), &scope1); - framework::Scope scope2; - RunAndCheck(platform::CPUPlace(), &scope2); -} - -#ifdef PADDLE_WITH_CUDA -TEST_F(TestCinnLaunchOp, TestRunGPUInstructionByCinnProgram) { - // set FLAGS_enable_pe_launch_cinn=false to switch to use - // default scheduler of CINN to execute the compiled program - FLAGS_enable_pe_launch_cinn = false; - FLAGS_enable_interpretercore_launch_cinn = false; - framework::Scope scope1; - RunAndCheck(platform::CUDAPlace(), &scope1); - framework::Scope scope2; - RunAndCheck(platform::CUDAPlace(), &scope2); -} -#endif - -TEST_F(TestCinnLaunchOp, TestRunWithAutoTuneEnabled) { - FLAGS_enable_cinn_auto_tune = true; - - // currently only check on cpu, will add a test for gpu after CINN ready - framework::Scope scope1; - RunAndCheck(platform::CPUPlace(), &scope1); - framework::Scope scope2; - RunAndCheck(platform::CPUPlace(), &scope2); -} - -namespace details { -// Testing helper function used on CinnLaunchOpKernel in the following: -// firstly build test data, then check both expected and illegal situations - -TEST(CinnLaunchOpHelperTest, TestPlaceToCinnTarget) { - ASSERT_EQ(PlaceToCinnTarget(platform::CPUPlace()), - ::cinn::common::DefaultHostTarget()); - ASSERT_EQ(PlaceToCinnTarget(platform::CUDAPlace(0)), - ::cinn::common::DefaultNVGPUTarget()); - ASSERT_THROW(PlaceToCinnTarget(platform::XPUPlace()), - paddle::platform::EnforceNotMet); -} - -} // namespace details -} // namespace paddle::operators diff --git a/test/cpp/fluid/framework/ir/memory_optimize_pass/CMakeLists.txt b/test/cpp/fluid/framework/ir/memory_optimize_pass/CMakeLists.txt index 841ebd7c0fcc0c..b6e2d8532325c0 100644 --- a/test/cpp/fluid/framework/ir/memory_optimize_pass/CMakeLists.txt +++ b/test/cpp/fluid/framework/ir/memory_optimize_pass/CMakeLists.txt @@ -1,9 +1,3 @@ -if(WITH_CINN) - paddle_test(share_varinfo_into_cinn_pass_test SRCS - share_varinfo_into_cinn_pass_test.cc) - list(APPEND EAGER_DELETETION_PASS_DEPS share_varinfo_into_cinn_pass) -endif() - paddle_test(test_reference_count_pass_last_lived_ops SRCS test_reference_count_pass_last_lived_ops.cc DEPS common) diff --git a/test/cpp/fluid/framework/ir/memory_optimize_pass/share_varinfo_into_cinn_pass_test.cc b/test/cpp/fluid/framework/ir/memory_optimize_pass/share_varinfo_into_cinn_pass_test.cc deleted file mode 100644 index 1f78e293a21a39..00000000000000 --- a/test/cpp/fluid/framework/ir/memory_optimize_pass/share_varinfo_into_cinn_pass_test.cc +++ /dev/null @@ -1,154 +0,0 @@ -// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// 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 - -#include "gtest/gtest.h" -#include "paddle/fluid/framework/details/computation_op_handle.h" -#include "paddle/fluid/framework/details/eager_deletion_op_handle.h" -#include "paddle/fluid/framework/ir/graph.h" -#include "paddle/fluid/framework/ir/memory_optimize_pass/memory_optimization_var_info.h" -#include "paddle/fluid/framework/ir/pass.h" -#include "paddle/fluid/framework/paddle2cinn/build_cinn_pass.h" -#include "paddle/fluid/framework/paddle2cinn/cinn_compiler.h" -#include "paddle/fluid/framework/parallel_executor.h" -#include "paddle/fluid/framework/program_desc.h" - -USE_OP_ITSELF(mul); -USE_OP_ITSELF(elementwise_add); - -USE_OP_ITSELF(cinn_launch); -PD_DECLARE_KERNEL(cinn_launch, CPU, ALL_LAYOUT); -#ifdef PADDLE_WITH_CUDA -PD_DECLARE_KERNEL(cinn_launch, GPU, ALL_LAYOUT); -#endif - -namespace paddle::framework { - -using Name2VarInfoMap = - std::unordered_map>; - -static ProgramDesc BuildProgramInsideCinnLaunchOp() { - ProgramDesc program; - auto* block = program.MutableBlock(0); - block->Var("var1"); - block->Var("var2"); - block->Var("var3"); - block->Var("var4"); - block->Var("var5"); - - auto add_op = - std::unique_ptr(new OpDesc("elementwise_add", - {{"X", {"var1"}}, {"Y", {"var2"}}}, - {{"Out", {"var3"}}}, - {})); - block->AppendAllocatedOp(std::move(add_op)); - auto mul_op = std::unique_ptr(new OpDesc( - "mul", {{"X", {"var3"}}, {"Y", {"var4"}}}, {{"Out", {"var5"}}}, {})); - block->AppendAllocatedOp(std::move(mul_op)); - return program; -} - -static ProgramDesc BuildProgramWithCinnLaunchOp(int64_t compilation_key) { - // create a cinn_launch op - ProgramDesc program; - auto* block = program.MutableBlock(0); - block->Var("var1"); - block->Var("var2"); - block->Var("var4"); - block->Var("var5"); - - auto cinn_launch_op = std::unique_ptr( - new OpDesc("cinn_launch", - {{"X", {"var1", "var2", "var4"}}}, - {{"Out", {"var5"}}}, - {{"compilation_key", compilation_key}})); - block->AppendAllocatedOp(std::move(cinn_launch_op)); - return program; -} - -struct TestPassContext { - explicit TestPassContext(const ProgramDesc& program) { - graph = std::make_unique(program); - details::BuildStrategy build_strategy; - details::ExecutionStrategy exec_strategy; - exec_strategy.use_device_ = paddle::platform::kCUDA; - executor.reset(new ParallelExecutor(platform::CUDAPlace(0), - &scope, - exec_strategy, - build_strategy, - graph.get())); - } - - Scope scope; - std::unique_ptr graph; - std::unique_ptr executor; -}; - -TEST(ShareMemInfoToSubGraphPassTest, test_main_graph_share_varinfo) { - // add a subgraph to CinnCompiler - auto subgraph = std::make_unique(BuildProgramInsideCinnLaunchOp()); - subgraph->GetOrInit( - paddle2cinn::kMemOptVarInfoFromMainGraph); - auto compilation_key = - paddle2cinn::CinnCompiler::GetInstance()->AddGraph(std::move(subgraph)); - - // build test data and apply pass - auto context = std::make_unique( - BuildProgramWithCinnLaunchOp(compilation_key)); - - // check result - const ir::Graph& result_subgraph = - paddle2cinn::CinnCompiler::GetInstance()->FindGraph(compilation_key); - const auto& dst_varinfo_map = result_subgraph.Get( - paddle2cinn::kMemOptVarInfoFromMainGraph); - ASSERT_EQ(dst_varinfo_map.size(), 4); - EXPECT_EQ(dst_varinfo_map.count("var1"), 1); - EXPECT_EQ(dst_varinfo_map.count("var5"), 1); - EXPECT_EQ(dst_varinfo_map.at("var1").use_count(), 2); - EXPECT_EQ(dst_varinfo_map.at("var5").use_count(), 2); -} - -TEST(ShareMemInfoToSubGraphPassTest, test_subgraph_take_varinfo) { - // build test data and apply pass - auto context = - std::make_unique(BuildProgramInsideCinnLaunchOp()); - auto& varinfo_map_shared = context->graph->GetOrInit( - paddle2cinn::kMemOptVarInfoFromMainGraph); - varinfo_map_shared = { - {"var1", std::make_shared("var1", 1)}, - {"var2", std::make_shared("var2", 2)}, - }; - - ir::MemOptVarInfoMapList varinfo_maps(1); - auto& dst_varinfo_map = varinfo_maps.front(); - dst_varinfo_map = {{"var1", std::make_shared("var1", 1)}, - {"var2", std::make_shared("var2", 1)}, - {"var3", std::make_shared("var3", 1)}, - {"var4", std::make_shared("var4", 1)}, - {"var5", std::make_shared("var5", 1)}}; - auto share_pass = - ir::PassRegistry::Instance().Get("share_varinfo_into_cinn_pass"); - share_pass->SetNotOwned(ir::kMemOptVarInfoMapList, &varinfo_maps); - share_pass->Apply(context->graph.get()); - - // check result - ASSERT_NE(dst_varinfo_map.at("var1")->ParentHolder(), nullptr); - ASSERT_NE(dst_varinfo_map.at("var2")->ParentHolder(), nullptr); - ASSERT_EQ(dst_varinfo_map.at("var3")->ParentHolder(), nullptr); - ASSERT_EQ(dst_varinfo_map.at("var4")->ParentHolder(), nullptr); - ASSERT_EQ(dst_varinfo_map.at("var5")->ParentHolder(), nullptr); -} - -} // namespace paddle::framework diff --git a/test/cpp/fluid/framework/paddle2cinn/build_cinn_pass_test.cc b/test/cpp/fluid/framework/paddle2cinn/build_cinn_pass_test.cc index 2935c0b5876cd9..d3cb9f7942ecaa 100644 --- a/test/cpp/fluid/framework/paddle2cinn/build_cinn_pass_test.cc +++ b/test/cpp/fluid/framework/paddle2cinn/build_cinn_pass_test.cc @@ -28,7 +28,7 @@ limitations under the License. */ #include "paddle/fluid/framework/paddle2cinn/cinn_compiler.h" #include "paddle/fluid/framework/program_desc.h" #include "paddle/fluid/framework/var_desc.h" -#include "paddle/fluid/operators/cinn/cinn_launch_op.h" +#include "paddle/fluid/operators/cinn/cinn_op_helper.h" namespace paddle { namespace framework { diff --git a/test/cpp/fluid/framework/paddle2cinn/cinn_compiler_test.cc b/test/cpp/fluid/framework/paddle2cinn/cinn_compiler_test.cc index ee34c2e59ce1f7..b3d974a7001545 100644 --- a/test/cpp/fluid/framework/paddle2cinn/cinn_compiler_test.cc +++ b/test/cpp/fluid/framework/paddle2cinn/cinn_compiler_test.cc @@ -34,7 +34,7 @@ #include "paddle/fluid/framework/paddle2cinn/build_cinn_pass.h" #include "paddle/fluid/framework/program_desc.h" #include "paddle/fluid/framework/scope.h" -#include "paddle/fluid/operators/cinn/cinn_launch_op.h" +#include "paddle/fluid/operators/cinn/cinn_op_helper.h" #include "paddle/fluid/platform/enforce.h" #include "paddle/fluid/platform/place.h" diff --git a/test/distributed_passes/test_build_cinn_pass_simple_net.py b/test/distributed_passes/test_build_cinn_pass_simple_net.py deleted file mode 100644 index 6a1fb5552982e1..00000000000000 --- a/test/distributed_passes/test_build_cinn_pass_simple_net.py +++ /dev/null @@ -1,46 +0,0 @@ -# Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# 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. - -import unittest - -from dist_pass_test_base import DistPassTestBase -from model_zoo import simple_net - -import paddle -from paddle.distributed.passes import PassManager, new_pass - - -class TestBuildCINNPass(DistPassTestBase): - def init(self): - self.atol = 0.0 - self.rtol = 0.0 - - def apply_passes(self, main_prog, startup_prog): - pass_manager = PassManager( - [ - new_pass("build_cinn"), - new_pass("fuse_elewise_add_act"), - ] - ) - pass_manager.apply([main_prog], [startup_prog]) - op_types = [op.type for op in main_prog.global_block().ops] - self.assertTrue('cinn_launch' in op_types) - - def test_bs_32(self): - if paddle.is_compiled_with_cinn(): - self.check_main(simple_net, batch_size=32) - - -if __name__ == "__main__": - unittest.main()