var_desc.h 2.4 KB
Newer Older
L
liuruilong 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/* Copyright (c) 2018 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. */

朔-望's avatar
朔-望 已提交
15 16
#pragma once

L
liuruilong 已提交
17 18
#include "framework/framework.pb.h"
#include "framework/paddle_mobile_object.h"
朔-望's avatar
朔-望 已提交
19 20

namespace paddle_mobile {
朔-望's avatar
朔-望 已提交
21
namespace framework {
朔-望's avatar
朔-望 已提交
22

朔-望's avatar
朔-望 已提交
23
class VarDesc {
朔-望's avatar
朔-望 已提交
24
 public:
25
  VarDesc(const proto::VarDesc &desc);
朔-望's avatar
朔-望 已提交
26

L
liuruilong 已提交
27 28
  VarDesc(const VarDesc &var_desc):desc_(var_desc.desc_) {}

29
  std::string Name() const { return desc_.name(); }
朔-望's avatar
朔-望 已提交
30

31
  proto::VarType::Type GetType() const { return desc_.type().type(); }
朔-望's avatar
朔-望 已提交
32

33
  bool Persistable() const { return desc_.persistable(); }
朔-望's avatar
朔-望 已提交
34

35 36
  const proto::VarType::ChannelDesc &channel_desc() const {
    switch (desc_.type().type()) {
朔-望's avatar
朔-望 已提交
37 38 39 40
      case proto::VarType::CHANNEL:
        return desc_.type().channel();
      default:
        break;
朔-望's avatar
朔-望 已提交
41
    }
42
  }
朔-望's avatar
朔-望 已提交
43

44 45
  const proto::VarType::TensorDesc &tensor_desc() const {
    switch (desc_.type().type()) {
朔-望's avatar
朔-望 已提交
46 47 48 49 50 51 52 53
      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();
      default:
        break;
朔-望's avatar
朔-望 已提交
54
    }
55
  }
朔-望's avatar
朔-望 已提交
56

57 58
  proto::VarType::Type GetDataType() const {
    switch (desc_.type().type()) {
朔-望's avatar
朔-望 已提交
59 60 61 62 63
      case proto::VarType::CHANNEL:
        return channel_desc().data_type();
        break;
      default:
        return tensor_desc().data_type();
朔-望's avatar
朔-望 已提交
64
    }
65
  }
朔-望's avatar
朔-望 已提交
66

67 68 69 70 71 72 73 74 75
  template <typename T>
  std::vector<T> RepeatedToVector(
      const google::protobuf::RepeatedField<T> &repeated_field) const {
    std::vector<T> ret;
    ret.reserve(repeated_field.size());
    std::copy(repeated_field.begin(), repeated_field.end(),
              std::back_inserter(ret));
    return ret;
  }
朔-望's avatar
朔-望 已提交
76

77 78 79
  std::vector<int64_t> GetShape() const {
    return this->RepeatedToVector(tensor_desc().dims());
  }
朔-望's avatar
朔-望 已提交
80

朔-望's avatar
朔-望 已提交
81
 private:
82
  proto::VarDesc desc_;
朔-望's avatar
朔-望 已提交
83
};
朔-望's avatar
朔-望 已提交
84

朔-望's avatar
朔-望 已提交
85 86
}  // namespace framework
}  // namespace paddle_mobile