From 64d9ee88d7da0a63247559e9789075da2fcc7929 Mon Sep 17 00:00:00 2001 From: cyy Date: Mon, 4 Nov 2024 23:57:39 +0000 Subject: [PATCH] [11/N] Fix extra warnings brought by clang-tidy-17 (#139599) Follows #139385 Pull Request resolved: https://github.com/pytorch/pytorch/pull/139599 Approved by: https://github.com/sraikund16 --- .clang-tidy | 3 ++- .lintrunner.toml | 3 +++ aten/src/ATen/core/CachingHostAllocator.h | 1 + aten/src/ATen/cuda/CUDASparseBlas.h | 2 ++ aten/src/ATen/cuda/CUDASparseDescriptors.cpp | 2 ++ aten/src/ATen/cudnn/AutocastRNN.cpp | 2 +- aten/src/ATen/cudnn/Descriptors.cpp | 13 ++++++++----- aten/src/ATen/cudnn/Descriptors.h | 7 ++++--- aten/src/ATen/cudnn/Types.cpp | 4 +--- aten/src/ATen/native/nested/NestedTensorUtils.h | 14 +++++++------- aten/src/ATen/templates/TensorBody.h | 4 ++-- c10/util/intrusive_ptr.h | 8 +++++--- tools/onnx/templates/rules.h.in | 1 + torch/csrc/autograd/python_autograd.h | 1 + torch/csrc/autograd/python_fft_functions.h | 1 + torch/csrc/autograd/python_linalg_functions.h | 1 + torch/csrc/autograd/python_sparse_functions.h | 1 + torch/csrc/autograd/python_special_functions.h | 2 +- torch/csrc/cuda/Module.h | 1 + torch/csrc/cuda/nccl.cpp | 2 ++ torch/csrc/cuda/shared/cudnn.cpp | 3 +-- torch/csrc/profiler/collection.h | 2 +- torch/csrc/profiler/kineto_client_interface.h | 2 +- torch/csrc/profiler/kineto_shim.cpp | 2 +- torch/csrc/profiler/orchestration/observer.cpp | 4 ++-- torch/csrc/profiler/orchestration/observer.h | 2 +- 26 files changed, 54 insertions(+), 34 deletions(-) diff --git a/.clang-tidy b/.clang-tidy index 1f7521ce7600..deff8f0fb8d3 100644 --- a/.clang-tidy +++ b/.clang-tidy @@ -35,13 +35,14 @@ cppcoreguidelines-*, hicpp-exception-baseclass, hicpp-avoid-goto, misc-*, +-misc-confusable-identifiers, -misc-const-correctness, -misc-include-cleaner, -misc-use-anonymous-namespace, -misc-unused-parameters, -misc-no-recursion, -misc-non-private-member-variables-in-classes, --misc-confusable-identifiers, +-misc-unused-using-decls, modernize-*, -modernize-macro-to-enum, -modernize-return-braced-init-list, diff --git a/.lintrunner.toml b/.lintrunner.toml index ea5fc7a4f468..3c628701bfbb 100644 --- a/.lintrunner.toml +++ b/.lintrunner.toml @@ -227,6 +227,9 @@ exclude_patterns = [ '**/generated/**', '**/*pb.h', '**/*inl.h', + 'aten/src/ATen/cpu/FlushDenormal.cpp', + 'aten/src/ATen/cpu/Utils.cpp', + 'aten/src/ATen/cpu/vml.h', 'aten/src/ATen/CPUFixedAllocator.h', 'aten/src/ATen/Parallel*.h', 'c10/xpu/**/*.h', diff --git a/aten/src/ATen/core/CachingHostAllocator.h b/aten/src/ATen/core/CachingHostAllocator.h index bf617ef806c6..87b57b4abaa1 100644 --- a/aten/src/ATen/core/CachingHostAllocator.h +++ b/aten/src/ATen/core/CachingHostAllocator.h @@ -40,6 +40,7 @@ struct alignas(64) FreeBlockList { namespace { // Max cached block sizes: (1 << MAX_SIZE_INDEX) bytes + // NOLINTNEXTLINE(misc-definitions-in-headers) constexpr size_t MAX_SIZE_INDEX = 64; } diff --git a/aten/src/ATen/cuda/CUDASparseBlas.h b/aten/src/ATen/cuda/CUDASparseBlas.h index c99d42c9a7de..a098496491d1 100644 --- a/aten/src/ATen/cuda/CUDASparseBlas.h +++ b/aten/src/ATen/cuda/CUDASparseBlas.h @@ -12,6 +12,7 @@ #include #include +// NOLINTBEGIN(misc-misplaced-const) namespace at::cuda::sparse { #define CUSPARSE_CSRGEAM2_BUFFERSIZE_ARGTYPES(scalar_t) \ @@ -316,3 +317,4 @@ void bsrsm2_solve>( #endif // AT_USE_HIPSPARSE_TRIANGULAR_SOLVE } // namespace at::cuda::sparse +// NOLINTEND(misc-misplaced-const) diff --git a/aten/src/ATen/cuda/CUDASparseDescriptors.cpp b/aten/src/ATen/cuda/CUDASparseDescriptors.cpp index b662996f3bc8..426f43c36ae5 100644 --- a/aten/src/ATen/cuda/CUDASparseDescriptors.cpp +++ b/aten/src/ATen/cuda/CUDASparseDescriptors.cpp @@ -8,6 +8,7 @@ namespace at::cuda::sparse { cusparseStatus_t destroyConstDnMat(const cusparseDnMatDescr* dnMatDescr) { + // NOLINTNEXTLINE(*const-cast) return cusparseDestroyDnMat(const_cast(dnMatDescr)); } @@ -83,6 +84,7 @@ cusparseDnMatDescr_t createRawDnMatDescriptor(const Tensor& input, int64_t batch #endif auto batch_stride = ndim > 2 && batch_offset >= 0 ? input_strides[ndim - 3] : 0; + // NOLINTNEXTLINE(*const-cast) void* data_ptr = is_const ? const_cast(input.const_data_ptr()) : input.data_ptr(); void* values_ptr = static_cast(data_ptr) + batch_offset * batch_stride * input.itemsize(); diff --git a/aten/src/ATen/cudnn/AutocastRNN.cpp b/aten/src/ATen/cudnn/AutocastRNN.cpp index 71cd199b3379..84571c9b45dc 100644 --- a/aten/src/ATen/cudnn/AutocastRNN.cpp +++ b/aten/src/ATen/cudnn/AutocastRNN.cpp @@ -18,7 +18,7 @@ Autocast wrapper for CuDNN RNNs (the weight reflattening needs special attention // To be registered for the "_cudnn_rnn(...)" schema. // _cudnn_rnn is autograd-exposed (test_autocast_cudnn_rnn in test_cuda.py includes a test to confirm) -std::tuple +static std::tuple _cudnn_rnn_cast_reflatten(const Tensor & input, TensorList weight, int64_t weight_stride0, diff --git a/aten/src/ATen/cudnn/Descriptors.cpp b/aten/src/ATen/cudnn/Descriptors.cpp index 8c2a4467a479..d7c32ac2cf33 100644 --- a/aten/src/ATen/cudnn/Descriptors.cpp +++ b/aten/src/ATen/cudnn/Descriptors.cpp @@ -6,6 +6,7 @@ #include #include +// NOLINTBEGIN(*c-arrays*) namespace at::native { namespace { @@ -101,7 +102,7 @@ std::ostream& operator<<(std::ostream & out, const TensorDescriptor& d) { int nbDims = 0; int dimA[CUDNN_DIM_MAX]; int strideA[CUDNN_DIM_MAX]; - cudnnDataType_t dtype; + cudnnDataType_t dtype{}; cudnnGetTensorNdDescriptor(d.desc(), CUDNN_DIM_MAX, &dtype, &nbDims, dimA, strideA); out << " type = " << cudnnTypeToString(dtype) << "\n"; out << " nbDims = " << nbDims << "\n"; @@ -143,7 +144,7 @@ void FilterDescriptor::set(const at::Tensor &t, const at::MemoryFormat memory_fo size[i] = (int) 1; } dim = std::max(dim, pad); - cudnnTensorFormat_t filter_format; + cudnnTensorFormat_t filter_format{}; switch(memory_format) { case at::MemoryFormat::Contiguous: filter_format = CUDNN_TENSOR_NCHW; @@ -155,7 +156,8 @@ void FilterDescriptor::set(const at::Tensor &t, const at::MemoryFormat memory_fo default: TORCH_INTERNAL_ASSERT(false, "unsupported memory_format for cuDNN filters"); } - set(getDataType(t), (int) dim, size, filter_format); + // NOLINTNEXTLINE(*narrowing-conversions) + set(getDataType(t), static_cast(dim), size, filter_format); } std::string cudnnMemoryFormatToString(cudnnTensorFormat_t tformat) { @@ -175,8 +177,8 @@ std::ostream& operator<<(std::ostream & out, const FilterDescriptor& d) { out << "FilterDescriptor " << static_cast(d.desc()) << "\n"; int nbDims = 0; int dimA[CUDNN_DIM_MAX]; - cudnnDataType_t dtype; - cudnnTensorFormat_t tformat; + cudnnDataType_t dtype{}; + cudnnTensorFormat_t tformat{}; cudnnGetFilterNdDescriptor(d.desc(), CUDNN_DIM_MAX, &dtype, &tformat, &nbDims, dimA); out << " type = " << cudnnTypeToString(dtype) << "\n"; out << " tensor_format = " << cudnnMemoryFormatToString(tformat) << "\n"; @@ -193,3 +195,4 @@ std::ostream& operator<<(std::ostream & out, const FilterDescriptor& d) { void FilterDescriptor::print() { std::cout << *this; } } +// NOLINTEND(*c-arrays*) diff --git a/aten/src/ATen/cudnn/Descriptors.h b/aten/src/ATen/cudnn/Descriptors.h index 8773af62fd62..6c2492b12e6b 100644 --- a/aten/src/ATen/cudnn/Descriptors.h +++ b/aten/src/ATen/cudnn/Descriptors.h @@ -92,6 +92,7 @@ struct DescriptorDeleter { // initialized the first time you call set() or any other initializing // function. template +// NOLINTNEXTLINE(bugprone-exception-escape) class TORCH_CUDA_CPP_API Descriptor { public: // TODO: Figure out why const-correctness doesn't work here @@ -128,7 +129,7 @@ public: void set(const at::Tensor &t, cudnnRNNDataLayout_t layout, int maxSeqLength, int batchSize, int vectorSize, const int* seqLengthArray); private: void set(cudnnDataType_t dataType, cudnnRNNDataLayout_t layout, int maxSeqLength, int batchSize, int vectorSize, const int* seqLengthArray) { - AT_CUDNN_CHECK(cudnnSetRNNDataDescriptor(mut_desc(), dataType, layout, maxSeqLength, batchSize, vectorSize, seqLengthArray, NULL)); + AT_CUDNN_CHECK(cudnnSetRNNDataDescriptor(mut_desc(), dataType, layout, maxSeqLength, batchSize, vectorSize, seqLengthArray, nullptr)); } }; @@ -224,6 +225,7 @@ struct TORCH_CUDA_CPP_API SpatialTransformerDescriptor } }; +// NOLINTNEXTLINE(bugprone-exception-escape) struct TORCH_CUDA_CPP_API DropoutDescriptor : public Descriptor< cudnnDropoutStruct, @@ -244,9 +246,8 @@ struct TORCH_CUDA_CPP_API DropoutDescriptor } // Restore a dropout descriptor given a dropout probability and existing RNG state. - void set(cudnnHandle_t handle, float dropout, at::Tensor state_) { + void set(cudnnHandle_t handle, float dropout, const at::Tensor& state) { TORCH_INTERNAL_ASSERT(dropout > 0, "dropout must be nonzero; otherwise call set_no_dropout"); - state = state_; void *state_ptr = state.data_ptr(); size_t state_size = state.size(0); // NB: The seed doesn't actually matter, so we give a dummy value diff --git a/aten/src/ATen/cudnn/Types.cpp b/aten/src/ATen/cudnn/Types.cpp index 4269f1dc0d4f..f6e080c433d6 100644 --- a/aten/src/ATen/cudnn/Types.cpp +++ b/aten/src/ATen/cudnn/Types.cpp @@ -5,7 +5,7 @@ namespace at::native { cudnnDataType_t getCudnnDataTypeFromScalarType(const at::ScalarType dtype) { - if (dtype == c10::kQInt8) { + if (dtype == c10::kQInt8 || dtype == at::kChar) { return CUDNN_DATA_INT8; } else if (dtype == at::kFloat) { return CUDNN_DATA_FLOAT; @@ -19,8 +19,6 @@ cudnnDataType_t getCudnnDataTypeFromScalarType(const at::ScalarType dtype) { return CUDNN_DATA_INT32; } else if (dtype == at::kByte) { return CUDNN_DATA_UINT8; - } else if (dtype == at::kChar) { - return CUDNN_DATA_INT8; } std::string msg("getCudnnDataTypeFromScalarType() not supported for "); msg += toString(dtype); diff --git a/aten/src/ATen/native/nested/NestedTensorUtils.h b/aten/src/ATen/native/nested/NestedTensorUtils.h index 4630aa3fde09..e36ae8a372f9 100644 --- a/aten/src/ATen/native/nested/NestedTensorUtils.h +++ b/aten/src/ATen/native/nested/NestedTensorUtils.h @@ -32,7 +32,7 @@ struct NestedTensorImpl; // The following functions are used to construct nested tensors from buffers and // metadata. -inline at::Tensor wrap_buffer(at::Tensor buffer, at::Tensor nested_sizes) { +inline at::Tensor wrap_buffer(const at::Tensor& buffer, const at::Tensor& nested_sizes) { TORCH_CHECK( buffer.dim() == 1, "Expected given buffer to be 1dim, but got ", @@ -41,19 +41,19 @@ inline at::Tensor wrap_buffer(at::Tensor buffer, at::Tensor nested_sizes) { TORCH_CHECK( buffer.is_contiguous(), "Expected given buffer to be contiguous."); return at::detail::make_tensor( - std::move(buffer), std::move(nested_sizes)); + buffer, nested_sizes); } // TODO: Figure out if we need a non-moving wrap_buffer() inline at::Tensor wrap_buffer( - at::Tensor buffer, + const at::Tensor& buffer, at::Tensor nested_sizes, at::Tensor nested_strides, at::Tensor storage_offsets) { TORCH_INTERNAL_ASSERT_DEBUG_ONLY( buffer.is_contiguous(), "Given buffer must be contiguous."); return at::detail::make_tensor( - std::move(buffer), + buffer, std::move(nested_sizes), std::move(nested_strides), std::move(storage_offsets)); @@ -95,9 +95,9 @@ inline at::Tensor create_nested_view_tensor( return at::detail::make_tensor( c10::TensorImpl::VIEW, base, - nested_sizes, - nested_strides, - storage_offsets); + std::move(nested_sizes), + std::move(nested_strides), + std::move(storage_offsets)); } // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ diff --git a/aten/src/ATen/templates/TensorBody.h b/aten/src/ATen/templates/TensorBody.h index 2e1520392ef9..7956ffb6aefd 100644 --- a/aten/src/ATen/templates/TensorBody.h +++ b/aten/src/ATen/templates/TensorBody.h @@ -195,7 +195,7 @@ class TORCH_API Tensor: public TensorBase { // // TODO: temporarily disabled - Tensor& operator=(const TensorBase& x) & { + Tensor& operator=(const TensorBase& x) & noexcept { impl_ = x.getIntrusivePtr(); return *this; } @@ -204,7 +204,7 @@ class TORCH_API Tensor: public TensorBase { return *this; } - Tensor& operator=(const Tensor &x) & { + Tensor& operator=(const Tensor &x) & noexcept { return operator=(static_cast(x)); } Tensor& operator=(Tensor &&x) & noexcept { diff --git a/c10/util/intrusive_ptr.h b/c10/util/intrusive_ptr.h index e1d551930e16..288b19df0a6c 100644 --- a/c10/util/intrusive_ptr.h +++ b/c10/util/intrusive_ptr.h @@ -664,15 +664,17 @@ struct MaybeOwnedTraits> { toDestroy.release(); } - static const owned_type& referenceFromBorrow(const borrow_type& borrow) { + static const owned_type& referenceFromBorrow( + const borrow_type& borrow) noexcept { return borrow; } - static const owned_type* pointerFromBorrow(const borrow_type& borrow) { + static const owned_type* pointerFromBorrow( + const borrow_type& borrow) noexcept { return &borrow; } - static bool debugBorrowIsValid(const borrow_type& /*borrow*/) { + static bool debugBorrowIsValid(const borrow_type& /*borrow*/) noexcept { return true; } }; diff --git a/tools/onnx/templates/rules.h.in b/tools/onnx/templates/rules.h.in index c4ec775b83fc..5d3e26012c4d 100644 --- a/tools/onnx/templates/rules.h.in +++ b/tools/onnx/templates/rules.h.in @@ -1,4 +1,5 @@ #pragma once +#include /** ${generated_comment} diff --git a/torch/csrc/autograd/python_autograd.h b/torch/csrc/autograd/python_autograd.h index a854d30c895c..73401b15ce3b 100644 --- a/torch/csrc/autograd/python_autograd.h +++ b/torch/csrc/autograd/python_autograd.h @@ -1,5 +1,6 @@ #ifndef THP_AUTOGRAD_H #define THP_AUTOGRAD_H +#include PyObject* THPAutograd_initExtension(PyObject* _unused, PyObject* unused); void THPAutograd_initFunctions(); diff --git a/torch/csrc/autograd/python_fft_functions.h b/torch/csrc/autograd/python_fft_functions.h index b95d25effcbb..1ce94653e1cb 100644 --- a/torch/csrc/autograd/python_fft_functions.h +++ b/torch/csrc/autograd/python_fft_functions.h @@ -1,4 +1,5 @@ #pragma once +#include namespace torch::autograd { diff --git a/torch/csrc/autograd/python_linalg_functions.h b/torch/csrc/autograd/python_linalg_functions.h index 685c87bb6d2a..9477556279d0 100644 --- a/torch/csrc/autograd/python_linalg_functions.h +++ b/torch/csrc/autograd/python_linalg_functions.h @@ -1,4 +1,5 @@ #pragma once +#include namespace torch::autograd { diff --git a/torch/csrc/autograd/python_sparse_functions.h b/torch/csrc/autograd/python_sparse_functions.h index d97018c51981..02e3b071eab3 100644 --- a/torch/csrc/autograd/python_sparse_functions.h +++ b/torch/csrc/autograd/python_sparse_functions.h @@ -1,4 +1,5 @@ #pragma once +#include namespace torch::autograd { diff --git a/torch/csrc/autograd/python_special_functions.h b/torch/csrc/autograd/python_special_functions.h index d036ce4383b5..a58235214bc9 100644 --- a/torch/csrc/autograd/python_special_functions.h +++ b/torch/csrc/autograd/python_special_functions.h @@ -1,5 +1,5 @@ #pragma once - +#include namespace torch::autograd { void initSpecialFunctions(PyObject* module); diff --git a/torch/csrc/cuda/Module.h b/torch/csrc/cuda/Module.h index 0c89e4bc65f2..f3a5ccb925e4 100644 --- a/torch/csrc/cuda/Module.h +++ b/torch/csrc/cuda/Module.h @@ -1,5 +1,6 @@ #ifndef THCP_CUDA_MODULE_INC #define THCP_CUDA_MODULE_INC +#include PyObject* THCPModule_getDevice_wrap(PyObject* self); PyObject* THCPModule_setDevice_wrap(PyObject* self, PyObject* arg); diff --git a/torch/csrc/cuda/nccl.cpp b/torch/csrc/cuda/nccl.cpp index 15c4273b6867..d7d8c9385e97 100644 --- a/torch/csrc/cuda/nccl.cpp +++ b/torch/csrc/cuda/nccl.cpp @@ -273,6 +273,7 @@ struct NcclCommList { devices.data())); } NcclCommList(NcclCommList&& foo) = default; + // NOLINTNEXTLINE(bugprone-exception-escape) ~NcclCommList() { if (comms) { for (const auto i : c10::irange(ndevices)) { @@ -457,6 +458,7 @@ AutoNcclGroup::AutoNcclGroup(ncclComm_t comm, bool comm_nonblocking) #endif } +// NOLINTNEXTLINE(bugprone-exception-escape) AutoNcclGroup::~AutoNcclGroup() noexcept(false) { #if defined(NCCL_MAJOR) && (NCCL_MAJOR >= 2) if (comm_nonblocking_ && comm_ != nullptr) { diff --git a/torch/csrc/cuda/shared/cudnn.cpp b/torch/csrc/cuda/shared/cudnn.cpp index 30a1383455be..f56899107fd5 100644 --- a/torch/csrc/cuda/shared/cudnn.cpp +++ b/torch/csrc/cuda/shared/cudnn.cpp @@ -4,7 +4,6 @@ #if defined(USE_CUDNN) || defined(USE_ROCM) #include -#include #include namespace { @@ -22,7 +21,7 @@ version_tuple getCompileVersion() { version_tuple getRuntimeVersion() { #ifndef USE_STATIC_CUDNN - int major, minor, patch; + int major = 0, minor = 0, patch = 0; cudnnGetProperty(MAJOR_VERSION, &major); cudnnGetProperty(MINOR_VERSION, &minor); cudnnGetProperty(PATCH_LEVEL, &patch); diff --git a/torch/csrc/profiler/collection.h b/torch/csrc/profiler/collection.h index 0b5bad4d2b49..01f02fa94fb6 100644 --- a/torch/csrc/profiler/collection.h +++ b/torch/csrc/profiler/collection.h @@ -378,7 +378,7 @@ struct TORCH_API Result : public std::enable_shared_from_this { } template - void visit_if_base(Fn&& fn) const { + void visit_if_base(const Fn& fn) const { visit([&](const auto& extra_fields) { using extra_fields_t = typename std::remove_cv_t< typename std::remove_reference_t>; diff --git a/torch/csrc/profiler/kineto_client_interface.h b/torch/csrc/profiler/kineto_client_interface.h index 6d3282560852..6cfabfd111cf 100644 --- a/torch/csrc/profiler/kineto_client_interface.h +++ b/torch/csrc/profiler/kineto_client_interface.h @@ -6,6 +6,6 @@ namespace torch { // declare global_kineto_init for libtorch_cpu.so to call -TORCH_API void global_kineto_init(void); +TORCH_API void global_kineto_init(); } // namespace torch diff --git a/torch/csrc/profiler/kineto_shim.cpp b/torch/csrc/profiler/kineto_shim.cpp index 4cbaf7c9a530..1bdff80b9b91 100644 --- a/torch/csrc/profiler/kineto_shim.cpp +++ b/torch/csrc/profiler/kineto_shim.cpp @@ -222,7 +222,7 @@ bool collectivesProfilerExists() { #ifdef USE_KINETO static const std::string setTraceID(const std::string& trace_id) { - if (trace_id == "") { + if (trace_id.empty()) { return ""; } std::stringstream configss; diff --git a/torch/csrc/profiler/orchestration/observer.cpp b/torch/csrc/profiler/orchestration/observer.cpp index c4d25f5f0786..4b443ccc23ee 100644 --- a/torch/csrc/profiler/orchestration/observer.cpp +++ b/torch/csrc/profiler/orchestration/observer.cpp @@ -39,7 +39,7 @@ ProfilerConfig::ProfilerConfig( bool with_flops, bool with_modules, ExperimentalConfig experimental_config, - const std::string& trace_id) + std::string trace_id) : state{state}, experimental_config{std::move(experimental_config)}, report_input_shapes{report_input_shapes}, @@ -47,7 +47,7 @@ ProfilerConfig::ProfilerConfig( with_stack{with_stack}, with_flops{with_flops}, with_modules{with_modules}, - trace_id{trace_id} {} + trace_id{std::move(trace_id)} {} bool ProfilerConfig::disabled() const { return state == torch::profiler::impl::ProfilerState::Disabled; diff --git a/torch/csrc/profiler/orchestration/observer.h b/torch/csrc/profiler/orchestration/observer.h index ef7b4d4566ea..c3beb4cca4d0 100644 --- a/torch/csrc/profiler/orchestration/observer.h +++ b/torch/csrc/profiler/orchestration/observer.h @@ -104,7 +104,7 @@ struct TORCH_API ProfilerConfig { bool with_flops = false, bool with_modules = false, ExperimentalConfig experimental_config = ExperimentalConfig(), - const std::string& trace_id = ""); + std::string trace_id = ""); bool disabled() const; bool global() const;