From 34facedcf680cf7be1ef6a8983607f19b00bb780 Mon Sep 17 00:00:00 2001 From: Aaron Meurer Date: Wed, 22 May 2024 12:39:29 -0600 Subject: [PATCH 1/5] Fix torch.linalg.vector_norm for axis=() --- array_api_compat/torch/linalg.py | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/array_api_compat/torch/linalg.py b/array_api_compat/torch/linalg.py index 7e7e2415..41d16519 100644 --- a/array_api_compat/torch/linalg.py +++ b/array_api_compat/torch/linalg.py @@ -78,7 +78,23 @@ def vector_norm( ) -> array: # torch.vector_norm incorrectly treats axis=() the same as axis=None if axis == (): - keepdims = True + out = kwargs.get('out') + if out is None: + dtype = None + if x.dtype == torch.complex64: + dtype = torch.float32 + elif x.dtype == torch.complex128: + dtype = torch.float64 + + out = torch.zeros_like(x, dtype=dtype) + + # The norm of a single scalar works out to abs(x) in every case except + # for ord=0, which is x != 0. + if ord == 0: + out[:] = (x != 0) + else: + out[:] = torch.abs(x) + return out return torch.linalg.vector_norm(x, ord=ord, axis=axis, keepdim=keepdims, **kwargs) __all__ = linalg_all + ['outer', 'matmul', 'matrix_transpose', 'tensordot', From 5e7128cc1824fa3c503795029d6e753a254211a9 Mon Sep 17 00:00:00 2001 From: Aaron Meurer Date: Wed, 22 May 2024 15:27:30 -0600 Subject: [PATCH 2/5] Fix torch.linalg.solve to handle batching correctly according to the standard --- array_api_compat/torch/linalg.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/array_api_compat/torch/linalg.py b/array_api_compat/torch/linalg.py index 41d16519..e26198b9 100644 --- a/array_api_compat/torch/linalg.py +++ b/array_api_compat/torch/linalg.py @@ -60,6 +60,22 @@ def vecdot(x1: array, x2: array, /, *, axis: int = -1, **kwargs) -> array: def solve(x1: array, x2: array, /, **kwargs) -> array: x1, x2 = _fix_promotion(x1, x2, only_scalar=False) + # Torch tries to emulate NumPy 1 solve behavior by using batched 1-D solve + # whenever + # 1. x1.ndim - 1 == x2.ndim + # 2. x1.shape[:-1] == x2.shape + # + # See linalg_solve_is_vector_rhs in + # aten/src/ATen/native/LinearAlgebraUtils.h and + # TORCH_META_FUNC(_linalg_solve_ex) in + # aten/src/ATen/native/BatchLinearAlgebra.cpp in the PyTorch source code. + # + # The easiest way to work around this is to prepend a size 1 dimension to + # x2, since x2 is already one dimension less than x1. + # + # See https://github.com/pytorch/pytorch/issues/52915 + if x2.ndim != 1 and x1.ndim - 1 == x2.ndim and x1.shape[:-1] == x2.shape: + x2 = x2[None] return torch.linalg.solve(x1, x2, **kwargs) # torch.trace doesn't support the offset argument and doesn't support stacking From 0d1d925d71233b75f4cec97be35459fd952dccce Mon Sep 17 00:00:00 2001 From: Aaron Meurer Date: Thu, 23 May 2024 15:36:59 -0600 Subject: [PATCH 3/5] Allow setting extra environment variables in the array api tests workflow --- .github/workflows/array-api-tests.yml | 73 ++++++++++++++------------- 1 file changed, 38 insertions(+), 35 deletions(-) diff --git a/.github/workflows/array-api-tests.yml b/.github/workflows/array-api-tests.yml index c1c709a7..62f77b1d 100644 --- a/.github/workflows/array-api-tests.yml +++ b/.github/workflows/array-api-tests.yml @@ -27,7 +27,10 @@ on: skips-file-extra: required: false type: string - + extra-env-vars: + required: false + type: string + description: 'Extra environment variables to set during the test run' env: PYTEST_ARGS: "--max-examples 200 -v -rxXfE --ci ${{ inputs.pytest-extra-args }} --hypothesis-disable-deadline" @@ -38,38 +41,38 @@ jobs: strategy: matrix: python-version: ['3.9', '3.10', '3.11', '3.12'] - steps: - - name: Checkout array-api-compat - uses: actions/checkout@v4 - with: - path: array-api-compat - - name: Checkout array-api-tests - uses: actions/checkout@v4 - with: - repository: data-apis/array-api-tests - submodules: 'true' - path: array-api-tests - - name: Set up Python ${{ matrix.python-version }} - uses: actions/setup-python@v5 - with: - python-version: ${{ matrix.python-version }} - - name: Install dependencies - # NumPy 1.21 doesn't support Python 3.11. There doesn't seem to be a way - # to put this in the numpy 1.21 config file. - if: "! ((matrix.python-version == '3.11' || matrix.python-version == '3.12') && inputs.package-name == 'numpy' && contains(inputs.package-version, '1.21'))" - run: | - python -m pip install --upgrade pip - python -m pip install '${{ inputs.package-name }} ${{ inputs.package-version }}' ${{ inputs.extra-requires }} - python -m pip install -r ${GITHUB_WORKSPACE}/array-api-tests/requirements.txt - - name: Run the array API testsuite (${{ inputs.package-name }}) - if: "! ((matrix.python-version == '3.11' || matrix.python-version == '3.12') && inputs.package-name == 'numpy' && contains(inputs.package-version, '1.21'))" - env: - ARRAY_API_TESTS_MODULE: array_api_compat.${{ inputs.module-name || inputs.package-name }} - # This enables the NEP 50 type promotion behavior (without it a lot of - # tests fail on bad scalar type promotion behavior) - NPY_PROMOTION_STATE: weak - run: | - export PYTHONPATH="${GITHUB_WORKSPACE}/array-api-compat" - cd ${GITHUB_WORKSPACE}/array-api-tests - pytest array_api_tests/ --xfails-file ${GITHUB_WORKSPACE}/array-api-compat/${{ inputs.package-name }}${{ inputs.xfails-file-extra }}-xfails.txt --skips-file ${GITHUB_WORKSPACE}/array-api-compat/${{ inputs.package-name }}${{ inputs.skips-file-extra}}-skips.txt ${PYTEST_ARGS} + - name: Checkout array-api-compat + uses: actions/checkout@v4 + with: + path: array-api-compat + - name: Checkout array-api-tests + uses: actions/checkout@v4 + with: + repository: data-apis/array-api-tests + submodules: 'true' + path: array-api-tests + - name: Set up Python ${{ matrix.python-version }} + uses: actions/setup-python@v5 + with: + python-version: ${{ matrix.python-version }} + - name: Install dependencies + # NumPy 1.21 doesn't support Python 3.11. There doesn't seem to be a way + # to put this in the numpy 1.21 config file. + if: "! ((matrix.python-version == '3.11' || matrix.python-version == '3.12') && inputs.package-name == 'numpy' && contains(inputs.package-version, '1.21'))" + run: | + python -m pip install --upgrade pip + python -m pip install '${{ inputs.package-name }} ${{ inputs.package-version }}' ${{ inputs.extra-requires }} + python -m pip install -r ${GITHUB_WORKSPACE}/array-api-tests/requirements.txt + - name: Run the array API testsuite (${{ inputs.package-name }}) + if: "! ((matrix.python-version == '3.11' || matrix.python-version == '3.12') && inputs.package-name == 'numpy' && contains(inputs.package-version, '1.21'))" + env: + ARRAY_API_TESTS_MODULE: array_api_compat.${{ inputs.module-name || inputs.package-name }} + # This enables the NEP 50 type promotion behavior (without it a lot of + # tests fail on bad scalar type promotion behavior) + NPY_PROMOTION_STATE: weak + ${{ inputs.extra-env-vars }} + run: | + export PYTHONPATH="${GITHUB_WORKSPACE}/array-api-compat" + cd ${GITHUB_WORKSPACE}/array-api-tests + pytest array_api_tests/ --xfails-file ${GITHUB_WORKSPACE}/array-api-compat/${{ inputs.package-name }}${{ inputs.xfails-file-extra }}-xfails.txt --skips-file ${GITHUB_WORKSPACE}/array-api-compat/${{ inputs.package-name }}${{ inputs.skips-file-extra}}-skips.txt ${PYTEST_ARGS} \ No newline at end of file From a13ee1b3840fc0007b54e15e6bfa122e7b84253c Mon Sep 17 00:00:00 2001 From: Aaron Meurer Date: Thu, 23 May 2024 15:37:58 -0600 Subject: [PATCH 4/5] Set ARRAY_API_TESTS_SKIP_DTYPES in the torch array-api-tests run --- .github/workflows/array-api-tests-torch.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.github/workflows/array-api-tests-torch.yml b/.github/workflows/array-api-tests-torch.yml index 3f87ecae..fa753166 100644 --- a/.github/workflows/array-api-tests-torch.yml +++ b/.github/workflows/array-api-tests-torch.yml @@ -10,3 +10,5 @@ jobs: # Proper linalg testing will require # https://github.com/data-apis/array-api-tests/pull/101 pytest-extra-args: "--disable-extension linalg" + extra-env-vars: | + ARRAY_API_TESTS_SKIP_DTYPES: uint16,uint32,uint64 From 3eb826dcb41ecc5b6caa2dc87269cf361c497c3c Mon Sep 17 00:00:00 2001 From: Aaron Meurer Date: Thu, 23 May 2024 15:41:12 -0600 Subject: [PATCH 5/5] Use a better method of setting environment variables in the GitHub actions file --- .github/workflows/array-api-tests-torch.yml | 2 +- .github/workflows/array-api-tests.yml | 75 +++++++++++---------- 2 files changed, 41 insertions(+), 36 deletions(-) diff --git a/.github/workflows/array-api-tests-torch.yml b/.github/workflows/array-api-tests-torch.yml index fa753166..98234ae2 100644 --- a/.github/workflows/array-api-tests-torch.yml +++ b/.github/workflows/array-api-tests-torch.yml @@ -11,4 +11,4 @@ jobs: # https://github.com/data-apis/array-api-tests/pull/101 pytest-extra-args: "--disable-extension linalg" extra-env-vars: | - ARRAY_API_TESTS_SKIP_DTYPES: uint16,uint32,uint64 + ARRAY_API_TESTS_SKIP_DTYPES=uint16,uint32,uint64 diff --git a/.github/workflows/array-api-tests.yml b/.github/workflows/array-api-tests.yml index 62f77b1d..6e709438 100644 --- a/.github/workflows/array-api-tests.yml +++ b/.github/workflows/array-api-tests.yml @@ -30,7 +30,7 @@ on: extra-env-vars: required: false type: string - description: 'Extra environment variables to set during the test run' + description: "Multiline string of environment variables to set for the test run." env: PYTEST_ARGS: "--max-examples 200 -v -rxXfE --ci ${{ inputs.pytest-extra-args }} --hypothesis-disable-deadline" @@ -41,38 +41,43 @@ jobs: strategy: matrix: python-version: ['3.9', '3.10', '3.11', '3.12'] + steps: - - name: Checkout array-api-compat - uses: actions/checkout@v4 - with: - path: array-api-compat - - name: Checkout array-api-tests - uses: actions/checkout@v4 - with: - repository: data-apis/array-api-tests - submodules: 'true' - path: array-api-tests - - name: Set up Python ${{ matrix.python-version }} - uses: actions/setup-python@v5 - with: - python-version: ${{ matrix.python-version }} - - name: Install dependencies - # NumPy 1.21 doesn't support Python 3.11. There doesn't seem to be a way - # to put this in the numpy 1.21 config file. - if: "! ((matrix.python-version == '3.11' || matrix.python-version == '3.12') && inputs.package-name == 'numpy' && contains(inputs.package-version, '1.21'))" - run: | - python -m pip install --upgrade pip - python -m pip install '${{ inputs.package-name }} ${{ inputs.package-version }}' ${{ inputs.extra-requires }} - python -m pip install -r ${GITHUB_WORKSPACE}/array-api-tests/requirements.txt - - name: Run the array API testsuite (${{ inputs.package-name }}) - if: "! ((matrix.python-version == '3.11' || matrix.python-version == '3.12') && inputs.package-name == 'numpy' && contains(inputs.package-version, '1.21'))" - env: - ARRAY_API_TESTS_MODULE: array_api_compat.${{ inputs.module-name || inputs.package-name }} - # This enables the NEP 50 type promotion behavior (without it a lot of - # tests fail on bad scalar type promotion behavior) - NPY_PROMOTION_STATE: weak - ${{ inputs.extra-env-vars }} - run: | - export PYTHONPATH="${GITHUB_WORKSPACE}/array-api-compat" - cd ${GITHUB_WORKSPACE}/array-api-tests - pytest array_api_tests/ --xfails-file ${GITHUB_WORKSPACE}/array-api-compat/${{ inputs.package-name }}${{ inputs.xfails-file-extra }}-xfails.txt --skips-file ${GITHUB_WORKSPACE}/array-api-compat/${{ inputs.package-name }}${{ inputs.skips-file-extra}}-skips.txt ${PYTEST_ARGS} \ No newline at end of file + - name: Checkout array-api-compat + uses: actions/checkout@v4 + with: + path: array-api-compat + - name: Checkout array-api-tests + uses: actions/checkout@v4 + with: + repository: data-apis/array-api-tests + submodules: 'true' + path: array-api-tests + - name: Set up Python ${{ matrix.python-version }} + uses: actions/setup-python@v5 + with: + python-version: ${{ matrix.python-version }} + - name: Set Extra Environment Variables + # Set additional environment variables if provided + if: inputs.extra-env-vars + run: | + echo "${{ inputs.extra-env-vars }}" >> $GITHUB_ENV + - name: Install dependencies + # NumPy 1.21 doesn't support Python 3.11. There doesn't seem to be a way + # to put this in the numpy 1.21 config file. + if: "! ((matrix.python-version == '3.11' || matrix.python-version == '3.12') && inputs.package-name == 'numpy' && contains(inputs.package-version, '1.21'))" + run: | + python -m pip install --upgrade pip + python -m pip install '${{ inputs.package-name }} ${{ inputs.package-version }}' ${{ inputs.extra-requires }} + python -m pip install -r ${GITHUB_WORKSPACE}/array-api-tests/requirements.txt + - name: Run the array API testsuite (${{ inputs.package-name }}) + if: "! ((matrix.python-version == '3.11' || matrix.python-version == '3.12') && inputs.package-name == 'numpy' && contains(inputs.package-version, '1.21'))" + env: + ARRAY_API_TESTS_MODULE: array_api_compat.${{ inputs.module-name || inputs.package-name }} + # This enables the NEP 50 type promotion behavior (without it a lot of + # tests fail on bad scalar type promotion behavior) + NPY_PROMOTION_STATE: weak + run: | + export PYTHONPATH="${GITHUB_WORKSPACE}/array-api-compat" + cd ${GITHUB_WORKSPACE}/array-api-tests + pytest array_api_tests/ --xfails-file ${GITHUB_WORKSPACE}/array-api-compat/${{ inputs.package-name }}${{ inputs.xfails-file-extra }}-xfails.txt --skips-file ${GITHUB_WORKSPACE}/array-api-compat/${{ inputs.package-name }}${{ inputs.skips-file-extra}}-skips.txt ${PYTEST_ARGS}