1 | #include <torch/csrc/QScheme.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 <c10/core/QScheme.h> |
8 | |
9 | #include <structmember.h> |
10 | #include <cstring> |
11 | #include <string> |
12 | |
13 | PyObject* THPQScheme_New(at::QScheme qscheme, const std::string& name) { |
14 | auto type = (PyTypeObject*)&THPQSchemeType; |
15 | auto self = THPObjectPtr{type->tp_alloc(type, 0)}; |
16 | if (!self) |
17 | throw python_error(); |
18 | auto self_ = reinterpret_cast<THPQScheme*>(self.get()); |
19 | self_->qscheme = qscheme; |
20 | std::strncpy(self_->name, name.c_str(), QSCHEME_NAME_LEN); |
21 | self_->name[QSCHEME_NAME_LEN] = '\0'; |
22 | return self.release(); |
23 | } |
24 | |
25 | PyObject* THPQScheme_reduce(PyObject* _self, PyObject* noargs) { |
26 | auto self = (THPQScheme*)_self; |
27 | return THPUtils_packString(self->name); |
28 | } |
29 | |
30 | // NOLINTNEXTLINE(cppcoreguidelines-avoid-c-arrays,cppcoreguidelines-avoid-non-const-global-variables,modernize-avoid-c-arrays) |
31 | static PyMethodDef THPQScheme_methods[] = { |
32 | {"__reduce__" , THPQScheme_reduce, METH_NOARGS, nullptr}, |
33 | {nullptr} /* Sentinel */ |
34 | }; |
35 | |
36 | PyObject* THPQScheme_repr(THPQScheme* self) { |
37 | std::string name = self->name; |
38 | return THPUtils_packString("torch." + name); |
39 | } |
40 | |
41 | PyTypeObject THPQSchemeType = { |
42 | PyVarObject_HEAD_INIT(nullptr, 0) "torch.qscheme" , /* tp_name */ |
43 | sizeof(THPQScheme), /* tp_basicsize */ |
44 | 0, /* tp_itemsize */ |
45 | nullptr, /* tp_dealloc */ |
46 | 0, /* tp_vectorcall_offset */ |
47 | nullptr, /* tp_getattr */ |
48 | nullptr, /* tp_setattr */ |
49 | nullptr, /* tp_reserved */ |
50 | (reprfunc)THPQScheme_repr, /* tp_repr */ |
51 | nullptr, /* tp_as_number */ |
52 | nullptr, /* tp_as_sequence */ |
53 | nullptr, /* tp_as_mapping */ |
54 | nullptr, /* tp_hash */ |
55 | nullptr, /* tp_call */ |
56 | nullptr, /* tp_str */ |
57 | nullptr, /* tp_getattro */ |
58 | nullptr, /* tp_setattro */ |
59 | nullptr, /* tp_as_buffer */ |
60 | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
61 | nullptr, /* tp_doc */ |
62 | nullptr, /* tp_traverse */ |
63 | nullptr, /* tp_clear */ |
64 | nullptr, /* tp_richcompare */ |
65 | 0, /* tp_weaklistoffset */ |
66 | nullptr, /* tp_iter */ |
67 | nullptr, /* tp_iternext */ |
68 | THPQScheme_methods, /* tp_methods */ |
69 | nullptr, /* tp_members */ |
70 | nullptr, /* tp_getset */ |
71 | nullptr, /* tp_base */ |
72 | nullptr, /* tp_dict */ |
73 | nullptr, /* tp_descr_get */ |
74 | nullptr, /* tp_descr_set */ |
75 | 0, /* tp_dictoffset */ |
76 | nullptr, /* tp_init */ |
77 | nullptr, /* tp_alloc */ |
78 | nullptr, /* tp_new */ |
79 | }; |
80 | |
81 | void THPQScheme_init(PyObject* module) { |
82 | if (PyType_Ready(&THPQSchemeType) < 0) { |
83 | throw python_error(); |
84 | } |
85 | Py_INCREF(&THPQSchemeType); |
86 | if (PyModule_AddObject(module, "qscheme" , (PyObject*)&THPQSchemeType) != 0) { |
87 | throw python_error(); |
88 | } |
89 | } |
90 | |