|
|
#pragma once
|
|
|
|
|
|
#include <c10/core/Device.h>
|
|
|
#include <c10/util/Exception.h>
|
|
|
|
|
|
#include <c10/core/Stream.h>
|
|
|
#include <c10/util/Registry.h>
|
|
|
|
|
|
#include <c10/core/Allocator.h>
|
|
|
|
|
|
#include <c10/util/python_stub.h>
|
|
|
#include <ATen/detail/AcceleratorHooksInterface.h>
|
|
|
|
|
|
#include <string>
|
|
|
namespace at {
|
|
|
class Context;
|
|
|
}
|
|
|
|
|
|
namespace at {
|
|
|
constexpr const char* MTIA_HELP =
|
|
|
"The MTIA backend requires MTIA extension for PyTorch;"
|
|
|
"this error has occurred because you are trying "
|
|
|
"to use some MTIA's functionality without MTIA extension included.";
|
|
|
|
|
|
struct TORCH_API MTIAHooksInterface : AcceleratorHooksInterface {
|
|
|
|
|
|
|
|
|
#define FAIL_MTIAHOOKS_FUNC(func) \
|
|
|
TORCH_CHECK(false, "Cannot execute ", func, "() without MTIA backend.");
|
|
|
|
|
|
~MTIAHooksInterface() override = default;
|
|
|
|
|
|
void init() const override {
|
|
|
|
|
|
|
|
|
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
virtual bool hasMTIA() const {
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
DeviceIndex deviceCount() const override {
|
|
|
return 0;
|
|
|
}
|
|
|
|
|
|
virtual void deviceSynchronize(c10::DeviceIndex ) const {
|
|
|
FAIL_MTIAHOOKS_FUNC(__func__);
|
|
|
}
|
|
|
|
|
|
virtual std::string showConfig() const {
|
|
|
FAIL_MTIAHOOKS_FUNC(__func__);
|
|
|
}
|
|
|
|
|
|
bool hasPrimaryContext(DeviceIndex ) const override {
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
void setCurrentDevice(DeviceIndex ) const override {
|
|
|
FAIL_MTIAHOOKS_FUNC(__func__);
|
|
|
}
|
|
|
|
|
|
DeviceIndex getCurrentDevice() const override {
|
|
|
FAIL_MTIAHOOKS_FUNC(__func__);
|
|
|
return -1;
|
|
|
}
|
|
|
|
|
|
DeviceIndex exchangeDevice(DeviceIndex ) const override {
|
|
|
FAIL_MTIAHOOKS_FUNC(__func__);
|
|
|
return -1;
|
|
|
}
|
|
|
|
|
|
DeviceIndex maybeExchangeDevice(DeviceIndex ) const override {
|
|
|
FAIL_MTIAHOOKS_FUNC(__func__);
|
|
|
return -1;
|
|
|
}
|
|
|
|
|
|
virtual c10::Stream getCurrentStream(DeviceIndex ) const {
|
|
|
FAIL_MTIAHOOKS_FUNC(__func__);
|
|
|
return c10::Stream::unpack3(-1, 0, c10::DeviceType::MTIA);
|
|
|
}
|
|
|
|
|
|
virtual int64_t getCurrentRawStream(DeviceIndex ) const {
|
|
|
FAIL_MTIAHOOKS_FUNC(__func__);
|
|
|
return -1;
|
|
|
}
|
|
|
|
|
|
virtual c10::Stream getDefaultStream(DeviceIndex ) const {
|
|
|
FAIL_MTIAHOOKS_FUNC(__func__);
|
|
|
return c10::Stream::unpack3(-1, 0, c10::DeviceType::MTIA);
|
|
|
}
|
|
|
|
|
|
virtual void setCurrentStream(const c10::Stream& ) const {
|
|
|
FAIL_MTIAHOOKS_FUNC(__func__);
|
|
|
}
|
|
|
|
|
|
bool isPinnedPtr(const void* ) const override {
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
Allocator* getPinnedMemoryAllocator() const override {
|
|
|
FAIL_MTIAHOOKS_FUNC(__func__);
|
|
|
return nullptr;
|
|
|
}
|
|
|
|
|
|
virtual PyObject* memoryStats(DeviceIndex ) const {
|
|
|
FAIL_MTIAHOOKS_FUNC(__func__);
|
|
|
return nullptr;
|
|
|
}
|
|
|
|
|
|
virtual PyObject* getDeviceCapability(DeviceIndex ) const {
|
|
|
FAIL_MTIAHOOKS_FUNC(__func__);
|
|
|
return nullptr;
|
|
|
}
|
|
|
|
|
|
virtual PyObject* getDeviceProperties(DeviceIndex device) const {
|
|
|
FAIL_MTIAHOOKS_FUNC(__func__);
|
|
|
return nullptr;
|
|
|
}
|
|
|
|
|
|
virtual void emptyCache() const {
|
|
|
FAIL_MTIAHOOKS_FUNC(__func__);
|
|
|
}
|
|
|
|
|
|
|
|
|
virtual void recordMemoryHistory(
|
|
|
const std::optional<std::string>& ,
|
|
|
const std::string& ,
|
|
|
size_t ) const {
|
|
|
FAIL_MTIAHOOKS_FUNC(__func__);
|
|
|
}
|
|
|
|
|
|
virtual PyObject* memorySnapshot(const std::optional<std::string>& local_path) const {
|
|
|
FAIL_MTIAHOOKS_FUNC(__func__);
|
|
|
return nullptr;
|
|
|
}
|
|
|
|
|
|
virtual DeviceIndex getDeviceCount() const {
|
|
|
FAIL_MTIAHOOKS_FUNC(__func__);
|
|
|
return 0;
|
|
|
}
|
|
|
|
|
|
virtual void resetPeakMemoryStats(DeviceIndex ) const {
|
|
|
FAIL_MTIAHOOKS_FUNC(__func__);
|
|
|
}
|
|
|
|
|
|
virtual void attachOutOfMemoryObserver(PyObject* observer) const {
|
|
|
FAIL_MTIAHOOKS_FUNC(__func__);
|
|
|
return;
|
|
|
}
|
|
|
};
|
|
|
|
|
|
struct TORCH_API MTIAHooksArgs {};
|
|
|
|
|
|
TORCH_DECLARE_REGISTRY(MTIAHooksRegistry, MTIAHooksInterface, MTIAHooksArgs);
|
|
|
#define REGISTER_MTIA_HOOKS(clsname) \
|
|
|
C10_REGISTER_CLASS(MTIAHooksRegistry, clsname, clsname)
|
|
|
|
|
|
namespace detail {
|
|
|
TORCH_API const MTIAHooksInterface& getMTIAHooks();
|
|
|
TORCH_API bool isMTIAHooksBuilt();
|
|
|
}
|
|
|
}
|
|
|
|