data_device_transform_test.cu 5.8 KB
Newer Older
1
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

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 "gtest/gtest.h"

Y
Yi Wang 已提交
17 18 19
#include "paddle/fluid/framework/lod_tensor.h"
#include "paddle/fluid/framework/op_info.h"
#include "paddle/fluid/framework/op_registry.h"
S
sneaxiy 已提交
20
#include "paddle/fluid/framework/scope.h"
W
Wu Yi 已提交
21
#include "paddle/fluid/operators/elementwise/elementwise_op_function.h"
Y
Yi Wang 已提交
22 23
#include "paddle/fluid/operators/math/math_function.h"
#include "paddle/fluid/platform/device_context.h"
24
#include "paddle/fluid/platform/init.h"
25

26 27 28
#include "paddle/fluid/framework/pten_utils.h"
#include "paddle/pten/include/core.h"

29 30 31 32 33 34 35 36 37 38
namespace paddle {
namespace framework {

template <typename T>
struct AddFunctor {
  inline HOSTDEVICE T operator()(T a, T b) const { return a + b; }
};

class OpKernelTestProtoAndCheckerMaker : public OpProtoAndCheckerMaker {
 public:
Y
Yu Yang 已提交
39
  void Make() {
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
    AddInput("input", "input1 of test op");
    AddOutput("output", "output of test op");
    AddAttr<bool>("use_gpu", "force to use gpu kernel").SetDefault(false);
    AddComment("This is test op");
  }
};

class TestOpWithKernel : public OperatorWithKernel {
 public:
  using OperatorWithKernel::OperatorWithKernel;

 protected:
  void InferShape(framework::InferShapeContext* ctx) const override {}
  OpKernelType GetExpectedKernelType(
      const ExecutionContext& ctx) const override {
    if (Attr<bool>("use_gpu")) {
M
minqiyang 已提交
56
      VLOG(3) << "force use gpu kernel";
57
      return OpKernelType(proto::VarType::FP32, platform::CUDAPlace(0));
58
    } else {
M
minqiyang 已提交
59
      VLOG(3) << "use default kernel";
60
      return OpKernelType(proto::VarType::FP32,
61 62 63 64 65 66 67 68 69
                          ctx.Input<Tensor>("input")->place());
    }
  }
};

template <typename DeviceContext, typename T>
class TestKernel : public OpKernel<float> {
 public:
  void Compute(const ExecutionContext& ctx) const {
H
hong 已提交
70
    std::cout << ctx.DebugString() << std::endl;
71 72 73 74 75 76 77 78

    const Tensor* input = ctx.Input<Tensor>("input");

    std::cout << "input place:" << input->place() << std::endl;
    auto* output = ctx.Output<framework::LoDTensor>("output");
    output->Resize(input->dims());
    output->mutable_data<T>(ctx.GetPlace());

79 80 81 82 83 84
    auto pt_input = paddle::experimental::MakePtenDenseTensor(*input);
    auto pt_out = paddle::experimental::MakePtenDenseTensor(*output);

    pten::funcs::TransformFunctor<AddFunctor<T>, T, DeviceContext> functor(
        *pt_input, *pt_input, pt_out.get(),
        ctx.template device_context<DeviceContext>(), AddFunctor<T>());
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
    functor.Run();
  }
};

}  // namespace framework
}  // namespace paddle

REGISTER_OP_WITHOUT_GRADIENT(
    test_op, paddle::framework::TestOpWithKernel,
    paddle::framework::OpKernelTestProtoAndCheckerMaker);
REGISTER_OP_CPU_KERNEL(
    test_op,
    paddle::framework::TestKernel<paddle::platform::CPUDeviceContext, float>);
REGISTER_OP_CUDA_KERNEL(
    test_op,
    paddle::framework::TestKernel<paddle::platform::CUDADeviceContext, float>);

static void BuildVar(const std::string& param_name,
                     std::initializer_list<const char*> arguments,
                     paddle::framework::proto::OpDesc::Var* var) {
  var->set_parameter(param_name);
  for (auto& arg_name : arguments) {
    *var->mutable_arguments()->Add() = arg_name;
  }
}

TEST(Operator, CPUtoGPU) {
112
  paddle::framework::InitDevices();
113 114 115 116 117 118 119 120 121 122 123 124

  paddle::framework::Scope scope;
  paddle::platform::CPUPlace cpu_place;

  // create an op to run on CPU
  paddle::framework::proto::OpDesc cpu_op_desc;
  cpu_op_desc.set_type("test_op");
  BuildVar("input", {"IN1"}, cpu_op_desc.add_inputs());
  BuildVar("output", {"OUT1"}, cpu_op_desc.add_outputs());

  auto cpu_op = paddle::framework::OpRegistry::CreateOp(cpu_op_desc);
  // prepare input
125 126 127
  auto* in_t = scope.Var("IN1")->GetMutable<paddle::framework::LoDTensor>();
  auto* src_ptr =
      in_t->mutable_data<float>({2, 3}, paddle::platform::CPUPlace());
128 129 130 131 132 133 134 135
  for (int i = 0; i < 2 * 3; ++i) {
    src_ptr[i] = static_cast<float>(i);
  }

  // get output
  auto* output = scope.Var("OUT1");
  cpu_op->Run(scope, cpu_place);

136
  auto* output_ptr = output->Get<paddle::framework::LoDTensor>().data<float>();
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
  for (int i = 0; i < 2 * 3; ++i) {
    ASSERT_EQ(output_ptr[i], static_cast<float>(i) * 2);
  }

  // create an op to run on GPU
  paddle::framework::proto::OpDesc gpu_op_desc;
  gpu_op_desc.set_type("test_op");
  BuildVar("input", {"OUT1"}, gpu_op_desc.add_inputs());
  BuildVar("output", {"OUT2"}, gpu_op_desc.add_outputs());

  auto attr = gpu_op_desc.mutable_attrs()->Add();
  attr->set_name("use_gpu");
  attr->set_type(paddle::framework::proto::AttrType::BOOLEAN);
  attr->set_b(true);

  auto gpu_op = paddle::framework::OpRegistry::CreateOp(gpu_op_desc);

  paddle::platform::CUDAPlace cuda_place(0);
  // get output
  auto* output2 = scope.Var("OUT2");
  gpu_op->Run(scope, cuda_place);
M
minqiyang 已提交
158
  VLOG(3) << "after gpu_op run";
159 160

  // auto* output2_ptr = output2->Get<LoDTensor>().data<float>();
161 162
  paddle::platform::DeviceContextPool& pool =
      paddle::platform::DeviceContextPool::Instance();
163 164 165
  auto dev_ctx = pool.Get(cuda_place);

  paddle::framework::Tensor output_tensor;
166 167 168
  paddle::framework::TensorCopy(output2->Get<paddle::framework::LoDTensor>(),
                                paddle::platform::CPUPlace(), *dev_ctx,
                                &output_tensor);
169 170 171 172 173 174 175

  dev_ctx->Wait();
  float* output2_ptr = output_tensor.data<float>();
  for (int i = 0; i < 2 * 3; ++i) {
    ASSERT_EQ(output2_ptr[i], static_cast<float>(i) * 4);
  }
}