Skip to content

Commit 5533400

Browse files
committed
fix comments
1 parent 0dcbeda commit 5533400

File tree

6 files changed

+35
-32
lines changed

6 files changed

+35
-32
lines changed

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ class Pool2dOpConverter : public OpConverter {
3030
framework::OpDesc op_desc(op, nullptr);
3131
// Declare inputs
3232
auto* input1 = engine_->GetITensor(op_desc.Input("X")[0]);
33+
3334
std::string pool_type =
3435
boost::get<std::string>(op_desc.GetAttr("pooling_type"));
3536
std::vector<int> ksize =
@@ -45,18 +46,18 @@ class Pool2dOpConverter : public OpConverter {
4546

4647
PADDLE_ENFORCE_EQ(input1->getDimensions().nbDims, 3UL);
4748

48-
nvinfer1::PoolingType pool_t = nvinfer1::PoolingType::kMAX;
49+
nvinfer1::PoolingType nv_pool_type = nvinfer1::PoolingType::kMAX;
4950
if (pool_type == "max") {
50-
pool_t = nvinfer1::PoolingType::kMAX;
51+
nv_pool_type = nvinfer1::PoolingType::kMAX;
5152
} else if (pool_type == "avg") {
52-
pool_t = nvinfer1::PoolingType::kAVERAGE;
53+
nv_pool_type = nvinfer1::PoolingType::kAVERAGE;
5354
} else {
5455
PADDLE_THROW("TensorRT unsupported pooling type!");
5556
}
5657

5758
auto* layer = TRT_ENGINE_ADD_LAYER(engine_, Pooling,
5859
*const_cast<nvinfer1::ITensor*>(input1),
59-
pool_t, nv_ksize);
60+
nv_pool_type, nv_ksize);
6061
PADDLE_ENFORCE_NOT_NULL(layer, "pool layer could not be created.");
6162
layer->setStride(nv_strides);
6263
layer->setPadding(nv_paddings);

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

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

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

41-
validator.Execute(runtime_batch);
40+
validator.Execute(5);
4241
}
4342

