Skip to content

Commit a2396b2

Browse files
cyyeverpytorchmergebot
authored andcommitted
[2/N] Fix extra warnings brought by clang-tidy-17 (pytorch#137459)
Follows pytorch#137407 Pull Request resolved: pytorch#137459 Approved by: https://github.com/Skylion007
1 parent b41fc14 commit a2396b2

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+138
-231
lines changed

aten/src/ATen/SparseCsrTensorUtils.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,8 +144,8 @@ class CheckSparseTensorInvariants {
144144
bool old_state;
145145

146146
public:
147-
CheckSparseTensorInvariants(bool state) {
148-
old_state = at::globalContext().checkSparseTensorInvariants();
147+
CheckSparseTensorInvariants(bool state)
148+
: old_state(at::globalContext().checkSparseTensorInvariants()) {
149149
at::globalContext().setCheckSparseTensorInvariants(state);
150150
}
151151

aten/src/ATen/ThreadLocalState.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ class TORCH_API ThreadLocalState {
8282
!defined(BUILD_LITE_INTERPRETER)
8383
// TLS for autocast dtypes
8484
std::array<at::ScalarType, at::COMPILE_TIME_MAX_DEVICE_TYPES>
85-
autocast_dtypes_;
85+
autocast_dtypes_{};
8686
#endif
8787

8888
friend class ThreadLocalStateGuard;

aten/src/ATen/cuda/CUDAGraph.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ void CUDAGraph::capture_begin(MempoolId_t pool/*=0*/, cudaStreamCaptureMode capt
125125
// due to the capture status being updated _after_ a capture had already started.
126126
c10::cuda::CUDACachingAllocator::beginAllocateToPool(capture_dev_, mempool_id_, [this](cudaStream_t stream) {
127127
cudaStreamCaptureStatus status;
128-
CaptureId_t stream_capture_id;
128+
CaptureId_t stream_capture_id = 0;
129129
AT_CUDA_CHECK(cudaStreamGetCaptureInfo(stream, &status, &stream_capture_id));
130130
return status == cudaStreamCaptureStatus::cudaStreamCaptureStatusActive && stream_capture_id == capture_id_;
131131
});

aten/src/ATen/functorch/BatchRulesConvolution.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,7 @@ static std::tuple<Tensor,Tensor,Tensor> convolution_backward_plumbing(
362362
const Tensor& grad_output_, const Tensor& input_, const Tensor& weight_,
363363
const c10::OptionalArrayRef<SymInt> bias_sizes_opt,
364364
c10::SymIntArrayRef stride, c10::SymIntArrayRef padding, c10::SymIntArrayRef dilation, bool transposed,
365+
// NOLINTNEXTLINE(performance-unnecessary-value-param)
365366
c10::SymIntArrayRef output_padding, c10::SymInt groups, std::array<bool, 3> output_mask) {
366367
const auto maybe_layer = maybeCurrentDynamicLayer();
367368
vmap_check_escaped(maybe_layer, "convolution_backward_plumbing");

aten/src/ATen/functorch/BatchRulesIndexing.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
#include <ATen/core/dispatch/Dispatcher.h>
99
#include <ATen/functorch/BatchRulesHelper.h>
1010

11-
namespace at { namespace functorch {
11+
namespace at::functorch {
1212

1313
#define OP_DECOMPOSE(op) m.impl(#op, static_cast<decltype(&ATEN_FN(op))>(native::op));
1414
#define OP_DECOMPOSE2(op, overload) m.impl(#op"."#overload, static_cast<decltype(&ATEN_FN2(op, overload))>(native::op));
@@ -20,4 +20,4 @@ TORCH_LIBRARY_IMPL(aten, FuncTorchBatched, m) {
2020
OP_DECOMPOSE(_unsafe_masked_index_put_accumulate);
2121
}
2222

23-
}}
23+
}

aten/src/ATen/functorch/BatchRulesModules.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ static Tensor one_hot_decomposition_hack(const Tensor &self, int64_t num_classes
226226
if (num_classes <= 0) {
227227
AT_ERROR("Can not infer total number of classes from empty tensor.");
228228
} else {
229-
shape.push_back(num_classes);
229+
shape.emplace_back(num_classes);
230230
return at::empty_symint(shape, self.options());
231231
}
232232
}
@@ -246,7 +246,7 @@ static Tensor one_hot_decomposition_hack(const Tensor &self, int64_t num_classes
246246
// TORCH_CHECK(num_classes > self.max().item().toLong(), "Class values must be smaller than num_classes.");
247247
// }
248248

249-
shape.push_back(num_classes);
249+
shape.emplace_back(num_classes);
250250
Tensor ret = at::zeros_symint(shape, self.options());
251251
return ret.scatter(-1, self.unsqueeze(-1), 1);
252252
}

aten/src/ATen/functorch/BatchRulesRandomness.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ static std::tuple<Tensor,Tensor> native_dropout_batching_rule(const Tensor& tens
213213
return std::make_tuple(output, mask);
214214
}
215215

216-
static Tensor multinomial_batching_rule(const Tensor& self, const int64_t num_samples, const bool replacement, const std::optional<Generator> generator) {
216+
static Tensor multinomial_batching_rule(const Tensor& self, const int64_t num_samples, const bool replacement, std::optional<Generator> generator) {
217217
c10::impl::ExcludeDispatchKeyGuard guard(DispatchKey::FuncTorchVmapMode);
218218
auto maybe_layer = maybeCurrentDynamicLayer();
219219
const auto cur_level = maybe_layer->layerId();
@@ -237,7 +237,7 @@ static Tensor multinomial_batching_rule(const Tensor& self, const int64_t num_sa
237237
if (is_2D_case) {
238238
self_value = reshape_dim_into(0, 0, self_value);
239239
}
240-
auto out = multinomial(self_value, num_samples, replacement, generator);
240+
auto out = multinomial(self_value, num_samples, replacement, std::move(generator));
241241
if (is_2D_case) {
242242
out = reshape_dim_outof_symint(0, maybe_layer->batchSize(), out);
243243
}
@@ -249,7 +249,7 @@ static Tensor multinomial_batching_rule(const Tensor& self, const int64_t num_sa
249249
// Must be same randomness with unbatched input
250250
// 1D case: S -> multinomial(S) -> S
251251
// 2D case: MS -> multinomial(MS) -> MS
252-
return multinomial(self_value, num_samples, replacement, generator);
252+
return multinomial(self_value, num_samples, replacement, std::move(generator));
253253
}
254254

255255
template <typename A, A a, typename C>

aten/src/ATen/functorch/LegacyVmapTransforms.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ static Tensor moveDimToFrontAndExpand(Tensor tensor, std::optional<int64_t> dim,
102102
} else {
103103
tensor = tensor.unsqueeze(0);
104104
auto expanded_sizes = tensor.sym_sizes().vec();
105-
expanded_sizes[0] = size;
105+
expanded_sizes[0] = std::move(size);
106106
tensor = tensor.expand_symint(expanded_sizes);
107107
}
108108
return tensor;

aten/src/ATen/functorch/PyTorchOperatorHacks.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
#include <ATen/WrapDimUtils.h>
55
#include <ATen/functorch/TensorWrapper.h>
66
#include <ATen/functorch/BatchedTensorImpl.h>
7-
#include <ATen/ATen.h>
87
#include <ATen/Dispatch.h>
98
#include <c10/util/irange.h>
109
#include <ATen/NamedTensorUtils.h>

aten/src/ATen/native/nested/NestedTensorBackward.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99
#include <ATen/NestedTensorImpl.h>
1010
#include <c10/core/DispatchKey.h>
1111
#include <ATen/native/nested/NestedTensorUtils.h>
12-
#include <ATen/native/nested/NestedTensorMath.h>
13-
#include <ATen/native/layer_norm.h>
1412
#include <c10/core/DeviceType.h>
1513

1614
#include <utility>

aten/src/ATen/native/nested/NestedTensorMatmul.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
#include <ATen/core/Tensor.h>
1414
#include <ATen/core/grad_mode.h>
1515
#include <ATen/native/layer_norm.h>
16-
#include <ATen/native/nested/NestedTensorUtils.h>
1716

1817
namespace at::native {
1918

aten/src/ATen/native/nested/cuda/NestedTensorTransformerFunctions.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
#include <ATen/ops/narrow_native.h>
1414
#endif
1515

16-
#include <ATen/native/NonSymbolicBC.h>
1716
#include <ATen/native/nested/NestedTensorTransformerFunctions.h>
1817
#include <ATen/native/nested/NestedTensorTransformerUtils.h>
1918
#include <ATen/native/nested/NestedTensorMath.h>

c10/core/CPUAllocator.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,7 @@ class DefaultMobileCPUAllocator final : public at::Allocator {
114114
}
115115

116116
auto alloc_size = PreGuardBytes + nbytes + PostGuardBytes;
117-
// NOLINTNEXTLINE(cppcoreguidelines-init-variables)
118-
void* data;
117+
void* data = nullptr;
119118
auto allocator_ptr = GetThreadLocalCachingAllocator();
120119
auto profiling_allocator_ptr = GetThreadLocalProfilingAllocator();
121120
if (allocator_ptr != nullptr) {

c10/core/GeneratorImpl.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,7 @@ static uint64_t readURandomLong() {
8888
* a 32 bit number to 64 bit.
8989
*/
9090
uint64_t getNonDeterministicRandom(bool is_cuda) {
91-
// NOLINTNEXTLINE(cppcoreguidelines-init-variables)
92-
uint64_t s;
91+
uint64_t s = 0;
9392
if (!is_cuda) {
9493
#ifdef _WIN32
9594
s = (uint64_t)std::chrono::high_resolution_clock::now()

c10/core/SymbolicShapeMeta.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,6 @@ SymBool SymbolicShapeMeta::compute_is_non_overlapping_and_dense_anydim() const {
186186
return is_contiguous() | compute_non_overlapping_and_dense();
187187
}
188188

189-
// NOLINTNEXTLINE(performance-unnecessary-value-param)
190189
void SymbolicShapeMeta::set_numel(SymInt val) const {
191190
std::scoped_lock lock(mutables_);
192191
if (has_numel()) {

c10/core/TensorImpl.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,6 @@ TensorImpl::TensorImpl(
111111
DispatchKeySet key_set,
112112
const caffe2::TypeMeta data_type)
113113
: storage_(std::move(storage)),
114-
115114
numel_(0),
116115
data_type_(data_type),
117116
device_opt_(storage_.device()),
@@ -123,7 +122,6 @@ TensorImpl::TensorImpl(
123122
}
124123
}
125124

126-
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-member-init)
127125
TensorImpl::TensorImpl(
128126
DispatchKeySet key_set,
129127
const caffe2::TypeMeta data_type,
@@ -137,7 +135,6 @@ TensorImpl::TensorImpl(
137135
const caffe2::TypeMeta data_type,
138136
std::optional<c10::Device> device_opt)
139137
: storage_(std::move(storage)),
140-
141138
numel_(0),
142139
data_type_(data_type),
143140
device_opt_(device_opt) {

c10/core/impl/alloc_cpu.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,7 @@ void* alloc_cpu(size_t nbytes) {
9292
"alloc_cpu() seems to have been called with negative number: ",
9393
nbytes);
9494

95-
// NOLINTNEXTLINE(cppcoreguidelines-init-variables)
96-
void* data;
95+
void* data = nullptr;
9796
#ifdef __ANDROID__
9897
data = memalign(gAlignment, nbytes);
9998
CAFFE_ENFORCE(

c10/mobile/CPUCachingAllocator.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@ std::mutex CPUCachingAllocator::mutex_;
1212
ska::flat_hash_map<void*, size_t> CPUCachingAllocator::allocation_map_;
1313

1414
inline void* CPUCachingAllocator::allocate_and_cache(const size_t bytes) {
15-
// NOLINTNEXTLINE(cppcoreguidelines-init-variables)
16-
void* ptr;
15+
void* ptr = nullptr;
1716
try {
1817
ptr = c10::alloc_cpu(bytes);
1918
} catch (c10::Error&) {

c10/mobile/CPUProfilingAllocator.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -152,10 +152,8 @@ std::vector<uint64_t> formulate_greedy_allocation_plan(
152152
create_and_sort_mem_events(allocation_sizes, allocation_lifetimes);
153153
uint64_t max_offset{0};
154154
for (const auto& mem_event : mem_events) {
155-
// NOLINTNEXTLINE(cppcoreguidelines-init-variables)
156-
uint64_t alloc_offset;
157-
// NOLINTNEXTLINE(cppcoreguidelines-init-variables)
158-
uint64_t new_offset, new_size;
155+
uint64_t alloc_offset = 0;
156+
uint64_t new_offset = 0, new_size = 0;
159157
if (mem_event.type == EventType::Allocate) {
160158
auto it = free_size_to_offset.lower_bound(mem_event.size);
161159
if (it == free_size_to_offset.end()) {

c10/test/util/Half_test.cpp

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -41,17 +41,15 @@ float halfbits2float(unsigned short h) {
4141
unsigned short float2halfbits(float src) {
4242
unsigned x = c10::detail::fp32_to_bits(src);
4343

44-
// NOLINTNEXTLINE(cppcoreguidelines-init-variables,cppcoreguidelines-avoid-magic-numbers)
45-
unsigned u = (x & 0x7fffffff), remainder, shift, lsb, lsb_s1, lsb_m1;
46-
// NOLINTNEXTLINE(cppcoreguidelines-init-variables)
47-
unsigned sign, exponent, mantissa;
44+
// NOLINTNEXTLINE(cppcoreguidelines-avoid-magic-numbers)
45+
unsigned u = (x & 0x7fffffff), shift = 0;
4846

4947
// Get rid of +NaN/-NaN case first.
5048
if (u > 0x7f800000) {
5149
return 0x7fffU;
5250
}
5351

54-
sign = ((x >> 16) & 0x8000);
52+
unsigned sign = ((x >> 16) & 0x8000);
5553

5654
// Get rid of +Inf/-Inf, +0/-0.
5755
if (u > 0x477fefff) {
@@ -61,8 +59,8 @@ unsigned short float2halfbits(float src) {
6159
return (sign | 0x0000);
6260
}
6361

64-
exponent = ((u >> 23) & 0xff);
65-
mantissa = (u & 0x7fffff);
62+
unsigned exponent = ((u >> 23) & 0xff);
63+
unsigned mantissa = (u & 0x7fffff);
6664

6765
if (exponent > 0x70) {
6866
shift = 13;
@@ -72,12 +70,12 @@ unsigned short float2halfbits(float src) {
7270
exponent = 0;
7371
mantissa |= 0x800000;
7472
}
75-
lsb = (1 << shift);
76-
lsb_s1 = (lsb >> 1);
77-
lsb_m1 = (lsb - 1);
73+
unsigned lsb = (1 << shift);
74+
unsigned lsb_s1 = (lsb >> 1);
75+
unsigned lsb_m1 = (lsb - 1);
7876

7977
// Round to nearest even.
80-
remainder = (mantissa & lsb_m1);
78+
unsigned remainder = (mantissa & lsb_m1);
8179
mantissa >>= shift;
8280
if (remainder > lsb_s1 || (remainder == lsb_s1 && (mantissa & 0x1))) {
8381
++mantissa;

c10/test/util/bfloat16_test.cpp

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,14 @@
77

88
namespace {
99
float float_from_bytes(uint32_t sign, uint32_t exponent, uint32_t fraction) {
10-
// NOLINTNEXTLINE(cppcoreguidelines-init-variables)
11-
uint32_t bytes;
12-
bytes = 0;
10+
uint32_t bytes = 0;
1311
bytes |= sign;
1412
bytes <<= 8;
1513
bytes |= exponent;
1614
bytes <<= 23;
1715
bytes |= fraction;
1816

19-
// NOLINTNEXTLINE(cppcoreguidelines-init-variables)
20-
float res;
17+
float res = 0;
2118
std::memcpy(&res, &bytes, sizeof(res));
2219
return res;
2320
}
@@ -160,8 +157,7 @@ TEST(BFloat16Math, NextAfterZero) {
160157
}
161158

162159
float BinaryToFloat(uint32_t bytes) {
163-
// NOLINTNEXTLINE(cppcoreguidelines-init-variables)
164-
float res;
160+
float res = 0;
165161
std::memcpy(&res, &bytes, sizeof(res));
166162
return res;
167163
}

torch/csrc/Exceptions.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,6 @@ using Arg = typename invoke_traits<Func>::template arg<i>::type;
353353

354354
template <typename Func, size_t... Is, bool release_gil>
355355
auto wrap_pybind_function_impl_(
356-
// NOLINTNEXTLINE(cppcoreguidelines-missing-std-forward)
357356
Func&& f,
358357
std::index_sequence<Is...>,
359358
std::bool_constant<release_gil>) {

torch/csrc/Storage.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -482,8 +482,7 @@ static PyObject* THPStorage_get(THPStorage* self, PyObject* index) {
482482
return THPByteUtils_newReal(value);
483483
/* Slice index */
484484
} else if (PySlice_Check(index)) {
485-
// NOLINTNEXTLINE(cppcoreguidelines-init-variables)
486-
Py_ssize_t start, stop, slicelength, step;
485+
Py_ssize_t start = 0, stop = 0, slicelength = 0, step = 0;
487486
if (PySlice_Unpack(index, &start, &stop, &step) < 0) {
488487
return nullptr;
489488
}
@@ -554,8 +553,7 @@ static int THPStorage_set(THPStorage* self, PyObject* index, PyObject* value) {
554553
storage_set(storage, nindex, rvalue);
555554
return 0;
556555
} else if (PySlice_Check(index)) {
557-
// NOLINTNEXTLINE(cppcoreguidelines-init-variables)
558-
Py_ssize_t start, stop, step;
556+
Py_ssize_t start = 0, stop = 0, step = 0;
559557
Py_ssize_t len = static_cast<Py_ssize_t>(storage.nbytes());
560558
if (PySlice_Unpack(index, &start, &stop, &step) < 0) {
561559
return -1;

torch/csrc/StorageSharing.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,6 @@ static PyObject* THPStorage_shareCuda(PyObject* self, PyObject* noargs) {
313313
THPObjectPtr _event_sync_required(Py_None);
314314
Py_INCREF(Py_None);
315315
if (storage.data()) {
316-
// NOLINTNEXTLINE(cppcoreguidelines-init-variables)
317316
auto shandle =
318317
c10::cuda::CUDACachingAllocator::shareIpcHandle(storage.mutable_data());
319318
_handle = PyBytes_FromStringAndSize(
@@ -470,8 +469,7 @@ static PyObject* THPStorage_newSharedCuda(PyObject* _unused, PyObject* args) {
470469
}
471470
auto ipc_event_handle = reinterpret_cast<const cudaIpcEventHandle_t*>(
472471
s_ipc_event_handle.c_str());
473-
// NOLINTNEXTLINE(cppcoreguidelines-init-variables)
474-
cudaEvent_t event;
472+
cudaEvent_t event = nullptr;
475473
cudaIpcOpenEventHandle(&event, *ipc_event_handle);
476474
C10_CUDA_CHECK(
477475
cudaStreamWaitEvent(c10::cuda::getCurrentCUDAStream(device), event, 0));

0 commit comments

Comments
 (0)