Skip to content
Merged
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
2 changes: 2 additions & 0 deletions .github/workflows/benchmark.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ on:
- bm-sq8-compare-fp16
- bm-sq8-train-fp32
- bm-sq8-train-fp16
- bm-spaces-sq4-fp32
- bm-spaces-sq4-sq4
- bm-svs-train-fp32
- bm-svs-train-fp16
- bm-basics-svs-fp32-single
Expand Down
159 changes: 159 additions & 0 deletions src/VecSim/spaces/IP/IP_AVX2_FMA_SQ4_FP32.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
/*
* Copyright (c) 2006-Present, Redis Ltd.
* All rights reserved.
*
* Licensed under your choice of the Redis Source Available License 2.0
* (RSALv2); or (b) the Server Side Public License v1 (SSPLv1); or (c) the
* GNU Affero General Public License v3 (AGPLv3).
*/
#pragma once
#include "VecSim/spaces/space_includes.h"
#include "VecSim/spaces/AVX_utils.h"
#include "VecSim/spaces/IP/IP.h"
#include "VecSim/types/sq4.h"
#include "VecSim/utils/alignment.h"
#include <immintrin.h>
#include <cmath>
#include <cstring>

namespace {

/*
* Asymmetric SQ4 inner product with the same identity as SQ8:
*
* IP(x, y) ≈ Σ((min + delta * q_i) * y_i) = min * y_sum + delta * Σ(q_i * y_i)
*
* Storage packs coordinate i into byte i / 2, the low nibble for even i and the high nibble for
* odd i, followed by unaligned FP32 metadata. The query blob carries y_sum after its dim floats.
*/

// Interleave the low and high nibbles of the packed bytes, so `first` holds the codes of the low
// eight bytes and `second` those of the high eight bytes, each in coordinate order.
static inline void SQ4_Unpack_AVX2_FMA(__m128i packed, __m128i &first, __m128i &second) {
const __m128i nibble = _mm_set1_epi8(15);
const __m128i low = _mm_and_si128(packed, nibble);
const __m128i high = _mm_and_si128(_mm_srli_epi16(packed, 4), nibble);
first = _mm_unpacklo_epi8(low, high);
second = _mm_unpackhi_epi8(low, high);
}

// Sixteen byte codes in coordinate order to two float vectors.
static inline void SQ4_ToFloats_AVX2_FMA(__m128i codes, __m256 &first, __m256 &second) {
first = _mm256_cvtepi32_ps(_mm256_cvtepu8_epi32(codes));
second = _mm256_cvtepi32_ps(_mm256_cvtepu8_epi32(_mm_unpackhi_epi64(codes, codes)));
}

// Exactly `bytes` (1..8) packed bytes in the low half of a vector, the rest zero.
template <unsigned bytes>
static inline __m128i SQ4_LoadBytes_AVX2_FMA(const uint8_t *data) {
static_assert(bytes >= 1 && bytes <= 8);
uint64_t bits = 0;
std::memcpy(&bits, data, bytes);
return _mm_cvtsi64_si128(static_cast<long long>(bits));
}

// Blend control selecting the top `lanes` (1..8) of eight float lanes.
template <unsigned lanes>
constexpr int SQ4_TailLanes_AVX2_FMA = (0xFF << (8 - lanes)) & 0xFF;

// The `lanes` (1..8) query floats that end at `end`, in the top lanes with zero below. The tail is
// read with a full 8-float load ending at `end`, which stays inside the query whenever dim >= 8
// (the dispatcher's minimum); a smaller dim copies the floats instead. Masked AVX2 loads are not
// used because some processors may fault on their masked-out lanes.
template <unsigned lanes>
static inline __m256 SQ4_TailQuery_AVX2_FMA(const float *end, size_t dimension) {
static_assert(lanes >= 1 && lanes <= 8);
if (dimension >= 8) {
return _mm256_blend_ps(_mm256_setzero_ps(), _mm256_loadu_ps(end - 8),
SQ4_TailLanes_AVX2_FMA<lanes>);
}
float window[8] = {};
std::memcpy(window + 8 - lanes, end - lanes, lanes * sizeof(float));
return _mm256_loadu_ps(window);
}

// The first `lanes` (1..8) byte codes of `codes` as floats in the top lanes, zero below, to line up
// with SQ4_TailQuery_AVX2_FMA. Later bytes, such as an odd dim's padding nibble, are shifted out.
template <unsigned lanes>
static inline __m256 SQ4_TailCodes_AVX2_FMA(__m128i codes) {
static_assert(lanes >= 1 && lanes <= 8);
return _mm256_cvtepi32_ps(_mm256_cvtepu8_epi32(_mm_slli_si128(codes, 8 - lanes)));
}

// Accumulate Σ(q_i * y_i) for 32 coordinates: one 16-byte load of packed codes.
static inline void SQ4_FP32_IPStep32_AVX2_FMA(const uint8_t *x, const float *y, __m256 &sum0,
__m256 &sum1, __m256 &sum2, __m256 &sum3) {
__m128i ordered0, ordered1;
SQ4_Unpack_AVX2_FMA(_mm_loadu_si128(reinterpret_cast<const __m128i *>(x)), ordered0, ordered1);
__m256 codes0, codes1, codes2, codes3;
SQ4_ToFloats_AVX2_FMA(ordered0, codes0, codes1);
SQ4_ToFloats_AVX2_FMA(ordered1, codes2, codes3);
sum0 = _mm256_fmadd_ps(codes0, _mm256_loadu_ps(y), sum0);
sum1 = _mm256_fmadd_ps(codes1, _mm256_loadu_ps(y + 8), sum1);
sum2 = _mm256_fmadd_ps(codes2, _mm256_loadu_ps(y + 16), sum2);
sum3 = _mm256_fmadd_ps(codes3, _mm256_loadu_ps(y + 24), sum3);
}

template <unsigned char residual> // 0..31
float SQ4_FP32_InnerProductSIMD32_AVX2_FMA(const void *pVect1v, const void *pVect2v,
size_t dimension) {
using vecsim_types::sq4;
const auto *x = static_cast<const uint8_t *>(pVect1v);
const auto *y = static_cast<const float *>(pVect2v);
// Eight accumulators break the FMA dependency chain: two 32-coordinate steps per iteration.
__m256 sum0 = _mm256_setzero_ps(), sum1 = _mm256_setzero_ps();
__m256 sum2 = _mm256_setzero_ps(), sum3 = _mm256_setzero_ps();
__m256 sum4 = _mm256_setzero_ps(), sum5 = _mm256_setzero_ps();
__m256 sum6 = _mm256_setzero_ps(), sum7 = _mm256_setzero_ps();
const size_t complete = dimension - residual;
size_t i = 0;
for (; i + 64 <= complete; i += 64) {
SQ4_FP32_IPStep32_AVX2_FMA(x + i / 2, y + i, sum0, sum1, sum2, sum3);
SQ4_FP32_IPStep32_AVX2_FMA(x + i / 2 + 16, y + i + 32, sum4, sum5, sum6, sum7);
}
if (i < complete) {
SQ4_FP32_IPStep32_AVX2_FMA(x + i / 2, y + i, sum0, sum1, sum2, sum3);
}
sum0 = _mm256_add_ps(sum0, sum4);
sum1 = _mm256_add_ps(sum1, sum5);
sum2 = _mm256_add_ps(sum2, sum6);
sum3 = _mm256_add_ps(sum3, sum7);
i = complete;
if constexpr (residual >= 16) {
// One 8-byte load covers exactly 16 coordinates.
__m128i ordered, unused;
SQ4_Unpack_AVX2_FMA(_mm_loadl_epi64(reinterpret_cast<const __m128i *>(x + i / 2)), ordered,
unused);
__m256 codes0, codes1;
SQ4_ToFloats_AVX2_FMA(ordered, codes0, codes1);
sum0 = _mm256_fmadd_ps(codes0, _mm256_loadu_ps(y + i), sum0);
sum1 = _mm256_fmadd_ps(codes1, _mm256_loadu_ps(y + i + 8), sum1);
i += 16;
}
constexpr unsigned rest = residual % 16;
if constexpr (rest > 0) {
// Exactly the tail's code bytes. The last query window ends at the last coordinate (see
// SQ4_TailQuery_AVX2_FMA).
__m128i ordered, unused;
SQ4_Unpack_AVX2_FMA(SQ4_LoadBytes_AVX2_FMA<(rest + 1) / 2>(x + i / 2), ordered, unused);
const float *end = y + dimension;
if constexpr (rest > 8) {
const __m256 codes0 = _mm256_cvtepi32_ps(_mm256_cvtepu8_epi32(ordered));
sum2 = _mm256_fmadd_ps(codes0, _mm256_loadu_ps(y + i), sum2);
sum3 = _mm256_fmadd_ps(
SQ4_TailCodes_AVX2_FMA<rest - 8>(_mm_unpackhi_epi64(ordered, ordered)),
SQ4_TailQuery_AVX2_FMA<rest - 8>(end, dimension), sum3);
} else {
sum2 = _mm256_fmadd_ps(SQ4_TailCodes_AVX2_FMA<rest>(ordered),
SQ4_TailQuery_AVX2_FMA<rest>(end, dimension), sum2);
}
Comment thread
cursor[bot] marked this conversation as resolved.
Resolved
}
const float dot = my_mm256_reduce_add_ps_tree(
_mm256_add_ps(_mm256_add_ps(sum0, sum1), _mm256_add_ps(sum2, sum3)));
const auto *params = x + sq4::data_bytes_count(dimension);
const float min_val = load_unaligned<float>(params + sq4::MIN_VAL * sizeof(float));
const float delta = load_unaligned<float>(params + sq4::DELTA * sizeof(float));
return 1.0f - (min_val * y[dimension + sq4::SUM_QUERY] + delta * dot);
}

} // namespace
82 changes: 82 additions & 0 deletions src/VecSim/spaces/IP/IP_AVX2_SQ4_SQ4.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
* Copyright (c) 2006-Present, Redis Ltd.
* All rights reserved.
*
* Licensed under your choice of the Redis Source Available License 2.0
* (RSALv2); or (b) the Server Side Public License v1 (SSPLv1); or (c) the
* GNU Affero General Public License v3 (AGPLv3).
*/
#pragma once
#include "VecSim/spaces/space_includes.h"
#include "VecSim/types/sq4.h"
#include "VecSim/spaces/quantized_distance_helpers.h"
#include <cstring>
#include <immintrin.h>

