Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Loongarch: modify lsx optimization(25215PR) for newest branch, and add Qemu tests #27662

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

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
83 changes: 83 additions & 0 deletions 83 .github/workflows/linux_qemu.yml
Original file line number Diff line number Diff line change
Expand Up @@ -178,3 +178,86 @@ jobs:
'"


linux_loongarch64_qemu:
# To enable this workflow on a fork, comment out:
if: github.repository == 'numpy/numpy'
runs-on: ubuntu-24.04
continue-on-error: true
strategy:
fail-fast: false
matrix:
BUILD_PROP:
- [
"loongarch64",
"loongarch64-linux-gnu",
"loongcr.lcpu.dev/lcpu/debian:unstable-240513",
"-Dallow-noblas=true",
"test_kind or test_multiarray or test_simd or test_umath or test_ufunc",
"loong64"
]
env:
TOOLCHAIN_NAME: ${{ matrix.BUILD_PROP[1] }}
DOCKER_CONTAINER: ${{ matrix.BUILD_PROP[2] }}
MESON_OPTIONS: ${{ matrix.BUILD_PROP[3] }}
RUNTIME_TEST_FILTER: ${{ matrix.BUILD_PROP[4] }}
ARCH: ${{ matrix.BUILD_PROP[5] }}
TERM: xterm-256color

name: "${{ matrix.BUILD_PROP[0] }}"
steps:
- uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1
with:
submodules: recursive
fetch-tags: true

- name: Initialize binfmt_misc for qemu-user-static
run: |
docker run --rm --privileged loongcr.lcpu.dev/multiarch/archlinux --reset -p yes

- name: Cache docker container
uses: actions/cache@v4.1.2
id: container-cache
with:
path: ~/docker_${{ matrix.BUILD_PROP[1] }}
key: container-${{ runner.os }}-${{ matrix.BUILD_PROP[1] }}-${{ matrix.BUILD_PROP[2] }}-${{ hashFiles('requirements/build_requirements.txt') }}

- name: Creates new container
if: steps.container-cache.outputs.cache-hit != 'true'
run: |
docker run --platform=linux/${ARCH} --name the_container --interactive \
-v /:/host -v $(pwd):/numpy ${DOCKER_CONTAINER} /bin/bash -c "
sed -i 's|URIs: http://mirrors.cloud.tencent.com/debian-ports|URIs: https://deb.debian.org/debian-ports|' /etc/apt/sources.list.d/debian.sources &&
apt update &&
apt install -y cmake cmake-data git python3 python-is-python3 python3-dev python3-pip gfortran &&
git config --global --add safe.directory /numpy &&
python -m pip install --break-system-packages -r /numpy/requirements/build_requirements.txt &&
python -m pip install --break-system-packages pytest pytest-xdist hypothesis typing_extensions
"
docker commit the_container the_container
mkdir -p "~/docker_${TOOLCHAIN_NAME}"
docker save -o "~/docker_${TOOLCHAIN_NAME}/the_container.tar" the_container

- name: Load container from cache
if: steps.container-cache.outputs.cache-hit == 'true'
run: docker load -i "~/docker_${TOOLCHAIN_NAME}/the_container.tar"

- name: Meson Build
run: |
docker run --rm --platform=linux/${ARCH} -e "TERM=xterm-256color" \
-v $(pwd):/numpy -v /:/host the_container \
/bin/script -e -q -c "/bin/bash --noprofile --norc -eo pipefail -c '
cd /numpy && spin build --clean -- ${MESON_OPTIONS}
'"

- name: Meson Log
if: always()
run: 'cat build/meson-logs/meson-log.txt'

- name: Run Tests
run: |
docker run --rm --platform=linux/${ARCH} -e "TERM=xterm-256color" \
-v $(pwd):/numpy -v /:/host the_container \
/bin/script -e -q -c "/bin/bash --noprofile --norc -eo pipefail -c '
cd /numpy && spin test -- -k \"${RUNTIME_TEST_FILTER}\"
'"

