Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
Differential Revision: D63406173
  • Loading branch information
mengdilin authored and facebook-github-bot committed Sep 25, 2024
1 parent 4ba3ce1 commit 0680ada
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 10 deletions.
20 changes: 10 additions & 10 deletions faiss/impl/ScalarQuantizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -671,7 +671,7 @@ struct QuantizerBF16<8> : QuantizerBF16<1> {

FAISS_ALWAYS_INLINE simd8float32
reconstruct_8_components(const uint8_t* code, int i) const {
#ifdef __AVX2__
// #ifdef __AVX2__
// reference impl: decode_bf16(((uint16_t*)code)[i]);
// decode_bf16(v) -> (uint32_t(v) << 16)
// read 128-bits (16 uint8_t) -> (uint16_t*)code)[i]
Expand All @@ -683,18 +683,18 @@ struct QuantizerBF16<8> : QuantizerBF16<1> {
simd8uint32 shifted_16 = code_256i << 16;
return as_float32(shifted_16);

#endif
// #endif

#ifdef __aarch64__
// #ifdef __aarch64__

uint16x4x2_t codei = vld1_u16_x2((const uint16_t*)(code + 2 * i));
return simd8float32(
{vreinterpretq_f32_u32(
vshlq_n_u32(vmovl_u16(codei.val[0]), 16)),
vreinterpretq_f32_u32(
vshlq_n_u32(vmovl_u16(codei.val[1]), 16))});
// uint16x4x2_t codei = vld1_u16_x2((const uint16_t*)(code + 2 *
// i)); return simd8float32(
// {vreinterpretq_f32_u32(
// vshlq_n_u32(vmovl_u16(codei.val[0]), 16)),
// vreinterpretq_f32_u32(
// vshlq_n_u32(vmovl_u16(codei.val[1]), 16))});

#endif
// #endif
}
};

Expand Down
69 changes: 69 additions & 0 deletions faiss/utils/simdlib_neon.h
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,11 @@ static inline uint32_t cmp_xe32(
return d0_mask | static_cast<uint32_t>(d1_mask) << 16;
}

template <std::uint8_t Shift>
static inline uint32x4_t vshlq(uint32x4_t vec) {
return vshlq_n_u32(vec, Shift);
}

template <std::uint8_t Shift>
static inline uint16x8_t vshlq(uint16x8_t vec) {
return vshlq_n_u16(vec, Shift);
Expand Down Expand Up @@ -972,6 +977,63 @@ struct simd8uint32 {
return ~(*this == other);
}

// shift must be known at compile time
simd8uint32 operator<<(const int shift) const {
switch (shift) {
case 0:
return *this;
case 1:
return simd8uint32{detail::simdlib::unary_func(data)
.call<detail::simdlib::vshlq<1>>()};
case 2:
return simd8uint32{detail::simdlib::unary_func(data)
.call<detail::simdlib::vshlq<2>>()};
case 3:
return simd8uint32{detail::simdlib::unary_func(data)
.call<detail::simdlib::vshlq<3>>()};
case 4:
return simd8uint32{detail::simdlib::unary_func(data)
.call<detail::simdlib::vshlq<4>>()};
case 5:
return simd8uint32{detail::simdlib::unary_func(data)
.call<detail::simdlib::vshlq<5>>()};
case 6:
return simd8uint32{detail::simdlib::unary_func(data)
.call<detail::simdlib::vshlq<6>>()};
case 7:
return simd8uint32{detail::simdlib::unary_func(data)
.call<detail::simdlib::vshlq<7>>()};
case 8:
return simd8uint32{detail::simdlib::unary_func(data)
.call<detail::simdlib::vshlq<8>>()};
case 9:
return simd8uint32{detail::simdlib::unary_func(data)
.call<detail::simdlib::vshlq<9>>()};
case 10:
return simd8uint32{detail::simdlib::unary_func(data)
.call<detail::simdlib::vshlq<10>>()};
case 11:
return simd8uint32{detail::simdlib::unary_func(data)
.call<detail::simdlib::vshlq<11>>()};
case 12:
return simd8uint32{detail::simdlib::unary_func(data)
.call<detail::simdlib::vshlq<12>>()};
case 13:
return simd8uint32{detail::simdlib::unary_func(data)
.call<detail::simdlib::vshlq<13>>()};
case 14:
return simd8uint32{detail::simdlib::unary_func(data)
.call<detail::simdlib::vshlq<14>>()};
case 15:
return simd8uint32{detail::simdlib::unary_func(data)
.call<detail::simdlib::vshlq<15>>()};
case 16:
return simd8uint32{detail::simdlib::unary_func(data)
.call<detail::simdlib::vshlq<16>>()};
default:
FAISS_THROW_FMT("Invalid shift %d", shift);
}
}
// Checks whether the other holds exactly the same bytes.
template <typename T>
bool is_same_as(T other) const {
Expand Down Expand Up @@ -1240,6 +1302,13 @@ inline simd8float32 load8(const uint8_t* code, int i) {
{vcvtq_f32_u32(vmovl_u16(y8_0)), vcvtq_f32_u32(vmovl_u16(y8_1))});
}

inline simd8uint32 load8_16bits_as_uint32(const uint8_t* code, int i) {
uint16x4x2_t codei = vld1_u16_x2((const uint16_t*)(code + 2 * i));
return simd8uint32({vmovl_u16(codei.val[0]), vmovl_u16(codei.val[1])});
}
inline simd8float32 as_float32(simd8uint32 x) {
return simd8float32(detail::simdlib::reinterpret_f32(x.data));
}
// The following primitive is a vectorized version of the following code
// snippet:
// float lowestValue = HUGE_VAL;
Expand Down

0 comments on commit 0680ada

Please sign in to comment.