program_desc.h 4.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
// 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

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

namespace paddle {
namespace lite {
namespace fbs {

29
class ProgramDescView : public ProgramDescAPI {
30
 public:
31 32 33
  ProgramDescView() = default;
  explicit ProgramDescView(const std::vector<char>& buf) { Init(buf); }
  explicit ProgramDescView(std::vector<char>&& buf) {
34
    Init(std::forward<std::vector<char>>(buf));
35
  }
36

37 38 39 40 41
  void Init(const std::vector<char>& buf) {
    CHECK(buf.data());
    buf_ = buf;
    InitProgramDesc();
  }
42

43 44
  void Init(std::vector<char>&& buf) {
    CHECK(buf.data());
45
    buf_ = std::move(buf);
46 47 48 49 50
    InitProgramDesc();
  }

  void InitProgramDesc() {
    desc_ = proto::GetProgramDesc(buf_.data());
51
    blocks_.resize(BlocksSize());
52
    for (size_t idx = 0; idx < BlocksSize(); ++idx) {
53
      blocks_[idx] = BlockDescView(desc_->blocks()->Get(idx));
54 55 56
    }
  }

57
  void CopyFrom(const ProgramDescView& other) {
58 59
    buf_ = other.buf();
    Init(buf_);
60 61
  }

62 63
  size_t BlocksSize() const override { return desc_->blocks()->size(); }

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

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

73
  const std::vector<BlockDescView>& GetBlocks() const { return blocks_; }
74

75 76 77 78 79 80 81
  bool HasVersion() const override { return desc_->version() != nullptr; }

  int64_t Version() const override {
    CHECK(HasVersion());
    return desc_->version()->version();
  }

82 83
  proto::ProgramDesc const* raw_desc() const { return desc_; }

84
  const std::vector<char>& buf() const { return buf_; }
85

86
 private:
87
  proto::ProgramDesc const* desc_;
88
  std::vector<char> buf_;
89
  std::vector<BlockDescView> blocks_;
90 91

 private:
92 93
  ProgramDescView& operator=(const ProgramDescView&) = delete;
  ProgramDescView(const ProgramDescView&) = delete;
94
  void NotImplemented() const {
95
    LOG(FATAL) << "The additional interfaces of ProgramDescView is temporarily "
96 97
                  "unavailable in read-only mode.";
  }
98 99
};

100
#ifdef LITE_WITH_FLATBUFFERS_DESC
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
class ProgramDesc : public ProgramDescAPI {
 public:
  ProgramDesc() = default;

  explicit ProgramDesc(const std::vector<char>& buf) {
    const auto* raw_buf = proto::GetProgramDesc(buf.data());
    raw_buf->UnPackTo(&desc_);
    SyncBlocks();
  }

  size_t BlocksSize() const override { return desc_.blocks.size(); }

  void ClearBlocks() override {
    desc_.blocks.clear();
    SyncBlocks();
  }

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

  template <typename T>
  T* AddBlock();

  bool HasVersion() const override { return desc_.version.get(); }

  int64_t Version() const override {
    if (!HasVersion()) {
      return -1;
    }
    return desc_.version->version;
  }

  void SetVersion(int64_t version_in) override {
    if (!HasVersion()) {
      desc_.version.reset(new fbs::proto::VersionT());
    }
    desc_.version->version = version_in;
  }

  const void* data() {
    SyncBuffer();
    return buf_.data();
  }

  size_t buf_size() {
    SyncBuffer();
    return buf_.size();
  }

 private:
  void SyncBlocks() {
    blocks_.resize(desc_.blocks.size());
    for (size_t i = 0; i < desc_.blocks.size(); ++i) {
154 155
      if (!blocks_[i] || blocks_[i]->raw_desc() != desc_.blocks[i].get()) {
        blocks_[i].reset(new BlockDesc(desc_.blocks[i].get()));
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
      }
    }
  }

  void SyncBuffer() {
    fbb_.Reset();
    flatbuffers::Offset<proto::ProgramDesc> desc =
        proto::ProgramDesc::Pack(fbb_, &desc_);
    fbb_.Finish(desc);
    buf_ = fbb_.Release();
  }

  flatbuffers::DetachedBuffer buf_;
  flatbuffers::FlatBufferBuilder fbb_;
  proto::ProgramDescT desc_;
171
  std::vector<std::unique_ptr<BlockDesc>> blocks_;
172
};
173
#endif  // LITE_WITH_FLATBUFFERS_DESC
174

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