11#ifndef C10_MACROS_MACROS_H_
22#define C10_MACROS_MACROS_H_
3+
4+ #ifdef __cplusplus
35#include < cassert>
6+ #else
7+ #include < assert.h>
8+ #endif
49
510/* Main entry for torch/headeronly/macros (used to be c10/macros).
611 *
139144
140145#define C10_RESTRICT __restrict
141146
147+ #ifdef __cplusplus
148+
142149// Simply define the namespace, in case a dependent library want to refer to
143150// the c10 namespace but not any nontrivial files.
144151namespace c10 {}
@@ -176,6 +183,8 @@ namespace at::xpu {
176183using namespace c10 ::xpu;
177184} // namespace at::xpu
178185
186+ #endif // __cplusplus
187+
179188// C10_LIKELY/C10_UNLIKELY
180189//
181190// These macros provide parentheses, so you can use these macros as:
@@ -236,7 +245,11 @@ using namespace c10::xpu;
236245
237246#define C10_ERASE C10_ALWAYS_INLINE C10_ATTR_VISIBILITY_HIDDEN
238247
248+ #ifdef __cplusplus
239249#include < cstdint>
250+ #else
251+ #include < stdint.h>
252+ #endif
240253
241254#ifdef __HIPCC__
242255// Unlike CUDA, HIP requires a HIP header to be included for __host__ to work.
@@ -467,7 +480,7 @@ __host__ __device__
467480// a non-negligible performance impact even if the assert condition is
468481// never triggered. We choose to use abort() instead which will still
469482// terminate the application but without a more useful error message.
470- #if !defined(C10_USE_ROCM_KERNEL_ASSERT) and defined(USE_ROCM)
483+ #if !defined(C10_USE_ROCM_KERNEL_ASSERT) && defined(USE_ROCM)
471484#define CUDA_KERNEL_ASSERT (cond ) \
472485 if C10_UNLIKELY (!(cond)) { \
473486 abort (); \
@@ -517,9 +530,21 @@ __host__ __device__
517530 __assert_fail ( \
518531 #cond, __FILE__, static_cast <unsigned int >(__LINE__), __func__); \
519532 }
520- #endif // C10_USE_ROCM_KERNEL_ASSERT and USE_ROCM
533+ #endif // C10_USE_ROCM_KERNEL_ASSERT && USE_ROCM
521534#endif // __APPLE__
522535
536+ // Compile-time switch to control how assertions are logged inside CUDA kernels.
537+ // If C10_CUDA_VERBOSE_ASSERT is defined, CUDA_KERNEL_ASSERT_VERBOSE will
538+ // take addition information passed to the macro and forward them to
539+ // CUDA_KERNEL_ASSERT_PRINTF If C10_CUDA_VERBOSE_ASSERT is not defined,
540+ // CUDA_KERNEL_ASSERT_VERBOSE will behave the same as CUDA_KERNEL_ASSERT.
541+ #ifdef C10_ENABLE_VERBOSE_ASSERT
542+ #define CUDA_KERNEL_ASSERT_VERBOSE (cond, ...) \
543+ CUDA_KERNEL_ASSERT_PRINTF (cond, __VA_ARGS__)
544+ #else
545+ #define CUDA_KERNEL_ASSERT_VERBOSE (cond, ...) CUDA_KERNEL_ASSERT (cond)
546+ #endif
547+
523548#ifdef __APPLE__
524549#include < TargetConditionals.h>
525550#endif
@@ -619,10 +644,7 @@ __host__ __device__
619644// loading custom extensions compiled against different libtorch
620645// versions where these APIs may have changed.
621646
622- // Helper macros for nested namespace expansion
623- #define _EXPAND (...) __VA_ARGS__
624-
625- // Macros to handle 1-3 hidden namespace levels when not windows
647+ // Helper macros to handle 1-3 hidden namespace levels when not windows
626648#define _HIDDEN_NS_GET_MACRO (_1, _2, _3, NAME , ...) NAME
627649#define _HIDDEN_NS_1 (n1 ) namespace n1 __attribute__ ((visibility(" hidden" ))) {
628650#define _HIDDEN_NS_2 (n1, n2 ) \
@@ -632,13 +654,15 @@ __host__ __device__
632654 namespace n1 ::n2 { \
633655 namespace n3 __attribute__ ((visibility(" hidden" ))) {
634656
635- // Macros to close namespaces when not windows
657+ // Helper macros to close namespaces when not windows
636658#define _HIDDEN_NS_END_1 (n1 ) }
637659#define _HIDDEN_NS_END_N (n1, ...) \
638660 } \
639661 }
640662
641- // Macros to join strs with :: (for win, where symbols are hidden by default)
663+ // Helper macros to join strs with :: (for win, where symbols are hidden by
664+ // default)
665+ #define _EXPAND (...) __VA_ARGS__
642666#define _JOIN_GET_MACRO (_1, _2, _3, NAME , ...) NAME
643667#define _JOIN_NS1 (a ) a
644668#define _JOIN_NS2 (a, b ) a::b
@@ -647,8 +671,8 @@ __host__ __device__
647671#if !defined(HIDDEN_NAMESPACE_BEGIN)
648672#if defined(__GNUG__) && !defined(_WIN32)
649673#define HIDDEN_NAMESPACE_BEGIN (...) \
650- _EXPAND ( _HIDDEN_NS_GET_MACRO ( \
651- __VA_ARGS__, _HIDDEN_NS_3, _HIDDEN_NS_2, _HIDDEN_NS_1)(__VA_ARGS__))
674+ _HIDDEN_NS_GET_MACRO ( \
675+ __VA_ARGS__, _HIDDEN_NS_3, _HIDDEN_NS_2, _HIDDEN_NS_1)(__VA_ARGS__)
652676#else
653677#define HIDDEN_NAMESPACE_BEGIN (...) \
654678 namespace _EXPAND ( _JOIN_GET_MACRO ( \
@@ -659,9 +683,9 @@ __host__ __device__
659683#if !defined(HIDDEN_NAMESPACE_END)
660684#if defined(__GNUG__) && !defined(_WIN32)
661685#define HIDDEN_NAMESPACE_END (...) \
662- _EXPAND ( _HIDDEN_NS_GET_MACRO ( \
686+ _HIDDEN_NS_GET_MACRO ( \
663687 __VA_ARGS__, _HIDDEN_NS_END_N, _HIDDEN_NS_END_N, _HIDDEN_NS_END_1)( \
664- __VA_ARGS__))
688+ __VA_ARGS__)
665689#else
666690#define HIDDEN_NAMESPACE_END (...) }
667691#endif
0 commit comments