SparseMatrix.h 8.1 KB
Newer Older
1
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.
Z
zhangjinchao01 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14 15

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. */

#pragma once
16 17 18

#ifndef PADDLE_MOBILE_INFERENCE

Z
zhangjinchao01 已提交
19 20
#include <cstddef>
#include "CpuSparseMatrix.h"
H
hedaoyuan 已提交
21
#include "Matrix.h"
Z
zhangjinchao01 已提交
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39

namespace paddle {

typedef std::shared_ptr<_hl_sparse_matrix_s> hl_sparse_matrix_s_ptr;

class GpuSparseMatrix : public Matrix {
public:
  MemoryHandlePtr sMemoryHandle_;
  int* rows_;
  int* cols_;
  real* value_;
  const char* end_; /* point to the end of sMemoryHandle_ */

  hl_sparse_matrix_s_ptr sMatrix_;
  SparseValueType valueType_;
  SparseFormat format_;

public:
40 41
  GpuSparseMatrix(size_t height,
                  size_t width,
Z
zhangjinchao01 已提交
42 43
                  size_t nnz, /* used to allocate space */
                  SparseValueType valueType = FLOAT_VALUE,
44 45
                  SparseFormat format_ = SPARSE_CSR,
                  bool trans = false);
Z
zhangjinchao01 已提交
46

47 48 49 50
  GpuSparseMatrix(GpuMemHandlePtr dataHandle,
                  hl_sparse_matrix_s_ptr sMatrix,
                  size_t height,
                  size_t width,
Z
zhangjinchao01 已提交
51 52
                  size_t nnz, /* used to allocate space */
                  SparseValueType valueType = FLOAT_VALUE,
53 54
                  SparseFormat format_ = SPARSE_CSR,
                  bool trans = false,
Z
zhangjinchao01 已提交
55 56
                  MemoryHandlePtr sMemoryHandle = NULL);

57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
  GpuSparseMatrix(real* value,
                  int* rows,
                  int* cols,
                  size_t height,
                  size_t width,
                  size_t nnz,
                  SparseValueType valueType,
                  SparseFormat format,
                  bool trans);

  GpuSparseMatrix(hl_sparse_matrix_s_ptr sMatrix,
                  size_t height,
                  size_t width,
                  size_t nnz,
                  SparseValueType valueType,
                  SparseFormat format,
                  bool trans,
                  MemoryHandlePtr sMemoryHandle);
Z
zhangjinchao01 已提交
75 76 77 78 79 80 81 82 83 84 85 86 87

protected:
  struct Element {
    int row;
    int col;
    real val;
    Element(int rowIn, int colIn, real valIn)
        : row(rowIn), col(colIn), val(valIn) {}
  };

public:
  ~GpuSparseMatrix() {}

88 89
  void resize(size_t newHeight,
              size_t newWidth,
Z
zhangjinchao01 已提交
90
              size_t newNnz, /* used to allocate space */
91 92
              SparseValueType valueType,
              SparseFormat format);
Z
zhangjinchao01 已提交
93 94 95 96 97 98 99

  void resize(size_t newHeight, size_t newWidth);

  void sparseResizeCSR();

  void sparseResizeCSC();

100 101 102
  void resizeCSR(size_t newHeight,
                 size_t newWidth,
                 size_t newNnz,
Z
zhangjinchao01 已提交
103 104
                 SparseValueType valueType);

105 106 107
  void resizeCSC(size_t newHeight,
                 size_t newWidth,
                 size_t newNnz,
Z
zhangjinchao01 已提交
108 109
                 SparseValueType valueType);

110
  void mul(const GpuMatrix& a, const GpuMatrix& b, real scaleAB, real scaleT);
Z
zhangjinchao01 已提交
111 112 113 114
  /// B = A , B.trans = !A.trans
  MatrixPtr getTranspose();

  /// B = A'
115
  void transpose(MatrixPtr& matTrans, bool memAlloc);
Z
zhangjinchao01 已提交
116 117 118 119 120 121 122 123 124 125 126 127 128 129

  void copyFrom(const Matrix& src);
  void copyFrom(const Matrix& src, hl_stream_t stream);
  void copyFromCSR(CpuSparseMatrix& src, hl_stream_t stream);
  void copyFromCSC(CpuSparseMatrix& src, hl_stream_t stream);

  void copyFrom(const IVector& src) { LOG(FATAL) << "not implemented"; }
  void copyFrom(const IVector& src, hl_stream_t stream) {
    LOG(FATAL) << "not implemented";
  }

  template <class T>
  void copyFrom(int64_t* ids, int64_t* indices, T* data, hl_stream_t stream);

130 131 132
  void setRow(size_t row,
              size_t colNum,
              const unsigned int* cols,
Z
zhangjinchao01 已提交
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
              const real* values);
  SparseValueType getValueType() const;
  SparseFormat getFormat() const { return format_; }

