Skip to content

Commit 11dea83

Browse files
committed
Raise PyTorch compiler standard to C++17
Fixes #56055
1 parent eb650ab commit 11dea83

File tree

16 files changed

+43
-256
lines changed

16 files changed

+43
-256
lines changed

CMakeLists.txt

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ string(FIND "${CMAKE_CXX_FLAGS}" "-std=c++" env_cxx_standard)
3535
if(env_cxx_standard GREATER -1)
3636
message(
3737
WARNING "C++ standard version definition detected in environment variable."
38-
"PyTorch requires -std=c++14. Please remove -std=c++ settings in your environment.")
38+
"PyTorch requires -std=c++17. Please remove -std=c++ settings in your environment.")
3939
endif()
40-
set(CMAKE_CXX_STANDARD 14 CACHE STRING "The C++ standard whose features are requested to build this target.")
40+
set(CMAKE_CXX_STANDARD 17 CACHE STRING "The C++ standard whose features are requested to build this target.")
4141
set(CMAKE_C_STANDARD 11 CACHE STRING "The C standard whose features are requested to build this target.")
4242

4343
if(DEFINED GLIBCXX_USE_CXX11_ABI)
@@ -891,7 +891,6 @@ if(NOT MSVC)
891891
append_cxx_flag_if_supported("-Wno-unused-private-field" CMAKE_CXX_FLAGS)
892892
append_cxx_flag_if_supported("-Wno-inconsistent-missing-override" CMAKE_CXX_FLAGS)
893893
append_cxx_flag_if_supported("-Wno-aligned-allocation-unavailable" CMAKE_CXX_FLAGS)
894-
append_cxx_flag_if_supported("-Wno-c++14-extensions" CMAKE_CXX_FLAGS)
895894
append_cxx_flag_if_supported("-Wno-constexpr-not-const" CMAKE_CXX_FLAGS)
896895
append_cxx_flag_if_supported("-Wno-missing-braces" CMAKE_CXX_FLAGS)
897896
append_cxx_flag_if_supported("-Wunused-lambda-capture" CMAKE_CXX_FLAGS)
@@ -993,7 +992,6 @@ if(APPLE)
993992
endif()
994993
append_cxx_flag_if_supported("-Wno-unused-private-field" CMAKE_CXX_FLAGS)
995994
append_cxx_flag_if_supported("-Wno-missing-braces" CMAKE_CXX_FLAGS)
996-
append_cxx_flag_if_supported("-Wno-c++14-extensions" CMAKE_CXX_FLAGS)
997995
append_cxx_flag_if_supported("-Wno-constexpr-not-const" CMAKE_CXX_FLAGS)
998996
endif()
999997

aten/src/ATen/Dispatch.h

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,6 @@ TORCH_API void record_kernel_function_dtype(std::string name);
5151
#define RECORD_KERNEL_FUNCTION_DTYPE(NAME, enum_type)
5252
#endif
5353

54-
// Avoid if_constexpr if possble, as it's more expensive to compile
55-
#if defined __cpp_if_constexpr
5654
#define AT_PRIVATE_CHECK_SELECTIVE_BUILD(enum_type) \
5755
do { \
5856
if constexpr (!at::should_include_kernel_dtype( \
@@ -64,17 +62,6 @@ TORCH_API void record_kernel_function_dtype(std::string name);
6462
at_dispatch_name); \
6563
} \
6664
} while (0)
67-
#else // defined __cpp_if_constexpr
68-
#define AT_PRIVATE_CHECK_SELECTIVE_BUILD(enum_type) \
69-
at::guts::if_constexpr<!at::should_include_kernel_dtype( \
70-
at_dispatch_name, enum_type)>([&] { \
71-
AT_ERROR( \
72-
"dtype '", \
73-
toString(enum_type), \
74-
"' not selected for kernel tag ", \
75-
at_dispatch_name); \
76-
})
77-
#endif
7865

7966
// Workaround for C10_UNUSED because CUDA 10.2 and below fails to handle unused
8067
// attribute in the type aliasing context. Keep name long and verbose to avoid

aten/src/ATen/core/boxing/impl/boxing.h

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -226,20 +226,17 @@ struct BoxedKernelWrapper<
226226
torch::jit::Stack stack = boxArgs<Args...>(std::forward<Args>(args)...);
227227
boxed_kernel_func.callBoxed(opHandle, dispatchKeySet, &stack);
228228

