Skip to content

Commit

Permalink
[1/N] Remove usage of C array (pytorch#139567)
Browse files Browse the repository at this point in the history
Fixes #ISSUE_NUMBER

Pull Request resolved: pytorch#139567
Approved by: https://github.com/Skylion007, https://github.com/ezyang
  • Loading branch information
cyyever authored and pytorchmergebot committed Nov 4, 2024
1 parent cadc50e commit 3179eb1
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 22 deletions.
9 changes: 5 additions & 4 deletions c10/core/impl/DeviceGuardImplInterface.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
#include <c10/core/impl/DeviceGuardImplInterface.h>
#include <array>

namespace c10::impl {

// NOLINTNEXTLINE(modernize-avoid-c-arrays,cppcoreguidelines-avoid-c-arrays)
std::atomic<const DeviceGuardImplInterface*>
device_guard_impl_registry[static_cast<size_t>(
DeviceType::COMPILE_TIME_MAX_DEVICE_TYPES)];
std::array<
std::atomic<const DeviceGuardImplInterface*>,
static_cast<size_t>(DeviceType::COMPILE_TIME_MAX_DEVICE_TYPES)>
device_guard_impl_registry;

DeviceGuardImplRegistrar::DeviceGuardImplRegistrar(
DeviceType type,
Expand Down
9 changes: 5 additions & 4 deletions c10/core/impl/DeviceGuardImplInterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
// Just for C10_ANONYMOUS_VARIABLE
#include <c10/util/Registry.h>

#include <array>
#include <atomic>

namespace c10 {
Expand Down Expand Up @@ -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<const DeviceGuardImplInterface*>
device_guard_impl_registry[static_cast<size_t>(
DeviceType::COMPILE_TIME_MAX_DEVICE_TYPES)];
extern C10_API std::array<
std::atomic<const DeviceGuardImplInterface*>,
static_cast<size_t>(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
Expand Down
7 changes: 4 additions & 3 deletions torch/csrc/Module.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1367,7 +1367,7 @@ static PyObject* THPModule_are_vmap_fallback_warnings_enabled(
END_HANDLE_TH_ERRORS
}

static PyMethodDef TorchMethods[] = { // NOLINT
static std::initializer_list<PyMethodDef> TorchMethods = {
{"_initExtension", THPModule_initExtension, METH_O, nullptr},
{"_autograd_init", THPAutograd_initExtension, METH_NOARGS, nullptr},
{"_add_docstr", THPModule_addDocStr, METH_VARARGS, nullptr},
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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);
Expand Down
6 changes: 2 additions & 4 deletions torch/csrc/jit/passes/onnx/shape_type_inference.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
9 changes: 5 additions & 4 deletions torch/csrc/multiprocessing/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <torch/csrc/utils/pybind.h>
#include <torch/csrc/utils/python_strings.h>

#include <initializer_list>
#include <stdexcept>

#if defined(__linux__)
Expand Down Expand Up @@ -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<PyMethodDef> methods = {
{
"_multiprocessing_init",
multiprocessing_init,
Expand All @@ -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
2 changes: 1 addition & 1 deletion torch/csrc/multiprocessing/init.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@

namespace torch::multiprocessing {

PyMethodDef* python_functions();
const PyMethodDef* python_functions();

} // namespace torch::multiprocessing
2 changes: 1 addition & 1 deletion torch/csrc/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ void THPUtils_setError(const char* format, ...) {

void THPUtils_addPyMethodDefs(
std::vector<PyMethodDef>& vector,
PyMethodDef* methods) {
const PyMethodDef* methods) {
if (!vector.empty()) {
// remove nullptr terminator
vector.pop_back();
Expand Down
2 changes: 1 addition & 1 deletion torch/csrc/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ std::vector<int> THPUtils_unpackIntTuple(PyObject* arg);

TORCH_PYTHON_API void THPUtils_addPyMethodDefs(
std::vector<PyMethodDef>& vector,
PyMethodDef* methods);
const PyMethodDef* methods);

int THPUtils_getCallable(PyObject* arg, PyObject** result);

Expand Down

0 comments on commit 3179eb1

Please sign in to comment.