From 3179eb15ae2ed08266392897ea18a498f91f8ba1 Mon Sep 17 00:00:00 2001 From: cyy Date: Mon, 4 Nov 2024 04:52:46 +0000 Subject: [PATCH] [1/N] Remove usage of C array (#139567) Fixes #ISSUE_NUMBER Pull Request resolved: https://github.com/pytorch/pytorch/pull/139567 Approved by: https://github.com/Skylion007, https://github.com/ezyang --- c10/core/impl/DeviceGuardImplInterface.cpp | 9 +++++---- c10/core/impl/DeviceGuardImplInterface.h | 9 +++++---- torch/csrc/Module.cpp | 7 ++++--- torch/csrc/jit/passes/onnx/shape_type_inference.cpp | 6 ++---- torch/csrc/multiprocessing/init.cpp | 9 +++++---- torch/csrc/multiprocessing/init.h | 2 +- torch/csrc/utils.cpp | 2 +- torch/csrc/utils.h | 2 +- 8 files changed, 24 insertions(+), 22 deletions(-) diff --git a/c10/core/impl/DeviceGuardImplInterface.cpp b/c10/core/impl/DeviceGuardImplInterface.cpp index 581f32f1e130bf..015bcd3e64fb35 100644 --- a/c10/core/impl/DeviceGuardImplInterface.cpp +++ b/c10/core/impl/DeviceGuardImplInterface.cpp @@ -1,11 +1,12 @@ #include +#include namespace c10::impl { -// NOLINTNEXTLINE(modernize-avoid-c-arrays,cppcoreguidelines-avoid-c-arrays) -std::atomic - device_guard_impl_registry[static_cast( - DeviceType::COMPILE_TIME_MAX_DEVICE_TYPES)]; +std::array< + std::atomic, + static_cast(DeviceType::COMPILE_TIME_MAX_DEVICE_TYPES)> + device_guard_impl_registry; DeviceGuardImplRegistrar::DeviceGuardImplRegistrar( DeviceType type, diff --git a/c10/core/impl/DeviceGuardImplInterface.h b/c10/core/impl/DeviceGuardImplInterface.h index f145db0d234dd7..29aa9bc8038562 100644 --- a/c10/core/impl/DeviceGuardImplInterface.h +++ b/c10/core/impl/DeviceGuardImplInterface.h @@ -8,6 +8,7 @@ // Just for C10_ANONYMOUS_VARIABLE #include +#include #include namespace c10 { @@ -327,10 +328,10 @@ struct NoOpDeviceGuardImpl final : public DeviceGuardImplInterface { // in a Meyer singleton), it implies that you must *leak* objects when // putting them in the registry. This is done by deleting the destructor // on DeviceGuardImplInterface. -// NOLINTNEXTLINE(*c-arrays*) -extern C10_API std::atomic - device_guard_impl_registry[static_cast( - DeviceType::COMPILE_TIME_MAX_DEVICE_TYPES)]; +extern C10_API std::array< + std::atomic, + static_cast(DeviceType::COMPILE_TIME_MAX_DEVICE_TYPES)> + device_guard_impl_registry; // I can't conveniently use c10/util/Registry.h for the following reason: // c10/util/Registry.h gives me a slow way of Create'ing a object of some diff --git a/torch/csrc/Module.cpp b/torch/csrc/Module.cpp index 03f30797f18bf8..bff13d43b1a3bc 100644 --- a/torch/csrc/Module.cpp +++ b/torch/csrc/Module.cpp @@ -1367,7 +1367,7 @@ static PyObject* THPModule_are_vmap_fallback_warnings_enabled( END_HANDLE_TH_ERRORS } -static PyMethodDef TorchMethods[] = { // NOLINT +static std::initializer_list TorchMethods = { {"_initExtension", THPModule_initExtension, METH_O, nullptr}, {"_autograd_init", THPAutograd_initExtension, METH_NOARGS, nullptr}, {"_add_docstr", THPModule_addDocStr, METH_VARARGS, nullptr}, @@ -1703,7 +1703,7 @@ PyObject* initModule() { if (!(cmd)) \ return nullptr - THPUtils_addPyMethodDefs(methods, TorchMethods); + THPUtils_addPyMethodDefs(methods, std::data(TorchMethods)); THPUtils_addPyMethodDefs(methods, DataLoaderMethods); THPUtils_addPyMethodDefs(methods, torch::autograd::python_functions()); THPUtils_addPyMethodDefs(methods, torch::multiprocessing::python_functions()); @@ -1848,7 +1848,8 @@ PyObject* initModule() { at::init(); // Automatically translate errors thrown from pybind11 functions - py::register_exception_translator([](std::exception_ptr e) { // NOLINT + // NOLINTNEXTLINE(performance-unnecessary-value-param) + py::register_exception_translator([](std::exception_ptr e) { try { if (e) { std::rethrow_exception(e); diff --git a/torch/csrc/jit/passes/onnx/shape_type_inference.cpp b/torch/csrc/jit/passes/onnx/shape_type_inference.cpp index 9a6dcd9bd761dd..798636966df840 100644 --- a/torch/csrc/jit/passes/onnx/shape_type_inference.cpp +++ b/torch/csrc/jit/passes/onnx/shape_type_inference.cpp @@ -2144,10 +2144,8 @@ void ONNXShapeTypeInference( ex.what(), " on graph: ", n_graph->toString()); - // NOLINTNEXTLINE(modernize-avoid-c-arrays,cppcoreguidelines-avoid-c-arrays) - const char shape_err[] = "ShapeInferenceError"; - // NOLINTNEXTLINE(modernize-avoid-c-arrays,cppcoreguidelines-avoid-c-arrays) - const char type_err[] = "TypeInferenceError"; + constexpr const char* shape_err = "ShapeInferenceError"; + constexpr const char* type_err = "TypeInferenceError"; if ((strstr(ex.what(), shape_err) == nullptr) && (strstr(ex.what(), type_err) == nullptr)) { throw; diff --git a/torch/csrc/multiprocessing/init.cpp b/torch/csrc/multiprocessing/init.cpp index 77bf9391dea18e..0720393fb0a790 100644 --- a/torch/csrc/multiprocessing/init.cpp +++ b/torch/csrc/multiprocessing/init.cpp @@ -5,6 +5,7 @@ #include #include +#include #include #if defined(__linux__) @@ -55,8 +56,8 @@ PyObject* get_thread_name(PyObject* _unused, PyObject* noargs) { } // namespace // multiprocessing methods on torch._C -// NOLINTNEXTLINE(cppcoreguidelines-avoid-c-arrays,modernize-avoid-c-arrays,cppcoreguidelines-avoid-non-const-global-variables) -static PyMethodDef methods[] = { +// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables) +static std::initializer_list methods = { { "_multiprocessing_init", multiprocessing_init, @@ -78,8 +79,8 @@ static PyMethodDef methods[] = { {nullptr, nullptr, 0, nullptr}, }; -PyMethodDef* python_functions() { - return methods; +const PyMethodDef* python_functions() { + return std::data(methods); } } // namespace torch::multiprocessing diff --git a/torch/csrc/multiprocessing/init.h b/torch/csrc/multiprocessing/init.h index 1773060e07c485..1b64349a4ca90c 100644 --- a/torch/csrc/multiprocessing/init.h +++ b/torch/csrc/multiprocessing/init.h @@ -4,6 +4,6 @@ namespace torch::multiprocessing { -PyMethodDef* python_functions(); +const PyMethodDef* python_functions(); } // namespace torch::multiprocessing diff --git a/torch/csrc/utils.cpp b/torch/csrc/utils.cpp index f1e70c89f92672..2c6c2b0c6f0b84 100644 --- a/torch/csrc/utils.cpp +++ b/torch/csrc/utils.cpp @@ -105,7 +105,7 @@ void THPUtils_setError(const char* format, ...) { void THPUtils_addPyMethodDefs( std::vector& vector, - PyMethodDef* methods) { + const PyMethodDef* methods) { if (!vector.empty()) { // remove nullptr terminator vector.pop_back(); diff --git a/torch/csrc/utils.h b/torch/csrc/utils.h index 7552f6d0c028aa..6431c34bd2327d 100644 --- a/torch/csrc/utils.h +++ b/torch/csrc/utils.h @@ -166,7 +166,7 @@ std::vector THPUtils_unpackIntTuple(PyObject* arg); TORCH_PYTHON_API void THPUtils_addPyMethodDefs( std::vector& vector, - PyMethodDef* methods); + const PyMethodDef* methods); int THPUtils_getCallable(PyObject* arg, PyObject** result);