229-
return guts::if_constexpr<!std::is_same<void, Result>::value>(
230-
[&] (auto delay_check) {
231-
// op has pushed one or more values onto the stack.
232-
return delay_check(PopResult<Result>::call(stack));
233-
},
234-
[&] {
235-
// op returns void, boxed kernel has pushed nothing onto stack.
236-
TORCH_INTERNAL_ASSERT_DEBUG_ONLY(
237-
stack.size() == 0,
238-
"Boxed kernel was expected to return no values on the stack, ",
239-
"but instead returned ", stack.size(), " values."
240-
);
241-
}
242-
);
229+
if constexpr (!std::is_same_v<void, Result>) {
230+
// op has pushed one or more values onto the stack.
231+
return PopResult<Result>::call(stack);
232+
} else {
233+
// op returns void, boxed kernel has pushed nothing onto stack.
234+
TORCH_INTERNAL_ASSERT_DEBUG_ONLY(
235+
stack.size() == 0,
236+
"Boxed kernel was expected to return no values on the stack, ",
237+
"but instead returned ", stack.size(), " values."
238+
);
239+
}
243240
}
244241
};
245242

aten/src/ATen/core/boxing/impl/make_boxed_from_unboxed_functor.h

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -103,12 +103,12 @@ namespace impl {
103103
template<class T, bool AllowDeprecatedTypes, class Enable = void>
104104
struct assert_is_valid_input_type {
105105
assert_is_valid_input_type() {
106-
guts::if_constexpr<guts::typelist::contains<supported_primitive_arg_types, T>::value>([] {
106+
if constexpr (guts::typelist::contains<supported_primitive_arg_types, T>::value) {
107107
/* everything is ok, this is a primitive type */
108-
}, /* else */ [] {
108+
} else {
109109
/* otherwise this must be an instance of a valid custom class, since it can only
110110
have been created via IValue(x), which ensures this. */
111-
});
111+
};
112112
}
113113
};
114114

@@ -209,12 +209,12 @@ namespace impl {
209209
template<class T, bool AllowDeprecatedTypes, class Enable = void>
210210
struct assert_is_valid_output_type {
211211
assert_is_valid_output_type() {
212-
guts::if_constexpr<guts::typelist::contains<supported_primitive_arg_types, T>::value>([] {
212+
if constexpr (guts::typelist::contains<supported_primitive_arg_types, T>::value) {
213213
/* everything is ok, this is a primitive type */
214-
}, /* else */ [] {
214+
} else {
215215
/* otherwise T is verified to be a registered custom class in the IValue
216216
constructor, so no benefit in double-checking here */
217-
});
217+
}
218218
}
219219
};
220220

@@ -556,34 +556,17 @@ namespace impl {
556556
using ArgTypes = typename c10::remove_DispatchKeySet_arg_from_func<KernelFunctor>::parameter_types;
557557
constexpr bool has_outputs = !std::is_same<void, ReturnType>::value;
558558
constexpr size_t num_inputs = guts::typelist::size<ArgTypes>::value;
559-
#ifdef __cpp_if_constexpr
560559
if constexpr (has_outputs) {
561-
#else
562-
guts::if_constexpr<has_outputs>([&] (auto delay_check) {
563-
#endif
564560
// Decay ReturnType to ReturnType_ so that if a reference gets returned, we actually store it by value
565561
// and don't get a dangling reference. This is only required because some kernels still return `Tensor&`.
566-
#ifdef __cpp_if_constexpr
567562
using ReturnType_ = std::decay_t<ReturnType>;
568563
ReturnType_ output = call_functor_with_args_from_stack<KernelFunctor, AllowDeprecatedTypes>(functor, dispatchKeySet, stack);
569-
#else
570-
using ReturnType_ = std::decay_t<typename decltype(delay_check)::template type_identity<ReturnType>>;
571-
ReturnType_ output = call_functor_with_args_from_stack<KernelFunctor, AllowDeprecatedTypes>(functor, dispatchKeySet, delay_check(stack));
572-
#endif
573564
torch::jit::drop(*stack, num_inputs);
574565
push_outputs<ReturnType_, AllowDeprecatedTypes>::call(std::move(output), stack);
575-
#ifdef __cpp_if_constexpr
576566
} else {
577-
#else
578-
}, /* else */ [&] {
579-
#endif
580567
call_functor_with_args_from_stack<KernelFunctor, AllowDeprecatedTypes>(functor, dispatchKeySet, stack);
581568
torch::jit::drop(*stack, num_inputs);
582-
#ifdef __cpp_if_constexpr
583569
}
584-
#else
585-
});
586-
#endif
587570
}
588571
};
589572
} // namespace impl

