-
Notifications
You must be signed in to change notification settings - Fork 175
feat(ascend): Implement Ascend backend ops (Linear, RMSNorm, SiLU) #615
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
ea640d2
1e78313
b78617a
a89c6fb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| # Ascend Add Op Demo | ||
|
|
||
| 这是一个简单的 demo,用于测试 Ascend 后端的 Add 算子实现。 | ||
|
|
||
| ## 功能 | ||
|
|
||
| - 初始化 Ascend 后端和内存池 | ||
| - 创建两个输入张量(shape: [2, 3]) | ||
| - 在 Ascend NPU 上执行 Add 操作 | ||
| - 验证计算结果是否正确 | ||
|
|
||
| ## 编译和运行 | ||
|
|
||
| ### 方法 1: 使用自动化脚本(推荐) | ||
|
|
||
| ```bash | ||
| cd /home/HwHiAiUser/mLLM/examples/ascend_add_demo | ||
| ./build_and_run.sh | ||
| ``` | ||
|
|
||
| 脚本会自动: | ||
| - 检查环境变量 | ||
| - 配置 CMake | ||
| - 编译项目 | ||
| - 运行 demo | ||
|
|
||
| ### 方法 2: 手动编译 | ||
|
|
||
| 确保已经设置了必要的环境变量: | ||
| - `ASCEND_HOME_PATH`: Ascend SDK 路径(已设置: `/usr/local/Ascend/ascend-toolkit/latest`) | ||
| - `ATB_HOME_PATH`: ATB 库路径(已设置: `/usr/local/Ascend/nnal/nnal/atb/latest/atb/cxx_abi_0`) | ||
|
|
||
| 在项目根目录下: | ||
|
|
||
| ```bash | ||
| # 1. 创建构建目录 | ||
| mkdir -p build-ascend-demo && cd build-ascend-demo | ||
|
|
||
| # 2. 配置 CMake | ||
| cmake .. \ | ||
| -DMLLM_BUILD_ASCEND_BACKEND=ON \ | ||
| -DMLLM_ENABLE_EXAMPLE=ON \ | ||
| -DCMAKE_BUILD_TYPE=Release | ||
|
|
||
| # 3. 编译 | ||
| make ascend_add_demo -j$(nproc) | ||
|
|
||
| # 4. 运行 | ||
| ./examples/ascend_add_demo/ascend_add_demo | ||
| ``` | ||
|
|
||
| ## 预期输出 | ||
|
|
||
| ``` | ||
| === Ascend Add Op Demo === | ||
| 1. Initializing Ascend backend... | ||
| ✓ Ascend backend initialized | ||
|
|
||
| 2. Creating input tensors... | ||
| Input x shape: [2, 3] | ||
| Input y shape: [2, 3] | ||
|
|
||
| 3. Transferring tensors to Ascend device... | ||
| ✓ Tensors transferred to Ascend | ||
|
|
||
| 4. Executing Add operation on Ascend... | ||
| ✓ Add operation completed | ||
|
|
||
| 5. Transferring result back to CPU and verifying... | ||
| Expected result: [11, 22, 33, 44, 55, 66] | ||
| Actual result: [11, 22, 33, 44, 55, 66] | ||
|
|
||
| ✓ Test PASSED! All values match expected results. | ||
| ``` | ||
|
Comment on lines
+54
to
+74
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Specify a language for the expected-output code fence. markdownlint MD040 requires a language; ✅ Suggested fix-```
+```text
=== Ascend Add Op Demo ===
...
-```
+```🧰 Tools🪛 markdownlint-cli2 (0.20.0)[warning] 54-54: Fenced code blocks should have a language specified (MD040, fenced-code-language) 🤖 Prompt for AI Agents |
||
|
|
||
| ## 注意事项 | ||
|
|
||
| - 当前实现使用 float16 数据类型 | ||
| - 需要 Ascend NPU 设备可用 | ||
| - 确保已正确安装 Ascend SDK 和 ATB 库 | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,74 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #!/bin/bash | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # Ascend Add Demo 编译和运行脚本 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| set -e # 遇到错误立即退出 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # 颜色输出 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| GREEN='\033[0;32m' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| YELLOW='\033[1;33m' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| RED='\033[0;31m' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| NC='\033[0m' # No Color | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| echo -e "${GREEN}=== Ascend Add Demo 编译和运行脚本 ===${NC}\n" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # 检查环境变量 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| echo -e "${YELLOW}检查环境变量...${NC}" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if [ -z "$ASCEND_HOME_PATH" ]; then | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| echo -e "${RED}错误: ASCEND_HOME_PATH 未设置${NC}" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| exit 1 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| fi | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if [ -z "$ATB_HOME_PATH" ]; then | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| echo -e "${RED}错误: ATB_HOME_PATH 未设置${NC}" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| exit 1 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| fi | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| echo -e "${GREEN}✓ ASCEND_HOME_PATH: $ASCEND_HOME_PATH${NC}" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| echo -e "${GREEN}✓ ATB_HOME_PATH: $ATB_HOME_PATH${NC}\n" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # 获取项目根目录 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| PROJECT_ROOT="$(cd "$SCRIPT_DIR/../.." && pwd)" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| BUILD_DIR="$PROJECT_ROOT/build-ascend-demo" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| echo -e "${YELLOW}项目根目录: $PROJECT_ROOT${NC}" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| echo -e "${YELLOW}构建目录: $BUILD_DIR${NC}\n" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # 创建构建目录 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if [ ! -d "$BUILD_DIR" ]; then | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| echo -e "${YELLOW}创建构建目录...${NC}" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| mkdir -p "$BUILD_DIR" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| fi | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| cd "$BUILD_DIR" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # 配置 CMake | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| echo -e "\n${YELLOW}配置 CMake...${NC}" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| cmake "$PROJECT_ROOT" \ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| -DMLLM_BUILD_ASCEND_BACKEND=ON \ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| -DMLLM_ENABLE_EXAMPLE=ON \ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| -DCMAKE_BUILD_TYPE=Release \ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| -DCMAKE_EXPORT_COMPILE_COMMANDS=ON | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # 编译 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| echo -e "\n${YELLOW}开始编译...${NC}" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| make ascend_add_demo -j$(nproc) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # 检查编译结果 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if [ $? -eq 0 ]; then | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| echo -e "\n${GREEN}✓ 编译成功!${NC}\n" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # 运行 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| echo -e "${YELLOW}运行 demo...${NC}\n" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ./examples/ascend_add_demo/ascend_add_demo | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if [ $? -eq 0 ]; then | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| echo -e "\n${GREEN}✓ Demo 运行成功!${NC}" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| else | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| echo -e "\n${RED}✗ Demo 运行失败${NC}" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| exit 1 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| fi | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+52
to
+69
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧩 Analysis chain🏁 Script executed: # First, locate and read the entire script to check for set -e
cat examples/ascend_add_demo/build_and_run.shRepository: UbiquitousLearning/mllm Length of output: 1667 Fix error handling: custom failure messages won't execute with With 🔧 Suggested update # 编译
echo -e "\n${YELLOW}开始编译...${NC}"
-make ascend_add_demo -j$(nproc)
-
-# 检查编译结果
-if [ $? -eq 0 ]; then
+if make ascend_add_demo -j"$(nproc)"; then
echo -e "\n${GREEN}✓ 编译成功!${NC}\n"
# 运行
echo -e "${YELLOW}运行 demo...${NC}\n"
- ./examples/ascend_add_demo/ascend_add_demo
-
- if [ $? -eq 0 ]; then
+ if ./examples/ascend_add_demo/ascend_add_demo; then
echo -e "\n${GREEN}✓ Demo 运行成功!${NC}"
else
echo -e "\n${RED}✗ Demo 运行失败${NC}"
exit 1
fi
else
echo -e "\n${RED}✗ 编译失败${NC}"
exit 1
fi📝 Committable suggestion
Suggested change
🧰 Tools🪛 Shellcheck (0.11.0)[warning] 54-54: Quote this to prevent word splitting. (SC2046) 🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| else | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| echo -e "\n${RED}✗ 编译失败${NC}" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| exit 1 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| fi | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Avoid hard-coded absolute paths in the demo instructions.
Using an environment-specific path makes the steps non-portable. Consider a placeholder such as
<repo_root>/examples/ascend_add_demo.✏️ Suggested edit
📝 Committable suggestion
🤖 Prompt for AI Agents