forked from PaddlePaddle/Paddle
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Update Paddle inference]: update inference ut (PaddlePaddle#317)
* update inference ut * fix copyright
- Loading branch information
Showing
7 changed files
with
417 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
184 changes: 184 additions & 0 deletions
184
paddle/fluid/inference/tests/api/ipu_ernie_fp16_test.cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,184 @@ | ||
// Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
#include "paddle/fluid/inference/tests/api/tester_helper.h" | ||
|
||
namespace paddle { | ||
namespace inference { | ||
|
||
using paddle::PaddleTensor; | ||
|
||
template <typename T> | ||
void GetValueFromStream(std::stringstream *ss, T *t) { | ||
(*ss) >> (*t); | ||
} | ||
|
||
template <> | ||
void GetValueFromStream<std::string>(std::stringstream *ss, std::string *t) { | ||
*t = ss->str(); | ||
} | ||
|
||
// Split string to vector | ||
template <typename T> | ||
void Split(const std::string &line, char sep, std::vector<T> *v) { | ||
std::stringstream ss; | ||
T t; | ||
for (auto c : line) { | ||
if (c != sep) { | ||
ss << c; | ||
} else { | ||
GetValueFromStream<T>(&ss, &t); | ||
v->push_back(std::move(t)); | ||
ss.str({}); | ||
ss.clear(); | ||
} | ||
} | ||
|
||
if (!ss.str().empty()) { | ||
GetValueFromStream<T>(&ss, &t); | ||
v->push_back(std::move(t)); | ||
ss.str({}); | ||
ss.clear(); | ||
} | ||
} | ||
|
||
// Parse tensor from string | ||
template <typename T> | ||
bool ParseTensor(const std::string &field, paddle::PaddleTensor *tensor) { | ||
std::vector<std::string> data; | ||
Split(field, ':', &data); | ||
if (data.size() < 2) return false; | ||
|
||
std::string shape_str = data[0]; | ||
|
||
std::vector<int> shape; | ||
Split(shape_str, ' ', &shape); | ||
|
||
std::string mat_str = data[1]; | ||
|
||
std::vector<T> mat; | ||
Split(mat_str, ' ', &mat); | ||
|
||
tensor->shape = shape; | ||
auto size = | ||
std::accumulate(shape.begin(), shape.end(), 1, std::multiplies<int>()) * | ||
sizeof(T); | ||
tensor->data.Resize(size); | ||
std::copy(mat.begin(), mat.end(), static_cast<T *>(tensor->data.data())); | ||
tensor->dtype = GetPaddleDType<T>(); | ||
|
||
return true; | ||
} | ||
|
||
// Parse input tensors from string | ||
bool ParseLine(const std::string &line, | ||
std::vector<paddle::PaddleTensor> *tensors) { | ||
std::vector<std::string> fields; | ||
Split(line, ';', &fields); | ||
|
||
tensors->clear(); | ||
tensors->reserve(4); | ||
|
||
int i = 0; | ||
auto input_name = FLAGS_ernie_large ? "eval_placeholder_" : "placeholder_"; | ||
for (; i < 3; i++) { | ||
paddle::PaddleTensor temp; | ||
ParseTensor<int64_t>(fields[i], &temp); | ||
temp.name = input_name + std::to_string(i); | ||
tensors->push_back(temp); | ||
} | ||
|
||
// input_mask | ||
paddle::PaddleTensor input_mask; | ||
ParseTensor<float>(fields[i], &input_mask); | ||
// fp32 to fp16 | ||
ConvertFP32toFP16(input_mask); | ||
input_mask.name = input_name + std::to_string(i); | ||
tensors->push_back(input_mask); | ||
|
||
return true; | ||
} | ||
|
||
bool LoadInputData(std::vector<std::vector<paddle::PaddleTensor>> *inputs, | ||
int batch_size = 1) { | ||
if (FLAGS_infer_data.empty()) { | ||
LOG(ERROR) << "please set input data path"; | ||
return false; | ||
} | ||
|
||
std::ifstream fin(FLAGS_infer_data); | ||
std::string line; | ||
int sample = 0; | ||
|
||
// The unit-test dataset only have 10 samples, each sample have 5 feeds. | ||
while (std::getline(fin, line)) { | ||
std::vector<paddle::PaddleTensor> feed_data; | ||
ParseLine(line, &feed_data); | ||
inputs->push_back(std::move(feed_data)); | ||
sample++; | ||
if (!FLAGS_test_all_data && sample == batch_size) break; | ||
} | ||
LOG(INFO) << "number of samples: " << sample; | ||
return true; | ||
} | ||
|
||
void SetConfig(AnalysisConfig *cfg, int batch_size = 1) { | ||
cfg->SetModel(FLAGS_infer_model); | ||
// ipu_device_num, ipu_micro_batch_size, ipu_enable_pipelining | ||
cfg->EnableIpu(1, batch_size, false); | ||
// ipu_enable_fp16, ipu_replica_num, ipu_available_memory_proportion, | ||
// ipu_enable_half_partial | ||
cfg->SetIpuConfig(true, 1, 1.0, true); | ||
} | ||
|
||
// Compare results | ||
TEST(Analyzer_Ernie_ipu, compare_results) { | ||
AnalysisConfig cfg; | ||
SetConfig(&cfg); | ||
|
||
std::vector<std::vector<PaddleTensor>> input_slots_all; | ||
LoadInputData(&input_slots_all); | ||
|
||
std::ifstream fin(FLAGS_refer_result); | ||
std::string line; | ||
std::vector<float> ref; | ||
|
||
while (std::getline(fin, line)) { | ||
Split(line, ' ', &ref); | ||
} | ||
|
||
auto predictor = CreateTestPredictor( | ||
reinterpret_cast<const PaddlePredictor::Config *>(&cfg), | ||
FLAGS_use_analysis); | ||
|
||
std::vector<PaddleTensor> outputs; | ||
for (size_t i = 0; i < input_slots_all.size(); i++) { | ||
outputs.clear(); | ||
predictor->Run(input_slots_all[i], &outputs); | ||
|
||
auto output = outputs.front(); | ||
ConvertFP16toFP32(output); | ||
auto outputs_size = 1; | ||
for (auto dim : output.shape) { | ||
outputs_size *= dim; | ||
} | ||
float *fp32_data = reinterpret_cast<float *>(output.data.data()); | ||
for (size_t j = 0; j < outputs_size; ++j) { | ||
EXPECT_NEAR(ref[i * outputs_size + j], fp32_data[j], 5e-3); | ||
} | ||
} | ||
} | ||
|
||
} // namespace inference | ||
} // namespace paddle |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
paddle/fluid/inference/tests/api/ipu_resnet50_fp16_test.cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
/* Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. */ | ||
|
||
#include <glog/logging.h> | ||
#include <gtest/gtest.h> | ||
#include <cmath> | ||
|
||
#include "gflags/gflags.h" | ||
#include "paddle/fluid/inference/tests/api/tester_helper.h" | ||
|
||
namespace paddle { | ||
namespace inference { | ||
|
||
// Compare results with 1 batch | ||
TEST(Analyzer_Resnet50_ipu, compare_results_1_batch) { | ||
std::string model_dir = FLAGS_infer_model + "/" + "model"; | ||
AnalysisConfig config; | ||
// ipu_device_num, ipu_micro_batch_size, ipu_enable_pipelining | ||
config.EnableIpu(1, 1, false); | ||
// ipu_enable_fp16, ipu_replica_num, ipu_available_memory_proportion, | ||
// ipu_enable_half_partial | ||
config.SetIpuConfig(true, 1, 1.0, true); | ||
config.SetModel(model_dir + "/model", model_dir + "/params"); | ||
|
||
std::vector<PaddleTensor> inputs; | ||
auto predictor = CreatePaddlePredictor(config); | ||
const int batch = 1; | ||
const int channel = 3; | ||
const int height = 318; | ||
const int width = 318; | ||
const int input_num = batch * channel * height * width; | ||
std::vector<float> input(input_num, 1); | ||
|
||
PaddleTensor in; | ||
in.shape = {batch, channel, height, width}; | ||
in.data = | ||
PaddleBuf(static_cast<void*>(input.data()), input_num * sizeof(float)); | ||
in.dtype = PaddleDType::FLOAT32; | ||
ConvertFP32toFP16(in); | ||
inputs.emplace_back(in); | ||
|
||
std::vector<PaddleTensor> outputs; | ||
|
||
ASSERT_TRUE(predictor->Run(inputs, &outputs)); | ||
|
||
const std::vector<float> truth_values = { | ||
127.779f, 738.165f, 1013.22f, -438.17f, 366.401f, 927.659f, | ||
736.222f, -633.684f, -329.927f, -430.155f, -633.062f, -146.548f, | ||
-1324.28f, -1349.36f, -242.675f, 117.448f, -801.723f, -391.514f, | ||
-404.818f, 454.16f, 515.48f, -133.031f, 69.293f, 590.096f, | ||
-1434.69f, -1070.89f, 307.074f, 400.525f, -316.12f, -587.125f, | ||
-161.056f, 800.363f, -96.4708f, 748.706f, 868.174f, -447.938f, | ||
112.737f, 1127.2f, 47.4355f, 677.72f, 593.186f, -336.4f, | ||
551.362f, 397.823f, 78.3979f, -715.398f, 405.969f, 404.256f, | ||
246.019f, -8.42969f, 131.365f, -648.051f}; | ||
|
||
const size_t expected_size = 1; | ||
EXPECT_EQ(outputs.size(), expected_size); | ||
|
||
auto output = outputs.front(); | ||
ConvertFP16toFP32(output); | ||
auto outputs_size = 1; | ||
for (auto dim : output.shape) { | ||
outputs_size *= dim; | ||
} | ||
float* fp32_data = reinterpret_cast<float*>(output.data.data()); | ||
|
||
for (size_t j = 0; j < outputs_size; j += 10) { | ||
EXPECT_NEAR((fp32_data[j] - truth_values[j / 10]) / truth_values[j / 10], | ||
0., 9e-2); | ||
} | ||
} | ||
|
||
} // namespace inference | ||
} // namespace paddle |
Oops, something went wrong.