kernel_frame.h 4.9 KB
Newer Older
Y
Yan Chunwei 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 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 154 155 156 157 158 159 160 161 162 163 164 165 166
// Copyright (c) 2021 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 <glog/logging.h>
#include <llvm/ADT/ArrayRef.h>

#include <utility>

#include "llvm/ADT/SmallVector.h"
#include "paddle/infrt/host_context/value.h"

namespace infrt::host_context {

/**
 * KernelFrame captures the states(input arguments, attributes, results)
 * associated with a kernel invocation.
 */
class KernelFrame {
 public:
  int GetNumArgs() const { return num_arguments_; }
  int GetNumResults() const { return num_results_; }
  int GetNumAttributes() const {
    return value_or_attrs_.size() - num_arguments_ -
           (num_results_ == -1 ? 0 : num_results_);
  }

  template <typename T>
  T& GetArgAt(int index) {
    CHECK_LT(index, GetNumArgs());
    return value_or_attrs_[index]->get<T>();
  }
  template <typename T>
  const T& GetArgAt(int index) const {
    CHECK_LT(index, GetNumArgs());
    return value_or_attrs_[index]->get<T>();
  }

  Value* GetArgAt(int index) {
    CHECK_LT(index, GetNumArgs());
    return value_or_attrs_[index];
  }

  // Get all arguments.
  llvm::ArrayRef<Value*> GetArguments() const {
    return GetValues(0, num_arguments_);
  }

  Value* GetAttributeAt(int idx) {
    CHECK_NE(num_results_, -1)
        << "Must call SetNumResults before GetAttributeAt";
    CHECK_LT(idx,
             static_cast<int>(value_or_attrs_.size() - num_arguments_ -
                              num_results_));
    return value_or_attrs_[num_arguments_ + num_results_ + idx];
  }

  void AddAttribute(Value* v) {
    CHECK_NE(num_results_, -1)
        << "Must call SetNumResults before calling AddAttribute";
    value_or_attrs_.emplace_back(v);
  }

  template <typename T, typename... Args>
  void EmplaceResult(Args&&... args) {
    EmplaceResult<T>(0, std::forward<Args>(args)...);
  }

  template <typename T, typename... Args>
  void EmplaceResult(int index, Args&&... args) {
    SetResultAt(index, T(std::forward<Args>(args)...));
  }

  template <typename T>
  void SetResultAt(int index, T&& value) {
    CHECK_LT(index, num_results_) << "Invalid result index";
    CHECK(value_or_attrs_[num_arguments_ + index]);
    value_or_attrs_[num_arguments_ + index]->set(std::move(value));
  }

  llvm::ArrayRef<Value*> GetResults() const {
    return GetValues(num_arguments_, num_results_);
  }
  llvm::MutableArrayRef<Value*> GetResults() {
    return GetMutableValues(num_arguments_, num_results_);
  }

  llvm::ArrayRef<Value*> GetValues(size_t from, size_t length) const {
    CHECK_LE(static_cast<int>(from + length), num_arguments_ + num_results_);
    if (length == 0) return {};

    return llvm::makeArrayRef(&value_or_attrs_[from], length);
  }

  llvm::MutableArrayRef<Value*> GetMutableValues(size_t from, size_t length) {
    CHECK_LE(static_cast<int>(from + length), num_arguments_ + num_results_);
    if (length == 0) return {};
    return llvm::makeMutableArrayRef(&value_or_attrs_[from], length);
  }

 protected:
  int num_arguments_{};
  int num_results_{-1};

  llvm::SmallVector<Value*, 8> value_or_attrs_;
};

std::ostream& operator<<(std::ostream& os, const KernelFrame& frame);

class KernelFrameBuilder : public KernelFrame {
 public:
  void AddArgument(Value* value) {
    CHECK(value);
    CHECK_EQ(num_results_, -1)
        << "Should call AddArgument before calling SetNumResults";
    value_or_attrs_.push_back(value);
    ++num_arguments_;
  }

  void SetResults(llvm::ArrayRef<Value*> values) {
    CHECK_EQ(num_arguments_, static_cast<int>(value_or_attrs_.size()));
    CHECK_EQ(num_results_, -1);
    for (Value* x : values) {
      value_or_attrs_.push_back(x);
    }
    num_results_ = values.size();
  }

  void SetNumResults(size_t n) {
    CHECK_EQ(num_arguments_, static_cast<int>(value_or_attrs_.size()));
    CHECK_EQ(num_results_, -1);
    num_results_ = n;
    for (size_t i = 0; i < n; i++) {
      value_or_attrs_.emplace_back(new Value);
    }
  }

  void SetResultAt(int result_id, Value* value) {
    CHECK_EQ(static_cast<int>(value_or_attrs_.size()),
             num_arguments_ + num_results_)
        << "Call SetNumResults first";
    CHECK_LT(result_id + num_arguments_,
             static_cast<int>(value_or_attrs_.size()));
    CHECK(value);
    value_or_attrs_[num_arguments_ + result_id]->set(value);
  }

  void Reset() {
    value_or_attrs_.clear();
    num_arguments_ = 0;
    num_results_ = -1;
  }
};

}  // namespace infrt::host_context