Skip to content

Improve SVE2 optimizations of SimdDescrIntCosineDistancesMxNa - #905

Merged
ermig1979 merged 2 commits into
devfrom
cursor/improve-sve2-descrint-cosine-mxna-1734
Aug 24, 2026
Merged

Improve SVE2 optimizations of SimdDescrIntCosineDistancesMxNa#905
ermig1979 merged 2 commits into
devfrom
cursor/improve-sve2-descrint-cosine-mxna-1734

Conversation

@ermig1979

@ermig1979 ermig1979 commented Aug 24, 2026

Copy link
Copy Markdown
Owner

The SVE2 path of SimdDescrIntCosineDistancesMxNa used a 4x1 unpack GEMM (_microNu = 1) with scalar 4–7 bit unpack, and the direct kernel skipped 4x4 tiles for depths 4 and 8. That is much weaker than the NEON 6x16 unpack / 4x4 direct layout and was slower than NEON.

This change:

  • Adds unpredicated 4x4 svdot_u32 micro-kernels for every depth on the direct path (nibble split for 4-bit, load+dot for 8-bit, existing svtbl unpack for 5/6/7-bit)
  • Rewrites the unpack path as a 4x4 row-major GEMM after a single vectorized unpack of A and B
  • Vectorizes UnpackData for 4–8 bits (svzip for nibbles, svtbl for 5/6/7, predicated copy for 8)
  • Transposes B norms so 4 distances can be decoded together
  • Does not use macros or structured svld4/svst4 loads/stores
  • Extends DescrIntCosineDistancesMxNaAutoTest with sizes 8x8x24, 4x12x256, and 5x7x128
  • Notes the improvement in release 7.2.165

Follow-up fix for a SIGSEGV on CosineDistancesMxNa-5-7-128-4:

  • UnpackNormB no longer uses vst1q_f32 with stride N (unaligned when N=7)
  • 4-wide decode uses scalar header loads instead of NEON 16-byte loads of that layout
  • svld1rq unpack tables are 16-byte aligned
  • SVE2 init binds SVE2 function pointers explicitly

SimdSve2DescrInt.cpp / SimdSve2DescrIntCdd.cpp / SimdSve2DescrIntCdu.cpp were already in prj/vs2022/Sve2.vcxproj and Sve2.vcxproj.filters.

Correctness:

  • Cross-compiled with aarch64-linux-gnu-g++ (-march=armv9-a+sve+sve2, scalable VL)
  • QEMU user-mode of the actual unpack + direct kernels for 5x7x128 and other remainder sizes, including unaligned headers (gap=17 and gap=1024), sve-max-vq=1/2/4 and neoverse-n2: all passed
  • x86 ./Test -fi=DescrIntCosineDistancesMxNa (includes 5x7x128-4): success
Open in Web Open in Cursor 

Replace the 4x1 unpack GEMM and missing 4/8-bit direct tiles with
unpredicated 4x4 svdot kernels, vectorized unpack of packed 4-7 bit
data, and transposed B norms. Drop the previous Mx1-only fallback
for depths 4 and 8, extend MxNa tests, and note the change in
release 7.2.165.

Co-authored-by: igor.ermolaev <igor.ermolaev@altezza.org>
@ermig1979
ermig1979 marked this pull request as ready for review August 24, 2026 08:40
UnpackNormB used vst1q_f32 with stride N, which is not 16-byte aligned
when N=7. Decode used the same NEON 16-byte loads on that layout and on
unaligned stack temps. Those accesses are new on the SVE2 unpack path
(NEON keeps N<8 on the direct path) and can raise a segment violation.

Copy headers with memcpy, decode 4-wide tiles with scalar loads, align
svld1rq tables, and bind SVE2 function pointers explicitly.

Co-authored-by: igor.ermolaev <igor.ermolaev@altezza.org>
@ermig1979
ermig1979 merged commit 44c487c into dev Aug 24, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants