program_desc.h 2.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 ProgramDesc : public ProgramDescAPI {
30
 public:
31
  ProgramDesc() = default;
32 33 34
  explicit ProgramDesc(const std::vector<char>& buf) { Init(buf); }
  explicit ProgramDesc(std::vector<char>&& buf) {
    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 52 53 54 55 56 57
    blocks_.reserve(BlocksSize());
    for (size_t idx = 0; idx < BlocksSize(); ++idx) {
      blocks_.push_back(BlockDesc(desc_->blocks()->Get(idx)));
    }
  }

  void CopyFrom(const ProgramDesc& 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 74
  const std::vector<BlockDesc>& GetBlocks() const { return blocks_; }

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 90 91 92 93 94 95 96 97
  std::vector<BlockDesc> blocks_;

 private:
  ProgramDesc& operator=(const ProgramDesc&) = delete;
  ProgramDesc(const ProgramDesc&) = delete;
  void NotImplemented() const {
    LOG(FATAL) << "The additional interfaces of ProgramDesc is temporarily "
                  "unavailable in read-only mode.";
  }
98 99 100 101 102
};

}  // namespace fbs
}  // namespace lite
}  // namespace paddle