提交 962061f0 编写于 作者: D dzhwinter

windows fix

上级 85f8dd1c
...@@ -82,7 +82,7 @@ if(CUDNN_FOUND) ...@@ -82,7 +82,7 @@ if(CUDNN_FOUND)
if(NOT CUDNN_MAJOR_VERSION) if(NOT CUDNN_MAJOR_VERSION)
set(CUDNN_VERSION "???") set(CUDNN_VERSION "???")
else() else()
math(EXPR CUDNN_VERSION math(EXPR CUDNN_VERSION
"${CUDNN_MAJOR_VERSION} * 1000 + "${CUDNN_MAJOR_VERSION} * 1000 +
${CUDNN_MINOR_VERSION} * 100 + ${CUDNN_PATCHLEVEL_VERSION}") ${CUDNN_MINOR_VERSION} * 100 + ${CUDNN_PATCHLEVEL_VERSION}")
......
...@@ -243,7 +243,7 @@ function(cc_library TARGET_NAME) ...@@ -243,7 +243,7 @@ function(cc_library TARGET_NAME)
# add libxxx.lib prefix in windows # add libxxx.lib prefix in windows
set(${TARGET_NAME}_LIB_NAME "${CMAKE_STATIC_LIBRARY_PREFIX}${TARGET_NAME}${CMAKE_STATIC_LIBRARY_SUFFIX}" CACHE STRING "output library name for target ${TARGET_NAME}") set(${TARGET_NAME}_LIB_NAME "${CMAKE_STATIC_LIBRARY_PREFIX}${TARGET_NAME}${CMAKE_STATIC_LIBRARY_SUFFIX}" CACHE STRING "output library name for target ${TARGET_NAME}")
endif(WIN32) endif(WIN32)
message("flags" ${CMAKE_CXX_FLAGS})
if(cc_library_SRCS) if(cc_library_SRCS)
if(cc_library_SHARED OR cc_library_shared) # build *.so if(cc_library_SHARED OR cc_library_shared) # build *.so
add_library(${TARGET_NAME} SHARED ${cc_library_SRCS}) add_library(${TARGET_NAME} SHARED ${cc_library_SRCS})
......
...@@ -293,26 +293,41 @@ void Executor::Run(const ProgramDesc& program, Scope* scope, ...@@ -293,26 +293,41 @@ void Executor::Run(const ProgramDesc& program, Scope* scope,
std::unique_ptr<ExecutorPrepareContext> Executor::Prepare( std::unique_ptr<ExecutorPrepareContext> Executor::Prepare(
const ProgramDesc& program, int block_id) { const ProgramDesc& program, int block_id) {
VLOG(3) << "before create prepare" << block_id << " " << program.Size();
std::unique_ptr<ExecutorPrepareContext> ctx( std::unique_ptr<ExecutorPrepareContext> ctx(
new ExecutorPrepareContext(program, block_id)); new ExecutorPrepareContext(program, block_id));
PADDLE_ENFORCE_LT(static_cast<size_t>(block_id), program.Size()); VLOG(3) << "after create prepare";
// PADDLE_ENFORCE_LT(static_cast<size_t>(block_id), program.Size());
VLOG(3) << "before create op_desc";
auto& block = program.Block(block_id); auto& block = program.Block(block_id);
VLOG(3) << "create before" << ctx->ops_.size() << " " << block.AllOps().size();
int counter = 0;
for (auto& op_desc : block.AllOps()) { for (auto& op_desc : block.AllOps()) {
ctx->ops_.push_back(OpRegistry::CreateOp(*op_desc)); ctx->ops_.push_back(OpRegistry::CreateOp(*op_desc));
VLOG(3) << "create op " << "index " << ++counter << " type " << op_desc->Type();
} }
VLOG(3) << "create finished" << ctx->ops_.size() << " " << block.AllOps().size();
return ctx; return ctx;
} }
std::vector<std::shared_ptr<ExecutorPrepareContext>> Executor::Prepare( std::vector<std::shared_ptr<ExecutorPrepareContext>> Executor::Prepare(
const ProgramDesc& program, const std::vector<int>& block_ids) { const ProgramDesc& program, const std::vector<int>& block_ids) {
VLOG(3) << "inside prepare";
std::vector<std::shared_ptr<ExecutorPrepareContext>> result; std::vector<std::shared_ptr<ExecutorPrepareContext>> result;
VLOG(3) << "before go through block_ids";
for (auto& bid : block_ids) { for (auto& bid : block_ids) {
VLOG(3) << "block id" << bid;
auto* ctx = new ExecutorPrepareContext(program, bid); auto* ctx = new ExecutorPrepareContext(program, bid);
PADDLE_ENFORCE_LT(static_cast<size_t>(bid), program.Size()); //PADDLE_ENFORCE_LT(static_cast<size_t>(bid), program.Size());
auto& block = program.Block(bid); auto& block = program.Block(bid);
int counter = 0;
VLOG(3) << "create before" << ctx->ops_.size() << " " << block.AllOps().size();
for (auto& op_desc : block.AllOps()) { for (auto& op_desc : block.AllOps()) {
ctx->ops_.push_back(OpRegistry::CreateOp(*op_desc)); ctx->ops_.push_back(OpRegistry::CreateOp(*op_desc));
VLOG(3) << "create op " << "index " << ++counter << " type " << op_desc->Type();
} }
VLOG(3) << "create finished" << ctx->ops_.size() << " " << block.AllOps().size();
result.push_back(std::shared_ptr<ExecutorPrepareContext>(ctx)); result.push_back(std::shared_ptr<ExecutorPrepareContext>(ctx));
} }
return result; return result;
......
...@@ -88,12 +88,16 @@ bool NativePaddlePredictor::Init( ...@@ -88,12 +88,16 @@ bool NativePaddlePredictor::Init(
VLOG(3) << config_.model_dir; VLOG(3) << config_.model_dir;
inference_program_ = paddle::inference::Load(executor_.get(), scope_.get(), inference_program_ = paddle::inference::Load(executor_.get(), scope_.get(),
config_.model_dir); config_.model_dir);
VLOG(3) << "load model Finish"; VLOG(3) << "load model finish";
} else if (!config_.prog_file.empty() && !config_.param_file.empty()) { } else if (!config_.prog_file.empty() && !config_.param_file.empty()) {
// All parameters are saved in a single file. // All parameters are saved in a single file.
// The file names should be consistent with that used // The file names should be consistent with that used
// in Python API `fluid.io.save_inference_model`. // in Python API `fluid.io.save_inference_model`.
VLOG(3) << "load program"; VLOG(3) << "load program before";
auto exe = executor_.get();
VLOG(3) << "executor_";
auto sc = scope_.get();
VLOG(3) << "scope_";
inference_program_ = paddle::inference::Load( inference_program_ = paddle::inference::Load(
executor_.get(), scope_.get(), config_.prog_file, config_.param_file); executor_.get(), scope_.get(), config_.prog_file, config_.param_file);
VLOG(3) << "load program finish"; VLOG(3) << "load program finish";
...@@ -101,13 +105,18 @@ bool NativePaddlePredictor::Init( ...@@ -101,13 +105,18 @@ bool NativePaddlePredictor::Init(
LOG(ERROR) << "fail to load inference model."; LOG(ERROR) << "fail to load inference model.";
return false; return false;
} }
VLOG(3) << "prepare"; VLOG(3) << "pointer" << inference_program_.get();
VLOG(3) << "prepare before";
ctx_ = executor_->Prepare(*inference_program_, 0); ctx_ = executor_->Prepare(*inference_program_, 0);
VLOG(3) << "prepare finished";
executor_->CreateVariables(*inference_program_, executor_->CreateVariables(*inference_program_,
sub_scope_ ? sub_scope_ : scope_.get(), 0); sub_scope_ ? sub_scope_ : scope_.get(), 0);
VLOG(3) << "create variables";
// Get the feed_target_names and fetch_target_names // Get the feed_target_names and fetch_target_names
PrepareFeedFetch(); PrepareFeedFetch();
VLOG(3) << "feed fetch";
return true; return true;
} }
......
...@@ -29,17 +29,18 @@ limitations under the License. */ ...@@ -29,17 +29,18 @@ limitations under the License. */
#include "paddle/fluid/inference/paddle_inference_api.h" #include "paddle/fluid/inference/paddle_inference_api.h"
std::string MODELDIR = ""; /* "Directory of the inference model." */ // NOLINT std::string MODELDIR = ""; /* "Directory of the inference model." */ // NOLINT
std::string REFER = ""; /*"path to reference result for comparison."*/ //NOTLINT std::string REFER = "";
/*"path to reference result for comparison."*/ //NOTLINT
/*path of data; each line is a record, format: /*path of data; each line is a record, format:
<space splitted floats as data>\t<space splitted ints as shape> <space splitted floats as data>\t<space splitted ints as shape>
Please check the demo data of data.txt for details. Please check the demo data of data.txt for details.
*/ */
std::string DATA = ""; std::string DATA = "";
bool USE_GPU = false; /*"Whether use gpu."*/ bool USE_GPU = true; /*"Whether use gpu."*/
auto message_err = []()
auto message_err = []() { {
std::cout << "Copyright (c) 2018 PaddlePaddle Authors." << std::endl; std::cout << "Copyright (c) 2018 PaddlePaddle Authors." << std::endl;
std::cout << "Demo Case for windows inference. " std::cout << "Demo Case for windows inference. "
<< "\n" << "\n"
...@@ -49,187 +50,197 @@ auto message_err = []() { ...@@ -49,187 +50,197 @@ auto message_err = []() {
std::cout << std::endl; std::cout << std::endl;
}; };
namespace paddle
namespace paddle { {
namespace demo { namespace demo
{
void split(const std::string& str, char sep, void split(const std::string& str, char sep,
std::vector<std::string>* pieces) { std::vector<std::string>* pieces)
pieces->clear(); {
if (str.empty()) { pieces->clear();
return; if (str.empty())
} {
size_t pos = 0; return;
size_t next = str.find(sep, pos); }
while (next != std::string::npos) { size_t pos = 0;
pieces->push_back(str.substr(pos, next - pos)); size_t next = str.find(sep, pos);
pos = next + 1; while (next != std::string::npos)
next = str.find(sep, pos); {
} pieces->push_back(str.substr(pos, next - pos));
if (!str.substr(pos).empty()) { pos = next + 1;
pieces->push_back(str.substr(pos)); next = str.find(sep, pos);
} }
} if (!str.substr(pos).empty())
{
/* pieces->push_back(str.substr(pos));
* Get a summary of a PaddleTensor content. }
*/ }
std::string SummaryTensor(const PaddleTensor& tensor) {
std::stringstream ss; /*
int num_elems = tensor.data.length() / PaddleDtypeSize(tensor.dtype); * Get a summary of a PaddleTensor content.
*/
ss << "data[:10]\t"; std::string SummaryTensor(const PaddleTensor& tensor)
switch (tensor.dtype) { {
case PaddleDType::INT64: { std::stringstream ss;
for (int i = 0; i < std::min(num_elems, 10); i++) { int num_elems = tensor.data.length() / PaddleDtypeSize(tensor.dtype);
ss << static_cast<int64_t*>(tensor.data.data())[i] << " ";
} ss << "data[:10]\t";
break; switch (tensor.dtype)
} {
case PaddleDType::FLOAT32: case PaddleDType::INT64:
for (int i = 0; i < std::min(num_elems, 10); i++) { for (int i = 0; i < std::min(num_elems, 10); i++)
ss << static_cast<float*>(tensor.data.data())[i] << " "; {
} ss << static_cast<int64_t*>(tensor.data.data())[i] << " ";
break; }
} break;
return ss.str(); case PaddleDType::FLOAT32:
} for (int i = 0; i < std::min(num_elems, 10); i++)
{
std::string ToString(const NativeConfig& config) { ss << static_cast<float*>(tensor.data.data())[i] << " ";
std::stringstream ss; }
ss << "Use GPU : " << (config.use_gpu ? "True" : "False") << "\n" break;
<< "Device : " << config.device << "\n" }
<< "fraction_of_gpu_memory : " << config.fraction_of_gpu_memory << "\n" return ss.str();
<< "specify_input_name : " }
<< (config.specify_input_name ? "True" : "False") << "\n"
<< "Program File : " << config.prog_file << "\n" std::string ToString(const NativeConfig& config)
<< "Param File : " << config.param_file; {
return ss.str(); std::stringstream ss;
} ss << "Use GPU : " << (config.use_gpu ? "True" : "False") << "\n"
<< "Device : " << config.device << "\n"
struct Record { << "fraction_of_gpu_memory : " << config.fraction_of_gpu_memory << "\n"
std::vector<float> data; << "specify_input_name : "
std::vector<int32_t> shape; << (config.specify_input_name ? "True" : "False") << "\n"
}; << "Program File : " << config.prog_file << "\n"
<< "Param File : " << config.param_file;
return ss.str();
Record ProcessALine(const std::string& line) { }
std::cout << "process a line" << std::endl;;
std::vector<std::string> columns; struct Record
split(line, '\t', &columns); {
assert(columns.size() == 2UL, std::vector<float> data;
"data format error, should be <data>\t<shape>"); std::vector<int32_t> shape;
};
Record record;
std::vector<std::string> data_strs; Record ProcessALine(const std::string& line)
split(columns[0], ' ', &data_strs); {
for (auto& d : data_strs) { std::cout << "process a line" << std::endl;
record.data.push_back(std::stof(d)); std::vector<std::string> columns;
} split(line, '\t', &columns);
assert(columns.size() == 2UL, "data format error, should be <data>\t<shape>");
std::vector<std::string> shape_strs;
split(columns[1], ' ', &shape_strs); Record record;
for (auto& s : shape_strs) { std::vector<std::string> data_strs;
record.shape.push_back(std::stoi(s)); split(columns[0], ' ', &data_strs);
} //将数据字符串转换为整型数据并放到record.data中
std::cout << "data size " << record.data.size() << std::endl; for (auto& d : data_strs)
std::cout << "data shape size " << record.shape.size() << std::endl; {
return record; record.data.push_back(std::stof(d));
} }
void CheckOutput(const std::string& referfile, const PaddleTensor& output) { std::vector<std::string> shape_strs;
std::string line; split(columns[1], ' ', &shape_strs);
std::ifstream file(referfile); for (auto& s : shape_strs)
std::getline(file, line); {
auto refer = ProcessALine(line); record.shape.push_back(std::stoi(s));
file.close(); }
std::cout << "data size " << record.data.size() << std::endl;
size_t numel = output.data.length() / PaddleDtypeSize(output.dtype); std::cout << "data shape size " << record.shape.size() << std::endl;
std::cout << "predictor output numel " << numel << std::endl; return record;
std::cout << "reference output numel " << refer.data.size() << std::endl; }
assert(numel == refer.data.size());
switch (output.dtype) { void CheckOutput(const std::string& referfile, const PaddleTensor& output)
case PaddleDType::INT64: { {
for (size_t i = 0; i < numel; ++i) { std::string line;
assert(static_cast<int64_t*>(output.data.data())[i] == std::ifstream file(referfile);
refer.data[i]); std::getline(file, line);
} auto refer = ProcessALine(line);
break; file.close();
}
case PaddleDType::FLOAT32: size_t numel = output.data.length() / PaddleDtypeSize(output.dtype);
for (size_t i = 0; i < numel; ++i) { std::cout << "predictor output numel " << numel << std::endl;
assert( std::cout << "reference output numel " << refer.data.size() << std::endl;
fabs(static_cast<float*>(output.data.data())[i] - refer.data[i]) <= assert(numel == refer.data.size());
1e-5); switch (output.dtype)
} {
break; case PaddleDType::INT64:
} for (size_t i = 0; i < numel; ++i)
} {
assert(static_cast<int64_t*>(output.data.data())[i] == refer.data[i]);
/* }
* Use the native fluid engine to inference the demo. break;
*/ case PaddleDType::FLOAT32:
void Main(bool use_gpu) { for (size_t i = 0; i < numel; ++i)
NativeConfig config; {
config.param_file = MODELDIR + "/__params__"; assert(fabs(static_cast<float*>(output.data.data())[i] - refer.data[i]) <= 1e-5);
config.prog_file = MODELDIR + "/__model__"; }
config.use_gpu = USE_GPU; break;
config.device = 0; }
if (USE_GPU) { }
config.fraction_of_gpu_memory = 0.1f; // set by yourself
} /*
std::cout << ToString(config) << std::endl; * Use the native fluid engine to inference the demo.
std::cout << "init predictor" << std::endl; */
auto predictor = void Main(bool use_gpu)
CreatePaddlePredictor<NativeConfig, PaddleEngineKind::kNative>(config); {
NativeConfig config;
std::cout << "begin to process data" << std::endl; config.model_dir = MODELDIR;
// Just a single batch of data. //config.param_file = MODELDIR + "/__params__";
std::string line; //config.prog_file = MODELDIR + "/__model__";
std::cout << "data : " << std::endl; config.use_gpu = USE_GPU;
std::ifstream file(DATA); config.device = 0;
if(!file.is_open()) { if (USE_GPU)
std::cout << "failed open data" << DATA << std::endl; {
exit(0); config.fraction_of_gpu_memory = 0.1f; // set by yourself
} }
std::getline(file, line); std::cout << ToString(config) << std::endl;
auto record = ProcessALine(line); std::cout << "init predictor" << std::endl;
file.close(); auto predictor = CreatePaddlePredictor<NativeConfig, PaddleEngineKind::kNative>(config);
// Inference. std::cout << "begin to process data" << std::endl;
PaddleTensor input; // Just a single batch of data.
input.shape = record.shape; std::string line;
input.data = std::cout << "data : " << std::endl;
PaddleBuf(record.data.data(), record.data.size() * sizeof(float)); std::ifstream file(DATA);
input.dtype = PaddleDType::FLOAT32; if (!file.is_open())
{
std::cout << "run executor" << std::endl; std::cout << "failed open data" << DATA << std::endl;
std::vector<PaddleTensor> output; exit(0);
predictor->Run({input}, &output); }
std::getline(file, line);
std::cout << "output.size " << output.size() << std::endl; auto record = ProcessALine(line);
auto& tensor = output.front(); file.close();
std::cout << "output: " << SummaryTensor(tensor) << std::endl;
// Inference.
// compare with reference result PaddleTensor input;
std::cout << "refer result : " << REFER << std::endl; input.shape = record.shape;
CheckOutput(REFER, tensor); input.data =
PaddleBuf(record.data.data(), record.data.size() * sizeof(float));
input.dtype = PaddleDType::FLOAT32;
std::cout << "run executor" << std::endl;
std::vector<PaddleTensor> output;
predictor->Run({ input }, &output);
std::cout << "output.size " << output.size() << std::endl;
auto& tensor = output.front();
std::cout << "output: " << SummaryTensor(tensor) << std::endl;
// compare with reference result
std::cout << "refer result : " << REFER << std::endl;
CheckOutput(REFER, tensor);
}
}
} }
int main(int argc, char** argv)
{
MODELDIR = "./LB_icnet_model";
//DATA = "./icnet_image.txt";
DATA = "./1.png.txt";
REFER = "./icnet_label.txt";
paddle::demo::Main(USE_GPU);
} // namespace demo system("pause");
} // namespace paddle return 0;
int main(int argc, char** argv) {
// ParseArgs();
MODELDIR = "./mobilenet/model";
DATA = "./mobilenet/data.txt";
REFER = "./mobilenet/result.txt";
USE_GPU = true;
paddle::demo::Main(false /* USE_GPU*/);
if (USE_GPU) {
paddle::demo::Main(true /*USE_GPU*/);
}
system("pause");
return 0;
} }
// Copyright (c) 2018 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 <chrono>
#include <iostream>
#include <fstream>
#include "paddle/fluid/inference/api/paddle_inference_api.h"
namespace paddle {
std::string DIRNAME = "./LB_icnet_model";
//std::string DIRNAME = "./infer_models";
NativeConfig GetConfig() {
NativeConfig config;
config.prog_file=DIRNAME + "/__model__";
config.param_file=DIRNAME + "/__params__";
config.fraction_of_gpu_memory = 0.8;
config.use_gpu = true;
config.device = 0;
return config;
}
using Time = decltype(std::chrono::high_resolution_clock::now());
Time time() { return std::chrono::high_resolution_clock::now(); };
double time_diff(Time t1, Time t2) {
typedef std::chrono::microseconds ms;
auto diff = t2 - t1;
ms counter = std::chrono::duration_cast<ms>(diff);
return counter.count() / 1000.0;
}
void test_naive(int batch_size){
NativeConfig config = GetConfig();
// config.model_dir = model_path;
auto predictor = CreatePaddlePredictor<NativeConfig>(config);
int height = 449;
int width = 581;
//int height = 3;
//int width = 3;
int num_sum = height * width * 3 * batch_size;
std::vector<float> data;
for(int i = 0; i < num_sum; i++) {
data.push_back(0.0);
}
PaddleTensor tensor;
tensor.shape = std::vector<int>({batch_size, 3, height, width});
tensor.data.Resize(sizeof(float) * batch_size * 3 * height * width);
std::copy(data.begin(), data.end(), static_cast<float*>(tensor.data.data()));
tensor.dtype = PaddleDType::FLOAT32;
std::vector<PaddleTensor> paddle_tensor_feeds(1, tensor);
PaddleTensor tensor_out;
std::vector<PaddleTensor> outputs(1, tensor_out);
predictor->Run(paddle_tensor_feeds, &outputs, batch_size);
std::cout << "start predict123:" << std::endl;
auto time1 = time();
for(size_t i = 0; i < 2; i++) {
predictor->Run(paddle_tensor_feeds, &outputs, batch_size);
std::cout << "pass " << i;
}
auto time2 = time();
std::ofstream ofresult("naive_test_result.txt", std::ios::app);
std::cout <<"batch: " << batch_size << " predict cost: " << time_diff(time1, time2) / 100.0 << "ms" << std::endl;
std::cout << outputs.size() << std::endl;
/*
int64_t * data_o = static_cast<int64_t*>(outputs[0].data.data());
for (size_t j = 0; j < outputs[0].data.length() / sizeof(int64_t); ++j) {
ofresult << std::to_string(data_o[j]) << " ";
}
ofresult << std::endl;
ofresult.close();
*/
}
} // namespace paddle
int main(int argc, char** argv) {
paddle::test_naive(1 << 0);
return 0;
}
\ No newline at end of file
...@@ -133,6 +133,7 @@ void MainThreads(int num_threads, bool use_gpu) { ...@@ -133,6 +133,7 @@ void MainThreads(int num_threads, bool use_gpu) {
} // namespace paddle } // namespace paddle
int main(int argc, char** argv) { int main(int argc, char** argv) {
FLAGS_dirname = "./word2vec.inference.model";
google::ParseCommandLineFlags(&argc, &argv, true); google::ParseCommandLineFlags(&argc, &argv, true);
paddle::demo::Main(false /* use_gpu*/); paddle::demo::Main(false /* use_gpu*/);
paddle::demo::MainThreads(1, false /* use_gpu*/); paddle::demo::MainThreads(1, false /* use_gpu*/);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册