4443
} // namespace tensorrt

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@ namespace tensorrt {
2323
TEST(fc_op, test) {
2424
std::unordered_set<std::string> parameters({"mul-Y"});
2525
framework::Scope scope;
26-
int runtime_batch = 2;
27-
TRTConvertValidation validator(10, parameters, scope, 1000, runtime_batch);
26+
TRTConvertValidation validator(10, parameters, scope, 1000);
2827
validator.DeclInputVar("mul-X", nvinfer1::Dims3(10, 1, 1));
2928
validator.DeclParamVar("mul-Y", nvinfer1::Dims2(10, 2));
3029
validator.DeclOutputVar("mul-Out", nvinfer1::Dims2(1, 2));
@@ -38,7 +37,7 @@ TEST(fc_op, test) {
3837

3938
validator.SetOp(*desc.Proto());
4039

41-
validator.Execute(runtime_batch);
40+
validator.Execute(10);
4241
}
4342

4443
} // namespace tensorrt

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@ namespace tensorrt {
2323
TEST(MulOpConverter, main) {
2424
framework::Scope scope;
2525
std::unordered_set<std::string> parameters;
26-
int runtime_batch = 0;
27-
TRTConvertValidation validator(10, parameters, scope, 1000, runtime_batch);
26+
TRTConvertValidation validator(10, parameters, scope, 1000, false);
2827
validator.DeclInputVar("mul-X", nvinfer1::Dims2(10, 6));
2928
validator.DeclInputVar("mul-Y", nvinfer1::Dims2(6, 10));
3029
validator.DeclOutputVar("mul-Out", nvinfer1::Dims2(10, 10));
@@ -40,7 +39,7 @@ TEST(MulOpConverter, main) {
4039
validator.SetOp(*desc.Proto());
4140
LOG(INFO) << "execute";
4241

43-
validator.Execute(1);
42+
validator.Execute(2);
4443
}
4544

4645
} // namespace tensorrt

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

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,10 @@ namespace tensorrt {
2323
TEST(Pool2dOpConverter, main) {
2424
framework::Scope scope;
2525
std::unordered_set<std::string> parameters;
26-
int runtime_batch = 3;
27-
TRTConvertValidation validator(5, parameters, scope, 1 << 15, runtime_batch);
26+
TRTConvertValidation validator(5, parameters, scope, 1 << 15);
2827

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.
28+
// The ITensor's Dims should not contain the batch size.
29+
// So, the ITensor's Dims of input and output should be C * H * W.
3230
validator.DeclInputVar("pool2d-X", nvinfer1::Dims3(3, 4, 4));
3331
validator.DeclOutputVar("pool2d-Out", nvinfer1::Dims3(3, 2, 2));
3432

@@ -52,7 +50,7 @@ TEST(Pool2dOpConverter, main) {
5250
validator.SetOp(*desc.Proto());
5351
LOG(INFO) << "execute";
5452

55-
validator.Execute(runtime_batch);
53+
validator.Execute(3);
5654
}
5755

5856
} // namespace tensorrt

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

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,11 @@ class TRTConvertValidation {
6666
TRTConvertValidation(int max_batch_size,
6767
const std::unordered_set<std::string>& parameters,
6868
framework::Scope& scope, // NOLINT
69-
int workspace_size = 1 << 10, int runtime_batch_size = 1)
69+
int workspace_size = 1 << 10, bool if_add_batch = true)
7070
: parameters_(parameters),
7171
scope_(scope),
72-
runtime_batch_size_(runtime_batch_size) {
72+
if_add_batch_(if_add_batch),
73+
max_batch_size_(max_batch_size) {
7374
// create engine.
7475
engine_.reset(new TensorRTEngine(max_batch_size, workspace_size, &stream_));
7576
engine_->InitNetwork();
@@ -102,12 +103,10 @@ class TRTConvertValidation {
102103
// Init Fluid tensor.
103104
std::vector<int> dim_vec(dims.d, dims.d + dims.nbDims);
104105
// 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_);
106+
// tensor's shape of fluid. If the variable is not parameter and the
107+
// if_add_batch_ flag is true, add the max batchsize to dim_vec.
108+
if (is_param != true && if_add_batch_ == true)
109+
dim_vec.insert(dim_vec.begin(), max_batch_size_);
111110
auto* x = scope_.Var(name);
112111
auto* x_tensor = x->GetMutable<framework::LoDTensor>();
113112
x_tensor->Resize(framework::make_ddim(dim_vec));
@@ -141,6 +140,7 @@ class TRTConvertValidation {
141140

142141
void Execute(int batch_size) {
143142
// Execute Fluid Op
143+
PADDLE_ENFORCE_LE(batch_size, max_batch_size_);
144144
platform::CPUPlace place;
145145
platform::CPUDeviceContext ctx(place);
146146
op_->Run(scope_, place);
@@ -159,9 +159,14 @@ class TRTConvertValidation {
159159
auto* var = scope_.FindVar(output);
160160
auto tensor = var->GetMutable<framework::LoDTensor>();
161161
framework::TensorToVector(*tensor, ctx, &fluid_out);
162+
163+
size_t fluid_out_size = fluid_out.size();
164+
if (if_add_batch_ == true) {
165+
fluid_out_size = batch_size * (tensor->dims().size() / max_batch_size_);
166+
}
162167
// Compare two output
163168
ASSERT_FALSE(fluid_out.empty());
164-
for (size_t i = 0; i < fluid_out.size(); i++) {
169+
for (size_t i = 0; i < fluid_out_size; i++) {
165170
// Loose the threshold for CI in different machine model.
166171
EXPECT_LT(std::abs(fluid_out[i] - trt_out[i]), 2e-5);
167172
}
@@ -177,10 +182,12 @@ class TRTConvertValidation {
177182
std::unique_ptr<framework::OpDesc> op_desc_;
178183
const std::unordered_set<std::string>& parameters_;
179184
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_;
185+
// The ITensor of trt does not cotain the batch size,
186+
// bug, in most cases, we need to set batch size for
187+
// fluid's tensor shape. This variable indicates
188+
// whether to add batch size to tensor shape of fluid.
189+
bool if_add_batch_;
190+
int max_batch_size_;
184191
};
185192

186193
} // namespace tensorrt

0 commit comments

Comments
 (0)