aten/src/ATen/core/ivalue_inl.h

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -975,11 +975,9 @@ struct C10_EXPORT ivalue::Future final : c10::intrusive_ptr_target {
975975
*/
976976
template <typename T>
977977
void addCallback(T callback) {
978-
#if __cpp_lib_is_invocable >= 201703
979978
static_assert(
980979
std::is_invocable_r<void, T, Future&>::value,
981980
"The callback must have signature void(Future&)");
982-
#endif
983981
std::unique_lock<std::mutex> lock(mutex_);
984982
if (completed()) {
985983
lock.unlock();
@@ -997,30 +995,26 @@ struct C10_EXPORT ivalue::Future final : c10::intrusive_ptr_target {
997995
template <typename T>
998996
c10::intrusive_ptr<Future> then(T callback, TypePtr type) {
999997
using IValueWithStorages = std::tuple<IValue, std::vector<WeakStorage>>;
1000-
#if __cpp_lib_is_invocable >= 201703
1001998
static_assert(
1002999
guts::disjunction<
10031000
std::is_invocable_r<IValue, T, Future&>,
10041001
std::is_invocable_r<IValueWithStorages, T, Future&>>::value,
10051002
"The callback must have signature IValue(Future&) or "
10061003
"std::tuple<IValue, std::vector<Storage>>(Future&)");
1007-
#endif
10081004
auto childFut = createInstance(std::move(type));
10091005
addCallback([childFut,
10101006
cb = std::move(callback)](Future& parentFut) mutable {
10111007
try {
1012-
guts::if_constexpr<std::is_convertible<
1008+
if constexpr (std::is_convertible_v<
10131009
typename c10::invoke_result_t<T &&, Future&>,
1014-
IValueWithStorages>::value>(
1015-
[&](auto identity) {
1010+
IValueWithStorages>) {
10161011
IValue value;
10171012
std::vector<WeakStorage> storages;
1018-
std::tie(value, storages) = identity(cb)(parentFut);
1013+
std::tie(value, storages) = cb(parentFut);
10191014
childFut->markCompleted(std::move(value), std::move(storages));
1020-
},
1021-
[&](auto identity) {
1022-
childFut->markCompleted(identity(cb)(parentFut));
1023-
});
1015+
} else {
1016+
childFut->markCompleted(cb(parentFut));
1017+
};
10241018
} catch (std::exception&) {
10251019
childFut->setError(std::current_exception());
10261020
}
@@ -1030,11 +1024,9 @@ struct C10_EXPORT ivalue::Future final : c10::intrusive_ptr_target {
10301024

10311025
template <typename T>
10321026
c10::intrusive_ptr<Future> thenAsync(T callback, TypePtr type) {
1033-
#if __cpp_lib_is_invocable >= 201703
10341027
static_assert(
10351028
std::is_invocable_r<c10::intrusive_ptr<Future>, T, Future&>::value,
10361029
"The callback must have signature c10::intrusive_ptr<Future>(Future&)");
1037-
#endif
10381030
auto childFut = createInstance(std::move(type));
10391031
addCallback(
10401032
[childFut, cb = std::move(callback)](Future& parentFut) mutable {
@@ -1111,11 +1103,9 @@ struct C10_EXPORT ivalue::Future final : c10::intrusive_ptr_target {
11111103
// synchronize them with the value, and so on (if needed).
11121104
template<typename T>
11131105
void invokeCallback(T callback) {
1114-
#if __cpp_lib_is_invocable >= 201703
11151106
static_assert(
11161107
std::is_invocable_r<void, T, Future&>::value,
11171108
"The callback must have signature void(Future&)");
1118-
#endif
11191109

11201110
c10::OptionalDeviceGuard deviceGuard(currentDevice_);
11211111

aten/src/ATen/mps/MPSGuardImpl.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -109,12 +109,12 @@ struct TORCH_API MPSGuardImpl final : public c10::impl::DeviceGuardImplInterface
109109
struct OptionalMPSGuard {
110110
explicit OptionalMPSGuard() : guard_() {}
111111

112-
explicit OptionalMPSGuard(optional<Device> device_opt)
112+
explicit OptionalMPSGuard(c10::optional<Device> device_opt)
113113
: guard_(device_opt) {}
114114

115115
/// Set the current MPS device to the passed device index, if it is not
116116
/// nullopt
117-
explicit OptionalMPSGuard(optional<DeviceIndex> device_index_opt)
117+
explicit OptionalMPSGuard(c10::optional<DeviceIndex> device_index_opt)
118118
: guard_(device_index_opt) {}
119119

120120
// Copy is not allowed
@@ -144,14 +144,14 @@ struct OptionalMPSGuard {
144144

145145
/// Returns the device that was set immediately prior to initialization of the
146146
/// guard, or nullopt if the guard is uninitialized.
147-
optional<Device> original_device() const {
147+
c10::optional<Device> original_device() const {
148148
return guard_.original_device();
149149
}
150150

151151
/// Returns the most recent device that was set using this device guard,
152152
/// either from construction, or via set_device, if the guard is initialized,
153153
/// or nullopt if the guard is uninitialized.
154-
optional<Device> current_device() const {
154+
c10::optional<Device> current_device() const {
155155
return guard_.current_device();
156156
}
157157

aten/src/ATen/native/ReduceOpsUtils.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ static inline void check_scalar_type_device_layout_equal(const Tensor& out, cons
102102
OPTION_TYPE_EQUALITY_CHECK(layout, out.options(), self.options());
103103
}
104104

105-
static inline Tensor integer_upcast(const Tensor& self, optional<ScalarType> dtype) {
105+
static inline Tensor integer_upcast(const Tensor& self, c10::optional<ScalarType> dtype) {
106106
ScalarType scalarType = self.scalar_type();
107107
ScalarType upcast_scalarType = dtype.value_or(at::isIntegralType(scalarType, /*includeBool=*/true) ? ScalarType::Long : scalarType);
108108
return self.toType(upcast_scalarType);

aten/src/ATen/native/cuda/jit_utils.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1531,7 +1531,7 @@ NvrtcFunction jit_pwise_function(
15311531
&program, code.c_str(), nullptr, 0, nullptr, nullptr));
15321532

15331533
#ifdef USE_ROCM
1534-
std::vector<const char*> args = {"--std=c++14"};
1534+
std::vector<const char*> args = {"--std=c++17"};
15351535
#else
15361536
// Constructs nvrtc build arguments
15371537
// CUDA 11.1 allows going directly to SASS (sm_) instead of PTX (compute_)
@@ -1546,7 +1546,7 @@ NvrtcFunction jit_pwise_function(
15461546
std::to_string(cuda_minor);
15471547
// NOLINTNEXTLINE(cppcoreguidelines-init-variables)
15481548
std::vector<const char*> args = {
1549-
"--std=c++14", compute.c_str(), "-default-device"};
1549+
"--std=c++17", compute.c_str(), "-default-device"};
15501550
#endif
15511551

15521552
#ifndef NDEBUG

aten/src/ATen/native/mps/operations/Distributions.mm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,7 @@ static void check_from_to_in_range(int64_t from, int64_t to_inc, ScalarType scal
388388
Tensor& random_mps_
389389
(Tensor& self,
390390
int64_t from,
391-
optional<int64_t> to_opt,
391+
c10::optional<int64_t> to_opt,
392392
c10::optional<Generator> gen) {
393393

394394
using namespace mps;

aten/src/ATen/native/mps/operations/ReduceOps.mm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ void set_axes_and_shapes(const Tensor& input_t,
301301
// Taken from ReduceOps.cpp
302302
inline ScalarType get_dtype_from_self(
303303
const Tensor& self,
304-
const optional<ScalarType>& dtype,
304+
const c10::optional<ScalarType>& dtype,
305305
bool promote_integers) {
306306
if (dtype.has_value()) {
307307
return dtype.value();

0 commit comments

Comments
 (0)