[9/N] Fixes clang-tidy warnings in c10/util/*.h (#116185)

Continued work to clean headers in c10/util.
Pull Request resolved: https://github.com/pytorch/pytorch/pull/116185
Approved by: https://github.com/Skylion007
This commit is contained in:
cyy
2023-12-22 09:35:41 +00:00
committed by PyTorch MergeBot
parent c7514ccc8c
commit 9a0c217a0a
32 changed files with 115 additions and 100 deletions

View File

@ -4,10 +4,13 @@
// InlineOptionalDeviceGuard.
#include <c10/core/Device.h>
#include <c10/core/DeviceType.h>
#include <c10/core/impl/DeviceGuardImplInterface.h>
#include <c10/core/impl/VirtualGuardImpl.h>
#include <c10/util/C++17.h>
#include <c10/util/Exception.h>
#include <c10/util/Optional.h>
#include <type_traits>
#include <utility>
namespace c10::impl {

View File

@ -1,9 +1,11 @@
#include <c10/macros/Macros.h>
#include <c10/util/Backtrace.h>
#include <c10/util/env.h>
#include <cstdlib>
#include <exception>
#include <iostream>
#include <mutex>
#include <optional>
namespace c10 {
class AbortHandlerHelper {
@ -49,10 +51,9 @@ class AbortHandlerHelper {
namespace detail {
C10_ALWAYS_INLINE void terminate_handler() {
std::cout << "Unhandled exception caught in c10/util/AbortHandler.h"
<< std::endl;
std::cout << "Unhandled exception caught in c10/util/AbortHandler.h" << '\n';
auto backtrace = get_backtrace();
std::cout << backtrace << std::endl << std::flush;
std::cout << backtrace << '\n' << std::flush;
auto prev_handler = AbortHandlerHelper::getInstance().getPrev();
if (prev_handler) {
prev_handler();
@ -70,7 +71,7 @@ C10_ALWAYS_INLINE void set_terminate_handler() {
use_custom_terminate = true;
#endif // _WIN32
auto result = c10::utils::check_env("TORCH_CUSTOM_TERMINATE");
if (result != c10::nullopt) {
if (result != std::nullopt) {
use_custom_terminate = result.value();
}
if (use_custom_terminate) {

View File

@ -2,17 +2,15 @@
#pragma once
#include <c10/macros/Export.h>
#include <array>
#include <chrono>
#include <cstddef>
#include <cstdint>
#include <list>
#include <string>
#include <unordered_map>
#include <vector>
#include <functional>
#include <type_traits>
#include <c10/macros/Macros.h>
#include <c10/util/Optional.h>
#include <c10/util/hash.h>
#include <ctime>
#ifndef _WIN32
#include <ctime>
@ -40,10 +38,10 @@
namespace c10 {
using time_t = int64_t;
using steady_clock_t = std::conditional<
using steady_clock_t = std::conditional_t<
std::chrono::high_resolution_clock::is_steady,
std::chrono::high_resolution_clock,
std::chrono::steady_clock>::type;
std::chrono::steady_clock>;
inline time_t getTimeSinceEpoch() {
auto now = std::chrono::system_clock::now().time_since_epoch();
@ -94,8 +92,8 @@ inline auto getApproximateTime() {
using approx_time_t = decltype(getApproximateTime());
static_assert(
std::is_same<approx_time_t, int64_t>::value ||
std::is_same<approx_time_t, uint64_t>::value,
std::is_same_v<approx_time_t, int64_t> ||
std::is_same_v<approx_time_t, uint64_t>,
"Expected either int64_t (`getTime`) or uint64_t (some TSC reads).");
// Convert `getCount` results to Nanoseconds since unix epoch.

View File

@ -15,12 +15,18 @@
#pragma once
#include <c10/macros/Macros.h>
#include <c10/util/Deprecated.h>
#include <c10/util/Exception.h>
#include <c10/util/SmallVector.h>
#include <array>
#include <cstddef>
#include <cstdint>
#include <initializer_list>
#include <iterator>
#include <ostream>
#include <type_traits>
#include <vector>
namespace c10 {

View File

@ -15,8 +15,7 @@ template <typename T>
struct is_reduced_floating_point
: std::integral_constant<
bool,
std::is_same<T, c10::Half>::value ||
std::is_same<T, c10::BFloat16>::value> {};
std::is_same_v<T, c10::Half> || std::is_same_v<T, c10::BFloat16>> {};
template <typename T>
constexpr bool is_reduced_floating_point_v =

View File

@ -1,8 +1,6 @@
#pragma once
#include <c10/macros/Macros.h>
#include <c10/util/C++17.h>
#include <c10/util/Optional.h>
#include <cstddef>
#if defined(_MSC_VER)
#include <intrin.h>
#endif

View File

@ -1,5 +1,6 @@
#pragma once
#include <c10/macros/Export.h>
#include <c10/util/Exception.h>
/// This file provides some simple utilities for detecting common deadlocks in
@ -15,14 +16,12 @@
/// to directly assert on PyGILState_Check (as it avoids a vcall and also
/// works correctly with torchdeploy.)
namespace c10 {
#define TORCH_ASSERT_NO_GIL_WITHOUT_PYTHON_DEP() \
TORCH_INTERNAL_ASSERT( \
!c10::impl::check_python_gil(), \
"Holding GIL before a blocking operation! Please release the GIL before blocking, or see https://github.com/pytorch/pytorch/issues/56297 for how to release the GIL for destructors of objects")
namespace impl {
namespace c10::impl {
C10_API bool check_python_gil();
@ -46,5 +45,4 @@ struct C10_API PythonGILHooksRegisterer {
}
};
} // namespace impl
} // namespace c10
} // namespace c10::impl

View File

@ -3,6 +3,7 @@
#include <c10/core/SymInt.h>
#include <c10/core/impl/SizesAndStrides.h>
#include <c10/util/SmallVector.h>
#include <cstddef>
#include <cstdint>
namespace c10 {

View File

@ -1,10 +1,11 @@
#ifndef C10_UTIL_EXCEPTION_H_
#define C10_UTIL_EXCEPTION_H_
#include <c10/macros/Export.h>
#include <c10/macros/Macros.h>
#include <c10/util/StringUtil.h>
#include <cstddef>
#include <cstdint>
#include <exception>
#include <string>
#include <variant>

View File

@ -30,9 +30,9 @@
* general - that will allow Python to run without wrong flags.
*/
#include <c10/macros/Export.h>
#include <string>
#include <c10/macros/Macros.h>
#include <c10/util/Registry.h>
namespace c10 {

View File

@ -1,10 +1,11 @@
#include <c10/util/Half.h>
#include <iostream>
#include <type_traits>
namespace c10 {
static_assert(
std::is_standard_layout<Half>::value,
std::is_standard_layout_v<Half>,
"c10::Half must be standard layout.");
std::ostream& operator<<(std::ostream& out, const Half& value) {

View File

@ -9,8 +9,8 @@
/// If you are writing a compute bound kernel, you can use the CUDA half
/// intrinsics directly on the Half type from device code.
#include <c10/macros/Export.h>
#include <c10/macros/Macros.h>
#include <c10/util/C++17.h>
#include <c10/util/TypeSafeSignMath.h>
#include <c10/util/complex.h>
#include <c10/util/floating_point_utils.h>
@ -28,15 +28,10 @@
#include <intrin.h>
#endif
#include <complex>
#include <cstdint>
#include <cstring>
#include <iosfwd>
#include <limits>
#include <sstream>
#include <stdexcept>
#include <string>
#include <utility>
#ifdef __CUDACC__
#include <cuda_fp16.h>
@ -52,8 +47,6 @@
#include <sycl/sycl.hpp> // for SYCL 2020
#endif
#include <typeinfo> // operator typeid
namespace c10 {
namespace detail {

View File

@ -1,5 +1,4 @@
#include <c10/macros/Macros.h>
#include <c10/util/C++17.h>
#include <c10/util/Synchronized.h>
#include <array>
#include <atomic>
@ -77,26 +76,26 @@ class LeftRight final {
}
template <typename F>
auto read(F&& readFunc) const -> typename c10::invoke_result_t<F, const T&> {
auto read(F&& readFunc) const {
detail::IncrementRAII _increment_counter(
&_counters[_foregroundCounterIndex.load()]);
return readFunc(_data[_foregroundDataIndex.load()]);
return std::forward<F>(readFunc)(_data[_foregroundDataIndex.load()]);
}
// Throwing an exception in writeFunc is ok but causes the state to be either
// the old or the new state, depending on if the first or the second call to
// writeFunc threw.
template <typename F>
auto write(F&& writeFunc) -> typename c10::invoke_result_t<F, T&> {
auto write(F&& writeFunc) {
std::unique_lock<std::mutex> lock(_writeMutex);
return _write(writeFunc);
return _write(std::forward<F>(writeFunc));
}
private:
template <class F>
auto _write(const F& writeFunc) -> typename c10::invoke_result_t<F, T&> {
auto _write(const F& writeFunc) {
/*
* Assume, A is in background and B in foreground. In simplified terms, we
* want to do the following:
@ -164,7 +163,7 @@ class LeftRight final {
template <class F>
auto _callWriteFuncOnBackgroundInstance(
const F& writeFunc,
uint8_t localDataIndex) -> typename c10::invoke_result_t<F, T&> {
uint8_t localDataIndex) {
try {
return writeFunc(_data[localDataIndex ^ 1]);
} catch (...) {
@ -204,14 +203,17 @@ class RWSafeLeftRightWrapper final {
RWSafeLeftRightWrapper& operator=(RWSafeLeftRightWrapper&&) noexcept = delete;
template <typename F>
auto read(F&& readFunc) const -> typename c10::invoke_result_t<F, const T&> {
// NOLINTNEXTLINE(cppcoreguidelines-missing-std-forward)
auto read(F&& readFunc) const {
return data_.withLock(
[&readFunc](T const& data) { return readFunc(data); });
[&readFunc](T const& data) { return std::forward<F>(readFunc)(data); });
}
template <typename F>
auto write(F&& writeFunc) -> typename c10::invoke_result_t<F, T&> {
return data_.withLock([&writeFunc](T& data) { return writeFunc(data); });
// NOLINTNEXTLINE(cppcoreguidelines-missing-std-forward)
auto write(F&& writeFunc) {
return data_.withLock(
[&writeFunc](T& data) { return std::forward<F>(writeFunc)(data); });
}
private:

View File

@ -9,7 +9,6 @@
// NB: This Registry works poorly when you have other namespaces.
// Make all macro invocations from inside the at namespace.
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <functional>
@ -20,6 +19,7 @@
#include <unordered_map>
#include <vector>
#include <c10/macros/Export.h>
#include <c10/macros/Macros.h>
#include <c10/util/Type.h>

View File

@ -45,9 +45,8 @@ class scope_exit {
//
// Interface is specified by p0052r2.
template <typename Callable>
scope_exit<typename std::decay<Callable>::type> make_scope_exit(Callable&& F) {
return scope_exit<typename std::decay<Callable>::type>(
std::forward<Callable>(F));
scope_exit<std::decay_t<Callable>> make_scope_exit(Callable&& F) {
return scope_exit<std::decay_t<Callable>>(std::forward<Callable>(F));
}
} // namespace c10

View File

@ -2,8 +2,6 @@
#include <mutex>
#include <c10/util/C++17.h>
namespace c10 {
/**
@ -28,7 +26,7 @@ class Synchronized final {
public:
Synchronized() = default;
Synchronized(T const& data) : data_(data) {}
Synchronized(T&& data) : data_(data) {}
Synchronized(T&& data) : data_(std::move(data)) {}
// Don't permit copy construction, move, assignment, or
// move assignment, since the underlying std::mutex
@ -44,9 +42,9 @@ class Synchronized final {
* provided callback safely.
*/
template <typename CB>
typename c10::invoke_result_t<CB, T&> withLock(CB cb) {
auto withLock(CB&& cb) {
std::lock_guard<std::mutex> guard(this->mutex_);
return cb(this->data_);
return std::forward<CB>(cb)(this->data_);
}
/**
@ -55,9 +53,9 @@ class Synchronized final {
* the provided callback safely.
*/
template <typename CB>
typename c10::invoke_result_t<CB, T const&> withLock(CB cb) const {
auto withLock(CB&& cb) const {
std::lock_guard<std::mutex> guard(this->mutex_);
return cb(this->data_);
return std::forward<CB>(cb)(this->data_);
}
};
} // end namespace c10

View File

@ -2,8 +2,8 @@
#include <c10/macros/Export.h>
#include <cstdint>
#include <memory>
#include <string>
namespace c10 {

View File

@ -1,11 +1,13 @@
#pragma once
#include <c10/util/C++17.h>
#include <c10/util/ConstexprCrc.h>
#include <c10/util/IdWrapper.h>
#include <c10/util/string_view.h>
#include <cinttypes>
#include <functional>
#include <cstdint>
#include <ostream>
#include <stdexcept>
#include <string>
#include <type_traits>
namespace c10::util {

View File

@ -3,6 +3,10 @@
#include <c10/util/C++17.h>
#include <c10/util/TypeTraits.h>
#include <algorithm>
#include <cstddef>
#include <tuple>
#include <type_traits>
#include <utility>
namespace c10::guts {

View File

@ -1,6 +1,7 @@
#pragma once
#include <c10/util/C++17.h>
#include <functional>
#include <type_traits>
namespace c10::guts {
@ -13,7 +14,7 @@ struct is_equality_comparable : std::false_type {};
template <class T>
struct is_equality_comparable<
T,
void_t<decltype(std::declval<T&>() == std::declval<T&>())>>
std::void_t<decltype(std::declval<T&>() == std::declval<T&>())>>
: std::true_type {};
template <class T>
using is_equality_comparable_t = typename is_equality_comparable<T>::type;
@ -24,7 +25,7 @@ using is_equality_comparable_t = typename is_equality_comparable<T>::type;
template <class T, class Enable = void>
struct is_hashable : std::false_type {};
template <class T>
struct is_hashable<T, void_t<decltype(std::hash<T>()(std::declval<T&>()))>>
struct is_hashable<T, std::void_t<decltype(std::hash<T>()(std::declval<T&>()))>>
: std::true_type {};
template <class T>
using is_hashable_t = typename is_hashable<T>::type;

View File

@ -1,6 +1,9 @@
#pragma once
#include <cstddef>
#include <memory>
#include <utility>
#include <c10/macros/Export.h>
#include <c10/macros/Macros.h>
namespace c10 {

View File

@ -1,8 +1,9 @@
#pragma once
#include <c10/util/Exception.h>
#include <c10/util/Optional.h>
#include <cstdlib>
#include <cstring>
#include <optional>
namespace c10 {
namespace utils {
@ -13,7 +14,7 @@ namespace utils {
//
// NB:
// Issues a warning if the value of the environment variable is not 0 or 1.
inline optional<bool> check_env(const char* name) {
inline std::optional<bool> check_env(const char* name) {
#ifdef _MSC_VER
#pragma warning(push)
#pragma warning(disable : 4996)
@ -36,7 +37,7 @@ inline optional<bool> check_env(const char* name) {
envar,
"valid values are 0 or 1.");
}
return c10::nullopt;
return std::nullopt;
}
} // namespace utils
} // namespace c10

View File

@ -1,8 +1,15 @@
#pragma once
#include <c10/util/Exception.h>
#include <cstddef>
#include <functional>
#include <iomanip>
#include <ios>
#include <sstream>
#include <string>
#include <tuple>
#include <type_traits>
#include <utility>
#include <vector>
#include <c10/util/ArrayRef.h>
@ -244,8 +251,7 @@ template <typename T>
size_t simple_get_hash(const T& o);
template <typename T, typename V>
using type_if_not_enum =
typename std::enable_if<!std::is_enum<T>::value, V>::type;
using type_if_not_enum = std::enable_if_t<!std::is_enum_v<T>, V>;
// Use SFINAE to dispatch to std::hash if possible, cast enum types to int
// automatically, and fall back to T::hash otherwise. NOTE: C++14 added support
@ -259,9 +265,8 @@ auto dispatch_hash(const T& o)
}
template <typename T>
typename std::enable_if<std::is_enum<T>::value, size_t>::type dispatch_hash(
const T& o) {
using R = typename std::underlying_type<T>::type;
std::enable_if_t<std::is_enum_v<T>, size_t> dispatch_hash(const T& o) {
using R = std::underlying_type_t<T>;
return std::hash<R>()(static_cast<R>(o));
}

View File

@ -6,8 +6,8 @@
#include <c10/util/TypeSafeSignMath.h>
#include <algorithm>
#include <cstddef>
#include <iterator>
#include <limits>
#include <type_traits>
namespace c10 {

View File

@ -2,7 +2,7 @@
#include <c10/macros/Export.h>
#include <c10/util/Flags.h>
#include <stddef.h>
#include <cstddef>
C10_DECLARE_bool(caffe2_cpu_numa_enabled);

View File

@ -18,21 +18,19 @@
#pragma once
#include <c10/util/C++17.h>
#include <algorithm>
#include <array>
#include <cmath>
#include <cstddef>
#include <cstdint>
#include <functional>
#include <initializer_list>
#include <iterator>
#include <memory>
#include <stdexcept>
#include <type_traits>
#include <utility>
C10_CLANG_DIAGNOSTIC_PUSH()
#if C10_CLANG_HAS_WARNING("-Wimplicit-int-float-conversion")
C10_CLANG_DIAGNOSTIC_IGNORE("-Wimplicit-int-float-conversion")
#endif
#ifdef _MSC_VER
#define SKA_NOINLINE(...) __declspec(noinline) __VA_ARGS__
#else
@ -176,7 +174,7 @@ struct sherwood_v3_entry {
};
inline int8_t log2(uint64_t value) {
static constexpr int8_t table[64] = {
static constexpr std::array<int8_t, 64> table = {
63, 0, 58, 1, 59, 47, 53, 2, 60, 39, 48, 27, 54, 33, 42, 3,
61, 51, 37, 40, 49, 18, 28, 20, 55, 30, 34, 11, 43, 14, 22, 4,
62, 57, 46, 52, 38, 26, 32, 41, 50, 36, 17, 19, 29, 10, 13, 21,
@ -488,9 +486,9 @@ class sherwood_v3_table : private EntryAlloc, private Hasher, private Equal {
// otherwise.
template <
class target_type = const value_type,
class = typename std::enable_if<
std::is_same<target_type, const value_type>::value &&
!std::is_same<target_type, value_type>::value>::type>
class = std::enable_if_t<
std::is_same_v<target_type, const value_type> &&
!std::is_same_v<target_type, value_type>>>
operator templated_iterator<target_type>() const {
return {current};
}
@ -529,6 +527,7 @@ class sherwood_v3_table : private EntryAlloc, private Hasher, private Equal {
return end();
}
const_iterator find(const FindKey& key) const {
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-const-cast)
return const_cast<sherwood_v3_table*>(this)->find(key);
}
uint64_t count(const FindKey& key) const {
@ -742,7 +741,7 @@ class sherwood_v3_table : private EntryAlloc, private Hasher, private Equal {
rehash_for_other_container(*this);
}
void swap(sherwood_v3_table& other) {
void swap(sherwood_v3_table& other) noexcept {
using std::swap;
swap_pointers(other);
swap(static_cast<ArgumentHash&>(*this), static_cast<ArgumentHash&>(other));
@ -824,7 +823,8 @@ class sherwood_v3_table : private EntryAlloc, private Hasher, private Equal {
uint64_t num_buckets_for_reserve(uint64_t num_elements_) const {
return static_cast<uint64_t>(std::ceil(
num_elements_ / std::min(0.5, static_cast<double>(_max_load_factor))));
static_cast<double>(num_elements_) /
std::min(0.5, static_cast<double>(_max_load_factor))));
}
void rehash_for_other_container(const sherwood_v3_table& other) {
rehash(
@ -1601,6 +1601,7 @@ struct prime_number_hash_policy {
// ClosestPrime(p * 2^(1/3)) and ClosestPrime(p * 2^(2/3)) and put those in
// the gaps
// 5. get PrevPrime(2^64) and put it at the end
// NOLINTNEXTLINE(*c-array*)
static constexpr const uint64_t prime_list[] = {
2llu,
3llu,
@ -1788,6 +1789,7 @@ struct prime_number_hash_policy {
11493228998133068689llu,
14480561146010017169llu,
18446744073709551557llu};
// NOLINTNEXTLINE(*c-array*)
static constexpr uint64_t (*const mod_functions[])(uint64_t) = {
&mod0,
&mod2,
@ -2026,7 +2028,7 @@ struct fibonacci_hash_policy {
int8_t next_size_over(uint64_t& size) const {
size = std::max(uint64_t(2), detailv3::next_power_of_two(size));
return 64 - detailv3::log2(size);
return static_cast<int8_t>(64 - detailv3::log2(size));
}
void commit(int8_t shift_) {
shift = shift_;
@ -2137,9 +2139,7 @@ class order_preserving_flat_hash_map
return false;
for (const typename Table::value_type& value : lhs) {
auto found = rhs.find(value.first);
if (found == rhs.end())
return false;
else if (value.second != found->second)
if (found == rhs.end() || value.second != found->second)
return false;
}
return true;
@ -2228,5 +2228,3 @@ struct power_of_two_std_hash : std::hash<T> {
};
} // namespace ska_ordered
C10_CLANG_DIAGNOSTIC_POP()

View File

@ -1,10 +1,7 @@
#pragma once
#include <c10/macros/Macros.h>
#include <c10/util/ArrayRef.h>
#include <iterator>
#include <numeric>
#include <type_traits>
#include <cstdint>
// GCC has __builtin_mul_overflow from before it supported __has_builtin
#ifdef _MSC_VER

View File

@ -1,7 +1,6 @@
#pragma once
// clang-format off
#include <cstddef>
// Default constraint for the probe arguments as operands.
#ifndef TORCH_SDT_ARG_CONSTRAINT

View File

@ -1,6 +1,7 @@
#pragma once
#include <c10/util/ArrayRef.h>
#include <c10/util/DimVector.h>
#include <algorithm>
namespace c10 {

View File

@ -1,10 +1,12 @@
#pragma once
#include <algorithm>
#include <cstddef>
#include <cstring>
#include <functional>
#include <iterator>
#include <limits>
#include <ostream>
#include <stdexcept>
#include <string>
#include <string_view>

View File

@ -3,6 +3,7 @@
#include <c10/macros/Export.h>
#include <optional>
#include <string>
#include <string_view>
#include <utility>
namespace c10 {

View File

@ -2,6 +2,7 @@
#include <array>
#include <atomic>
#include <cstddef>
#include <cstdint>
#include <memory>
#include <mutex>
@ -10,15 +11,17 @@
#include <type_traits>
#include <vector>
#include <c10/macros/Export.h>
#include <c10/macros/Macros.h>
#include <c10/util/Exception.h>
#include <c10/util/Half.h>
#include <c10/util/IdWrapper.h>
#include <c10/util/TypeIndex.h>
#include <c10/util/TypeTraits.h>
#include <c10/util/irange.h>
#include <c10/util/string_view.h>
#include <c10/core/ScalarType.h>
#include <c10/util/irange.h>
/*
* TypeIdentifier is a small type containing an id.