Skip to content

Commit

Permalink
Merge branch 'main' into yfc/support_stable_diffusion
Browse files Browse the repository at this point in the history
  • Loading branch information
yao-fengchen authored Jan 8, 2024
2 parents ec5fe06 + 3ecb00b commit 2f8ecdd
Show file tree
Hide file tree
Showing 143 changed files with 3,815 additions and 1,904 deletions.
28 changes: 27 additions & 1 deletion .github/CODEOWNERS
Original file line number Diff line number Diff line change
@@ -1,4 +1,30 @@
# IMPORTANT:
# This file is ONLY used to merge PRs. Approvals from people in this file are required for merging.
#
# WARNING: The last matching pattern takes the most precedence and OVERWRITES previous rules.
# Please be very careful when adding new patterns.

/dipu/tests/python @lljbash @mrdanielw
# ---------- base ----------

* @mrdanielw @jinminxi104
/.github/ @mrdanielw @wugeshui
/.github/CODEOWNERS @mrdanielw @jinminxi104

# ---------- dipu ----------

### directories & files
/dipu/torch_dipu/csrc_dipu/ @mrdanielw @fandaoyi @lljbash
/dipu/tests/python/ @mrdanielw @lljbash
/dipu/scripts/autogen_diopi_wrapper/ @mrdanielw @lljbash
/dipu/scripts/autogen_diopi_wrapper/autogen_diopi_wrapper.py @mrdanielw @zhaoguochun1995
/dipu/scripts/ci/ @mrdanielw @wugeshui

### build & linter
/dipu/**/CMakeLists.txt @mrdanielw @lljbash @wiryls @Wrench-Git
/dipu/**/*.cmake @mrdanielw @lljbash @wiryls @Wrench-Git
/dipu/.clang* @mrdanielw @lljbash @wiryls

# ---------- dicp ----------

/dicp/ @jinminxi104
/dicp/scripts/ci/ @jinminxi104 @wugeshui
6 changes: 5 additions & 1 deletion .github/actions/code-build-test/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ runs:
else
export CI=true
source ~/.bashrc
cd ${WORK_PATH} && rm -rf ${JOB_NAME} && cp -R source ${JOB_NAME} && cd ${JOB_NAME}
cd ${WORK_PATH}
if [ "${{ inputs.cover_job }}" == "0" ];then
rm -rf ${JOB_NAME} && cp -R source ${JOB_NAME}
fi
cd ${JOB_NAME}
${{ inputs.build_shell }} ${cleaner_shell}
fi
45 changes: 29 additions & 16 deletions .github/workflows/_runs-on-ascend.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ on:
description: Set up the build environment
type: string
required: false
default: "tps-ascend-ci"
default: "dicp-ascend-ci-910b"

jobs:
checkout_code:
Expand All @@ -22,25 +22,38 @@ jobs:
- name: Checkout Code
uses: DeepLink-org/deeplink.framework/.github/actions/checkout-code@main

