Add test-simd package to validate WASM/SSE/AVX intrinsics #5880
Add test-simd package to validate WASM/SSE/AVX intrinsics #5880ryanking13 merged 12 commits intopyodide:mainfrom
Conversation
for more information, see https://pre-commit.ci
ryanking13
left a comment
There was a problem hiding this comment.
Thanks for working on this @teddygood! Overall it looks good, but I left some tips for creating a test package for this.
packages/test-simd/meta.yaml
Outdated
| - library | ||
| - shared_library | ||
| source: | ||
| path: src | ||
| build: | ||
| type: shared_library |
There was a problem hiding this comment.
Instead of making a shared_library, let's write a Python package that imports the shared library. It is much easy to test and read, while it adds a little bit of more code.
You can refer to the dummy-nonpure package to see how you can write a Python module that is written in C.
- I think you can keep the existing implementation, while making a wrapper Python function.
For example, the Python function can be defined such as:
// Python wrapper for simd_avx_add8_sum
static PyObject *
simd_avx_add8_sum(PyObject *self, PyObject *args)
{
float a0, a1, a2, a3, b0, b1, b2, b3;
if (!PyArg_ParseTuple(args, "ffffffff", &a0, &a1, &a2, &a3, &b0, &b1, &b2, &b3))
return NULL;
float result = simd_avx_add8_sum_impl(a0, a1, a2, a3, b0, b1, b2, b3);
return Py_BuildValue("f", result);
}- The build script can also be moved to
setup.py, such as
import os
from setuptools import setup, Extension
module = Extension(
'test_simd.simd_wrapper',
sources=[os.path.join('test_simd', 'simd_avx.c')],
extra_compile_args=['-mavx', '-msimd128'],
)- Then it can be tested such as
@run_in_pyodide(packages=["test-simd"])
def test_simd_functions(selenium):
from test_simd import simd_wrapper
a = (1.0, 2.0, 3.0, 4.0)
b = (5.0, 6.0, 7.0, 8.0)
simd_wrapper.avx_mul(*a, *b)(The code in this comment is not verified, so some details might be incorrect)
There was a problem hiding this comment.
Thank you for the review! I’ll make the changes and commit them.
packages/test-simd/meta.yaml
Outdated
| build: | ||
| type: cpython_module | ||
| script: python -m pip install . -v |
There was a problem hiding this comment.
| build: | |
| type: cpython_module | |
| script: python -m pip install . -v |
These are not needed.
cpython_moduletype is for unvendored CPython stdlibs- build script will automatically run for normal Python packages.
There was a problem hiding this comment.
Thanks, I’ll update it.
|
I learned something again today. Thank you... |
| tag: | ||
| - pyodide.test |
There was a problem hiding this comment.
| tag: | |
| - pyodide.test | |
| tag: | |
| - core | |
| - pyodide.test |
Could you add tag: core as well? It seems like this package is not tested in CI because of a missing tag.
There was a problem hiding this comment.
Yes, I’ll add it right away.
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Description
resolve #5855
wasm_simd128.h, f32x4 add/mul)_mm_add_ps,_mm_mul_ps, f32x4)_mm_add_pd,_mm_mul_pd, f64x2)_mm256_add_ps,_mm256_mul_ps, f32x8, using duplicated 128b halves)pytestintegration to dynamically load the.solibraries viapyodide.loadPackage("test-simd")and compare scalar results against expected values.For the AVX case, WebAssembly SIMD only works with 128-bit vectors.
Emscripten converts each 256-bit AVX instruction into two 128-bit ones.
In our test, we copied the lower 128 bits into the upper half to fill the 256-bit register.
Because of this, the scalar sum equals twice the SSE result. This shows that the upper 128 bits are used and the lowering works as expected.
Checklist