1 change: 1 addition & 0 deletions 1 meson.options
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ option('test-simd', type: 'array',
'VSX', 'VSX2', 'VSX3', 'VSX4',
'NEON', 'ASIMD',
'VX', 'VXE', 'VXE2',
'LSX',
],
description: 'Specify a list of CPU features to be tested against NumPy SIMD interface')
option('test-simd-args', type: 'string', value: '',
Expand Down
8 changes: 8 additions & 0 deletions 8 meson_cpu/loongarch64/meson.build
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
source_root = meson.project_source_root()
mod_features = import('features')

LSX = mod_features.new(
'LSX', 1, args: ['-mlsx'],
test_code: files(source_root + '/numpy/distutils/checks/cpu_lsx.c')[0]
)
LOONGARCH64_FEATURES = {'LSX': LSX}
4 changes: 4 additions & 0 deletions 4 meson_cpu/main_config.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -389,4 +389,8 @@
#ifdef @P@HAVE_RVV
#include <riscv_vector.h>
#endif

#ifdef @P@HAVE_LSX
#include <lsxintrin.h>
#endif
#endif // @P@_CPU_DISPATCHER_CONF_H_
4 changes: 4 additions & 0 deletions 4 meson_cpu/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,15 @@ subdir('ppc64')
subdir('s390x')
subdir('arm')
subdir('riscv64')
subdir('loongarch64')

CPU_FEATURES = {}
CPU_FEATURES += ARM_FEATURES
CPU_FEATURES += X86_FEATURES
CPU_FEATURES += PPC64_FEATURES
CPU_FEATURES += S390X_FEATURES
CPU_FEATURES += RV64_FEATURES
CPU_FEATURES += LOONGARCH64_FEATURES

# Parse the requested baseline (CPU_CONF_BASELINE) and dispatch features
# (CPU_CONF_DISPATCH).
Expand All @@ -97,6 +99,7 @@ min_features = {
'aarch64': [ASIMD],
'riscv64': [],
'wasm32': [],
'loongarch64': [LSX],
}.get(cpu_family, [])
if host_machine.endian() == 'little' and cpu_family == 'ppc64'
min_features = [VSX2]
Expand All @@ -112,6 +115,7 @@ max_features_dict = {
'aarch64': ARM_FEATURES,
'riscv64': RV64_FEATURES,
'wasm32': {},
'loongarch64': LOONGARCH64_FEATURES,
}.get(cpu_family, {})
max_features = []
foreach fet_name, fet_obj : max_features_dict
Expand Down
4 changes: 2 additions & 2 deletions 4 numpy/_core/include/numpy/npy_cpu.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,8 @@
#elif __riscv_xlen == 32
#define NPY_CPU_RISCV32
#endif
#elif defined(__loongarch__)
#define NPY_CPU_LOONGARCH
#elif defined(__loongarch64)
#define NPY_CPU_LOONGARCH64
#elif defined(__EMSCRIPTEN__)
/* __EMSCRIPTEN__ is defined by emscripten: an LLVM-to-Web compiler */
#define NPY_CPU_WASM
Expand Down
1 change: 1 addition & 0 deletions 1 numpy/_core/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -890,6 +890,7 @@ foreach gen_mtargets : [
NEON,
VSX4, VSX2,
VX,
LSX,
]
],
[
Expand Down
22 changes: 21 additions & 1 deletion 22 numpy/_core/src/common/npy_cpu_features.c
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,8 @@ static struct {
{NPY_CPU_FEATURE_ASIMDDP, "ASIMDDP"},
{NPY_CPU_FEATURE_ASIMDFHM, "ASIMDFHM"},
{NPY_CPU_FEATURE_SVE, "SVE"},
{NPY_CPU_FEATURE_RVV, "RVV"}};
{NPY_CPU_FEATURE_RVV, "RVV"},
{NPY_CPU_FEATURE_LSX, "LSX"}};


