8000 ENH: Improve the performance of einsum by using universal simd by Qiyu8 · Pull Request #17049 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

ENH: Improve the performance of einsum by using universal simd #17049

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 40 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
e26dcf7
new npyv intrinsics
Qiyu8 Aug 11, 2020
47118fb
einsum dispatch and usimd process
Qiyu8 Aug 11, 2020
ad0b3b4
update
Qiyu8 Aug 11, 2020
55200fc
add float32 benchmark case
Qiyu8 Aug 11, 2020
94cff77
Merge branch 'master' of github.com:numpy/numpy into einsum-usimd
Qiyu8 Aug 12, 2020
8000 4d7d94d
update
Qiyu8 Aug 12, 2020
ae53e35
fix typos
Qiyu8 Aug 12, 2020
2e713b0
add avx512 reduce sum comments
Qiyu8 Aug 13, 2020
5e7cbd1
add non_contigous arrays ,improve reduce the sum
Qiyu8 Aug 20, 2020
80c0ed4
Merge branch 'master' of github.com:numpy/numpy into einsum-usimd
Qiyu8 Aug 24, 2020
9060231
rebase after split for a better review
Qiyu8 Aug 24, 2020
b0375dc
Merge branch 'einsum-usimd' of github.com:Qiyu8/numpy into einsum-usimd
Qiyu8 Aug 25, 2020
1990c13
headers reconstruct
Qiyu8 Aug 25, 2020
7b756af
use for loop replace begin repeat for readability
Qiyu8 Aug 25, 2020
4877e40
add ivdeps and handle header dependency
Qiyu8 Aug 26, 2020
168c6c9
Merge branch 'einsum-usimd' of github.com:Qiyu8/numpy into einsum-usimd
Qiyu8 Aug 26, 2020
954e642
revert to faster simd code
Qiyu8 Aug 27, 2020
50c6b7e
changed to baseline solution
Qiyu8 Aug 28, 2020
23e28c0
remove redundant typedef
Qiyu8 Aug 31, 2020
21f1c0b
update
Qiyu8 Sep 1, 2020
a07455a
Merge branch 'master' of github.com:numpy/numpy into einsum-usimd
Qiyu8 Sep 10, 2020
d298c8e
remove redundant intrinsics
Qiyu8 Sep 10, 2020
6dac52e
add blank lines
Qiyu8 Sep 11, 2020
985e5b2
add format
Qiyu8 Sep 14, 2020
88c2747
Update numpy/core/src/common/simd/avx512/arithmetic.h
Qiyu8 Sep 14, 2020
90026f9
Merge branch 'master' of github.com:numpy/numpy into einsum-usimd
Qiyu8 Sep 15, 2020
54943e0
modify the int to npy_intp
Qiyu8 Sep 15, 2020
e993af2
split benchmark and define common macro
Qiyu8 Sep 18, 2020
38f7382
avx2 test
Qiyu8 Sep 18, 2020
f351665
Merge branch 'einsum-usimd' of github.com:Qiyu8/numpy into einsum-usimd
Qiyu8 Sep 18, 2020
c6c1e30
explain for auto-vectorize part
Qiyu8 Sep 18, 2020
f18ade4
add explantion
Qiyu8 Sep 18, 2020
33b7d2a
remove duplicated message
Qiyu8 Sep 19, 2020
5a692ed
Update benchmarks/benchmarks/bench_linalg.py
Qiyu8 Sep 29, 2020
20d5cda
Update numpy/core/src/multiarray/einsum_sumprod.c.src
Qiyu8 Sep 30, 2020
83734bf
Merge branch 'master' of github.com:numpy/numpy into einsum-usimd
Qiyu8 Oct 9, 2020
f8f7482
Merge branch 'einsum-usimd' of github.com:Qiyu8/numpy into einsum-usimd
Qiyu8 Oct 9, 2020
1889738
Merge branch 'master' of github.com:numpy/numpy into einsum-usimd
Qiyu8 Oct 12, 2020
7ff7324
fix typos
Qiyu8 Oct 12, 2020
73f61c3
remove extra test
Qiyu8 Oct 13, 2020
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
19 changes: 17 additions & 2 deletions benchmarks/benchmarks/bench_linalg.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,14 @@ class Einsum(Benchmark):
def setup(self, dtype):
self.a = np.arange(2900, dtype=dtype)
self.b = np.arange(3000, dtype=dtype)
self.b1 = np.arange(240000, dtype=dtype).reshape(400, 600)
self.c = np.arange(24000, dtype=dtype).reshape(20, 30, 40)
self.c1 = np.arange(1200, dtype=dtype).reshape(30, 40)
self.c2 = np.arange(480000, dtype=dtype)
self.c3 = np.arange(600, dtype=dtype)
self.d = np.arange(10000, dtype=dtype).reshape(10,100,10)

#outer(a,b): trigger sum_of_products_contig_stride0_outcontig_two
# outer(a,b): trigger sum_of_products_contig_stride0_outcontig_two
def time_einsum_outer(self, dtype):
np.einsum("i,j", self.a, self.b, optimize=True)

