Skip to content

Commit f09a3cd

Browse files
authored
Merge/ascend dev0824 to main0824 (#41)
* add ascend build files * fix torch and torch_npu deps * fix(ascend): add missing cache_store_interface and stack_tracer deps * feat(ascend): add Ascend NPU support in ExecOps and BUILD deps * fix(ascend): add ops stub for ascend to avoid compiling cuda ops * feat(ascend): Phase 1 (memory & device management), add Ascend NPU support on different components * feat(ascend): Phase 2 (operator adaption) add Ascend support * refactor LinearFactory: move NV cutlass into cuda directory * feat(ascend): Phase 3-4 add Ascend support(attention and KV cache) * fix(ascend): fix the parameters of Attention * fix(ascend): attention prepare actions * fix(ascend): fix build problems * fix(ascend): 为AscendDecodeAttnOp补充缺失的block_size参数 * Fix crc instruction compile error. * Fix std::regex memory error at runtime. * fix runtime bugs * fix py310 path * fix C++ symbol bug * fix: KV out of range, KV invalid format * feat: add simplified fusedCopy * fix compile problems * fix: replace _npu_paged_attention with FIA for Ascend decode attention * feat/fix: delete ascend_prefill debug info * Revert "Pr/clean debug info" * ascend_prefill.py * fix(ascend-decode): fix off-by-one context_lens precision bug * AscendAttn: Fix prefill/decode actual_seq_kv & context_lens compute bug
1 parent 4b8d39c commit f09a3cd

136 files changed

Lines changed: 5058 additions & 236 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.bazelrc

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
common --experimental_downloader_config=bazel/bazel_downloader.cfg
22
fetch:downloader --experimental_remote_downloader=grpc://com.taobao.search.bazel.server:9092 --remote_cache=grpc://com.taobao.search.bazel.server:9092
33

4-
54
build --python_top=//:python310 --incompatible_use_python_toolchains=false # force use /opt/conda310/bin/python3
65

76
build --spawn_strategy=local # avoid nvcc conflicts
@@ -388,6 +387,39 @@ build:rocm --copt="-DFORCE_NVSHMEM_API=1"
388387
build:rocm --copt="-DFLASHINFER_CUB_SUBTRACTLEFT_DEFINED" # ROCm 7.2 hipcub uses SubtractLeft instead of FlagHeads
389388
test:rocm --test_env="/opt/rocm/lib/:/opt/taobao/java/jre/lib/amd64/server/:/opt/amdgpu/lib64/"
390389

390+
# ==================== Ascend NPU ====================
391+
build:ascend --copt="-DENABLE_BF16=1"
392+
build:ascend --action_env TF_NEED_CUDA="0"
393+
build:ascend --host_action_env TF_NEED_CUDA="0"
394+
build:ascend --crosstool_top=@bazel_tools//tools/cpp:toolchain
395+
build:ascend --host_crosstool_top=@bazel_tools//tools/cpp:toolchain
396+
build:ascend --define=using_cuda=false --define=using_cuda_nvcc=false
397+
build:ascend --define=using_ascend=true
398+
build:ascend --action_env TF_NEED_ASCEND=1
399+
build:ascend --host_action_env TF_NEED_ASCEND=1
400+
build:ascend --action_env ASCEND_TOOLKIT_PATH="/usr/local/Ascend/ascend-toolkit/latest"
401+
build:ascend --host_action_env ASCEND_TOOLKIT_PATH="/usr/local/Ascend/ascend-toolkit/latest"
402+
build:ascend --copt="-DUSING_CUDA=0"
403+
build:ascend --copt="-DUSING_ASCEND=1"
404+
build:ascend --copt="-D_GLIBCXX_USE_CXX11_ABI=1"
405+
build:ascend --linkopt="-L/usr/local/Ascend/ascend-toolkit/latest/lib64"
406+
build:ascend --linkopt="-lascendcl"
407+
build:ascend --linkopt="-lhccl"
408+
build:ascend --copt=-Wno-deprecated-declarations
409+
build:ascend --copt="-Wno-error=unused-result"
410+
build:ascend --copt="-Wno-error=array-bounds"
411+
build:ascend --copt=-Wno-tautological-compare
412+
build:ascend --copt=-Wno-array-parameter
413+
build:ascend --copt=-Wno-unused-variable
414+
build:ascend --action_env LD_LIBRARY_PATH="/usr/local/Ascend/ascend-toolkit/latest/lib64:/usr/local/Ascend/driver/lib64:/usr/local/Ascend/add-ons/"
415+
build:ascend --host_action_env LD_LIBRARY_PATH="/usr/local/Ascend/ascend-toolkit/latest/lib64:/usr/local/Ascend/driver/lib64:/usr/local/Ascend/add-ons/"
416+
417+
test:ascend --test_env LD_LIBRARY_PATH="/usr/local/Ascend/ascend-toolkit/latest/lib64:/usr/local/Ascend/driver/lib64:/usr/local/Ascend/driver/lib64/driver/:/usr/local/Ascend/add-ons/:bazel-bin/3rdparty/aclnn_custom_ops/lib:/root/miniconda3/envs/py310/lib/python3.10/site-packages/torch/lib:/root/miniconda3/envs/py310/lib/python3.10/site-packages/torch_npu/lib:/opt/conda310/lib"
418+
test:ascend --test_env PYTHONHOME="/root/miniconda3/envs/py310"
419+
test:ascend --test_env TORCH_DEVICE_BACKEND_AUTOLOAD="0"
420+
test:ascend --test_env TEST_USING_DEVICE="ASCEND"
421+
test:ascend --test_env ASCEND_CUSTOM_OPP_PATH="bazel-bin/3rdparty/aclnn_custom_ops/opp"
422+
391423
build:arm --copt="-DENABLE_BF16=1"
392424
build:arm --define=using_cuda=false --define=using_cuda_nvcc=false
393425
build:arm --define=using_arm=true

3rdparty/aclnn_custom_ops/BUILD

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package(default_visibility = ["//visibility:public"])
2+
3+
genrule(
4+
name = "build_aclnn_ops",
5+
srcs = ["@aclnn_custom_ops_src//:all_srcs"],
6+
outs = [
7+
"lib/libcust_opapi.so",
8+
"lib/libcust_opsproto_rt2.0.so",
9+
"lib/libcust_opmaster_rt2.0.so",
10+
"opp/op_impl/ai_core/tbe/op_tiling/liboptiling.so",
11+
],
12+
cmd = "bash $(location @aclnn_custom_ops_src//:build_for_bazel.sh) " +
13+
"$(RULEDIR) 'apply_top_k_top_p_custom' ascend950",
14+
tools = ["@aclnn_custom_ops_src//:build_for_bazel.sh"],
15+
target_compatible_with = select({
16+
"@//:using_ascend": [],
17+
"//conditions:default": ["@platforms//:incompatible"],
18+
}),
19+
tags = ["local"],
20+
)
21+
22+
cc_library(
23+
name = "aclnn_custom_ops",
24+
data = [
25+
"lib/libcust_opapi.so",
26+
"lib/libcust_opsproto_rt2.0.so",
27+
"lib/libcust_opmaster_rt2.0.so",
28+
"opp/op_impl/ai_core/tbe/op_tiling/liboptiling.so",
29+
],
30+
deps = [
31+
":build_aclnn_ops",
32+
"@local_config_ascend//ascend:ascend_headers",
33+
],
34+
)

3rdparty/gpus/ascend_configure.bzl

Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
"""Repository rule for Ascend CANN autoconfiguration.
2+
3+
`ascend_configure` depends on the following environment variables:
4+
5+
* `TF_NEED_ASCEND`: Whether to enable building with Ascend CANN.
6+
* `ASCEND_TOOLKIT_PATH`: The path to the Ascend CANN toolkit. Default is
7+
`/usr/local/Ascend/ascend-toolkit/latest`.
8+
* `TF_ASCEND_VERSION`: The version of the CANN toolkit.
9+
"""
10+
11+
12+
13+
_ASCEND_TOOLKIT_PATH = "ASCEND_TOOLKIT_PATH"
14+
_TF_ASCEND_VERSION = "TF_ASCEND_VERSION"
15+
_TF_NEED_ASCEND = "TF_NEED_ASCEND"
16+
_DEFAULT_ASCEND_TOOLKIT_PATH = "/usr/local/Ascend/ascend-toolkit/latest"
17+
18+
def _ascend_autoconf_impl(repository_ctx):
19+
"""Implementation of the ascend_configure repository rule."""
20+
21+
# 1. Check TF_NEED_ASCEND environment variable
22+
tf_need_ascend = repository_ctx.os.environ.get(_TF_NEED_ASCEND, "0")
23+
if tf_need_ascend != "1":
24+
_create_dummy_repo(repository_ctx)
25+
return
26+
27+
# 2. Determine CANN installation path
28+
ascend_toolkit_path = repository_ctx.os.environ.get(
29+
_ASCEND_TOOLKIT_PATH, _DEFAULT_ASCEND_TOOLKIT_PATH
30+
)
31+
32+
# 3. Probe CANN headers and libraries
33+
include_path = ascend_toolkit_path + "/include"
34+
lib_path = ascend_toolkit_path + "/lib64"
35+
36+
_check_file_exists(repository_ctx, include_path + "/acl/acl.h", "CANN header")
37+
_check_file_exists(repository_ctx, lib_path + "/libascendcl.so", "CANN library")
38+
39+
# 4. Get version number
40+
ascend_version = repository_ctx.os.environ.get(_TF_ASCEND_VERSION, "")
41+
42+
# 5. Generate template files
43+
_ascend_create(repository_ctx, ascend_toolkit_path, include_path,
44+
lib_path, ascend_version)
45+
46+
47+
def _check_file_exists(repository_ctx, path, label):
48+
if not repository_ctx.path(path).exists:
49+
fail("Cannot find %s at %s" % (label, path))
50+
51+
52+
def _create_dummy_repo(repository_ctx):
53+
"""Create empty BUILD and build_defs.bzl when Ascend is not configured."""
54+
repository_ctx.file("ascend/BUILD", """
55+
package(default_visibility = ["//visibility:public"])
56+
config_setting(name = "using_ascend", values = {"define": "using_ascend=true"})
57+
""")
58+
repository_ctx.file("ascend/build_defs.bzl", """
59+
def if_ascend(if_true, if_false = []):
60+
return select({
61+
"@local_config_ascend//ascend:using_ascend": if_true,
62+
"//conditions:default": if_false,
63+
})
64+
65+
def ascend_default_copts():
66+
return if_ascend([])
67+
68+
def ascend_is_configured():
69+
return False
70+
""")
71+
72+
73+
def _ascend_create(repository_ctx, toolkit_path, include_path, lib_path, version):
74+
"""Generate the @local_config_ascend repository from CANN installation."""
75+
76+
# Symlink headers so they exist at analysis time (glob evaluation time)
77+
repository_ctx.symlink(repository_ctx.path(include_path), "ascend/include")
78+
79+
# Symlink library files
80+
repository_ctx.execute(["mkdir", "-p", "ascend/lib"])
81+
lib_files = {
82+
"libascendcl.so": "ascend",
83+
"libhccl.so": "hccl",
84+
}
85+
for lib_name, target_name in lib_files.items():
86+
src = repository_ctx.path(lib_path + "/" + lib_name)
87+
if src.exists:
88+
repository_ctx.symlink(src, "ascend/lib/" + lib_name)
89+
90+
# --- Generate BUILD content ---
91+
build_content = """package(default_visibility = ["//visibility:public"])
92+
93+
config_setting(
94+
name = "using_ascend",
95+
values = {"define": "using_ascend=true"},
96+
)
97+
98+
cc_library(
99+
name = "ascend_headers",
100+
hdrs = glob(["include/**/*.h"]),
101+
includes = [".", "include"],
102+
visibility = ["//visibility:public"],
103+
)
104+
105+
cc_library(
106+
name = "ascend",
107+
srcs = ["lib/libascendcl.so"],
108+
data = ["lib/libascendcl.so"],
109+
includes = [".", "include"],
110+
linkstatic = 1,
111+
visibility = ["//visibility:public"],
112+
)
113+
114+
cc_library(
115+
name = "hccl",
116+
srcs = ["lib/libhccl.so"],
117+
data = ["lib/libhccl.so"],
118+
includes = [".", "include"],
119+
linkstatic = 1,
120+
visibility = ["//visibility:public"],
121+
)
122+
"""
123+
124+
repository_ctx.file("ascend/BUILD", build_content)
125+
126+
# --- Generate build_defs.bzl ---
127+
repository_ctx.file("ascend/build_defs.bzl", """def if_ascend(if_true, if_false = []):
128+
return select({
129+
"@local_config_ascend//ascend:using_ascend": if_true,
130+
"//conditions:default": if_false,
131+
})
132+
133+
def ascend_default_copts():
134+
return if_ascend([])
135+
136+
def ascend_is_configured():
137+
return True
138+
""")
139+
140+
# --- Generate ascend_config.h ---
141+
repository_ctx.file("ascend/ascend_config.h", """#ifndef ASCEND_ASCEND_CONFIG_H_
142+
#define ASCEND_ASCEND_CONFIG_H_
143+
#define TF_ASCEND_TOOLKIT_PATH "%s"
144+
#define TF_ASCEND_VERSION "%s"
145+
#endif
146+
""" % (toolkit_path, version))
147+
148+
149+
ascend_configure = repository_rule(
150+
implementation = _ascend_autoconf_impl,
151+
environ = [
152+
_TF_NEED_ASCEND,
153+
_ASCEND_TOOLKIT_PATH,
154+
_TF_ASCEND_VERSION,
155+
],
156+
)

BUILD

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,12 @@ config_setting(
8787
values = {"define": "using_rocm=true"},
8888
)
8989

90+
config_setting(
91+
name = "using_ascend",
92+
values = {"define": "using_ascend=true"},
93+
)
94+
95+
9096
config_setting(
9197
name = "rocm_gfx950",
9298
define_values = {
@@ -143,6 +149,7 @@ cc_binary(
143149
copts = copts(),
144150
linkopts = [
145151
"-Wl,-rpath='$$ORIGIN'",
152+
"-Wl,-Bsymbolic-functions", # Fox CXX abi mismatch issue, import torch_npu would link several CANN libraries, which include library with CXXabi=1 and CXXabi=0, and will cause std::regex memory issue. This linking option will cause the program to use internal symbols at runtime.
146153
],
147154
linkshared = 1,
148155
visibility = ["//visibility:public"],

BUILD.torch_npu

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
cc_library(
2+
name = "torch_npu",
3+
srcs = glob([
4+
"torch_npu.libs/libtorch_npu*.so*",
5+
"torch_npu/lib/libtorch_npu*.so*",
6+
]),
7+
hdrs = glob([
8+
"torch_npu/include/**/*.h",
9+
"torch_npu/include/**/*.hpp",
10+
]),
11+
deps = [
12+
"@local_config_python//:python_headers",
13+
"@torch_cpu_ascend//:torch",
14+
],
15+
strip_include_prefix = "torch_npu/include",
16+
visibility = ["//visibility:public"],
17+
)
18+
19+
cc_library(
20+
name = "torch_npu_api",
21+
hdrs = glob([
22+
"torch_npu/include/**/*.h",
23+
"torch_npu/include/**/*.hpp",
24+
]),
25+
deps = [
26+
"@local_config_python//:python_headers",
27+
],
28+
strip_include_prefix = "torch_npu/include",
29+
visibility = ["//visibility:public"],
30+
)

WORKSPACE

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,16 @@ workspace(name = "rtp_llm")
22

33
load("//3rdparty/cuda_config:cuda_configure.bzl", "cuda_configure")
44
load("//3rdparty/gpus:rocm_configure.bzl", "rocm_configure")
5+
load("//3rdparty/gpus:ascend_configure.bzl", "ascend_configure")
56
load("//3rdparty/py:python_configure.bzl", "python_configure")
7+
load("@bazel_tools//tools/build_defs/repo:git.bzl", "git_repository")
68

79
cuda_configure(name = "local_config_cuda")
810

911
rocm_configure(name = "local_config_rocm")
1012

13+
ascend_configure(name = "local_config_ascend")
14+
1115
python_configure(name = "local_config_python")
1216

1317
local_repository(
@@ -20,6 +24,12 @@ local_repository(
2024
path = "arch_config",
2125
)
2226

27+
git_repository(
28+
name = "aclnn_custom_ops_src",
29+
remote = "https://gitcode.com/skywang2/rtp-llm-AscendC.git",
30+
commit = "6af5e08",
31+
)
32+
2333
load("@rtp_deps//:http.bzl", "http_deps")
2434

2535
http_deps()
@@ -67,5 +77,8 @@ pip_cuda13_arm_torch_install_deps()
6777
load("@pip_gpu_rocm_torch//:requirements.bzl", pip_gpu_rocm_torch_install_deps = "install_deps")
6878
pip_gpu_rocm_torch_install_deps()
6979

80+
load("@pip_ascend_torch//:requirements.bzl", pip_ascend_torch_install_deps = "install_deps")
81+
pip_ascend_torch_install_deps()
82+
7083
load("//:def.bzl", "read_release_version")
7184
read_release_version(name = "release_version")

0 commit comments

Comments
 (0)