program_desc.h 4.7 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(desc_->blocks()->size());
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
  size_t BlocksSize() const override { return blocks_.size(); }
63

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

  template <typename T>
68
  T* GetBlock(int32_t idx) {
69
    LITE_MODEL_INTERFACE_NOT_IMPLEMENTED;
70
    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 84 85 86 87 88
  void ClearBlocks() override {
    CHECK_EQ(BlocksSize(), 0u) << "For backward compatibility, in the "
                                  "read-only flatbuffers version, this "
                                  "interface degenerates to force the number "
                                  "of blocks to be zero.";
  }

89 90
  proto::ProgramDesc const* raw_desc() const { return desc_; }

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

93
 private:
94
  proto::ProgramDesc const* desc_;
95
  std::vector<char> buf_;
96
  std::vector<BlockDescView> blocks_;
97 98

 private:
99 100
  ProgramDescView& operator=(const ProgramDescView&) = delete;
  ProgramDescView(const ProgramDescView&) = delete;
101 102
};

103
#ifdef LITE_WITH_FLATBUFFERS_DESC
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
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;
  }

143
  std::vector<char> data() {
144
    SyncBuffer();
145 146 147 148
    std::vector<char> cache;
    cache.resize(buf_.size());
    std::memcpy(cache.data(), buf_.data(), buf_.size());
    return cache;
149 150 151 152 153 154
  }

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

  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_;
172
  std::vector<std::unique_ptr<BlockDesc>> blocks_;
173
};
174
#endif  // LITE_WITH_FLATBUFFERS_DESC
175

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