Skip to content

Commit deef069

Browse files
authored
Sync C10 headers with updated PyTorch pin (pytorch#23126)
### Summary Advances the pinned PyTorch revision from `ea89e4e90dc68302e8ef5cba3ddbdaa9d50d9512` to `2bb2a9c522e7f944bf98c02c9b85fd959fa0bf0b` and synchronizes the two vendored C10 headers changed by that range. Keeping the pin and headers in one change prevents `check-c10-sync` from observing mismatched revisions. Authored with assistance from OpenAI Codex. ### Test plan `.ci/scripts/check_c10_sync.sh` `lintrunner -a .ci/docker/ci_commit_pins/pytorch.txt runtime/core/portable_type/c10/torch/headeronly/macros/Macros.h runtime/core/portable_type/c10/torch/headeronly/util/overflows.h` cc @digantdesai @freddan80 @per @zingo @oscarandersson8218 @mansnils @Sebastian-Larsson @robell @rascani
1 parent 01c4cfa commit deef069

4 files changed

Lines changed: 102 additions & 36 deletions

File tree

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
ea89e4e90dc68302e8ef5cba3ddbdaa9d50d9512
1+
2bb2a9c522e7f944bf98c02c9b85fd959fa0bf0b

‎backends/arm/test/models/test_torch_functions.py‎

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -94,14 +94,7 @@ def forward(self, *args):
9494
test_parameters = {test[0]: test[1:] for test in module_tests}
9595

9696

97-
@parametrize(
98-
"test_data",
99-
test_parameters,
100-
xfails={
101-
"topk": "NotImplementedError: No registered serialization name for <class 'torch.return_types.topk'> found",
102-
"sort": "NotImplementedError: No registered serialization name for <class 'torch.return_types.sort'> found",
103-
},
104-
)
97+
@parametrize("test_data", test_parameters)
10598
def test_torch_functions_tosa_FP(test_data):
10699
module, inputs = test_data
107100
pipeline = TosaPipelineFP[input_t](
@@ -119,15 +112,7 @@ def test_torch_functions_tosa_FP(test_data):
119112
raise e
120113

121114

122-
@parametrize(
123-
"test_data",
124-
test_parameters,
125-
xfails={
126-
"topk": "NotImplementedError: No registered serialization name for <class 'torch.return_types.topk'> found",
127-
"sort": "NotImplementedError: No registered serialization name for <class 'torch.return_types.sort'> found",
128-
},
129-
strict=True,
130-
)
115+
@parametrize("test_data", test_parameters)
131116
def test_torch_functions_tosa_INT(test_data):
132117
module, inputs = test_data
133118
pipeline = TosaPipelineINT[input_t](

‎runtime/core/portable_type/c10/torch/headeronly/macros/Macros.h‎

Lines changed: 82 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -471,14 +471,15 @@ __host__ __device__
471471
(void)(printf( \
472472
"[CUDA_KERNEL_ASSERT] " __FILE__ ":" C10_STRINGIZE( \
473473
__LINE__) ": %s: block: [%d,%d,%d], thread: [%d,%d,%d]: " \
474-
"Assertion failed: `" #cond "`: " msg "\n", \
474+
"Assertion failed: `%s`: " msg "\n", \
475475
__func__, \
476476
blockIdx.x, \
477477
blockIdx.y, \
478478
blockIdx.z, \
479479
threadIdx.x, \
480480
threadIdx.y, \
481481
threadIdx.z, \
482+
#cond, \
482483
##__VA_ARGS__)); \
483484
(void)(_wassert( \
484485
_CRT_WIDE(#cond), \
@@ -554,11 +555,89 @@ __host__ __device__
554555
abort(); \
555556
}
556557
#else
558+
#if defined(USE_ROCM) && defined(__HIP_DEVICE_COMPILE__)
559+
namespace torch::headeronly::detail {
560+
// Compile-time string fragment for CUDA_KERNEL_ASSERT message pieces.
561+
template <unsigned N>
562+
struct RoCmAssertLit {
563+
char data[N]{};
564+
constexpr RoCmAssertLit(const char (&s)[N]) {
565+
for (unsigned i = 0; i < N; ++i) {
566+
data[i] = s[i];
567+
}
568+
}
569+
static constexpr unsigned size() {
570+
return N;
571+
}
572+
constexpr unsigned percent() const {
573+
unsigned count = 0;
574+
for (unsigned i = 0; i < N - 1; ++i) {
575+
if (data[i] == '%') {
576+
++count;
577+
}
578+
}
579+
return count;
580+
}
581+
};
582+
583+
// OCKL is_last=1 treats the append as printf format; escape '%'. Size the
584+
// buffer as Out + NumPercent.
585+
template <RoCmAssertLit A, RoCmAssertLit B, RoCmAssertLit C>
586+
constexpr auto rocm_assert_concat() {
587+
constexpr unsigned out = A.size() + B.size() + C.size() - 2;
588+
constexpr unsigned pct = A.percent() + B.percent() + C.percent();
589+
struct {
590+
char data[out + pct];
591+
unsigned length;
592+
} msg{};
593+
unsigned j = 0;
594+
auto append = [&](const char* s, unsigned n) {
595+
for (unsigned k = 0; k < n; ++k) {
596+
if (s[k] == '%') {
597+
msg.data[j++] = '%';
598+
}
599+
msg.data[j++] = s[k];
600+
}
601+
};
602+
append(A.data, A.size() - 1);
603+
append(B.data, B.size() - 1);
604+
append(C.data, C.size());
605+
msg.length = j;
606+
return msg;
607+
}
608+
609+
// __host__ __device__: hipcc's device pass still type-checks host-only call
610+
// sites (e.g. MemoryAccess.cuh LoadWithCast ctors). Host runtime uses #else
611+
// __assert_fail; this body is not emitted for host codegen.
612+
template <unsigned N>
613+
__host__ __device__ inline __attribute__((always_inline, flatten)) void
614+
rocm_assert_one_shot(const char (&msg)[N], unsigned length) {
615+
auto d = __ockl_fprintf_stderr_begin();
616+
__ockl_fprintf_append_string_n(d, msg, length, 1);
617+
__builtin_trap();
618+
}
619+
} // namespace torch::headeronly::detail
620+
621+
#define ROCM_ASSERT_LIT(s) ::torch::headeronly::detail::RoCmAssertLit(s)
622+
623+
#define CUDA_KERNEL_ASSERT(cond) \
624+
if C10_UNLIKELY (!(cond)) { \
625+
static constexpr auto _rocm_assert_msg = \
626+
::torch::headeronly::detail::rocm_assert_concat< \
627+
ROCM_ASSERT_LIT(__FILE__ ":" C10_STRINGIZE(__LINE__) ": "), \
628+
ROCM_ASSERT_LIT(__func__), \
629+
ROCM_ASSERT_LIT(": Device-side assertion `" #cond \
630+
"' failed.\n")>(); \
631+
::torch::headeronly::detail::rocm_assert_one_shot( \
632+
_rocm_assert_msg.data, _rocm_assert_msg.length); \
633+
}
634+
#else
557635
#define CUDA_KERNEL_ASSERT(cond) \
558636
if (C10_UNLIKELY(!(cond))) { \
559637
__assert_fail( \
560638
#cond, __FILE__, static_cast<unsigned int>(__LINE__), __func__); \
561639
}
640+
#endif // defined(USE_ROCM) && defined(__HIP_DEVICE_COMPILE__)
562641
#define CUDA_KERNEL_ASSERT_MSG(cond, msg) \
563642
if (C10_UNLIKELY(!(cond))) { \
564643
__assert_fail( \
@@ -569,14 +648,15 @@ __host__ __device__
569648
printf( \
570649
"[CUDA_KERNEL_ASSERT] " __FILE__ ":" C10_STRINGIZE( \
571650
__LINE__) ": %s: block: [%d,%d,%d], thread: [%d,%d,%d]: " \
572-
"Assertion failed: `" #cond "`: " msg "\n", \
651+
"Assertion failed: `%s`: " msg "\n", \
573652
__func__, \
574653
blockIdx.x, \
575654
blockIdx.y, \
576655
blockIdx.z, \
577656
threadIdx.x, \
578657
threadIdx.y, \
579658
threadIdx.z, \
659+
#cond, \
580660
##__VA_ARGS__); \
581661
__assert_fail( \
582662
#cond, __FILE__, static_cast<unsigned int>(__LINE__), __func__); \

‎runtime/core/portable_type/c10/torch/headeronly/util/overflows.h‎

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
#include <limits>
99
#include <type_traits>
1010

11-
HIDDEN_NAMESPACE_BEGIN(torch, headeronly)
11+
namespace c10 {
1212
// In some versions of MSVC, there will be a compiler error when building.
1313
// C4146: unary minus operator applied to unsigned type, result still unsigned
1414
// C4804: unsafe use of type 'bool' in operation
@@ -43,25 +43,26 @@ std::enable_if_t<std::is_same_v<From, bool>, bool> overflows(
4343
template <typename To, typename From>
4444
std::enable_if_t<std::is_integral_v<From> && !std::is_same_v<From, bool>, bool>
4545
overflows(From f, bool strict_unsigned = false) {
46-
using limit = std::numeric_limits<typename scalar_value_type<To>::type>;
46+
using limit = std::numeric_limits<
47+
typename torch::headeronly::scalar_value_type<To>::type>;
4748
if constexpr (!limit::is_signed && std::numeric_limits<From>::is_signed) {
4849
// allow for negative numbers to wrap using two's complement arithmetic.
4950
// For example, with uint8, this allows for `a - b` to be treated as
5051
// `a + 255 * b`.
5152
if (!strict_unsigned) {
52-
return greater_than_max<To>(f) ||
53-
(is_negative(f) &&
53+
return c10::greater_than_max<To>(f) ||
54+
(c10::is_negative(f) &&
5455
-static_cast<uint64_t>(f) > static_cast<uint64_t>(limit::max()));
5556
}
5657
}
57-
return less_than_lowest<To>(f) || greater_than_max<To>(f);
58+
return c10::less_than_lowest<To>(f) || c10::greater_than_max<To>(f);
5859
}
5960

6061
template <typename To, typename From>
6162
std::enable_if_t<std::is_floating_point_v<From>, bool> overflows(
6263
From f,
6364
bool strict_unsigned [[maybe_unused]] = false) {
64-
using ToScalar = typename scalar_value_type<To>::type;
65+
using ToScalar = typename torch::headeronly::scalar_value_type<To>::type;
6566
using limit = std::numeric_limits<ToScalar>;
6667
if (limit::has_infinity && std::isinf(static_cast<double>(f))) {
6768
return false;
@@ -93,27 +94,27 @@ C10_CLANG_DIAGNOSTIC_POP()
9394
#endif
9495

9596
template <typename To, typename From>
96-
std::enable_if_t<is_complex<From>::value, bool> overflows(
97+
std::enable_if_t<torch::headeronly::is_complex<From>::value, bool> overflows(
9798
From f,
9899
bool strict_unsigned = false) {
99100
// casts from complex to real are considered to overflow if the
100101
// imaginary component is non-zero
101-
if (!is_complex<To>::value && f.imag() != 0) {
102+
if (!torch::headeronly::is_complex<To>::value && f.imag() != 0) {
102103
return true;
103104
}
104105
// Check for overflow componentwise
105106
// (Technically, the imag overflow check is guaranteed to be false
106107
// when !is_complex<To>, but any optimizer worth its salt will be
107108
// able to figure it out.)
108-
return overflows<
109-
typename scalar_value_type<To>::type,
109+
return c10::overflows<
110+
typename torch::headeronly::scalar_value_type<To>::type,
110111
typename From::value_type>(f.real(), strict_unsigned) ||
111-
overflows<
112-
typename scalar_value_type<To>::type,
112+
c10::overflows<
113+
typename torch::headeronly::scalar_value_type<To>::type,
113114
typename From::value_type>(f.imag(), strict_unsigned);
114115
}
115-
HIDDEN_NAMESPACE_END(torch, headeronly)
116-
117-
namespace c10 {
118-
using torch::headeronly::overflows;
119116
} // namespace c10
117+
118+
HIDDEN_NAMESPACE_BEGIN(torch, headeronly)
119+
using c10::overflows;
120+
HIDDEN_NAMESPACE_END(torch, headeronly)

0 commit comments

Comments
 (0)