| File: | build/../torch/csrc/MemoryFormat.cpp | 
| Warning: | line 16, column 28 PyObject ownership leak with reference count of 1  | 
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
| 1 | #include <torch/csrc/utils/tensor_memoryformats.h> | |||
| 2 | ||||
| 3 | #include <torch/csrc/DynamicTypes.h> | |||
| 4 | #include <torch/csrc/Exceptions.h> | |||
| 5 | #include <torch/csrc/MemoryFormat.h> | |||
| 6 | #include <c10/core/MemoryFormat.h> | |||
| 7 | ||||
| 8 | #include <torch/csrc/python_headers.h> | |||
| 9 | #include <torch/csrc/utils/object_ptr.h> | |||
| 10 | ||||
| 11 | ||||
| 12 | namespace torch { | |||
| 13 | namespace utils { | |||
| 14 | ||||
| 15 | #define _ADD_MEMORY_FORMAT(format, name){ std::string module_name = "torch."; PyObject* memory_format = THPMemoryFormat_New(format, module_name + name); _Py_INCREF (((PyObject*)(memory_format))); if (PyModule_AddObject(torch_module , name, memory_format) != 0) { throw python_error(); } } \  | |||
| 16 | { \ | |||
| 17 | std::string module_name = "torch."; \ | |||
| 18 | PyObject* memory_format = THPMemoryFormat_New(format, module_name + name); \ | |||
| 19 | Py_INCREF(memory_format)_Py_INCREF(((PyObject*)(memory_format))); \ | |||
| 20 | if (PyModule_AddObject(torch_module, name, memory_format) != 0) { \ | |||
| 21 | throw python_error(); \ | |||
| 22 | } \ | |||
| 23 | } | |||
| 24 | ||||
| 25 | void initializeMemoryFormats() { | |||
| 26 | auto torch_module = THPObjectPtr(PyImport_ImportModule("torch")); | |||
| 27 | if (!torch_module) { | |||
  | ||||
| 28 | throw python_error(); | |||
| 29 | } | |||
| 30 | ||||
| 31 |   _ADD_MEMORY_FORMAT(at::MemoryFormat::Preserve, "preserve_format"){ std::string module_name = "torch."; PyObject* memory_format = THPMemoryFormat_New(at::MemoryFormat::Preserve, module_name + "preserve_format"); _Py_INCREF(((PyObject*)(memory_format) )); if (PyModule_AddObject(torch_module, "preserve_format", memory_format ) != 0) { throw python_error(); } };  | |||
| 32 |   _ADD_MEMORY_FORMAT(at::MemoryFormat::Contiguous, "contiguous_format"){ std::string module_name = "torch."; PyObject* memory_format = THPMemoryFormat_New(at::MemoryFormat::Contiguous, module_name + "contiguous_format"); _Py_INCREF(((PyObject*)(memory_format ))); if (PyModule_AddObject(torch_module, "contiguous_format" , memory_format) != 0) { throw python_error(); } };  | |||
| 33 |   _ADD_MEMORY_FORMAT(at::MemoryFormat::ChannelsLast, "channels_last"){ std::string module_name = "torch."; PyObject* memory_format = THPMemoryFormat_New(at::MemoryFormat::ChannelsLast, module_name + "channels_last"); _Py_INCREF(((PyObject*)(memory_format))) ; if (PyModule_AddObject(torch_module, "channels_last", memory_format ) != 0) { throw python_error(); } };  | |||
| 34 |   _ADD_MEMORY_FORMAT(at::MemoryFormat::ChannelsLast3d, "channels_last_3d"){ std::string module_name = "torch."; PyObject* memory_format = THPMemoryFormat_New(at::MemoryFormat::ChannelsLast3d, module_name + "channels_last_3d"); _Py_INCREF(((PyObject*)(memory_format ))); if (PyModule_AddObject(torch_module, "channels_last_3d", memory_format) != 0) { throw python_error(); } };  | |||
| 35 | ||||
| 36 | } | |||
| 37 | ||||
| 38 | } // namespace utils | |||
| 39 | } // namespace torch | 
| 1 | void _Py_INCREF(PyObject *op) { ++op->ob_refcnt; } | 
| 1 | #include <torch/csrc/MemoryFormat.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/MemoryFormat.h> | |||
| 8 | ||||
| 9 | #include <structmember.h> | |||
| 10 | #include <cstring> | |||
| 11 | #include <string> | |||
| 12 | ||||
| 13 | PyObject *THPMemoryFormat_New(at::MemoryFormat memory_format, const std::string& name) | |||
| 14 | { | |||
| 15 | auto type = (PyTypeObject*)&THPMemoryFormatType; | |||
| 16 | auto self = THPObjectPtr{type->tp_alloc(type, 0)}; | |||
  | ||||
| 17 | if (!self) throw python_error(); | |||
| 18 | auto self_ = reinterpret_cast<THPMemoryFormat*>(self.get()); | |||
| 19 | self_->memory_format = memory_format; | |||
| 20 | std::strncpy (self_->name, name.c_str(), MEMORY_FORMAT_NAME_LEN); | |||
| 21 | self_->name[MEMORY_FORMAT_NAME_LEN] = '\0'; | |||
| 22 | return self.release(); | |||
| 23 | } | |||
| 24 | ||||
| 25 | PyObject *THPMemoryFormat_repr(THPMemoryFormat *self) | |||
| 26 | { | |||
| 27 | return THPUtils_packString(self->name); | |||
| 28 | } | |||
| 29 | ||||
| 30 | // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables) | |||
| 31 | PyTypeObject THPMemoryFormatType = { | |||
| 32 | PyVarObject_HEAD_INIT(nullptr, 0){ { 1, nullptr }, 0 }, | |||
| 33 | "torch.memory_format", /* tp_name */ | |||
| 34 | sizeof(THPMemoryFormat), /* tp_basicsize */ | |||
| 35 | 0, /* tp_itemsize */ | |||
| 36 | nullptr, /* tp_dealloc */ | |||
| 37 | // NOLINTNEXTLINE(modernize-use-nullptr) | |||
| 38 | 0, /* tp_vectorcall_offset */ | |||
| 39 | nullptr, /* tp_getattr */ | |||
| 40 | nullptr, /* tp_setattr */ | |||
| 41 | nullptr, /* tp_reserved */ | |||
| 42 | (reprfunc)THPMemoryFormat_repr, /* tp_repr */ | |||
| 43 | nullptr, /* tp_as_number */ | |||
| 44 | nullptr, /* tp_as_sequence */ | |||
| 45 | nullptr, /* tp_as_mapping */ | |||
| 46 | nullptr, /* tp_hash */ | |||
| 47 | nullptr, /* tp_call */ | |||
| 48 | nullptr, /* tp_str */ | |||
| 49 | nullptr, /* tp_getattro */ | |||
| 50 | nullptr, /* tp_setattro */ | |||
| 51 | nullptr, /* tp_as_buffer */ | |||
| 52 | Py_TPFLAGS_DEFAULT( 0 | (1UL << 18) | 0), /* tp_flags */ | |||
| 53 | nullptr, /* tp_doc */ | |||
| 54 | nullptr, /* tp_traverse */ | |||
| 55 | nullptr, /* tp_clear */ | |||
| 56 | nullptr, /* tp_richcompare */ | |||
| 57 | 0, /* tp_weaklistoffset */ | |||
| 58 | nullptr, /* tp_iter */ | |||
| 59 | nullptr, /* tp_iternext */ | |||
| 60 | nullptr, /* tp_methods */ | |||
| 61 | nullptr, /* tp_members */ | |||
| 62 | nullptr, /* tp_getset */ | |||
| 63 | nullptr, /* tp_base */ | |||
| 64 | nullptr, /* tp_dict */ | |||
| 65 | nullptr, /* tp_descr_get */ | |||
| 66 | nullptr, /* tp_descr_set */ | |||
| 67 | 0, /* tp_dictoffset */ | |||
| 68 | nullptr, /* tp_init */ | |||
| 69 | nullptr, /* tp_alloc */ | |||
| 70 | nullptr, /* tp_new */ | |||
| 71 | }; | |||
| 72 | ||||
| 73 | void THPMemoryFormat_init(PyObject *module) | |||
| 74 | { | |||
| 75 | if (PyType_Ready(&THPMemoryFormatType) < 0) { | |||
| 76 | throw python_error(); | |||
| 77 | } | |||
| 78 | Py_INCREF(&THPMemoryFormatType)_Py_INCREF(((PyObject*)(&THPMemoryFormatType))); | |||
| 79 | if (PyModule_AddObject(module, "memory_format", (PyObject *)&THPMemoryFormatType) != 0) { | |||
| 80 | throw python_error(); | |||
| 81 | } | |||
| 82 | } |