block_desc.h 4.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
// Copyright (c) 2020 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

17
#include <memory>
18
#include <vector>
19 20
#include "lite/model_parser/base/block_desc.h"
#include "lite/model_parser/flatbuffers/framework_generated.h"
21 22
#include "lite/model_parser/flatbuffers/op_desc.h"
#include "lite/model_parser/flatbuffers/var_desc.h"
23 24 25 26 27 28
#include "lite/utils/all.h"

namespace paddle {
namespace lite {
namespace fbs {

29
class BlockDescView : public BlockDescAPI {
30
 public:
31
  explicit BlockDescView(proto::BlockDesc const* desc) : desc_(desc) {
32 33 34 35
    CHECK(desc_);
    vars_.reserve(VarsSize());
    ops_.reserve(OpsSize());
    for (size_t idx = 0; idx < VarsSize(); ++idx) {
36
      vars_.push_back(VarDescView(desc_->vars()->Get(idx)));
37 38
    }
    for (size_t idx = 0; idx < OpsSize(); ++idx) {
39
      ops_.push_back(OpDescView(desc_->ops()->Get(idx)));
40 41
    }
  }
42 43 44 45 46 47 48 49

  int32_t Idx() const override { return desc_->idx(); }

  int32_t ParentIdx() const override { return desc_->parent_idx(); }

  size_t VarsSize() const override { return desc_->vars()->size(); }

  template <typename T>
50
  T const* GetVar(int32_t idx) const;
51 52

  template <typename T>
53 54 55
  T* GetVar(int32_t idx) {
    NotImplemented();
    return nullptr;
56 57 58 59 60 61 62 63 64
  }

  size_t OpsSize() const override {
    CHECK(desc_);
    CHECK(desc_->ops());
    return desc_->ops()->size();
  }

  template <typename T>
65
  T const* GetOp(int32_t idx) const;
66 67

  template <typename T>
68 69 70
  T* GetOp(int32_t idx) {
    NotImplemented();
    return nullptr;
71 72
  }

73
  const std::vector<VarDescView>& GetVars() const { return vars_; }
74

75 76 77 78
  int32_t ForwardBlockIdx() const override {
    return desc_->forward_block_idx();
  }

79
  BlockDescView() { NotImplemented(); }
80 81

 private:
82
  proto::BlockDesc const* desc_;  // not_own
83 84
  std::vector<VarDescView> vars_;
  std::vector<OpDescView> ops_;
85 86 87

 private:
  void NotImplemented() const {
88
    LOG(FATAL) << "The additional interfaces of BlockDescView is temporarily "
89 90
                  "unavailable in read-only mode.";
  }
91 92
};

93 94 95
class BlockDesc : public BlockDescAPI {
 public:
  BlockDesc() : owned_(true), desc_(new proto::BlockDescT()) {}
96 97 98 99 100
  explicit BlockDesc(proto::BlockDescT* desc) : desc_(desc) {
    CHECK(desc_);
    SyncVars();
    SyncOps();
  }
101 102 103 104 105 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 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153

  int32_t Idx() const override { return desc_->idx; }

  void SetIdx(int32_t idx) override { desc_->idx = idx; }

  int32_t ParentIdx() const override { return desc_->parent_idx; }

  void SetParentIdx(int32_t idx) override { desc_->parent_idx = idx; }

  size_t VarsSize() const override { return desc_->vars.size(); }

  void ClearVars() override {
    desc_->vars.clear();
    SyncVars();
  }

  size_t OpsSize() const override { return desc_->ops.size(); }

  void ClearOps() override {
    desc_->ops.clear();
    SyncOps();
  }

  int32_t ForwardBlockIdx() const override { return desc_->forward_block_idx; }

  void SetForwardBlockIdx(int32_t idx_in) override {
    desc_->forward_block_idx = idx_in;
  }

  proto::BlockDescT* raw_desc() { return desc_; }

  template <typename T>
  T* GetVar(int32_t idx);

  template <typename T>
  T* AddVar();

  template <typename T>
  T* GetOp(int32_t idx);

  template <typename T>
  T* AddOp();

  ~BlockDesc() {
    if (owned_) {
      delete desc_;
    }
  }

 private:
  void SyncVars() {
    vars_.resize(desc_->vars.size());
    for (size_t i = 0; i < desc_->vars.size(); ++i) {
154 155
      if (!vars_[i] || vars_[i]->raw_desc() != desc_->vars[i].get()) {
        vars_[i].reset(new VarDesc(desc_->vars[i].get()));
156 157 158 159 160 161
      }
    }
  }
  void SyncOps() {
    ops_.resize(desc_->ops.size());
    for (size_t i = 0; i < desc_->ops.size(); ++i) {
162 163
      if (!ops_[i] || ops_[i]->raw_desc() != desc_->ops[i].get()) {
        ops_[i].reset(new OpDesc(desc_->ops[i].get()));
164 165 166 167 168 169
      }
    }
  }

  bool owned_{false};
  proto::BlockDescT* desc_{nullptr};
170 171
  std::vector<std::unique_ptr<VarDesc>> vars_;
  std::vector<std::unique_ptr<OpDesc>> ops_;
172 173
};

174 175 176
}  // namespace fbs
}  // namespace lite
}  // namespace paddle