@@ -421,6 +421,61 @@ static inline int C10_WARP_SIZE_INTERNAL() {
421421#define __func__ __FUNCTION__
422422#endif
423423
424+ // The HIDDEN_NAMESPACE_BEGIN and HIDDEN_NAMESPACE_END below
425+ // are needed for maintaining robustness in our header APIs in
426+ // torch/headeronly and torch/csrc/stable under the namespaces
427+ // torch::headeronly and torch::stable respectively. We enforce
428+ // hidden visibility for these APIs because we want to enable
429+ // loading custom extensions compiled against different libtorch
430+ // versions where these APIs may have changed.
431+
432+ // Helper macros to handle 1-3 hidden namespace levels when not windows
433+ #define _HIDDEN_NS_GET_MACRO (_1, _2, _3, NAME , ...) NAME
434+ #define _HIDDEN_NS_1 (n1 ) namespace n1 __attribute__ ((visibility(" hidden" ))) {
435+ #define _HIDDEN_NS_2 (n1, n2 ) \
436+ namespace n1 { \
437+ namespace n2 __attribute__ ((visibility(" hidden" ))) {
438+ #define _HIDDEN_NS_3 (n1, n2, n3 ) \
439+ namespace n1 ::n2 { \
440+ namespace n3 __attribute__ ((visibility(" hidden" ))) {
441+
442+ // Helper macros to close namespaces when not windows
443+ #define _HIDDEN_NS_END_1 (n1 ) }
444+ #define _HIDDEN_NS_END_N (n1, ...) \
445+ } \
446+ }
447+
448+ // Helper macros to join strs with :: (for win, where symbols are hidden by
449+ // default)
450+ #define _EXPAND (...) __VA_ARGS__
451+ #define _JOIN_GET_MACRO (_1, _2, _3, NAME , ...) NAME
452+ #define _JOIN_NS1 (a ) a
453+ #define _JOIN_NS2 (a, b ) a::b
454+ #define _JOIN_NS3 (a, b, c ) a::b::c
455+
456+ #if !defined(HIDDEN_NAMESPACE_BEGIN)
457+ #if defined(__GNUG__) && !defined(_WIN32)
458+ #define HIDDEN_NAMESPACE_BEGIN (...) \
459+ _HIDDEN_NS_GET_MACRO ( \
460+ __VA_ARGS__, _HIDDEN_NS_3, _HIDDEN_NS_2, _HIDDEN_NS_1)(__VA_ARGS__)
461+ #else
462+ #define HIDDEN_NAMESPACE_BEGIN (...) \
463+ namespace _EXPAND ( _JOIN_GET_MACRO ( \
464+ __VA_ARGS__, _JOIN_NS3, _JOIN_NS2, _JOIN_NS1)(__VA_ARGS__)) {
465+ #endif
466+ #endif
467+
468+ #if !defined(HIDDEN_NAMESPACE_END)
469+ #if defined(__GNUG__) && !defined(_WIN32)
470+ #define HIDDEN_NAMESPACE_END (...) \
471+ _HIDDEN_NS_GET_MACRO ( \
472+ __VA_ARGS__, _HIDDEN_NS_END_N, _HIDDEN_NS_END_N, _HIDDEN_NS_END_1)( \
473+ __VA_ARGS__)
474+ #else
475+ #define HIDDEN_NAMESPACE_END (...) }
476+ #endif
477+ #endif
478+
424479// CUDA_KERNEL_ASSERT checks the assertion
425480// even when NDEBUG is defined. This is useful for important assertions in CUDA
426481// code that would otherwise be suppressed when building Release.
@@ -556,7 +611,7 @@ __host__ __device__
556611 }
557612#else
558613#if defined(USE_ROCM) && defined(__HIP_DEVICE_COMPILE__)
559- namespace torch :: headeronly::detail {
614+ HIDDEN_NAMESPACE_BEGIN ( torch, headeronly, detail)
560615// Compile-time string fragment for CUDA_KERNEL_ASSERT message pieces.
561616template <unsigned N>
562617struct RoCmAssertLit {
@@ -616,7 +671,7 @@ rocm_assert_one_shot(const char (&msg)[N], unsigned length) {
616671 __ockl_fprintf_append_string_n (d, msg, length, 1 );
617672 __builtin_trap ();
618673}
619- } // namespace torch:: headeronly:: detail
674+ HIDDEN_NAMESPACE_END ( torch, headeronly, detail)
620675
621676#define ROCM_ASSERT_LIT (s ) ::torch::headeronly::detail::RoCmAssertLit(s)
622677
@@ -772,59 +827,4 @@ rocm_assert_one_shot(const char (&msg)[N], unsigned length) {
772827#define C10_RETURN_MOVE_IF_OLD_COMPILER 0
773828#endif
774829
775- // The HIDDEN_NAMESPACE_BEGIN and HIDDEN_NAMESPACE_END below
776- // are needed for maintaining robustness in our header APIs in
777- // torch/headeronly and torch/csrc/stable under the namespaces
778- // torch::headeronly and torch::stable respectively. We enforce
779- // hidden visibility for these APIs because we want to enable
780- // loading custom extensions compiled against different libtorch
781- // versions where these APIs may have changed.
782-
783- // Helper macros to handle 1-3 hidden namespace levels when not windows
784- #define _HIDDEN_NS_GET_MACRO (_1, _2, _3, NAME , ...) NAME
785- #define _HIDDEN_NS_1 (n1 ) namespace n1 __attribute__ ((visibility(" hidden" ))) {
786- #define _HIDDEN_NS_2 (n1, n2 ) \
787- namespace n1 { \
788- namespace n2 __attribute__ ((visibility(" hidden" ))) {
789- #define _HIDDEN_NS_3 (n1, n2, n3 ) \
790- namespace n1 ::n2 { \
791- namespace n3 __attribute__ ((visibility(" hidden" ))) {
792-
793- // Helper macros to close namespaces when not windows
794- #define _HIDDEN_NS_END_1 (n1 ) }
795- #define _HIDDEN_NS_END_N (n1, ...) \
796- } \
797- }
798-
799- // Helper macros to join strs with :: (for win, where symbols are hidden by
800- // default)
801- #define _EXPAND (...) __VA_ARGS__
802- #define _JOIN_GET_MACRO (_1, _2, _3, NAME , ...) NAME
803- #define _JOIN_NS1 (a ) a
804- #define _JOIN_NS2 (a, b ) a::b
805- #define _JOIN_NS3 (a, b, c ) a::b::c
806-
807- #if !defined(HIDDEN_NAMESPACE_BEGIN)
808- #if defined(__GNUG__) && !defined(_WIN32)
809- #define HIDDEN_NAMESPACE_BEGIN (...) \
810- _HIDDEN_NS_GET_MACRO ( \
811- __VA_ARGS__, _HIDDEN_NS_3, _HIDDEN_NS_2, _HIDDEN_NS_1)(__VA_ARGS__)
812- #else
813- #define HIDDEN_NAMESPACE_BEGIN (...) \
814- namespace _EXPAND ( _JOIN_GET_MACRO ( \
815- __VA_ARGS__, _JOIN_NS3, _JOIN_NS2, _JOIN_NS1)(__VA_ARGS__)) {
816- #endif
817- #endif
818-
819- #if !defined(HIDDEN_NAMESPACE_END)
820- #if defined(__GNUG__) && !defined(_WIN32)
821- #define HIDDEN_NAMESPACE_END (...) \
822- _HIDDEN_NS_GET_MACRO ( \
823- __VA_ARGS__, _HIDDEN_NS_END_N, _HIDDEN_NS_END_N, _HIDDEN_NS_END_1)( \
824- __VA_ARGS__)
825- #else
826- #define HIDDEN_NAMESPACE_END (...) }
827- #endif
828- #endif
829-
830830#endif // C10_MACROS_MACROS_H_
0 commit comments