Expand All @@ -130,4 +133,16 @@ def time_einsum_sum_mul(self, dtype):

# sum and multiply:trigger sum_of_products_stride0_contig_outstride0_two
def time_einsum_sum_mul2(self, dtype):
np.einsum("i...,->", self.d, 300, optimize=True)
np.einsum("i...,->", self.d, 300, optimize=True)

# scalar mul: trigger sum_of_products_stride0_contig_outcontig_two
def time_einsum_mul(self, dtype):
np.einsum("i,->i", self.c2, 300, optimize=True)

# trigger contig_contig_outstride0_two
def time_einsum_contig_contig(self, dtype):
np.einsum("ji,i->", self.b1, self.c3, optimize=True)

# trigger sum_of_products_contig_outstride0_one
def time_einsum_contig_outstride0(self, dtype):
np.einsum("i->", self.c2, optimize=True)
1 change: 1 addition & 0 deletions numpy/core/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -853,6 +853,7 @@ def get_mathlib_info(*args):
join('src', 'multiarray', 'dragon4.c'),
join('src', 'multiarray', 'dtype_transfer.c'),
join('src', 'multiarray', 'einsum.c.src'),
join('src', 'multiarray', 'einsum.dispatch.c.src'),
join('src', 'multiarray', 'flagsobject.c'),
join('src', 'multiarray', 'getset.c'),
join('src', 'multiarray', 'hashdescr.c'),
Expand Down
25 changes: 25 additions & 0 deletions numpy/core/src/common/simd/avx2/arithmetic.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,13 @@
#define npyv_mul_f32 _mm256_mul_ps
#define npyv_mul_f64 _mm256_mul_pd

#ifdef NPY_HAVE_FMA3
#define npyv_muladd_f32 _mm256_fmadd_ps
#define npyv_muladd_f64 _mm256_fmadd_pd
#else
#define npyv_muladd_f32(a, b, c) npyv_add_f32(npyv_mul_f32(a, b), c)
#define npyv_muladd_f64(a, b, c) npyv_add_f64(npyv_mul_f64(a, b), c)
#endif
// saturated
// TODO: after implment Packs intrins

Expand All @@ -72,4 +79,22 @@
#define npyv_div_f32 _mm256_div_ps
#define npyv_div_f64 _mm256_div_pd

// Horizontal add: Calculates the sum of all vector elements.
NPY_FINLINE float npyv_sum_f32(__m256 a)
{
__m128 t1 = _mm_add_ps(_mm256_castps256_ps128(a), _mm256_extractf128_ps(a,1));
__m128 t2 = _mm_movehdup_ps(t1);
__m128 t 10000 3 = _mm_add_ps(t1, t2);
__m128 t4 = _mm_movehl_ps(t3, t3);
__m128 t5 = _mm_add_ss(t3, t4);
return _mm_cvtss_f32(t5);
}

NPY_FINLINE double npyv_sum_f64(__m256d a)
{
__m128d t1 = _mm_add_pd(_mm256_castpd256_pd128(a), _mm256_extractf128_pd(a,1));
__m128d t2 = _mm_unpackhi_pd(t1, t1);
__m128d t3 = _mm_add_sd(t2, t1);
return _mm_cvtsd_f64(t3);
}
#endif // _NPY_SIMD_AVX2_ARITHMETIC_H
25 changes: 25 additions & 0 deletions numpy/core/src/common/simd/avx512/arithmetic.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,9 @@ NPY_FINLINE __m512i npyv_mul_u8(__m512i a, __m512i b)
#define npyv_mul_f32 _mm512_mul_ps
#define npyv_mul_f64 _mm512_mul_pd

#define npyv_muladd_f32 _mm512_fmadd_ps
#define npyv_muladd_f64 _mm512_fmadd_pd

// saturated
// TODO: after implment Packs intrins

Expand All @@ -112,5 +115,27 @@ NPY_FINLINE __m512i npyv_mul_u8(__m512i a, __m512i b)
// TODO: emulate integer division
#define npyv_div_f32 _mm512_div_ps
#define npyv_div_f64 _mm512_div_pd
NPY_FINLINE float npyv_sum_f32(npyv_f32 a)
{
__m512 h64 = _mm512_shuffle_f32x4(a, a, _MM_SHUFFLE(3, 2, 3, 2));
__m512 sum32 = _mm512_add_ps(a, h64);
__m512 h32 = _mm512_shuffle_f32x4(sum32, sum32, _MM_SHUFFLE(1, 0, 3, 2));
__m512 sum16 = _mm512_add_ps(sum32, h32);
__m512 h16 = _mm512_permute_ps(sum16, _MM_SHUFFLE(1, 0, 3, 2));
__m512 sum8 = _mm512_add_ps(sum16, h16);
__m512 h4 = _mm512_permute_ps(sum8, _MM_SHUFFLE(2, 3, 0, 1));
__m512 sum4 = _mm512_add_ps(sum8, h4);
return _mm_cvtss_f32(_mm512_castps512_ps128(sum4));
}
NPY_FINLINE double npyv_sum_f64(npyv_f64 a)
{
__m512d h64 = _mm512_shuffle_f64x2(a, a, _MM_SHUFFLE(3, 2, 3, 2));
__m512d sum32 = _mm512_add_pd(a, h64);
__m512d h32 = _mm512_permutex_pd(sum32, _MM_SHUFFLE(1, 0, 3, 2));
__m512d sum16 = _mm512_add_pd(sum32, h32);
__m512d h16 = _mm512_permute_pd(sum16, _MM_SHUFFLE(2, 3, 0, 1));
__m512d sum8 = _mm512_add_pd(sum16, h16);
return _mm_cvtsd_f64(_mm512_castpd512_pd128(sum8));
}

