Skip to content

Commit 5bb7d59

Browse files
authored
Fix cpplint errors with paddle/fluid/inference (#9702)
* Correct inference * Update * Update
1 parent 1543c4c commit 5bb7d59

9 files changed

+61
-55
lines changed

paddle/fluid/inference/tests/book/test_inference_fit_a_line.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
99
See the License for the specific language governing permissions and
1010
limitations under the License. */
1111

12-
#include <gtest/gtest.h>
1312
#include "gflags/gflags.h"
13+
#include "gtest/gtest.h"
1414
#include "paddle/fluid/inference/tests/test_helper.h"
1515

1616
DEFINE_string(dirname, "", "Directory of the inference model.");
@@ -30,7 +30,7 @@ TEST(inference, fit_a_line) {
3030
// The second dim of the input tensor should be 13
3131
// The input data should be >= 0
3232
int64_t batch_size = 10;
33-
SetupTensor<float>(input, {batch_size, 13}, static_cast<float>(0),
33+
SetupTensor<float>(&input, {batch_size, 13}, static_cast<float>(0),
3434
static_cast<float>(10));
3535
std::vector<paddle::framework::LoDTensor*> cpu_feeds;
3636
cpu_feeds.push_back(&input);

paddle/fluid/inference/tests/book/test_inference_image_classification.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
See the License for the specific language governing permissions and
1313
limitations under the License. */
1414

15-
#include <gtest/gtest.h>
1615
#include "gflags/gflags.h"
16+
#include "gtest/gtest.h"
1717
#include "paddle/fluid/inference/tests/test_helper.h"
1818

1919
DEFINE_string(dirname, "", "Directory of the inference model.");
@@ -35,7 +35,7 @@ TEST(inference, image_classification) {
3535
paddle::framework::LoDTensor input;
3636
// Use normilized image pixels as input data,
3737
// which should be in the range [0.0, 1.0].
38-
SetupTensor<float>(input, {FLAGS_batch_size, 3, 32, 32},
38+
SetupTensor<float>(&input, {FLAGS_batch_size, 3, 32, 32},
3939
static_cast<float>(0), static_cast<float>(1));
4040
std::vector<paddle::framework::LoDTensor*> cpu_feeds;
4141
cpu_feeds.push_back(&input);

paddle/fluid/inference/tests/book/test_inference_label_semantic_roles.cc

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
See the License for the specific language governing permissions and
1313
limitations under the License. */
1414

15-
#include <gtest/gtest.h>
1615
#include "gflags/gflags.h"
16+
#include "gtest/gtest.h"
1717
#include "paddle/fluid/inference/tests/test_helper.h"
1818

1919
DEFINE_string(dirname, "", "Directory of the inference model.");
@@ -36,21 +36,21 @@ TEST(inference, label_semantic_roles) {
3636
int64_t predicate_dict_len = 3162;
3737
int64_t mark_dict_len = 2;
3838

39-
SetupLoDTensor(word, lod, static_cast<int64_t>(0),
39+
SetupLoDTensor(&word, lod, static_cast<int64_t>(0),
4040
static_cast<int64_t>(word_dict_len - 1));
41-
SetupLoDTensor(predicate, lod, static_cast<int64_t>(0),
41+
SetupLoDTensor(&predicate, lod, static_cast<int64_t>(0),
4242
static_cast<int64_t>(predicate_dict_len - 1));
43-
SetupLoDTensor(ctx_n2, lod, static_cast<int64_t>(0),
43+
SetupLoDTensor(&ctx_n2, lod, static_cast<int64_t>(0),
4444
static_cast<int64_t>(word_dict_len - 1));
45-
SetupLoDTensor(ctx_n1, lod, static_cast<int64_t>(0),
45+
SetupLoDTensor(&ctx_n1, lod, static_cast<int64_t>(0),
4646
static_cast<int64_t>(word_dict_len - 1));
47-
SetupLoDTensor(ctx_0, lod, static_cast<int64_t>(0),
47+
SetupLoDTensor(&ctx_0, lod, static_cast<int64_t>(0),
4848
static_cast<int64_t>(word_dict_len - 1));
49-
SetupLoDTensor(ctx_p1, lod, static_cast<int64_t>(0),
49+
SetupLoDTensor(&ctx_p1, lod, static_cast<int64_t>(0),
5050
static_cast<int64_t>(word_dict_len - 1));
51-
SetupLoDTensor(ctx_p2, lod, static_cast<int64_t>(0),
51+
SetupLoDTensor(&ctx_p2, lod, static_cast<int64_t>(0),
5252
static_cast<int64_t>(word_dict_len - 1));
53-
SetupLoDTensor(mark, lod, static_cast<int64_t>(0),
53+
SetupLoDTensor(&mark, lod, static_cast<int64_t>(0),
5454
static_cast<int64_t>(mark_dict_len - 1));
5555

5656
std::vector<paddle::framework::LoDTensor*> cpu_feeds;

paddle/fluid/inference/tests/book/test_inference_recognize_digits.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
See the License for the specific language governing permissions and
1313
limitations under the License. */
1414

15-
#include <gtest/gtest.h>
1615
#include "gflags/gflags.h"
16+
#include "gtest/gtest.h"
1717
#include "paddle/fluid/inference/tests/test_helper.h"
1818

1919
DEFINE_string(dirname, "", "Directory of the inference model.");
@@ -35,7 +35,7 @@ TEST(inference, recognize_digits) {
3535
paddle::framework::LoDTensor input;
3636
// Use normilized image pixels as input data,
3737
// which should be in the range [-1.0, 1.0].
38-
SetupTensor<float>(input, {FLAGS_batch_size, 1, 28, 28},
38+
SetupTensor<float>(&input, {FLAGS_batch_size, 1, 28, 28},
3939
static_cast<float>(-1), static_cast<float>(1));
4040
std::vector<paddle::framework::LoDTensor*> cpu_feeds;
4141
cpu_feeds.push_back(&input);

paddle/fluid/inference/tests/book/test_inference_recommender_system.cc

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
See the License for the specific language governing permissions and
1313
limitations under the License. */
1414

15-
#include <gtest/gtest.h>
1615
#include "gflags/gflags.h"
16+
#include "gtest/gtest.h"
1717
#include "paddle/fluid/inference/tests/test_helper.h"
1818

1919
DEFINE_string(dirname, "", "Directory of the inference model.");
@@ -36,25 +36,25 @@ TEST(inference, recommender_system) {
3636

3737
// Use the first data from paddle.dataset.movielens.test() as input
3838
std::vector<int64_t> user_id_data = {1};
39-
SetupTensor<int64_t>(user_id, {batch_size, 1}, user_id_data);
39+
SetupTensor<int64_t>(&user_id, {batch_size, 1}, user_id_data);
4040

4141
std::vector<int64_t> gender_id_data = {1};
42-
SetupTensor<int64_t>(gender_id, {batch_size, 1}, gender_id_data);
42+
SetupTensor<int64_t>(&gender_id, {batch_size, 1}, gender_id_data);
4343

4444
std::vector<int64_t> age_id_data = {0};
45-
SetupTensor<int64_t>(age_id, {batch_size, 1}, age_id_data);
45+
SetupTensor<int64_t>(&age_id, {batch_size, 1}, age_id_data);
4646

4747
std::vector<int64_t> job_id_data = {10};
48-
SetupTensor<int64_t>(job_id, {batch_size, 1}, job_id_data);
48+
SetupTensor<int64_t>(&job_id, {batch_size, 1}, job_id_data);
4949

5050
std::vector<int64_t> movie_id_data = {783};
51-
SetupTensor<int64_t>(movie_id, {batch_size, 1}, movie_id_data);
51+
SetupTensor<int64_t>(&movie_id, {batch_size, 1}, movie_id_data);
5252

5353
std::vector<int64_t> category_id_data = {10, 8, 9};
54-
SetupLoDTensor<int64_t>(category_id, {3, 1}, {{0, 3}}, category_id_data);
54+
SetupLoDTensor<int64_t>(&category_id, {3, 1}, {{0, 3}}, category_id_data);
5555

5656
std::vector<int64_t> movie_title_data = {1069, 4140, 2923, 710, 988};
57-
SetupLoDTensor<int64_t>(movie_title, {5, 1}, {{0, 5}}, movie_title_data);
57+
SetupLoDTensor<int64_t>(&movie_title, {5, 1}, {{0, 5}}, movie_title_data);
5858

5959
std::vector<paddle::framework::LoDTensor*> cpu_feeds;
6060
cpu_feeds.push_back(&user_id);

paddle/fluid/inference/tests/book/test_inference_rnn_encoder_decoder.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
See the License for the specific language governing permissions and
1313
limitations under the License. */
1414

15-
#include <gtest/gtest.h>
1615
#include "gflags/gflags.h"
16+
#include "gtest/gtest.h"
1717
#include "paddle/fluid/inference/tests/test_helper.h"
1818

1919
DEFINE_string(dirname, "", "Directory of the inference model.");
@@ -32,9 +32,9 @@ TEST(inference, rnn_encoder_decoder) {
3232
paddle::framework::LoDTensor word_data, trg_word;
3333
paddle::framework::LoD lod{{0, 4, 10}};
3434

35-
SetupLoDTensor(word_data, lod, static_cast<int64_t>(0),
35+
SetupLoDTensor(&word_data, lod, static_cast<int64_t>(0),
3636
static_cast<int64_t>(1));
37-
SetupLoDTensor(trg_word, lod, static_cast<int64_t>(0),
37+
SetupLoDTensor(&trg_word, lod, static_cast<int64_t>(0),
3838
static_cast<int64_t>(1));
3939

4040
std::vector<paddle::framework::LoDTensor*> cpu_feeds;

paddle/fluid/inference/tests/book/test_inference_understand_sentiment.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
See the License for the specific language governing permissions and
1313
limitations under the License. */
1414

15-
#include <gtest/gtest.h>
1615
#include "gflags/gflags.h"
16+
#include "gtest/gtest.h"
1717
#include "paddle/fluid/inference/tests/test_helper.h"
1818

1919
DEFINE_string(dirname, "", "Directory of the inference model.");
@@ -33,7 +33,7 @@ TEST(inference, understand_sentiment) {
3333
paddle::framework::LoD lod{{0, 4, 10}};
3434
int64_t word_dict_len = 5147;
3535

36-
SetupLoDTensor(words, lod, static_cast<int64_t>(0),
36+
SetupLoDTensor(&words, lod, static_cast<int64_t>(0),
3737
static_cast<int64_t>(word_dict_len - 1));
3838

3939
std::vector<paddle::framework::LoDTensor*> cpu_feeds;

paddle/fluid/inference/tests/book/test_inference_word2vec.cc

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
See the License for the specific language governing permissions and
1313
limitations under the License. */
1414

15-
#include <gtest/gtest.h>
1615
#include "gflags/gflags.h"
16+
#include "gtest/gtest.h"
1717
#include "paddle/fluid/inference/tests/test_helper.h"
1818

1919
DEFINE_string(dirname, "", "Directory of the inference model.");
@@ -33,10 +33,10 @@ TEST(inference, word2vec) {
3333
paddle::framework::LoD lod{{0, 1}};
3434
int64_t dict_size = 2073; // The size of dictionary
3535

36-
SetupLoDTensor(first_word, lod, static_cast<int64_t>(0), dict_size - 1);
37-
SetupLoDTensor(second_word, lod, static_cast<int64_t>(0), dict_size - 1);
38-
SetupLoDTensor(third_word, lod, static_cast<int64_t>(0), dict_size - 1);
39-
SetupLoDTensor(fourth_word, lod, static_cast<int64_t>(0), dict_size - 1);
36+
SetupLoDTensor(&first_word, lod, static_cast<int64_t>(0), dict_size - 1);
37+
SetupLoDTensor(&second_word, lod, static_cast<int64_t>(0), dict_size - 1);
38+
SetupLoDTensor(&third_word, lod, static_cast<int64_t>(0), dict_size - 1);
39+
SetupLoDTensor(&fourth_word, lod, static_cast<int64_t>(0), dict_size - 1);
4040

4141
std::vector<paddle::framework::LoDTensor*> cpu_feeds;
4242
cpu_feeds.push_back(&first_word);

paddle/fluid/inference/tests/test_helper.h

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -11,53 +11,59 @@ distributed under the License is distributed on an "AS IS" BASIS,
1111
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
See the License for the specific language governing permissions and
1313
limitations under the License. */
14+
#pragma once
15+
16+
#include <map>
17+
#include <random>
18+
#include <string>
19+
#include <vector>
1420

15-
#include <time.h>
1621
#include "paddle/fluid/framework/lod_tensor.h"
1722
#include "paddle/fluid/inference/io.h"
1823
#include "paddle/fluid/platform/profiler.h"
1924

2025
template <typename T>
21-
void SetupTensor(paddle::framework::LoDTensor& input,
26+
void SetupTensor(paddle::framework::LoDTensor* input,
2227
paddle::framework::DDim dims, T lower, T upper) {
23-
srand(time(0));
24-
T* input_ptr = input.mutable_data<T>(dims, paddle::platform::CPUPlace());
25-
for (int i = 0; i < input.numel(); ++i) {
26-
input_ptr[i] =
27-
(static_cast<T>(rand()) / static_cast<T>(RAND_MAX)) * (upper - lower) +
28-
lower;
28+
std::mt19937 rng(100); // An arbitrarily chosen but fixed seed.
29+
std::uniform_real_distribution<double> uniform_dist(0, 1);
30+
31+
T* input_ptr = input->mutable_data<T>(dims, paddle::platform::CPUPlace());
32+
for (int i = 0; i < input->numel(); ++i) {
33+
input_ptr[i] = static_cast<T>(uniform_dist(rng) * (upper - lower) + lower);
2934
}
3035
}
3136

3237
template <typename T>
33-
void SetupTensor(paddle::framework::LoDTensor& input,
34-
paddle::framework::DDim dims, std::vector<T>& data) {
38+
void SetupTensor(paddle::framework::LoDTensor* input,
39+
paddle::framework::DDim dims, const std::vector<T>& data) {
3540
CHECK_EQ(paddle::framework::product(dims), static_cast<int64_t>(data.size()));
36-
T* input_ptr = input.mutable_data<T>(dims, paddle::platform::CPUPlace());
37-
memcpy(input_ptr, data.data(), input.numel() * sizeof(T));
41+
T* input_ptr = input->mutable_data<T>(dims, paddle::platform::CPUPlace());
42+
memcpy(input_ptr, data.data(), input->numel() * sizeof(T));
3843
}
3944

4045
template <typename T>
41-
void SetupLoDTensor(paddle::framework::LoDTensor& input,
42-
paddle::framework::LoD& lod, T lower, T upper) {
43-
input.set_lod(lod);
46+
void SetupLoDTensor(paddle::framework::LoDTensor* input,
47+
const paddle::framework::LoD& lod, T lower, T upper) {
48+
input->set_lod(lod);
4449
int dim = lod[0][lod[0].size() - 1];
4550
SetupTensor<T>(input, {dim, 1}, lower, upper);
4651
}
4752

4853
template <typename T>
49-
void SetupLoDTensor(paddle::framework::LoDTensor& input,
50-
paddle::framework::DDim dims, paddle::framework::LoD lod,
51-
std::vector<T>& data) {
54+
void SetupLoDTensor(paddle::framework::LoDTensor* input,
55+
paddle::framework::DDim dims,
56+
const paddle::framework::LoD lod,
57+
const std::vector<T>& data) {
5258
const size_t level = lod.size() - 1;
5359
CHECK_EQ(dims[0], static_cast<int64_t>((lod[level]).back()));
54-
input.set_lod(lod);
60+
input->set_lod(lod);
5561
SetupTensor<T>(input, dims, data);
5662
}
5763

5864
template <typename T>
59-
void CheckError(paddle::framework::LoDTensor& output1,
60-
paddle::framework::LoDTensor& output2) {
65+
void CheckError(const paddle::framework::LoDTensor& output1,
66+
const paddle::framework::LoDTensor& output2) {
6167
// Check lod information
6268
EXPECT_EQ(output1.lod(), output2.lod());
6369

@@ -85,7 +91,7 @@ void CheckError(paddle::framework::LoDTensor& output1,
8591
template <typename Place>
8692
void TestInference(const std::string& dirname,
8793
const std::vector<paddle::framework::LoDTensor*>& cpu_feeds,
88-
std::vector<paddle::framework::LoDTensor*>& cpu_fetchs,
94+
const std::vector<paddle::framework::LoDTensor*>& cpu_fetchs,
8995
const int repeat = 1, const bool is_combined = false) {
9096
// 1. Define place, executor, scope
9197
auto place = Place();

0 commit comments

Comments
 (0)