NPY_VISIBILITY_HIDDEN PyObject *
Expand Down Expand Up @@ -665,6 +666,25 @@ npy__cpu_init_features(void)
npy__cpu_have[NPY_CPU_FEATURE_VX] = 1;
}

/***************** LoongArch ******************/

#elif defined(__loongarch64)

#include <sys/auxv.h>
#include <asm/hwcap.h>

static void
npy__cpu_init_features(void)
{
memset(npy__cpu_have, 0, sizeof(npy__cpu_have[0]) * NPY_CPU_FEATURE_MAX);
unsigned int hwcap = getauxval(AT_HWCAP);

if ((hwcap & HWCAP_LOONGARCH_LSX)) {
npy__cpu_have[NPY_CPU_FEATURE_LSX] = 1;
return;
}
}


/***************** ARM ******************/

Expand Down
11 changes: 7 additions & 4 deletions 11 numpy/_core/src/common/npy_cpu_features.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ enum npy_cpu_features

// IBM/ZARCH
NPY_CPU_FEATURE_VX = 350,

// Vector-Enhancements Facility 1
NPY_CPU_FEATURE_VXE = 351,

Expand All @@ -101,6 +101,9 @@ enum npy_cpu_features
// RISC-V
NPY_CPU_FEATURE_RVV = 400,

// LOONGARCH
NPY_CPU_FEATURE_LSX = 500,

NPY_CPU_FEATURE_MAX
};

