test_activation_op.cc 3.2 KB
Newer Older
L
Luo Tao 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/* Copyright (c) 2018 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 <gtest/gtest.h>
#include "paddle/fluid/framework/lod_tensor.h"
#include "paddle/fluid/framework/op_registry.h"
#include "paddle/fluid/framework/program_desc.h"
19
#include "paddle/fluid/inference/tensorrt/convert/io_converter.h"
L
Luo Tao 已提交
20 21 22 23 24 25 26 27 28 29
#include "paddle/fluid/inference/tensorrt/convert/op_converter.h"
#include "paddle/fluid/platform/device_context.h"
#include "paddle/fluid/platform/place.h"

USE_OP(relu);

namespace paddle {
namespace inference {
namespace tensorrt {

30
void Compare(const std::string op_type, float input, float expect) {
L
Luo Tao 已提交
31 32 33 34 35 36 37 38
  framework::Scope scope;
  platform::CUDAPlace place;
  platform::CUDADeviceContext ctx(place);

  // init fluid op and variable
  auto x_var = scope.Var("X");
  auto x_tensor = x_var->GetMutable<framework::LoDTensor>();
  x_tensor->Resize({1, 1});
39
  x_tensor->mutable_data<float>(place);
L
Luo Tao 已提交
40 41 42 43 44 45 46 47 48 49
  std::vector<float> init;
  init.push_back(input);
  framework::TensorFromVector(init, ctx, x_tensor);

  auto out_var = scope.Var("Out");
  auto out_tensor = out_var->GetMutable<framework::LoDTensor>();
  out_tensor->Resize({1, 1});
  out_tensor->mutable_data<float>(place);

  framework::OpDesc op_desc;
50
  op_desc.SetType(op_type);
L
Luo Tao 已提交
51 52 53
  op_desc.SetInput("X", {"X"});
  op_desc.SetOutput("Out", {"Out"});

54
  auto op = framework::OpRegistry::CreateOp(op_desc);
L
Luo Tao 已提交
55 56

  // run fluid op
57 58
  op->Run(scope, place);
  // get fluid output
L
Luo Tao 已提交
59 60 61 62 63 64 65 66 67 68
  std::vector<float> out1;
  framework::TensorToVector(*out_tensor, ctx, &out1);

  // init tensorrt op
  cudaStream_t stream;
  ASSERT_EQ(0, cudaStreamCreate(&stream));
  TensorRTEngine* engine = new TensorRTEngine(1, 1 << 10, &stream);
  engine->InitNetwork();
  engine->DeclareInput("X", nvinfer1::DataType::kFLOAT,
                       nvinfer1::DimsCHW{1, 1, 1});
69
  // convert op
L
Luo Tao 已提交
70 71 72 73 74
  OpConverter op_converter;
  op_converter.ConvertOp(op_desc, engine);
  engine->DeclareOutput("Out");
  engine->FreezeNetwork();

75 76
  // convert LoDTensor to ITensor
  size_t size = x_tensor->memory_size();
77 78
  EngineIOConverter::ConvertInput(op_type, *x_tensor,
                                  engine->buffer("X").buffer, size, &stream);
79
  // run tensorrt Outp
L
Luo Tao 已提交
80
  engine->Execute(1);
81
  // convert ITensor to LoDTensor
82 83
  EngineIOConverter::ConvertOutput(op_type, engine->buffer("Out").buffer,
                                   out_tensor, size, &stream);
84 85 86 87 88 89
  // get tensorrt output
  std::vector<float> out2;
  framework::TensorToVector(*out_tensor, ctx, &out2);

  // compare
  ASSERT_EQ(out1[0], out2[0]);
L
Luo Tao 已提交
90 91 92 93 94 95 96
  ASSERT_EQ(out1[0], expect);

  delete engine;
  cudaStreamDestroy(stream);
}

TEST(OpConverter, ConvertRelu) {
97 98
  Compare("relu", 1, 1);   // relu(1) = 1
  Compare("relu", -5, 0);  // relu(-5) = 0
L
Luo Tao 已提交
99 100 101 102 103
}

}  // namespace tensorrt
}  // namespace inference
}  // namespace paddle