var_desc.cc 2.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 {

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

void VarDescBind::SetType(VarDesc::VarType type) { desc_.set_type(type); }

F
fengjiayi 已提交
25
void VarDescBind::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
void VarDescBind::SetDataType(DataType data_type) {
Y
Yu Yang 已提交
30
  mutable_tensor_desc()->set_data_type(data_type);
F
fengjiayi 已提交
31 32 33
}

std::vector<int64_t> VarDescBind::Shape() const {
Y
Yu Yang 已提交
34
  return RepeatedToVector(tensor_desc().dims());
F
fengjiayi 已提交
35 36
}

Y
Yu Yang 已提交
37
DataType VarDescBind::GetDataType() const { return tensor_desc().data_type(); }
Y
Stash  
Yu Yang 已提交
38 39

void VarDescBind::SetLoDLevel(int32_t lod_level) {
Y
Yu Yang 已提交
40 41 42 43 44 45 46 47 48 49
  switch (desc_.type()) {
    case VarDesc::LOD_TENSOR:
      desc_.mutable_lod_tensor()->set_lod_level(lod_level);
      break;
    case VarDesc::LOD_TENSOR_ARRAY:
      desc_.mutable_tensor_array()->set_lod_level(lod_level);
      break;
    default:
      PADDLE_THROW("Tensor type=%d does not support LoDLevel", desc_.type());
  }
Y
Stash  
Yu Yang 已提交
50 51 52
}

int32_t VarDescBind::GetLodLevel() const {
Y
Yu Yang 已提交
53 54 55 56 57 58 59 60
  switch (desc_.type()) {
    case VarDesc::LOD_TENSOR:
      return desc_.lod_tensor().lod_level();
    case VarDesc::LOD_TENSOR_ARRAY:
      return desc_.tensor_array().lod_level();
    default:
      PADDLE_THROW("Tensor type=%d does not support LoDLevel", desc_.type());
  }
Y
Stash  
Yu Yang 已提交
61
}
Y
Yu Yang 已提交
62 63 64 65 66 67 68 69

const TensorDesc &VarDescBind::tensor_desc() const {
  PADDLE_ENFORCE(desc_.has_type(), "invoke TensorDesc must after set type");
  switch (desc_.type()) {
    case VarDesc::SELECTED_ROWS:
      return desc_.selected_rows();
    case VarDesc::LOD_TENSOR:
      return desc_.lod_tensor().tensor();
Y
Yu Yang 已提交
70 71
    case VarDesc::LOD_TENSOR_ARRAY:
      return desc_.tensor_array().tensor();
Y
Yu Yang 已提交
72 73 74 75 76 77 78 79 80 81 82 83 84
    default:
      PADDLE_THROW("Unexpected branch.");
  }
}

TensorDesc *VarDescBind::mutable_tensor_desc() {
  PADDLE_ENFORCE(desc_.has_type(),
                 "invoke MutableTensorDesc must after set type");
  switch (desc_.type()) {
    case VarDesc::SELECTED_ROWS:
      return desc_.mutable_selected_rows();
    case VarDesc::LOD_TENSOR:
      return desc_.mutable_lod_tensor()->mutable_tensor();
Y
Yu Yang 已提交
85 86
    case VarDesc::LOD_TENSOR_ARRAY:
      return desc_.mutable_tensor_array()->mutable_tensor();
Y
Yu Yang 已提交
87 88 89 90
    default:
      PADDLE_THROW("Unexpected branch.");
  }
}
F
fengjiayi 已提交
91 92
}  // namespace framework
}  // namespace paddle