GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
GeezerVison is showing github.com — ENH: Convert fp32 sin/cos from C universal intrinsics to C++ using Highway by r-devulap · Pull Request #25781 · numpy/numpy Conversation
Member
This patch is to experiment with highway and see how we can leverage its intrinsics using static dispatch. I would think these are the minimum requirements:
On x86, both AVX-512 and AVX2 seem to have performance regressions. Yet to figure out where they are coming from. AVX-512 benchmarksThese are about 1.5x slower even when built with AVX2 benchmarksThese are about 1.34x slower when built using
Sorry, something went wrong. Uh oh!
There was an error while loading. Please reload this page. All reactions
Member
All reactions
Sorry, something went wrong. Uh oh!
There was an error while loading. Please reload this page.
Mousius
reviewed
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This used to be wrapped in
Sorry, something went wrong. Uh oh!
There was an error while loading. Please reload this page. All reactions
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, I had test failures which got resolved when I got rid of it. Need to figure that out.
Sorry, something went wrong. Uh oh!
There was an error while loading. Please reload this page. All reactions
Mousius
reviewed
|
| } | ||
|
|
||
| NPY_FINLINE vec_f32 | ||
| GatherIndexN(const float* src, npy_intp ssrc, npy_intp len) |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
I'm not massively familiar with x86, but it looks like npyv_loadn_tillz_s32 uses a gather instruction here rather than a loop, should try it to see if it provides similar performance?
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
The 1.5x slowdown actually happens in the non strided case which don't use Gather or Scatter. But even for the strided case, using gather is slower than scalar method (since the DOWNFALL CVE).
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Ahh, apologies, I wasn't sure if the code generated for Gather was pretty slow on x86 as well, it doesn't look as good as the ASIMD npyv_ function 😅 Too many architectures 😸
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
Ok, so I played a bit of spot the difference and left some comments, and I ran some benchmarks quickly - it looks like with ASIMD, there's regressions with these: This indicates the gather/scatter aren't as optimal as the NumPy ones; I wonder if we can blend the NumPy loads and stores with the Highway code here 🤔 |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| ) | ||
| AVX2 = mod_features.new( | ||
| 'AVX2', 25, implies: F16C, args: '-mavx2', | ||
| 'AVX2', 25, implies: F16C, args: '-march=skylake', |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
I wonder if this is more related to implying -mtune=skylake? Highway relies on the compiler to do some optimisations, and I do not know what -mtune=generic does with just -mavx2 🤔
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Just looked at this a bit more deeply, and it looks like with just -mavx2 you don't get HWY_AVX2, these flags worked to get HWY_AVX2:
-mpclmul -maes -mavx -mavx2 -mbmi -mbmi2 -mfma -mf16c
I assume they're all implied by haswell but avx2 is actually way more limiting. Is there a processor which supports avx2 without all these things? 🤔
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
yet, the surprising thing was using -march=haswell makes the performance a lot worse (nearly 4x slower). I need some input from @jan-wassenberg to see what is happening here.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
You are exactly right, we require more flags than just -march=haswell: also -maes, then it is sufficient for HWY_AVX2.
Unfortunately there are some very few Haswells without AES, so we do check for that.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Out of curiosity: why do you need -maes for HWY_AVX2 if you aren't using any AES related instructions?
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
AESRound is a supported Highway op; we do not know whether users will use it. I suppose the options are to detect at runtime (but that would add considerable overhead), or add another target for AVX2 \ AES, but this is very rare, so not worthwhile, right?
Sorry, something went wrong.
There was an error while loading. Please reload this page.
That is extremely likely. My Gather/Scatter were just a quick and dirty way to make this work. I will eventually move to using the highway implementation. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
|
Please explain to me, where did this conclusion come from? the current speed-up is related to special cases (the libm fallback has been improved somehow) which affects both contiguous and non-contiguous. So I think maybe the regression is related to it. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
@seiko2plus I am seeing slowdown for strided cases as well. I just meant this could be a result of my |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
Right, Highway checks for multiple CPU flags before using AVX3. -march=skx is sufficient here, but for the individual -mavx512, that would require a long list. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| * these numbers | ||
| */ | ||
| if (!hn::AllFalse(f32, simd_mask)) { | ||
| vec_f32 x = hn::IfThenElse(hn::And(nnan_mask, simd_mask), x_in, zerosf); |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Could also use IfThenElseZero here and above if you like.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| cos = hn::IfThenElse(sine_mask, sin, cos); | ||
|
|
||
| // multiply by -1 for appropriate elements | ||
| opmask_t negate_mask = hn::RebindMask(f32, hn::Eq(hn::And(iquadrant, twos), twos)); |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Is IfNegativeThenNegOrUndefIfZero faster than these two lines?
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| ScatterIndexN(cos, dst, sdst, len); | ||
| } | ||
| } | ||
| if (simd_maski != (npy_uint64)((1 << lanes) - 1)) { |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
StoreMaskBits is not necessarily cheap. Recommend instead using CountTrue to get the number instead.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
I am now using if (!hn::AllTrue(f32, simd_mask)) instead and moved the StoreMaskBits inside the if condition (compute only when required). I am afraid CountTrue won't be sufficient here. We also need to know which specific lanes have bit set to 1.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
c0ea944 to
bb36792
Compare
There was an error while loading. Please reload this page.
|
Moving the |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
|
||
| for (; len > 0; len -= lanes, src += ssrc*lanes, dst += sdst*lanes) { | ||
| vec_f32 x_in; | ||
| if (ssrc == 1) { |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
LoadN should only be used in the tail of a loop. It is probably worthwhile replicating the body of the loop (e.g. by moving it to a helper function), with most iterations using a normal load, and only the last iteration using LoadN.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| } else { | ||
| x_in = GatherIndexN(src, ssrc, len); | ||
| } | ||
| opmask_t nnan_mask = hn::Not(hn::IsNaN(x_in)); |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
We can avoid the Not here by swapping the order of args to subsequent IfThenElse that use this, and also replacing And(nnan_mask, ..) with AndNot(nan_mask, ..).
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| cos = hn::IfThenElse(nnan_mask, cos, hn::Set(f32, NPY_NANF)); | ||
|
|
||
| if (sdst == 1) { | ||
| hn::StoreN(cos, f32, dst, len); |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
As with LoadN, StoreN should only be called in the last iteration.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| ScatterIndexN(cos, dst, sdst, len); | ||
| } | ||
| } | ||
| if (!hn::AllTrue(f32, simd_mask)) { |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Consider HWY_UNLIKELY annotation?
Sorry, something went wrong.
There was an error while loading. Please reload this page.
Cool, I have added some additional suggestions :) |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
@jan-wassenberg just looking at the CI failures and it seems there's some compiler incompatibility on PPC (https://github.com/numpy/numpy/actions/runs/8087779139/job/22100478961?pr=25781#step:8:504) and an abort on Z13 (https://github.com/numpy/numpy/actions/runs/8087779139/job/22100479504?pr=25781) any ideas? There's also a failure on |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
hm, the Z13 error is The latter at least I can help with. We are missing HWY_ATTR:
|
Sorry, something went wrong.
There was an error while loading. Please reload this page.
Adding HWY_ATTR fixed the build errors on ppc64le. Why did it fail only for this platform though? Need help with debugging 3 more failures:
|
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
hm, strange. Neither the x86 implementation of TableLookupBytesOr0, nor the quoted line and the one before it, have a numeric constant. Which compiler is cygwin using? |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
From the logs: |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
hm, here we are able to compile StoreInterleaved3 using GCC 11.4. Are you able to repro the issue in godbolt? |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
LGTM! 🥳
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
Huge milestone, thanks @r-devulap! |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
This was a big lift, thank you all and congrats on getting it over the finish line! |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
|
@r-devulap, it's great to see progress on moving to Highway! I noticed that you've disabled dispatching for the CPU features of ppc64 and zSystem, which not going to prevent baseline capabilities. Have you opened any issues related to Highway or NumPy reporting regarding this? |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
@seiko2plus good to hear from you again!
Good point, we haven't. Perhaps highway is the right place? We aren't sure where the bug is coming from though. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
hm, what's the issue we were addressing by removing the VSX etc? |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
@jan-wassenberg IIRC, it was a seg fault. We should see the failure in #27627 |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
For clarification, SIMD optimizations for sine and cosine functions on both ppc64 and z/Architecture (IBM Z) were disabled by gh-25781 to bypass CI tests. This PR aims to re-enable optimizations for z/Architecture after addressing the following runtime errors, while gh-27627 re-enabled ppc64 optimizations. * Re-enable VXE for sin/cos HWY implementation --------- Co-authored-by: Sayed Adel <[email protected]>
There was an error while loading. Please reload this page.
Successfully merging this pull request may close these issues.
There was an error while loading. Please reload this page.