var_desc.h 2.3 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 <string>

L
liuruilong 已提交
19
#include "framework/framework.pb-c.h"
L
liuruilong 已提交
20
#include "framework/program/tensor_desc.h"
朔-望's avatar
朔-望 已提交
21 22

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

L
liuruilong 已提交
25 26 27 28 29 30 31 32
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_;
33
  }
L
liuruilong 已提交
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
  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
朔-望 已提交
49

L
liuruilong 已提交
50
        break;
朔-望's avatar
朔-望 已提交
51 52
      default:
        break;
朔-望's avatar
朔-望 已提交
53
    }
L
liuruilong 已提交
54 55 56
    switch (type_) {
      case VARTYPE_TYPE_CHANNEL:
        data_type_ = (VarType_Type)desc->type->channel->data_type;
朔-望's avatar
朔-望 已提交
57 58
        break;
      default:
L
liuruilong 已提交
59 60
        data_type_ = tensor_desc_.DataType();
        break;
朔-望's avatar
朔-望 已提交
61
    }
62
  }
L
liuruilong 已提交
63 64 65 66 67
  std::string Name() const { return name_; }

  VarType_Type Type() const { return type_; }

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

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

朔-望's avatar
朔-望 已提交
71
 private:
L
liuruilong 已提交
72 73 74 75 76
  std::string name_;
  bool persistable_;
  TensorDesc tensor_desc_;
  VarType_Type type_;
  VarType_Type data_type_;
朔-望's avatar
朔-望 已提交
77
};
朔-望's avatar
朔-望 已提交
78

朔-望's avatar
朔-望 已提交
79 80
}  // namespace framework
}  // namespace paddle_mobile