param_desc.cc 8.2 KB
Newer Older
Y
Yan Chunwei 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
// Copyright (c) 2019 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.

#include "lite/model_parser/naive_buffer/param_desc.h"
#include <string>
#include <vector>
#include "lite/model_parser/naive_buffer/naive_buffer_wrapper_helper.h"

namespace paddle {
namespace lite {
namespace naive_buffer {

Y
Yan Chunwei 已提交
24 25 26 27 28 29 30 31 32 33
std::string ParamDesc::Name() const {
  return desc_->GetField<StringBuilder>("name").data();
}

void ParamDesc::SetName(const std::string& name) {
  auto* build = desc_->GetMutableField<StringBuilder>("name");
  CHECK(build);
  build->set(name);
}

Y
Yan Chunwei 已提交
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 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 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 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 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
uint32_t ParamDesc::ModelVersion() const { return Version("model_version"); }

void ParamDesc::SetModelVersion(uint32_t version) {
  SetVersion("model_version", version);
}

uint32_t ParamDesc::TensorVersion() const { return Version("tensor_version"); }

void ParamDesc::SetTensorVersion(uint32_t version) {
  SetVersion("tensor_version", version);
}

uint64_t ParamDesc::LoDLevel() const {
  return desc_->GetField<UInt64Builder>("lod_level").data();
}

void ParamDesc::SetLoDLevel(uint64_t lod_level) {
  auto* build = desc_->GetMutableField<UInt64Builder>("lod_level");
  CHECK(build);
  build->set(lod_level);
}

std::vector<std::vector<uint64_t>> ParamDesc::LoD() const {
  using out_builder_type = ListBuilder<ListBuilder<UInt64Builder>>;

  std::vector<std::vector<uint64_t>> res;
  auto& out_builder = desc_->GetField<out_builder_type>("lod");
  for (size_t i = 0; i < out_builder.size(); ++i) {
    res.emplace_back(
        RepeatedToVector<uint64_t, UInt64Builder>(out_builder.Get(i)));
  }
  return res;
}

void ParamDesc::SetLoD(const std::vector<std::vector<uint64_t>>& lod) {
  using out_builder_type = ListBuilder<ListBuilder<UInt64Builder>>;

  auto* out_builder = desc_->GetMutableField<out_builder_type>("lod");
  CHECK(out_builder);
  out_builder->Clear();
  for (const auto& vals : lod) {
    VectorToRepeated<uint64_t, UInt64Builder>(vals, out_builder->New());
  }
}

VarDescAPI::VarDataType ParamDesc::GetDataType() const {
  using data_type_builder_t = EnumBuilder<proto::VarDataType>;

  auto data_type =
      GetTensorDesc().GetField<data_type_builder_t>("data_type").data();
#define GET_DATA_TYPE_CASE_ITEM(type__) \
  case proto::VarDataType::type__:      \
    return VarDescAPI::VarDataType::type__

  switch (data_type) {
    // Only support primary data type now.
    GET_DATA_TYPE_CASE_ITEM(UINT8);
    GET_DATA_TYPE_CASE_ITEM(INT8);
    GET_DATA_TYPE_CASE_ITEM(INT16);
    GET_DATA_TYPE_CASE_ITEM(INT32);
    GET_DATA_TYPE_CASE_ITEM(INT64);
    GET_DATA_TYPE_CASE_ITEM(FP32);
    GET_DATA_TYPE_CASE_ITEM(FP64);
    default:
      LOG(FATAL) << "Unknown var data type";
  }
#undef GET_DATA_TYPE_CASE_ITEM
}

void ParamDesc::SetDataType(VarDescAPI::VarDataType data_type) {
  using data_type_builder_t = EnumBuilder<proto::VarDataType>;

  auto* data_type_builder =
      GetMutableTensorDesc()->GetMutableField<data_type_builder_t>("data_type");
  CHECK(data_type_builder);
#define SET_DATA_TYPE_CASE_ITEM(type__)                 \
  case VarDescAPI::VarDataType::type__:                 \
    data_type_builder->set(proto::VarDataType::type__); \
    break

  switch (data_type) {
    // Only support primary data type now.
    SET_DATA_TYPE_CASE_ITEM(UINT8);
    SET_DATA_TYPE_CASE_ITEM(INT8);
    SET_DATA_TYPE_CASE_ITEM(INT16);
    SET_DATA_TYPE_CASE_ITEM(INT32);
    SET_DATA_TYPE_CASE_ITEM(INT64);
    SET_DATA_TYPE_CASE_ITEM(FP32);
    SET_DATA_TYPE_CASE_ITEM(FP64);
    default:
      LOG(FATAL) << "Unknown var data type";
#undef SET_DATA_TYPE_CASE_ITEM
  }
}

std::vector<int64_t> ParamDesc::Dim() const {
  using out_builder_type = ListBuilder<Int64Builder>;

  auto& out_builder = GetTensorDesc().GetField<out_builder_type>("dims");
  return RepeatedToVector<int64_t, Int64Builder>(out_builder);
}

void ParamDesc::SetDim(const std::vector<int64_t>& dim) {
  using out_builder_type = ListBuilder<Int64Builder>;

  auto* out_builder =
      GetMutableTensorDesc()->GetMutableField<out_builder_type>("dims");
  CHECK(out_builder);
  VectorToRepeated<int64_t, Int64Builder>(dim, out_builder);
}

#define GET_DATA_IMPL(T, type__)                                            \
  template <>                                                               \
  std::vector<T> ParamDesc::Data() const {                                  \
    CHECK(GetDataType() == VarDescAPI::VarDataType::type__)                 \
        << "Data Type mismatch";                                            \
    std::vector<T> res;                                                     \
    auto& data_builder = desc_->GetField<ListBuilder<CharBuilder>>("data"); \
    auto data = RepeatedToVector<char, CharBuilder>(data_builder);          \
    size_t size = data.size() / sizeof(T);                                  \
    auto* data_ptr = reinterpret_cast<T*>(&data[0]);                        \
    for (size_t i = 0; i < size; ++i) {                                     \
      res.push_back(data_ptr[i]);                                           \
    }                                                                       \
    return res;                                                             \
  }
GET_DATA_IMPL(uint8_t, UINT8);
GET_DATA_IMPL(int8_t, INT8);
GET_DATA_IMPL(int16_t, INT16);
GET_DATA_IMPL(int32_t, INT32);
GET_DATA_IMPL(int64_t, INT64);
GET_DATA_IMPL(float, FP32);
GET_DATA_IMPL(double, FP64);
#undef GET_DATA_IMPL

// NOTE: Must set data type first
#define SET_DATA_COMMON_IMPL(T, type__, size__, data_ptr__)     \
  CHECK(GetDataType() == VarDescAPI::VarDataType::type__)       \
      << "Data Type mismatch, Call SetDataType first";          \
  auto* data_builder =                                          \
      desc_->GetMutableField<ListBuilder<CharBuilder>>("data"); \
  CHECK(data_builder);                                          \
  data_builder->Clear();                                        \
  size_t size = size__ * sizeof(T);                             \
  auto* data_ptr = reinterpret_cast<const char*>(data_ptr__);   \
  for (size_t i = 0; i < size; ++i) {                           \
    data_builder->New()->set(data_ptr[i]);                      \
  }

#define SET_DATA_IMPL(T, type__)                                \
  template <>                                                   \
  void ParamDesc::SetData<T>(const std::vector<T>& data) {      \
    SET_DATA_COMMON_IMPL(T, type__, data.size(), &data[0])      \
  }                                                             \
                                                                \
  template <>                                                   \
  void ParamDesc::SetData<T>(const T* data, size_t data_size) { \
    CHECK(data);                                                \
    SET_DATA_COMMON_IMPL(T, type__, data_size, data);           \
  }

SET_DATA_IMPL(uint8_t, UINT8);
SET_DATA_IMPL(int8_t, INT8);
SET_DATA_IMPL(int16_t, INT16);
SET_DATA_IMPL(int32_t, INT32);
SET_DATA_IMPL(int64_t, INT64);
SET_DATA_IMPL(float, FP32);
SET_DATA_IMPL(double, FP64);
#undef SET_DATA_IMPL
#undef SET_DATA_COMMON_IMPL

uint32_t ParamDesc::Version(const std::string& name) const {
  auto& builder = desc_->GetField<UInt32Builder>(name);
  return builder.data();
}

void ParamDesc::SetVersion(const std::string& name, uint32_t version) {
  auto* builder = desc_->GetMutableField<UInt32Builder>(name);
  CHECK(builder);
  return builder->set(version);
}

const proto::TensorDesc& ParamDesc::GetTensorDesc() const {
  return desc_->GetField<proto::TensorDesc>("tensor_desc");
}

proto::TensorDesc* ParamDesc::GetMutableTensorDesc() {
  auto* builder = desc_->GetMutableField<proto::TensorDesc>("tensor_desc");
  CHECK(builder);
  return builder;
}

}  // namespace naive_buffer
}  // namespace lite
}  // namespace paddle