Skip to content

Commit 2d84c6e

Browse files
committed
Remove SparseMatrix in mobile inference.
1 parent a5494fa commit 2d84c6e

File tree

6 files changed

+174
-0
lines changed

6 files changed

+174
-0
lines changed

paddle/capi/Matrix.cpp

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

8282
paddle_matrix paddle_matrix_create_sparse(
8383
uint64_t height, uint64_t width, uint64_t nnz, bool isBinary, bool useGpu) {
84+
#ifndef PADDLE_MOBILE_INFERENCE
8485
auto ptr = new paddle::capi::CMatrix();
8586
ptr->mat = paddle::Matrix::createSparseMatrix(
8687
height,
@@ -91,6 +92,9 @@ paddle_matrix paddle_matrix_create_sparse(
9192
false,
9293
useGpu);
9394
return ptr;
95+
#else
96+
return nullptr;
97+
#endif
9498
}
9599

96100
paddle_error paddle_matrix_sparse_copy_from(paddle_matrix mat,
@@ -100,6 +104,7 @@ paddle_error paddle_matrix_sparse_copy_from(paddle_matrix mat,
100104
uint64_t colSize,
101105
float* valueArray,
102106
uint64_t valueSize) {
107+
#ifndef PADDLE_MOBILE_INFERENCE
103108
if (mat == nullptr) return kPD_NULLPTR;
104109
auto ptr = cast(mat);
105110
if (rowArray == nullptr || colArray == nullptr ||
@@ -120,4 +125,7 @@ paddle_error paddle_matrix_sparse_copy_from(paddle_matrix mat,
120125
} else {
121126
return kPD_NOT_SUPPORTED;
122127
}
128+
#else
129+
return kPD_NOT_SUPPORTED;
130+
#endif
123131
}

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);
@@ -110,6 +111,7 @@ PD_API paddle_error paddle_matrix_get_shape(paddle_matrix mat,
110111
* NULL if the matrix is binary.
111112
* @param [in] valueSize length of value array. Zero if the matrix is binary.
112113
* @return paddle_error
114+
* @note Mobile inference does not support this interface.
113115
*/
114116
PD_API paddle_error paddle_matrix_sparse_copy_from(paddle_matrix mat,
115117
int* rowArray,

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

paddle/math/SparseMatrix.h

Lines changed: 47 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 "CpuSparseMatrix.h"
1821
#include "Matrix.h"
@@ -237,3 +240,47 @@ class GpuSparseMatrix : public Matrix {
237240
};
238241

239242
} // namespace paddle
243+
244+
#else
245+
246+
#include "CpuSparseMatrix.h"
247+
248+
namespace paddle {
249+
250+
class GpuSparseMatrix : public Matrix {
251+
public:
252+
GpuSparseMatrix(size_t height,
253+
size_t width,
254+
size_t nnz, /* used to allocate space */
255+
SparseValueType valueType = FLOAT_VALUE,
256+
SparseFormat format_ = SPARSE_CSR,
257+
bool trans = false)
258+
: Matrix(NULL, height, width, trans, false) {}
259+
260+
GpuSparseMatrix(real* value,
261+
int* rows,
262+
int* cols,
263+
size_t height,
264+
size_t width,
265+
size_t nnz,
266+
SparseValueType valueType,
267+
SparseFormat format,
268+
bool trans)
269+
: Matrix(NULL, height, width, trans, true) {}
270+
271+
void resize(size_t newHeight,
272+
size_t newWidth,
273+
size_t newNnz, /* used to allocate space */
274+
SparseValueType valueType,
275+
SparseFormat format) {}
276+
void resize(size_t newHeight, size_t newWidth) {}
277+
MatrixPtr getTranspose() { return nullptr; }
278+
void setRow(size_t row,
279+
size_t colNum,
280+
const unsigned int* cols,
281+
const real* values) {}
282+
};
283+
284+
} // namespace paddle
285+
286+
#endif

0 commit comments

Comments
 (0)