Skip to content
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

Zq/add specified autocompare #785

Draft
wants to merge 30 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
update readme
  • Loading branch information
NeosZhang committed Apr 19, 2024
commit 5348043e7ddb9cdf464f99066a3126a8685b4fe6
47 changes: 27 additions & 20 deletions dipu/QuickStart.md
Original file line number Diff line number Diff line change
Expand Up @@ -158,9 +158,9 @@

### 算子库拓展功能

#### 算子 Fallback

Fallback 给定算子
#### 算子Fallback功能

Check failure on line 161 in dipu/QuickStart.md

View workflow job for this annotation

GitHub Actions / markdownlint

Trailing spaces

dipu/QuickStart.md:161:18 MD009/no-trailing-spaces Trailing spaces [Expected: 0 or 2; Actual: 1] https://github.com/DavidAnson/markdownlint/blob/v0.32.1/doc/md009.md

Check failure on line 161 in dipu/QuickStart.md

View workflow job for this annotation

GitHub Actions / markdownlint

Headings should be surrounded by blank lines

dipu/QuickStart.md:161 MD022/blanks-around-headings Headings should be surrounded by blank lines [Expected: 1; Actual: 0; Below] [Context: "#### 算子Fallback功能"] https://github.com/DavidAnson/markdownlint/blob/v0.32.1/doc/md022.md
Fallback指的是使用算子的CPU实现,而非设备实现。
Fallback给定算子

```bash
export DIPU_FORCE_FALLBACK_OPS_LIST=add.out,conv2d
Expand All @@ -181,20 +181,10 @@
python -c "import torch_dipu"
```

#### 算子精度自动对比功能介绍

由于该功能默认不开启,使用该功能时需要打开该功能并重新编译DIPU。

可以通过设置环境变量USE_GLOBAL_AUTOCOMPARE=ON,来开启该功能,然后需要重新编译DIPU。

```shell
export USE_GLOBAL_AUTOCOMPARE=ON
```

以上方法是对所有算子开启自动精度对比。如果只需要对特定算子做精度对比,也可只给需要的算子做精度对比,只需要在相关的配置文件(如 `dipu/scripts/autogen_diopi_wrapper/diopi_functions.yaml`)给相应的算子添加 `autocompare: True` 即可。

#### 算子精度自动对比功能

Check failure on line 184 in dipu/QuickStart.md

View workflow job for this annotation

GitHub Actions / markdownlint

Headings should be surrounded by blank lines

dipu/QuickStart.md:184 MD022/blanks-around-headings Headings should be surrounded by blank lines [Expected: 1; Actual: 0; Below] [Context: "#### 算子精度自动对比功能"] https://github.com/DavidAnson/markdownlint/blob/v0.32.1/doc/md022.md
算子精度自动对比功能用于确保算子计算结果的正确性,通过将设备参数拷贝到CPU上,对比CPU和设备的计算结果来判断精度是否达标。以下是算子精度自动对比功能的使用例子:
```shell

Check failure on line 186 in dipu/QuickStart.md

View workflow job for this annotation

GitHub Actions / markdownlint

Fenced code blocks should be surrounded by blank lines

dipu/QuickStart.md:186 MD031/blanks-around-fences Fenced code blocks should be surrounded by blank lines [Context: "```shell"] https://github.com/DavidAnson/markdownlint/blob/v0.32.1/doc/md031.md
$ unset DIPU_FORCE_FALLBACK_OPS_LIST # 主要是确保要比较的算子没有强制 fallback 到 cpu, 可选
$ unset DIPU_FORCE_FALLBACK_OPS_LIST # 主要是确保要比较的算子没有强制 fallback到CPU, 可选
$ python
>>> import torch
>>> import torch_dipu
Expand All @@ -220,11 +210,28 @@
>>>
```

可以看到,CPU 计算结果与设备计算结果 `allclose`,也能看到 CPU 和设备计算结果的 `shape`、`dtype` 等信息。特别的,需要注意以下几个问题:
可以看到,输出包括 CPU 和设备计算结果的 `shape`、`stride`、`dtype` 等信息, 最终结果是CPU和设备的self和out都是allclose的。

##### 算子精度自动对比功能的设置

Check failure on line 215 in dipu/QuickStart.md

View workflow job for this annotation

GitHub Actions / markdownlint

Headings should be surrounded by blank lines

dipu/QuickStart.md:215 MD022/blanks-around-headings Headings should be surrounded by blank lines [Expected: 1; Actual: 0; Below] [Context: "##### 算子精度自动对比功能的设置"] https://github.com/DavidAnson/markdownlint/blob/v0.32.1/doc/md022.md
算子精度自动对比功能默认不开启,可以设置环境变量`USE_GLOBAL_AUTOCOMPARE`和`SPECIFIED_AUTOCOMPARE_OPS_LIST`来控制该功能,在开启算子自动对比功能前,必须unset `DIPU_FORCE_FALLBACK_OPS_LIST`
- 可以通过设置环境变量`USE_GLOBAL_AUTOCOMPARE=ON`,开启全局的精度对比,这种情况下所有调用的算子都会进行精度对比,也可以设置为OFF来关闭所有算子的精度自动对比功能

Check failure on line 217 in dipu/QuickStart.md

View workflow job for this annotation

GitHub Actions / markdownlint

Lists should be surrounded by blank lines

dipu/QuickStart.md:217 MD032/blanks-around-lists Lists should be surrounded by blank lines [Context: "- 可以通过设置环境变量`USE_GLOBAL_AUTOCO..."] https://github.com/DavidAnson/markdownlint/blob/v0.32.1/doc/md032.md

