Skip to content

Commit

Permalink
Revert cpp spacing to 80 for cleaner PR
Browse files Browse the repository at this point in the history
  • Loading branch information
markkohdev committed Aug 19, 2024
1 parent ba06fd2 commit 0532250
Show file tree
Hide file tree
Showing 22 changed files with 1,332 additions and 703 deletions.
2 changes: 1 addition & 1 deletion cpp/.clang-format
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ InsertNewlineAtEOF: true
---
Language: Cpp
# Use 120 columns since we have big screens now
ColumnLimit: 120
ColumnLimit: 80
23 changes: 15 additions & 8 deletions cpp/src/E4M3.h
Original file line number Diff line number Diff line change
Expand Up @@ -294,9 +294,11 @@ class E4M3 {

E4M3() : E4M3(0, 0, 0) {}

E4M3(uint8_t sign, uint8_t exponent, uint8_t mantissa) : sign(sign), exponent(exponent), mantissa(mantissa) {}
E4M3(uint8_t sign, uint8_t exponent, uint8_t mantissa)
: sign(sign), exponent(exponent), mantissa(mantissa) {}

E4M3(uint8_t c) : sign(c >> 7), exponent((c >> 3) & 0b1111), mantissa(c & 0b111) {}
E4M3(uint8_t c)
: sign(c >> 7), exponent((c >> 3) & 0b1111), mantissa(c & 0b111) {}

E4M3(float input) {
if (std::isnan(input) || std::isinf(input)) {
Expand All @@ -314,11 +316,15 @@ class E4M3 {
// TODO: Don't hard-code these, and instead compute them based on the bit
// widths above!
if (input < -448 || input > 448) {
throw std::domain_error("E4M3 cannot represent values outside of [-448, 448].");
throw std::domain_error(
"E4M3 cannot represent values outside of [-448, 448].");
}

int originalExponent = ((*((const unsigned int *)&input) & 0b01111111100000000000000000000000) >> 23);
int originalMantissa = (*((const unsigned int *)&input) & 0b00000000011111111111111111111111);
int originalExponent = ((*((const unsigned int *)&input) &
0b01111111100000000000000000000000) >>
23);
int originalMantissa =
(*((const unsigned int *)&input) & 0b00000000011111111111111111111111);

sign = input < 0;

Expand Down Expand Up @@ -380,9 +386,10 @@ class E4M3 {
if (mantissa == 0b111) {
if (exponent == 0b1111) {
// Rounding up would push us just outside of the representable range!
throw std::domain_error("E4M3 cannot represent values outside of [-448, "
"448] - tried to convert " +
std::to_string(input) + ".");
throw std::domain_error(
"E4M3 cannot represent values outside of [-448, "
"448] - tried to convert " +
std::to_string(input) + ".");
} else {
exponent++;
mantissa = 0;
Expand Down
24 changes: 15 additions & 9 deletions cpp/src/Index.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,27 +62,33 @@ class Index {

virtual void saveIndex(const std::string &pathToIndex) = 0;
virtual void saveIndex(std::shared_ptr<OutputStream> outputStream) = 0;
virtual void loadIndex(const std::string &pathToIndex, bool searchOnly = false) = 0;
virtual void loadIndex(std::shared_ptr<InputStream> inputStream, bool searchOnly = false) = 0;
virtual void loadIndex(const std::string &pathToIndex,
bool searchOnly = false) = 0;
virtual void loadIndex(std::shared_ptr<InputStream> inputStream,
bool searchOnly = false) = 0;

virtual float getDistance(std::vector<float> a, std::vector<float> b) = 0;

virtual hnswlib::labeltype addItem(std::vector<float> vector, std::optional<hnswlib::labeltype> id) = 0;
virtual std::vector<hnswlib::labeltype> addItems(NDArray<float, 2> input, std::vector<hnswlib::labeltype> ids = {},
int numThreads = -1) = 0;
virtual hnswlib::labeltype addItem(std::vector<float> vector,
std::optional<hnswlib::labeltype> id) = 0;
virtual std::vector<hnswlib::labeltype>
addItems(NDArray<float, 2> input, std::vector<hnswlib::labeltype> ids = {},
int numThreads = -1) = 0;

virtual std::vector<float> getVector(hnswlib::labeltype id) = 0;
virtual NDArray<float, 2> getVectors(std::vector<hnswlib::labeltype> ids) = 0;

virtual std::vector<hnswlib::labeltype> getIDs() const = 0;
virtual long long getIDsCount() const = 0;
virtual const std::unordered_map<hnswlib::labeltype, hnswlib::tableint> &getIDsMap() const = 0;
virtual const std::unordered_map<hnswlib::labeltype, hnswlib::tableint> &
getIDsMap() const = 0;

virtual std::tuple<std::vector<hnswlib::labeltype>, std::vector<float>> query(std::vector<float> queryVector,
int k = 1, long queryEf = -1) = 0;
virtual std::tuple<std::vector<hnswlib::labeltype>, std::vector<float>>
query(std::vector<float> queryVector, int k = 1, long queryEf = -1) = 0;

virtual std::tuple<NDArray<hnswlib::labeltype, 2>, NDArray<float, 2>>
query(NDArray<float, 2> queryVectors, int k = 1, int numThreads = -1, long queryEf = -1) = 0;
query(NDArray<float, 2> queryVectors, int k = 1, int numThreads = -1,
long queryEf = -1) = 0;

virtual void markDeleted(hnswlib::labeltype label) = 0;
virtual void unmarkDeleted(hnswlib::labeltype label) = 0;
Expand Down
26 changes: 18 additions & 8 deletions cpp/src/Metadata.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,10 @@ namespace Metadata {
*/
class V1 {
public:
V1(int numDimensions, SpaceType spaceType, StorageDataType storageDataType, float maxNorm,
bool useOrderPreservingTransform)
: numDimensions(numDimensions), spaceType(spaceType), storageDataType(storageDataType), maxNorm(maxNorm),
V1(int numDimensions, SpaceType spaceType, StorageDataType storageDataType,
float maxNorm, bool useOrderPreservingTransform)
: numDimensions(numDimensions), spaceType(spaceType),
storageDataType(storageDataType), maxNorm(maxNorm),
useOrderPreservingTransform(useOrderPreservingTransform) {}

V1() {}
Expand All @@ -51,12 +52,20 @@ class V1 {

float getMaxNorm() { return maxNorm; }

bool getUseOrderPreservingTransform() const { return useOrderPreservingTransform; }
void setUseOrderPreservingTransform(bool newValue) { useOrderPreservingTransform = newValue; }
bool getUseOrderPreservingTransform() const {
return useOrderPreservingTransform;
}
void setUseOrderPreservingTransform(bool newValue) {
useOrderPreservingTransform = newValue;
}

void setNumDimensions(int newNumDimensions) { numDimensions = newNumDimensions; }
void setNumDimensions(int newNumDimensions) {
numDimensions = newNumDimensions;
}

void setStorageDataType(StorageDataType newStorageDataType) { storageDataType = newStorageDataType; }
void setStorageDataType(StorageDataType newStorageDataType) {
storageDataType = newStorageDataType;
}

void setSpaceType(SpaceType newSpaceType) { spaceType = newSpaceType; }

Expand Down Expand Up @@ -89,7 +98,8 @@ class V1 {
bool useOrderPreservingTransform;
};

static std::unique_ptr<Metadata::V1> loadFromStream(std::shared_ptr<InputStream> inputStream) {
static std::unique_ptr<Metadata::V1>
loadFromStream(std::shared_ptr<InputStream> inputStream) {
uint32_t header = inputStream->peek();
if (header != 'AYOV') {
return nullptr;
Expand Down
52 changes: 35 additions & 17 deletions cpp/src/Spaces/Euclidean.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,10 @@ namespace hnswlib {
* should automatically do the loop unrolling for us here and vectorize as
* appropriate.
*/
template <typename dist_t, typename data_t = dist_t, int K = 1, typename scalefactor = std::ratio<1, 1>>
static dist_t L2Sqr(const data_t *__restrict pVect1, const data_t *__restrict pVect2, const size_t qty) {
template <typename dist_t, typename data_t = dist_t, int K = 1,
typename scalefactor = std::ratio<1, 1>>
static dist_t L2Sqr(const data_t *__restrict pVect1,
const data_t *__restrict pVect2, const size_t qty) {
dist_t res = 0;

for (size_t i = 0; i < qty / K; i++) {
Expand All @@ -49,18 +51,22 @@ static dist_t L2Sqr(const data_t *__restrict pVect1, const data_t *__restrict pV
return (res * scale * scale);
}

template <typename dist_t, typename data_t = dist_t, int K, typename scalefactor = std::ratio<1, 1>>
static dist_t L2SqrAtLeast(const data_t *__restrict pVect1, const data_t *__restrict pVect2, const size_t qty) {
template <typename dist_t, typename data_t = dist_t, int K,
typename scalefactor = std::ratio<1, 1>>
static dist_t L2SqrAtLeast(const data_t *__restrict pVect1,
const data_t *__restrict pVect2, const size_t qty) {
size_t remainder = qty - K;

return L2Sqr<dist_t, data_t, K, scalefactor>(pVect1, pVect2, K) +
L2Sqr<dist_t, data_t, 1, scalefactor>(pVect1 + K, pVect2 + K, remainder);
L2Sqr<dist_t, data_t, 1, scalefactor>(pVect1 + K, pVect2 + K,
remainder);
}

#if defined(USE_AVX512)

// Favor using AVX512 if available.
static float L2SqrSIMD16Ext(const float *pVect1, const float *pVect2, const size_t qty) {
static float L2SqrSIMD16Ext(const float *pVect1, const float *pVect2,
const size_t qty) {
float PORTABLE_ALIGN64 TmpRes[16];
size_t qty16 = qty >> 4;

Expand All @@ -80,16 +86,19 @@ static float L2SqrSIMD16Ext(const float *pVect1, const float *pVect2, const size
}

_mm512_store_ps(TmpRes, sum);
float res = TmpRes[0] + TmpRes[1] + TmpRes[2] + TmpRes[3] + TmpRes[4] + TmpRes[5] + TmpRes[6] + TmpRes[7] +
TmpRes[8] + TmpRes[9] + TmpRes[10] + TmpRes[11] + TmpRes[12] + TmpRes[13] + TmpRes[14] + TmpRes[15];
float res = TmpRes[0] + TmpRes[1] + TmpRes[2] + TmpRes[3] + TmpRes[4] +
TmpRes[5] + TmpRes[6] + TmpRes[7] + TmpRes[8] + TmpRes[9] +
TmpRes[10] + TmpRes[11] + TmpRes[12] + TmpRes[13] + TmpRes[14] +
TmpRes[15];

return (res);
}

#elif defined(USE_AVX)

// Favor using AVX if available.
static float L2SqrSIMD16Ext(const float *pVect1, const float *pVect2, const size_t qty) {
static float L2SqrSIMD16Ext(const float *pVect1, const float *pVect2,
const size_t qty) {
float PORTABLE_ALIGN32 TmpRes[8];
size_t qty16 = qty >> 4;

Expand All @@ -115,12 +124,14 @@ static float L2SqrSIMD16Ext(const float *pVect1, const float *pVect2, const size
}

_mm256_store_ps(TmpRes, sum);
return TmpRes[0] + TmpRes[1] + TmpRes[2] + TmpRes[3] + TmpRes[4] + TmpRes[5] + TmpRes[6] + TmpRes[7];
return TmpRes[0] + TmpRes[1] + TmpRes[2] + TmpRes[3] + TmpRes[4] + TmpRes[5] +
TmpRes[6] + TmpRes[7];
}

#elif defined(USE_SSE)

static float L2SqrSIMD16Ext(const float *pVect1, const float *pVect2, const size_t qty) {
static float L2SqrSIMD16Ext(const float *pVect1, const float *pVect2,
const size_t qty) {
float PORTABLE_ALIGN32 TmpRes[8];
size_t qty16 = qty >> 4;

Expand Down Expand Up @@ -166,18 +177,21 @@ static float L2SqrSIMD16Ext(const float *pVect1, const float *pVect2, const size
#endif

#if defined(USE_SSE) || defined(USE_AVX) || defined(USE_AVX512)
static float L2SqrSIMD16ExtResiduals(const float *pVect1, const float *pVect2, const size_t qty) {
static float L2SqrSIMD16ExtResiduals(const float *pVect1, const float *pVect2,
const size_t qty) {
size_t qty16 = qty >> 4 << 4;
float res = L2SqrSIMD16Ext(pVect1, pVect2, qty16);

size_t qty_left = qty - qty16;
float res_tail = L2Sqr<float, float>(pVect1 + qty16, pVect2 + qty16, qty_left);
float res_tail =
L2Sqr<float, float>(pVect1 + qty16, pVect2 + qty16, qty_left);
return (res + res_tail);
}
#endif

#ifdef USE_SSE
static float L2SqrSIMD4Ext(const float *pVect1, const float *pVect2, const size_t qty) {
static float L2SqrSIMD4Ext(const float *pVect1, const float *pVect2,
const size_t qty) {
float PORTABLE_ALIGN32 TmpRes[8];
size_t qty4 = qty >> 2;

Expand All @@ -198,7 +212,8 @@ static float L2SqrSIMD4Ext(const float *pVect1, const float *pVect2, const size_
return TmpRes[0] + TmpRes[1] + TmpRes[2] + TmpRes[3];
}

static float L2SqrSIMD4ExtResiduals(const float *pVect1, const float *pVect2, const size_t qty) {
static float L2SqrSIMD4ExtResiduals(const float *pVect1, const float *pVect2,
const size_t qty) {
size_t qty4 = qty >> 2 << 2;

float res = L2SqrSIMD4Ext(pVect1, pVect2, qty4);
Expand All @@ -210,7 +225,8 @@ static float L2SqrSIMD4ExtResiduals(const float *pVect1, const float *pVect2, co
}
#endif

template <typename dist_t, typename data_t = dist_t, typename scalefactor = std::ratio<1, 1>>
template <typename dist_t, typename data_t = dist_t,
typename scalefactor = std::ratio<1, 1>>
class EuclideanSpace : public Space<dist_t, data_t> {
DISTFUNC<dist_t, data_t> fstdistfunc_;
size_t data_size_;
Expand Down Expand Up @@ -256,7 +272,9 @@ class EuclideanSpace : public Space<dist_t, data_t> {
~EuclideanSpace() {}
};

template <> EuclideanSpace<float, float>::EuclideanSpace(size_t dim) : data_size_(dim * sizeof(float)), dim_(dim) {
template <>
EuclideanSpace<float, float>::EuclideanSpace(size_t dim)
: data_size_(dim * sizeof(float)), dim_(dim) {
fstdistfunc_ = L2Sqr<float, float>;
#if defined(USE_SSE) || defined(USE_AVX) || defined(USE_AVX512)
if (dim % 16 == 0)
Expand Down
Loading

0 comments on commit 0532250

Please sign in to comment.