From 1feae27ed6ebe11134577560a651c9660dc46e2e Mon Sep 17 00:00:00 2001 From: cyy Date: Tue, 24 Dec 2024 03:29:35 +0000 Subject: [PATCH] [16/N] Fix extra warnings brought by clang-tidy-17 (#143714) Fixes #ISSUE_NUMBER Pull Request resolved: https://github.com/pytorch/pytorch/pull/143714 Approved by: https://github.com/Skylion007, https://github.com/albanD --- .lintrunner.toml | 1 + aten/src/ATen/SDPBackend.h | 1 + aten/src/ATen/cuda/CUDABlas.cpp | 3 +- aten/src/ATen/functorch/BatchRulesModules.cpp | 4 +- torch/_inductor/codegen/cpp_prefix.h | 44 +++++++++---------- torch/csrc/Generator.cpp | 3 +- .../api/include/torch/nn/modules/upsampling.h | 2 +- torch/csrc/api/src/nn/modules/upsampling.cpp | 4 +- torch/csrc/dynamo/guards.cpp | 18 ++++---- .../aoti_package/model_package_loader.cpp | 1 + 10 files changed, 40 insertions(+), 41 deletions(-) diff --git a/.lintrunner.toml b/.lintrunner.toml index 7bf014661b7..595f9bffc32 100644 --- a/.lintrunner.toml +++ b/.lintrunner.toml @@ -266,6 +266,7 @@ exclude_patterns = [ 'torch/csrc/inductor/aoti_torch/c/shim.h', 'torch/csrc/jit/**/*', 'torch/csrc/jit/serialization/mobile_bytecode_generated.h', + 'torch/csrc/utils/generated_serialization_types.h', 'torch/csrc/utils/pythoncapi_compat.h', 'torch/csrc/inductor/aoti_runtime/sycl_runtime_wrappers.h', ] diff --git a/aten/src/ATen/SDPBackend.h b/aten/src/ATen/SDPBackend.h index 5328842ae07..93267a41a45 100644 --- a/aten/src/ATen/SDPBackend.h +++ b/aten/src/ATen/SDPBackend.h @@ -1,4 +1,5 @@ #pragma once +#include namespace at { diff --git a/aten/src/ATen/cuda/CUDABlas.cpp b/aten/src/ATen/cuda/CUDABlas.cpp index 76818c1c26e..67d1f330d6c 100644 --- a/aten/src/ATen/cuda/CUDABlas.cpp +++ b/aten/src/ATen/cuda/CUDABlas.cpp @@ -284,8 +284,7 @@ class CuBlasLtMatmulDescriptor : public CuBlasLtDescriptor< } template inline void setAttribute(cublasLtMatmulDescAttributes_t attr, const T value) { - // NOLINTNEXTLINE(bugprone-sizeof-expression) - TORCH_CUDABLAS_CHECK(::cublasLtMatmulDescSetAttribute(descriptor(), attr, &value, sizeof(T))); + TORCH_CUDABLAS_CHECK(::cublasLtMatmulDescSetAttribute(descriptor(), attr, &value, sizeof(value))); } }; diff --git a/aten/src/ATen/functorch/BatchRulesModules.cpp b/aten/src/ATen/functorch/BatchRulesModules.cpp index eb409e2eb6e..2f88bff9efc 100644 --- a/aten/src/ATen/functorch/BatchRulesModules.cpp +++ b/aten/src/ATen/functorch/BatchRulesModules.cpp @@ -111,7 +111,7 @@ embedding_dense_backward_batch_rule( */ template std::tuple> -grid_sample_batch_rule(const Tensor& input, std::optional input_bdim, const Tensor& grid, std::optional grid_bdim, ExtraArgs... extra_args) { +static grid_sample_batch_rule(const Tensor& input, std::optional input_bdim, const Tensor& grid, std::optional grid_bdim, ExtraArgs... extra_args) { std::tuple> result; if (input_bdim && !grid_bdim) { auto new_input = reshape_dim_into(*input_bdim, 1, input); @@ -175,7 +175,7 @@ grid_sample_backward_helper_out( template std::tuple, Tensor, std::optional> -grid_sample_backward_batch_rule( +static grid_sample_backward_batch_rule( const Tensor& grad_output, std::optional grad_output_bdim, const Tensor& input, std::optional input_bdim, const Tensor& grid, std::optional grid_bdim, diff --git a/torch/_inductor/codegen/cpp_prefix.h b/torch/_inductor/codegen/cpp_prefix.h index 8fc9e63dbee..94aec3d3340 100644 --- a/torch/_inductor/codegen/cpp_prefix.h +++ b/torch/_inductor/codegen/cpp_prefix.h @@ -136,10 +136,10 @@ Welford welford_combine(const Welford& acc, const T& data, const WeightRec template struct IndexValue { - int64_t index; + int64_t index{}; T value; - IndexValue(int64_t idx, T val) :index(idx), value(val) {}; - IndexValue() {}; + IndexValue(int64_t idx, T val) :index(idx), value(val) {} + IndexValue() = default; }; #if INDUCTOR_USE_VECTOR_TYPES() @@ -564,16 +564,16 @@ constexpr float uint32_to_uniform_float(uint32_t value) { return static_cast(value & 0x7FFFFFFF) * scale; } -float normalized_rand_cpu(uint32_t seed, uint32_t offset) { +inline float normalized_rand_cpu(uint32_t seed, uint32_t offset) { return uint32_to_uniform_float(at::Philox4_32(seed, 0, offset)()); } -float randn_cpu(uint32_t seed, uint32_t offset) { +inline float randn_cpu(uint32_t seed, uint32_t offset) { at::Philox4_32 engine(seed, 0, offset); return engine.randn(10); } -int64_t randint64_cpu(uint32_t seed, uint32_t offset, int64_t low, int64_t high) { +inline int64_t randint64_cpu(uint32_t seed, uint32_t offset, int64_t low, int64_t high) { auto gen = at::Philox4_32(seed, 0, offset); uint64_t r0 = gen(); uint64_t r1 = gen(); @@ -646,7 +646,7 @@ void atomic_add_vec(T *addr, at::vec::VectorizedN index, at::vec::V } #endif -std::tuple, int> _get_factors(int64_t number) { +inline std::tuple, int> _get_factors(int64_t number) { int count = 0; for (int64_t i = std::sqrt(number); i > 0; --i) { if (number % i == 0) { @@ -664,7 +664,7 @@ std::tuple, int> _get_factors(int64_t number) { return std::make_tuple(factors, count); } -std::tuple, int> get_factors(int64_t number) { +inline std::tuple, int> get_factors(int64_t number) { thread_local std::map, int>> cache; auto it = cache.find(number); if (it != cache.end()) { @@ -676,7 +676,7 @@ std::tuple, int> get_factors(int64_t number) { } } -void _mm_get_thread_blocking( +inline void _mm_get_thread_blocking( int num_threads, int max_k_slices, int64_t M, @@ -772,7 +772,7 @@ void _mm_get_thread_blocking( assert(Mt != 0); } -void mm_get_thread_blocking( +inline void mm_get_thread_blocking( int num_threads, int max_k_slices, int64_t M, @@ -887,25 +887,23 @@ void mm_get_cache_blocking( } struct amx_tilecfg { - uint8_t palette_id; - uint8_t start_row; - uint8_t reserved_0[14]; - uint16_t colsb[16]; - uint8_t rows[16]; + uint8_t palette_id{0}; + uint8_t start_row{0}; + uint8_t reserved_0[14]{}; + uint16_t colsb[16]{}; + uint8_t rows[16]{}; }; class AMXState { private: - amx_tilecfg tilecfg_; - uint8_t rows_; - uint16_t colsb_; - uint8_t num_tile_rows_; - uint8_t num_tile_columns_; + amx_tilecfg tilecfg_{}; + uint8_t rows_{0}; + uint16_t colsb_{0}; + uint8_t num_tile_rows_{0}; + uint8_t num_tile_columns_{0}; public: - AMXState() : rows_(0), colsb_(0), num_tile_rows_(0), num_tile_columns_(0) { - memset(&tilecfg_, 0, sizeof(tilecfg_)); - } + AMXState() = default; inline void configure( uint8_t rows, diff --git a/torch/csrc/Generator.cpp b/torch/csrc/Generator.cpp index ddc6e26966d..a00985c8aa1 100644 --- a/torch/csrc/Generator.cpp +++ b/torch/csrc/Generator.cpp @@ -154,9 +154,8 @@ static PyObject* THPGenerator_cloneState(PyObject* _self, PyObject* noargs) { // See Note [Acquire lock when using random generators] std::scoped_lock lock(gen.mutex()); - auto new_generator = gen.clone(); - return THPGenerator_Wrap(new_generator); + return THPGenerator_Wrap(gen.clone()); END_HANDLE_TH_ERRORS } diff --git a/torch/csrc/api/include/torch/nn/modules/upsampling.h b/torch/csrc/api/include/torch/nn/modules/upsampling.h index 66513579130..e02658a6af4 100644 --- a/torch/csrc/api/include/torch/nn/modules/upsampling.h +++ b/torch/csrc/api/include/torch/nn/modules/upsampling.h @@ -30,7 +30,7 @@ namespace torch::nn { /// ``` class TORCH_API UpsampleImpl : public Cloneable { public: - explicit UpsampleImpl(const UpsampleOptions& options_ = {}); + explicit UpsampleImpl(UpsampleOptions options_ = {}); void reset() override; diff --git a/torch/csrc/api/src/nn/modules/upsampling.cpp b/torch/csrc/api/src/nn/modules/upsampling.cpp index 186738d8ee5..420ffe5a881 100644 --- a/torch/csrc/api/src/nn/modules/upsampling.cpp +++ b/torch/csrc/api/src/nn/modules/upsampling.cpp @@ -4,8 +4,8 @@ namespace F = torch::nn::functional; namespace torch::nn { -UpsampleImpl::UpsampleImpl(const UpsampleOptions& options_) - : options(options_) {} +UpsampleImpl::UpsampleImpl(UpsampleOptions options_) + : options(std::move(options_)) {} void UpsampleImpl::reset() {} diff --git a/torch/csrc/dynamo/guards.cpp b/torch/csrc/dynamo/guards.cpp index e1ea82e2341..dbcee607cf3 100644 --- a/torch/csrc/dynamo/guards.cpp +++ b/torch/csrc/dynamo/guards.cpp @@ -499,7 +499,7 @@ PyObject* TensorGuards_check_verbose( } std::string fail_reason = checks[i].check_verbose( state, THPVariable_Unpack(item), tensor_check_names[i]); - if (fail_reason.length() > 0) { + if (!fail_reason.empty()) { return Py_BuildValue("s", fail_reason.c_str()); } } @@ -1127,7 +1127,7 @@ std::unordered_set compute_overlapping_tensors( const std::vector& tensors) { std::unordered_set aliased_tensor_indices; for (int64_t i = 0; i < static_cast(tensors.size()); i++) { - auto tensor_i = tensors[i]; + const auto& tensor_i = tensors[i]; for (int64_t j = 0; j < i; j++) { if (!tensors_definitely_do_not_overlap(tensor_i, tensors[j])) { aliased_tensor_indices.insert(i); @@ -1890,7 +1890,7 @@ class STORAGE_OVERLAPPING : public RelationalGuard { py::object verbose_code_parts) : RelationalGuard(std::move(verbose_code_parts)), _overlapping(overlapping), - _checker(checker) {} + _checker(std::move(checker)) {} bool check_nopybind(PyObject* value) override { _checker->add(value, _overlapping); @@ -2210,8 +2210,8 @@ class GuardManager { */ template GuardManager* get_child_manager( - py::object accessor_key, - std::string source, + const py::object& accessor_key, + const std::string& source, py::handle example_value, py::handle guard_manager_enum) { // accessor_key type depends on the GuardAccessorT @@ -4624,11 +4624,11 @@ void install_no_tensor_aliasing_guard( } void install_storage_overlapping_guard_with_checker( - std::shared_ptr checker, + const std::shared_ptr& checker, const py::list& guard_managers, - py::object verbose_code_parts, + const py::object& verbose_code_parts, bool overlapping) { - if (guard_managers.size() == 0) { + if (guard_managers.empty()) { // If there are no GuardManagers, there's no need to create a // STORAGE_OVERLAPPING guard. return; @@ -4648,7 +4648,7 @@ void install_storage_overlapping_guard_with_checker( void install_storage_overlapping_guard( const py::list& overlapping_guard_managers, const py::list& non_overlapping_guard_managers, - py::object verbose_code_parts) { + const py::object& verbose_code_parts) { // Create a single StorageOverlapChecker that will be shared amongst // the 2 STORAGE_OVERLAPPING guards below. std::shared_ptr checker = diff --git a/torch/csrc/inductor/aoti_package/model_package_loader.cpp b/torch/csrc/inductor/aoti_package/model_package_loader.cpp index 1aa408bcafd..9473492bb30 100644 --- a/torch/csrc/inductor/aoti_package/model_package_loader.cpp +++ b/torch/csrc/inductor/aoti_package/model_package_loader.cpp @@ -501,6 +501,7 @@ std::vector AOTIModelPackageLoader::get_constant_fqns() { std::unordered_map constant_name_to_fqn = runner_->getConstantNamesToOriginalFQNs(); std::vector constant_fqns; + constant_fqns.reserve(constant_name_to_fqn.size()); for (const auto& it : constant_name_to_fqn) { constant_fqns.push_back(it.second); }