Expand All @@ -113,7 +116,7 @@ enum npy_cpu_features
* - uses 'NPY_DISABLE_CPU_FEATURES' to disable dispatchable features
* - uses 'NPY_ENABLE_CPU_FEATURES' to enable dispatchable features
*
* It will set a RuntimeError when
* It will set a RuntimeError when
* - CPU baseline features from the build are not supported at runtime
* - 'NPY_DISABLE_CPU_FEATURES' tries to disable a baseline feature
* - 'NPY_DISABLE_CPU_FEATURES' and 'NPY_ENABLE_CPU_FEATURES' are
Expand All @@ -122,14 +125,14 @@ enum npy_cpu_features
* by the machine or build
* - 'NPY_ENABLE_CPU_FEATURES' tries to enable a feature when the project was
* not built with any feature optimization support
*
*
* It will set an ImportWarning when:
* - 'NPY_DISABLE_CPU_FEATURES' tries to disable a feature that is not supported
* by the machine or build
* - 'NPY_DISABLE_CPU_FEATURES' or 'NPY_ENABLE_CPU_FEATURES' tries to
* disable/enable a feature when the project was not built with any feature
* optimization support
*
*
* return 0 on success otherwise return -1
*/
NPY_VISIBILITY_HIDDEN int
Expand Down
21 changes: 20 additions & 1 deletion 21 numpy/_core/src/common/simd/intdiv.h
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,10 @@ NPY_FINLINE npyv_u8x3 npyv_divisor_u8(npy_uint8 d)
divisor.val[0] = npyv_setall_u8(m);
divisor.val[1] = npyv_reinterpret_u8_s8(npyv_setall_s8(-sh1));
divisor.val[2] = npyv_reinterpret_u8_s8(npyv_setall_s8(-sh2));
#elif defined(NPY_HAVE_LSX)
divisor.val[0] = npyv_setall_u16(m);
divisor.val[1] = npyv_setall_u8(sh1);
divisor.val[2] = npyv_setall_u8(sh2);
#else
#error "please initialize the shifting operand for the new architecture"
#endif
Expand All @@ -225,7 +229,7 @@ NPY_FINLINE npyv_u8x3 npyv_divisor_u8(npy_uint8 d)
NPY_FINLINE npyv_s16x3 npyv_divisor_s16(npy_int16 d);
NPY_FINLINE npyv_s8x3 npyv_divisor_s8(npy_int8 d)
{
#ifdef NPY_HAVE_SSE2 // SSE/AVX2/AVX512
#if defined(NPY_HAVE_SSE2) || defined(NPY_HAVE_LSX) // SSE/AVX2/AVX512
npyv_s16x3 p = npyv_divisor_s16(d);
npyv_s8x3 r;
r.val[0] = npyv_reinterpret_s8_s16(p.val[0]);
Expand Down Expand Up @@ -291,6 +295,9 @@ NPY_FINLINE npyv_u16x3 npyv_divisor_u16(npy_uint16 d)
#elif defined(NPY_HAVE_NEON)
divisor.val[1] = npyv_reinterpret_u16_s16(npyv_setall_s16(-sh1));
divisor.val[2] = npyv_reinterpret_u16_s16(npyv_setall_s16(-sh2));
#elif defined(NPY_HAVE_LSX)
divisor.val[1] = npyv_setall_u16(sh1);
divisor.val[2] = npyv_setall_u16(sh2);
#else
#error "please initialize the shifting operand for the new architecture"
#endif
Expand Down Expand Up @@ -321,6 +328,8 @@ NPY_FINLINE npyv_s16x3 npyv_divisor_s16(npy_int16 d)
divisor.val[1] = npyv_setall_s16(sh);
#elif defined(NPY_HAVE_NEON)
divisor.val[1] = npyv_setall_s16(-sh);
#elif defined(NPY_HAVE_LSX)
divisor.val[1] = npyv_setall_s16(sh);
#else
#error "please initialize the shifting operand for the new architecture"
#endif
Expand Down Expand Up @@ -358,6 +367,9 @@ NPY_FINLINE npyv_u32x3 npyv_divisor_u32(npy_uint32 d)
#elif defined(NPY_HAVE_NEON)
divisor.val[1] = npyv_reinterpret_u32_s32(npyv_setall_s32(-sh1));
divisor.val[2] = npyv_reinterpret_u32_s32(npyv_setall_s32(-sh2));
#elif defined(NPY_HAVE_LSX)
divisor.val[1] = npyv_setall_u32(sh1);
divisor.val[2] = npyv_setall_u32(sh2);
#else
#error "please initialize the shifting operand for the new architecture"
#endif
Expand Down Expand Up @@ -393,6 +405,8 @@ NPY_FINLINE npyv_s32x3 npyv_divisor_s32(npy_int32 d)
divisor.val[1] = npyv_setall_s32(sh);
#elif defined(NPY_HAVE_NEON)
divisor.val[1] = npyv_setall_s32(-sh);
#elif defined(NPY_HAVE_LSX)
divisor.val[1] = npyv_setall_s32(sh);
#else
#error "please initialize the shifting operand for the new architecture"
#endif
Expand Down Expand Up @@ -427,6 +441,9 @@ NPY_FINLINE npyv_u64x3 npyv_divisor_u64(npy_uint64 d)
#ifdef NPY_HAVE_SSE2 // SSE/AVX2/AVX512
divisor.val[1] = npyv_set_u64(sh1);
divisor.val[2] = npyv_set_u64(sh2);
#elif defined(NPY_HAVE_LSX)
divisor.val[1] = npyv_setall_u64(sh1);
divisor.val[2] = npyv_setall_u64(sh2);
#else
#error "please initialize the shifting operand for the new architecture"
#endif
Expand Down Expand Up @@ -465,6 +482,8 @@ NPY_FINLINE npyv_s64x3 npyv_divisor_s64(npy_int64 d)
divisor.val[2] = npyv_setall_s64(d < 0 ? -1 : 0); // sign of divisor
#ifdef NPY_HAVE_SSE2 // SSE/AVX2/AVX512
divisor.val[1] = npyv_set_s64(sh);
#elif defined(NPY_HAVE_LSX)
divisor.val[1] = npyv_setall_s64(sh);
#else
#error "please initialize the shifting operand for the new architecture"
#endif
Expand Down
Loading
Loading
Morty Proxy This is a proxified and sanitized view of the page, visit original site.