Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cinn_instruction_run_op test #39576

Merged
merged 21 commits into from
Feb 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
f7e9811
add cinn_instruction_run_op for launching execution of a cinn instruc…
CtfGo Feb 9, 2022
81c7ebe
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
CtfGo Feb 9, 2022
cc4e106
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
CtfGo Feb 10, 2022
a645c3e
fix multi definition compilation error
CtfGo Feb 10, 2022
f64f8c2
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
CtfGo Feb 10, 2022
b6fff8d
update cmake
CtfGo Feb 10, 2022
3220d21
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
CtfGo Feb 10, 2022
79c0bb7
fix bug at infershape
CtfGo Feb 11, 2022
370cfb0
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
CtfGo Feb 11, 2022
3937d3c
fix compile error due to lacking header file
CtfGo Feb 11, 2022
2462113
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
CtfGo Feb 11, 2022
238fed4
add cinn_instruction_run_op test code
CtfGo Feb 11, 2022
f84c576
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
CtfGo Feb 15, 2022
2fea564
update several interfaces of CinnLaunchContext
CtfGo Feb 15, 2022
88fcc60
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
CtfGo Feb 16, 2022
f5c99a2
update several interfaces and add detail comments in CinnLaunchContex…
CtfGo Feb 16, 2022
fe0556e
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
CtfGo Feb 16, 2022
5a2f5f4
to skip the bug of error message check
CtfGo Feb 16, 2022
af3f3fe
to skip the bug of error message check
CtfGo Feb 16, 2022
40e7577
fix ut test failed due to reliant interface updated
CtfGo Feb 16, 2022
1c5aecd
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
CtfGo Feb 16, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion paddle/fluid/operators/cinn/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ if (WITH_TESTING)
cc_test(cinn_launch_context_test SRCS cinn_launch_context_test.cc DEPS ddim lod_tensor scope cinn_launch_context)
set_tests_properties(cinn_launch_context_test PROPERTIES LABELS "RUN_TYPE=CINN")

SET(CINN_RUN_ENVIRONMENT "OMP_NUM_THREADS=1;runtime_include_dir=${PADDLE_BINARY_DIR}/third_party/CINN/src/external_cinn/cinn/runtime/cuda")
cc_test(cinn_launch_op_test SRCS cinn_launch_op_test.cc DEPS cinn_compiler cinn_launch_op elementwise_add_op)
set_tests_properties(cinn_launch_op_test PROPERTIES LABELS "RUN_TYPE=CINN" ENVIRONMENT "OMP_NUM_THREADS=1;runtime_include_dir=${PADDLE_BINARY_DIR}/third_party/CINN/src/external_cinn/cinn/runtime/cuda")
set_tests_properties(cinn_launch_op_test PROPERTIES LABELS "RUN_TYPE=CINN" ENVIRONMENT "${CINN_RUN_ENVIRONMENT}")

cc_test(cinn_instruction_run_op_test SRCS cinn_instruction_run_op_test.cc DEPS cinn_compiler cinn_launch_op cinn_instruction_run_op elementwise_add_op)
set_tests_properties(cinn_instruction_run_op_test PROPERTIES LABELS "RUN_TYPE=CINN" ENVIRONMENT "${CINN_RUN_ENVIRONMENT}")
endif()
90 changes: 90 additions & 0 deletions paddle/fluid/operators/cinn/cinn_instruction_run_op_test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/* 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 <stdlib.h>
#include <string>
#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/operators/cinn/test_helper.h"
#include "paddle/fluid/platform/cpu_helper.h"
#include "paddle/fluid/platform/enforce.h"
#include "paddle/fluid/platform/init.h"

USE_OP(cinn_launch);
USE_OP(cinn_instruction_run);
USE_OP_ITSELF(elementwise_add);

namespace paddle::operators {

using framework::paddle2cinn::CinnCompiler;

TEST(CinnInstructionOpTest, TestWithElementwiseAdd) {
paddle::framework::InitDevices();
platform::SetNumThreads(1);
// cache test graph into CinnCompiler
const std::string& test_op_out_name = "cinn_instruction_run_op_out";
const std::string& add_op_out_name = "add_op_out";
auto compilation_key = CinnCompiler::GetInstance()->AddGraph(
CreateOnlyElementwiseAddGraph("x", "y", test_op_out_name));

// create a cinn_launch_op and run firstly to launch the compilation
// of the above graph and cache the compiled object in CinnCompiler
auto cinn_launch_op = paddle::framework::OpRegistry::CreateOp(
"cinn_launch", {{"X", {"x", "y"}}}, {{"Out", {test_op_out_name}}},
{{"compilation_key", compilation_key}});

// create cinn_instruction_run_op and elementwise_add op
auto cinn_instruction_run_op = paddle::framework::OpRegistry::CreateOp(
"cinn_instruction_run", {{"X", {"x", "y"}}},
{{"Out", {test_op_out_name}}},
{{"cached_index", 0}, {"instruction_index", 1}});
auto elementwise_add_op = paddle::framework::OpRegistry::CreateOp(
"elementwise_add", {{"X", {"x"}}, {"Y", {"y"}}},
{{"Out", {add_op_out_name}}}, {{}});

// 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;
platform::CPUPlace place;
InitVariablesWithRandomValue<float>({"x", "y"}, {10, 20}, place, &scope);
scope.Var(test_op_out_name)->GetMutable<LoDTensor>();
ASSERT_THROW(cinn_instruction_run_op->Run(scope, place),
paddle::platform::EnforceNotMet);
cinn_launch_op->Run(scope, place);

// Run ops and check the computation results
auto run_and_check_fn = [&](const platform::Place& place) {
framework::Scope scope;
InitVariablesWithRandomValue<float>({"x", "y"}, {10, 20}, place, &scope);
scope.Var(test_op_out_name)->GetMutable<LoDTensor>();
scope.Var(add_op_out_name)->GetMutable<LoDTensor>();
cinn_instruction_run_op->Run(scope, place);
elementwise_add_op->Run(scope, place);
CompareOpResult<float>(scope.GetVar(test_op_out_name),
scope.GetVar(add_op_out_name));
};

// CPU
run_and_check_fn(platform::CPUPlace());
run_and_check_fn(platform::CPUPlace());
#ifdef PADDLE_WITH_CUDA
// GPU
run_and_check_fn(platform::CUDAPlace());
run_and_check_fn(platform::CUDAPlace());
#endif
}

} // namespace paddle::operators
Loading