xref: /aosp_15_r20/external/pytorch/torch/csrc/Layout.cpp (revision da0073e96a02ea20f0ac840b70461e3646d07c45)
1 #include <torch/csrc/Layout.h>
2 
3 #include <torch/csrc/Exceptions.h>
4 #include <torch/csrc/utils/object_ptr.h>
5 #include <torch/csrc/utils/python_strings.h>
6 
7 #include <ATen/Layout.h>
8 
9 #include <structmember.h>
10 #include <cstring>
11 #include <string>
12 
THPLayout_New(at::Layout layout,const std::string & name)13 PyObject* THPLayout_New(at::Layout layout, const std::string& name) {
14   auto type = (PyTypeObject*)&THPLayoutType;
15   auto self = THPObjectPtr{type->tp_alloc(type, 0)};
16   if (!self)
17     throw python_error();
18   auto self_ = reinterpret_cast<THPLayout*>(self.get());
19   self_->layout = layout;
20   std::strncpy(self_->name, name.c_str(), LAYOUT_NAME_LEN);
21   self_->name[LAYOUT_NAME_LEN] = '\0';
22   return self.release();
23 }
24 
THPLayout_repr(THPLayout * self)25 PyObject* THPLayout_repr(THPLayout* self) {
26   return THPUtils_packString(self->name);
27 }
28 
29 PyTypeObject THPLayoutType = {
30     PyVarObject_HEAD_INIT(nullptr, 0) "torch.layout", /* tp_name */
31     sizeof(THPLayout), /* tp_basicsize */
32     0, /* tp_itemsize */
33     nullptr, /* tp_dealloc */
34     0, /* tp_vectorcall_offset */
35     nullptr, /* tp_getattr */
36     nullptr, /* tp_setattr */
37     nullptr, /* tp_reserved */
38     (reprfunc)THPLayout_repr, /* tp_repr */
39     nullptr, /* tp_as_number */
40     nullptr, /* tp_as_sequence */
41     nullptr, /* tp_as_mapping */
42     nullptr, /* tp_hash  */
43     nullptr, /* tp_call */
44     nullptr, /* tp_str */
45     nullptr, /* tp_getattro */
46     nullptr, /* tp_setattro */
47     nullptr, /* tp_as_buffer */
48     Py_TPFLAGS_DEFAULT, /* tp_flags */
49     nullptr, /* tp_doc */
50     nullptr, /* tp_traverse */
51     nullptr, /* tp_clear */
52     nullptr, /* tp_richcompare */
53     0, /* tp_weaklistoffset */
54     nullptr, /* tp_iter */
55     nullptr, /* tp_iternext */
56     nullptr, /* tp_methods */
57     nullptr, /* tp_members */
58     nullptr, /* tp_getset */
59     nullptr, /* tp_base */
60     nullptr, /* tp_dict */
61     nullptr, /* tp_descr_get */
62     nullptr, /* tp_descr_set */
63     0, /* tp_dictoffset */
64     nullptr, /* tp_init */
65     nullptr, /* tp_alloc */
66     nullptr, /* tp_new */
67 };
68 
THPLayout_init(PyObject * module)69 void THPLayout_init(PyObject* module) {
70   if (PyType_Ready(&THPLayoutType) < 0) {
71     throw python_error();
72   }
73   Py_INCREF(&THPLayoutType);
74   if (PyModule_AddObject(module, "layout", (PyObject*)&THPLayoutType) != 0) {
75     throw python_error();
76   }
77 }
78