#include <torch/csrc/Device.h>
#include <torch/csrc/Event.h>
#include <torch/csrc/Stream.h>
#include <torch/csrc/utils/pycfunction_helpers.h>
#include <torch/csrc/utils/python_arg_parser.h>
#include <c10/core/Event.h>
#include <c10/core/Stream.h>
#include <c10/core/DeviceType.h>
#include <c10/core/impl/DeviceGuardImplInterface.h>
#include <string>
PyTypeObject* THPEventClass = nullptr;
static PyObject* THPEvent_pynew(
PyTypeObject* type,
PyObject* args,
PyObject* kwargs) {
HANDLE_TH_ERRORS
unsigned char enable_timing = 0;
unsigned char blocking = 0;
unsigned char interprocess = 0;
static torch::PythonArgParser parser({
"Event(Device device=None, *, bool enable_timing=False, bool blocking=False, bool interprocess=False)",
});
torch::ParsedArgs<4> parsed_args;
auto r = parser.parse(args, kwargs, parsed_args);
auto device = r.deviceOptional(0);
if (!device.has_value()) {
device = at::Device(at::getAccelerator(false).value_or(at::kCPU));
}
enable_timing = r.toBoolWithDefault(1, false);
blocking = r.toBoolWithDefault(2, false);
interprocess = r.toBoolWithDefault(3, false);
THPObjectPtr ptr(type->tp_alloc(type, 0));
if (!ptr) {
TORCH_CHECK(ptr, "Failed to allocate memory for Event");
}
THPEvent* self = reinterpret_cast<THPEvent*>(ptr.get());
self->weakreflist = nullptr;
const c10::EventFlag flag =
(enable_timing ? c10::EventFlag::TIMING
: c10::EventFlag::PYTORCH_DEFAULT) |
(blocking ? c10::EventFlag::BLOCKING : c10::EventFlag::PYTORCH_DEFAULT) |
(interprocess ? c10::EventFlag::INTERPROCESS
: c10::EventFlag::PYTORCH_DEFAULT);
new (&self->event) c10::Event(device->type(), flag);
return static_cast<PyObject*>(ptr.release());
END_HANDLE_TH_ERRORS
}
PyObject* THPEvent_new(c10::DeviceType device_type, c10::EventFlag flag) {
auto type = &THPEventType;
auto self = THPObjectPtr{type->tp_alloc(type, 0)};
TORCH_CHECK(self, "Failed to allocate memory for Event");
auto self_ = reinterpret_cast<THPEvent*>(self.get());
self_->weakreflist = nullptr;
new (&self_->event) c10::Event(device_type, flag);
return self.release();
}
void THPEvent_dealloc_common(THPEvent* self) {
PyObject_ClearWeakRefs((PyObject*)self);
Py_TYPE(self)->tp_free(reinterpret_cast<PyObject*>(self));
}
static void THPEvent_dealloc(THPEvent* self) {
{
pybind11::gil_scoped_release no_gil{};
self->event.~Event();
}
THPEvent_dealloc_common(self);
}
static PyObject* THPEvent_get_device(THPEvent* self, void* ) {
HANDLE_TH_ERRORS
return THPDevice_New(self->event.device());
END_HANDLE_TH_ERRORS
}
static PyObject* THPEvent_record(
PyObject* _self,
PyObject* args,
PyObject* kwargs) {
HANDLE_TH_ERRORS
auto self = reinterpret_cast<THPEvent*>(_self);
PyObject* _stream = Py_None;
constexpr const char* accepted_args[] = {"stream", nullptr};
if (!PyArg_ParseTupleAndKeywords(
args,
kwargs,
"|O",
const_cast<char**>(accepted_args),
&_stream)) {
TORCH_WARN("Parsing THPEvent_record arg fails");
return nullptr;
}
if (!Py_IsNone(_stream)) {
auto stream = reinterpret_cast<THPStream*>(_stream);
self->event.record(c10::Stream::unpack3(
stream->stream_id,
static_cast<c10::DeviceIndex>(stream->device_index),
static_cast<c10::DeviceType>(stream->device_type)));
} else {
c10::impl::VirtualGuardImpl impl{
static_cast<c10::DeviceType>(self->event.device_type())};
self->event.record(impl.getStream(impl.getDevice()));
}
Py_RETURN_NONE;
END_HANDLE_TH_ERRORS
}
static PyObject* THPEvent_from_ipc_handle(
PyObject* _type,
PyObject* args,
PyObject* kwargs) {
HANDLE_TH_ERRORS
auto type = reinterpret_cast<PyTypeObject*>(_type);
static torch::PythonArgParser parser({
"from_ipc_handle(Device device, std::string ipc_handle)",
});
torch::ParsedArgs<2> parsed_args;
auto r = parser.parse(args, kwargs, parsed_args);
at::Device device = r.device(0);
std::string handle_string = r.string(1);
const auto acc_type = at::accelerator::getAccelerator(true).value();
TORCH_CHECK(
device.type() == acc_type,
"IPC Event can only be created on ",
acc_type,
" devices, but got device type ",
device.type());
THPObjectPtr ptr(type->tp_alloc(type, 0));
if (!ptr) {
return nullptr;
}
THPEvent* self = reinterpret_cast<THPEvent*>(ptr.get());
new (&self->event) c10::Event(device, handle_string);
return static_cast<PyObject*>(ptr.release());
END_HANDLE_TH_ERRORS
}
static PyObject* THPEvent_ipc_handle(
PyObject* _self [[maybe_unused]],
PyObject* ) {
HANDLE_TH_ERRORS
auto self = reinterpret_cast<THPEvent*>(_self);
auto handle = self->event.ipcHandle();
return PyBytes_FromStringAndSize(
handle.c_str(), static_cast<Py_ssize_t>(handle.size()));
END_HANDLE_TH_ERRORS
}
static PyObject* THPEvent_wait(
PyObject* _self,
PyObject* args,
PyObject* kwargs) {
HANDLE_TH_ERRORS {
auto self = reinterpret_cast<THPEvent*>(_self);
PyObject* _stream = Py_None;
constexpr const char* accepted_args[] = {"stream", nullptr};
if (!PyArg_ParseTupleAndKeywords(
args,
kwargs,
"|O",
const_cast<char**>(accepted_args),
&_stream)) {
TORCH_WARN("Parsing THPEvent_wait arg fails");
return nullptr;
}
if (!Py_IsNone(_stream)) {
auto stream = reinterpret_cast<THPStream*>(_stream);
self->event.block(c10::Stream::unpack3(
stream->stream_id,
static_cast<c10::DeviceIndex>(stream->device_index),
static_cast<c10::DeviceType>(stream->device_type)));
} else {
c10::impl::VirtualGuardImpl impl{
static_cast<c10::DeviceType>(self->event.device_type())};
self->event.block(impl.getStream(impl.getDevice()));
}
}
Py_RETURN_NONE;
END_HANDLE_TH_ERRORS
}
static PyObject* THPEvent_query(PyObject* _self, PyObject* ) {
HANDLE_TH_ERRORS
auto self = reinterpret_cast<THPEvent*>(_self);
return PyBool_FromLong(self->event.query());
END_HANDLE_TH_ERRORS
}
static PyObject* THPEvent_elapsed_time(PyObject* _self, PyObject* _other) {
HANDLE_TH_ERRORS
auto self = reinterpret_cast<THPEvent*>(_self);
TORCH_CHECK(
Py_TYPE(_other) == THPEventClass,
"expected other to be a torch.Event object");
auto other = reinterpret_cast<THPEvent*>(_other);
return PyFloat_FromDouble(self->event.elapsedTime(other->event));
END_HANDLE_TH_ERRORS
}
static PyObject* THPEvent_synchronize(PyObject* _self, PyObject* ) {
HANDLE_TH_ERRORS {
pybind11::gil_scoped_release no_gil{};
auto self = reinterpret_cast<THPEvent*>(_self);
self->event.synchronize();
}
Py_RETURN_NONE;
END_HANDLE_TH_ERRORS
}
static PyObject* THPEvent_evend_id(PyObject* _self, PyObject* ) {
HANDLE_TH_ERRORS
auto self = reinterpret_cast<THPEvent*>(_self);
return PyLong_FromVoidPtr(self->event.eventId());
END_HANDLE_TH_ERRORS
}
static PyObject* THPEvent_repr(THPEvent* self) {
HANDLE_TH_ERRORS
return THPUtils_packString(
"torch.Event device_type=" +
c10::DeviceTypeName(
static_cast<c10::DeviceType>(self->event.device_type()), true) +
", device_index=" + std::to_string(self->event.device_index()) +
", event_flag=" +
std::to_string(static_cast<int64_t>(self->event.flag())) + ", event_id=" +
std::to_string(reinterpret_cast<int64_t>(self->event.eventId())));
END_HANDLE_TH_ERRORS
}
static struct PyGetSetDef THPEvent_properties[] = {
{"device",
reinterpret_cast<getter>(THPEvent_get_device),
nullptr,
nullptr,
nullptr},
{"event_id",
reinterpret_cast<getter>(THPEvent_evend_id),
nullptr,
nullptr,
nullptr},
{nullptr}};
static PyMethodDef THPEvent_methods[] = {
{"from_ipc_handle",
castPyCFunctionWithKeywords(THPEvent_from_ipc_handle),
METH_CLASS | METH_VARARGS | METH_KEYWORDS,
nullptr},
{"record",
castPyCFunctionWithKeywords(THPEvent_record),
METH_VARARGS | METH_KEYWORDS,
nullptr},
{"wait",
castPyCFunctionWithKeywords(THPEvent_wait),
METH_VARARGS | METH_KEYWORDS,
nullptr},
{"query", THPEvent_query, METH_NOARGS, nullptr},
{"elapsed_time", THPEvent_elapsed_time, METH_O, nullptr},
{"synchronize", THPEvent_synchronize, METH_NOARGS, nullptr},
{"ipc_handle", THPEvent_ipc_handle, METH_NOARGS, nullptr},
{nullptr}};
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Winvalid-offsetof"
PyTypeObject THPEventType = {
PyVarObject_HEAD_INIT(nullptr, 0)
"torch.Event",
sizeof(THPEvent),
0,
reinterpret_cast<destructor>(THPEvent_dealloc),
0,
nullptr,
nullptr,
nullptr,
reinterpret_cast<reprfunc>(THPEvent_repr),
nullptr,
nullptr,
nullptr,
nullptr,
nullptr,
nullptr,
nullptr,
nullptr,
nullptr,
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
nullptr,
nullptr,
nullptr,
nullptr,
offsetof(THPEvent, weakreflist),
nullptr,
nullptr,
THPEvent_methods,
nullptr,
THPEvent_properties,
nullptr,
nullptr,
nullptr,
nullptr,
0,
nullptr,
nullptr,
THPEvent_pynew,
};
#pragma GCC diagnostic pop
void THPEvent_init(PyObject* module) {
THPEventClass = &THPEventType;
TORCH_CHECK_PYTHON(PyModule_AddType(module, &THPEventType) >= 0);
}