build:
build_test:
runs-on: ${{ inputs.runner }}
needs: checkout_code
steps:
- name: build on ascend
- name: build and test on ascend
uses: DeepLink-org/deeplink.framework/.github/actions/code-build-test@main
with:
build_shell: "pwd" #Write the script you want to execute here,If you don't know which parameters to fill in, you can refer to the actions/code-build-test
job_name: "build"
build_shell: "
source dicp/scripts/ci/ascend/dipu_env.sh && \
rm -rf /tmp/torchinductor_autolink/* && \
rm -rf /tmp/dicp_ascend/* && \
cd /mnt/cache/share/deeplinkci/dicp_env/transformers && \
pip uninstall transformers -y && \
patch -p1 < modeling_llama.diff && patch -p1 < utils.diff && \
python setup.py clean && \
python setup.py install --user && \
patch -R -p1 < modeling_llama.diff && patch -R -p1 < utils.diff && \
cd - && \
cd /mnt/cache/share/deeplinkci/dicp_env/accelerate && \
pip uninstall accelerate -y && \
python setup.py clean && \
python setup.py install --user && \
cd - && \
pip uninstall torch_dipu -y && \
pip uninstall dicp -y && \
cd dipu && python setup.py clean && python setup.py install --user && \
cd ../dicp && python setup.py clean && python setup.py install --user && \
source scripts/ci/ascend/test_env.sh /mnt/cache/share/deeplinkci/dicp_env/llama_models && \
export TEST_DIR=$(pwd)/test && echo ${TEST_DIR} && \
bash ${TEST_DIR}/ascend_scripts/ops/run_test_ops.sh false && \
bash ${TEST_DIR}/ascend_scripts/models/run_test_models.sh false
" #Write the script you want to execute here,If you don't know which parameters to fill in, you can refer to the actions/code-build-test
job_name: "build_test"
cover_job: "0"
cleaner: "clean_all_if_error"

test:
runs-on: ${{ inputs.runner }}
needs: build
steps:
- name: rt test on ascend
uses: DeepLink-org/deeplink.framework/.github/actions/code-build-test@main
with:
build_shell: "pwd" #Write the script you want to execute here,If you don't know which parameters to fill in, you can refer to the actions/code-build-test
job_name: "build"
cover_job: "1"
20 changes: 14 additions & 6 deletions .github/workflows/_runs-on-topsrider.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,27 @@ jobs:
runs-on: ${{ inputs.runner }}
needs: checkout_code
steps:
- name: build and test on topsrider
- name: build on topsrider
uses: DeepLink-org/deeplink.framework/.github/actions/code-build-test@main
with:
build_shell: "
source dicp/scripts/ci/tops/ci_tops_build_env.sh && \
pip uninstall torch_dipu -y && \
pip uninstall dicp -y && \
cd dipu && python setup.py install --user && \
cd ../dicp && python setup.py install --user && \
cd .. && source dicp/scripts/ci/tops/ci_tops_test_env.sh /mnt/models/llama_models && \
export TEST_DIR=$(pwd)/dicp/test && echo ${TEST_DIR} && \
bash ${TEST_DIR}/tops_scripts/ops/run_test_ops.sh false && \
bash ${TEST_DIR}/tops_scripts/models/run_test_models.sh false
cd ../dicp && python setup.py install --user
"
job_name: "build_test"
cover_job: "0"

- name: test ops on topsrider
uses: DeepLink-org/deeplink.framework/.github/actions/code-build-test@main
with:
build_shell: "
source dicp/scripts/ci/tops/ci_tops_test_env.sh \
/mnt/models/llama_models /mnt/models/stable_diffusion_models && \
export TEST_DIR=$(pwd)/dicp/test && \
bash ${TEST_DIR}/tops_scripts/ops/run_test_ops.sh false
"
job_name: "build_test"
cover_job: "1"
15 changes: 7 additions & 8 deletions .github/workflows/dicp.yml
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
name: dicp ci
on:
workflow_dispatch:
push:
branches:
- main
schedule:
- cron: '10 23 * * *'
pull_request:
paths-ignore:
- "**.md"
- ".github/ISSUE_TEMPLATE/**"
- ".git*"
- "CODE_OF_CONDUCT**"
paths:
- ".github/workflows/dicp.yml"
- ".github/workflows/_runs-on-ascend.yml"
- ".github/workflows/_runs-on-topsrider.yml"
- "dicp/**"

env:
ENV_PATH: '/mnt/cache/share/platform/env'
Expand Down
15 changes: 9 additions & 6 deletions .github/workflows/format.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,22 @@ jobs:
markdownlint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Checkout code
uses: actions/checkout@v4
with:
fetch-depth: 2
- uses: tj-actions/changed-files@v40
fetch-depth: 8
- name: Collect changed files
uses: tj-actions/changed-files@v40
id: changed-files
with:
files: '**/*.md'
separator: ","
- uses: DavidAnson/markdownlint-cli2-action@v14
separator: ','
- name: MarkdownLint
if: steps.changed-files.outputs.any_changed == 'true'
uses: DavidAnson/markdownlint-cli2-action@v14
with:
globs: ${{ steps.changed-files.outputs.all_changed_files }}
separator: ","
separator: ','

