2020-09-26 09:13:16 +08:00
|
|
|
//===- acap_dispatch.h ------------------------------------------*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// This file is licensed under a pytorch-style license
|
|
|
|
// See frontends/pytorch/LICENSE for license information.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// "ATen Capture" dispatcher: Defines facility for capturing programs by
|
|
|
|
// registering dispatch keys to intercept op execution.
|
|
|
|
// References:
|
|
|
|
// http://blog.ezyang.com/2020/09/lets-talk-about-the-pytorch-dispatcher/
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2020-09-29 09:36:00 +08:00
|
|
|
#ifndef NPCOMP_FRONTENDS_PYTORCH_CSRC_C10_DISPATCH_ACAP_DISPATCH_H
|
|
|
|
#define NPCOMP_FRONTENDS_PYTORCH_CSRC_C10_DISPATCH_ACAP_DISPATCH_H
|
|
|
|
|
2020-09-26 09:13:16 +08:00
|
|
|
#include <list>
|
|
|
|
#include <memory>
|
|
|
|
|
2020-10-02 09:59:58 +08:00
|
|
|
#include "../pybind.h"
|
|
|
|
|
|
|
|
#include "func_builder.h"
|
2020-09-26 09:13:16 +08:00
|
|
|
|
2020-09-29 09:36:00 +08:00
|
|
|
#include "mlir-c/IR.h"
|
|
|
|
|
2020-09-26 09:13:16 +08:00
|
|
|
#include <ATen/core/dispatch/Dispatcher.h>
|
2020-10-06 14:21:21 +08:00
|
|
|
#include <ATen/core/ivalue.h>
|
2020-09-26 09:13:16 +08:00
|
|
|
#include <c10/core/impl/LocalDispatchKeySet.h>
|
|
|
|
|
|
|
|
namespace torch_mlir {
|
|
|
|
|
|
|
|
/// Main entry point for managing device capture.
|
|
|
|
class AcapController : public std::enable_shared_from_this<AcapController> {
|
|
|
|
public:
|
2020-10-06 14:21:21 +08:00
|
|
|
AcapController(TypeMapper &typeMapper,
|
|
|
|
std::unique_ptr<FuncBuilder> funcBuilder)
|
|
|
|
: typeMapper(typeMapper), funcBuilder(std::move(funcBuilder)) {}
|
2020-09-26 09:13:16 +08:00
|
|
|
|
|
|
|
// Enter and exit the context manager.
|
|
|
|
pybind11::object contextEnter();
|
|
|
|
void contextExit(pybind11::object exc_type, pybind11::object exc_val,
|
|
|
|
pybind11::object exc_tb);
|
|
|
|
|
2020-10-06 14:21:21 +08:00
|
|
|
// Terminates capture and returns tensors from the function.
|
|
|
|
void returns(std::vector<at::Tensor> tensors);
|
|
|
|
|
2020-09-26 09:13:16 +08:00
|
|
|
// Gets and clears the current debug log.
|
|
|
|
std::vector<std::string> getDebugLog();
|
|
|
|
|
|
|
|
// Returns the current AcapController (if it has been activated on this
|
|
|
|
// thread. Returns nullptr if none.
|
|
|
|
static std::shared_ptr<AcapController> getCurrent();
|
|
|
|
|
|
|
|
// The fallback boxed kernel that we route captured dispatches through.
|
|
|
|
static void fallbackKernel(const c10::OperatorHandle &opHandle,
|
|
|
|
c10::Stack *stack);
|
|
|
|
|
|
|
|
private:
|
2020-10-06 14:21:21 +08:00
|
|
|
void redispatch(const c10::OperatorHandle &opHandle, c10::Stack *stack);
|
|
|
|
void fallbackKernelImpl(const c10::OperatorHandle &opHandle,
|
|
|
|
c10::Stack *stack);
|
|
|
|
MlirValue mapIValueToMlirValue(MlirLocation loc, c10::IValue &ival);
|
|
|
|
MlirType mapIValueToMlirType(MlirLocation loc, c10::IValue &ival);
|
|
|
|
void verifyHasNotReturned();
|
2020-09-26 09:13:16 +08:00
|
|
|
struct Activation {
|
|
|
|
Activation(std::shared_ptr<AcapController> controller)
|
|
|
|
: controller(std::move(controller)) {}
|
|
|
|
std::shared_ptr<AcapController> controller;
|
|
|
|
// The RAII dispatch key guard is not movable, so heap allocate it. This is
|
|
|
|
// a bit outside of its intended design, but since this is thread local as
|
|
|
|
// well, it should be fine.
|
|
|
|
std::unique_ptr<c10::impl::IncludeDispatchKeyGuard> dispatchGuard;
|
|
|
|
};
|
|
|
|
// Gets the thread local stack of active acap controllers.
|
|
|
|
static std::list<Activation> &getThreadLocalActiveStack();
|
2020-09-29 09:36:00 +08:00
|
|
|
|
2020-10-06 14:21:21 +08:00
|
|
|
TypeMapper &typeMapper;
|
2020-10-02 09:59:58 +08:00
|
|
|
std::unique_ptr<FuncBuilder> funcBuilder;
|
2020-09-26 09:13:16 +08:00
|
|
|
std::vector<std::string> captureLog;
|
2020-10-06 14:21:21 +08:00
|
|
|
bool hasReturned = false;
|
2020-09-26 09:13:16 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace torch_mlir
|
2020-09-29 09:36:00 +08:00
|
|
|
|
|
|
|
#endif // NPCOMP_FRONTENDS_PYTORCH_CSRC_C10_DISPATCH_ACAP_DISPATCH_H
|