BufferArg.h 6.5 KB
Newer Older
H
hedaoyuan 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.

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

#include <glog/logging.h>

#include "TensorShape.h"
#include "TensorType.h"
#include "paddle/math/CpuSparseMatrix.h"
#include "paddle/math/Matrix.h"
#include "paddle/math/SparseMatrix.h"

namespace paddle {

enum BufferType {
  TENSOR_NORMAL = 0,
  TENSOR_SEQUENCE_ID = 1,
  TENSOR_SEQUENCE_DATA = 2,
  TENSOR_SPARSE = 3
};

enum SparseDataType {
  SPARSE_NO_VALUE = 0,  // do not need value pointer, all values are 1
  SPARSE_FLOAT_VALUE = 1
};

enum SparseDataFormat { SPARSE_CSR_FORMAT = 0, SPARSE_CSC_FORMAT = 1 };

/**
 * BufferArg used as the argument type for Function.
 */
class BufferArg;
class SequenceArg;
class SparseMatrixArg;
typedef std::shared_ptr<BufferArg> BufferArgPtr;

// an array of arbitrary dimensions
class BufferArg {
public:
  BufferArg(void* buf, ValueType valueType, const TensorShape& shape)
      : buf_(buf), valueType_(valueType), shape_(shape) {}

  BufferArg(void* buf, ValueType valueType)
      : buf_(buf), valueType_(valueType) {}

  BufferArg(const Matrix& matrix)
H
hedaoyuan 已提交
59
      : buf_(reinterpret_cast<void*>(matrix.getData())),
H
hedaoyuan 已提交
60 61 62 63 64 65 66
        valueType_(DataType<real>::value),
        shape_(2) {
    shape_.setDim(0, matrix.getHeight());
    shape_.setDim(1, matrix.getWidth());
  }

  BufferArg(const Matrix& matrix, const TensorShape& shape)
H
hedaoyuan 已提交
67
      : buf_(reinterpret_cast<void*>(matrix.getData())),
H
hedaoyuan 已提交
68 69 70 71 72 73
        valueType_(DataType<real>::value),
        shape_(shape) {
    CHECK_EQ(matrix.getElementCnt(), shape.getElements());
  }

  BufferArg(const Vector& vector)
H
hedaoyuan 已提交
74
      : buf_(reinterpret_cast<void*>(vector.getData())),
H
hedaoyuan 已提交
75 76 77 78 79 80
        valueType_(DataType<real>::value),
        shape_(1) {
    shape_.setDim(0, vector.getSize());
  }

  BufferArg(const IVector& vector)
H
hedaoyuan 已提交
81 82 83
      : buf_(reinterpret_cast<void*>(vector.getData())),
        valueType_(VALUE_TYPE_INT32),
        shape_(1) {
H
hedaoyuan 已提交
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
    shape_.setDim(0, vector.getSize());
  }

  template <DeviceType DType>
  typename Tensor<real, DType>::Matrix matrix() const {
    CHECK(buf_);
    CHECK(valueType_ == DataType<real>::value);
    // CHECK(deviceType_ == DType);
    CHECK_EQ(2, shape_.ndims());
    return typename Tensor<real, DType>::Matrix(
        reinterpret_cast<real*>(buf_), shape_[0], shape_[1]);
  }

  template <typename VType, DeviceType DType>
  typename Tensor<VType, DType>::Vector vector() const {
    CHECK(buf_);
    CHECK(valueType_ == DataType<VType>::value);
    // CHECK(deviceType_ == DType);
    CHECK_EQ(1, shape_.ndims());
    return typename Tensor<VType, DType>::Vector(
        shape_[0], reinterpret_cast<VType*>(buf_));
  }

  virtual ~BufferArg() {}

  template <typename T>
  T* data() const {
    return reinterpret_cast<T*>(buf_);
  }

  void* data() const { return buf_; }
  ValueType valueType() const { return valueType_; }
  BufferType bufferType() const { return bufferType_; }
  const TensorShape& shape() const { return shape_; }

