#include <torch/csrc/Device.h>
#include <torch/csrc/Exceptions.h>
#include <torch/csrc/utils/object_ptr.h>
#include <torch/csrc/utils/python_arg_parser.h>
#include <torch/csrc/utils/python_numbers.h>
#include <torch/csrc/utils/python_strings.h>
#include <c10/util/Exception.h>
#include <limits>
#include <sstream>
static PyObject* THPUpperModuleOfDevice = nullptr;
PyObject* THPDevice_New(const at::Device& device) {
auto type = &THPDeviceType;
auto self = THPObjectPtr{type->tp_alloc(type, 0)};
TORCH_CHECK_PYTHON(self);
auto self_ = reinterpret_cast<THPDevice*>(self.get());
self_->device = device;
return self.release();
}
static PyObject* THPDevice_repr(THPDevice* self) {
std::ostringstream oss;
oss << "device(type=\'" << self->device.type() << '\'';
if (self->device.has_index()) {
oss << ", index=" << static_cast<uint16_t>(self->device.index());
}
oss << ')';
return THPUtils_packString(std::move(oss).str().c_str());
}
static PyObject* THPDevice_str(THPDevice* self) {
std::ostringstream oss;
oss << self->device;
return THPUtils_packString(std::move(oss).str().c_str());
}
static PyObject* THPDevice_pynew(
PyTypeObject* ,
PyObject* args,
PyObject* kwargs) {
HANDLE_TH_ERRORS
static torch::PythonArgParser parser(
{"device(Device device)",
"device(std::string_view type, int64_t? index=-1)"});
torch::ParsedArgs<2> parsed_args;
auto r = parser.parse(args, kwargs, parsed_args);
if (r.has_torch_function()) {
return handle_torch_function(
r, nullptr, args, kwargs, THPUpperModuleOfDevice, "torch");
}
if (r.idx == 0) {
auto device = r.device(0);
return THPDevice_New(device);
} else if (r.idx == 1) {
auto as_device = r.device(0);
if (as_device.has_index()) {
auto device_type = r.string(0);
TORCH_CHECK(
false,
"type (string) must not include an index because index "
"was passed explicitly: " +
device_type);
}
int64_t device_index = -1;
if (!r.isNone(1)) {
device_index = r.toInt64(1);
TORCH_CHECK(device_index >= 0, "Device index must not be negative");
}
at::Device device(
as_device.type(), static_cast<c10::DeviceIndex>(device_index));
return THPDevice_New(device);
}
Py_RETURN_NONE;
END_HANDLE_TH_ERRORS
}
static PyObject* THPDevice_type(THPDevice* self, PyObject* ) {
HANDLE_TH_ERRORS
std::ostringstream oss;
oss << self->device.type();
return THPUtils_packString(std::move(oss).str().c_str());
Py_RETURN_NONE;
END_HANDLE_TH_ERRORS
}
static PyObject* THPDevice_index(THPDevice* self, PyObject* ) {
HANDLE_TH_ERRORS
if (self->device.has_index()) {
return THPUtils_packInt64(self->device.index());
} else {
Py_RETURN_NONE;
}
END_HANDLE_TH_ERRORS
}
static Py_ssize_t THPDevice_hash(THPDevice* self) {
HANDLE_TH_ERRORS
return static_cast<Py_ssize_t>(
std::hash<at::Device>{}(self->device) %
std::numeric_limits<Py_ssize_t>::max());
END_HANDLE_TH_ERRORS_RET(-1)
}
static PyObject* THPDevice_rc(PyObject* a, PyObject* b, int op) {
HANDLE_TH_ERRORS
if (!THPDevice_Check(a) || !THPDevice_Check(b)) {
Py_RETURN_NOTIMPLEMENTED;
}
THPDevice* da = reinterpret_cast<THPDevice*>(a);
THPDevice* db = reinterpret_cast<THPDevice*>(b);
switch (op) {
case Py_EQ:
if (da->device == db->device) {
Py_RETURN_TRUE;
} else {
Py_RETURN_FALSE;
}
case Py_NE:
if (da->device == db->device) {
Py_RETURN_FALSE;
} else {
Py_RETURN_TRUE;
}
case Py_LT:
case Py_LE:
case Py_GT:
case Py_GE:
TORCH_CHECK_TYPE(false, "comparison not implemented");
default:
TORCH_CHECK_TYPE(false, "unexpected comparison op");
}
END_HANDLE_TH_ERRORS
}
static PyObject* THPDevice_reduce(PyObject* _self, PyObject* ) {
HANDLE_TH_ERRORS
auto self = reinterpret_cast<THPDevice*>(_self);
auto ret = THPObjectPtr{PyTuple_New(2)};
TORCH_CHECK_PYTHON(ret);
py::object torch_module = py::module::import("torch");
py::object torch_device = torch_module.attr("device");
PyTuple_SET_ITEM(ret.get(), 0, torch_device.release().ptr());
THPObjectPtr args;
std::ostringstream oss;
oss << self->device.type();
if (self->device.has_index()) {
args = THPObjectPtr{Py_BuildValue(
"(si)", oss.str().c_str(), static_cast<int>(self->device.index()))};
} else {
args = THPObjectPtr{Py_BuildValue("(s)", std::move(oss).str().c_str())};
}
TORCH_CHECK_PYTHON(args);
PyTuple_SET_ITEM(ret.get(), 1, args.release());
return ret.release();
END_HANDLE_TH_ERRORS
}
static PyObject* THPDevice_enter(PyObject* self, PyObject* ) {
HANDLE_TH_ERRORS
py::object mode = py::module::import("torch.utils._device")
.attr("DeviceContext")(py::handle(self));
at::impl::PythonTorchFunctionTLS::push_onto_stack(
std::make_shared<c10::SafePyObject>(
mode.release().ptr(), getPyInterpreter()));
return Py_NewRef(self);
END_HANDLE_TH_ERRORS
}
static PyObject* THPDevice_exit(PyObject* , PyObject* ) {
HANDLE_TH_ERRORS
at::impl::PythonTorchFunctionTLS::pop_stack();
Py_RETURN_NONE;
END_HANDLE_TH_ERRORS
}
// TODO: We're not sure if this is a good idea or not, because making
// torch.device callable means that it will start returning true
// for callable() queries, and that is unexpected. We can always add
// this later, so for now, don't actually implement this.
static PyObject* THPDevice_call(
PyObject* self,
PyObject* args,
PyObject* kwargs) {
HANDLE_TH_ERRORS
py::object deco =
py::module::import("torch.utils._device").attr("device_decorator");
return deco(py::handle(self), *py::handle(args), **py::handle(kwargs))
.release()
.ptr();
END_HANDLE_TH_ERRORS
}
*/
typedef PyObject* (*getter)(PyObject*, void*);
static const std::initializer_list<PyGetSetDef> THPDevice_properties = {
{"type",
reinterpret_cast<getter>(THPDevice_type),
nullptr,
nullptr,
nullptr},
{"index",
reinterpret_cast<getter>(THPDevice_index),
nullptr,
nullptr,
nullptr},
{nullptr}};
static const std::initializer_list<PyMethodDef> THPDevice_methods = {
{"__reduce__", THPDevice_reduce, METH_NOARGS, nullptr},
{"__enter__", THPDevice_enter, METH_NOARGS, nullptr},
{"__exit__", THPDevice_exit, METH_VARARGS, nullptr},
{nullptr}
};
PyTypeObject THPDeviceType = {
PyVarObject_HEAD_INIT(nullptr, 0)
"torch.device",
sizeof(THPDevice),
0,
nullptr,
0,
nullptr,
nullptr,
nullptr,
reinterpret_cast<reprfunc>(THPDevice_repr),
nullptr,
nullptr,
nullptr,
reinterpret_cast<hashfunc>(THPDevice_hash),
nullptr,
reinterpret_cast<reprfunc>(THPDevice_str),
nullptr,
nullptr,
nullptr,
Py_TPFLAGS_DEFAULT,
nullptr,
nullptr,
nullptr,
static_cast<richcmpfunc>(THPDevice_rc),
0,
nullptr,
nullptr,
const_cast<PyMethodDef*>(std::data(THPDevice_methods)),
nullptr,
const_cast<PyGetSetDef*>(std::data(THPDevice_properties)),
nullptr,
nullptr,
nullptr,
nullptr,
0,
nullptr,
nullptr,
THPDevice_pynew,
};
void THPDevice_init(PyObject* module) {
THPUpperModuleOfDevice = module;
TORCH_CHECK_PYTHON(PyModule_AddType(module, &THPDeviceType) >= 0);
}