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

15
#include <gtest/gtest.h>
16
#include "paddle/fluid/framework/lod_tensor.h"
L
Luo Tao 已提交
17
#include "paddle/fluid/inference/tensorrt/convert/io_converter.h"
18 19 20 21 22

namespace paddle {
namespace inference {
namespace tensorrt {

23 24 25
void IOConverterTester(const platform::DeviceContext& ctx) {
  cudaStream_t stream;
  ASSERT_EQ(0, cudaStreamCreate(&stream));
26

27 28 29 30 31 32 33 34 35 36
  // init fluid in_tensor
  framework::LoDTensor in_tensor;
  in_tensor.Resize({10, 10});
  auto place = ctx.GetPlace();
  in_tensor.mutable_data<float>(place);
  std::vector<float> init;
  for (int64_t i = 0; i < 10 * 10; ++i) {
    init.push_back(i);
  }
  framework::TensorFromVector(init, ctx, &in_tensor);
37

38
  // init tensorrt buffer
39
  void* buffer;
40 41
  size_t size = in_tensor.memory_size();
  ASSERT_EQ(cudaMalloc(&buffer, size), 0);
42

43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
  // convert fluid in_tensor to tensorrt buffer
  EngineIOConverter::ConvertInput("test", in_tensor, buffer, size, &stream);

  // convert tensorrt buffer to fluid out_tensor
  framework::LoDTensor out_tensor;
  out_tensor.Resize({10, 10});
  out_tensor.mutable_data<float>(place);
  EngineIOConverter::ConvertOutput("test", buffer, &out_tensor, size, &stream);

  // compare in_tensor and out_tensor
  std::vector<float> result;
  framework::TensorToVector(out_tensor, ctx, &result);
  EXPECT_EQ(init.size(), result.size());
  for (size_t i = 0; i < init.size(); i++) {
    EXPECT_EQ(init[i], result[i]);
  }
  cudaStreamDestroy(stream);
60 61
}

62 63 64 65 66
TEST(EngineIOConverterTester, DefaultCPU) {
  platform::CPUPlace place;
  platform::CPUDeviceContext ctx(place);
  IOConverterTester(ctx);
}
67

68 69 70 71
TEST(EngineIOConverterTester, DefaultGPU) {
  platform::CUDAPlace place;
  platform::CUDADeviceContext ctx(place);
  IOConverterTester(ctx);
72 73 74 75 76
}

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