program_desc.h 2.8 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 52
    blocks_.reserve(BlocksSize());
    for (size_t idx = 0; idx < BlocksSize(); ++idx) {
53
      blocks_.push_back(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 101 102
};

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