#include <torch/csrc/MemoryFormat.h>
#include <torch/csrc/Exceptions.h>
#include <torch/csrc/utils/object_ptr.h>
#include <torch/csrc/utils/python_strings.h>
#include <torch/csrc/utils/refcount_contention.h>
#include <cstring>
#include <string>
PyObject* THPMemoryFormat_New(
at::MemoryFormat memory_format,
const std::string& name) {
auto type = &THPMemoryFormatType;
auto self = THPObjectPtr{type->tp_alloc(type, 0)};
TORCH_CHECK_PYTHON(self);
auto self_ = reinterpret_cast<THPMemoryFormat*>(self.get());
self_->memory_format = memory_format;
std::strncpy(self_->name, name.c_str(), MEMORY_FORMAT_NAME_LEN);
self_->name[MEMORY_FORMAT_NAME_LEN] = '\0';
torch::utils::set_immortal_if_possible(self.get());
return self.release();
}
static PyObject* THPMemoryFormat_repr(THPMemoryFormat* self) {
return THPUtils_packString(self->name);
}
static PyObject* THPMemoryFormat_reduce(PyObject* _self, PyObject* ) {
auto* self = reinterpret_cast<THPMemoryFormat*>(_self);
return THPUtils_packString(self->name);
}
static PyMethodDef THPMemoryFormat_methods[] = {
{"__reduce__", THPMemoryFormat_reduce, METH_NOARGS, nullptr},
{nullptr}
};
PyTypeObject THPMemoryFormatType = {
PyVarObject_HEAD_INIT(nullptr, 0)
"torch.memory_format",
sizeof(THPMemoryFormat),
0,
nullptr,
0,
nullptr,
nullptr,
nullptr,
reinterpret_cast<reprfunc>(THPMemoryFormat_repr),
nullptr,
nullptr,
nullptr,
nullptr,
nullptr,
nullptr,
nullptr,
nullptr,
nullptr,
Py_TPFLAGS_DEFAULT,
nullptr,
nullptr,
nullptr,
nullptr,
0,
nullptr,
nullptr,
THPMemoryFormat_methods,
nullptr,
nullptr,
nullptr,
nullptr,
nullptr,
nullptr,
0,
nullptr,
nullptr,
nullptr,
};
void THPMemoryFormat_init(PyObject* module) {
TORCH_CHECK_PYTHON(PyModule_AddType(module, &THPMemoryFormatType) >= 0);
}