  const SequenceArg& sequence() const;
  const SparseMatrixArg& sparse() const;

protected:
  void* buf_;
  ValueType valueType_;
  TensorShape shape_;
  BufferType bufferType_;
  // leading dimensions. The size is dims_.size()
  // Dims lds_;
};

// sequence start positions in a mini-batch of sequences
// shape_.ndims() == 1
// valueType_ = int32
H
hedaoyuan 已提交
134
// if a < b then value_.buf_[a] < value_.buf_[b]
H
hedaoyuan 已提交
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 201 202 203 204 205 206 207
class SequenceIdArg : public BufferArg {
public:
  SequenceIdArg(void* buf, const TensorShape& shape)
      : BufferArg(buf, VALUE_TYPE_INT32, shape) {
    CHECK_EQ(shape_.ndims(), 1);
    numSeqs_ = shape_[0] - 1;
  }

  SequenceIdArg(const IVector& vector) : BufferArg(vector) {
    numSeqs_ = shape_[0] - 1;
  }

  ~SequenceIdArg() {}

  size_t numSeqs() const { return numSeqs_; }

private:
  size_t numSeqs_;
};

// sequence data
class SequenceArg : public BufferArg {
public:
  SequenceArg(void* buf,
              ValueType valueType,
              const TensorShape& shape,
              const SequenceIdArg& startPositions)
      : BufferArg(buf, valueType, shape), startPositions_(startPositions) {}

  SequenceArg(const Matrix& matrix, const IVector& vector)
      : BufferArg(matrix), startPositions_(vector) {}

  ~SequenceArg() {}

  void* getIdBuf() const { return startPositions_.data(); }
  size_t numSeqs() const { return startPositions_.numSeqs(); }

private:
  SequenceIdArg startPositions_;
};

// sparse matrix
// valueType_ == float or double
// shape_.ndims() == 2
class SparseMatrixArg : public BufferArg {
public:
  SparseMatrixArg(void* buf,
                  ValueType valueType,
                  const TensorShape& shape,
                  const BufferArg& row,
                  const BufferArg& col,
                  size_t nnz,
                  SparseDataFormat format,
                  SparseDataType type)
      : BufferArg(buf, valueType, shape),
        row_(row),
        col_(col),
        nnz_(nnz),
        format_(format),
        type_(type) {
    CHECK((valueType == VALUE_TYPE_FLOAT) || (valueType == VALUE_TYPE_DOUBLE));
    CHECK_EQ(shape_.ndims(), 2);
    CHECK_EQ(row_.shape().ndims(), 1);
    CHECK_EQ(col_.shape().ndims(), 1);
    if (format == SPARSE_CSR_FORMAT) {
      CHECK_EQ(nnz, col.shape()[0]);
    } else if (format == SPARSE_CSC_FORMAT) {
      CHECK_EQ(nnz, row.shape()[0]);
    }
  }

  SparseMatrixArg(const CpuSparseMatrix& sparse)
      : BufferArg(sparse),
H
hedaoyuan 已提交
208 209
        row_(reinterpret_cast<void*>(sparse.getRows()), VALUE_TYPE_INT32),
        col_(reinterpret_cast<void*>(sparse.getCols()), VALUE_TYPE_INT32) {}
H
hedaoyuan 已提交
210 211 212

  SparseMatrixArg(const GpuSparseMatrix& sparse)
      : BufferArg(sparse),
H
hedaoyuan 已提交
213 214
        row_(reinterpret_cast<void*>(sparse.getRows()), VALUE_TYPE_INT32),
        col_(reinterpret_cast<void*>(sparse.getCols()), VALUE_TYPE_INT32) {}
H
hedaoyuan 已提交
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236

  ~SparseMatrixArg() {}

  void* getRowBuf() const { return row_.data(); }

  void* getColBuf() const { return col_.data(); }

  size_t nnz() const { return nnz_; }

  SparseDataFormat dataFormat() const { return format_; }

  SparseDataType dataType() const { return type_; }

private:
  BufferArg row_;
  BufferArg col_;
  size_t nnz_;
  SparseDataFormat format_;
  SparseDataType type_;
};

}  // namespace paddle