GeezerVison is showing github.com — ENH: Adopt new macOS Accelerate BLAS/LAPACK Interfaces, including ILP… · charris/numpy@93c40e5
File treeExpand file treeCollapse file tree Open diff view settings doc/release/upcoming_changes
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboard +135Lines changed: 135 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +name: macOS tests (meson) | | 2 | + | | 3 | +on: | | 4 | + pull_request: | | 5 | + branches: | | 6 | + - main | | 7 | + - maintenance/** | | 8 | + | | 9 | +permissions: | | 10 | + contents: read # to fetch code (actions/checkout) | | 11 | + | | 12 | +env: | | 13 | + CCACHE_DIR: "${{ github.workspace }}/.ccache" | | 14 | + | | 15 | +concurrency: | | 16 | + group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }} | | 17 | + cancel-in-progress: true | | 18 | + | | 19 | +jobs: | | 20 | + x86_conda: | | 21 | + name: macOS x86-64 conda | | 22 | + if: "github.repository == 'numpy/numpy'" | | 23 | + runs-on: macos-latest | | 24 | + strategy: | | 25 | + matrix: | | 26 | + python-version: ["3.11"] | | 27 | + | | 28 | + steps: | | 29 | + - uses: actions/checkout@f43a0e5ff2bd294095638e18286ca9a3d1956744 # v3.6.0 | | 30 | + with: | | 31 | + submodules: recursive | | 32 | + fetch-depth: 0 | | 33 | + | | 34 | + - name: Prepare cache dirs and timestamps | | 35 | + id: prep-ccache | | 36 | + shell: bash -l {0} | | 37 | + run: | | | 38 | + mkdir -p "${CCACHE_DIR}" | | 39 | + echo "dir=$CCACHE_DIR" >> $GITHUB_OUTPUT | | 40 | + NOW=$(date -u +"%F-%T") | | 41 | + echo "timestamp=${NOW}" >> $GITHUB_OUTPUT | | 42 | + echo "today=$(/bin/date -u '+%Y%m%d')" >> $GITHUB_OUTPUT | | 43 | +
| | 44 | + - name: Setup compiler cache | | 45 | + uses: actions/cache@88522ab9f39a2ea568f7027eddc7d8d8bc9d59c8 # v3.3.1 | | 46 | + id: cache-ccache | | 47 | + with: | | 48 | + path: ${{ steps.prep-ccache.outputs.dir }} | | 49 | + key: ${{ github.workflow }}-${{ matrix.python-version }}-ccache-macos-${{ steps.prep-ccache.outputs.timestamp }} | | 50 | + restore-keys: | | | 51 | + ${{ github.workflow }}-${{ matrix.python-version }}-ccache-macos- | | 52 | +
| | 53 | + - name: Setup Mambaforge | | 54 | + uses: conda-incubator/setup-miniconda@3b0f2504dd76ef23b6d31f291f4913fb60ab5ff3 # v2.2.0 | | 55 | + with: | | 56 | + python-version: ${{ matrix.python-version }} | | 57 | + channels: conda-forge | | 58 | + channel-priority: true | | 59 | + activate-environment: numpy-dev | | 60 | + use-only-tar-bz2: false | | 61 | + miniforge-variant: Mambaforge | | 62 | + miniforge-version: latest | | 63 | + use-mamba: true | | 64 | + | | 65 | + # Updates if `environment.yml` or the date changes. The latter is needed to | | 66 | + # ensure we re-solve once a day (since we don't lock versions). Could be | | 67 | + # replaced by a conda-lock based approach in the future. | | 68 | + - name: Cache conda environment | | 69 | + uses: actions/cache@88522ab9f39a2ea568f7027eddc7d8d8bc9d59c8 # v3.3.1 | | 70 | + env: | | 71 | + # Increase this value to reset cache if environment.yml has not changed | | 72 | + CACHE_NUMBER: 1 | | 73 | + with: | | 74 | + path: ${{ env.CONDA }}/envs/numpy-dev | | 75 | + key: | | 76 | + ${{ runner.os }}--${{ steps.prep-ccache.outputs.today }}-conda-${{ env.CACHE_NUMBER }}-${{ hashFiles('environment.yml') }} | | 77 | + id: envcache | | 78 | + | | 79 | + - name: Update Conda Environment | | 80 | + run: mamba env update -n numpy-dev -f environment.yml | | 81 | + if: steps.envcache.outputs.cache-hit != 'true' | | 82 | + | | 83 | + - name: Build and Install NumPy | | 84 | + shell: bash -l {0} | | 85 | + run: | | | 86 | + conda activate numpy-dev | | 87 | + CC="ccache $CC" spin build -j2 | | 88 | +
| | 89 | + - name: Run test suite (full) | | 90 | + shell: bash -l {0} | | 91 | + run: | | | 92 | + conda activate numpy-dev | | 93 | + export OMP_NUM_THREADS=2 | | 94 | + spin test -j2 -m full | | 95 | +
| | 96 | + - name: Ccache statistics | | 97 | + shell: bash -l {0} | | 98 | + run: | | | 99 | + conda activate numpy-dev | | 100 | + ccache -s | | 101 | +
| | 102 | + accelerate: | | 103 | + name: Accelerate ILP64 | | 104 | + if: "github.repository == 'numpy/numpy'" | | 105 | + runs-on: macos-13 | | 106 | + steps: | | 107 | + - uses: actions/checkout@f43a0e5ff2bd294095638e18286ca9a3d1956744 # v3.6.0 | | 108 | + with: | | 109 | + submodules: recursive | | 110 | + fetch-depth: 0 | | 111 | + | | 112 | + - uses: actions/setup-python@61a6322f88396a6271a6ee3565807d608ecaddd1 # v4.7.0 | | 113 | + with: | | 114 | + python-version: '3.10' | | 115 | + | | 116 | + - uses: maxim-lobanov/setup-xcode@9a697e2b393340c3cacd97468baa318e4c883d98 # v1.5.1 | | 117 | + with: | | 118 | + xcode-version: '14.3' | | 119 | + | | 120 | + - name: Install dependencies | | 121 | + run: | | | 122 | + pip install -r build_requirements.txt | | 123 | + pip install pytest pytest-xdist hypothesis | | 124 | +
| | 125 | + - name: Build NumPy against Accelerate (ILP64) | | 126 | + run: | | | 127 | + spin build -- -Dblas=accelerate -Dlapack=accelerate -Duse-ilp64=true | | 128 | +
| | 129 | + - name: Show meson-log.txt | | 130 | + if: always() | | 131 | + run: 'cat build/meson-logs/meson-log.txt' | | 132 | + | | 133 | + - name: Test | | 134 | + run: | | | 135 | + spin test -j2 |
Collapse file Copy file name to clipboard +5Lines changed: 5 additions & 0 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +Support for the updated Accelerate BLAS/LAPACK library, including ILP64 (64-bit | | 2 | +integer) support, in macOS 13.3 has been added. This brings arm64 support, and | | 3 | +significant performance improvements of up to 10x for commonly used linear | | 4 | +algebra operations. When Accelerate is selected at build time, the 13.3+ | | 5 | +version will automatically be used if available. |
Collapse file Copy file name to clipboardExpand all lines: numpy/core/src/common/npy_cblas.h +15Lines changed: 15 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -25,6 +25,21 @@ enum CBLAS_SIDE {CblasLeft=141, CblasRight=142}; | 25 | 25 |
| 26 | 26 | #define CBLAS_INDEX size_t /* this may vary between platforms */ | 27 | 27 |
| | 28 | +#ifdef ACCELERATE_NEW_LAPACK | | 29 | + #if __MAC_OS_X_VERSION_MAX_ALLOWED < 130300 | | 30 | + #ifdef HAVE_BLAS_ILP64 | | 31 | + #error "Accelerate ILP64 support is only available with macOS 13.3 SDK or later" | | 32 | + #endif | | 33 | + #else | | 34 | + #define NO_APPEND_FORTRAN | | 35 | + #ifdef HAVE_BLAS_ILP64 | | 36 | + #define BLAS_SYMBOL_SUFFIX $NEWLAPACK$ILP64 | | 37 | + #else | | 38 | + #define BLAS_SYMBOL_SUFFIX $NEWLAPACK | | 39 | + #endif | | 40 | + #endif | | 41 | +#endif | | 42 | + | 28 | 43 | #ifdef NO_APPEND_FORTRAN | 29 | 44 | #define BLAS_FORTRAN_SUFFIX | 30 | 45 | #else | |
Collapse file Copy file name to clipboardExpand all lines: numpy/distutils/system_info.py +23-4Lines changed: 23 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 47 | 47 | _numpy_info:Numeric | 48 | 48 | _pkg_config_info:None | 49 | 49 | accelerate_info:accelerate | | 50 | + accelerate_lapack_info:accelerate | 50 | 51 | agg2_info:agg2 | 51 | 52 | amd_info:amd | 52 | 53 | atlas_3_10_blas_info:atlas | @@ -534,6 +535,7 @@ def get_info(name, notfound_action=0): | 534 | 535 | 'lapack_ssl2': lapack_ssl2_info, | 535 | 536 | 'blas_ssl2': blas_ssl2_info, | 536 | 537 | 'accelerate': accelerate_info, # use blas_opt instead | | 538 | + 'accelerate_lapack': accelerate_lapack_info, | 537 | 539 | 'openblas64_': openblas64__info, | 538 | 540 | 'openblas64__lapack': openblas64__lapack_info, | 539 | 541 | 'openblas_ilp64': openblas_ilp64_info, | @@ -2015,14 +2017,17 @@ def _check_info(self, info): | 2015 | 2017 |
| 2016 | 2018 | class lapack_ilp64_opt_info(lapack_opt_info, _ilp64_opt_info_mixin): | 2017 | 2019 | notfounderror = LapackILP64NotFoundError | 2018 | | - lapack_order = ['openblas64_', 'openblas_ilp64'] | | 2020 | + lapack_order = ['openblas64_', 'openblas_ilp64', 'accelerate'] | 2019 | 2021 | order_env_var_name = 'NPY_LAPACK_ILP64_ORDER' | 2020 | 2022 |
| 2021 | 2023 | def _calc_info(self, name): | | 2024 | + print('lapack_ilp64_opt_info._calc_info(name=%s)' % (name)) | 2022 | 2025 | info = get_info(name + '_lapack') | 2023 | 2026 | if self._check_info(info): | 2024 | 2027 | self.set_info(**info) | 2025 | 2028 | return True | | 2029 | + else: | | 2030 | + print('%s_lapack does not exist' % (name)) | 2026 | 2031 | return False | 2027 | 2032 |
| 2028 | 2033 |
| @@ -2163,7 +2168,7 @@ def calc_info(self): | 2163 | 2168 |
| 2164 | 2169 | class blas_ilp64_opt_info(blas_opt_info, _ilp64_opt_info_mixin): | 2165 | 2170 | notfounderror = BlasILP64NotFoundError | 2166 | | - blas_order = ['openblas64_', 'openblas_ilp64'] | | 2171 | + blas_order = ['openblas64_', 'openblas_ilp64', 'accelerate'] | 2167 | 2172 | order_env_var_name = 'NPY_BLAS_ILP64_ORDER' | 2168 | 2173 |
| 2169 | 2174 | def _calc_info(self, name): | @@ -2625,13 +2630,27 @@ def calc_info(self): | 2625 | 2630 | link_args.extend(['-Wl,-framework', '-Wl,vecLib']) | 2626 | 2631 |
| 2627 | 2632 | if args: | | 2633 | + macros = [ | | 2634 | + ('NO_ATLAS_INFO', 3), | | 2635 | + ('HAVE_CBLAS', None), | | 2636 | + ('ACCELERATE_NEW_LAPACK', None), | | 2637 | + ] | | 2638 | + if(os.getenv('NPY_USE_BLAS_ILP64', None)): | | 2639 | + print('Setting HAVE_BLAS_ILP64') | | 2640 | + macros += [ | | 2641 | + ('HAVE_BLAS_ILP64', None), | | 2642 | + ('ACCELERATE_LAPACK_ILP64', None), | | 2643 | + ] | 2628 | 2644 | self.set_info(extra_compile_args=args, | 2629 | 2645 | extra_link_args=link_args, | 2630 | | - define_macros=[('NO_ATLAS_INFO', 3), | 2631 | | - ('HAVE_CBLAS', None)]) | | 2646 | + define_macros=macros) | 2632 | 2647 |
| 2633 | 2648 | return | 2634 | 2649 |
| | 2650 | +class accelerate_lapack_info(accelerate_info): | | 2651 | + def _calc_info(self): | | 2652 | + return super()._calc_info() | | 2653 | + | 2635 | 2654 | class blas_src_info(system_info): | 2636 | 2655 | # BLAS_SRC is deprecated, please do not use this! | 2637 | 2656 | # Build or install a BLAS library via your package manager or from | |
Collapse file Copy file name to clipboardExpand all lines: numpy/linalg/meson.build +6-4Lines changed: 6 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | # Note that `python_xerbla.c` was excluded on Windows in setup.py; | 2 | 2 | # unclear why and it seems needed, so unconditionally used here. | 3 | | -lapack_lite_sources = [ | 4 | | - 'lapack_lite/python_xerbla.c', | 5 | | -] | | 3 | +python_xerbla_sources = ['lapack_lite/python_xerbla.c'] | | 4 | + | | 5 | +lapack_lite_sources = [] | 6 | 6 | if not have_lapack | 7 | | - lapack_lite_sources += [ | | 7 | + lapack_lite_sources = [ | 8 | 8 | 'lapack_lite/f2c.c', | 9 | 9 | 'lapack_lite/f2c_c_lapack.c', | 10 | 10 | 'lapack_lite/f2c_d_lapack.c', | | 19 | 19 | py.extension_module('lapack_lite', | 20 | 20 | [ | 21 | 21 | 'lapack_litemodule.c', | | 22 | + python_xerbla_sources, | 22 | 23 | lapack_lite_sources, | 23 | 24 | ], | 24 | 25 | dependencies: [np_core_dep, blas_dep, lapack_dep], | @@ -29,6 +30,7 @@ py.extension_module('lapack_lite', | 29 | 30 | py.extension_module('_umath_linalg', | 30 | 31 | [ | 31 | 32 | 'umath_linalg.cpp', | | 33 | + python_xerbla_sources, | 32 | 34 | lapack_lite_sources, | 33 | 35 | ], | 34 | 36 | dependencies: [np_core_dep, blas_dep, lapack_dep], | |
Collapse file Copy file name to clipboardExpand all lines: numpy/meson.build +55-4Lines changed: 55 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 48 | 48 | ] | 49 | 49 | endif | 50 | 50 |
| | 51 | +macOS13_3_or_later = false | | 52 | +if host_machine.system() == 'darwin' | | 53 | + r = run_command('xcrun', '-sdk', 'macosx', '--show-sdk-version', check: true) | | 54 | + sdkVersion = r.stdout().strip() | | 55 | + | | 56 | + macOS13_3_or_later = sdkVersion.version_compare('>=13.3') | | 57 | +endif | 51 | 58 |
| 52 | 59 | # This is currently injected directly into CFLAGS/CXXFLAGS for wheel builds | 53 | 60 | # (see cibuildwheel settings in pyproject.toml), but used by CI jobs already | | 81 | 88 | # https://github.com/mesonbuild/meson/issues/2835 | 82 | 89 | blas_name = get_option('blas') | 83 | 90 | lapack_name = get_option('lapack') | | 91 | + | 84 | 92 | # pkg-config uses a lower-case name while CMake uses a capitalized name, so try | 85 | 93 | # that too to make the fallback detection with CMake work | 86 | 94 | if blas_name == 'openblas' | @@ -90,6 +98,23 @@ if blas_name == 'openblas' | 90 | 98 | _openblas_names = ['openblas', 'OpenBLAS'] | 91 | 99 | endif | 92 | 100 | blas = dependency(_openblas_names, required: false) | | 101 | +elif blas_name.to_lower() == 'accelerate' | | 102 | + # macOS 13.3+ has updated interfaces aligned with BLAS/LAPACK 3.9.1. Use them if available. | | 103 | + if macOS13_3_or_later | | 104 | + accelerate_compile_args = ['-DACCELERATE_NEW_LAPACK'] | | 105 | + if(use_ilp64) | | 106 | + accelerate_compile_args += '-DACCELERATE_LAPACK_ILP64' | | 107 | + endif | | 108 | + blas = declare_dependency( | | 109 | + compile_args: accelerate_compile_args, | | 110 | + dependencies: dependency('Accelerate') | | 111 | + ) | | 112 | + else | | 113 | + if(use_ilp64) | | 114 | + error('macOS SDK 13.3+ is required for ILP64 support.') | | 115 | + endif | | 116 | + blas = dependency('Accelerate') | | 117 | + endif | 93 | 118 | else | 94 | 119 | blas = dependency(blas_name, required: false) | 95 | 120 | endif | @@ -112,14 +137,22 @@ if have_blas | 112 | 137 | # `dependency('blas', modules: cblas)` | 113 | 138 | # see https://github.com/mesonbuild/meson/pull/10921. | 114 | 139 | have_cblas = false | 115 | | - if cc.links(''' | | 140 | + if blas_name.to_lower() == 'accelerate' | | 141 | + _cblas_header = '<Accelerate/Accelerate.h>' | | 142 | + elif blas_name.to_lower().startswith('mkl') | | 143 | + _cblas_header = '<mkl_cblas.h>' | | 144 | + else | | 145 | + _cblas_header = '<cblas.h>' | | 146 | + endif | | 147 | + if cc.links(f''' | 116 | 148 | #ifndef BLAS_SYMBOL_SUFFIX | 117 | 149 | # define BLAS_SYMBOL_SUFFIX | 118 | 150 | #endif | 119 | 151 | #define EXPAND(suffix) cblas_ddot ## suffix | 120 | 152 | #define DDOT(suffix) EXPAND(suffix) | 121 | 153 |
| 122 | | - #include <cblas.h> | | 154 | + #include @_cblas_header@ | | 155 | +
| 123 | 156 | int main(int argc, const char *argv[]) | 124 | 157 | { | 125 | 158 | double a[4] = {1,2,3,4}; | @@ -178,9 +211,27 @@ else | 178 | 211 | endif | 179 | 212 |
| 180 | 213 | if lapack_name == 'openblas' | 181 | | - lapack_name = ['openblas', 'OpenBLAS'] | | 214 | + lapack_dep = dependency(['openblas', 'OpenBLAS'], required: false) | | 215 | +elif lapack_name.to_lower() == 'accelerate' | | 216 | + # macOS 13.3+ has updated interfaces aligned with BLAS/LAPACK 3.9.1. Use them if available. | | 217 | + if macOS13_3_or_later | | 218 | + accelerate_compile_args = ['-DACCELERATE_NEW_LAPACK'] | | 219 | + if(use_ilp64) | | 220 | + accelerate_compile_args += '-DACCELERATE_LAPACK_ILP64' | | 221 | + endif | | 222 | + lapack_dep = declare_dependency( | | 223 | + compile_args: accelerate_compile_args, | | 224 | + dependencies: dependency('Accelerate') | | 225 | + ) | | 226 | + else | | 227 | + if(use_ilp64) | | 228 | + error('macOS SDK 13.3+ is required for ILP64 support.') | | 229 | + endif | | 230 | + lapack_dep = dependency('Accelerate') | | 231 | + endif | | 232 | +else | | 233 | + lapack_dep = dependency(lapack_name, required: false) | 182 | 234 | endif | 183 | | -lapack_dep = dependency(lapack_name, required: false) | 184 | 235 | have_lapack = lapack_dep.found() | 185 | 236 | if not have_lapack and not allow_noblas | 186 | 237 | error('No LAPACK library detected! Install one, or use the ' + \ | |
|