Skip to content

Commit 00c7230

Browse files
committed
Merge branch 'develop' into all_data
2 parents 4c283d8 + 6de0a18 commit 00c7230

25 files changed

+147
-161
lines changed

CMakeLists.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,9 +213,11 @@ include(configure) # add paddle env configuration
213213
if(WITH_GPU)
214214
include(cuda)
215215
include(tensorrt)
216+
endif()
217+
if(WITH_MKL OR WITH_MKLML)
216218
include(external/anakin)
217219
elseif()
218-
set(WITH_ANAKIN OFF CACHE STRING "Anakin is used in GPU only now." FORCE)
220+
set(WITH_ANAKIN OFF CACHE STRING "Anakin is used in MKL only now." FORCE)
219221
endif()
220222

221223
include(generic) # simplify cmake module

cmake/external/anakin.cmake

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,6 @@ set(ANAKIN_LIBRARY ${ANAKIN_INSTALL_DIR})
1616
set(ANAKIN_SHARED_LIB ${ANAKIN_LIBRARY}/libanakin.so)
1717
set(ANAKIN_SABER_LIB ${ANAKIN_LIBRARY}/libanakin_saber_common.so)
1818

19-
# TODO(luotao): ANAKIN_MODLE_URL etc will move to demo ci later.
20-
set(INFERENCE_URL "http://paddle-inference-dist.bj.bcebos.com")
21-
set(ANAKIN_MODLE_URL "${INFERENCE_URL}/mobilenet_v2.anakin.bin")
22-
set(ANAKIN_RNN_MODLE_URL "${INFERENCE_URL}/anakin_test%2Fditu_rnn.anakin2.model.bin")
23-
set(ANAKIN_RNN_DATA_URL "${INFERENCE_URL}/anakin_test%2Fditu_rnn_data.txt")
24-
execute_process(COMMAND bash -c "mkdir -p ${ANAKIN_SOURCE_DIR}")
25-
execute_process(COMMAND bash -c "cd ${ANAKIN_SOURCE_DIR}; wget -q --no-check-certificate ${ANAKIN_MODLE_URL} -N")
26-
execute_process(COMMAND bash -c "cd ${ANAKIN_SOURCE_DIR}; wget -q --no-check-certificate ${ANAKIN_RNN_MODLE_URL} -N")
27-
execute_process(COMMAND bash -c "cd ${ANAKIN_SOURCE_DIR}; wget -q --no-check-certificate ${ANAKIN_RNN_DATA_URL} -N")
28-
2919
include_directories(${ANAKIN_INCLUDE})
3020
include_directories(${ANAKIN_INCLUDE}/saber/)
3121
include_directories(${ANAKIN_INCLUDE}/saber/core/)
@@ -48,6 +38,11 @@ set(ANAKIN_COMPILE_EXTRA_FLAGS
4838
-Wno-reorder
4939
-Wno-error=cpp)
5040

