kernel_utils.h 12.8 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 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
// 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 "paddle/infrt/host_context/kernel_frame.h"
#include "paddle/infrt/host_context/value.h"

namespace infrt {
namespace host_context {

template <typename T>
class Argument {
 public:
  explicit Argument(ValueRef value) : value_(value) {}

  ValueRef& value() { return value_; }
  const ValueRef& value() const { return value_; }

  T& get() const { return value_.get<T>(); }

 private:
  ValueRef value_;
};

/**
 * RemainingArguments collects all remaining arguments in an ArrayRef.
 */
class RemainingArguments {
 public:
  explicit RemainingArguments(llvm::ArrayRef<Value*> remaining_arguments)
      : remaining_arguments_(remaining_arguments) {}

  llvm::ArrayRef<Value*> values() const { return remaining_arguments_; }
  size_t size() const { return remaining_arguments_.size(); }
  const Value* operator[](size_t i) const { return remaining_arguments_[i]; }

 private:
  llvm::ArrayRef<Value*> remaining_arguments_;
};

/**
 * RemainingResults collects all remaining results in a MutableArrayRef.
 */
class RemainingResults {
 public:
  explicit RemainingResults(llvm::MutableArrayRef<ValueRef> remaining_results)
      : remaining_results_(remaining_results) {}
  llvm::MutableArrayRef<ValueRef> values() { return remaining_results_; }
  size_t size() const { return remaining_results_.size(); }

  template <typename T>
  const ValueRef& AllocateAt(int index) {
    // eagerly create a ValueRef
    if (remaining_results_[index].get()) return remaining_results_[index];
    remaining_results_[index] = ValueRef(new Value);
    return remaining_results_[index];
  }
  ValueRef& operator[](size_t i) const { return remaining_results_[i]; }

 private:
  llvm::MutableArrayRef<ValueRef> remaining_results_;
};

template <typename T>
class Result {
 public:
  explicit Result(ValueRef* result) : result_(result) {}

  template <typename... Args>
  void Emplace(Args&&... args) {
    ValueRef v;
    Set(T(std::forward<Args>(args)...));
  }

  void Set(Argument<T> argument) {
    CHECK(!result_->IsValid());
    *result_ = argument.value();
  }

 private:
  ValueRef* result_{};
};

template <typename T>
class Attribute {
 public:
  explicit Attribute(const Value* value) : value_(value) {}

  const T& get() const { return value_->get<T>(); }

 private:
  const Value* value_;
};

template <typename ViewT>
class ArgumentView {
  using UnderlyingT = typename ViewT::UnderlyingT;

 public:
  explicit ArgumentView(Value* value)
      : value_(value), arg_(&value->template get<UnderlyingT>()) {}

  Value* value() const { return value_; }
  ViewT& get() const { return arg_; }
  ViewT* operator->() const { return &get(); }
  ViewT& operator*() const { return get(); }

 private:
  Value* value_{};
  mutable ViewT arg_;
};

template <typename F, F f>
struct KernelImpl;

template <typename T>
struct TypeTag {};

#define INFRT_KERNEL(...)                                   \
  ::infrt::host_context::KernelImpl<decltype(&__VA_ARGS__), \
                                    &__VA_ARGS__>::Invoke

template <typename Return, typename... Args, Return (*impl_fn)(Args...)>
struct KernelImpl<Return (*)(Args...), impl_fn> {
  static void Invoke(KernelFrame* frame) {
    KernelCallHelper<Args..., TypeTag<int>>::template Invoke<0, 0, 0>(frame);
  }

  // Helper that introspects the arguments to derive the signature and cast
  // parts of the KernelFrame to their type before passing them to impl_fn.
  template <typename... RemainingArgs>
  struct KernelCallHelper;

  // Casts the return value of the kernel, if non-void.
  // bool _ is an unnecessary parameter to make compiler allow templace specific
  // in non-namespace scope.
  template <typename T, bool _>
  struct KernelReturnHelper {
    static void Invoke(KernelFrame* frame, const Args&... args) {
      HandleReturn(frame, impl_fn(args...));
    }
  };

