traits.h 4.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
// Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
//
// 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 <string>
#include <vector>
19 20
#include "lite/api/paddle_place.h"
#include "lite/utils/cp_logging.h"
21

22 23 24 25
#define LITE_MODEL_INTERFACE_NOT_IMPLEMENTED                \
  LOG(FATAL) << "This additional interface is temporarily " \
                "unavailable in flatbuffers read-only mode."

26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
namespace paddle {
namespace lite {

// The AttrType is used to make the proto::AttrType portable.
enum class OpAttrType {
  INT = 0,
  FLOAT = 1,
  STRING = 2,
  INTS = 3,
  FLOATS = 4,
  STRINGS = 5,
  BOOLEAN = 6,
  BOOLEANS = 7,
  BLOCK = 8,
  LONG = 9,
  BLOCKS = 10,
  LONGS = 11,
  UNK,
};

46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 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
enum class VarDataType {
  // Pod Types
  BOOL = 0,
  INT16,
  INT32,
  INT64,
  FP16,
  FP32,
  FP64,
  // Tensor<size_t> is used in C++.
  SIZE_T,
  UINT8,
  INT8,

  // Other types that may need additional descriptions
  LOD_TENSOR,
  SELECTED_ROWS,
  FEED_MINIBATCH,
  FETCH_LIST,
  STEP_SCOPES,
  LOD_RANK_TABLE,
  LOD_TENSOR_ARRAY,
  PLACE_LIST,
  READER,
  // Any runtime decided variable type is raw
  // raw variables should manage their own allocations
  // in operators like nccl_op
  RAW,
  TUPLE
};

inline VarDataType ConvertPrecisionType(lite_api::PrecisionType type) {
#define CASE(ptype, vtype)                \
  case lite_api::PrecisionType::k##ptype: \
    return lite::VarDataType::vtype;      \
    break
  switch (type) {
    CASE(Float, FP32);
    CASE(Int8, INT8);
    CASE(Int32, INT32);
    CASE(FP16, FP16);
    CASE(Bool, BOOL);
    CASE(Int64, INT64);
    CASE(Int16, INT16);
    default:
      LOG(FATAL) << "Illegal flatbuffer VarType.";
      return lite::VarDataType();
  }
#undef CASE
}

inline lite_api::PrecisionType ConvertPrecisionType(VarDataType type) {
#define CASE(ptype, vtype)                    \
  case lite::VarDataType::vtype:              \
    return lite_api::PrecisionType::k##ptype; \
    break
  switch (type) {
    CASE(Float, FP32);
    CASE(Int8, INT8);
    CASE(Int32, INT32);
    CASE(FP16, FP16);
    CASE(Bool, BOOL);
    CASE(Int64, INT64);
    CASE(Int16, INT16);
    default:
      LOG(FATAL) << "Illegal flatbuffer VarType.";
      return lite_api::PrecisionType();
  }
#undef CASE
}

117 118 119 120 121 122 123 124 125
struct Standard {};
struct Flatbuffers {};

template <typename T, typename U>
class VectorView;

template <typename T, typename U = Standard>
struct OpDataTypeTrait;

126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
#define ATTR_TYPE_TRAIT_IMPL(T, type__)                \
  template <typename U>                                \
  struct OpDataTypeTrait<type__, U> {                  \
    typedef type__ ET;                                 \
    typedef type__ RT;                                 \
    static constexpr OpAttrType AT{OpAttrType::T};     \
    static constexpr const char* ATN{#T};              \
  };                                                   \
  template <typename U>                                \
  constexpr OpAttrType OpDataTypeTrait<type__, U>::AT; \
  template <typename U>                                \
  constexpr const char* OpDataTypeTrait<type__, U>::ATN;
#define ATTR_VECTOR_TYPE_TRAIT_IMPL(T, type__)                      \
  template <typename U>                                             \
  struct OpDataTypeTrait<std::vector<type__>, U> {                  \
    typedef type__ ET;                                              \
    typedef VectorView<type__, U> RT;                               \
    static constexpr OpAttrType AT{OpAttrType::T};                  \
    static constexpr const char* ATN{#T};                           \
  };                                                                \
  template <typename U>                                             \
  constexpr OpAttrType OpDataTypeTrait<std::vector<type__>, U>::AT; \
  template <typename U>                                             \
  constexpr const char* OpDataTypeTrait<std::vector<type__>, U>::ATN;
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167

ATTR_TYPE_TRAIT_IMPL(BLOCK, int16_t);
ATTR_TYPE_TRAIT_IMPL(INT, int32_t);
ATTR_TYPE_TRAIT_IMPL(FLOAT, float);
ATTR_TYPE_TRAIT_IMPL(STRING, std::string);
ATTR_TYPE_TRAIT_IMPL(BOOLEAN, bool);
ATTR_TYPE_TRAIT_IMPL(LONG, int64_t);

ATTR_VECTOR_TYPE_TRAIT_IMPL(INTS, int32_t);
ATTR_VECTOR_TYPE_TRAIT_IMPL(FLOATS, float);
ATTR_VECTOR_TYPE_TRAIT_IMPL(STRINGS, std::string);
ATTR_VECTOR_TYPE_TRAIT_IMPL(LONGS, int64_t);

#undef ATTR_TYPE_TRAIT_IMPL
#undef ATTR_VECTOR_TYPE_TRAIT_IMPL

}  // namespace lite
}  // namespace paddle