namespace {

/*
* The packed code dot is the sum of low-nibble products and high-nibble products.
* Both operands' codes are 0..15, so maddubs' signed operand is nonnegative and its
* saturating pair sum is at most 450. madd with ones widens groups of four products
* to int32 before accumulation. Each lane of each accumulator adds at most 900 per
* 64 coordinates. Even after combining low and high, 1800 * ceil(dim / 64) is below
* 537 million at SQ4_MAX_EXACT_SIMD_DIM, safely below INT32_MAX. The horizontal sum
* uses wrapping SIMD additions and unsigned extraction: the dispatch bound keeps
* the full sum, at most 225 * dim, within UINT32_MAX.
*/

static inline void SQ4_SQ4_DotStep_AVX2(__m256i &sum_low, __m256i &sum_high, __m256i x, __m256i y) {
const __m256i nibble = _mm256_set1_epi8(15);
const __m256i ones = _mm256_set1_epi16(1);
const __m256i low =
_mm256_maddubs_epi16(_mm256_and_si256(x, nibble), _mm256_and_si256(y, nibble));
const __m256i high = _mm256_maddubs_epi16(_mm256_and_si256(_mm256_srli_epi16(x, 4), nibble),
_mm256_and_si256(_mm256_srli_epi16(y, 4), nibble));
sum_low = _mm256_add_epi32(sum_low, _mm256_madd_epi16(low, ones));
sum_high = _mm256_add_epi32(sum_high, _mm256_madd_epi16(high, ones));
}

// Raw inner product, shared with the stored-sum L2 identity.
template <unsigned char residual> // 0..63
float SQ4_SQ4_InnerProductSIMD64_AVX2_IMP(const void *pVect1v, const void *pVect2v,
size_t dimension) {
const auto *x = static_cast<const uint8_t *>(pVect1v);
const auto *y = static_cast<const uint8_t *>(pVect2v);
__m256i sum_low = _mm256_setzero_si256();
__m256i sum_high = _mm256_setzero_si256();
const size_t complete = dimension - residual;
for (size_t i = 0; i < complete; i += 64) {
SQ4_SQ4_DotStep_AVX2(sum_low, sum_high,
_mm256_loadu_si256(reinterpret_cast<const __m256i *>(x + i / 2)),
_mm256_loadu_si256(reinterpret_cast<const __m256i *>(y + i / 2)));
}
if constexpr (residual > 0) {
// Copy only code bytes, without reading into the stored metadata. Clear the unused
// high nibble for an odd dimension, even if a foreign writer left it nonzero.
alignas(32) uint8_t xt[32] = {};
alignas(32) uint8_t yt[32] = {};
std::memcpy(xt, x + complete / 2, (residual + 1) / 2);
std::memcpy(yt, y + complete / 2, (residual + 1) / 2);
if constexpr (residual % 2 != 0) {
xt[residual / 2] &= 15;
}
SQ4_SQ4_DotStep_AVX2(sum_low, sum_high,
_mm256_load_si256(reinterpret_cast<const __m256i *>(xt)),
_mm256_load_si256(reinterpret_cast<const __m256i *>(yt)));
}
const __m256i sum = _mm256_add_epi32(sum_low, sum_high);
const __m128i s4 = _mm_add_epi32(_mm256_castsi256_si128(sum), _mm256_extracti128_si256(sum, 1));
const __m128i s2 = _mm_add_epi32(s4, _mm_srli_si128(s4, 8));
const __m128i s1 = _mm_add_epi32(s2, _mm_srli_si128(s2, 4));
const auto code_dot = static_cast<uint32_t>(_mm_cvtsi128_si32(s1));
return spaces::quantized::Stored_InnerProduct_FromCodeDot<vecsim_types::sq4>(
x, y, dimension, static_cast<float>(code_dot));
}

template <unsigned char residual> // 0..63
float SQ4_SQ4_InnerProductSIMD64_AVX2(const void *pVect1v, const void *pVect2v, size_t dimension) {
return 1.0f - SQ4_SQ4_InnerProductSIMD64_AVX2_IMP<residual>(pVect1v, pVect2v, dimension);
}

} // namespace
83 changes: 83 additions & 0 deletions src/VecSim/spaces/IP/IP_AVX512F_BW_VL_VNNI_SQ4_FP32.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
* Copyright (c) 2006-Present, Redis Ltd.
* All rights reserved.
*
* Licensed under your choice of the Redis Source Available License 2.0
* (RSALv2); or (b) the Server Side Public License v1 (SSPLv1); or (c) the
* GNU Affero General Public License v3 (AGPLv3).
*/
#pragma once
#include "VecSim/spaces/space_includes.h"
#include "VecSim/spaces/IP/IP.h"
#include "VecSim/types/sq4.h"
#include "VecSim/utils/alignment.h"
#include <immintrin.h>
#include <cmath>

