var_desc.h 4.8 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
#include "framework/framework.pb-c.h"
L
liuruilong 已提交
18
#include "framework/paddle_mobile_object.h"
L
liuruilong 已提交
19
#include "framework/program/tensor_desc.h"
朔-望's avatar
朔-望 已提交
20 21

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

L
liuruilong 已提交
24
/*
朔-望's avatar
朔-望 已提交
25

L
liuruilong 已提交
26 27 28 29 30 31 32
PADDLE_MOBILE__FRAMEWORK__PROTO__VAR_TYPE__TYPE__BOOL = 0,
        PADDLE_MOBILE__FRAMEWORK__PROTO__VAR_TYPE__TYPE__INT16 = 1,
        PADDLE_MOBILE__FRAMEWORK__PROTO__VAR_TYPE__TYPE__INT32 = 2,
        PADDLE_MOBILE__FRAMEWORK__PROTO__VAR_TYPE__TYPE__INT64 = 3,
        PADDLE_MOBILE__FRAMEWORK__PROTO__VAR_TYPE__TYPE__FP16 = 4,
        PADDLE_MOBILE__FRAMEWORK__PROTO__VAR_TYPE__TYPE__FP32 = 5,
        PADDLE_MOBILE__FRAMEWORK__PROTO__VAR_TYPE__TYPE__FP64 = 6,
L
liuruilong 已提交
33

L
liuruilong 已提交
34 35 36 37 38 39 40 41 42 43
        PADDLE_MOBILE__FRAMEWORK__PROTO__VAR_TYPE__TYPE__LOD_TENSOR = 7,
        PADDLE_MOBILE__FRAMEWORK__PROTO__VAR_TYPE__TYPE__SELECTED_ROWS = 8,
        PADDLE_MOBILE__FRAMEWORK__PROTO__VAR_TYPE__TYPE__FEED_MINIBATCH = 9,
        PADDLE_MOBILE__FRAMEWORK__PROTO__VAR_TYPE__TYPE__FETCH_LIST = 10,
        PADDLE_MOBILE__FRAMEWORK__PROTO__VAR_TYPE__TYPE__STEP_SCOPES = 11,
        PADDLE_MOBILE__FRAMEWORK__PROTO__VAR_TYPE__TYPE__LOD_RANK_TABLE = 12,
        PADDLE_MOBILE__FRAMEWORK__PROTO__VAR_TYPE__TYPE__LOD_TENSOR_ARRAY = 13,
        PADDLE_MOBILE__FRAMEWORK__PROTO__VAR_TYPE__TYPE__PLACE_LIST = 14,
        PADDLE_MOBILE__FRAMEWORK__PROTO__VAR_TYPE__TYPE__READER = 15,
        PADDLE_MOBILE__FRAMEWORK__PROTO__VAR_TYPE__TYPE__CHANNEL = 16,
朔-望's avatar
朔-望 已提交
44

L
liuruilong 已提交
45 46
        PADDLE_MOBILE__FRAMEWORK__PROTO__VAR_TYPE__TYPE__RAW = 17,
        PADDLE_MOBILE__FRAMEWORK__PROTO__VAR_TYPE__TYPE__TUPLE = 18
朔-望's avatar
朔-望 已提交
47 48


L
liuruilong 已提交
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
                                                                 */

class VarDesc {
 public:
  VarDesc(const VarDesc &var_desc) {
    this->data_type_ = var_desc.data_type_;
    this->name_ = var_desc.name_;
    this->persistable_ = var_desc.persistable_;
    this->tensor_desc_ = var_desc.tensor_desc_;
    this->type_ = var_desc.type_;
    /*
     *
     *  std::string name_;
  bool persistable_;
  TensorDesc tensor_desc_;
  VarType_Type type_;
  VarType_Type data_type_;
     * */
67
  }
L
liuruilong 已提交
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
  VarDesc(PaddleMobile__Framework__Proto__VarDesc *desc) {
    type_ = (VarType_Type)desc->type->type;
    name_ = std::string(desc->name);
    persistable_ = (bool)desc->persistable;

    switch (type_) {
      case VARTYPE_TYPE_SELECTED_ROWS:
        tensor_desc_ = TensorDesc(desc->type->selected_rows);
        break;
      case VARTYPE_TYPE_LOD_TENSOR:
        tensor_desc_ = TensorDesc(desc->type->lod_tensor->tensor);
        break;
      case VARTYPE_TYPE_STEP_LOD_TENSOR_ARRAY:
        desc->type->tensor_array->tensor->data_type;
        tensor_desc_ = TensorDesc(desc->type->tensor_array->tensor);
朔-望's avatar
朔-望 已提交
83

L
liuruilong 已提交
84
        break;
朔-望's avatar
朔-望 已提交
85 86
      default:
        break;
朔-望's avatar
朔-望 已提交
87
    }
L
liuruilong 已提交
88 89 90
    switch (type_) {
      case VARTYPE_TYPE_CHANNEL:
        data_type_ = (VarType_Type)desc->type->channel->data_type;
朔-望's avatar
朔-望 已提交
91 92
        break;
      default:
L
liuruilong 已提交
93 94
        data_type_ = tensor_desc_.DataType();
        break;
朔-望's avatar
朔-望 已提交
95
    }
96
  }
L
liuruilong 已提交
97 98 99 100 101
  std::string Name() const { return name_; }

  VarType_Type Type() const { return type_; }

  bool Persistable() const { return persistable_; }
朔-望's avatar
朔-望 已提交
102

L
liuruilong 已提交
103
  const TensorDesc &Tensor_desc() const { return tensor_desc_; }
朔-望's avatar
朔-望 已提交
104

L
liuruilong 已提交
105
  //  const proto::VarType::ChannelDesc &channel_desc() const {
L
liuruilong 已提交
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
  //    switch (desc_.type().type()) {
  //      case proto::VarType::CHANNEL:
  //        return desc_.type().channel();
  //      default:
  //        break;
  //    }
  //  }

  //  proto::VarType::Type GetDataType() const {
  //    switch (desc_.type().type()) {
  //      case proto::VarType::CHANNEL:
  //        return channel_desc().data_type();
  //        break;
  //      default:
  //        return tensor_desc().data_type();
  //    }
  //  }

  //  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;
  //  }

  //  std::vector<int64_t> GetShape() const {
  //    return this->RepeatedToVector(tensor_desc().dims());
  //  }
L
liuruilong 已提交
137

朔-望's avatar
朔-望 已提交
138
 private:
L
liuruilong 已提交
139 140 141 142 143
  std::string name_;
  bool persistable_;
  TensorDesc tensor_desc_;
  VarType_Type type_;
  VarType_Type data_type_;
朔-望's avatar
朔-望 已提交
144
};
朔-望's avatar
朔-望 已提交
145

朔-望's avatar
朔-望 已提交
146 147
}  // namespace framework
}  // namespace paddle_mobile