Skip to content

Commit 01566fb

Browse files
committed
1. support mutil batch utest 2. support pool op
1 parent 21890ca commit 01566fb

File tree

7 files changed

+73
-25
lines changed

7 files changed

+73
-25
lines changed

paddle/fluid/inference/tensorrt/convert/pool2d_op.cc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ class Pool2dOpConverter : public OpConverter {
4343
const nvinfer1::DimsHW nv_strides(strides[0], strides[1]);
4444
const nvinfer1::DimsHW nv_paddings(paddings[0], paddings[1]);
4545

46+
PADDLE_ENFORCE_EQ(input1->getDimensions().nbDims, 3UL);
47+
4648
nvinfer1::PoolingType pool_t = nvinfer1::PoolingType::kMAX;
4749
if (pool_type == "max") {
4850
pool_t = nvinfer1::PoolingType::kMAX;

paddle/fluid/inference/tensorrt/convert/test_activation_op.cc

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ namespace tensorrt {
2323
TEST(ReluOpConverter, main) {
2424
framework::Scope scope;
2525
std::unordered_set<std::string> parameters;
26-
TRTConvertValidation validator(10, parameters, scope, 1000);
26+
int runtime_batch = 3;
27+
TRTConvertValidation validator(10, parameters, scope, 1000, runtime_batch);
2728
validator.DeclInputVar("relu-X", nvinfer1::Dims2(10, 6));
2829
validator.DeclOutputVar("relu-Out", nvinfer1::Dims2(10, 6));
2930

@@ -37,7 +38,7 @@ TEST(ReluOpConverter, main) {
3738
validator.SetOp(*desc.Proto());
3839
LOG(INFO) << "execute";
3940

40-
validator.Execute(1);
41+
validator.Execute(runtime_batch);
4142
}
4243

4344
} // namespace tensorrt

paddle/fluid/inference/tensorrt/convert/test_fc_op.cc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@ namespace tensorrt {
2323
TEST(fc_op, test) {
2424
std::unordered_set<std::string> parameters({"mul-Y"});
2525
framework::Scope scope;
26-
TRTConvertValidation validator(10, parameters, scope, 1000);
27-
validator.DeclInputVar("mul-X", nvinfer1::Dims4(1, 10, 1, 1));
26+
int runtime_batch = 2;
27+
TRTConvertValidation validator(10, parameters, scope, 1000, runtime_batch);
28+
validator.DeclInputVar("mul-X", nvinfer1::Dims3(10, 1, 1));
2829
validator.DeclParamVar("mul-Y", nvinfer1::Dims2(10, 2));
29-
// validator.DeclParamVar("mul-Y", nvinfer1::Dims2(8, 2));
3030
validator.DeclOutputVar("mul-Out", nvinfer1::Dims2(1, 2));
3131

3232
// Prepare Op description
@@ -38,7 +38,7 @@ TEST(fc_op, test) {
3838

3939
validator.SetOp(*desc.Proto());
4040

41-
validator.Execute(1);
41+
validator.Execute(runtime_batch);
4242
}
4343

4444
} // namespace tensorrt

paddle/fluid/inference/tensorrt/convert/test_mul_op.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ namespace tensorrt {
2323
TEST(MulOpConverter, main) {
2424
framework::Scope scope;
2525
std::unordered_set<std::string> parameters;
26-
TRTConvertValidation validator(10, parameters, scope, 1000);
26+
int runtime_batch = 0;
27+
TRTConvertValidation validator(10, parameters, scope, 1000, runtime_batch);
2728
validator.DeclInputVar("mul-X", nvinfer1::Dims2(10, 6));
2829
validator.DeclInputVar("mul-Y", nvinfer1::Dims2(6, 10));
2930
validator.DeclOutputVar("mul-Out", nvinfer1::Dims2(10, 10));

paddle/fluid/inference/tensorrt/convert/test_pool2d_op.cc

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,14 @@ namespace tensorrt {
2323
TEST(Pool2dOpConverter, main) {
2424
framework::Scope scope;
2525
std::unordered_set<std::string> parameters;
26-
TRTConvertValidation validator(10, parameters, scope, 1000);
27-
validator.DeclInputVar("pool2d-X", nvinfer1::Dims4(10, 3, 2, 2));
28-
validator.DeclOutputVar("pool2d-Out", nvinfer1::Dims4(10, 3, 1, 1));
26+
int runtime_batch = 3;
27+
TRTConvertValidation validator(5, parameters, scope, 1 << 15, runtime_batch);
28+
29+
// We have already set the runtime batchsize, so the
30+
// Dims should not contain the batch size.
31+
// The ITensor's Dims of input and output should be C * H * W.
32+
validator.DeclInputVar("pool2d-X", nvinfer1::Dims3(3, 4, 4));
33+
validator.DeclOutputVar("pool2d-Out", nvinfer1::Dims3(3, 2, 2));
2934

3035
// Prepare Op description
3136
framework::OpDesc desc;
@@ -34,26 +39,20 @@ TEST(Pool2dOpConverter, main) {
3439
desc.SetOutput("Out", {"pool2d-Out"});
3540

3641
std::vector<int> ksize({2, 2});
37-
std::vector<int> strides({1, 1});
42+
std::vector<int> strides({2, 2});
3843
std::vector<int> paddings({0, 0});
3944
std::string pooling_t = "max";
4045

4146
desc.SetAttr("pooling_type", pooling_t);
4247
desc.SetAttr("ksize", ksize);
4348
desc.SetAttr("strides", strides);
4449
desc.SetAttr("paddings", paddings);
45-
// std::string temp = "";
46-
// (*desc.Proto()).SerializeToString(&temp);
47-
48-
// std::cout << temp << std::endl;
49-
// std::ofstream f("__temp__", std::ios::out);
50-
// f << temp;
5150

5251
LOG(INFO) << "set OP";
5352
validator.SetOp(*desc.Proto());
5453
LOG(INFO) << "execute";
5554

56-
validator.Execute(10);
55+
validator.Execute(runtime_batch);
5756
}
5857

5958
} // namespace tensorrt

paddle/fluid/inference/tensorrt/convert/ut_helper.h

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,15 @@ class TRTConvertValidation {
6363
public:
6464
TRTConvertValidation() = delete;
6565

66-
TRTConvertValidation(int batch_size,
66+
TRTConvertValidation(int max_batch_size,
6767
const std::unordered_set<std::string>& parameters,
6868
framework::Scope& scope, // NOLINT
69-
int workspace_size = 1 << 10)
70-
: parameters_(parameters), scope_(scope) {
69+
int workspace_size = 1 << 10, int runtime_batch_size = 1)
70+
: parameters_(parameters),
71+
scope_(scope),
72+
runtime_batch_size_(runtime_batch_size) {
7173
// create engine.
72-
engine_.reset(new TensorRTEngine(batch_size, workspace_size, &stream_));
74+
engine_.reset(new TensorRTEngine(max_batch_size, workspace_size, &stream_));
7375
engine_->InitNetwork();
7476

7577
PADDLE_ENFORCE_EQ(cudaStreamCreate(&stream_), 0);
@@ -84,20 +86,28 @@ class TRTConvertValidation {
8486

8587
// Declare a parameter varaible in the scope.
8688
void DeclParamVar(const std::string& name, const nvinfer1::Dims& dims) {
87-
DeclVar(name, dims);
89+
DeclVar(name, dims, true);
8890
}
8991

9092
void DeclOutputVar(const std::string& name, const nvinfer1::Dims& dims) {
9193
DeclVar(name, dims);
9294
}
9395

9496
// Declare a variable in a fluid Scope.
95-
void DeclVar(const std::string& name, const nvinfer1::Dims& dims) {
97+
void DeclVar(const std::string& name, const nvinfer1::Dims& dims,
98+
bool is_param = false) {
9699
platform::CPUPlace place;
97100
platform::CPUDeviceContext ctx(place);
98101

99102
// Init Fluid tensor.
100103
std::vector<int> dim_vec(dims.d, dims.d + dims.nbDims);
104+
// There is no batchsize in ITensor's shape, but We should add it to
105+
// tensor's
106+
// shape of fluid. If the variable is not parameter and the batch size
107+
// greater than 0,
108+
// add the batchsize to dim_vec.
109+
if (is_param != true && runtime_batch_size_ > 0)
110+
dim_vec.insert(dim_vec.begin(), runtime_batch_size_);
101111
auto* x = scope_.Var(name);
102112
auto* x_tensor = x->GetMutable<framework::LoDTensor>();
103113
x_tensor->Resize(framework::make_ddim(dim_vec));
@@ -167,6 +177,10 @@ class TRTConvertValidation {
167177
std::unique_ptr<framework::OpDesc> op_desc_;
168178
const std::unordered_set<std::string>& parameters_;
169179
framework::Scope& scope_;
180+
// It represents the runtime batchsize when we test.
181+
// If the value greater than 0, we add this to
182+
// the first dimension of tensor's shape of fluid.
183+
int runtime_batch_size_;
170184
};
171185

172186
} // namespace tensorrt

paddle/fluid/inference/tensorrt/test_engine.cc

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ TEST_F(TensorRTEngineTest, add_layer_multi_dim) {
113113
ASSERT_EQ(y_cpu[1], 14.5);
114114
}
115115

116-
TEST_F(TensorRTEngineTest, test_conv2d_temp) {
116+
TEST_F(TensorRTEngineTest, test_conv2d) {
117117
// Weight in CPU memory.
118118
float raw_weight[9] = {1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0};
119119
float raw_bias[1] = {0};
@@ -146,6 +146,37 @@ TEST_F(TensorRTEngineTest, test_conv2d_temp) {
146146
ASSERT_EQ(y_cpu[1], 6.0);
147147
}
148148

149+
TEST_F(TensorRTEngineTest, test_pool2d) {
150+
// Weight in CPU memory.
151+
auto* x = engine_->DeclareInput("x", nvinfer1::DataType::kFLOAT,
152+
nvinfer1::Dims3{1, 2, 2});
153+
154+
nvinfer1::PoolingType pool_t = nvinfer1::PoolingType::kAVERAGE;
155+
auto* pool_layer =
156+
TRT_ENGINE_ADD_LAYER(engine_, Pooling, *const_cast<nvinfer1::ITensor*>(x),
157+
pool_t, nvinfer1::DimsHW{2, 2});
158+
159+
PADDLE_ENFORCE(pool_layer != nullptr);
160+
pool_layer->setStride(nvinfer1::DimsHW{1, 1});
161+
pool_layer->setPadding(nvinfer1::DimsHW{0, 0});
162+
163+
engine_->DeclareOutput(pool_layer, 0, "y");
164+
engine_->FreezeNetwork();
165+
ASSERT_EQ(engine_->engine()->getNbBindings(), 2);
166+
167+
float x_v[8] = {1.0, 2.0, 5.0, 0.0, 2.0, 3.0, 5.0, 10.0};
168+
engine_->SetInputFromCPU("x", reinterpret_cast<void*>(&x_v),
169+
8 * sizeof(float));
170+
engine_->Execute(2);
171+
172+
LOG(INFO) << "to get output";
173+
float* y_cpu = new float[2];
174+
engine_->GetOutputInCPU("y", &y_cpu[0], 2 * sizeof(float));
175+
176+
ASSERT_EQ(y_cpu[0], 2.0);
177+
ASSERT_EQ(y_cpu[1], 5.0);
178+
}
179+
149180
} // namespace tensorrt
150181
} // namespace inference
151182
} // namespace paddle

0 commit comments

Comments
 (0)