From 45efa1aaa8a7932d2d162b7a2d8303195fdd3f89 Mon Sep 17 00:00:00 2001 From: cyy Date: Mon, 5 May 2025 17:48:34 +0000 Subject: [PATCH] [3/N] Use internal linkage in C++ files (#151297) Follows #151070. Pull Request resolved: https://github.com/pytorch/pytorch/pull/151297 Approved by: https://github.com/Skylion007 --- benchmarks/static_runtime/test_static_module.cc | 2 -- caffe2/utils/threadpool/ThreadPool.h | 11 +++++++++++ caffe2/utils/threadpool/pthreadpool-cpp.cc | 6 +----- caffe2/utils/threadpool/thread_pool_guard.cpp | 2 +- tools/autograd/gen_autograd_functions.py | 16 ++++++++-------- .../templates/python_variable_methods.cpp | 2 +- torch/csrc/jit/api/function_impl.h | 2 +- torch/csrc/jit/mobile/train/export_data.cpp | 2 -- torch/csrc/jit/python/init.cpp | 3 ++- .../jit/runtime/profiling_graph_executor_impl.h | 5 +++++ torch/csrc/jit/runtime/static/impl.h | 1 + torch/csrc/jit/runtime/static/ops.h | 3 ++- torch/csrc/jit/runtime/static/passes.h | 1 + .../operator_versions/gen_mobile_upgraders.py | 5 +---- 14 files changed, 35 insertions(+), 26 deletions(-) diff --git a/benchmarks/static_runtime/test_static_module.cc b/benchmarks/static_runtime/test_static_module.cc index b035ef346ce2..6102c5f804f0 100644 --- a/benchmarks/static_runtime/test_static_module.cc +++ b/benchmarks/static_runtime/test_static_module.cc @@ -16,8 +16,6 @@ using namespace torch; using namespace torch::jit; using namespace torch::jit::test; -C10_DECLARE_bool(static_runtime_disable_debug_memory_overlap_check); - namespace { StaticModule makeStaticModuleFromScript(const std::string& script) { diff --git a/caffe2/utils/threadpool/ThreadPool.h b/caffe2/utils/threadpool/ThreadPool.h index dbaefc51389f..48eb7610eb6c 100644 --- a/caffe2/utils/threadpool/ThreadPool.h +++ b/caffe2/utils/threadpool/ThreadPool.h @@ -9,6 +9,7 @@ #include #include +#include "c10/util/Flags.h" #include "caffe2/core/common.h" // @@ -65,4 +66,14 @@ class TORCH_API /*alignas(kCacheLineSize)*/ ThreadPool { size_t getDefaultNumThreads(); } // namespace caffe2 +C10_DECLARE_bool(caffe2_threadpool_force_inline); + +// Whether or not threadpool caps apply to Android +C10_DECLARE_int(caffe2_threadpool_android_cap); + +// Whether or not threadpool caps apply to iOS and MacOS +C10_DECLARE_int(caffe2_threadpool_ios_cap); +C10_DECLARE_int(caffe2_threadpool_macos_cap); + +C10_DECLARE_int(pthreadpool_size); #endif // CAFFE2_UTILS_THREADPOOL_H_ diff --git a/caffe2/utils/threadpool/pthreadpool-cpp.cc b/caffe2/utils/threadpool/pthreadpool-cpp.cc index 6766b13d2b84..9e3175108689 100644 --- a/caffe2/utils/threadpool/pthreadpool-cpp.cc +++ b/caffe2/utils/threadpool/pthreadpool-cpp.cc @@ -1,9 +1,8 @@ #include #include +#include #include -#include - namespace { // After fork, the child process inherits the data-structures of the parent // process' thread-pool, but since those threads don't exist, the thread-pool @@ -102,9 +101,6 @@ PThreadPool* pthreadpool(size_t thread_count) { return threadpool.get(); } -// Forward declaration -size_t getDefaultNumThreads(); - PThreadPool* pthreadpool() { return pthreadpool(getDefaultNumThreads()); } diff --git a/caffe2/utils/threadpool/thread_pool_guard.cpp b/caffe2/utils/threadpool/thread_pool_guard.cpp index e030547e6ed8..b20b4d4a4270 100644 --- a/caffe2/utils/threadpool/thread_pool_guard.cpp +++ b/caffe2/utils/threadpool/thread_pool_guard.cpp @@ -2,7 +2,7 @@ namespace caffe2 { -thread_local bool _NoPThreadPoolGuard_enabled = false; +static thread_local bool _NoPThreadPoolGuard_enabled = false; bool _NoPThreadPoolGuard::is_enabled() { return _NoPThreadPoolGuard_enabled; diff --git a/tools/autograd/gen_autograd_functions.py b/tools/autograd/gen_autograd_functions.py index ea275b58f0f6..69474e06d18e 100644 --- a/tools/autograd/gen_autograd_functions.py +++ b/tools/autograd/gen_autograd_functions.py @@ -272,7 +272,7 @@ PY_RAW_GETSETDEF_STRUCT = CodeTemplate( # Getter templates GETTER_DEFINITION = CodeTemplate( """\ -PyObject* THP${op}_${name}_getter(THPCppFunction *self, void *_unused) { +static PyObject* THP${op}_${name}_getter(THPCppFunction *self, void *_unused) { HANDLE_TH_ERRORS auto prop = static_cast<${op}*>(self->cdata.get())->${name}; ${body} @@ -283,7 +283,7 @@ PyObject* THP${op}_${name}_getter(THPCppFunction *self, void *_unused) { GETTER_DEFINITION_SAVEDVAR = CodeTemplate( """\ -PyObject* THP${op}_${name}_getter(THPCppFunction *self, void *_unused) { +static PyObject* THP${op}_${name}_getter(THPCppFunction *self, void *_unused) { HANDLE_TH_ERRORS const auto& prop = static_cast<${op}*>(self->cdata.get())->${name}_; ${body} @@ -294,7 +294,7 @@ PyObject* THP${op}_${name}_getter(THPCppFunction *self, void *_unused) { GETTER_DEFINITION_RAW_SAVEDVAR = CodeTemplate( """\ -PyObject* THP${op}_${name}_raw_getter(THPCppFunction *self, void *_unused) { +static PyObject* THP${op}_${name}_raw_getter(THPCppFunction *self, void *_unused) { HANDLE_TH_ERRORS const auto& prop = static_cast<${op}*>(self->cdata.get())->${name}_; ${body} @@ -305,7 +305,7 @@ PyObject* THP${op}_${name}_raw_getter(THPCppFunction *self, void *_unused) { GETTER_DEFINITION_VEC_SAVEDVAR = CodeTemplate( """\ -PyObject* THP${op}_${name}_getter(THPCppFunction *self, void *_unused) { +static PyObject* THP${op}_${name}_getter(THPCppFunction *self, void *_unused) { HANDLE_TH_ERRORS const auto *node = static_cast<${op}*>(self->cdata.get()); const auto& prop = node->${name}_; @@ -321,7 +321,7 @@ PyObject* THP${op}_${name}_getter(THPCppFunction *self, void *_unused) { GETTER_DEFINITION_RAW_VEC_SAVEDVAR = CodeTemplate( """\ -PyObject* THP${op}_${name}_raw_getter(THPCppFunction *self, void *_unused) { +static PyObject* THP${op}_${name}_raw_getter(THPCppFunction *self, void *_unused) { HANDLE_TH_ERRORS const auto *node = static_cast<${op}*>(self->cdata.get()); const auto& prop = node->${name}_; @@ -337,7 +337,7 @@ PyObject* THP${op}_${name}_raw_getter(THPCppFunction *self, void *_unused) { GETTER_DEFINITION_OPT = CodeTemplate( """\ -PyObject* THP${op}_${name}_getter(THPCppFunction *self, void *_unused) { +static PyObject* THP${op}_${name}_getter(THPCppFunction *self, void *_unused) { HANDLE_TH_ERRORS auto opt_prop = static_cast<${op}*>(self->cdata.get())->${name}; if (!opt_prop.has_value()) { @@ -352,7 +352,7 @@ PyObject* THP${op}_${name}_getter(THPCppFunction *self, void *_unused) { GETTER_DEFINITION_OPT_ARRAYREF = CodeTemplate( """\ -PyObject* THP${op}_${name}_getter(THPCppFunction *self, void *_unused) { +static PyObject* THP${op}_${name}_getter(THPCppFunction *self, void *_unused) { HANDLE_TH_ERRORS auto opt_prop = static_cast<${op}*>(self->cdata.get())->${name}; if (!opt_prop.list.has_value()) { @@ -832,7 +832,7 @@ def process_function(info: DifferentiabilityInfo, template: CodeTemplate) -> str getter_definitions.append( CodeTemplate( """\ -PyObject* THP${op}_${name}_getter(THPCppFunction *self, void *_unused) { +static PyObject* THP${op}_${name}_getter(THPCppFunction *self, void *_unused) { HANDLE_TH_ERRORS const auto *node = static_cast<${op}*>(self->cdata.get()); const auto& prop = node->${name}; diff --git a/tools/autograd/templates/python_variable_methods.cpp b/tools/autograd/templates/python_variable_methods.cpp index 505ccc6d2f6d..9507fbe6e13c 100644 --- a/tools/autograd/templates/python_variable_methods.cpp +++ b/tools/autograd/templates/python_variable_methods.cpp @@ -842,7 +842,7 @@ static PyObject * THPVariable_requires_grad_(PyObject* self, PyObject* args, PyO END_HANDLE_TH_ERRORS } -inline bool dispatch_is_contiguous(const Tensor & self, MemoryFormat memory_format) { +static inline bool dispatch_is_contiguous(const Tensor & self, MemoryFormat memory_format) { return self.is_contiguous(memory_format); } diff --git a/torch/csrc/jit/api/function_impl.h b/torch/csrc/jit/api/function_impl.h index b5d336db2b6e..f5d86039ec0a 100644 --- a/torch/csrc/jit/api/function_impl.h +++ b/torch/csrc/jit/api/function_impl.h @@ -176,5 +176,5 @@ struct TORCH_API GraphFunction : public Function { TORCH_API GraphFunction* tryToGraphFunction(Function&) noexcept; TORCH_API GraphFunction& toGraphFunction(Function&); TORCH_API const GraphFunction& toGraphFunction(const Function&); - } // namespace torch::jit +C10_DECLARE_bool(torch_jit_do_not_store_optimized_graph); diff --git a/torch/csrc/jit/mobile/train/export_data.cpp b/torch/csrc/jit/mobile/train/export_data.cpp index 56ff96c429f4..2d0a91096a0c 100644 --- a/torch/csrc/jit/mobile/train/export_data.cpp +++ b/torch/csrc/jit/mobile/train/export_data.cpp @@ -18,8 +18,6 @@ namespace torch::jit { namespace mobile { -char const* toString(OpCode op); - namespace { /** diff --git a/torch/csrc/jit/python/init.cpp b/torch/csrc/jit/python/init.cpp index 63ead58ad395..2ef9b48aae30 100644 --- a/torch/csrc/jit/python/init.cpp +++ b/torch/csrc/jit/python/init.cpp @@ -163,7 +163,8 @@ std::optional toTypeInferredIValueOptional(py::handle input) { } } // anonymous namespace -#if !defined(USE_ROCM) +#if defined(BUILDING_TESTS) && !defined(USE_ROCM) +// NOLINTNEXTLINE(misc-use-internal-linkage) TORCH_API void runJITCPPTests(); #endif diff --git a/torch/csrc/jit/runtime/profiling_graph_executor_impl.h b/torch/csrc/jit/runtime/profiling_graph_executor_impl.h index c64e0b123d65..da437bb456e9 100644 --- a/torch/csrc/jit/runtime/profiling_graph_executor_impl.h +++ b/torch/csrc/jit/runtime/profiling_graph_executor_impl.h @@ -7,6 +7,11 @@ TORCH_DECLARE_bool(torch_jit_static_then_dynamic); TORCH_DECLARE_bool(torch_jit_always_dynamic); +C10_DECLARE_bool(torch_jit_release_profiling_graph_after_optimization); +C10_DECLARE_int32(torch_jit_release_profiling_graph_delay_in_seconds); +C10_DECLARE_int64(torch_jit_num_profiled_runs); +C10_DECLARE_int64(torch_jit_bailout_depth); + namespace torch::jit { TORCH_API void runNooptPassPipeline(std::shared_ptr& graph); diff --git a/torch/csrc/jit/runtime/static/impl.h b/torch/csrc/jit/runtime/static/impl.h index e8a3bdbc42ff..b4138c590e83 100644 --- a/torch/csrc/jit/runtime/static/impl.h +++ b/torch/csrc/jit/runtime/static/impl.h @@ -1145,3 +1145,4 @@ class TORCH_API StaticRuntime { }; } // namespace torch::jit +C10_DECLARE_bool(static_runtime_disable_debug_memory_overlap_check); diff --git a/torch/csrc/jit/runtime/static/ops.h b/torch/csrc/jit/runtime/static/ops.h index eb3dafeb59e2..dbe43cc93641 100644 --- a/torch/csrc/jit/runtime/static/ops.h +++ b/torch/csrc/jit/runtime/static/ops.h @@ -182,5 +182,6 @@ bool sr_schema_check( } bool sr_schema_check_kind(torch::jit::Node* node, c10::Symbol node_kind); - } // namespace torch::jit + +C10_DECLARE_bool(static_runtime_enable_fast_math); diff --git a/torch/csrc/jit/runtime/static/passes.h b/torch/csrc/jit/runtime/static/passes.h index 96f2e8236941..e7b7a272f391 100644 --- a/torch/csrc/jit/runtime/static/passes.h +++ b/torch/csrc/jit/runtime/static/passes.h @@ -86,4 +86,5 @@ TORCH_API void UseInPlaceGetRealInputsFromOptionalInputsV2( TORCH_API void PrepackWeights(std::shared_ptr& graph); +C10_DECLARE_bool(enable_clip_ranges_gather_fusions); } // namespace torch::jit diff --git a/torchgen/operator_versions/gen_mobile_upgraders.py b/torchgen/operator_versions/gen_mobile_upgraders.py index 845034cb7484..d29b274f71bd 100644 --- a/torchgen/operator_versions/gen_mobile_upgraders.py +++ b/torchgen/operator_versions/gen_mobile_upgraders.py @@ -129,12 +129,9 @@ UPGRADER_CPP_SRC = CodeTemplate( MOBILE_UPGRADERS_HEADER_DESCRIPTION + """ #include +#include #include -namespace c10 { -TypePtr parseType(const std::string& pythonStr); -} // namespace c10 - namespace torch { namespace jit {