Check failure on line 217 in dipu/QuickStart.md

View workflow job for this annotation

GitHub Actions / markdownlint

Lists should be surrounded by blank lines

dipu/QuickStart.md:217 MD032/blanks-around-lists Lists should be surrounded by blank lines [Context: "- 可以通过设置环境变量`USE_GLOBAL_AUTOCO..."] https://github.com/DavidAnson/markdownlint/blob/v0.32.1/doc/md032.md
```shell

Check failure on line 218 in dipu/QuickStart.md

View workflow job for this annotation

GitHub Actions / markdownlint

Fenced code blocks should be surrounded by blank lines

dipu/QuickStart.md:218 MD031/blanks-around-fences Fenced code blocks should be surrounded by blank lines [Context: "```shell"] https://github.com/DavidAnson/markdownlint/blob/v0.32.1/doc/md031.md
# 开启全局的算子精度自动对比功能
export USE_GLOBAL_AUTOCOMPARE=ON
```

- 在未开启`USE_GLOBAL_AUTOCOMPARE`的前提下,可以设置`SPECIFIED_AUTOCOMPARE_OPS_LIST`来指定算子开启自动精度对比,支持正则表达式匹配。算子名可以参考[diopi_functions.yaml](https://github.com/DeepLink-org/deeplink.framework/blob/main/dipu/scripts/autogen_diopi_wrapper/diopi_functions.yaml)。

```shell
# 关闭全局的算子精度自动对比功能,并指定add*算子进行对比
export USE_GLOBAL_AUTOCOMPARE=OFF
export SPECIFIED_AUTOCOMPARE_OPS_LIST=add*
```

1. `dipu/scripts/autogen_diopi_wrapper/diopi_functions.yaml` 中配置了 `autograd:True` 的算子 (`cross_entropy_loss`、`conv2d`、`dropout`、`dropout_`、`linear`) 暂不支持 *backward* 的精度自动对比。如模型精度对不齐,可根据需要先将这几个算子 fallback 到 CPU 来确定问题。
2. 随机数生成相关的算子(`dipu/scripts/autogen_diopi_wrapper/diopi_functions.yaml` 中配置了 `autocompare:False`)没有做 `autocompare`,因为结果总是 `not_allclose`。
3. 对输入做检查是确保算子输入不被意外修改。
NOTE:

Check failure on line 231 in dipu/QuickStart.md

View workflow job for this annotation

GitHub Actions / markdownlint

Trailing spaces

dipu/QuickStart.md:231:6 MD009/no-trailing-spaces Trailing spaces [Expected: 0 or 2; Actual: 1] https://github.com/DavidAnson/markdownlint/blob/v0.32.1/doc/md009.md
1. 部分算子并不支持自动精度对比功能,可以查看[diopi_functions.yaml](https://github.com/DeepLink-org/deeplink.framework/blob/main/dipu/scripts/autogen_diopi_wrapper/diopi_functions.yaml),其中的`autocompare`配置项为`disable`即不支持自动精度对比功能,同时也可以修改`diopi_functions.yaml`,将某些算子的`autocompare`配置项设置为`disable`来禁用自动对比功能。

Check failure on line 232 in dipu/QuickStart.md

View workflow job for this annotation

GitHub Actions / markdownlint

Lists should be surrounded by blank lines

dipu/QuickStart.md:232 MD032/blanks-around-lists Lists should be surrounded by blank lines [Context: "1. 部分算子并不支持自动精度对比功能,可以查看[diopi..."] https://github.com/DavidAnson/markdownlint/blob/v0.32.1/doc/md032.md
2. `dipu/scripts/autogen_diopi_wrapper/diopi_functions.yaml` 中配置了 `autograd:True` 的算子 (`cross_entropy_loss`、`conv2d`、`dropout`、`dropout_`、`linear`) 暂不支持 *backward* 的精度自动对比。如模型精度对不齐,可根据需要先将这几个算子 fallback 到 CPU 来确定问题。
3. 对输入参数(self)做检查是确保算子的输入不被意外修改。

#### 抓取算子参数

Expand Down
15 changes: 8 additions & 7 deletions dipu/torch_dipu/csrc_dipu/aten/RegisterDIPU.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,15 @@ void dipu_fallback(const c10::OperatorHandle& op, DispatchKeySet dispatch_keys,
// for non-custom ops through function dipuKeepTorchopDefaultImpl firstly in the
// future, and we use force fallback to keep torchop default impl now.
#define addAutoCompare(wrapperFunc) wrapperFunc##_autocompare
#define DIOPI_ATEN_FUNC(opname, diopiFunc, wrapperFunc) \
#define DIOPI_ATEN_FUNC(opname, diopiFunc, wrapperFunc) \
do { \
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

嵌套太多层了,这里的逻辑可以简化很多

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我把嵌套的if条件拆开了

if ((reinterpret_cast<void*>(diopiFunc) != nullptr) && \
(!dipu::whetherOpMatch(opname, fallbackMatchers))) { \
if (dipu::whetherAutoCompare(opname, autocompareMatchers)) { \
m.impl(opname, TORCH_FN(addAutoCompare(wrapperFunc))); \
if (dipu::whetherAutoCompare(opname, autocompareMatchers)) { \
m.impl(opname, TORCH_FN(addAutoCompare(wrapperFunc))); \
} else { \
m.impl(opname, TORCH_FN(wrapperFunc)); \
} \
m.impl(opname, TORCH_FN(wrapperFunc)); \
} else { \
if ((reinterpret_cast<void*>(diopiFunc) == nullptr)) { \
DIPU_OP_LOG_WARNING_ONCE(#diopiFunc << " is not yet implemented, "); \
Expand All @@ -66,11 +67,11 @@ void dipu_fallback(const c10::OperatorHandle& op, DispatchKeySet dispatch_keys,
} \
} while (false);

#define DIOPI_ATEN_FUNC_DISABLE_AUTOCOMPARE(opname, diopiFunc, wrapperFunc) \
#define DIOPI_ATEN_FUNC_DISABLE_AUTOCOMPARE(opname, diopiFunc, wrapperFunc) \
do { \
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

if ((reinterpret_cast<void*>(diopiFunc) != nullptr) && \
(!dipu::whetherOpMatch(opname, fallbackMatchers))) { \
m.impl(opname, TORCH_FN(wrapperFunc)); \
(!dipu::whetherOpMatch(opname, fallbackMatchers))) { \
m.impl(opname, TORCH_FN(wrapperFunc)); \
} else { \
if ((reinterpret_cast<void*>(diopiFunc) == nullptr)) { \
DIPU_OP_LOG_WARNING_ONCE(#diopiFunc << " is not yet implemented, "); \
Expand Down
6 changes: 2 additions & 4 deletions dipu/torch_dipu/csrc_dipu/aten/ops/OpRegexMatch.cpp
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

考虑到用户对正则的不熟悉,是否有必要改成 wildcard?

Copy link
Collaborator Author

@NeosZhang NeosZhang Apr 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

多数场景下,算子匹配并不会用到很复杂的正则的语法,在readme里面多给几个常用的场景的示例就可以了。
但如果换成wildcard,灵活性会差很多,可能不能满足一些极端场景的需求

Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,8 @@ bool whetherAutoCompare(const char* opname, std::vector<std::regex> autocompareM
return true;
}
// else if opname in SPECIFIED_AUTOCOMPARE_OPS_LIST, the specified op will be autocomapred
// return whetherOpMatch(opname, autocompareMatchers);
return false;

}
return whetherOpMatch(opname, autocompareMatchers);
}
} // end of namespace dipu

const char* fallback_env_name = "DIPU_FORCE_FALLBACK_OPS_LIST";
Expand Down
Loading