Skip to content

Commit 9c7fde4

Browse files
committed
enhance test_analyzer to profile ditu inference demo
1 parent decda73 commit 9c7fde4

File tree

5 files changed

+32
-28
lines changed

5 files changed

+32
-28
lines changed

paddle/fluid/framework/ir/graph_pattern_detecter_tester.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,8 +163,8 @@ TEST(GraphPatternDetecter, MultiSubgraph) {
163163
// 3. Detect op2 -> var2 -> op4
164164
// 4. Detect op2 -> var3 -> op5
165165
// But 2 and 3 and 4 overlapped, so keep 2, so the final choices are 1 and 2
166-
ASSERT_GE(count, 1UL);
167-
ASSERT_LE(count, 2UL);
166+
ASSERT_GE(count, 1);
167+
ASSERT_LE(count, 2);
168168
}
169169

170170
} // namespace ir

paddle/fluid/framework/selected_rows.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ int64_t SelectedRows::AutoGrownIndex(int64_t key, bool auto_grown) {
139139
}
140140
auto write_iter = id_to_index_.find(key);
141141
if (write_iter == id_to_index_.end()) {
142-
size_t row_num = rows_.size();
142+
int row_num = rows_.size();
143143
if (row_num == value_->dims()[0]) {
144144
rwlock_->UNLock();
145145
PADDLE_THROW("selected rows is full, then length exceed %d", row_num);
@@ -182,7 +182,7 @@ void SelectedRows::Get(const framework::Tensor& ids, framework::Tensor* value,
182182
PADDLE_ENFORCE_EQ(value_width, value->numel() / value->dims()[0],
183183
"output tensor should have the same shape with table "
184184
"except the dims[0].");
185-
for (size_t i = 0; i < ids.numel(); ++i) {
185+
for (int i = 0; i < ids.numel(); ++i) {
186186
int64_t index = AutoGrownIndex(ids.data<int64_t>()[i], auto_grown);
187187
framework::VisitDataType(
188188
framework::ToDataType(value_->type()),

paddle/fluid/inference/analysis/analyzer_tester.cc

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323

2424
DEFINE_string(infer_ditu_rnn_model, "", "model path for ditu RNN");
2525
DEFINE_string(infer_ditu_rnn_data, "", "data path for ditu RNN");
26+
DEFINE_int32(batch_size, 10, "batch size.");
27+
DEFINE_int32(repeat, 1, "Running the inference program repeat times.");
2628

2729
namespace paddle {
2830
namespace inference {
@@ -92,7 +94,7 @@ struct DataRecord {
9294
size_t batch_iter{0};
9395
size_t batch_size{1};
9496
DataRecord() = default;
95-
DataRecord(const std::string &path, int batch_size = 1)
97+
explicit DataRecord(const std::string &path, int batch_size = 1)
9698
: batch_size(batch_size) {
9799
Load(path);
98100
}
@@ -165,7 +167,6 @@ struct DataRecord {
165167
};
166168
void PrepareInputs(std::vector<PaddleTensor> *input_slots, DataRecord *data,
167169
int batch_size) {
168-
// DataRecord data(FLAGS_datapath, batch_size);
169170
PaddleTensor lod_attention_tensor, init_zero_tensor, lod_tensor_tensor,
170171
week_tensor, minute_tensor;
171172
lod_attention_tensor.name = "data_lod_attention";
@@ -174,28 +175,33 @@ void PrepareInputs(std::vector<PaddleTensor> *input_slots, DataRecord *data,
174175
week_tensor.name = "week";
175176
minute_tensor.name = "minute";
176177
auto one_batch = data->NextBatch();
177-
// clang-format off
178-
std::vector<int> rnn_link_data_shape
179-
({static_cast<int>(one_batch.rnn_link_data.size()), static_cast<int>(one_batch.rnn_link_data.front().size())});
178+
std::vector<int> rnn_link_data_shape(
179+
{static_cast<int>(one_batch.rnn_link_data.size()),
180+
static_cast<int>(one_batch.rnn_link_data.front().size())});
180181
lod_attention_tensor.shape.assign({1, 2});
181182
lod_attention_tensor.lod.assign({one_batch.lod1, one_batch.lod2});
182183
init_zero_tensor.shape.assign({batch_size, 15});
183184
init_zero_tensor.lod.assign({one_batch.lod3});
184185
lod_tensor_tensor.shape = rnn_link_data_shape;
185186
lod_tensor_tensor.lod.assign({one_batch.lod1});
186-
week_tensor.shape.assign({(int) one_batch.rnn_week_datas.size(), (int) one_batch.rnn_week_datas.front().size()});
187+
// clang-format off
188+
week_tensor.shape.assign(
189+
{static_cast<int>(one_batch.rnn_week_datas.size()),
190+
static_cast<int>(one_batch.rnn_week_datas.front().size())});
187191
week_tensor.lod.assign({one_batch.lod3});
188-
minute_tensor.shape.assign({(int) one_batch.rnn_minute_datas.size(),
189-
(int) one_batch.rnn_minute_datas.front().size()});
192+
minute_tensor.shape.assign(
193+
{static_cast<int>(one_batch.rnn_minute_datas.size()),
194+
static_cast<int>(one_batch.rnn_minute_datas.front().size())});
190195
minute_tensor.lod.assign({one_batch.lod3});
196+
// clang-format on
191197
// assign data
192-
TensorAssignData(&lod_attention_tensor, std::vector<std::vector<float>>({{0, 0}}));
198+
TensorAssignData(&lod_attention_tensor,
199+
std::vector<std::vector<float>>({{0, 0}}));
193200
std::vector<float> tmp_zeros(batch_size * 15, 0.);
194201
TensorAssignData(&init_zero_tensor, {tmp_zeros});
195202
TensorAssignData(&lod_tensor_tensor, one_batch.rnn_link_data);
196203
TensorAssignData(&week_tensor, one_batch.rnn_week_datas);
197204
TensorAssignData(&minute_tensor, one_batch.rnn_minute_datas);
198-
// clang-format on
199205
// Set inputs.
200206
auto init_zero_tensor1 = init_zero_tensor;
201207
init_zero_tensor1.name = "hidden_init";
@@ -231,12 +237,9 @@ std::string DescribeTensor(const PaddleTensor &tensor) {
231237
os << "\n";
232238
os << " - data: ";
233239

234-
// clang-format off
235-
int dim = std::accumulate(tensor.shape.begin(),
236-
tensor.shape.end(),
237-
1,
238-
[](int a, int b) { return a * b; }); // clang-format on
239-
for (size_t i = 0; i < dim; i++) {
240+
int dim = std::accumulate(tensor.shape.begin(), tensor.shape.end(), 1,
241+
[](int a, int b) { return a * b; });
242+
for (int i = 0; i < dim; i++) {
240243
os << static_cast<float *>(tensor.data.data())[i] << " ";
241244
}
242245
os << '\n';
@@ -300,13 +303,16 @@ void TestDituRNNPrediction(const std::string &model_path,
300303
for (int i = 0; i < num_times; i++) {
301304
predictor->Run(input_slots, &outputs);
302305
}
303-
LOG(INFO) << "time/batch: " << timer.toc() / num_times;
306+
LOG(INFO) << "===========profile result===========";
307+
LOG(INFO) << "batch_size: " << batch_size << ", repeat: " << num_times
308+
<< ", latency: " << timer.toc() / num_times << "ms";
309+
LOG(INFO) << "=====================================";
304310

305311
for (auto &out : outputs) {
306312
size_t size = std::accumulate(out.shape.begin(), out.shape.end(), 1,
307313
[](int a, int b) { return a * b; });
308314
float *data = static_cast<float *>(out.data.data());
309-
for (int i = 0;
315+
for (size_t i = 0;
310316
i < std::min(sizeof(ditu_rnn_target_data) / sizeof(float), size);
311317
i++) {
312318
EXPECT_NEAR(data[i], ditu_rnn_target_data[i], 1e-3);
@@ -336,22 +342,22 @@ TEST(Analyzer, SupportIRPass) {
336342
// Directly infer with the original model.
337343
TEST(Analyzer, DituRNN_without_analysis) {
338344
TestDituRNNPrediction(FLAGS_infer_ditu_rnn_model, FLAGS_infer_ditu_rnn_data,
339-
10, false, false);
345+
FLAGS_batch_size, false, false, FLAGS_repeat);
340346
}
341347

342348
// Inference with the original model with the analysis turned on, the analysis
343349
// module will transform the program to a data flow graph.
344350
TEST(Analyzer, DituRNN_with_analysis) {
345351
LOG(INFO) << "ditu rnn with analysis";
346352
TestDituRNNPrediction(FLAGS_infer_ditu_rnn_model, FLAGS_infer_ditu_rnn_data,
347-
10, true, false, 1);
353+
FLAGS_batch_size, true, false, FLAGS_repeat);
348354
}
349355

350356
// Inference with analysis and IR. The IR module will fuse some large kernels.
351357
TEST(Analyzer, DituRNN_with_analysis_with_IR) {
352358
LOG(INFO) << "ditu rnn with analysis and IR fuse";
353359
TestDituRNNPrediction(FLAGS_infer_ditu_rnn_model, FLAGS_infer_ditu_rnn_data,
354-
10, true, true, 1);
360+
FLAGS_batch_size, true, true, FLAGS_repeat);
355361
}
356362

357363
} // namespace analysis

paddle/fluid/operators/sampling_id_op.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ class SamplingIdKernel : public framework::OpKernel<T> {
5454
static_cast<T>(context.Attr<float>("max")));
5555

5656
std::vector<T> ids(batch_size);
57-
for (size_t i = 0; i < batch_size; ++i) {
57+
for (int i = 0; i < batch_size; ++i) {
5858
T r = dist(engine);
5959
int idx = width - 1;
6060
for (int j = 0; j < width; ++j) {

paddle/scripts/paddle_build.sh

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,6 @@ function cmake_gen() {
116116
-DCMAKE_EXPORT_COMPILE_COMMANDS=ON
117117
-DWITH_CONTRIB=${WITH_CONTRIB:-ON}
118118
-DWITH_ANAKIN=${WITH_ANAKIN:-OFF}
119-
-DWITH_INFERENCE_DEMO=${WITH_INFERENCE_DEMO:-ON}
120119
-DPY_VERSION=${PY_VERSION:-2.7}
121120
========================================
122121
EOF
@@ -146,7 +145,6 @@ EOF
146145
-DCMAKE_EXPORT_COMPILE_COMMANDS=ON \
147146
-DWITH_CONTRIB=${WITH_CONTRIB:-ON} \
148147
-DWITH_ANAKIN=${WITH_ANAKIN:-OFF} \
149-
-DWITH_INFERENCE_DEMO=${WITH_INFERENCE_DEMO:-ON} \
150148
-DPY_VERSION=${PY_VERSION:-2.7}
151149
}
152150

0 commit comments

Comments
 (0)