cinn_launch_op_test.cc 5.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/* 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. */

15
#include "paddle/fluid/operators/cinn/cinn_launch_op.h"
16

17
#include <stdlib.h>
18

19
#include <mutex>
20 21
#include <random>
#include <string>
22

23 24 25 26 27 28
#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/init.h"
29
#include "paddle/phi/core/ddim.h"
30
#include "paddle/phi/core/flags.h"
31
#include "paddle/phi/core/kernel_registry.h"
32
#include "test/cpp/fluid/cinn/test_helper.h"
33

34 35
USE_OP_ITSELF(cinn_launch);
USE_OP_ITSELF(cinn_instruction_run);
36
USE_OP_ITSELF(elementwise_add);
37 38 39 40
PHI_DECLARE_double(eager_delete_tensor_gb);
PHI_DECLARE_bool(enable_pe_launch_cinn);
PHI_DECLARE_bool(enable_interpretercore_launch_cinn);
PHI_DECLARE_bool(enable_cinn_auto_tune);
41

42 43
PD_DECLARE_KERNEL(cinn_launch, CPU, ALL_LAYOUT);
PD_DECLARE_KERNEL(cinn_instruction_run, CPU, ALL_LAYOUT);
44 45
PD_DECLARE_KERNEL(add, CPU, ALL_LAYOUT);
#ifdef PADDLE_WITH_CUDA
46 47
PD_DECLARE_KERNEL(cinn_launch, GPU, ALL_LAYOUT);
PD_DECLARE_KERNEL(cinn_instruction_run, GPU, ALL_LAYOUT);
48
PD_DECLARE_KERNEL(add, KPS, ALL_LAYOUT);
49 50
#endif

51
namespace paddle::operators {
52

53
using framework::paddle2cinn::CinnCompiler;
54

55 56
class TestCinnLaunchOp : public ::testing::Test {
 public:
57
  const char* test_op_out_name = "test_op_out";
58 59 60 61 62 63 64 65 66 67 68 69 70
  const char* add_op_out_name = "add_op_out";
  std::unique_ptr<framework::OperatorBase> cinn_launch_op;
  std::unique_ptr<framework::OperatorBase> 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(
71 72 73
        "cinn_launch",
        {{"X", {"x", "y"}}},
        {{"Out", {test_op_out_name}}},
74
        {{"compilation_key", compilation_key}});
75 76 77 78 79
    elementwise_add_op =
        paddle::framework::OpRegistry::CreateOp("elementwise_add",
                                                {{"X", {"x"}}, {"Y", {"y"}}},
                                                {{"Out", {add_op_out_name}}},
                                                {{}});
80 81
  }

82
  void RunAndCheck(const platform::Place& place, framework::Scope* scope) {
83
    // Run ops and check the computation results
84 85 86 87 88 89 90
    InitVariablesWithRandomValue<float>({"x", "y"}, {10, 20}, place, scope);
    scope->Var(test_op_out_name)->GetMutable<phi::DenseTensor>();
    scope->Var(add_op_out_name)->GetMutable<phi::DenseTensor>();
    elementwise_add_op->Run(*scope, place);
    cinn_launch_op->Run(*scope, place);
    CompareOpResult<float>(scope->GetVar(test_op_out_name),
                           scope->GetVar(add_op_out_name));
91 92 93 94
  }

  void TearDown() override { CinnCompiler::GetInstance()->Clear(); }
};
95

96
TEST_F(TestCinnLaunchOp, TestRunCPUInstructionByPE) {
97 98
  framework::Scope scope1;
  RunAndCheck(platform::CPUPlace(), &scope1);
99
  // the second run on the same place is to check the cache logic
100 101
  framework::Scope scope2;
  RunAndCheck(platform::CPUPlace(), &scope2);
102 103
}

104
#ifdef PADDLE_WITH_CUDA
105
TEST_F(TestCinnLaunchOp, TestRunGPUInstructionByPE) {
106 107 108 109
  framework::Scope scope1;
  RunAndCheck(platform::CUDAPlace(), &scope1);
  framework::Scope scope2;
  RunAndCheck(platform::CUDAPlace(), &scope2);
110
}
111
#endif
112

113
TEST_F(TestCinnLaunchOp, TestRunCPUInstructionByCinnProgram) {
114 115 116
  // 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;
117 118 119 120 121
  FLAGS_enable_interpretercore_launch_cinn = false;
  framework::Scope scope1;
  RunAndCheck(platform::CPUPlace(), &scope1);
  framework::Scope scope2;
  RunAndCheck(platform::CPUPlace(), &scope2);
122 123
}

124
#ifdef PADDLE_WITH_CUDA
125 126 127 128
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;
129 130 131 132 133
  FLAGS_enable_interpretercore_launch_cinn = false;
  framework::Scope scope1;
  RunAndCheck(platform::CUDAPlace(), &scope1);
  framework::Scope scope2;
  RunAndCheck(platform::CUDAPlace(), &scope2);
134
}
135
#endif
136

137 138 139 140
TEST_F(TestCinnLaunchOp, TestRunWithAutoTuneEnabled) {
  FLAGS_enable_cinn_auto_tune = true;

  // currently only check on cpu, will add a test for gpu after CINN ready
141 142 143 144
  framework::Scope scope1;
  RunAndCheck(platform::CPUPlace(), &scope1);
  framework::Scope scope2;
  RunAndCheck(platform::CPUPlace(), &scope2);
145 146
}

147 148 149 150 151 152 153 154 155 156 157 158 159 160
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
161
}  // namespace paddle::operators