Skip to content

Commit

Permalink
naive implementation of L2
Browse files Browse the repository at this point in the history
  • Loading branch information
meiravgri committed Dec 3, 2024
1 parent 2af891d commit 227a54d
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/VecSim/spaces/L2/L2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "L2.h"
#include "VecSim/types/bfloat16.h"
#include "VecSim/types/float16.h"
#include "VecSim/types/int8.h"
#include <cstring>

using bfloat16 = vecsim_types::bfloat16;
Expand Down Expand Up @@ -70,3 +71,26 @@ float FP16_L2Sqr(const void *pVect1, const void *pVect2, size_t dimension) {
}
return res;
}

template <bool is_little>
float INT8_L2Sqr(const void *pVect1v, const void *pVect2v, size_t dimension) {
int8_t *pVect1 = (int8_t *)pVect1v;
int8_t *pVect2 = (int8_t *)pVect2v;

int res = 0;
for (size_t i = 0; i < dimension; i++) {
int16_t a = vecsim_types::int8_to_int16<is_little>(pVect1[i]);
int16_t b = vecsim_types::int8_to_int16<is_little>(pVect2[i]);
int16_t diff = a - b;
res += diff * diff;
}
return float(res);
}

float INT8_L2Sqr_LittleEndian(const void *pVect1v, const void *pVect2v, size_t dimension) {
return INT8_L2Sqr<true>(pVect1v, pVect2v, dimension);
}

float INT8_L2Sqr_BigEndian(const void *pVect1v, const void *pVect2v, size_t dimension) {
return INT8_L2Sqr<false>(pVect1v, pVect2v, dimension);
}
39 changes: 39 additions & 0 deletions src/VecSim/types/int8.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
*Copyright Redis Ltd. 2021 - present
*Licensed under your choice of the Redis Source Available License 2.0 (RSALv2) or
*the Server Side Public License v1 (SSPLv1).
*/

#pragma once

#include <cstdint>
#include <cstring>
#include <cmath>

namespace vecsim_types {
struct bfloat16 {
uint16_t val;
bfloat16() = default;
explicit constexpr bfloat16(uint16_t val) : val(val) {}
operator uint16_t() const { return val; }
};

static inline bfloat16 float_to_bf16(const float ff) {
uint32_t *p_f32 = (uint32_t *)&ff;
uint32_t f32 = *p_f32;
uint32_t lsb = (f32 >> 16) & 1;
uint32_t round = lsb + 0x7FFF;
f32 += round;
return bfloat16(f32 >> 16);
}

template <bool is_little = true>
inline float bfloat16_to_float32(bfloat16 val) {
size_t constexpr bytes_offset = is_little ? 1 : 0;
float result = 0;
bfloat16 *p_result = (bfloat16 *)&result + bytes_offset;
*p_result = val;
return result;
}

} // namespace vecsim_types

0 comments on commit 227a54d

Please sign in to comment.