var_desc.cc 8.8 KB
Newer Older
1
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.
F
fengjiayi 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14

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. */

Y
Yi Wang 已提交
15 16
#include "paddle/fluid/framework/var_desc.h"
#include "paddle/fluid/platform/enforce.h"
F
fengjiayi 已提交
17 18 19 20

namespace paddle {
namespace framework {

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

23 24 25
void VarDesc::SetType(proto::VarType::Type type) {
  desc_.mutable_type()->set_type(type);
}
Q
QI JUN 已提交
26

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

F
fengjiayi 已提交
31
void VarDesc::SetTensorDescNum(size_t num) {
32 33 34 35
  switch (desc_.type().type()) {
    case proto::VarType::READER: {
      auto *lod_tensors_ptr =
          desc_.mutable_type()->mutable_reader()->mutable_lod_tensor();
F
fengjiayi 已提交
36 37 38 39 40 41 42 43 44 45 46 47 48 49
      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 {
50 51 52
  switch (desc_.type().type()) {
    case proto::VarType::READER:
      return desc_.type().reader().lod_tensor_size();
F
fengjiayi 已提交
53 54 55 56 57 58 59 60 61
      break;
    default:
      PADDLE_THROW(
          "Getting 'sub_tensor_number' is not supported by the type of var %s.",
          this->Name());
  }
}

void VarDesc::SetShapes(
F
fengjiayi 已提交
62
    const std::vector<std::vector<int64_t>> &multiple_dims) {
F
fengjiayi 已提交
63 64 65 66 67 68 69
  if (multiple_dims.size() != GetTensorDescNum()) {
    VLOG(3) << "WARNING: The number of given shapes(" << multiple_dims.size()
            << ") doesn't match the existing tensor number("
            << GetTensorDescNum()
            << "). The Reader is going to be reinitialized.";
    SetTensorDescNum(multiple_dims.size());
  }
70
  std::vector<proto::VarType::TensorDesc *> tensors = mutable_tensor_descs();
F
fengjiayi 已提交
71 72 73 74 75 76 77 78 79 80
  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 {
81
  std::vector<proto::VarType::TensorDesc> descs = tensor_descs();
F
fengjiayi 已提交
82 83 84 85 86 87 88 89
  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;
}

A
Abhinav Arora 已提交
90
void VarDesc::SetDataType(proto::VarType::Type data_type) {
Y
Yu Yang 已提交
91
  mutable_tensor_desc()->set_data_type(data_type);
F
fengjiayi 已提交
92 93
}

F
fengjiayi 已提交
94
void VarDesc::SetDataTypes(
A
Abhinav Arora 已提交
95
    const std::vector<proto::VarType::Type> &multiple_data_type) {
F
fengjiayi 已提交
96 97 98 99 100 101 102 103
  if (multiple_data_type.size() != GetTensorDescNum()) {
    VLOG(3) << "WARNING: The number of given data types("
            << multiple_data_type.size()
            << ") doesn't match the existing tensor number("
            << GetTensorDescNum()
            << "). The Reader is going to be reinitialized.";
    SetTensorDescNum(multiple_data_type.size());
  }
104 105
  std::vector<proto::VarType::TensorDesc *> tensor_descs =
      mutable_tensor_descs();
F
fengjiayi 已提交
106 107 108
  for (size_t i = 0; i < multiple_data_type.size(); ++i) {
    tensor_descs[i]->set_data_type(multiple_data_type[i]);
  }
F
fengjiayi 已提交
109 110
}

A
Abhinav Arora 已提交
111
proto::VarType::Type VarDesc::GetDataType() const {
112 113
  return tensor_desc().data_type();
}
Y
Stash  
Yu Yang 已提交
114

A
Abhinav Arora 已提交
115
std::vector<proto::VarType::Type> VarDesc::GetDataTypes() const {
116
  std::vector<proto::VarType::TensorDesc> descs = tensor_descs();
A
Abhinav Arora 已提交
117
  std::vector<proto::VarType::Type> res;
F
fengjiayi 已提交
118 119 120 121 122 123 124
  res.reserve(descs.size());
  for (const auto &tensor_desc : descs) {
    res.push_back(tensor_desc.data_type());
  }
  return res;
}

Y
Yu Yang 已提交
125
void VarDesc::SetLoDLevel(int32_t lod_level) {
126 127 128
  switch (desc_.type().type()) {
    case proto::VarType::LOD_TENSOR:
      desc_.mutable_type()->mutable_lod_tensor()->set_lod_level(lod_level);
Y
Yu Yang 已提交
129
      break;
130 131
    case proto::VarType::LOD_TENSOR_ARRAY:
      desc_.mutable_type()->mutable_tensor_array()->set_lod_level(lod_level);
Y
Yu Yang 已提交
132 133
      break;
    default:
F
fengjiayi 已提交
134 135 136 137 138 139 140
      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) {
F
fengjiayi 已提交
141 142 143 144 145 146 147 148
  if (multiple_lod_level.size() != GetTensorDescNum()) {
    VLOG(3) << "WARNING: The number of given lod_levels("
            << multiple_lod_level.size()
            << ") doesn't match the existing tensor number("
            << GetTensorDescNum()
            << "). The Reader is going to be reinitialized.";
    SetTensorDescNum(multiple_lod_level.size());
  }
149 150
  switch (desc_.type().type()) {
    case proto::VarType::READER: {
F
fengjiayi 已提交
151
      size_t i = 0;
152 153
      for (auto &lod_tensor :
           *desc_.mutable_type()->mutable_reader()->mutable_lod_tensor()) {
F
fengjiayi 已提交
154 155 156 157 158 159 160
        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 已提交
161
  }
Y
Stash  
Yu Yang 已提交
162 163
}

164
int32_t VarDesc::GetLoDLevel() const {
165 166 167 168 169
  switch (desc_.type().type()) {
    case proto::VarType::LOD_TENSOR:
      return desc_.type().lod_tensor().lod_level();
    case proto::VarType::LOD_TENSOR_ARRAY:
      return desc_.type().tensor_array().lod_level();
Y
Yu Yang 已提交
170
    default:
F
fengjiayi 已提交
171 172 173 174 175 176 177 178
      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;
179 180 181 182
  switch (desc_.type().type()) {
    case proto::VarType::READER:
      res.reserve(desc_.type().reader().lod_tensor_size());
      for (auto &lod_tensor : desc_.type().reader().lod_tensor()) {
F
fengjiayi 已提交
183 184 185 186 187 188 189 190
        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 已提交
191
  }
Y
Stash  
Yu Yang 已提交
192
}
Y
Yu Yang 已提交
193

194
const proto::VarType::TensorDesc &VarDesc::tensor_desc() const {
F
fengjiayi 已提交
195
  PADDLE_ENFORCE(desc_.has_type(), "The var's type hasn't been set.");
196 197 198 199 200 201 202 203
  PADDLE_ENFORCE(desc_.type().has_type(), "The var type hasn't been set.");
  switch (desc_.type().type()) {
    case proto::VarType::SELECTED_ROWS:
      return desc_.type().selected_rows();
    case proto::VarType::LOD_TENSOR:
      return desc_.type().lod_tensor().tensor();
    case proto::VarType::LOD_TENSOR_ARRAY:
      return desc_.type().tensor_array().tensor();
Y
Yu Yang 已提交
204
    default:
F
fengjiayi 已提交
205 206 207 208 209 210
      PADDLE_THROW(
          "Getting 'tensor_desc' is not supported by the type of var %s.",
          this->Name());
  }
}

211
std::vector<proto::VarType::TensorDesc> VarDesc::tensor_descs() const {
F
fengjiayi 已提交
212
  PADDLE_ENFORCE(desc_.has_type(), "The var type hasn't been set.");
213
  std::vector<proto::VarType::TensorDesc> res;
F
fengjiayi 已提交
214
  res.reserve(GetTensorDescNum());
215 216 217
  switch (desc_.type().type()) {
    case proto::VarType::READER:
      for (const auto &lod_tensor : desc_.type().reader().lod_tensor()) {
F
fengjiayi 已提交
218 219 220 221 222 223 224 225
        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 已提交
226 227 228
  }
}

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

247
std::vector<proto::VarType::TensorDesc *> VarDesc::mutable_tensor_descs() {
F
fengjiayi 已提交
248
  PADDLE_ENFORCE(desc_.has_type(), "The var type hasn't been set.");
249 250
  PADDLE_ENFORCE(desc_.type().has_type(), "The var type hasn't been set.");
  std::vector<proto::VarType::TensorDesc *> res;
F
fengjiayi 已提交
251
  res.reserve(GetTensorDescNum());
252 253 254 255
  switch (desc_.type().type()) {
    case proto::VarType::READER:
      for (auto &lod_tensor :
           *desc_.mutable_type()->mutable_reader()->mutable_lod_tensor()) {
F
fengjiayi 已提交
256 257 258 259 260 261 262 263 264 265 266
        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 已提交
267 268
}  // namespace framework
}  // namespace paddle