cinn_launch_op_test.cc 3.3 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
#include <stdlib.h>
17
#include <mutex>
18 19 20 21 22 23
#include <random>
#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"
24
#include "paddle/fluid/operators/cinn/test_helper.h"
25 26 27 28
#include "paddle/fluid/platform/cpu_helper.h"
#include "paddle/fluid/platform/init.h"

USE_OP(cinn_launch);
29
USE_OP_ITSELF(elementwise_add);
30

31
namespace paddle::operators {
32

33
using framework::paddle2cinn::CinnCompiler;
34

35
TEST(CinnLaunchOpTest, TestWithElementwiseAdd) {
36 37 38
  paddle::framework::InitDevices();
  platform::SetNumThreads(1);
  // cache test graph into CinnCompiler
39 40
  const std::string& test_op_out_name = "cinn_launch_op_out";
  const std::string& add_op_out_name = "add_op_out";
41
  auto compilation_key = CinnCompiler::GetInstance()->AddGraph(
42 43
      CreateOnlyElementwiseAddGraph("x", "y", test_op_out_name));

44 45
  // create cinn_launch_op and elementwise_add op
  auto cinn_launch_op = paddle::framework::OpRegistry::CreateOp(
46
      "cinn_launch", {{"X", {"x", "y"}}}, {{"Out", {test_op_out_name}}},
47 48
      {{"compilation_key", compilation_key}});
  auto elementwise_add_op = paddle::framework::OpRegistry::CreateOp(
49 50 51
      "elementwise_add", {{"X", {"x"}}, {"Y", {"y"}}},
      {{"Out", {add_op_out_name}}}, {{}});

52 53 54
  // Run ops and check the computation results
  auto run_and_check_fn = [&](const platform::Place& place) {
    framework::Scope scope;
55 56 57 58 59 60 61
    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_launch_op->Run(scope, place);
    elementwise_add_op->Run(scope, place);
    CompareOpResult<float>(scope.GetVar(test_op_out_name),
                           scope.GetVar(add_op_out_name));
62 63
  };

64
  // CPU
65 66
  run_and_check_fn(platform::CPUPlace());
  run_and_check_fn(platform::CPUPlace());
67
#ifdef PADDLE_WITH_CUDA
68
  // GPU
69 70
  run_and_check_fn(platform::CUDAPlace());
  run_and_check_fn(platform::CUDAPlace());
71
#endif
72 73
}

74 75 76 77 78 79 80 81 82 83 84 85 86 87
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
88
}  // namespace paddle::operators