41+
if(WITH_GPU)
42+
set(CMAKE_ARGS_PREFIX -DUSE_GPU_PLACE=YES -DCUDNN_ROOT=${CUDNN_ROOT} -DCUDNN_INCLUDE_DIR=${CUDNN_INCLUDE_DIR})
43+
else()
44+
set(CMAKE_ARGS_PREFIX -DUSE_GPU_PLACE=NO)
45+
endif()
5146
ExternalProject_Add(
5247
extern_anakin
5348
${EXTERNAL_PROJECT_LOG_ARGS}
@@ -56,13 +51,11 @@ ExternalProject_Add(
5651
GIT_TAG "9424277cf9ae180a14aff09560d3cd60a49c76d2"
5752
PREFIX ${ANAKIN_SOURCE_DIR}
5853
UPDATE_COMMAND ""
59-
CMAKE_ARGS -DUSE_GPU_PLACE=YES
54+
CMAKE_ARGS ${CMAKE_ARGS_PREFIX}
6055
-DUSE_X86_PLACE=YES
6156
-DBUILD_WITH_UNIT_TEST=NO
6257
-DPROTOBUF_ROOT=${THIRD_PARTY_PATH}/install/protobuf
6358
-DMKLML_ROOT=${THIRD_PARTY_PATH}/install/mklml
64-
-DCUDNN_ROOT=${CUDNN_ROOT}
65-
-DCUDNN_INCLUDE_DIR=${CUDNN_INCLUDE_DIR}
6659
-DENABLE_OP_TIMER=${ANAKIN_ENABLE_OP_TIMER}
6760
${EXTERNAL_OPTIONAL_ARGS}
6861
CMAKE_CACHE_ARGS -DCMAKE_INSTALL_PREFIX:PATH=${ANAKIN_INSTALL_DIR}

cmake/inference_lib.cmake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ copy(memory_lib
145145
set(inference_deps paddle_fluid_shared paddle_fluid)
146146

147147
set(module "inference/api")
148-
if (WITH_ANAKIN AND WITH_GPU)
148+
if (WITH_ANAKIN AND WITH_MKL)
149149
copy(anakin_inference_lib DEPS paddle_inference_api inference_anakin_api
150150
SRCS
151151
${PADDLE_BINARY_DIR}/paddle/fluid/inference/api/libinference_anakin_api* # compiled anakin api

doc/fluid/api/layers.rst

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -822,6 +822,14 @@ pad
822822
.. autofunction:: paddle.fluid.layers.pad
823823
:noindex:
824824

825+
.. _api_fluid_layers_pad_constant_like:
826+
827+
pad_constant_like
828+
---
829+
830+
.. autofunction:: paddle.fluid.layers.pad_constant_like
831+
:noindex:
832+
825833
.. _api_fluid_layers_label_smooth:
826834

827835
label_smooth
@@ -1145,6 +1153,14 @@ sigmoid
11451153
.. autofunction:: paddle.fluid.layers.sigmoid
11461154
:noindex:
11471155

1156+
.. _api_fluid_layers_hsigmoid:
1157+
1158+
hsigmoid
1159+
-------
1160+
1161+
.. autofunction:: paddle.fluid.layers.hsigmoid
1162+
:noindex:
1163+
11481164
.. _api_fluid_layers_logsigmoid:
11491165

11501166
logsigmoid

paddle/fluid/inference/analysis/CMakeLists.txt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ cc_library(analysis SRCS pass_manager.cc node.cc data_flow_graph.cc graph_traits
66
analyzer.cc
77
helper.cc
88
# passes
9+
analysis_pass.cc
910
fluid_to_data_flow_graph_pass.cc
1011
data_flow_graph_to_fluid_pass.cc
1112
dfg_graphviz_draw_pass.cc
@@ -99,12 +100,17 @@ inference_analysis_test(test_analyzer_lac SRCS analyzer_lac_tester.cc
99100

100101

101102
set(TEXT_CLASSIFICATION_MODEL_URL "http://paddle-inference-dist.bj.bcebos.com/text-classification-Senta.tar.gz")
103+
set(TEXT_CLASSIFICATION_DATA_URL "http://paddle-inference-dist.bj.bcebos.com/text_classification_data.txt.tar.gz")
102104
set(TEXT_CLASSIFICATION_INSTALL_DIR "${THIRD_PARTY_PATH}/inference_demo/text_classification" CACHE PATH "Text Classification model and data root." FORCE)
103105

104106
if (NOT EXISTS ${TEXT_CLASSIFICATION_INSTALL_DIR} AND WITH_TESTING AND WITH_INFERENCE)
105107
inference_download_and_uncompress(${TEXT_CLASSIFICATION_INSTALL_DIR} ${TEXT_CLASSIFICATION_MODEL_URL} "text-classification-Senta.tar.gz")
108+
inference_download_and_uncompress(${TEXT_CLASSIFICATION_INSTALL_DIR} ${TEXT_CLASSIFICATION_DATA_URL} "text_classification_data.txt.tar.gz")
106109
endif()
107110

108111
inference_analysis_test(test_text_classification SRCS analyzer_text_classification_tester.cc
109112
EXTRA_DEPS paddle_inference_api paddle_fluid_api analysis_predictor
110-
ARGS --infer_model=${TEXT_CLASSIFICATION_INSTALL_DIR}/text-classification-Senta)
113+
ARGS --infer_model=${TEXT_CLASSIFICATION_INSTALL_DIR}/text-classification-Senta
114+
--infer_data=${TEXT_CLASSIFICATION_INSTALL_DIR}/data.txt
115+
--topn=1 # Just run top 1 batch.
116+
)

paddle/fluid/inference/analysis/pass.cc renamed to paddle/fluid/inference/analysis/analysis_pass.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,4 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
#include "paddle/fluid/inference/analysis/pass.h"
15+
#include "paddle/fluid/inference/analysis/analysis_pass.h"

paddle/fluid/inference/analysis/pass.h renamed to paddle/fluid/inference/analysis/analysis_pass.h

Lines changed: 8 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@ namespace paddle {
2828
namespace inference {
2929
namespace analysis {
3030

31-
class Pass {
31+
class AnalysisPass {
3232
public:
33-
Pass() = default;
34-
virtual ~Pass() = default;
33+
AnalysisPass() = default;
34+
virtual ~AnalysisPass() = default;
3535
// Mutable Pass.
3636
virtual bool Initialize(Argument *argument) { return false; }
3737
// Readonly Pass.
@@ -42,53 +42,25 @@ class Pass {
4242
virtual bool Finalize() { return false; }
4343

4444
// Get a Pass appropriate to print the Node this pass operates on.
45-
virtual Pass *CreatePrinterPass(std::ostream &os,
46-
const std::string &banner) const {
45+
virtual AnalysisPass *CreatePrinterPass(std::ostream &os,
46+
const std::string &banner) const {
4747
return nullptr;
4848
}
4949

5050
// Create a debugger Pass that draw the DFG by graphviz toolkit.
51-
virtual Pass *CreateGraphvizDebugerPass() const { return nullptr; }
51+
virtual AnalysisPass *CreateGraphvizDebugerPass() const { return nullptr; }
5252

53-
virtual void Run() { LOG(FATAL) << "not valid"; }
54-
// Run on a single Node.
55-
virtual void Run(Node *x) { LOG(FATAL) << "not valid"; }
56-
// Run on a single Function.
57-
virtual void Run(Function *x) { LOG(FATAL) << "not valid"; }
58-
// Run on a single FunctionBlock.
59-
virtual void Run(FunctionBlock *x) { LOG(FATAL) << "not valid"; }
6053
// Run on a single DataFlowGraph.
61-
virtual void Run(DataFlowGraph *x) { LOG(FATAL) << "not valid"; }
54+
virtual void Run(DataFlowGraph *x) = 0;
6255

6356
// Human-readable short representation.
6457
virtual std::string repr() const = 0;
6558
// Human-readable long description.
6659
virtual std::string description() const { return "No DOC"; }
6760
};
6861

69-
// NodePass process on any Node types.
70-
class NodePass : public Pass {
71-
public:
72-
virtual void Run(Node *node) = 0;
73-
};
74-
75-
// NodePass process on any Function node types.
76-
class FunctionPass : public Pass {
77-
public:
78-
virtual void Run(Function *node) = 0;
79-
};
80-
81-
// NodePass process on any FunctionBlock node types.
82-
class FunctionBlockPass : public Pass {
83-
public:
84-
virtual void Run(FunctionBlock *node) = 0;
85-
};
86-
8762
// GraphPass processes on any GraphType.
88-
class DataFlowGraphPass : public Pass {
89-
public:
90-
virtual void Run(DataFlowGraph *graph) = 0;
91-
};
63+
class DataFlowGraphPass : public AnalysisPass {};
9264

9365
} // namespace analysis
9466
} // namespace inference

paddle/fluid/inference/analysis/analyzer.cc

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "paddle/fluid/inference/analysis/analyzer.h"
1616
#include <string>
1717
#include <vector>
18+
1819
#include "paddle/fluid/inference/analysis/data_flow_graph_to_fluid_pass.h"
1920
#include "paddle/fluid/inference/analysis/dfg_graphviz_draw_pass.h"
2021
#include "paddle/fluid/inference/analysis/fluid_to_data_flow_graph_pass.h"
@@ -58,7 +59,7 @@ class DfgPassManagerImpl final : public DfgPassManager {
5859
std::string description() const override { return "DFG pass manager."; }
5960

6061
private:
61-
void AddPass(const std::string& name, Pass* pass) {
62+
void AddPass(const std::string& name, AnalysisPass* pass) {
6263
VLOG(3) << "Adding pass " << name;
6364
Register(name, pass);
6465
AddGraphvizDebugerPass(pass);
@@ -87,7 +88,7 @@ class DfgPassManagerImpl final : public DfgPassManager {
8788
}
8889

8990
// Add the graphviz debuger pass if the parent pass has one.
90-
void AddGraphvizDebugerPass(Pass* pass) {
91+
void AddGraphvizDebugerPass(AnalysisPass* pass) {
9192
auto* debuger_pass = pass->CreateGraphvizDebugerPass();
9293
if (debuger_pass) {
9394
Register(debuger_pass->repr(), debuger_pass);

paddle/fluid/inference/analysis/analyzer.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,11 @@ limitations under the License. */
3636
*/
3737

3838
#include <gflags/gflags.h>
39+
#include <string>
40+
#include <vector>
41+
42+
#include "paddle/fluid/inference/analysis/analysis_pass.h"
3943
#include "paddle/fluid/inference/analysis/flags.h"
40-
#include "paddle/fluid/inference/analysis/pass.h"
4144
#include "paddle/fluid/inference/analysis/pass_manager.h"
4245

4346
namespace paddle {

paddle/fluid/inference/analysis/analyzer_text_classification_tester.cc

Lines changed: 46 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include <gflags/gflags.h>
1717
#include <glog/logging.h> // use glog instead of PADDLE_ENFORCE to avoid importing other paddle header files.
1818
#include <gtest/gtest.h>
19+
#include <fstream>
1920
#include "paddle/fluid/framework/ir/pass.h"
2021
#include "paddle/fluid/inference/analysis/ut_helper.h"
2122
#include "paddle/fluid/inference/api/helper.h"
@@ -27,43 +28,70 @@ DEFINE_string(infer_model, "", "Directory of the inference model.");
2728
DEFINE_string(infer_data, "", "Path of the dataset.");
2829
DEFINE_int32(batch_size, 1, "batch size.");
2930
DEFINE_int32(repeat, 1, "How many times to repeat run.");
31+
DEFINE_int32(topn, -1, "Run top n batches of data to save time");
3032

3133
namespace paddle {
3234
namespace inference {
3335

34-
void Main(int batch_size) {
35-
// Three sequence inputs.
36-
std::vector<PaddleTensor> input_slots(1);
37-
// one batch starts
38-
// data --
39-
int64_t data0[] = {0, 1, 2};
40-
for (auto &input : input_slots) {
41-
input.data.Reset(data0, sizeof(data0));
42-
input.shape = std::vector<int>({3, 1});
43-
// dtype --
44-
input.dtype = PaddleDType::INT64;
45-
// LoD --
46-
input.lod = std::vector<std::vector<size_t>>({{0, 3}});
36+
struct DataReader {
37+
explicit DataReader(const std::string &path)
38+
: file(new std::ifstream(path)) {}
39+
40+
bool NextBatch(PaddleTensor *tensor, int batch_size) {
41+
PADDLE_ENFORCE_EQ(batch_size, 1);
42+
std::string line;
43+
tensor->lod.clear();
44+
tensor->lod.emplace_back(std::vector<size_t>({0}));
45+
std::vector<int64_t> data;
46+
47+
for (int i = 0; i < batch_size; i++) {
48+
if (!std::getline(*file, line)) return false;
49+
inference::split_to_int64(line, ' ', &data);
50+
}
51+
tensor->lod.front().push_back(data.size());
52+
53+
tensor->data.Resize(data.size() * sizeof(int64_t));
54+
memcpy(tensor->data.data(), data.data(), data.size() * sizeof(int64_t));
55+
tensor->shape.clear();
56+
tensor->shape.push_back(data.size());
57+
tensor->shape.push_back(1);
58+
return true;
4759
}
4860

61+
std::unique_ptr<std::ifstream> file;
62+
};
63+
64+
void Main(int batch_size) {
4965
// shape --
5066
// Create Predictor --
5167
AnalysisConfig config;
5268
config.model_dir = FLAGS_infer_model;
5369
config.use_gpu = false;
5470
config.enable_ir_optim = true;
55-
config.ir_passes.push_back("fc_lstm_fuse_pass");
5671
auto predictor =
5772
CreatePaddlePredictor<AnalysisConfig, PaddleEngineKind::kAnalysis>(
5873
config);
5974

75+
std::vector<PaddleTensor> input_slots(1);
76+
// one batch starts
77+
// data --
78+
auto &input = input_slots[0];
79+
input.dtype = PaddleDType::INT64;
80+
6081
inference::Timer timer;
6182
double sum = 0;
6283
std::vector<PaddleTensor> output_slots;
63-
for (int i = 0; i < FLAGS_repeat; i++) {
64-
timer.tic();
65-
CHECK(predictor->Run(input_slots, &output_slots));
66-
sum += timer.toc();
84+
85+
int num_batches = 0;
86+
for (int t = 0; t < FLAGS_repeat; t++) {
87+
DataReader reader(FLAGS_infer_data);
88+
while (reader.NextBatch(&input, FLAGS_batch_size)) {
89+
if (FLAGS_topn > 0 && num_batches > FLAGS_topn) break;
90+
timer.tic();
91+
CHECK(predictor->Run(input_slots, &output_slots));
92+
sum += timer.toc();
93+
++num_batches;
94+
}
6795
}
6896
PrintTime(batch_size, FLAGS_repeat, 1, 0, sum / FLAGS_repeat);
6997

0 commit comments

Comments
 (0)