#endif // _NPY_SIMD_AVX512_ARITHMETIC_H
19 changes: 19 additions & 0 deletions numpy/core/src/common/simd/neon/arithmetic.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@
#define npyv_mul_f32 vmulq_f32
#define npyv_mul_f64 vmulq_f64

#ifdef NPY_HAVE_NEON_VFPV4
#define npyv_muladd_f32(A, B, C) vfmaq_f32(C, A, B)
#else
#define npyv_muladd_f32(A, B, C) vmlaq_f32(C, A, B)
#endif
#define npyv_muladd_f64(A, B, C) vfmaq_f64(C, A, B)
/***************************
* Division
***************************/
Expand All @@ -75,4 +81,17 @@
#endif
#define npyv_div_f64 vdivq_f64

// Horizontal add: Calculates the sum of all vector elements.
NPY_FINLINE float npyv_sum_f32(float32x4_t a)
{
float32x2_t r = vadd_f32(vget_high_f32(a), vget_low_f32(a));
return vget_lane_f32(vpadd_f32(r, r), 0);
}
#ifdef __aarch64__
NPY_FINLINE double npyv_sum_f64(float64x2_t a)
{
return vget_lane_f64(vget_low_f64(a) + vget_high_f64(a), 0);
}
#endif

#endif // _NPY_SIMD_NEON_ARITHMETIC_H
24 changes: 24 additions & 0 deletions numpy/core/src/common/simd/sse/arithmetic.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,13 @@ NPY_FINLINE __m128i npyv_mul_u8(__m128i a, __m128i b)
#define npyv_mul_f32 _mm_mul_ps
#define npyv_mul_f64 _mm_mul_pd

#ifdef NPY_HAVE_FMA3
#define npyv_muladd_f32 _mm_fmadd_ps
#define npyv_muladd_f64 _mm_fmadd_pd
#else
#define npyv_muladd_f32(a, b, c) npyv_add_f32(npyv_mul_f32(a, b), c)
#define npyv_muladd_f64(a, b, c) npyv_add_f64(npyv_mul_f64(a, b), c)
#endif
// saturated
// TODO: after implment Packs intrins

Expand All @@ -92,4 +99,21 @@ NPY_FINLINE __m128i npyv_mul_u8(__m128i a, __m128i b)
#define npyv_div_f32 _mm_div_ps
#define npyv_div_f64 _mm_div_pd

// Horizontal add: Calculates the sum of all vector elements.
NPY_FINLINE float npyv_sum_f32(__m128 a)
{
__m128 t1 = _mm_movehl_ps(a, a);
__m128 t2 = _mm_add_ps(a, t1);
__m128 t3 = _mm_shuffle_ps(t2, t2, 1);
__m128 t4 = _mm_add_ss(t2, t3);
return _mm_cvtss_f32(t4);
}

NPY_FINLINE double npyv_sum_f64(__m128d a)
{
__m128 t0 = _mm_castpd_ps(a);
__m128d t1 = _mm_castps_pd(_mm_movehl_ps(t0,t0));
__m128d t2 = _mm_add_sd(a,t1);
return _mm_cvtsd_f64(t2);
}
#endif // _NPY_SIMD_SSE_ARITHMETIC_H
15 changes: 15 additions & 0 deletions numpy/core/src/common/simd/vsx/arithmetic.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,25 @@
#define npyv_mul_f32 vec_mul
#define npyv_mul_f64 vec_mul

#define npyv_muladd_f32 vec_madd
#define npyv_muladd_f64 vec_madd

/***************************
* Division
***************************/
#define npyv_div_f32 vec_div
#define npyv_div_f64 vec_div

// TODO: Horizontal add: Calculates the sum of all vector elements.
NPY_FINLINE float npyv_sum_f32(npyv_f32 a)
{
return vec_extract(a, 0) + vec_extract(a, 1) +
vec_extract(a, 2) + vec_extract(a, 3);
}

NPY_FINLINE double npyv_sum_f64(npyv_f64 a)
{
return vec_extract(a, 0) + vec_extract(a, 1);
}

#endif // _NPY_SIMD_VSX_ARITHMETIC_H
Loading
0