runtime.h 2.9 KB
Newer Older
S
update  
superjomn 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
// Copyright (c) 2019 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.

#pragma once
#include "paddle/fluid/framework/framework.pb.h"
#include "paddle/fluid/lite/utils/all.h"

namespace paddle {
namespace lite {

// We define the runtime data structure for framework.proto to support some
// other model format such as JSON if needed.
using proto_type_t = framework::proto::VarType::Type;

class TensorDesc {
 public:
  proto_type_t data_type;
  std::vector<int64_t> dims;

  TensorDesc() = default;
  explicit TensorDesc(const framework::proto::VarType_TensorDesc& proto) {
    Parse(proto);
  }

  void Parse(const framework::proto::VarType_TensorDesc& proto) {
    data_type = proto.data_type();
    for (auto& d : proto.dims()) dims.push_back(d);
  }
};

class LoDTensorDesc {
 public:
  TensorDesc tensor;
  int lod_level{-1};

  LoDTensorDesc(const framework::proto::VarType_LoDTensorDesc& proto) {
    Parse(proto);
  }

  void Parse(const framework::proto::VarType_LoDTensorDesc& proto) {
    tensor.Parse(proto.tensor());
    lod_level = proto.lod_level();
  }
};

class LoDTensorArrayDesc {
 public:
  TensorDesc tensor;
  int lod_level{-1};

  LoDTensorArrayDesc(
      const framework::proto::VarType_LoDTensorArrayDesc& proto) {
    Parse(proto);
  }

  void Parse(const framework::proto::VarType_LoDTensorArrayDesc& proto) {
    tensor.Parse(proto.tensor());
    lod_level = proto.lod_level();
  }
};

class VarType {
 public:
  framework::proto::VarType::Type type;
S
superjomn 已提交
76
  variant<LoDTensorDesc, TensorDesc> desc;
S
update  
superjomn 已提交
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96

  void Parse(const framework::proto::VarType& proto);
};

class VarDesc {
 public:
  void Parse(const framework::proto::VarDesc& desc);

  std::string name;
  VarType type;
  bool persistable{false};
};

class OpDesc {
 public:
  void Parse(const framework::proto::OpDesc& desc);

  std::string op_type;
  std::map<std::string, std::vector<std::string>> inputs;
  std::map<std::string, std::vector<std::string>> outputs;
S
Superjomn 已提交
97
  std::map<std::string, variant<int, float, std::string>> attrs;
S
update  
superjomn 已提交
98 99 100 101 102 103 104 105 106 107 108 109 110
};

class BlockDesc {
 public:
  void Parse(const framework::proto::BlockDesc& desc);

  int idx{-1};
  int parent_idx{-1};
  int forward_block_idx{-1};
  std::map<std::string, VarDesc> vars;
  std::vector<OpDesc> ops;
};

S
superjomn 已提交
111 112 113
class ProgramDesc {
 public:
  void Parse(const framework::proto::ProgramDesc& desc);
S
Superjomn 已提交
114 115

  BlockDesc block;
S
superjomn 已提交
116 117
};

S
update  
superjomn 已提交
118 119
}  // namespace lite
}  // namespace paddle