[Reland] [1/N] fix clang-tidy warnings in torch/csrc (#108114)

Reland of PR #107648 with auto replaced with Py_ssize_t in eval_frame.c. This PR applies fixes to some found issues by clang-tidy in torch/csrc.

Pull Request resolved: https://github.com/pytorch/pytorch/pull/108114
Approved by: https://github.com/Skylion007
This commit is contained in:
cyy
2023-08-30 17:11:11 +00:00
committed by PyTorch MergeBot
parent 7be233f3a5
commit 01fc6466d1
13 changed files with 72 additions and 90 deletions

View File

@ -90,7 +90,7 @@ struct C10_API Storage {
return storage_impl_->mutable_data(); return storage_impl_->mutable_data();
} }
at::DataPtr& mutable_data_ptr() { at::DataPtr& mutable_data_ptr() const {
return storage_impl_->mutable_data_ptr(); return storage_impl_->mutable_data_ptr();
} }

View File

@ -124,24 +124,18 @@ static PyObject* THPModule_errorIfAnyWorkerFails(
PyObject* module, PyObject* module,
PyObject* noargs) { PyObject* noargs) {
HANDLE_TH_ERRORS HANDLE_TH_ERRORS
// NOLINTNEXTLINE(cppcoreguidelines-init-variables)
int error;
// NOLINTNEXTLINE(cppcoreguidelines-init-variables)
std::set<pid_t>* pid_set;
// NOLINTNEXTLINE(cppcoreguidelines-init-variables)
pid_t worker_pid;
siginfo_t infop;
// Only check the pids we care about // Only check the pids we care about
for (auto& w : worker_pids) { for (auto& w : worker_pids) {
pid_set = &(w.second); auto& pid_set = w.second;
for (auto pid_it = pid_set->begin(); pid_it != pid_set->end(); ++pid_it) { for (auto worker_pid : pid_set) {
worker_pid = *pid_it;
// Use waitid rather than waitpid so that we can set NOWAIT, and that // Use waitid rather than waitpid so that we can set NOWAIT, and that
// Python and other handlers can get whatever info they want about the // Python and other handlers can get whatever info they want about the
// child. // child.
siginfo_t infop{};
infop.si_pid = 0; infop.si_pid = 0;
error = waitid(P_PID, worker_pid, &infop, WEXITED | WNOHANG | WNOWAIT); auto error =
waitid(P_PID, worker_pid, &infop, WEXITED | WNOHANG | WNOWAIT);
// ignore errors and case with no waitable child // ignore errors and case with no waitable child
if (error < 0 || infop.si_pid == 0) if (error < 0 || infop.si_pid == 0)
continue; continue;
@ -154,7 +148,7 @@ static PyObject* THPModule_errorIfAnyWorkerFails(
<< "num_workers=0 may give better error trace."; << "num_workers=0 may give better error trace.";
// This is necessary. Otherwise, the runtime error will kill the other // This is necessary. Otherwise, the runtime error will kill the other
// workers, and trigger this again. // workers, and trigger this again.
pid_set->clear(); pid_set.clear();
throw std::runtime_error(oss.str()); throw std::runtime_error(oss.str());
} else if ( } else if (
infop.si_code == CLD_KILLED || infop.si_code == CLD_KILLED ||
@ -168,7 +162,7 @@ static PyObject* THPModule_errorIfAnyWorkerFails(
} }
// This is necessary. Otherwise, the runtime error will kill the other // This is necessary. Otherwise, the runtime error will kill the other
// workers, and trigger this again. // workers, and trigger this again.
pid_set->clear(); pid_set.clear();
throw std::runtime_error(oss.str()); throw std::runtime_error(oss.str());
} }
} }

View File

@ -66,21 +66,22 @@ PyObject* THPDevice_pynew(
return THPDevice_New(device); return THPDevice_New(device);
} else if (r.idx == 1) { } else if (r.idx == 1) {
auto as_device = r.device(0); // this works, because device can take strings auto as_device = r.device(0); // this works, because device can take strings
auto device_type = r.string(0);
if (as_device.has_index()) { if (as_device.has_index()) {
auto device_type = r.string(0);
throw std::runtime_error( throw std::runtime_error(
"type (string) must not include an index because index " "type (string) must not include an index because index "
"was passed explicitly: " + "was passed explicitly: " +
device_type); device_type);
} }
int32_t device_index = -1; int64_t device_index = -1;
if (!r.isNone(1)) { if (!r.isNone(1)) {
device_index = r.toInt64(1); device_index = r.toInt64(1);
// -1 is allowed in ATen/C++, to mean the default device, but not in // -1 is allowed in ATen/C++, to mean the default device, but not in
// Python. // Python.
TORCH_CHECK(device_index >= 0, "Device index must not be negative"); TORCH_CHECK(device_index >= 0, "Device index must not be negative");
} }
at::Device device(as_device.type(), device_index); at::Device device(
as_device.type(), static_cast<c10::DeviceIndex>(device_index));
return THPDevice_New(device); return THPDevice_New(device);
} }
Py_RETURN_NONE; Py_RETURN_NONE;
@ -163,8 +164,8 @@ PyObject* THPDevice_reduce(PyObject* _self, PyObject* noargs) {
std::ostringstream oss; std::ostringstream oss;
oss << self->device.type(); oss << self->device.type();
if (self->device.has_index()) { if (self->device.has_index()) {
args = THPObjectPtr{ args = THPObjectPtr{Py_BuildValue(
Py_BuildValue("(si)", oss.str().c_str(), self->device.index())}; "(si)", oss.str().c_str(), static_cast<int>(self->device.index()))};
} else { } else {
args = THPObjectPtr{Py_BuildValue("(s)", oss.str().c_str())}; args = THPObjectPtr{Py_BuildValue("(s)", oss.str().c_str())};
} }

View File

@ -5,6 +5,7 @@
#include <ATen/Device.h> #include <ATen/Device.h>
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-member-init)
struct TORCH_API THPDevice { struct TORCH_API THPDevice {
PyObject_HEAD at::Device device; PyObject_HEAD at::Device device;
}; };