clang-format:
needs: markdownlint
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ env:
CAMB_CLUSTER: CAMB
CAMB_TORCH_BASE_DIR: '/mnt/lustre/share/parrotsci/github/cibuild/pytorchbase'
CUDA_CI_PATH: '/mnt/cache/share/parrotsci/github/cibuild/${{ github.repository }}'
CUDA_PARTATION: ${{ vars.SH1988_SLURM_PAR != '' && vars.SH1988_SLURM_PAR || 'pat_rd -x SH-IDC1-10-198-8-60' }}
CUDA_PARTATION: ${{ vars.SH1988_SLURM_PAR != '' && vars.SH1988_SLURM_PAR || 'pat_rd' }}
CUDA_CLUSTER: SH1988
DEEPLINK_PATH: '/mnt/cache/share/deeplinkci/github/${{ github.repository }}'
ASCEND_CLUSTER: ASCEND
Expand All @@ -24,7 +24,7 @@ env:
CI_BUILD_FLAG: "ci_build_flag"
PYTORCH_COMMIT: ${{ vars.PYTORCH_COMMIT != '' && vars.PYTORCH_COMMIT || 'c263bd43e8e8502d4726643bc6fd046f0130ac0e' }} # pytorch tag 2.0
ALL_COVERAGE: ${{ (contains( github.ref, 'main') || startsWith(github.ref, 'refs/heads/v') || startsWith(github.ref, 'refs/heads/dev')) && 'ON' || 'OFF' }}
REQUIRE_COVERAGE: ${{ vars.REQUIRE_COVERAGE != '' && vars.REQUIRE_COVERAGE || '40' }}
REQUIRE_COVERAGE: ${{ vars.REQUIRE_COVERAGE != '' && vars.REQUIRE_COVERAGE || '0' }}
REPO: ${{ github.event.repository.name }}

