|
1 | 1 | /* Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
|
2 | 2 |
|
3 |
| -Licensed under the Apache License, Version 2.0 (the "License"); |
4 |
| -you may not use this file except in compliance with the License. |
5 |
| -You may obtain a copy of the License at |
| 3 | + Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | + you may not use this file except in compliance with the License. |
| 5 | + You may obtain a copy of the License at |
6 | 6 |
|
7 |
| - http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | + http://www.apache.org/licenses/LICENSE-2.0 |
8 | 8 |
|
9 |
| -Unless required by applicable law or agreed to in writing, software |
10 |
| -distributed under the License is distributed on an "AS IS" BASIS, |
11 |
| -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 |
| -See the License for the specific language governing permissions and |
13 |
| -limitations under the License. */ |
| 9 | + Unless required by applicable law or agreed to in writing, software |
| 10 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | + See the License for the specific language governing permissions and |
| 13 | + limitations under the License. */ |
14 | 14 |
|
15 | 15 | #include <gtest/gtest.h>
|
16 |
| -#include "paddle/fluid/framework/lod_tensor.h" |
17 | 16 | #include "paddle/fluid/framework/op_registry.h"
|
18 |
| -#include "paddle/fluid/framework/program_desc.h" |
19 |
| -#include "paddle/fluid/inference/tensorrt/convert/io_converter.h" |
20 |
| -#include "paddle/fluid/inference/tensorrt/convert/op_converter.h" |
21 |
| -#include "paddle/fluid/platform/device_context.h" |
22 |
| -#include "paddle/fluid/platform/place.h" |
23 |
| - |
24 |
| -USE_OP(relu); |
| 17 | +#include "paddle/fluid/inference/tensorrt/convert/ut_helper.h" |
25 | 18 |
|
26 | 19 | namespace paddle {
|
27 | 20 | namespace inference {
|
28 | 21 | namespace tensorrt {
|
29 | 22 |
|
30 |
| -void Compare(const std::string op_type, float input, float expect) { |
| 23 | +TEST(ReluOpConverter, main) { |
31 | 24 | framework::Scope scope;
|
32 |
| - platform::CUDAPlace place; |
33 |
| - platform::CUDADeviceContext ctx(place); |
34 |
| - |
35 |
| - // init fluid op and variable |
36 |
| - auto x_var = scope.Var("X"); |
37 |
| - auto x_tensor = x_var->GetMutable<framework::LoDTensor>(); |
38 |
| - x_tensor->Resize({1, 1}); |
39 |
| - x_tensor->mutable_data<float>(place); |
40 |
| - std::vector<float> init; |
41 |
| - init.push_back(input); |
42 |
| - framework::TensorFromVector(init, ctx, x_tensor); |
43 |
| - |
44 |
| - auto out_var = scope.Var("Out"); |
45 |
| - auto out_tensor = out_var->GetMutable<framework::LoDTensor>(); |
46 |
| - out_tensor->Resize({1, 1}); |
47 |
| - out_tensor->mutable_data<float>(place); |
48 |
| - |
49 |
| - framework::OpDesc op_desc; |
50 |
| - op_desc.SetType(op_type); |
51 |
| - op_desc.SetInput("X", {"X"}); |
52 |
| - op_desc.SetOutput("Out", {"Out"}); |
53 |
| - |
54 |
| - auto op = framework::OpRegistry::CreateOp(*op_desc.Proto()); |
55 |
| - |
56 |
| - // run fluid op |
57 |
| - op->Run(scope, place); |
58 |
| - // get fluid output |
59 |
| - std::vector<float> out1; |
60 |
| - framework::TensorToVector(*out_tensor, ctx, &out1); |
61 |
| - |
62 |
| - // init tensorrt op |
63 |
| - cudaStream_t stream; |
64 |
| - ASSERT_EQ(0, cudaStreamCreate(&stream)); |
65 |
| - TensorRTEngine* engine = new TensorRTEngine(1, 1 << 10, &stream); |
66 |
| - engine->InitNetwork(); |
67 |
| - engine->DeclareInput("X", nvinfer1::DataType::kFLOAT, |
68 |
| - nvinfer1::DimsCHW{1, 1, 1}); |
69 |
| - // convert op |
70 |
| - OpConverter op_converter; |
71 |
| - op_converter.ConvertOp(*op_desc.Proto(), engine); |
72 |
| - |
73 |
| - engine->DeclareOutput("Out"); |
74 |
| - engine->FreezeNetwork(); |
75 |
| - |
76 |
| - // convert LoDTensor to ITensor |
77 |
| - size_t size = x_tensor->memory_size(); |
78 |
| - EngineIOConverter::ConvertInput(op_type, *x_tensor, |
79 |
| - engine->buffer("X").buffer, size, &stream); |
80 |
| - // run tensorrt Outp |
81 |
| - engine->Execute(1); |
82 |
| - // convert ITensor to LoDTensor |
83 |
| - EngineIOConverter::ConvertOutput(op_type, engine->buffer("Out").buffer, |
84 |
| - out_tensor, size, &stream); |
85 |
| - // get tensorrt output |
86 |
| - std::vector<float> out2; |
87 |
| - framework::TensorToVector(*out_tensor, ctx, &out2); |
88 |
| - |
89 |
| - // compare |
90 |
| - ASSERT_EQ(out1[0], out2[0]); |
91 |
| - ASSERT_EQ(out1[0], expect); |
92 |
| - |
93 |
| - delete engine; |
94 |
| - cudaStreamDestroy(stream); |
95 |
| -} |
96 |
| - |
97 |
| -TEST(OpConverter, ConvertRelu) { |
98 |
| - Compare("relu", 1, 1); // relu(1) = 1 |
99 |
| - Compare("relu", -5, 0); // relu(-5) = 0 |
| 25 | + std::unordered_set<std::string> parameters; |
| 26 | + TRTConvertValidation validator(10, parameters, scope, 1000); |
| 27 | + validator.DeclInputVar("relu-X", nvinfer1::Dims2(10, 6)); |
| 28 | + validator.DeclOutputVar("relu-Out", nvinfer1::Dims2(10, 6)); |
| 29 | + |
| 30 | + // Prepare Op description |
| 31 | + framework::OpDesc desc; |
| 32 | + desc.SetType("relu"); |
| 33 | + desc.SetInput("X", {"relu-X"}); |
| 34 | + desc.SetOutput("Out", {"relu-Out"}); |
| 35 | + |
| 36 | + LOG(INFO) << "set OP"; |
| 37 | + validator.SetOp(*desc.Proto()); |
| 38 | + LOG(INFO) << "execute"; |
| 39 | + |
| 40 | + validator.Execute(10); |
100 | 41 | }
|
101 | 42 |
|
102 | 43 | } // namespace tensorrt
|
103 | 44 | } // namespace inference
|
104 | 45 | } // namespace paddle
|
105 | 46 |
|
106 |
| -USE_OP(activation); |
| 47 | +USE_OP(relu); |
0 commit comments