View File

@ -39,7 +39,8 @@ PyObject* THPDtype_is_floating_point(THPDtype* self, PyObject* noargs) {
PyObject* THPDtype_itemsize(THPDtype* self, PyObject* noargs) { PyObject* THPDtype_itemsize(THPDtype* self, PyObject* noargs) {
HANDLE_TH_ERRORS HANDLE_TH_ERRORS
return THPUtils_packInt64(scalarTypeToTypeMeta(self->scalar_type).itemsize()); return THPUtils_packUInt64(
scalarTypeToTypeMeta(self->scalar_type).itemsize());
END_HANDLE_TH_ERRORS END_HANDLE_TH_ERRORS
} }
@ -115,8 +116,7 @@ static PyMethodDef THPDtype_methods[] = {
}; };
PyObject* THPDtype_repr(THPDtype* self) { PyObject* THPDtype_repr(THPDtype* self) {
std::string name = self->name; return THPUtils_packString(std::string("torch.") + self->name);
return THPUtils_packString("torch." + name);
} }
PyTypeObject THPDtypeType = { PyTypeObject THPDtypeType = {

View File

@ -119,8 +119,7 @@ static PyObject* THPGenerator_setState(PyObject* _self, PyObject* _new_state) {
} }
uint64_t unpack_uint64(PyObject* pyobj) { uint64_t unpack_uint64(PyObject* pyobj) {
// NOLINTNEXTLINE(cppcoreguidelines-init-variables) uint64_t unsigned_obj = 0;
uint64_t unsigned_obj;
try { try {
// First try to interpret as unsigned long // First try to interpret as unsigned long
unsigned_obj = THPUtils_unpackUInt64(pyobj); unsigned_obj = THPUtils_unpackUInt64(pyobj);
@ -223,11 +222,7 @@ static PyMethodDef THPGenerator_methods[] = {
// NOLINTNEXTLINE(cppcoreguidelines-avoid-c-arrays,modernize-avoid-c-arrays,cppcoreguidelines-avoid-non-const-global-variables) // NOLINTNEXTLINE(cppcoreguidelines-avoid-c-arrays,modernize-avoid-c-arrays,cppcoreguidelines-avoid-non-const-global-variables)
static struct PyMemberDef THPGenerator_members[] = { static struct PyMemberDef THPGenerator_members[] = {
{(char*)"_cdata", {"_cdata", T_ULONGLONG, offsetof(THPGenerator, cdata), READONLY, nullptr},
T_ULONGLONG,
offsetof(THPGenerator, cdata),
READONLY,
nullptr},
{nullptr}}; {nullptr}};
PyTypeObject THPGeneratorType = { PyTypeObject THPGeneratorType = {

View File

@ -38,7 +38,7 @@ PyObject* THPSize_New(const torch::autograd::Variable& var) {
return self.release(); return self.release();
} }
PyObject* THPSize_NewFromSizes(int dim, const int64_t* sizes) { PyObject* THPSize_NewFromSizes(int64_t dim, const int64_t* sizes) {
auto self = THPObjectPtr(THPSizeType.tp_alloc(&THPSizeType, dim)); auto self = THPObjectPtr(THPSizeType.tp_alloc(&THPSizeType, dim));
if (!self) if (!self)
throw python_error(); throw python_error();
@ -49,7 +49,8 @@ PyObject* THPSize_NewFromSizes(int dim, const int64_t* sizes) {
PyObject* THPSize_NewFromSymSizes(const at::Tensor& self_) { PyObject* THPSize_NewFromSymSizes(const at::Tensor& self_) {
auto sym_sizes = self_.sym_sizes(); auto sym_sizes = self_.sym_sizes();
auto ret = THPObjectPtr(THPSizeType.tp_alloc(&THPSizeType, sym_sizes.size())); auto ret = THPObjectPtr(THPSizeType.tp_alloc(
&THPSizeType, static_cast<Py_ssize_t>(sym_sizes.size())));
if (!ret) if (!ret)
throw python_error(); throw python_error();
@ -70,8 +71,8 @@ PyObject* THPSize_NewFromSymSizes(const at::Tensor& self_) {
// Otherwise, we know that it is an actual integer value. // Otherwise, we know that it is an actual integer value.
auto m = si.maybe_as_int(); auto m = si.maybe_as_int();
if (torch::jit::tracer::isTracing()) { if (torch::jit::tracer::isTracing()) {
PyObject* py_size_tensor = PyObject* py_size_tensor = THPVariable_Wrap(
THPVariable_Wrap(torch::jit::tracer::getSizeOf(self_, i)); torch::jit::tracer::getSizeOf(self_, static_cast<int64_t>(i)));
if (!py_size_tensor) if (!py_size_tensor)
throw python_error(); throw python_error();
PyTuple_SET_ITEM(ret.get(), i, py_size_tensor); PyTuple_SET_ITEM(ret.get(), i, py_size_tensor);

View File

@ -9,7 +9,7 @@ extern PyTypeObject THPSizeType;
#define THPSize_Check(obj) (Py_TYPE(obj) == &THPSizeType) #define THPSize_Check(obj) (Py_TYPE(obj) == &THPSizeType)
PyObject* THPSize_New(const torch::autograd::Variable& t); PyObject* THPSize_New(const torch::autograd::Variable& t);
PyObject* THPSize_NewFromSizes(int dim, const int64_t* sizes); PyObject* THPSize_NewFromSizes(int64_t dim, const int64_t* sizes);
PyObject* THPSize_NewFromSymSizes(const at::Tensor& t); PyObject* THPSize_NewFromSymSizes(const at::Tensor& t);
void THPSize_init(PyObject* module); void THPSize_init(PyObject* module);

View File

@ -57,7 +57,7 @@ static PyObject* THPStorage_dataPtr(PyObject* self, PyObject* noargs) {
TORCH_CHECK( TORCH_CHECK(
!invalid, !invalid,
"Attempted to access the data pointer on an invalid python storage.") "Attempted to access the data pointer on an invalid python storage.")
return PyLong_FromVoidPtr(const_cast<void*>(self_.data())); return PyLong_FromVoidPtr(self_.mutable_data());
END_HANDLE_TH_ERRORS END_HANDLE_TH_ERRORS
} }
@ -109,7 +109,6 @@ static PyObject* THPStorage_new(PyObject* self, PyObject* noargs) {
allocator, allocator,
/*resizable=*/true); /*resizable=*/true);
// NOLINTNEXTLINE(cppcoreguidelines-init-variables)
return THPStorage_New(std::move(new_storage)); return THPStorage_New(std::move(new_storage));
END_HANDLE_TH_ERRORS END_HANDLE_TH_ERRORS
} }
@ -168,7 +167,7 @@ static PyObject* THPStorage_resize_(PyObject* self, PyObject* number_arg) {
auto new_tensor = at::empty(src_tensor.sizes(), src_tensor.options()); auto new_tensor = at::empty(src_tensor.sizes(), src_tensor.options());
new_tensor.copy_(src_tensor); new_tensor.copy_(src_tensor);
storage.set_data_ptr_noswap( storage.set_data_ptr_noswap(
std::move(const_cast<at::DataPtr&>(new_tensor.storage().data_ptr()))); std::move(new_tensor.storage().mutable_data_ptr()));
storage.unsafeGetStorageImpl()->set_allocator( storage.unsafeGetStorageImpl()->set_allocator(
new_tensor.storage().unsafeGetStorageImpl()->allocator()); new_tensor.storage().unsafeGetStorageImpl()->allocator());
storage.set_nbytes(new_tensor.storage().nbytes()); storage.set_nbytes(new_tensor.storage().nbytes());
@ -224,6 +223,7 @@ static PyObject* THPStorage_fromBuffer(
args, args,
keywds, keywds,
argtypes, argtypes,
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-const-cast)
const_cast<char**>(kwlist), const_cast<char**>(kwlist),
&obj, &obj,
&byte_order_str, &byte_order_str,
@ -248,8 +248,7 @@ static PyObject* THPStorage_fromBuffer(
"function missing required argument 'byte_order' (pos 2)"); "function missing required argument 'byte_order' (pos 2)");
size_t element_size = c10::elementSize(scalar_type); size_t element_size = c10::elementSize(scalar_type);
// NOLINTNEXTLINE(cppcoreguidelines-init-variables) bool do_byte_swap = false;
bool do_byte_swap;
if (!is_endian_independent) { if (!is_endian_independent) {
if (strcmp(byte_order_str, "native") == 0) { if (strcmp(byte_order_str, "native") == 0) {
do_byte_swap = false; do_byte_swap = false;
@ -283,8 +282,7 @@ static PyObject* THPStorage_fromBuffer(
return nullptr; return nullptr;
} }
// NOLINTNEXTLINE(cppcoreguidelines-init-variables) size_t size_bytes = 0;
size_t size_bytes;
if (count < 0) { if (count < 0) {
if ((buffer.len - offset) % element_size != 0) { if ((buffer.len - offset) % element_size != 0) {
PyErr_SetString( PyErr_SetString(
@ -297,7 +295,7 @@ static PyObject* THPStorage_fromBuffer(
return nullptr; return nullptr;
} }
size_bytes = buffer.len - offset; size_bytes = buffer.len - offset;
count = size_bytes / element_size; count = static_cast<Py_ssize_t>(size_bytes / element_size);
} else { } else {
size_bytes = count * element_size; size_bytes = count * element_size;
} }
@ -400,8 +398,7 @@ static PyObject* THPStorage_fromFile(
PyObject* args, PyObject* args,
PyObject* keywds) { PyObject* keywds) {
HANDLE_TH_ERRORS HANDLE_TH_ERRORS
// NOLINTNEXTLINE(cppcoreguidelines-init-variables) const char* filename = nullptr;
const char* filename;
Py_ssize_t nbytes = 0; Py_ssize_t nbytes = 0;
int shared = 0; int shared = 0;
// NOLINTNEXTLINE(cppcoreguidelines-avoid-c-arrays,modernize-avoid-c-arrays) // NOLINTNEXTLINE(cppcoreguidelines-avoid-c-arrays,modernize-avoid-c-arrays)
@ -410,6 +407,7 @@ static PyObject* THPStorage_fromFile(
args, args,
keywds, keywds,
"s|in", "s|in",
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-const-cast)
const_cast<char**>(kwlist), const_cast<char**>(kwlist),
&filename, &filename,
&shared, &shared,

View File

@ -196,10 +196,9 @@ static PyObject* THPStorage_shareFd(PyObject* self, PyObject* noargs) {
const auto& storage = THPStorage_Unpack(self); const auto& storage = THPStorage_Unpack(self);
TORCH_CHECK( TORCH_CHECK(
storage.device_type() == at::kCPU, "_share_fd_: only available on CPU"); storage.device_type() == at::kCPU, "_share_fd_: only available on CPU");
// NOLINTNEXTLINE(cppcoreguidelines-init-variables) at::MapAllocator* ctx = at::MapAllocator::fromDataPtr(storage.data_ptr());
at::MapAllocator* ctx;
// Storage is already in shared memory, just return a handle // Storage is already in shared memory, just return a handle
if ((ctx = at::MapAllocator::fromDataPtr(storage.data_ptr()))) { if (ctx) {
// done // done
} else { } else {
at::Storage new_storage(at::new_shm_fd_storage(storage.nbytes())); at::Storage new_storage(at::new_shm_fd_storage(storage.nbytes()));
@ -248,11 +247,10 @@ static PyObject* THPStorage_newSharedFd(PyObject* _unused, PyObject* args) {
"a file descriptor (int) and storage size (int)"); "a file descriptor (int) and storage size (int)");
return nullptr; return nullptr;
} }
// NOLINTNEXTLINE(cppcoreguidelines-init-variables)
int fd;
int tmp_fd = (int)THPUtils_unpackLong(_tmp_fd); int tmp_fd = (int)THPUtils_unpackLong(_tmp_fd);
int64_t size = THPUtils_unpackLong(_size); int64_t size = THPUtils_unpackLong(_size);
if ((fd = dup(tmp_fd)) == -1) { int fd = dup(tmp_fd);
if (fd == -1) {
THPUtils_setError("could not duplicate a shared memory file descriptor"); THPUtils_setError("could not duplicate a shared memory file descriptor");
return nullptr; return nullptr;
} }
@ -405,16 +403,14 @@ static PyObject* THPStorage_releaseIPCCounter(
#ifdef USE_CUDA #ifdef USE_CUDA
static std::string THPStorage_bytesAsHandleString(PyObject* handle) { static std::string THPStorage_bytesAsHandleString(PyObject* handle) {
// NOLINTNEXTLINE(cppcoreguidelines-init-variables) char* buffer = nullptr;
char* buffer; Py_ssize_t handle_size = 0;
// NOLINTNEXTLINE(cppcoreguidelines-init-variables)
Py_ssize_t handle_size;
if (PyBytes_AsStringAndSize(handle, &buffer, &handle_size) == -1) { if (PyBytes_AsStringAndSize(handle, &buffer, &handle_size) == -1) {
// NOLINTNEXTLINE(bugprone-string-constructor) THPUtils_assertRet(
return nullptr; "", handle_size == CUDA_IPC_HANDLE_SIZE, "incorrect handle");
} }
// NOLINTNEXTLINE(bugprone-string-constructor) THPUtils_assertRet(
THPUtils_assert(handle_size == CUDA_IPC_HANDLE_SIZE, "incorrect handle size"); "", handle_size == CUDA_IPC_HANDLE_SIZE, "incorrect handle size");
return std::string(buffer, handle_size); return std::string(buffer, handle_size);
} }
#endif #endif
@ -457,6 +453,9 @@ static PyObject* THPStorage_newSharedCuda(PyObject* _unused, PyObject* args) {
// Ensure that producer prepared all tensor's data // Ensure that producer prepared all tensor's data
std::string s_ipc_event_handle = std::string s_ipc_event_handle =
THPStorage_bytesAsHandleString(_event_handle); THPStorage_bytesAsHandleString(_event_handle);
if (s_ipc_event_handle.empty()) {
return nullptr;
}
auto ipc_event_handle = reinterpret_cast<const cudaIpcEventHandle_t*>( auto ipc_event_handle = reinterpret_cast<const cudaIpcEventHandle_t*>(
s_ipc_event_handle.c_str()); s_ipc_event_handle.c_str());
// NOLINTNEXTLINE(cppcoreguidelines-init-variables) // NOLINTNEXTLINE(cppcoreguidelines-init-variables)
@ -467,12 +466,14 @@ static PyObject* THPStorage_newSharedCuda(PyObject* _unused, PyObject* args) {
} }
std::string s_handle = THPStorage_bytesAsHandleString(_handle); std::string s_handle = THPStorage_bytesAsHandleString(_handle);
if (s_handle.empty()) {
return nullptr;
}
std::shared_ptr<void> basePtr = std::shared_ptr<void> basePtr =
c10::cuda::CUDACachingAllocator::getIpcDevPtr(s_handle); c10::cuda::CUDACachingAllocator::getIpcDevPtr(s_handle);
// Offset the basePtr to reconstruct the real storage // Offset the basePtr to reconstruct the real storage
// devPtr = basePtr + storage_offset // devPtr = basePtr + storage_offset
// NOLINTNEXTLINE(cppcoreguidelines-init-variables)
void* devPtr = basePtr.get(); void* devPtr = basePtr.get();
devPtr = (char*)devPtr + storage_offset_bytes; devPtr = (char*)devPtr + storage_offset_bytes;

View File

@ -23,6 +23,7 @@ static PyObject* THPStream_pynew(
args, args,
kwargs, kwargs,
"|LLL", "|LLL",
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-const-cast)
const_cast<char**>(kwlist), const_cast<char**>(kwlist),
&stream_id, &stream_id,
&device_index, &device_index,
@ -53,7 +54,8 @@ PyObject* THPStream_Wrap(const c10::Stream& stream) {
THPStream* self = (THPStream*)ptr.get(); THPStream* self = (THPStream*)ptr.get();
self->stream_id = stream.id(); self->stream_id = stream.id();
self->device_index = stream.device_index(); // NOLINTNEXTLINE(bugprone-signed-char-misuse)
self->device_index = static_cast<int64_t>(stream.device_index());
self->device_type = static_cast<int64_t>(stream.device_type()); self->device_type = static_cast<int64_t>(stream.device_type());
return ptr.release(); return ptr.release();
END_HANDLE_TH_ERRORS END_HANDLE_TH_ERRORS
@ -65,11 +67,9 @@ static void THPStream_dealloc(THPStream* self) {
static PyObject* THPStream_get_device(THPStream* self, void* unused) { static PyObject* THPStream_get_device(THPStream* self, void* unused) {
HANDLE_TH_ERRORS HANDLE_TH_ERRORS
return THPDevice_New(c10::Stream::unpack3( return THPDevice_New(c10::Device(
self->stream_id, static_cast<c10::DeviceType>(self->device_type),
self->device_index, static_cast<c10::DeviceIndex>(self->device_index)));
static_cast<c10::DeviceType>(self->device_type))
.device());
END_HANDLE_TH_ERRORS END_HANDLE_TH_ERRORS
} }
@ -84,17 +84,17 @@ static PyObject* THPStream_eq(THPStream* self, THPStream* other) {
// NOLINTNEXTLINE(cppcoreguidelines-avoid-c-arrays,modernize-avoid-c-arrays,cppcoreguidelines-avoid-non-const-global-variables) // NOLINTNEXTLINE(cppcoreguidelines-avoid-c-arrays,modernize-avoid-c-arrays,cppcoreguidelines-avoid-non-const-global-variables)
static struct PyMemberDef THPStream_members[] = { static struct PyMemberDef THPStream_members[] = {
{(char*)"stream_id", {"stream_id",
T_LONGLONG, T_LONGLONG,
offsetof(THPStream, stream_id), offsetof(THPStream, stream_id),
READONLY, READONLY,
nullptr}, nullptr},
{(char*)"device_index", {"device_index",
T_LONGLONG, T_LONGLONG,
offsetof(THPStream, device_index), offsetof(THPStream, device_index),
READONLY, READONLY,
nullptr}, nullptr},
{(char*)"device_type", {"device_type",
T_LONGLONG, T_LONGLONG,
offsetof(THPStream, device_type), offsetof(THPStream, device_type),
READONLY, READONLY,
@ -108,7 +108,7 @@ static struct PyGetSetDef THPStream_properties[] = {
// NOLINTNEXTLINE(cppcoreguidelines-avoid-c-arrays,modernize-avoid-c-arrays,cppcoreguidelines-avoid-non-const-global-variables) // NOLINTNEXTLINE(cppcoreguidelines-avoid-c-arrays,modernize-avoid-c-arrays,cppcoreguidelines-avoid-non-const-global-variables)
static PyMethodDef THPStream_methods[] = { static PyMethodDef THPStream_methods[] = {
{(char*)"__eq__", (PyCFunction)THPStream_eq, METH_O, nullptr}, {"__eq__", (PyCFunction)THPStream_eq, METH_O, nullptr},
{nullptr}}; {nullptr}};
PyTypeObject THPStreamType = { PyTypeObject THPStreamType = {

View File

@ -131,7 +131,7 @@ PyObject* THCPModule_getDevice_wrap(PyObject* self, PyObject* noargs) {
HANDLE_TH_ERRORS HANDLE_TH_ERRORS
torch::utils::cuda_lazy_init(); torch::utils::cuda_lazy_init();
// NOLINTNEXTLINE(bugprone-signed-char-misuse) // NOLINTNEXTLINE(bugprone-signed-char-misuse)
auto device = static_cast<int>(c10::cuda::current_device()); auto device = static_cast<int32_t>(c10::cuda::current_device());
return THPUtils_packInt32(device); return THPUtils_packInt32(device);
END_HANDLE_TH_ERRORS END_HANDLE_TH_ERRORS
} }
@ -269,8 +269,7 @@ PyObject* THCPModule_setStream_wrap(
auto stream = at::cuda::CUDAStream::unpack3( auto stream = at::cuda::CUDAStream::unpack3(
stream_id, device_index, static_cast<c10::DeviceType>(device_type)); stream_id, device_index, static_cast<c10::DeviceType>(device_type));
// NOLINTNEXTLINE(bugprone-signed-char-misuse) auto device = c10::cuda::current_device();
auto device = static_cast<int>(c10::cuda::current_device());
if (device != stream.device_index()) { if (device != stream.device_index()) {
THCPModule_setDevice(stream.device_index()); THCPModule_setDevice(stream.device_index());
} }
@ -310,9 +309,7 @@ PyObject* THCPModule_cudaCachingAllocator_raw_alloc(
return nullptr; return nullptr;
} }
auto size = PyLong_AsSsize_t(size_o); auto size = PyLong_AsSsize_t(size_o);
// NOLINTNEXTLINE(cppcoreguidelines-init-variables)
cudaStream_t stream = static_cast<cudaStream_t>(PyLong_AsVoidPtr(stream_o)); cudaStream_t stream = static_cast<cudaStream_t>(PyLong_AsVoidPtr(stream_o));
// NOLINTNEXTLINE(cppcoreguidelines-init-variables)
void* mem = void* mem =
c10::cuda::CUDACachingAllocator::raw_alloc_with_stream(size, stream); c10::cuda::CUDACachingAllocator::raw_alloc_with_stream(size, stream);
return PyLong_FromVoidPtr(mem); return PyLong_FromVoidPtr(mem);

View File

@ -184,7 +184,7 @@ static PyObject* profiler_end_hook = NULL;
static PyObject* guard_profiler_name_str = NULL; /* cached py str */ static PyObject* guard_profiler_name_str = NULL; /* cached py str */
// Points to the extra scratch space on the code object // Points to the extra scratch space on the code object
static size_t extra_index = -1; static Py_ssize_t extra_index = -1;
static Py_tss_t eval_frame_callback_key = Py_tss_NEEDS_INIT; static Py_tss_t eval_frame_callback_key = Py_tss_NEEDS_INIT;
@ -341,8 +341,7 @@ static struct PyGetSetDef CacheEntry_properties[] = {
static PyObject* cache_entry_new(PyTypeObject* type, PyObject* args, PyObject* kwargs) { static PyObject* cache_entry_new(PyTypeObject* type, PyObject* args, PyObject* kwargs) {
CacheEntry *self; CacheEntry *self = (CacheEntry*) type->tp_alloc(type, 0);
self = (CacheEntry*) type->tp_alloc(type, 0);
if (self != NULL) { if (self != NULL) {
// The corresponding decrefs for Py_None are in cache_entry_init. // The corresponding decrefs for Py_None are in cache_entry_init.
Py_INCREF(Py_None); Py_INCREF(Py_None);
@ -586,7 +585,7 @@ Debugger helper functions.
PyObject* _debug_get_cache_entry_list(PyObject* self, PyObject* args) { PyObject* _debug_get_cache_entry_list(PyObject* self, PyObject* args) {
// TODO(anijain2305) - CacheEntry being the first class Python object might // TODO(anijain2305) - CacheEntry being the first class Python object might
// obviate the need of this function. Revisit. // obviate the need of this function. Revisit.
PyObject* object; PyObject* object = NULL;
if (!PyArg_ParseTuple(args, "O", &object)) { if (!PyArg_ParseTuple(args, "O", &object)) {
return NULL; return NULL;
} }
@ -688,7 +687,7 @@ static PyObject* lookup(CacheEntry* e, THP_EVAL_API_FRAME_OBJECT *frame, CacheEn
PyObject* valid = PyObject_CallOneArg(e->check_fn, f_locals); PyObject* valid = PyObject_CallOneArg(e->check_fn, f_locals);
if (unlikely(valid == NULL)) { if (unlikely(valid == NULL)) {
if (guard_error_hook != NULL) { if (guard_error_hook != NULL) {
PyObject *type, *value, *traceback; PyObject *type = NULL, *value = NULL, *traceback = NULL;
PyErr_Fetch(&type, &value, &traceback); PyErr_Fetch(&type, &value, &traceback);
PyObject* r = call_guard_fail_hook(guard_error_hook, e, index, f_locals); PyObject* r = call_guard_fail_hook(guard_error_hook, e, index, f_locals);
if (r == NULL) { if (r == NULL) {
@ -729,24 +728,13 @@ inline static PyObject* eval_custom_code(
THP_EVAL_API_FRAME_OBJECT* frame, THP_EVAL_API_FRAME_OBJECT* frame,
PyCodeObject* code, PyCodeObject* code,
int throw_flag) { int throw_flag) {
Py_ssize_t ncells = 0;
Py_ssize_t nfrees = 0;
Py_ssize_t nlocals_new = code->co_nlocals;
Py_ssize_t nlocals_old = frame->f_code->co_nlocals;
ncells = PyCode_GetNCellvars(code);
nfrees = PyCode_GetNFreevars(code);
DEBUG_NULL_CHECK(tstate); DEBUG_NULL_CHECK(tstate);
DEBUG_NULL_CHECK(frame); DEBUG_NULL_CHECK(frame);
DEBUG_NULL_CHECK(code); DEBUG_NULL_CHECK(code);
DEBUG_CHECK(nlocals_new >= nlocals_old);
#if IS_PYTHON_3_11_PLUS #if IS_PYTHON_3_11_PLUS
DEBUG_CHECK(ncells == frame->f_code->co_ncellvars);
DEBUG_CHECK(nfrees == frame->f_code->co_nfreevars);
// Generate Python function object and _PyInterpreterFrame in a way similar to // Generate Python function object and _PyInterpreterFrame in a way similar to
// https://github.com/python/cpython/blob/e715da6db1d1d70cd779dc48e1ba8110c51cc1bf/Python/ceval.c#L1130 // https://github.com/python/cpython/blob/e715da6db1d1d70cd779dc48e1ba8110c51cc1bf/Python/ceval.c#L1130
#if IS_PYTHON_3_12_PLUS #if IS_PYTHON_3_12_PLUS
@ -829,6 +817,12 @@ inline static PyObject* eval_custom_code(
Py_DECREF(name_to_idx); Py_DECREF(name_to_idx);
#else #else
Py_ssize_t nlocals_new = code->co_nlocals;
Py_ssize_t nlocals_old = frame->f_code->co_nlocals;
DEBUG_CHECK(nlocals_new >= nlocals_old);
Py_ssize_t ncells = PyCode_GetNCellvars(code);
Py_ssize_t nfrees = PyCode_GetNFreevars(code);
DEBUG_CHECK(ncells == PyTuple_GET_SIZE(frame->f_code->co_cellvars)); DEBUG_CHECK(ncells == PyTuple_GET_SIZE(frame->f_code->co_cellvars));
DEBUG_CHECK(nfrees == PyTuple_GET_SIZE(frame->f_code->co_freevars)); DEBUG_CHECK(nfrees == PyTuple_GET_SIZE(frame->f_code->co_freevars));