|
|
@ -16,19 +16,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
#include <glog/logging.h>
|
|
|
|
#include <glog/logging.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <algorithm>
|
|
|
|
#include <chrono> // NOLINT
|
|
|
|
#include <numeric>
|
|
|
|
#include <numeric>
|
|
|
|
#include <sstream>
|
|
|
|
#include <sstream>
|
|
|
|
#include <string>
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
#include <vector>
|
|
|
|
#include "paddle/fluid/framework/lod_tensor.h"
|
|
|
|
|
|
|
|
#include "paddle/fluid/inference/api/paddle_inference_api.h"
|
|
|
|
|
|
|
|
#include "paddle/fluid/inference/api/timer.h"
|
|
|
|
|
|
|
|
#include "paddle/fluid/string/printf.h"
|
|
|
|
#include "paddle/fluid/string/printf.h"
|
|
|
|
|
|
|
|
#include "paddle_inference_api.h"
|
|
|
|
|
|
|
|
|
|
|
|
namespace paddle {
|
|
|
|
namespace paddle {
|
|
|
|
namespace inference {
|
|
|
|
namespace inference {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Timer for timer
|
|
|
|
|
|
|
|
class Timer {
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
std::chrono::high_resolution_clock::time_point start;
|
|
|
|
|
|
|
|
std::chrono::high_resolution_clock::time_point startu;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void tic() { start = std::chrono::high_resolution_clock::now(); }
|
|
|
|
|
|
|
|
double toc() {
|
|
|
|
|
|
|
|
startu = std::chrono::high_resolution_clock::now();
|
|
|
|
|
|
|
|
std::chrono::duration<double> time_span =
|
|
|
|
|
|
|
|
std::chrono::duration_cast<std::chrono::duration<double>>(startu -
|
|
|
|
|
|
|
|
start);
|
|
|
|
|
|
|
|
double used_time_ms = static_cast<double>(time_span.count()) * 1000.0;
|
|
|
|
|
|
|
|
return used_time_ms;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
static void split(const std::string &str, char sep,
|
|
|
|
static void split(const std::string &str, char sep,
|
|
|
|
std::vector<std::string> *pieces) {
|
|
|
|
std::vector<std::string> *pieces) {
|
|
|
|
pieces->clear();
|
|
|
|
pieces->clear();
|
|
|
@ -154,127 +169,5 @@ static void PrintTime(int batch_size, int repeat, int num_threads, int tid,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
|
|
|
|
std::string LoDTensorSummary(const framework::LoDTensor &tensor) {
|
|
|
|
|
|
|
|
std::stringstream ss;
|
|
|
|
|
|
|
|
ss << "\n---- tensor ---" << '\n';
|
|
|
|
|
|
|
|
ss << "lod: [";
|
|
|
|
|
|
|
|
for (const auto &level : tensor.lod()) {
|
|
|
|
|
|
|
|
ss << "[ ";
|
|
|
|
|
|
|
|
for (auto i : level) {
|
|
|
|
|
|
|
|
ss << i << ", ";
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
ss << "]";
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
ss << "]\n";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ss << "shape: [";
|
|
|
|
|
|
|
|
int size = 1;
|
|
|
|
|
|
|
|
for (int i = 0; i < tensor.dims().size(); i++) {
|
|
|
|
|
|
|
|
int dim = tensor.dims()[i];
|
|
|
|
|
|
|
|
ss << dim << ", ";
|
|
|
|
|
|
|
|
size *= dim;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
ss << "]\n";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ss << "data: ";
|
|
|
|
|
|
|
|
for (int i = 0; i < std::min(20, size); i++) {
|
|
|
|
|
|
|
|
ss << tensor.data<T>()[i] << " ";
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
ss << "\n";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return ss.str();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static bool CompareLoD(const framework::LoD &a, const framework::LoD &b) {
|
|
|
|
|
|
|
|
if (a.size() != b.size()) {
|
|
|
|
|
|
|
|
LOG(ERROR) << string::Sprintf("lod size not match %d != %d", a.size(),
|
|
|
|
|
|
|
|
b.size());
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
for (size_t i = 0; i < a.size(); i++) {
|
|
|
|
|
|
|
|
auto &al = a[i];
|
|
|
|
|
|
|
|
auto &bl = b[i];
|
|
|
|
|
|
|
|
if (al.size() != bl.size()) {
|
|
|
|
|
|
|
|
LOG(ERROR) << string::Sprintf("level size %d != %d", al.size(),
|
|
|
|
|
|
|
|
bl.size());
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static bool CompareShape(const std::vector<int64_t> &a,
|
|
|
|
|
|
|
|
const std::vector<int64_t> &b) {
|
|
|
|
|
|
|
|
if (a.size() != b.size()) {
|
|
|
|
|
|
|
|
LOG(ERROR) << string::Sprintf("shape size not match %d != %d", a.size(),
|
|
|
|
|
|
|
|
b.size());
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
for (size_t i = 0; i < a.size(); i++) {
|
|
|
|
|
|
|
|
if (a[i] != b[i]) {
|
|
|
|
|
|
|
|
LOG(ERROR) << string::Sprintf("shape %d-th element not match %d != %d", i,
|
|
|
|
|
|
|
|
a[i], b[i]);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static bool CompareTensorData(const framework::LoDTensor &a,
|
|
|
|
|
|
|
|
const framework::LoDTensor &b) {
|
|
|
|
|
|
|
|
auto a_shape = framework::vectorize(a.dims());
|
|
|
|
|
|
|
|
auto b_shape = framework::vectorize(b.dims());
|
|
|
|
|
|
|
|
size_t a_size = std::accumulate(a_shape.begin(), a_shape.end(), 1,
|
|
|
|
|
|
|
|
[](int a, int b) { return a * b; });
|
|
|
|
|
|
|
|
size_t b_size = std::accumulate(b_shape.begin(), b_shape.end(), 1,
|
|
|
|
|
|
|
|
[](int a, int b) { return a * b; });
|
|
|
|
|
|
|
|
if (a_size != b_size) {
|
|
|
|
|
|
|
|
LOG(ERROR) << string::Sprintf("tensor data size not match, %d != %d",
|
|
|
|
|
|
|
|
a_size, b_size);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for (size_t i = 0; i < a_size; i++) {
|
|
|
|
|
|
|
|
if (a.type() == typeid(float)) {
|
|
|
|
|
|
|
|
const auto *a_data = a.data<float>();
|
|
|
|
|
|
|
|
const auto *b_data = b.data<float>();
|
|
|
|
|
|
|
|
if (std::abs(a_data[i] - b_data[i]) > 1e-3) {
|
|
|
|
|
|
|
|
LOG(ERROR) << string::Sprintf(
|
|
|
|
|
|
|
|
"tensor data %d-th element not match, %f != %f", i, a_data[i],
|
|
|
|
|
|
|
|
b_data[i]);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
} else if (a.type() == typeid(int64_t)) {
|
|
|
|
|
|
|
|
const auto *a_data = a.data<int64_t>();
|
|
|
|
|
|
|
|
const auto *b_data = b.data<int64_t>();
|
|
|
|
|
|
|
|
if (std::abs(a_data[i] - b_data[i]) > 1e-3) {
|
|
|
|
|
|
|
|
LOG(ERROR) << string::Sprintf(
|
|
|
|
|
|
|
|
"tensor data %d-th element not match, %f != %f", i, a_data[i],
|
|
|
|
|
|
|
|
b_data[i]);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static bool CompareTensor(const framework::LoDTensor &a,
|
|
|
|
|
|
|
|
const framework::LoDTensor &b) {
|
|
|
|
|
|
|
|
if (!CompareLoD(a.lod(), b.lod())) {
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!CompareShape(framework::vectorize(a.dims()),
|
|
|
|
|
|
|
|
framework::vectorize(b.dims()))) {
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!CompareTensorData(a, b)) {
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} // namespace inference
|
|
|
|
} // namespace inference
|
|
|
|
} // namespace paddle
|
|
|
|
} // namespace paddle
|