  template <bool _>
  struct KernelReturnHelper<void, _> {
    static void Invoke(KernelFrame* frame, const Args&... args) {
      impl_fn(args...);
    }
  };

  // Specialization to cast a single input argument(Head).
  template <typename Head, typename... Tail>
  struct KernelCallHelper<Argument<Head>, Tail...> {
    template <int in_idx, int out_idx, int const_idx, typename... PreviousArgs>
    static void Invoke(KernelFrame* frame, const PreviousArgs&... pargs) {
      static_assert(in_idx != -1,
                    "Do not place Arguments after RemainingArguments");
      static_assert(out_idx == 0, "Arguments should appear before results");
      static_assert(const_idx == 0,
                    "Arguments and results should appear before attributes.");

      Argument<Head> arg(frame->GetArgAt(in_idx));
      KernelCallHelper<
          Tail...>::template Invoke<in_idx + 1, out_idx, const_idx>(frame,
                                                                    pargs...,
                                                                    arg);
    }
  };

  template <typename Head, typename... Tail>
  struct KernelCallHelper<ArgumentView<Head>, Tail...> {
    template <int in_idx, int out_idx, int const_idx, typename... PreviousArgs>
    static void Invoke(KernelFrame* frame, const PreviousArgs&... pargs) {
      static_assert(in_idx != -1,
                    "Do not place Arguments after RemainingArguments");
      static_assert(out_idx == 0, "Arguments should appear before results");
      static_assert(const_idx == 0,
                    "Arguments and results should appear before attributes.");

      ArgumentView<Head> arg(frame->GetArgAt(in_idx));
      KernelCallHelper<
          Tail...>::template Invoke<in_idx + 1, out_idx, const_idx>(frame,
                                                                    pargs...,
                                                                    arg);
    }
  };

  // Specialization to cast a single result argument (Head).
  template <typename Head, typename... Tail>
  struct KernelCallHelper<Result<Head>, Tail...> {
    template <int in_idx, int out_idx, int const_idx, typename... PreviousArgs>
    static void Invoke(KernelFrame* frame, const PreviousArgs&... pargs) {
      static_assert(out_idx != -1,
                    "Do not place Results after RemainingResults");
212 213 214 215 216
      // static_assert(const_idx == 0,
      //              "Arguments and results should appear before attributes");

      // Result<Head> arg(&frame->GetResults()[out_idx]);
      Result<Head> arg(new ValueRef());
Y
Yan Chunwei 已提交
217 218 219 220 221 222 223 224 225 226 227 228
      KernelCallHelper<
          Tail...>::template Invoke<in_idx, out_idx + 1, const_idx>(frame,
                                                                    pargs...,
                                                                    arg);
    }
  };

  // Specialization to cast a single attribute.
  template <typename Head, typename... Tail>
  struct KernelCallHelper<Attribute<Head>, Tail...> {
    template <int in_idx, int out_idx, int const_idx, typename... PreviousArgs>
    static void Invoke(KernelFrame* frame, const PreviousArgs&... pargs) {
229 230
      // static_assert(const_idx != -1,
      //              "Do not place Attributes after RemainingAttributes");
Y
Yan Chunwei 已提交
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
      Attribute<Head> arg(frame->GetAttributeAt(const_idx));
      KernelCallHelper<
          Tail...>::template Invoke<in_idx, out_idx, const_idx + 1>(frame,
                                                                    pargs...,
                                                                    arg);
    }
  };

  // Treat other pointer as an Argument.
  template <typename Head, typename... Tail>
  struct KernelCallHelper<Head*, Tail...> {
    template <int in_idx, int out_idx, int const_idx, typename... PreviousArgs>
    static void Invoke(KernelFrame* frame, const PreviousArgs&... pargs) {
      static_assert(in_idx != -1,
                    "Do not place Arguments after RemainingArguments");
      static_assert(out_idx == 0, "Arguments should appear before results");
247 248
      // static_assert(const_idx == 0,
      //              "Arguments and results should appear before attributes.");
249
      auto* arg = &frame->template GetElementAt<Head>(in_idx);
Y
Yan Chunwei 已提交
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
      KernelCallHelper<
          Tail...>::template Invoke<in_idx + 1, out_idx, const_idx>(frame,
                                                                    pargs...,
                                                                    arg);
    }
  };

