本文最后更新于 2024-12-05,文章内容可能已经过时。

【快乐开源】Paddle Tensor 规范化二期 · Issue #69908 · PaddlePaddle/Paddle

背景

参考项目一期:#69082

任务列表

  1. 【51个】API 支持 0-size Tensor:要求 API 底层的前向、反向 kernel 支持 0-size Tensor,0-size Tensor 参与运算时,其形状遵循一般情况下的形状计算规则,如add([0,4], [0, 4]),其结果还是[0, 4],[0, 4].sum(axis=-1),其结果是[0]

  2. 【25个】API 鲁棒性增强:某些参数组合下计算的结果不符合预期,以 paddle.arange 为例

    image
    start=0,stop=-9007199254740994,step=-9007199254740993时,结果是[0, -9007199254740992],其中-9007199254740992是不正确,正确的值应该是-9007199254740993,这需要在Kernel中进行修复

  3. 【19个】常用功能的复数类型支持:这类问题比较多,需要统一集中解决,目前可以先初步支持不涉及数值计算,只涉及比较判断的场景,如 paddle.equal、paddle.less、paddle.isnan,为其底层Kernel支持复数。

  4. 【9个】其他类型支持:大部分集中在Kernel支持布尔类型问题上,由于布尔类型本身只有两种取值,所以支持难度较低,只需要在原Kernel的基础上,额外添加布尔类型的特化版本代码,并为Kernel注册布尔类型即可。

  5. 【15个】其它问题:比较基础的简单问题,如diagonal这个API没有在paddle.linalg模块下

单测方法

选定任务之后,将表格中的单测名称复制下来,然后按照以下步骤先复现出报错:

# container
# 安装 pytest
pip install pytest hypothesis==6.112.2

# 安装 array-api-compat
git clone https://github.com/HydrogenSulfate/array-api-compat.git -b support_paddle
cd array-api-compat
pip install -e .
cd ..

# 下载 array-api-tests
git clone https://github.com/HydrogenSulfate/array-api-tests.git -b paddle
cd array-api-tests

# 运行某个单测(以 array_api_tests/test_operators_and_elementwise_functions.py::test_logaddexp 为例)
ARRAY_API_TESTS_MODULE=array_api_compat.paddle \
ARRAY_API_TESTS_SKIP_DTYPES=int8,int16,uint16,uint8,uint32,uint64 \
ARRAY_API_TESTS_VERSION="2023.12" \
python -m pytest -s -vvv array_api_tests/test_operators_and_elementwise_functions.py::test_logaddexp

接着根据报错信息,分析报错原因,再视情况,对 Paddle 中的相关代码进行修复;
本地开发完毕后,重新运行上述单测,能够通过说明修复成功,提交PR到Paddle下(复数类型支持的还需要修改对应API的文档,添加复数类型支持到参数说明中)

问题


ImportError while loading conftest '/home/array-api-tests/conftest.py'.
conftest.py:17: in <module>
    from reporting import pytest_metadata, pytest_json_modifyreport, add_extra_json_metadata # noqa
reporting.py:1: in <module>
    from array_api_tests.dtype_helpers import dtype_to_name
array_api_tests/dtype_helpers.py:319: in <module>
    default_int = xp.asarray(int()).dtype
../array-api-compat/array_api_compat/paddle/_aliases.py:1093: in asarray
    obj = np.array(obj, copy=False)
E   ValueError: Unable to avoid copy while creating an array as requested.
E   If using `np.array(obj, copy=False)` replace it with `np.asarray(obj)` to allow a copy when needed (no behavior change in NumPy 1.x).
E   For more details, see https://numpy.org/devdocs/numpy_2_0_migration_guide.html#adapting-to-changes-in-the-copy-keyword.

解决方法:

/array-api-compat/array_api_compat/paddle/_aliases.py 修改1093行:

obj = np.array(obj, copy=False)

obj = np.array(obj)
ImportError while loading conftest '/home/array-api-tests/conftest.py'.
conftest.py:17: in <module>
    from reporting import pytest_metadata, pytest_json_modifyreport, add_extra_json_metadata # noqa
reporting.py:17: in <module>
    raise ImportError("pytest-json-report is required to run the array API tests")
E   ImportError: pytest-json-report is required to run the array API tests

安装依赖即可

pip install pytest-json-report

测试样例运行结果如下,根据错误进行更正即可