TensorType.h 3.1 KB
Newer Older
1
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

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

X
Xin Pan 已提交
17
#include "paddle/legacy/math/Matrix.h"
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33

namespace paddle {

enum ValueType {
  VALUE_TYPE_INT32 = 0,
  VALUE_TYPE_FLOAT = 1,
  VALUE_TYPE_DOUBLE = 2,
  VALUE_TYPE_BYTE = 3
};

enum DeviceType {
  DEVICE_TYPE_UNSPECIFIED = 0,
  DEVICE_TYPE_CPU = 1,
  DEVICE_TYPE_GPU = 2
};

34 35 36 37
enum SparseDataType { T_NO_VALUE = 0, T_FLOAT_VALUE = 1 };

enum SparseDataFormat { T_SPARSE_CSR = 0, T_SPARSE_CSC = 1 };

38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
inline int sizeOfValuType(ValueType valueType) {
  if (valueType == VALUE_TYPE_INT32) {
    return 4;
  } else if (valueType == VALUE_TYPE_FLOAT) {
    return 4;
  } else if (valueType == VALUE_TYPE_DOUBLE) {
    return 8;
  } else {
    LOG(FATAL) << "Unknown type: " << valueType;
    return 0;
  }
}

template <typename T>
struct DataType;

template <>
struct DataType<float> {
  static const ValueType value = VALUE_TYPE_FLOAT;
};

template <>
struct DataType<double> {
  static const ValueType value = VALUE_TYPE_DOUBLE;
};

H
hedaoyuan 已提交
64 65 66 67 68
template <>
struct DataType<int> {
  static const ValueType value = VALUE_TYPE_INT32;
};

H
hedaoyuan 已提交
69
namespace detail {
70

H
hedaoyuan 已提交
71 72
template <typename VType, DeviceType Device>
struct MatrixT;
73

H
hedaoyuan 已提交
74 75 76 77
template <>
struct MatrixT<real, DEVICE_TYPE_CPU> {
  using type = CpuMatrix;
};
78

H
hedaoyuan 已提交
79 80 81 82
template <>
struct MatrixT<real, DEVICE_TYPE_GPU> {
  using type = GpuMatrix;
};
83

H
hedaoyuan 已提交
84 85 86 87
template <>
struct MatrixT<int, DEVICE_TYPE_CPU> {
  using type = void;  // Not implemented
};
88

H
hedaoyuan 已提交
89 90 91 92 93
template <>
struct MatrixT<int, DEVICE_TYPE_GPU> {
  using type = void;  // Not implemented
};

94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
template <typename VType, DeviceType Device>
struct SparseMatrixT;

template <>
struct SparseMatrixT<real, DEVICE_TYPE_CPU> {
  using type = CpuSparseMatrix;
};

template <>
struct SparseMatrixT<real, DEVICE_TYPE_GPU> {
  using type = GpuSparseMatrix;
};

template <>
struct SparseMatrixT<int, DEVICE_TYPE_CPU> {
  using type = void;  // Not implemented
};

template <>
struct SparseMatrixT<int, DEVICE_TYPE_GPU> {
  using type = void;  // Not implemented
};

H
hedaoyuan 已提交
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
template <typename VType, DeviceType Device>
struct VectorT;

template <>
struct VectorT<real, DEVICE_TYPE_CPU> {
  using type = CpuVector;
};

template <>
struct VectorT<real, DEVICE_TYPE_GPU> {
  using type = GpuVector;
};

template <>
struct VectorT<int, DEVICE_TYPE_CPU> {
  using type = CpuIVector;
};

template <>
struct VectorT<int, DEVICE_TYPE_GPU> {
  using type = GpuIVector;
};

}  // namespace detail
141

H
hedaoyuan 已提交
142 143 144
template <typename VType, DeviceType DType>
struct Tensor {
  typedef typename detail::VectorT<VType, DType>::type Vector;
145 146
  typedef typename detail::MatrixT<VType, DType>::type Matrix;
  typedef typename detail::SparseMatrixT<VType, DType>::type SparseMatrix;
147 148 149
};

}  // namespace paddle