  const int* getRowCols(size_t x) const { return cols_ + rows_[x]; }
  const real* getRowValues(size_t x) const { return value_ + rows_[x]; }
  size_t getColNum(size_t x) const { return rows_[x + 1] - rows_[x]; }
  void print(std::ostream& os) const;

  /**
   * @brief only set value_ of FLOAT_VALUE sparse matrix to zero
   */
  void zeroMem();

  /**
   * @brief sparseMatrix += denseMatrix
   *
   * Named add3 just because add/add2 has been used in BaseMatrix.cu
   * and they are not virtual function.
   *
   * Only add value of same (row, col) index in dense matrix
   * and do not use others values.
   *
   * @param[in]  b   dense matrix
   */
  void add3(GpuMatrix* b);
  void add3(MatrixPtr b);

  /**
   * @brief sparseMatrix[i,j] += bias[j], (j is the col index of sparse matrix)
   *
   * @param[in]  b      bias, dense matrix and height = 1
   * @param[in]  scale  scale of b
   */
  void addBias(Matrix& b, real scale);

  /**
   * @brief return rows, which is gpu address
   */
  int* getRows() const {
    CHECK(sMatrix_.get()) << "sMatrix_ is NULL";
    return hl_sparse_matrix_get_rows(sMatrix_.get());
  }

  /**
   * @brief return cols, which is gpu address
   */
  int* getCols() const {
    CHECK(sMatrix_.get()) << "sMatrix_ is NULL";
    return hl_sparse_matrix_get_cols(sMatrix_.get());
  }

  /**
   * @brief return value, which is gpu address
   */
  real* getValue() const {
    CHECK(sMatrix_.get()) << "sMatrix_ is NULL";
    return hl_sparse_matrix_get_value(sMatrix_.get());
  }

  /**
   * @brief return value_ of sparse matrix
   *
   * Some times CpuSparseMatrix maybe Matrix,
   * if getValue, must dynamic_cast to CpuSparseMatrix,
   * getData is convenient to get value
   */
  real* getData() { return getValue(); }
201
  const real* getData() const { return getValue(); }
Z
zhangjinchao01 已提交
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220

  /**
   * @brief  Get top k value of each row in sparse matrix.
   *
   * Store the value in maxVal and theirs index in maxIds.
   * k = maxVal.width
   *
   * @param[out]  maxIds    index of top k
   * @param[out]  maxVal    value of top k
   */
  void rowMax(IVector& maxIds, Matrix& maxVal);

protected:
  void sparseResize();

  void copyRow(int offsets, size_t colNum, const sparse_non_value_t* row);
  void copyRow(int offsets, size_t colNum, const sparse_float_value_t* row);

public:
221
  void mul(const Matrix& a, const Matrix& b, real scaleAB, real scaleT);
Z
zhangjinchao01 已提交
222 223 224 225 226 227 228 229 230 231

  void copyFrom(CpuSparseMatrix& src, hl_stream_t stream);
  void copyFrom(GpuSparseMatrix& src, hl_stream_t stream);

  void trimFrom(const CpuSparseMatrix& src);
  void trimFromCSR(const CpuSparseMatrix& src);
  void trimFromCSC(const CpuSparseMatrix& src);

  // BaseMatrixT interface
public:
232
  bool isSparse() const { return true; }
Z
zhangjinchao01 已提交
233 234 235 236

private:
  using Matrix::mul;
  using Matrix::copyFrom;
H
hedaoyuan 已提交
237 238 239
  using Matrix::rowMax;
  using Matrix::print;
  using Matrix::subMatrix;
Z
zhangjinchao01 已提交
240 241 242
};

}  // namespace paddle
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286

#else

#include "CpuSparseMatrix.h"

namespace paddle {

class GpuSparseMatrix : public Matrix {
public:
  GpuSparseMatrix(size_t height,
                  size_t width,
                  size_t nnz, /* used to allocate space */
                  SparseValueType valueType = FLOAT_VALUE,
                  SparseFormat format_ = SPARSE_CSR,
                  bool trans = false)
      : Matrix(NULL, height, width, trans, false) {}

  GpuSparseMatrix(real* value,
                  int* rows,
                  int* cols,
                  size_t height,
                  size_t width,
                  size_t nnz,
                  SparseValueType valueType,
                  SparseFormat format,
                  bool trans)
      : Matrix(NULL, height, width, trans, true) {}

  void resize(size_t newHeight,
              size_t newWidth,
              size_t newNnz, /* used to allocate space */
              SparseValueType valueType,
              SparseFormat format) {}
  void resize(size_t newHeight, size_t newWidth) {}
  MatrixPtr getTranspose() { return nullptr; }
  void setRow(size_t row,
              size_t colNum,
              const unsigned int* cols,
              const real* values) {}
};

}  // namespace paddle

#endif