var_desc.cc 7.9 KB
Newer Older
F
fengjiayi 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/* 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. */

#include "paddle/framework/var_desc.h"
Y
Yu Yang 已提交
16
#include "paddle/platform/enforce.h"
F
fengjiayi 已提交
17 18 19 20

namespace paddle {
namespace framework {

Y
Yu Yang 已提交
21
proto::VarDesc::VarType VarDesc::GetType() const { return desc_.type(); }
Q
QI JUN 已提交
22

Y
Yu Yang 已提交
23
void VarDesc::SetType(proto::VarDesc::VarType type) { desc_.set_type(type); }
Q
QI JUN 已提交
24

Y
Yu Yang 已提交
25
void VarDesc::SetShape(const std::vector<int64_t> &dims) {
Y
Yu Yang 已提交
26
  VectorToRepeated(dims, mutable_tensor_desc()->mutable_dims());
F
fengjiayi 已提交
27 28
}

F
fengjiayi 已提交
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
void VarDesc::SetTensorDescNum(size_t num) {
  switch (desc_.type()) {
    case proto::VarDesc::READER: {
      auto *lod_tensors_ptr = desc_.mutable_reader()->mutable_lod_tensor();
      lod_tensors_ptr->Clear();
      for (size_t i = 0; i < num; ++i) {
        lod_tensors_ptr->Add();
      }
      return;
    } break;
    default:
      PADDLE_THROW(
          "Setting 'sub_tensor_number' is not supported by the type of var %s.",
          this->Name());
  }
}

size_t VarDesc::GetTensorDescNum() const {
  switch (desc_.type()) {
    case proto::VarDesc::READER:
      return desc_.reader().lod_tensor_size();
      break;
    default:
      PADDLE_THROW(
          "Getting 'sub_tensor_number' is not supported by the type of var %s.",
          this->Name());
  }
}

void VarDesc::SetShapes(
F
fengjiayi 已提交
59
    const std::vector<std::vector<int64_t>> &multiple_dims) {
F
fengjiayi 已提交
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
  PADDLE_ENFORCE_EQ(multiple_dims.size(), GetTensorDescNum(),
                    "The number of given shapes(%d) doesn't equal to the "
                    "number of sub tensor.",
                    multiple_dims.size(), GetTensorDescNum());
  std::vector<proto::TensorDesc *> tensors = mutable_tensor_descs();
  for (size_t i = 0; i < multiple_dims.size(); ++i) {
    VectorToRepeated(multiple_dims[i], tensors[i]->mutable_dims());
  }
}

std::vector<int64_t> VarDesc::GetShape() const {
  return RepeatedToVector(tensor_desc().dims());
}

std::vector<std::vector<int64_t>> VarDesc::GetShapes() const {
  std::vector<proto::TensorDesc> descs = tensor_descs();
  std::vector<std::vector<int64_t>> res;
  res.reserve(descs.size());
  for (const auto &tensor_desc : descs) {
    res.push_back(RepeatedToVector(tensor_desc.dims()));
  }
  return res;
}

Y
Yu Yang 已提交
84
void VarDesc::SetDataType(proto::DataType data_type) {
Y
Yu Yang 已提交
85
  mutable_tensor_desc()->set_data_type(data_type);
F
fengjiayi 已提交
86 87
}

F
fengjiayi 已提交
88 89 90 91 92 93 94 95 96 97
void VarDesc::SetDataTypes(
    const std::vector<proto::DataType> &multiple_data_type) {
  PADDLE_ENFORCE_EQ(multiple_data_type.size(), GetTensorDescNum(),
                    "The number of given data types(%d) doesn't equal to the "
                    "number of sub tensor.",
                    multiple_data_type.size(), GetTensorDescNum());
  std::vector<proto::TensorDesc *> tensor_descs = mutable_tensor_descs();
  for (size_t i = 0; i < multiple_data_type.size(); ++i) {
    tensor_descs[i]->set_data_type(multiple_data_type[i]);
  }
F
fengjiayi 已提交
98 99
}

Y
Yu Yang 已提交
100
proto::DataType VarDesc::GetDataType() const {
101 102
  return tensor_desc().data_type();
}
Y
Stash  
Yu Yang 已提交
103

F
fengjiayi 已提交
104 105 106 107 108 109 110 111 112 113
std::vector<proto::DataType> VarDesc::GetDataTypes() const {
  std::vector<proto::TensorDesc> descs = tensor_descs();
  std::vector<proto::DataType> res;
  res.reserve(descs.size());
  for (const auto &tensor_desc : descs) {
    res.push_back(tensor_desc.data_type());
  }
  return res;
}

Y
Yu Yang 已提交
114
void VarDesc::SetLoDLevel(int32_t lod_level) {
Y
Yu Yang 已提交
115
  switch (desc_.type()) {
116
    case proto::VarDesc::LOD_TENSOR:
Y
Yu Yang 已提交
117 118
      desc_.mutable_lod_tensor()->set_lod_level(lod_level);
      break;
119
    case proto::VarDesc::LOD_TENSOR_ARRAY:
Y
Yu Yang 已提交
120 121 122
      desc_.mutable_tensor_array()->set_lod_level(lod_level);
      break;
    default:
F
fengjiayi 已提交
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
      PADDLE_THROW(
          "Setting 'lod_level' is not supported by the type of var %s.",
          this->Name());
  }
}

void VarDesc::SetLoDLevels(const std::vector<int32_t> &multiple_lod_level) {
  PADDLE_ENFORCE_EQ(multiple_lod_level.size(), GetTensorDescNum(),
                    "The number of given data types(%d) doesn't equal to the "
                    "number of sub tensor.",
                    multiple_lod_level.size(), GetTensorDescNum());
  switch (desc_.type()) {
    case proto::VarDesc::READER: {
      size_t i = 0;
      for (auto &lod_tensor : *desc_.mutable_reader()->mutable_lod_tensor()) {
        lod_tensor.set_lod_level(multiple_lod_level[i++]);
      }
    } break;
    default:
      PADDLE_THROW(
          "Setting 'lod_levels' is not supported by the type of var %s.",
          this->Name());
Y
Yu Yang 已提交
145
  }
Y
Stash  
Yu Yang 已提交
146 147
}

148
int32_t VarDesc::GetLoDLevel() const {
Y
Yu Yang 已提交
149
  switch (desc_.type()) {
150
    case proto::VarDesc::LOD_TENSOR:
Y
Yu Yang 已提交
151
      return desc_.lod_tensor().lod_level();
152
    case proto::VarDesc::LOD_TENSOR_ARRAY:
Y
Yu Yang 已提交
153 154
      return desc_.tensor_array().lod_level();
    default:
F
fengjiayi 已提交
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
      PADDLE_THROW(
          "Getting 'lod_level' is not supported by the type of var %s.",
          this->Name());
  }
}

std::vector<int32_t> VarDesc::GetLoDLevels() const {
  std::vector<int32_t> res;
  switch (desc_.type()) {
    case proto::VarDesc::READER:
      res.reserve(desc_.reader().lod_tensor_size());
      for (auto &lod_tensor : desc_.reader().lod_tensor()) {
        res.push_back(lod_tensor.lod_level());
      }
      return res;
      break;
    default:
      PADDLE_THROW(
          "Getting 'lod_levels' is not supported by the type of var %s.",
          this->Name());
Y
Yu Yang 已提交
175
  }
Y
Stash  
Yu Yang 已提交
176
}
Y
Yu Yang 已提交
177

Y
Yu Yang 已提交
178
const proto::TensorDesc &VarDesc::tensor_desc() const {
F
fengjiayi 已提交
179
  PADDLE_ENFORCE(desc_.has_type(), "The var's type hasn't been set.");
Y
Yu Yang 已提交
180
  switch (desc_.type()) {
181
    case proto::VarDesc::SELECTED_ROWS:
Y
Yu Yang 已提交
182
      return desc_.selected_rows();
183
    case proto::VarDesc::LOD_TENSOR:
Y
Yu Yang 已提交
184
      return desc_.lod_tensor().tensor();
185
    case proto::VarDesc::LOD_TENSOR_ARRAY:
Y
Yu Yang 已提交
186
      return desc_.tensor_array().tensor();
Y
Yu Yang 已提交
187
    default:
F
fengjiayi 已提交
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
      PADDLE_THROW(
          "Getting 'tensor_desc' is not supported by the type of var %s.",
          this->Name());
  }
}

std::vector<proto::TensorDesc> VarDesc::tensor_descs() const {
  PADDLE_ENFORCE(desc_.has_type(), "The var type hasn't been set.");
  std::vector<proto::TensorDesc> res;
  res.reserve(GetTensorDescNum());
  switch (desc_.type()) {
    case proto::VarDesc::READER:
      for (const auto &lod_tensor : desc_.reader().lod_tensor()) {
        res.push_back(lod_tensor.tensor());
      }
      return res;
    default:
      PADDLE_THROW(
          "Getting 'tensor_descs' is not supported by the type of var "
          "%s.",
          this->Name());
Y
Yu Yang 已提交
209 210 211
  }
}

Y
Yu Yang 已提交
212
proto::TensorDesc *VarDesc::mutable_tensor_desc() {
F
fengjiayi 已提交
213
  PADDLE_ENFORCE(desc_.has_type(), "The var type hasn't been set.");
Y
Yu Yang 已提交
214
  switch (desc_.type()) {
215
    case proto::VarDesc::SELECTED_ROWS:
Y
Yu Yang 已提交
216
      return desc_.mutable_selected_rows();
217
    case proto::VarDesc::LOD_TENSOR:
Y
Yu Yang 已提交
218
      return desc_.mutable_lod_tensor()->mutable_tensor();
219
    case proto::VarDesc::LOD_TENSOR_ARRAY:
Y
Yu Yang 已提交
220
      return desc_.mutable_tensor_array()->mutable_tensor();
Y
Yu Yang 已提交
221
    default:
F
fengjiayi 已提交
222 223 224 225
      PADDLE_THROW(
          "Getting 'mutable_tensor_desc' is not supported by the type of var "
          "%s.",
          this->Name());
Y
Yu Yang 已提交
226 227
  }
}
F
fengjiayi 已提交
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246

std::vector<proto::TensorDesc *> VarDesc::mutable_tensor_descs() {
  PADDLE_ENFORCE(desc_.has_type(), "The var type hasn't been set.");
  std::vector<proto::TensorDesc *> res;
  res.reserve(GetTensorDescNum());
  switch (desc_.type()) {
    case proto::VarDesc::READER:
      for (auto &lod_tensor : *desc_.mutable_reader()->mutable_lod_tensor()) {
        res.push_back(lod_tensor.mutable_tensor());
      }
      return res;
    default:
      PADDLE_THROW(
          "Getting 'tensor_descs' is not supported by the type of var "
          "%s.",
          this->Name());
  }
}

F
fengjiayi 已提交
247 248
}  // namespace framework
}  // namespace paddle