未验证 提交 31a2c876 编写于 作者: Y Yan Chunwei 提交者: GitHub

fea/lightly support lod (#12451)

上级 38863a2c
...@@ -37,19 +37,21 @@ TEST(inference, anakin) { ...@@ -37,19 +37,21 @@ TEST(inference, anakin) {
float data[1 * 3 * 224 * 224] = {1.0f}; float data[1 * 3 * 224 * 224] = {1.0f};
PaddleTensor tensor{.name = "input_0", PaddleTensor tensor;
.shape = std::vector<int>({1, 3, 224, 224}), tensor.name = "input_0";
.data = PaddleBuf(data, sizeof(data)), tensor.shape = std::vector<int>({1, 3, 224, 224});
.dtype = PaddleDType::FLOAT32}; tensor.data = PaddleBuf(data, sizeof(data));
tensor.dtype = PaddleDType::FLOAT32;
// For simplicity, we set all the slots with the same data. // For simplicity, we set all the slots with the same data.
std::vector<PaddleTensor> paddle_tensor_feeds; std::vector<PaddleTensor> paddle_tensor_feeds;
paddle_tensor_feeds.emplace_back(std::move(tensor)); paddle_tensor_feeds.emplace_back(std::move(tensor));
PaddleTensor tensor_out{.name = "prob_out", PaddleTensor tensor_out;
.shape = std::vector<int>({1000, 1}), tensor_out.name = "prob_out";
.data = PaddleBuf(), tensor_out.shape = std::vector<int>({1000, 1});
.dtype = PaddleDType::FLOAT32}; tensor_out.data = PaddleBuf();
tensor_out.dtype = PaddleDType::FLOAT32;
std::vector<PaddleTensor> outputs; std::vector<PaddleTensor> outputs;
outputs.emplace_back(std::move(tensor_out)); outputs.emplace_back(std::move(tensor_out));
......
...@@ -183,6 +183,13 @@ bool NativePaddlePredictor::SetFeed(const std::vector<PaddleTensor> &inputs, ...@@ -183,6 +183,13 @@ bool NativePaddlePredictor::SetFeed(const std::vector<PaddleTensor> &inputs,
// TODO(panyx0718): Init LoDTensor from existing memcpy to save a copy. // TODO(panyx0718): Init LoDTensor from existing memcpy to save a copy.
std::memcpy(static_cast<void *>(input_ptr), inputs[i].data.data(), std::memcpy(static_cast<void *>(input_ptr), inputs[i].data.data(),
inputs[i].data.length()); inputs[i].data.length());
// TODO(Superjomn) Low performance, need optimization for heavy LoD copy.
framework::LoD lod;
for (auto &level : inputs[i].lod) {
lod.emplace_back(level);
}
input.set_lod(lod);
feeds->push_back(input); feeds->push_back(input);
} }
return true; return true;
...@@ -248,6 +255,10 @@ bool NativePaddlePredictor::GetFetch( ...@@ -248,6 +255,10 @@ bool NativePaddlePredictor::GetFetch(
buffer.Resize(sizeof(float) * data.size()); buffer.Resize(sizeof(float) * data.size());
} }
std::memcpy(buffer.data(), data.data(), buffer.length()); std::memcpy(buffer.data(), data.data(), buffer.length());
// copy LoD
for (const auto &level : fetchs[i].lod()) {
outputs->at(i).lod.emplace_back(level);
}
outputs->at(i).dtype = PaddleDType::FLOAT32; outputs->at(i).dtype = PaddleDType::FLOAT32;
// TODO(panyx0718): support other types? fill tensor name? avoid a copy. // TODO(panyx0718): support other types? fill tensor name? avoid a copy.
} }
......
...@@ -49,11 +49,10 @@ void CompareTensorRTWithFluid(bool enable_tensorrt) { ...@@ -49,11 +49,10 @@ void CompareTensorRTWithFluid(bool enable_tensorrt) {
std::vector<int64_t> data(20); std::vector<int64_t> data(20);
for (int i = 0; i < 20; i++) data[i] = i; for (int i = 0; i < 20; i++) data[i] = i;
PaddleTensor tensor{ PaddleTensor tensor;
.name = "", tensor.shape = std::vector<int>({10, 1});
.shape = std::vector<int>({10, 1}), tensor.data = PaddleBuf(data.data(), data.size() * sizeof(int64_t));
.data = PaddleBuf(data.data(), data.size() * sizeof(int64_t)), tensor.dtype = PaddleDType::INT64;
.dtype = PaddleDType::INT64};
// For simplicity, we set all the slots with the same data. // For simplicity, we set all the slots with the same data.
std::vector<PaddleTensor> slots(4, tensor); std::vector<PaddleTensor> slots(4, tensor);
......
...@@ -47,10 +47,10 @@ void Main(bool use_gpu) { ...@@ -47,10 +47,10 @@ void Main(bool use_gpu) {
//# 2. Prepare input. //# 2. Prepare input.
int64_t data[4] = {1, 2, 3, 4}; int64_t data[4] = {1, 2, 3, 4};
PaddleTensor tensor{.name = "", PaddleTensor tensor;
.shape = std::vector<int>({4, 1}), tensor.shape = std::vector<int>({4, 1});
.data = PaddleBuf(data, sizeof(data)), tensor.data = PaddleBuf(data, sizeof(data));
.dtype = PaddleDType::INT64}; tensor.dtype = PaddleDType::INT64;
// For simplicity, we set all the slots with the same data. // For simplicity, we set all the slots with the same data.
std::vector<PaddleTensor> slots(4, tensor); std::vector<PaddleTensor> slots(4, tensor);
...@@ -94,10 +94,11 @@ void MainThreads(int num_threads, bool use_gpu) { ...@@ -94,10 +94,11 @@ void MainThreads(int num_threads, bool use_gpu) {
for (int batch_id = 0; batch_id < num_batches; ++batch_id) { for (int batch_id = 0; batch_id < num_batches; ++batch_id) {
// 2. Dummy Input Data // 2. Dummy Input Data
int64_t data[4] = {1, 2, 3, 4}; int64_t data[4] = {1, 2, 3, 4};
PaddleTensor tensor{.name = "", PaddleTensor tensor;
.shape = std::vector<int>({4, 1}), tensor.shape = std::vector<int>({4, 1});
.data = PaddleBuf(data, sizeof(data)), tensor.data = PaddleBuf(data, sizeof(data));
.dtype = PaddleDType::INT64}; tensor.dtype = PaddleDType::INT64;
std::vector<PaddleTensor> inputs(4, tensor); std::vector<PaddleTensor> inputs(4, tensor);
std::vector<PaddleTensor> outputs; std::vector<PaddleTensor> outputs;
// 3. Run // 3. Run
......
...@@ -123,11 +123,11 @@ void Main(bool use_gpu) { ...@@ -123,11 +123,11 @@ void Main(bool use_gpu) {
file.close(); file.close();
// Inference. // Inference.
PaddleTensor input{ PaddleTensor input;
.name = "xx", input.shape = record.shape;
.shape = record.shape, input.data =
.data = PaddleBuf(record.data.data(), record.data.size() * sizeof(float)), PaddleBuf(record.data.data(), record.data.size() * sizeof(float));
.dtype = PaddleDType::FLOAT32}; input.dtype = PaddleDType::FLOAT32;
VLOG(3) << "run executor"; VLOG(3) << "run executor";
std::vector<PaddleTensor> output; std::vector<PaddleTensor> output;
......
...@@ -67,9 +67,9 @@ struct PaddleTensor { ...@@ -67,9 +67,9 @@ struct PaddleTensor {
PaddleTensor() = default; PaddleTensor() = default;
std::string name; // variable name. std::string name; // variable name.
std::vector<int> shape; std::vector<int> shape;
// TODO(Superjomn) for LoD support, add a vector<vector<int>> field if needed.
PaddleBuf data; // blob of data. PaddleBuf data; // blob of data.
PaddleDType dtype; PaddleDType dtype;
std::vector<std::vector<uint64_t>> lod; // lod data
}; };
enum class PaddleEngineKind { enum class PaddleEngineKind {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册