Skip to content

Commit 456c87c

Browse files
cyyeverSkylion007
authored andcommitted
[8/N] Fix extra warnings brought by clang-tidy-17 (pytorch#139151)
Fixes #ISSUE_NUMBER Pull Request resolved: pytorch#139151 Approved by: https://github.com/ezyang Co-authored-by: Aaron Gokaslan <aaronGokaslan@gmail.com>
1 parent 44257c0 commit 456c87c

31 files changed

+72
-54
lines changed

.lintrunner.toml

+4
Original file line numberDiff line numberDiff line change
@@ -222,9 +222,11 @@ exclude_patterns = [
222222
# caffe2_pb.h, otherwise we'd have to build protos as part of this CI job.
223223
# CUDA files are also excluded.
224224
'**/fb/**',
225+
'**/generated/**',
225226
'**/*pb.h',
226227
'c10/xpu/**/*.h',
227228
'c10/xpu/**/*.cpp',
229+
'c10/benchmark/intrusive_ptr_benchmark.cpp',
228230
'c10/cuda/CUDAAlgorithm.h',
229231
'c10/util/complex_math.h',
230232
'c10/util/complex_utils.h',
@@ -250,6 +252,8 @@ exclude_patterns = [
250252
'torch/csrc/inductor/aoti_torch/c/shim.h',
251253
'torch/csrc/jit/**/*',
252254
'torch/csrc/jit/serialization/mobile_bytecode_generated.h',
255+
'torch/csrc/utils/pythoncapi_compat.h',
256+
'torch/csrc/utils/throughput_benchmark-inl.h',
253257
]
254258
init_command = [
255259
'python3',

aten/src/ATen/core/Array.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ struct Array {
3030
Array() = default;
3131
Array(const Array&) = default;
3232
Array& operator=(const Array&) = default;
33-
Array(Array&&) = default;
34-
Array& operator=(Array&&) = default;
33+
Array(Array&&) noexcept = default;
34+
Array& operator=(Array&&) noexcept = default;
3535
~Array() = default;
3636
#endif
3737
static constexpr int size() {

torch/csrc/Stream.cpp

+19-24
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ static PyObject* THPStream_pynew(
2424
HANDLE_TH_ERRORS
2525

2626
int64_t stream_id = -1;
27-
int64_t device_type = 0;
28-
int64_t device_index = 0;
27+
c10::DeviceType device_type{};
28+
c10::DeviceIndex device_index{};
2929
int64_t priority = 0;
3030

3131
static torch::PythonArgParser parser({
@@ -42,27 +42,25 @@ static PyObject* THPStream_pynew(
4242
auto default_accelerator = at::getAccelerator(false);
4343
auto device = r.deviceOptional(0);
4444
if (device.has_value()) {
45-
device_type = static_cast<int64_t>(device->type());
46-
device_index = static_cast<int64_t>(device->index());
45+
device_type = device->type();
46+
device_index = device->index();
4747
// Initialize device guard if device is not None.
4848
device_guard_ptr = std::make_unique<c10::DeviceGuard>(device.value());
4949
} else {
5050
// If device is None, we will use the current accelerator and index.
5151
// If the current accelerator is not set, we will use the CPU as device
5252
// type.
53-
device_type = static_cast<int64_t>(
54-
default_accelerator.value_or(c10::DeviceType::CPU));
55-
c10::impl::VirtualGuardImpl impl{
56-
static_cast<c10::DeviceType>(device_type)};
53+
device_type = default_accelerator.value_or(c10::DeviceType::CPU);
54+
c10::impl::VirtualGuardImpl impl{device_type};
5755
const auto current_device = impl.getDevice();
5856
device_index = current_device.index();
5957
}
6058
priority = r.toInt64WithDefault(1, 0);
6159
} else if (r.idx == 1) {
6260
stream_id = r.toInt64WithDefault(0, -1);
63-
device_index = r.toInt64WithDefault(1, 0);
64-
device_type =
65-
r.toInt64WithDefault(2, static_cast<int64_t>(c10::DeviceType::CPU));
61+
device_index = static_cast<c10::DeviceIndex>(r.toInt64WithDefault(1, 0));
62+
device_type = static_cast<c10::DeviceType>(
63+
r.toInt64WithDefault(2, static_cast<int64_t>(c10::DeviceType::CPU)));
6664
priority = r.toInt64WithDefault(3, 0);
6765
} else {
6866
TORCH_CHECK(
@@ -84,19 +82,16 @@ static PyObject* THPStream_pynew(
8482
// manage the lifetime of streams.
8583
std::optional<c10::Stream> stream_opt;
8684
if (r.idx == 0) {
87-
c10::impl::VirtualGuardImpl impl{static_cast<c10::DeviceType>(device_type)};
85+
c10::impl::VirtualGuardImpl impl{device_type};
8886
stream_opt = impl.getNewStream(
89-
c10::Device(static_cast<c10::DeviceType>(device_type), device_index),
90-
static_cast<int>(priority));
87+
c10::Device(device_type, device_index), static_cast<int>(priority));
9188
} else {
92-
stream_opt = c10::Stream::unpack3(
93-
stream_id,
94-
static_cast<c10::DeviceIndex>(device_index),
95-
static_cast<c10::DeviceType>(device_type));
89+
stream_opt = c10::Stream::unpack3(stream_id, device_index, device_type);
9690
}
9791

9892
TORCH_CHECK(stream_opt.has_value(), "Failed to create stream");
9993
self->stream_id = static_cast<int64_t>(stream_opt->id());
94+
// NOLINTNEXTLINE(bugprone-signed-char-misuse)
10095
self->device_index = static_cast<int64_t>(stream_opt->device_index());
10196
self->device_type = static_cast<int64_t>(stream_opt->device_type());
10297

@@ -139,7 +134,7 @@ static PyObject* THPStream_query(PyObject* _self, PyObject* noargs) {
139134

140135
return PyBool_FromLong(c10::Stream::unpack3(
141136
self->stream_id,
142-
self->device_index,
137+
static_cast<c10::DeviceIndex>(self->device_index),
143138
static_cast<c10::DeviceType>(self->device_type))
144139
.query());
145140

@@ -153,7 +148,7 @@ static PyObject* THPStream_synchronize(PyObject* _self, PyObject* noargs) {
153148

154149
c10::Stream::unpack3(
155150
self->stream_id,
156-
self->device_index,
151+
static_cast<c10::DeviceIndex>(self->device_index),
157152
static_cast<c10::DeviceType>(self->device_type))
158153
.synchronize();
159154
}
@@ -167,7 +162,7 @@ static PyObject* THPStream_wait_event(PyObject* _self, PyObject* _event) {
167162
auto event = (THPEvent*)_event;
168163
c10::Stream::unpack3(
169164
self->stream_id,
170-
self->device_index,
165+
static_cast<c10::DeviceIndex>(self->device_index),
171166
static_cast<c10::DeviceType>(self->device_type))
172167
.wait(event->event);
173168
}
@@ -184,11 +179,11 @@ static PyObject* THPStream_wait_stream(PyObject* _self, PyObject* _other) {
184179
c10::EventFlag::PYTORCH_DEFAULT);
185180
new_event.record(c10::Stream::unpack3(
186181
other_stream->stream_id,
187-
other_stream->device_index,
182+
static_cast<c10::DeviceIndex>(other_stream->device_index),
188183
static_cast<c10::DeviceType>(other_stream->device_type)));
189184
c10::Stream::unpack3(
190185
self->stream_id,
191-
self->device_index,
186+
static_cast<c10::DeviceIndex>(self->device_index),
192187
static_cast<c10::DeviceType>(self->device_type))
193188
.wait(new_event);
194189
}
@@ -229,7 +224,7 @@ static PyObject* THPStream_record_event(
229224
TORCH_CHECK(new_event, "event must not be null");
230225
new_event->event.record(c10::Stream::unpack3(
231226
self->stream_id,
232-
self->device_index,
227+
static_cast<c10::DeviceIndex>(self->device_index),
233228
static_cast<c10::DeviceType>(self->device_type)));
234229
return (PyObject*)new_event;
235230
END_HANDLE_TH_ERRORS

torch/csrc/api/include/torch/python.h

-2
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,7 @@
1717

1818
#include <iterator>
1919
#include <string>
20-
#include <unordered_map>
2120
#include <utility>
22-
#include <vector>
2321

2422
namespace torch::python {
2523
namespace detail {

torch/csrc/autograd/python_anomaly_mode.h

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ struct PyAnomalyMetadata : public AnomalyMetadata {
1616
// NOLINTNEXTLINE(cppcoreguidelines-prefer-member-initializer)
1717
dict_ = PyDict_New();
1818
}
19+
// NOLINTNEXTLINE(bugprone-exception-escape)
1920
~PyAnomalyMetadata() override {
2021
// If python is already dead, leak the wrapped python objects
2122
if (Py_IsInitialized()) {

torch/csrc/autograd/python_nested_functions.h

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#pragma once
22

3+
#include <torch/csrc/utils/python_compat.h>
34
namespace torch::autograd {
45

56
PyMethodDef* get_nested_functions_manual();

torch/csrc/autograd/variable.cpp

+8-4
Original file line numberDiff line numberDiff line change
@@ -886,19 +886,23 @@ std::unique_ptr<ViewFunc> ChainedViewFunc::clone_and_set(
886886
if (symints.has_value()) {
887887
TORCH_INTERNAL_ASSERT(symints->size() == num_symints());
888888
first_symints = std::vector<c10::SymInt>(
889-
symints->begin(), symints->begin() + first->num_symints());
889+
symints->begin(),
890+
symints->begin() + static_cast<std::ptrdiff_t>(first->num_symints()));
890891
second_symints = std::vector<c10::SymInt>(
891-
symints->begin() + first->num_symints(), symints->end());
892+
symints->begin() + static_cast<std::ptrdiff_t>(first->num_symints()),
893+
symints->end());
892894
}
893895

894896
std::optional<std::vector<at::Tensor>> first_tensors;
895897
std::optional<std::vector<at::Tensor>> second_tensors;
896898
if (tensors.has_value()) {
897899
TORCH_INTERNAL_ASSERT(tensors->size() == num_tensors());
898900
first_tensors = std::vector<at::Tensor>(
899-
tensors->begin(), tensors->begin() + first->num_tensors());
901+
tensors->begin(),
902+
tensors->begin() + static_cast<std::ptrdiff_t>(first->num_tensors()));
900903
second_tensors = std::vector<at::Tensor>(
901-
tensors->begin() + first->num_tensors(), tensors->end());
904+
tensors->begin() + static_cast<std::ptrdiff_t>(first->num_tensors()),
905+
tensors->end());
902906
}
903907

904908
return std::make_unique<ChainedViewFunc>(

torch/csrc/cuda/Graph.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ void THCPGraph_init(PyObject* module) {
3131
"capture_begin",
3232
[](::at::cuda::CUDAGraph& self,
3333
std::optional<c10::cuda::MempoolId_t> pool_opt,
34-
std::string capture_error_mode) {
35-
cudaStreamCaptureMode capture_mode;
34+
const std::string& capture_error_mode) {
35+
cudaStreamCaptureMode capture_mode{};
3636
c10::cuda::MempoolId_t pool = pool_opt.has_value()
3737
? pool_opt.value()
3838
: c10::cuda::MempoolId_t{0, 0};

torch/csrc/cuda/Module.cpp

+5-4
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,8 @@ PyObject* THCPModule_canDeviceAccessPeer_wrap(PyObject* self, PyObject* args) {
150150
THPUtils_checkLong(arg1), "invalid argument to canDeviceAccessPeer");
151151
TORCH_CHECK(
152152
THPUtils_checkLong(arg2), "invalid argument to canDeviceAccessPeer");
153-
int64_t device = THPUtils_unpackLong(arg1);
154-
int64_t peer_device = THPUtils_unpackLong(arg2);
153+
auto device = THPUtils_unpackDeviceIndex(arg1);
154+
auto peer_device = THPUtils_unpackDeviceIndex(arg2);
155155

156156
torch::utils::device_lazy_init(at::kCUDA);
157157
auto can_access = at::cuda::canDeviceAccessPeer(device, peer_device);
@@ -1719,7 +1719,7 @@ PyObject* THCPModule_cuda_tunableop_get_results(
17191719
for (const auto& [op_sig, kernelmap] : results) {
17201720
result_size += kernelmap.size();
17211721
}
1722-
THPObjectPtr outer_tuple(PyTuple_New(result_size));
1722+
THPObjectPtr outer_tuple(PyTuple_New(static_cast<Py_ssize_t>(result_size)));
17231723
if (!outer_tuple)
17241724
throw python_error();
17251725
size_t result_index = 0;
@@ -1759,7 +1759,8 @@ PyObject* THCPModule_cuda_tunableop_get_validators(
17591759
auto validators = at::cuda::tunable::getTuningContext()
17601760
->GetTuningResultsValidator()
17611761
.GetAllValidators();
1762-
THPObjectPtr outer_tuple(PyTuple_New(validators.size()));
1762+
THPObjectPtr outer_tuple(
1763+
PyTuple_New(static_cast<Py_ssize_t>(validators.size())));
17631764
if (!outer_tuple)
17641765
throw python_error();
17651766
size_t validator_index = 0;

torch/csrc/cuda/Stream.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ static PyObject* THCPStream_pynew(
7171

7272
THCPStream* self = (THCPStream*)ptr.get();
7373
self->stream_id = static_cast<int64_t>(stream.id());
74+
// NOLINTNEXTLINE(bugprone-signed-char-misuse)
7475
self->device_index = static_cast<int64_t>(stream.device_index());
7576
self->device_type = static_cast<int64_t>(stream.device_type());
7677
new (&self->cuda_stream) at::cuda::CUDAStream(stream);

torch/csrc/cuda/nccl.cpp

+3-1
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,9 @@ struct NcclCommList {
265265
NcclCommList(const std::vector<int>& devices)
266266
: comms(new ncclComm_t[devices.size()]), ndevices(devices.size()) {
267267
NCCL_CHECK(ncclCommInitAll(
268-
to_nccl_comm(comms.get()), devices.size(), devices.data()));
268+
to_nccl_comm(comms.get()),
269+
static_cast<int>(devices.size()),
270+
devices.data()));
269271
}
270272
NcclCommList(NcclCommList&& foo) = default;
271273
~NcclCommList() {

torch/csrc/cuda/nccl.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ typedef void* ncclComm_t;
3131
/** redefine nccl unique ID in torch scope. this should be identical to native
3232
* nccl impp. */
3333
#define NCCL_UNIQUE_ID_BYTES 128
34-
// NOLINTNEXTLINE(cppcoreguidelines-avoid-c-arrays,modernize-avoid-c-arrays)
3534
typedef struct {
35+
// NOLINTNEXTLINE(*array)
3636
char internal[NCCL_UNIQUE_ID_BYTES];
3737
} ncclUniqueId;
3838

torch/csrc/cuda/python_comm.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ void initCommMethods(PyObject* module) {
2828
py::call_guard<py::gil_scoped_release>())
2929
.def(
3030
"_broadcast",
31-
[](at::Tensor& tensor, std::vector<int64_t> devices) {
31+
[](at::Tensor& tensor, const std::vector<int64_t>& devices) {
3232
return broadcast(tensor, devices);
3333
},
3434
py::call_guard<py::gil_scoped_release>(),
@@ -46,7 +46,7 @@ void initCommMethods(PyObject* module) {
4646
"_scatter",
4747
[](at::Tensor& tensor,
4848
std::vector<int64_t>& devices,
49-
std::optional<std::vector<int64_t>> chunk_sizes,
49+
const std::optional<std::vector<int64_t>>& chunk_sizes,
5050
int64_t dim,
5151
std::optional<py::object> py_streams) {
5252
std::optional<std::vector<std::optional<at::cuda::CUDAStream>>>

torch/csrc/cuda/python_comm.h

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#pragma once
22

3+
#include <torch/csrc/utils/pythoncapi_compat.h>
34
namespace torch::cuda::python {
45

56
void initCommMethods(PyObject* module);

torch/csrc/cuda/utils.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ THPUtils_PySequence_to_CUDAStreamList(PyObject* obj) {
2727
// Spicy hot reinterpret cast!!
2828
streams.emplace_back(at::cuda::CUDAStream::unpack3(
2929
(reinterpret_cast<THCPStream*>(stream))->stream_id,
30-
(reinterpret_cast<THCPStream*>(stream))->device_index,
30+
static_cast<c10::DeviceIndex>(
31+
reinterpret_cast<THCPStream*>(stream)->device_index),
3132
static_cast<c10::DeviceType>(
3233
(reinterpret_cast<THCPStream*>(stream))->device_type)));
3334
} else if (stream == Py_None) {

torch/csrc/inductor/aoti_runtime/model.h

+2
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@
3333
// https://man7.org/linux/man-pages/man1/objcopy.1.html
3434
// todo: use #embed in C++ 23 once available
3535
// The constants are NOT readonly because they may be mutated.
36+
// NOLINTNEXTLINE(*array*)
3637
extern uint8_t _binary_constants_bin_start[];
38+
// NOLINTNEXTLINE(*array*)
3739
extern uint8_t _binary_constants_bin_end[];
3840

3941
#define AOTI_CONST_GPU_ALIGNMENT 64

torch/csrc/inductor/aoti_runtime/model_container.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ class AOTInductorModelContainer {
225225
}
226226

227227
bool _should_skip_update(const size_t idx) const {
228-
auto constant_type = models_[0]->constant_type(idx);
228+
auto constant_type = models_[0]->constant_type(static_cast<int64_t>(idx));
229229
return constant_type == ConstantType::TensorConstant;
230230
}
231231

torch/csrc/inductor/aoti_runtime/thread_local.h

+4
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ struct ThreadLocalCachedOutputTensor<ArrayRefTensor<T>> {
6363
private:
6464
void realloc(const ArrayRefTensor<T>& t) {
6565
capacity_ = t.numel();
66+
// NOLINTNEXTLINE(*arrays*)
6667
storage_ = std::make_unique<T[]>(t.numel());
6768
AtenTensorHandle handle = nullptr;
6869
AOTI_TORCH_ERROR_CODE_CHECK(aoti_torch_create_tensor_from_blob(
@@ -78,6 +79,7 @@ struct ThreadLocalCachedOutputTensor<ArrayRefTensor<T>> {
7879
tensor_ = handle;
7980
}
8081

82+
// NOLINTNEXTLINE(*arrays*)
8183
std::unique_ptr<T[]> storage_;
8284
int64_t capacity_ = 0;
8385
RAIIAtenTensorHandle tensor_;
@@ -140,6 +142,7 @@ struct ThreadLocalCachedOutputArray<ArrayRefTensor<T>> {
140142
void copy_data_from(const ArrayRefTensor<T>& t) {
141143
if (t.numel() > capacity_) {
142144
capacity_ = t.numel();
145+
// NOLINTNEXTLINE(*arrays*)
143146
storage_ = std::make_unique<T[]>(capacity_);
144147
}
145148
std::copy(t.data(), t.data() + t.numel(), storage_.get());
@@ -148,6 +151,7 @@ struct ThreadLocalCachedOutputArray<ArrayRefTensor<T>> {
148151
}
149152

150153
private:
154+
// NOLINTNEXTLINE(*arrays*)
151155
std::unique_ptr<T[]> storage_;
152156
uint32_t capacity_ = 0;
153157
ArrayRefTensor<T> tensor_;

torch/csrc/inductor/aoti_torch/shim_common.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -1000,6 +1000,7 @@ AOTITorchError aoti_torch_index_put_out(
10001000
AtenTensorHandle self,
10011001
const AtenTensorHandle* indices,
10021002
const uint32_t num_indices,
1003+
// NOLINTNEXTLINE(misc-misplaced-const)
10031004
const AtenTensorHandle values,
10041005
bool accumulate) {
10051006
AOTI_TORCH_CONVERT_EXCEPTION_TO_ERROR_CODE({

torch/csrc/lazy/core/hash.h

+1
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ static inline hash_t Hash(const at::Generator& value) {
169169
// Use an arbitrary randomly-selected 64-bit integer rather than a
170170
// small constant that we then hash at runtime so we don't have to
171171
// repeatedly hash a constant at runtime.
172+
// NOLINTNEXTLINE(*-narrowing-conversions)
172173
static const int64_t kNullOpt = 0x8655d738f3678dda;
173174

174175
// Hashing for std::optional types contributes to hash

torch/csrc/lazy/core/metrics.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ class TORCH_API TimedSection {
260260

261261
~TimedSection() {
262262
int64_t now = NowNs();
263-
metric_->AddSample(now, now - start_);
263+
metric_->AddSample(now, static_cast<double>(now - start_));
264264
}
265265

266266
double Elapsed() const {

torch/csrc/lazy/core/shape.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class TORCH_API Shape {
3030
}
3131

3232
int64_t dim() const {
33-
return sizes_.size();
33+
return static_cast<int64_t>(sizes_.size());
3434
}
3535
c10::ArrayRef<int64_t> sizes() const {
3636
return sizes_;

0 commit comments

Comments
 (0)