#include <torch/csrc/python_headers.h>
#ifdef _MSC_VER
#include <c10/util/win32-headers.h>
#endif
#include <structmember.h>
#include <ATen/mps/MPSDevice.h>
#include <c10/core/CPUAllocator.h>
#include <torch/csrc/CudaIPCTypes.h>
#include <torch/csrc/Device.h>
#include <torch/csrc/DynamicTypes.h>
#include <torch/csrc/StorageMethods.h>
#include <torch/csrc/StorageSharing.h>
#include <torch/csrc/THP.h>
#include <torch/csrc/copy_utils.h>
#include <torch/csrc/utils/device_lazy_init.h>
#include <torch/csrc/utils/pyobject_preservation.h>
#include <torch/csrc/utils/python_arg_parser.h>
#include <c10/util/intrusive_ptr.h>
#include <fmt/format.h>
using torch::utils::PyObjectPreservation;
template <>
void THPPointer<c10::StorageImpl>::free() {
if (ptr) {
c10::raw::intrusive_ptr::decref(ptr);
}
}
PyTypeObject* THPStorageClass = nullptr;
static PyObject* THPStorage_New(PyTypeObject* type, c10::Storage _storage) {
PyObject* obj = type->tp_alloc(type, 0);
TORCH_CHECK(obj, "Failed to allocate a ", type->tp_name, " object");
auto s = (THPStorage*)obj;
new (&s->cdata) c10::Storage(std::move(_storage));
return obj;
}
PyObject* THPStorage_NewWithStorage(PyTypeObject* type, c10::Storage _storage) {
TORCH_CHECK(
type == THPStorageClass || PyType_IsSubtype(type, &THPStorageType),
"Creating a Storage subclass from a class that does not inherit from ",
"Storage is not possible. Make sure your class inherits from Storage.");
TORCH_INTERNAL_ASSERT(_storage.use_count() == 1);
c10::StorageImpl* storage_impl = _storage.unsafeGetStorageImpl();
PyObject* obj = THPStorage_New(type, std::move(_storage));
PyObjectPreservation::init_fresh_nonatomic(*storage_impl, obj);
return obj;
}
PyObject* THPStorage_Wrap(c10::Storage storage) {
c10::StorageImpl* storage_impl = storage.unsafeGetStorageImpl();
return PyObjectPreservation::get_or_init(*storage_impl, [&]() {
return THPStorage_New(THPStorageClass, std::move(storage));
});
}
static void THPStorage_dealloc(PyObject* self) {
THPStorage* _self = reinterpret_cast<THPStorage*>(self);
auto pyobj_slot = _self->cdata.unsafeGetStorageImpl()->pyobj_slot();
if (pyobj_slot->load_pyobj() == self) {
TORCH_INTERNAL_ASSERT(_self->cdata.use_count() == 1);
pyobj_slot->clear();
pybind11::gil_scoped_release no_gil;
_self->cdata.~Storage();
} else {
_self->cdata.~Storage();
}
Py_TYPE(_self)->tp_free(self);
}
static PyObject* THPStorage_pynew(
PyTypeObject* type,
PyObject* args,
PyObject* kwargs) {
HANDLE_TH_ERRORS
TORCH_CHECK(
type != &THPStorageType,
"Cannot directly construct StorageBase; subclass it and then construct that");
static torch::PythonArgParser parser({
THPStorageStr "(*, int64_t allocator=None, Device device=None)",
THPStorageStr
"(int64_t size, *, int64_t allocator=None, Device device=None)",
THPStorageStr
"(PyObject* sequence, *, int64_t allocator=None, Device device=None)",
});
torch::ParsedArgs<3> parsed_args;
auto r = parser.parse(args, kwargs, parsed_args);
int allocator_arg_idx = 0;
int device_arg_idx = 1;
if (r.idx > 0) {
allocator_arg_idx = 1;
device_arg_idx = 2;
}
std::optional<int64_t> allocator_opt = r.toInt64Optional(allocator_arg_idx);
std::optional<at::Device> device_opt = r.deviceOptional(device_arg_idx);
TORCH_CHECK(
!allocator_opt.has_value() || !device_opt.has_value(),
THPStorageStr,
"(): only one or neither of 'allocator' or 'device' can ",
"be given, but not both");
PyObject* self = nullptr;
c10::Allocator* allocator = nullptr;
at::OptionalDeviceGuard device_guard;
if (allocator_opt.has_value()) {
allocator = reinterpret_cast<c10::Allocator*>(allocator_opt.value());
} else if (device_opt.has_value()) {
at::Device device = device_opt.value();
torch::utils::maybe_initialize_device(device);
switch (device.type()) {
case at::kCPU:
allocator = c10::GetDefaultCPUAllocator();
break;
#ifdef USE_CUDA
case at::kCUDA:
allocator = c10::cuda::CUDACachingAllocator::get();
break;
#endif
#ifdef USE_MPS
case at::kMPS:
allocator = at::mps::GetMPSAllocator();
break;
#endif
case at::DeviceType::XPU:
case at::DeviceType::HPU:
case at::DeviceType::Meta:
case at::DeviceType::PrivateUse1:
case at::DeviceType::MAIA:
case at::DeviceType::MTIA:
allocator = c10::GetAllocator(device.type());
break;
default:
TORCH_CHECK(
false,
THPStorageStr,
"(): Storage device not recognized: ",
device.type());
}
device_guard.reset_device(device);
} else {
allocator = c10::GetDefaultCPUAllocator();
}
if (r.idx == 0) {
self = THPStorage_NewWithStorage(
type,
make_storage_impl(
c10::StorageImpl::use_byte_size_t(),
0,
at::DataPtr(),
allocator,
true,
device_opt));
} else if (r.idx == 1) {
int64_t size = r.toInt64(0);
self = THPStorage_NewWithStorage(
type,
make_storage_impl(
c10::StorageImpl::use_byte_size_t(),
size,
at::DataPtr(),
allocator,
true,
device_opt));
} else if (r.idx == 2) {
PyObject* sequence = r.pyobject(0);
Py_ssize_t length = PySequence_Length(sequence);
TORCH_CHECK(
PySequence_Check(sequence),
THPStorageStr,
"(): Expected a sequence type, but got ",
THPUtils_typename(sequence));
TORCH_CHECK(
length >= 0,
THPStorageStr,
"(): Could not obtain the length of sequence of type ",
THPUtils_typename(sequence));
self = THPStorage_NewWithStorage(
type,
make_storage_impl(
c10::StorageImpl::use_byte_size_t(),
length,
at::DataPtr(),
allocator,
true,
device_opt));
THPObjectPtr item;
try {
const auto& storage = THPStorage_Unpack(self);
for (Py_ssize_t i = 0; i < length; i++) {
item = PySequence_GetItem(sequence, i);
uint8_t value = THPByteUtils_unpackReal(item.get());
if (allocator == c10::GetDefaultCPUAllocator()) {
static_cast<uint8_t*>(storage.mutable_data())[i] = value;
} else {
storage_set(storage, i, value);
}
}
} catch (const std::exception&) {
TORCH_CHECK(
false,
THPStorageStr "(): tried to construct a storage from a sequence (",
THPUtils_typename(sequence),
"), ",
"but one of the items was of type ",
THPUtils_typename(item.get()),
" instead of int");
}
}
return self;
Py_RETURN_NONE;
END_HANDLE_TH_ERRORS
}
static Py_ssize_t THPStorage_length(THPStorage* self) {
HANDLE_TH_ERRORS
THPStorage_assertNotNull(self);
return static_cast<Py_ssize_t>(THPStorage_Unpack(self).nbytes());
END_HANDLE_TH_ERRORS_RET(-1)
}
static PyObject* THPStorage_get(THPStorage* self, PyObject* index) {
HANDLE_TH_ERRORS
THPStorage_assertNotNull(self);
const auto& storage = THPStorage_Unpack(self);
int64_t len = static_cast<int64_t>(storage.nbytes());
if (THPUtils_checkLong(index)) {
int64_t nindex = THPUtils_unpackLong(index);
if (nindex < 0)
nindex += len;
if (nindex < 0 || nindex >= len) {
PyErr_SetString(
PyExc_IndexError,
fmt::format(
"index {} out of range for storage of size {}", nindex, len));
return nullptr;
}
uint8_t value = storage_get(storage, nindex);
return THPUtils_packUInt32(value);
} else if (PySlice_Check(index)) {
Py_ssize_t start = 0, stop = 0, slicelength = 0, step = 0;
if (PySlice_Unpack(index, &start, &stop, &step) < 0) {
return nullptr;
}
slicelength = PySlice_AdjustIndices(len, &start, &stop, step);
if (step != 1) {
TORCH_CHECK(
false,
"Trying to slice with a step of ",
step,
", but only a step of "
"1 is supported");
}
const auto& storage = THPStorage_Unpack(self);
auto data = static_cast<uint8_t*>(storage.mutable_data());
at::StorageImpl* old_storage_impl = storage.unsafeGetStorageImpl();
c10::raw::intrusive_ptr::incref(old_storage_impl);
std::optional<at::Device> device_opt = old_storage_impl->device();
auto new_storage_impl = make_storage_impl(
c10::StorageImpl::use_byte_size_t(),
#ifdef THQUANTIZED
slicelength * sizeof(quantized_t),
#else
slicelength,
#endif
at::DataPtr(
static_cast<void*>(data + start),
old_storage_impl,
[](void* s) {
c10::raw::intrusive_ptr::decref(static_cast<at::StorageImpl*>(s));
},
old_storage_impl->device()),
old_storage_impl->allocator(),
false,
device_opt);
PyObject* _ret =
THPStorage_NewWithStorage(Py_TYPE(self), std::move(new_storage_impl));
return _ret;
}
PyErr_Format(
PyExc_TypeError,
"can't index a " THPStorageStr " with %s",
THPUtils_typename(index));
return nullptr;
END_HANDLE_TH_ERRORS
}
static int THPStorage_set(THPStorage* self, PyObject* index, PyObject* value) {
HANDLE_TH_ERRORS
THPStorage_assertNotNull(self);
if (!THPByteUtils_checkReal(value)) {
TORCH_CHECK(
false,
"can only set storage content with a int types, but got ",
THPUtils_typename(value),
" instead");
}
uint8_t rvalue = THPByteUtils_unpackReal(value);
const auto& storage = THPStorage_Unpack(self);
if (THPUtils_checkLong(index)) {
int64_t nindex = THPUtils_unpackLong(index);
storage_set(storage, nindex, rvalue);
return 0;
} else if (PySlice_Check(index)) {
Py_ssize_t start = 0, stop = 0, step = 0;
Py_ssize_t len = static_cast<Py_ssize_t>(storage.nbytes());
if (PySlice_Unpack(index, &start, &stop, &step) < 0) {
return -1;
}
PySlice_AdjustIndices(len, &start, &stop, step);
if (step != 1) {
TORCH_CHECK(
false,
"Trying to slice with a step of ",
step,
", but only a step of "
"1 is supported");
}
for (; start < stop; start++)
storage_set(storage, start, rvalue);
return 0;
}
TORCH_CHECK(
false, "can't index a " THPStorageStr " with ", THPUtils_typename(index));
END_HANDLE_TH_ERRORS_RET(-1)
}
static PyMappingMethods THPStorage_mappingmethods = {
reinterpret_cast<lenfunc>(THPStorage_length),
reinterpret_cast<binaryfunc>(THPStorage_get),
reinterpret_cast<objobjargproc>(THPStorage_set)};
PyTypeObject THPStorageType = {
PyVarObject_HEAD_INIT(DEFERRED_ADDRESS(&PyType_Type), 0)
"torch._C.StorageBase",
sizeof(THPStorage),
0,
THPStorage_dealloc,
0,
nullptr,
nullptr,
nullptr,
nullptr,
nullptr,
nullptr,
&THPStorage_mappingmethods,
nullptr,
nullptr,
nullptr,
nullptr,
nullptr,
nullptr,
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
nullptr,
nullptr,
nullptr,
nullptr,
0,
nullptr,
nullptr,
nullptr,
nullptr,
nullptr,
nullptr,
nullptr,
nullptr,
nullptr,
0,
nullptr,
nullptr,
THPStorage_pynew,
};
static PyObject* THPStorage_device(THPStorage* self, void* ) {
HANDLE_TH_ERRORS
THPStorage_assertNotNull(self);
return THPDevice_New(THPStorage_Unpack(self).device());
END_HANDLE_TH_ERRORS
}
static PyObject* THPStorage_get_cdata(THPStorage* self, void* ) {
HANDLE_TH_ERRORS
return PyLong_FromVoidPtr(THPStorage_Unpack(self).unsafeGetStorageImpl());
END_HANDLE_TH_ERRORS
}
typedef PyObject* (*getter)(PyObject*, void*);
static struct PyGetSetDef THPStorage_properties[] = {
{"device",
reinterpret_cast<getter>(THPStorage_device),
nullptr,
nullptr,
nullptr},
{"_cdata",
reinterpret_cast<getter>(THPStorage_get_cdata),
nullptr,
nullptr,
nullptr},
{nullptr}};
bool THPStorage_init(PyObject* module) {
static std::vector<PyMethodDef> methods;
THPUtils_addPyMethodDefs(methods, THPStorage_getMethods());
THPUtils_addPyMethodDefs(methods, THPStorage_getSharingMethods());
THPStorageType.tp_methods = methods.data();
THPStorageType.tp_getset = THPStorage_properties;
if (PyModule_AddType(module, &THPStorageType) < 0)
return false;
return true;
}
void THPStorage_postInit(PyObject* module) {
THPStorageClass = reinterpret_cast<PyTypeObject*>(
PyObject_GetAttrString(module, "UntypedStorage"));
TORCH_CHECK_PYTHON(THPStorageClass);
}
void THPStorage_assertNotNull(THPStorage* storage) {
TORCH_CHECK(
THPStorage_Unpack(storage).unsafeGetStorageImpl(), "Got a null Storage");
}
void THPStorage_assertNotNull(PyObject* obj) {
THPStorage_assertNotNull(reinterpret_cast<THPStorage*>(obj));
}