Skip to content

Commit b0be30d

Browse files
cyyeverpytorchmergebot
authored andcommitted
[19/N] Fix extra warnings brought by clang-tidy-17 (pytorch#144448)
Apply more clang-tidy fixes. There was a bug introduced by pytorch#144014 due to incorrect namespace concatenation which is reverted here. Pull Request resolved: pytorch#144448 Approved by: https://github.com/albanD
1 parent 1353f3b commit b0be30d

File tree

27 files changed

+89
-64
lines changed

27 files changed

+89
-64
lines changed

aten/src/ATen/ParallelNative.cpp

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -86,14 +86,14 @@ TaskThreadPoolBase& _get_intraop_pool() {
8686
#endif // C10_MOBILE
8787

8888
// Run lambda function `fn` over `task_id` in [0, `range`) with threadpool.
89-
// `fn` will be called with params: (thread_pool_task_id, task_id).
90-
void _run_with_pool(const std::function<void(int, size_t)>& fn, size_t range) {
89+
// `fn` will be called with params: task_id.
90+
static void _run_with_pool(const std::function<void(size_t)>& fn, size_t range) {
9191
#ifndef C10_MOBILE
9292
for (const auto i : c10::irange(1, range)) {
93-
_get_intraop_pool().run([fn, i]() { fn((int)i, i); });
93+
_get_intraop_pool().run([fn, i]() { fn(i); });
9494
}
9595
// Run the first task on the current thread directly.
96-
fn(0, 0);
96+
fn(0);
9797
#else
9898
caffe2::PThreadPool* const pool = caffe2::pthreadpool();
9999
TORCH_INTERNAL_ASSERT(pool, "Invalid thread pool!");
@@ -102,7 +102,7 @@ void _run_with_pool(const std::function<void(int, size_t)>& fn, size_t range) {
102102
// PThreadPool::run() is blocking. A std::function [const] reference to
103103
// this lambda cannot go out of scope before PThreadPool::run() returns.
104104
[&fn](const size_t task_id) {
105-
fn(0 /* unused */, task_id);
105+
fn(task_id);
106106
}, range);
107107
#endif // C10_MOBILE
108108
}
@@ -113,6 +113,10 @@ struct ParallelRegionGuard {
113113
internal::set_thread_num(task_id);
114114
_set_in_parallel_region(true);
115115
}
116+
ParallelRegionGuard(const ParallelRegionGuard&) = delete;
117+
ParallelRegionGuard(ParallelRegionGuard&&) = delete;
118+
ParallelRegionGuard& operator=(const ParallelRegionGuard&) = delete;
119+
ParallelRegionGuard& operator=(ParallelRegionGuard&&) = delete;
116120

117121
~ParallelRegionGuard() {
118122
_set_in_parallel_region(false);
@@ -124,16 +128,16 @@ struct ParallelRegionGuard {
124128

125129
namespace internal {
126130

127-
inline std::tuple<size_t, size_t> calc_num_tasks_and_chunk_size(
131+
static std::tuple<size_t, size_t> calc_num_tasks_and_chunk_size(
128132
int64_t begin, int64_t end, int64_t grain_size) {
129133
if ((end - begin) < grain_size) {
130134
return std::make_tuple(1, std::max((int64_t)0, end - begin));
131135
}
132136
// Choose number of tasks based on grain size and number of threads.
133-
size_t chunk_size = divup((end - begin), get_num_threads());
137+
int64_t chunk_size = divup((end - begin), get_num_threads());
134138
// Make sure each task is at least grain_size size.
135-
chunk_size = std::max((size_t)grain_size, chunk_size);
136-
size_t num_tasks = divup((end - begin), chunk_size);
139+
chunk_size = std::max(grain_size, chunk_size);
140+
size_t num_tasks = static_cast<size_t>(divup((end - begin), chunk_size));
137141
return std::make_tuple(num_tasks, chunk_size);
138142
}
139143

@@ -157,12 +161,12 @@ void invoke_parallel(
157161
} state;
158162

159163
auto task = [f, &state, begin, end, chunk_size]
160-
(int /* unused */, size_t task_id) {
161-
int64_t local_start = begin + task_id * chunk_size;
164+
(size_t task_id) {
165+
int64_t local_start = static_cast<int64_t>(begin + task_id * chunk_size);
162166
if (local_start < end) {
163-
int64_t local_end = std::min(end, (int64_t)(chunk_size + local_start));
167+
int64_t local_end = std::min(end, static_cast<int64_t>(chunk_size + local_start));
164168
try {
165-
ParallelRegionGuard guard(task_id);
169+
ParallelRegionGuard guard(static_cast<int>(task_id));
166170
f(local_start, local_end);
167171
} catch (...) {
168172
if (!state.err_flag.test_and_set()) {

aten/src/ATen/cuda/CUDABlas.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,7 @@ class CuBlasLtMatmulDescriptor : public CuBlasLtDescriptor<
284284
}
285285
template <typename T>
286286
inline void setAttribute(cublasLtMatmulDescAttributes_t attr, const T value) {
287+
// NOLINTNEXTLINE(bugprone-sizeof-expression)
287288
TORCH_CUDABLAS_CHECK(::cublasLtMatmulDescSetAttribute(descriptor(), attr, &value, sizeof(value)));
288289
}
289290
};
@@ -392,7 +393,7 @@ inline void bgemm_internal_cublaslt(CUDABLAS_BGEMM_ARGTYPES(Dtype)) {
392393
preference.setAttribute(CUBLASLT_MATMUL_PREF_MIN_ALIGNMENT_C_BYTES, c_alignment);
393394
#endif
394395

395-
auto workspace = at::empty(workspaceSize, at::TensorOptions().dtype(at::kByte).device(at::kCUDA));
396+
auto workspace = at::empty(static_cast<int64_t>(workspaceSize), at::TensorOptions().dtype(at::kByte).device(at::kCUDA));
396397

397398
cublasLtMatmulHeuristicResult_t heuristicResult = {};
398399
int returnedResult = 0;
@@ -901,12 +902,10 @@ void gemm_internal_cublas<at::Half>(CUDABLAS_GEMM_ARGTYPES(at::Half)) {
901902
#else
902903
cudaDeviceProp* prop = at::cuda::getCurrentDeviceProperties();
903904
if (prop->major >= 5) {
904-
#ifndef USE_ROCM
905905
cublasMath_t cublas_flags = CUBLAS_DEFAULT_MATH;
906906
if (!at::globalContext().allowFP16ReductionCuBLAS()) {
907907
cublas_flags = static_cast<cublasMath_t>(cublas_flags | CUBLAS_MATH_DISALLOW_REDUCED_PRECISION_REDUCTION);
908908
}
909-
#endif
910909
// Disallow fp16 reductions that could lead to unexpected overflow issues.
911910
TORCH_CUDABLAS_CHECK(cublasSetMathMode(handle, cublas_flags));
912911
TORCH_CUDABLAS_CHECK(cublasGemmEx(
@@ -1284,7 +1283,7 @@ void gemm_and_bias(
12841283
preference.setAttribute(CUBLASLT_MATMUL_PREF_MIN_ALIGNMENT_D_BYTES, d_alignment);
12851284
#endif
12861285

1287-
auto workspace = at::empty(workspaceSize, at::TensorOptions().dtype(at::kByte).device(at::kCUDA));
1286+
auto workspace = at::empty(static_cast<int64_t>(workspaceSize), at::TensorOptions().dtype(at::kByte).device(at::kCUDA));
12881287

12891288
cublasLtMatmulHeuristicResult_t heuristicResult = {};
12901289
int returnedResult = 0;
@@ -1466,7 +1465,7 @@ void scaled_gemm(
14661465
computeDesc.setAttribute(CUBLASLT_MATMUL_DESC_BIAS_DATA_TYPE, ScalarTypeToCudaDataType(bias_dtype));
14671466
}
14681467
size_t workspaceSize = _getWorkspaceSize();
1469-
auto workspace = at::empty(workspaceSize, at::TensorOptions().dtype(at::kByte).device(at::kCUDA));
1468+
auto workspace = at::empty(static_cast<int64_t>(workspaceSize), at::TensorOptions().dtype(at::kByte).device(at::kCUDA));
14701469

14711470
CuBlasLtMatmulPreference preference;
14721471
preference.setAttribute(CUBLASLT_MATMUL_PREF_MAX_WORKSPACE_BYTES, workspaceSize);

aten/src/ATen/cuda/CUDASparseDescriptors.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ cusparseIndexType_t getCuSparseIndexType(const c10::ScalarType& scalar_type) {
5656
}
5757
}
5858

59-
#if AT_USE_CUSPARSE_GENERIC_API() || AT_USE_HIPSPARSE_GENERIC_API()
6059
cusparseDnMatDescr_t createRawDnMatDescriptor(const Tensor& input, int64_t batch_offset, bool is_const=false) {
6160
TORCH_INTERNAL_ASSERT_DEBUG_ONLY(input.layout() == kStrided);
6261
IntArrayRef input_strides = input.strides();
@@ -121,7 +120,6 @@ CuSparseDnMatDescriptor::CuSparseDnMatDescriptor(const Tensor& input, int64_t ba
121120
CuSparseConstDnMatDescriptor::CuSparseConstDnMatDescriptor(const Tensor& input, int64_t batch_offset) {
122121
descriptor_.reset(createRawDnMatDescriptor(input, batch_offset, /*is_const*/true));
123122
}
124-
#endif // AT_USE_CUSPARSE_GENERIC_API() || AT_USE_HIPSPARSE_GENERIC_API()
125123

126124
CuSparseDnVecDescriptor::CuSparseDnVecDescriptor(const Tensor& input) {
127125
// cuSPARSE doesn't support batched vectors

aten/src/ATen/detail/MTIAHooksInterface.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ struct TORCH_API MTIAHooksInterface : AcceleratorHooksInterface {
116116

117117

118118
virtual void recordMemoryHistory(
119-
std::optional<std::string> enabled,
119+
const std::optional<std::string>& enabled,
120120
const std::string& stacks,
121121
size_t max_entries) const {
122122
FAIL_MTIAHOOKS_FUNC(__func__);

aten/src/ATen/functorch/BatchRulesModules.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ grid_sample_backward_helper_in(
162162

163163
static std::tuple<Tensor, std::optional<int64_t>, Tensor, std::optional<int64_t>>
164164
grid_sample_backward_helper_out(
165+
// NOLINTNEXTLINE(performance-unnecessary-value-param)
165166
std::tuple<Tensor, Tensor> bw_out,
166167
int64_t grad_input_out_bdim,
167168
int64_t grad_grid_out_bdim,
@@ -261,7 +262,7 @@ struct UpsampleBackwardBatchRuleHelper<F, Func, typelist<A, B, C, T...>> {
261262

262263
auto out = Func(
263264
std::move(grad_output_),
264-
std::move(output_size),
265+
output_size,
265266
std::move(physical_input_size),
266267
std::forward<T>(extra_args)...);
267268
// NOLINTNEXTLINE(bugprone-unchecked-optional-access)

aten/src/ATen/native/Activation.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -579,7 +579,7 @@ static void _rrelu_with_noise_train(
579579
Tensor& noise,
580580
const Scalar& lower_,
581581
const Scalar& upper_,
582-
std::optional<Generator> generator) {
582+
const std::optional<Generator>& generator) {
583583
using opmath_t = at::opmath_type<scalar_t>;
584584
opmath_t lower = lower_.to<opmath_t>();
585585
opmath_t upper = upper_.to<opmath_t>();

aten/src/ATen/native/mkldnn/xpu/Blas.cpp

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
#include <ATen/ops/mm_native.h>
1818
#endif
1919

20-
namespace at::native::xpu {
20+
namespace at::native {
21+
namespace xpu {
2122

2223
// result = beta * self + alpha * (mat1 * mat2)
2324
Tensor& addmm_out(
@@ -454,7 +455,7 @@ Tensor& tensordot_out(
454455
TORCH_LIBRARY_IMPL(aten, XPU, m) {
455456
m.impl("tensordot.out", TORCH_FN(tensordot_out));
456457
}
457-
} // namespace at::native::xpu
458+
} // namespace xpu
458459

459460
TORCH_IMPL_FUNC(addmm_out_xpu)
460461
(const Tensor& self,
@@ -469,11 +470,13 @@ TORCH_IMPL_FUNC(addmm_out_xpu)
469470

470471
TORCH_IMPL_FUNC(mm_out_xpu)
471472
(const Tensor& self, const Tensor& mat2, const Tensor& result) {
473+
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-const-cast)
472474
xpu::mm_out(self, mat2, const_cast<Tensor&>(result));
473475
}
474476

475477
TORCH_IMPL_FUNC(bmm_out_xpu)
476478
(const Tensor& self, const Tensor& batch2, const Tensor& result) {
479+
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-const-cast)
477480
xpu::bmm_out(self, batch2, const_cast<Tensor&>(result));
478481
}
479482

@@ -498,7 +501,13 @@ TORCH_IMPL_FUNC(baddbmm_out_xpu)
498501
const Scalar& alpha,
499502
const Tensor& result) {
500503
xpu::baddbmm_out(
501-
self, batch1, batch2, beta, alpha, const_cast<Tensor&>(result));
504+
self,
505+
batch1,
506+
batch2,
507+
beta,
508+
alpha,
509+
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-const-cast)
510+
const_cast<Tensor&>(result));
502511
}
503512

504513
TORCH_IMPL_FUNC(addmv_out_xpu)
@@ -508,5 +517,8 @@ TORCH_IMPL_FUNC(addmv_out_xpu)
508517
const Scalar& beta,
509518
const Scalar& alpha,
510519
const Tensor& result) {
520+
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-const-cast)
511521
xpu::addmv_out(self, mat, vec, beta, alpha, const_cast<Tensor&>(result));
512522
}
523+
524+
} // namespace at::native

torch/csrc/Generator.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ PyObject* THPGenerator_initDefaultGenerator(const at::Generator& cdata) {
2626
if (!self)
2727
throw python_error();
2828
auto self_ = reinterpret_cast<THPGenerator*>(self.get());
29-
self_->cdata = std::move(cdata);
29+
self_->cdata = cdata;
3030
return self.release();
3131
}
3232

torch/csrc/api/include/torch/detail/TensorDataContainer.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,7 @@ struct TensorDataContainer {
118118
type_(TensorDataContainerType::InitList) {}
119119
#define TENSOR(T, S) \
120120
TensorDataContainer(T value) \
121-
: sizes_(), \
122-
scalar_type_(at::k##S), \
121+
: scalar_type_(at::k##S), \
123122
type_(TensorDataContainerType::Scalar), \
124123
scalar_(value) {}
125124
AT_FORALL_SCALAR_TYPES_AND3(Bool, Half, BFloat16, TENSOR)

torch/csrc/autograd/python_function.cpp

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ namespace torch::autograd {
136136
// NOTE: this function is written in a way that assumes it's only called for
137137
// backward; it's used by engine.cpp. This is responsible for forwarding a call
138138
// from C++'s Node::apply to a Python method "apply".
139+
// NOLINTNEXTLINE(*-rvalue-reference*)
139140
auto PyNode::apply(variable_list&& inputs) -> variable_list {
140141
pybind11::gil_scoped_acquire gil;
141142
at::OptionalDeviceGuard _device_guard;
@@ -184,7 +185,7 @@ auto PyNode::apply(variable_list&& inputs) -> variable_list {
184185
}
185186

186187
auto PyNode::defer_to_dynamo(
187-
variable_list&& inputs,
188+
const variable_list& inputs,
188189
const std::optional<PyObject*>& compiler) -> variable_list {
189190
pybind11::gil_scoped_acquire gil;
190191
at::OptionalDeviceGuard _device_guard;
@@ -526,7 +527,7 @@ static void THPFunction_dealloc(THPFunction* self) {
526527
Py_TYPE(self)->tp_free((PyObject*)self);
527528
}
528529

529-
PyObject* THPFunction_new(
530+
static PyObject* THPFunction_new(
530531
PyTypeObject* type,
531532
PyObject* args,
532533
PyObject* kwargs) {
@@ -875,6 +876,7 @@ struct InputFlags {
875876
std::vector<bool> is_variable_input;
876877
};
877878

879+
namespace {
878880
template <bool enforce_variables>
879881
std::pair<UnpackedInput, InputFlags> unpack_input(PyObject* args) {
880882
UnpackedInput unpacked;
@@ -938,7 +940,7 @@ std::pair<UnpackedInput, InputFlags> unpack_input(PyObject* args) {
938940
// value is assigned by the prim::PythonOp node and helps to eventually route
939941
// the outputs of the subgraph correctly This newly created subgraph is then
940942
// added to the prim::PythonOp node as a subgraph attribute
941-
static void _append_subgraph(
943+
void _append_subgraph(
942944
torch::jit::Node* node,
943945
torch::jit::Graph* graph,
944946
std::vector<torch::jit::Value*> trace_outputs,
@@ -980,7 +982,7 @@ static void _append_subgraph(
980982
}
981983
}
982984

983-
static torch::jit::Node* _trace_pre_record(
985+
torch::jit::Node* _trace_pre_record(
984986
PyObject* op_obj,
985987
PyObject* input_objects,
986988
const variable_list& input_vars) {
@@ -1011,7 +1013,7 @@ static torch::jit::Node* _trace_pre_record(
10111013
std::move(pyobj), arg_types, input_vars, std::move(scalar_args));
10121014
}
10131015

1014-
static void _trace_post_record(
1016+
void _trace_post_record(
10151017
torch::jit::Node* node,
10161018
PyObject* op_obj,
10171019
const variable_list& input_vars,
@@ -1218,8 +1220,6 @@ PyObject* THPFunction_maybe_clear_saved_tensors(
12181220
END_HANDLE_TH_ERRORS
12191221
}
12201222

1221-
namespace {
1222-
12231223
THPObjectPtr make_ctx_input_tuple(
12241224
THPFunction* ctx,
12251225
const UnpackedInput& unpacked_input,
@@ -1253,8 +1253,6 @@ THPObjectPtr make_ctx_input_output_tuple(
12531253
return result;
12541254
}
12551255

1256-
} // namespace
1257-
12581256
static PyObject* THPFunction_setup_context = nullptr;
12591257

12601258
static PyObject* get_base_setup_context() {
@@ -1652,6 +1650,7 @@ PyObject* THPFunction_metadata(THPFunction* self, void* _unused) {
16521650
return metadata;
16531651
END_HANDLE_TH_ERRORS
16541652
}
1653+
} // namespace
16551654

16561655
using getter = PyObject* (*)(PyObject*, void*);
16571656
using setter = int (*)(PyObject*, PyObject*, void*);

0 commit comments

Comments
 (0)