  // Treat any other type as an Argument.
  template <typename Head, typename... Tail>
  struct KernelCallHelper<Head, Tail...> {
    using ArgT = std::decay_t<Head>;

    template <int in_idx, int out_idx, int const_idx, typename... PreviousArgs>
    static void Invoke(KernelFrame* frame, const PreviousArgs&... pargs) {
      static_assert(in_idx != -1,
                    "Do not place Arguments after RemainingArguments");
      static_assert(out_idx == 0, "Arguments should appear before results");
      static_assert(const_idx == 0,
                    "Arguments and results should appear before attributes.");

270
      auto* value = frame->GetElementAt(in_idx);
Y
Yan Chunwei 已提交
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354
      auto&& arg = value->get<ArgT>();

      KernelCallHelper<
          Tail...>::template Invoke<in_idx + 1, out_idx, const_idx>(frame,
                                                                    pargs...,
                                                                    arg);
    }
  };

  // RemainingArguments provides an ArrayRef<AsyncValue*> containing all
  // remaining arguments. Useful for variadic
  // kernels.
  template <typename... Tail>
  struct KernelCallHelper<RemainingArguments, Tail...> {
    template <int in_idx, int out_idx, int const_idx, typename... PreviousArgs>
    static void Invoke(KernelFrame* frame, const PreviousArgs&... pargs) {
      static_assert(in_idx != -1,
                    "Do not use more than one RemainingArguments");
      static_assert(out_idx == 0, "Arguments should appear before results.");
      static_assert(const_idx == 0,
                    "Arguments and results should appear before attributes");
      RemainingArguments remaining_arguments(
          frame->GetArguments().drop_front(in_idx));

      KernelCallHelper<Tail...>::template Invoke<-1, out_idx, const_idx>(
          frame, pargs..., remaining_arguments);
    }
  };

  // RemainingResults provides an MutableArrayRef<AsyncValue*> containing all
  // remaining results.
  template <typename... Tail>
  struct KernelCallHelper<RemainingResults, Tail...> {
    template <int in_idx, int out_idx, int const_idx, typename... PreviousArgs>
    static void Invoke(KernelFrame* frame, const PreviousArgs&... pargs) {
      static_assert(out_idx != -1, "Do not use more than one RemainingResults");
      static_assert(const_idx == 0,
                    "Arguments and results should appear before attributes");
      llvm::MutableArrayRef<Value*> returned_results =
          frame->GetResults().drop_front(out_idx);

      llvm::SmallVector<ValueRef, 4> result_values;
      for (size_t i = 0; i < returned_results.size(); i++)
        result_values.emplace_back(returned_results[i]);

      RemainingResults remaining_results(result_values);
      KernelCallHelper<Tail...>::template Invoke<in_idx, -1, const_idx>(
          frame, pargs..., remaining_results);
    }
  };

  // No arguments left.
  template <typename T>
  struct KernelCallHelper<TypeTag<T>> {
    template <int in_idx, int out_idx, int const_idx, typename... PreviousArgs>
    static void Invoke(KernelFrame* frame, const PreviousArgs&... pargs) {
      KernelReturnHelper<Return, false>::Invoke(frame, pargs...);
    }
  };

  // Handle pair result
  template <typename T0, typename T1>
  static void HandleReturn(KernelFrame* frame, std::pair<T0, T1>&& t) {
    CHECK_EQ(frame->GetNumResults(), 2);
    StoreResultAt(frame, 0, std::move(t.first));
    StoreResultAt(frame, 1, std::move(t.second));
  }

  // Store the function result back to the output Value in KernelFrame.
  template <typename T>
  static void HandleReturn(KernelFrame* frame, T&& t) {
    assert(frame->GetNumResults() == 1 && "Extra results passed to kernel.");
    StoreResultAt(frame, 0, std::forward<T>(t));
  }

  // Store result as an Value output in KernelFrame.
  template <typename T>
  static void StoreResultAt(KernelFrame* frame, int index, T&& t) {
    frame->EmplaceResult<std::decay_t<T>>(index, std::forward<T>(t));
  }
};

}  // namespace host_context
}  // namespace infrt