namespace {

/*
* Asymmetric SQ4 inner product with the same identity as SQ8:
*
* IP(x, y) ≈ Σ((min + delta * q_i) * y_i) = min * y_sum + delta * Σ(q_i * y_i)
*
* Storage packs coordinate i into byte i / 2, the low nibble for even i and the high nibble for
* odd i, followed by unaligned FP32 metadata. The query blob carries y_sum after its dim floats.
*/

// Unpack 16 packed bytes (32 coordinates) into two float vectors in coordinate order.
static inline void SQ4_Unpack32_AVX512F_BW_VL_VNNI(__m128i packed, __m512 &first, __m512 &second) {
const __m128i nibble = _mm_set1_epi8(15);
const __m128i low = _mm_and_si128(packed, nibble);
const __m128i high = _mm_and_si128(_mm_srli_epi16(packed, 4), nibble);
first = _mm512_cvtepi32_ps(_mm512_cvtepu8_epi32(_mm_unpacklo_epi8(low, high)));
second = _mm512_cvtepi32_ps(_mm512_cvtepu8_epi32(_mm_unpackhi_epi8(low, high)));
}

// Masks for a tail of `residual` (0..31) coordinates: the packed bytes that hold it, and the query
// lanes of each 16-coordinate half.
template <unsigned char residual>
struct SQ4TailMasks_AVX512F_BW_VL_VNNI {
static constexpr __mmask16 bytes = (1u << ((residual + 1) / 2)) - 1;
static constexpr __mmask16 first = residual >= 16 ? 0xffff : (1u << residual) - 1;
static constexpr __mmask16 second = residual > 16 ? (1u << (residual - 16)) - 1 : 0;
};

template <unsigned char residual> // 0..31
float SQ4_FP32_InnerProductSIMD32_AVX512F_BW_VL_VNNI(const void *pVect1v, const void *pVect2v,
size_t dimension) {
using vecsim_types::sq4;
const auto *x = static_cast<const uint8_t *>(pVect1v);
const auto *y = static_cast<const float *>(pVect2v);
__m512 sum0 = _mm512_setzero_ps();
__m512 sum1 = _mm512_setzero_ps();
const size_t complete = dimension - residual;
for (size_t i = 0; i < complete; i += 32) {
__m512 codes0, codes1;
SQ4_Unpack32_AVX512F_BW_VL_VNNI(
_mm_loadu_si128(reinterpret_cast<const __m128i *>(x + i / 2)), codes0, codes1);
sum0 = _mm512_fmadd_ps(codes0, _mm512_loadu_ps(y + i), sum0);
sum1 = _mm512_fmadd_ps(codes1, _mm512_loadu_ps(y + i + 16), sum1);
}
if constexpr (residual > 0) {
// Masked loads of exactly the tail's code bytes and query floats, so nothing past the codes
// or the query's dim floats is read. Masked-out query lanes are zero, so the padding nibble
// of an odd dim adds nothing.
using masks = SQ4TailMasks_AVX512F_BW_VL_VNNI<residual>;
__m512 codes0, codes1;
SQ4_Unpack32_AVX512F_BW_VL_VNNI(_mm_maskz_loadu_epi8(masks::bytes, x + complete / 2),
codes0, codes1);
sum0 = _mm512_fmadd_ps(codes0, _mm512_maskz_loadu_ps(masks::first, y + complete), sum0);
if constexpr (masks::second != 0) {
sum1 = _mm512_fmadd_ps(codes1, _mm512_maskz_loadu_ps(masks::second, y + complete + 16),
sum1);
}
}
const float dot = _mm512_reduce_add_ps(_mm512_add_ps(sum0, sum1));
const auto *params = x + sq4::data_bytes_count(dimension);
const float min_val = load_unaligned<float>(params + sq4::MIN_VAL * sizeof(float));
const float delta = load_unaligned<float>(params + sq4::DELTA * sizeof(float));
return 1.0f - (min_val * y[dimension + sq4::SUM_QUERY] + delta * dot);
}

} // namespace
Loading
Loading