Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,10 @@ if(WITH_CAMBRICON)
target_compile_options(infiniops PUBLIC
"$<$<COMPILE_LANGUAGE:CXX>:SHELL:-idirafter /usr/local/neuware/lib/clang/11.1.0/include>"
)
set(_cambricon_host_bang_compat_option
"$<$<COMPILE_LANGUAGE:CXX>:SHELL:-include ${CMAKE_CURRENT_SOURCE_DIR}/native/cambricon/host_bang_compat.h>")
target_compile_options(infiniops PRIVATE
"${_cambricon_host_bang_compat_option}")
endif()

list(APPEND DEVICE_LIST "cambricon")
Expand Down Expand Up @@ -1507,6 +1511,11 @@ if(GENERATE_PYTHON_BINDINGS)
"$<$<COMPILE_LANGUAGE:CXX,CUDA>:-Wno-deprecated-declarations>"
)

if(WITH_CAMBRICON AND CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang")
target_compile_options(ops PRIVATE
"${_cambricon_host_bang_compat_option}")
endif()

if(GENERATE_OPERATOR_CALL_INSTANTIATIONS)
target_compile_definitions(ops PRIVATE
INFINI_OPS_USE_OPERATOR_CALL_INSTANTIATIONS=1)
Expand Down
34 changes: 34 additions & 0 deletions src/native/cambricon/host_bang_compat.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#ifndef INFINI_OPS_NATIVE_CAMBRICON_HOST_BANG_COMPAT_H_
#define INFINI_OPS_NATIVE_CAMBRICON_HOST_BANG_COMPAT_H_

// Neuware 6.0 leaves __BANG_HOSTDEVICE__ empty when its FP16/BF16 headers are
// parsed by a host C++ compiler. Consequently, the function definitions in
// those headers have external linkage and collide across translation units.
// Pre-include each header with the BANG annotations mapped to standard C++
// inline semantics. The vendor include guards then keep later cnrt/cnnl
// includes from emitting the non-inline definitions.
#if !defined(__BANGCC__) && !defined(__CUDACC__)
#pragma push_macro("__BANGCC__")
#pragma push_macro("__mlu_host__")
#pragma push_macro("__mlu_func__")
#define __BANGCC__ 1
#define __mlu_host__
#define __mlu_func__
#include <bang_fp16.h>
#pragma pop_macro("__mlu_func__")
#pragma pop_macro("__mlu_host__")
#pragma pop_macro("__BANGCC__")

#pragma push_macro("__BANGCC__")
#pragma push_macro("__mlu_host__")
#pragma push_macro("__mlu_func__")
#define __BANGCC__ 1
#define __mlu_host__ inline
#define __mlu_func__
#include <bang_bf16.h>
#pragma pop_macro("__mlu_func__")
#pragma pop_macro("__mlu_host__")
#pragma pop_macro("__BANGCC__")
#endif

#endif
16 changes: 16 additions & 0 deletions tests/test_generate_wrappers.py
Original file line number Diff line number Diff line change
Expand Up @@ -1133,3 +1133,19 @@ def test_shared_triton_config_parser_has_one_explicit_schema():

assert "namespace infini::ops::triton::jit" in parser_header
assert "namespace detail" in parser_header


def test_cmake_configures_cambricon_half_compatibility():
root = pathlib.Path(__file__).parents[1]
source_cmake = (root / "src" / "CMakeLists.txt").read_text(encoding="utf-8")
compatibility_header = (
root / "src" / "native" / "cambricon" / "host_bang_compat.h"
).read_text(encoding="utf-8")

assert "native/cambricon/host_bang_compat.h" in source_cmake
assert "target_compile_options(infiniops PRIVATE" in source_cmake
assert "target_compile_options(ops PRIVATE" in source_cmake
assert source_cmake.count("_cambricon_host_bang_compat_option}") == 2
assert "#include <bang_fp16.h>" in compatibility_header
assert "#include <bang_bf16.h>" in compatibility_header
assert "#define __mlu_host__ inline" in compatibility_header
Loading