Skip to content

Commit 562599e

Browse files
authored
Merge pull request #5252 from hedaoyuan/inference
Remove some code to reduce the size of the mobile inference library.
2 parents feaf1e2 + 4854a42 commit 562599e

File tree

17 files changed

+281
-9
lines changed

17 files changed

+281
-9
lines changed

paddle/capi/Matrix.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ paddle_error paddle_matrix_get_shape(paddle_matrix mat,
121121

122122
paddle_matrix paddle_matrix_create_sparse(
123123
uint64_t height, uint64_t width, uint64_t nnz, bool isBinary, bool useGpu) {
124+
#ifndef PADDLE_MOBILE_INFERENCE
124125
auto ptr = new paddle::capi::CMatrix();
125126
ptr->mat = paddle::Matrix::createSparseMatrix(
126127
height,
@@ -131,6 +132,9 @@ paddle_matrix paddle_matrix_create_sparse(
131132
false,
132133
useGpu);
133134
return ptr;
135+
#else
136+
return nullptr;
137+
#endif
134138
}
135139

136140
paddle_error paddle_matrix_sparse_copy_from(paddle_matrix mat,
@@ -140,6 +144,7 @@ paddle_error paddle_matrix_sparse_copy_from(paddle_matrix mat,
140144
uint64_t colSize,
141145
float* valueArray,
142146
uint64_t valueSize) {
147+
#ifndef PADDLE_MOBILE_INFERENCE
143148
if (mat == nullptr) return kPD_NULLPTR;
144149
auto ptr = cast(mat);
145150
if (rowArray == nullptr || colArray == nullptr ||
@@ -160,4 +165,7 @@ paddle_error paddle_matrix_sparse_copy_from(paddle_matrix mat,
160165
} else {
161166
return kPD_NOT_SUPPORTED;
162167
}
168+
#else
169+
return kPD_NOT_SUPPORTED;
170+
#endif
163171
}

paddle/capi/matrix.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ PD_API paddle_matrix paddle_matrix_create(uint64_t height,
4848
* @param isBinary is binary (either 1 or 0 in matrix) or not.
4949
* @param useGpu is using GPU or not.
5050
* @return paddle_matrix.
51+
* @note Mobile inference does not support this interface.
5152
*/
5253
PD_API paddle_matrix paddle_matrix_create_sparse(
5354
uint64_t height, uint64_t width, uint64_t nnz, bool isBinary, bool useGpu);
@@ -129,6 +130,7 @@ PD_API paddle_error paddle_matrix_get_shape(paddle_matrix mat,
129130
* NULL if the matrix is binary.
130131
* @param [in] valueSize length of value array. Zero if the matrix is binary.
131132
* @return paddle_error
133+
* @note Mobile inference does not support this interface.
132134
*/
133135
PD_API paddle_error paddle_matrix_sparse_copy_from(paddle_matrix mat,
134136
int* rowArray,

paddle/cuda/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@ if(WITH_GPU)
2727
set_source_files_properties(${CUDA_CXX_SOURCES}
2828
PROPERTIES COMPILE_FLAGS "-D__NVCC__")
2929
else()
30+
if (NOT MOBILE_INFERENCE)
3031
set(CUDA_CXX_SOURCES src/hl_warpctc_wrap.cc)
32+
endif()
3133
endif()
3234

3335
set(CUDA_CU_SOURCES

paddle/gserver/CMakeLists.txt

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,49 @@ if(MOBILE_INFERENCE)
8585
gradientmachines/GradientMachineMode.cpp
8686
gradientmachines/MultiGradientMachine.cpp)
8787

88-
# Remove useless layers
88+
# Remove layers that used in training
8989
list(REMOVE_ITEM GSERVER_SOURCES
90-
layers/RecurrentLayerGroup.cpp)
90+
layers/RecurrentLayerGroup.cpp
91+
layers/CostLayer.cpp
92+
layers/MultiBoxLossLayer.cpp
93+
layers/WarpCTCLayer.cpp
94+
layers/CTCLayer.cpp
95+
layers/LinearChainCTC.cpp
96+
layers/PrintLayer.cpp)
97+
list(REMOVE_ITEM GSERVER_SOURCES
98+
layers/OuterProdLayer.cpp
99+
layers/SumToOneNormLayer.cpp
100+
layers/ConvShiftLayer.cpp
101+
layers/InterpolationLayer.cpp
102+
layers/AgentLayer.cpp
103+
layers/DotMulOperator.cpp
104+
layers/GruStepLayer.cpp
105+
layers/LstmStepLayer.cpp
106+
layers/ConvexCombinationLayer.cpp
107+
layers/Conv3DLayer.cpp
108+
layers/DeConv3DLayer.cpp
109+
layers/CropLayer.cpp
110+
layers/CrossEntropyOverBeam.cpp
111+
layers/DataNormLayer.cpp
112+
layers/FeatureMapExpandLayer.cpp
113+
layers/HierarchicalSigmoidLayer.cpp
114+
layers/MultinomialSampler.cpp
115+
layers/NCELayer.cpp
116+
layers/KmaxSeqScoreLayer.cpp
117+
layers/MDLstmLayer.cpp
118+
layers/MultiplexLayer.cpp
119+
layers/PadLayer.cpp
120+
layers/Pool3DLayer.cpp
121+
layers/ResizeLayer.cpp
122+
layers/RotateLayer.cpp
123+
layers/RowConvLayer.cpp
124+
layers/RowL2NormLayer.cpp
125+
layers/SamplingIdLayer.cpp
126+
layers/ScaleShiftLayer.cpp
127+
layers/SelectiveFullyConnectedLayer.cpp
128+
layers/SpatialPyramidPoolLayer.cpp
129+
layers/BilinearInterpLayer.cpp
130+
layers/ClipLayer.cpp)
91131
endif()
92132

93133
if(WITH_GPU)

paddle/gserver/gradientmachines/NeuralNetwork.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ limitations under the License. */
1616

1717
#include "NeuralNetwork.h"
1818
#include "hl_gpu.h"
19-
#include "paddle/gserver/layers/AgentLayer.h"
2019
#include "paddle/utils/CustomStackTrace.h"
2120
#include "paddle/utils/Logging.h"
2221
#include "paddle/utils/Stat.h"
@@ -28,6 +27,7 @@ limitations under the License. */
2827
#ifndef PADDLE_MOBILE_INFERENCE
2928
#include "MultiNetwork.h"
3029
#include "RecurrentGradientMachine.h"
30+
#include "paddle/gserver/layers/AgentLayer.h"
3131
#endif
3232

3333
namespace paddle {
@@ -192,9 +192,11 @@ void NeuralNetwork::init(const ModelConfig& config,
192192
void NeuralNetwork::connect(LayerPtr agentLayer,
193193
LayerPtr realLayer,
194194
int height) {
195+
#ifndef PADDLE_MOBILE_INFERENCE
195196
AgentLayer* agent = dynamic_cast<AgentLayer*>(agentLayer.get());
196197
CHECK_NOTNULL(agent);
197198
agent->setRealLayer(realLayer, height);
199+
#endif
198200
}
199201

200202
void NeuralNetwork::connect(std::string agentLayerName,

paddle/gserver/layers/Layer.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ ClassRegistrar<Layer, LayerConfig> Layer::registrar_;
9898
LayerPtr Layer::create(const LayerConfig& config) {
9999
std::string type = config.type();
100100

101+
#ifndef PADDLE_MOBILE_INFERENCE
101102
// NOTE: As following types have illegal character '-',
102103
// they can not use REGISTER_LAYER to registrar.
103104
// Besides, to fit with old training models,
@@ -106,7 +107,6 @@ LayerPtr Layer::create(const LayerConfig& config) {
106107
return LayerPtr(new MultiClassCrossEntropy(config));
107108
else if (type == "rank-cost")
108109
return LayerPtr(new RankingCost(config));
109-
#ifndef PADDLE_MOBILE_INFERENCE
110110
else if (type == "auc-validation")
111111
return LayerPtr(new AucValidation(config));
112112
else if (type == "pnpair-validation")

paddle/gserver/tests/CMakeLists.txt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
# gserver pacakge unittests
22

33
add_simple_unittest(test_LinearChainCRF)
4-
add_simple_unittest(test_MultinomialSampler)
54
add_simple_unittest(test_RecurrentLayer)
65

6+
if(NOT MOBILE_INFERENCE)
7+
add_simple_unittest(test_MultinomialSampler)
8+
endif()
9+
710
function(gserver_test TARGET)
811
add_unittest_without_exec(${TARGET}
912
${TARGET}.cpp
@@ -49,7 +52,7 @@ if(WITH_PYTHON)
4952
endif()
5053

5154
############### test_WarpCTCLayer #######################
52-
if(NOT WITH_DOUBLE)
55+
if(NOT WITH_DOUBLE AND NOT MOBILE_INFERENCE)
5356
add_unittest_without_exec(test_WarpCTCLayer
5457
test_WarpCTCLayer.cpp)
5558

paddle/math/BaseMatrix.cu

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1902,5 +1902,52 @@ void BaseMatrixT<real>::sumOfProducts(BaseMatrixT& b,
19021902
}
19031903

19041904
template class BaseMatrixT<real>;
1905+
1906+
#ifndef PADDLE_MOBILE_INFERENCE
1907+
19051908
template class BaseMatrixT<int>;
1909+
1910+
#else
1911+
1912+
template <>
1913+
void BaseMatrixT<int>::zero() {
1914+
applyUnary(unary::Zero<int>());
1915+
}
1916+
1917+
template <>
1918+
void BaseMatrixT<int>::assign(int p) {
1919+
applyUnary(unary::Assign<int>(p));
1920+
}
1921+
1922+
template <>
1923+
void BaseMatrixT<int>::isEqualTo(BaseMatrixT& b, int value) {
1924+
applyBinary(binary::IsEqual<int>(value), b);
1925+
}
1926+
1927+
template <>
1928+
void BaseMatrixT<int>::neg() {
1929+
applyUnary(unary::Neg<int>());
1930+
}
1931+
1932+
template <>
1933+
void BaseMatrixT<int>::abs2() {
1934+
applyUnary(unary::Abs<int>());
1935+
}
1936+
1937+
template <>
1938+
void BaseMatrixT<int>::add(int p) {
1939+
applyUnary(unary::Add<int>(p));
1940+
}
1941+
1942+
template <>
1943+
void BaseMatrixT<int>::add(int p1, int p2) {
1944+
applyUnary(unary::Add2<int>(p1, p2));
1945+
}
1946+
1947+
template <>
1948+
void BaseMatrixT<int>::applyL1(int learningRate, int decayRate) {
1949+
applyUnary(unary::ApplyL1<int>(learningRate * decayRate));
1950+
}
1951+
1952+
#endif
19061953
} // namespace paddle

paddle/math/CMakeLists.txt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,19 @@ else()
2525
message(STATUS "Compile with MKLDNNMatrix")
2626
endif()
2727

28+
if(MOBILE_INFERENCE)
29+
list(REMOVE_ITEM MATH_SOURCES
30+
${CMAKE_CURRENT_SOURCE_DIR}/SIMDFunctions.cpp)
31+
# Remove sparse
32+
list(REMOVE_ITEM MATH_HEADERS
33+
${CMAKE_CURRENT_SOURCE_DIR}/CpuSparseMatrix.h
34+
${CMAKE_CURRENT_SOURCE_DIR}/SparseMatrix.h
35+
${CMAKE_CURRENT_SOURCE_DIR}/SparseRowMatrix.h)
36+
list(REMOVE_ITEM MATH_SOURCES
37+
${CMAKE_CURRENT_SOURCE_DIR}/CpuSparseMatrix.cpp
38+
${CMAKE_CURRENT_SOURCE_DIR}/SparseMatrix.cpp
39+
${CMAKE_CURRENT_SOURCE_DIR}/SparseRowMatrix.cpp)
40+
endif()
2841
set(MATH_SOURCES
2942
"${PADDLE_SOURCE_DIR}/paddle/math/BaseMatrix.cu"
3043
"${PADDLE_SOURCE_DIR}/paddle/math/TrainingAlgorithmOp.cu"

paddle/math/CpuSparseMatrix.h

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ See the License for the specific language governing permissions and
1313
limitations under the License. */
1414

1515
#pragma once
16+
17+
#ifndef PADDLE_MOBILE_INFERENCE
18+
1619
#include <cstddef>
1720
#include "Matrix.h"
1821

@@ -309,3 +312,57 @@ class CpuSparseMatrix : public Matrix {
309312
using Matrix::subMatrix;
310313
};
311314
} // namespace paddle
315+
316+
#else
317+
318+
#include "Matrix.h"
319+
320+
namespace paddle {
321+
322+
class CpuSparseMatrix : public Matrix {
323+
public:
324+
CpuSparseMatrix(size_t height,
325+
size_t width,
326+
size_t nnz, /* used to allocate space */
327+
SparseValueType valueType = FLOAT_VALUE,
328+
SparseFormat format = SPARSE_CSR,
329+
bool trans = false)
330+
: Matrix(NULL, height, width, trans, false) {}
331+
332+
CpuSparseMatrix(real* data,
333+
int* rows,
334+
int* cols,
335+
size_t height,
336+
size_t width,
337+
size_t nnz,
338+
SparseValueType valueType,
339+
SparseFormat format,
340+
bool trans)
341+
: Matrix(NULL, height, width, trans, false) {}
342+
343+
real* getValue() const { return nullptr; }
344+
size_t getColStartIdx(size_t i) const { return 0; }
345+
size_t getRowStartIdx(size_t i) const { return 0; }
346+
size_t getColNum(size_t i) const { return 0; }
347+
int* getRowCols(size_t i) const { return nullptr; }
348+
349+
CpuSparseMatrixPtr getTmpSparseMatrix(size_t height, size_t width) {
350+
return nullptr;
351+
}
352+
353+
void resize(size_t newHeight,
354+
size_t newWidth,
355+
size_t newNnz, /* used to allocate space */
356+
SparseValueType valueType,
357+
SparseFormat format) {}
358+
void resize(size_t newHeight, size_t newWidth) {}
359+
MatrixPtr getTranspose() { return nullptr; }
360+
void setRow(size_t row,
361+
size_t colNum,
362+
const unsigned int* cols,
363+
const real* values) {}
364+
};
365+
366+
} // namespace paddle
367+
368+
#endif

0 commit comments

Comments
 (0)