concurrency:
Expand Down Expand Up @@ -128,7 +128,7 @@ jobs:
cd ${CAMB_CI_PATH}/${GITHUB_RUN_NUMBER}/Build-Camb
rm -rf scripts
ln -s ${CAMB_CI_PATH}/${GITHUB_RUN_NUMBER}/source-main/dipu/third_party/DIOPI/scripts scripts
source /mnt/cache/share/platform/env/camb_ci_diopi_impl
source /mnt/cache/share/platform/env/pt2.0_diopi
bash scripts/increment_coverage.sh ${REQUIRE_COVERAGE}
"""
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Deeplink.framework 是 DeepLink 推出的介于 AI 训练框架和硬件语言

### DIPU

DIPU (Device Independent Process Unit) 是由一组抽象设备 runtime 接口,一组框架能力相关的运行时基类/接口,一个针对 DIOPI 标准算子的适配层共同组成的拓展包。 用来在训练框架 PyTorch 上接入 DIOPI 算子库,实现 Eager 模式的推理和训练。其能够在编译时,决定抽象设备被影射的方式;并使用统一的运行时,减少在多硬件上适配训练框架的成本。DIPU 即可以基于统一的设备运行时来屏蔽厂商的实际设备;也可以基于统一的框架相关的运行时基类,由厂商自行实现特有的运行时逻辑。
DIPU (Device Independent Process Unit) 是由一组抽象设备 runtime 接口,一组框架能力相关的运行时基类/接口,一个针对 DIOPI 标准算子的适配层共同组成的拓展包。用来在训练框架 PyTorch 上接入 DIOPI 算子库,实现 Eager 模式的推理和训练。其能够在编译时,决定抽象设备被影射的方式;并使用统一的运行时,减少在多硬件上适配训练框架的成本。DIPU 即可以基于统一的设备运行时来屏蔽厂商的实际设备;也可以基于统一的框架相关的运行时基类,由厂商自行实现特有的运行时逻辑。

### DICP

Expand Down
2 changes: 2 additions & 0 deletions dicp/MANIFEST.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
recursive-include dicp/vendor/TopsGraph/codegen *
recursive-include dicp/vendor/AscendGraph/codegen *
85 changes: 85 additions & 0 deletions dicp/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<!-- markdownlint-disable-next-line MD041 MD033 -->
<div align=center>
<!-- markdownlint-disable-next-line MD033 -->
<img src="https://deeplink.readthedocs.io/zh-cn/latest/_static/image/logo.png" alt="DeepLink Logo">
</div>

# DICP

标准编译协议(Device-Independent Compile Protocol, DICP)定义了统一的计算描述(中间表示),通过计算图获取深度学习模型中的计算任务表达为上述中间表示,然后通过计算图优化技术自动生成人工智能芯片设备代码,从而提高研发效率和计算的执行性能。中间表示是介于源语言和目标语言之间的程序表示,能够极大程度地提高编译流程的可拓展性,同时也能降低优化流程对前端和后端的破坏。多层次中间表示包含从应用到芯片端的多种表示层次,不同层次旨在解决不同尺度的问题。

DICP 主要的核心功能如下:

1. 通过接入编译路线带来性能优势,在大模型场景最大限度释放芯片能力。
2. 作为训练框架与国产硬件芯片之间的通用桥梁,支持多种前后端,带来使用易用性。
3. 提供易用、高效的一站式编译适配流程,灵活支持国产硬件图编译器的特性,提高芯片适配效率。

下图描述了 DICP 在编译链路中的位置:

![DICP 在编译链路中的位置](https://deeplink.readthedocs.io/zh-cn/latest/_static/image/DICP/dicp_flow.png)

1. 训练框架通过图获取模块将用户的模型代码转换成统一的中间表达。此处的中间表达完全与芯片无关。所以在之后的编译协议部分中,需要建立起与后端芯片的联系。这样才能高效的完成接入。
2. 编译协议完成了衔接框架与芯片编译器的工作,其中包含硬件相关的切图,统一中间表达与芯片所支持的算子之间的映射关系以及数据格式的转换模块。
3. 在编译协议吸收了芯片特点之后,由代码生成模块生成最终的代码,并通过芯片的编译器生成二进制可执行文件之后由框架调用。

## 基于 DICP 的国产硬件接入 PyTorch 2 实践

<!-- ### DICP vs 纯 Dynamo -->

基于上述 DICP,国产硬件可快速接入 PyTorch 2 的编译路线。此路线中的 TorchDynamo 组件,可使国产硬件在运行时的 overhead 大幅缩小。
并且针对国产硬件实现了以下特性:

- 灵活支持国产硬件图编译器的特性
- 支持多种国产硬件数据格式
- 支持动态 shape

### 运行逻辑

DICP 的运行逻辑如下图所示:

<!-- (**这张图有问题,需要讨论 by jinminxi**) -->
![DICP 的运行逻辑](https://deeplink.readthedocs.io/zh-cn/latest/_static/image/DICP/structure.png)

其中:

1. **算子映射**:主要解决框架层算子与后端图编译器的算子之间的语义差别,包括 1 对 1 和 1 对多的转换。
2. **Shape & Dtype 推导**:进行 Shape & data_type 的推导,补全整张静态图上的信息,便于之后在代码生成模块能生成代码。
3. **子图改写**:将多个小算子融合成为一个或多个适合图编译器的算子,配合后端图编译器将计算效率最大化。
4. **数据格式调整**:是根据后端芯片与其图编译器的特性,针对特定的算子调整其输入输出的数据格式,使得最大程度的发挥芯片性能。

### 目录结构

- `dicp/dynamo_bridge`:多后端通用的接入代码,包含了
1. 接收从 AOTAutograd 下发而来的 FX Graph
2. 启动各个厂商的 IR 转换与优化
3. 启动 CodeGen 以及 JIT 缓存的逻辑。
- `dicp/vender`: 主要包含了各个厂商 IR 的定义,AtenIR 到厂商 IR 的转换,厂商 IR 上的优化以及最后的代码生成模块。
- `test`: 包含了 model 测试与 op 测试

### Demo

#### 安装 DICP

```bash
cd /path_to_dicp
pip install .
```

#### 在华为 910 上执行 llama7B 前向推理

```bash
export DIPU_MOCK_CUDA = false
export DICP_TOPS_DIPU = True
export TEST_DIR = /path_to_dicp/test/
export LLAMA_MODEL_DIR=/path_to_llama_model
bash /path_to_dicp/test/model/run_test_model.sh llama ascendgraph false
```

#### 在燧原 T20 上执行 resnet50 训练

```bash
export DIPU_MOCK_CUDA = false
export DICP_TOPS_DIPU = True
export TEST_DIR = /path_to_dicp/test/
bash /path_to_dicp/test/model/run_test_model.sh resnet50 topsgraph false
```
3 changes: 3 additions & 0 deletions dicp/dicp/dynamo_bridge/op_transformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ def get_proxy(self, target, args: Tuple[Argument, ...], kwargs: Dict[str, Any] =
'call_function', target.get_singleton(), args, kwargs)
return proxy

def get_proxy_from_node(self, node):
return self.tracer.proxy(node)

def call_function(self, target: Target, args: Tuple[Argument, ...], kwargs: Dict[str, Any]) -> Any:
if target in self._conversions:
converted_target = self._conversions[target]
Expand Down
2 changes: 1 addition & 1 deletion dicp/dicp/dynamo_bridge/operator.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ def make_cpu(x):
except Exception as e:
log = logging.getLogger(__name__)
if hasattr(self, "infer_result"):
log.warning(
log.debug(
str(self.__name__) + ": infer shape and dtype failed,ignore"
)
elif hasattr(self, "torch_op"):
Expand Down
Loading

0 comments on commit 2f8ecdd

Please sign in to comment.