feat: windows support (#216)
Preliminary support for Windows with remaining TODOs
This commit is contained in:
@@ -891,8 +891,10 @@ void KmeansCluster::update_features_thread(size_t column,
|
||||
for (size_t i = column, j = 0; i < shard_cluster_features_.size();
|
||||
i += cluster_count) {
|
||||
const std::vector<const void *> &it = shard_cluster_features_[i];
|
||||
std::memcpy(&cluster_features[j], it.data(), it.size() * sizeof(void *));
|
||||
j += it.size();
|
||||
if (!it.empty()) {
|
||||
std::memcpy(&cluster_features[j], it.data(), it.size() * sizeof(void *));
|
||||
j += it.size();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -327,8 +327,10 @@ void OptKmeansAlgorithm::update_features_thread(
|
||||
for (size_t i = column, j = 0; i < shard_cluster_features_.size();
|
||||
i += cluster_count) {
|
||||
const std::vector<const void *> &it = shard_cluster_features_[i];
|
||||
std::memcpy(&cluster_features[j], it.data(), it.size() * sizeof(void *));
|
||||
j += it.size();
|
||||
if (!it.empty()) {
|
||||
std::memcpy(&cluster_features[j], it.data(), it.size() * sizeof(void *));
|
||||
j += it.size();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -13,6 +13,7 @@
|
||||
// limitations under the License.
|
||||
#pragma once
|
||||
|
||||
#include <numeric>
|
||||
#include <zvec/core/framework/index_builder.h>
|
||||
#include <zvec/core/framework/index_helper.h>
|
||||
#include "flat_utility.h"
|
||||
|
||||
@@ -41,7 +41,6 @@ struct LinearIndexHeader {
|
||||
block_count(0),
|
||||
index_meta_size(0) {
|
||||
memset(reserved_, 0, sizeof(reserved_));
|
||||
memset(index_meta, 0, sizeof(index_meta));
|
||||
}
|
||||
uint32_t header_size{0};
|
||||
uint32_t total_vector_count{0};
|
||||
@@ -52,7 +51,11 @@ struct LinearIndexHeader {
|
||||
uint32_t block_count{0};
|
||||
uint32_t index_meta_size{0};
|
||||
char reserved_[28] = {0};
|
||||
#ifdef _MSC_VER
|
||||
char index_meta[];
|
||||
#else
|
||||
char index_meta[0];
|
||||
#endif
|
||||
};
|
||||
|
||||
/*! Index Format of Linear Index Meta for each Linear list
|
||||
|
||||
@@ -180,10 +180,10 @@ class FlatStreamer : public IndexStreamer {
|
||||
State state_{STATE_INIT};
|
||||
mutable std::mutex mapping_mutex_{};
|
||||
ailego::SharedMutex dump_mutex_{};
|
||||
FlatStreamerEntity entity_;
|
||||
bool column_major_order_{false};
|
||||
bool use_key_info_map_{true};
|
||||
uint32_t read_block_size_{0};
|
||||
FlatStreamerEntity entity_;
|
||||
};
|
||||
|
||||
} // namespace core
|
||||
|
||||
@@ -14,6 +14,7 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <numeric>
|
||||
#include "flat_streamer.h"
|
||||
#include "flat_utility.h"
|
||||
|
||||
|
||||
@@ -389,7 +389,6 @@ class FlatStreamerEntity {
|
||||
IndexStorage::Pointer storage_{};
|
||||
IndexMetric::MatrixDistance row_distance_{}, column_distance_{};
|
||||
mutable std::vector<IndexStorage::Segment::Pointer> segments_{};
|
||||
StreamerLinearMeta meta_{};
|
||||
IndexStreamer::Stats &stats_;
|
||||
mutable std::shared_ptr<ailego::SharedMutex> key_info_map_lock_{};
|
||||
std::unordered_map<uint64_t, VectorLocation> key_info_map_{};
|
||||
@@ -401,6 +400,7 @@ class FlatStreamerEntity {
|
||||
uint32_t vec_unit_size_{0};
|
||||
uint32_t vec_cols_{0};
|
||||
mutable std::string vec_buf_{};
|
||||
StreamerLinearMeta meta_{};
|
||||
};
|
||||
|
||||
} // namespace core
|
||||
|
||||
@@ -15,6 +15,7 @@
|
||||
#include "flat_sparse_streamer_entity.h"
|
||||
#include <cstdint>
|
||||
#include <memory>
|
||||
#include <numeric>
|
||||
#include <utility>
|
||||
#include <ailego/utility/memory_helper.h>
|
||||
#include <zvec/ailego/internal/platform.h>
|
||||
|
||||
@@ -136,8 +136,8 @@ int HnswBuilderEntity::get_vector(const node_id_t *ids, uint32_t count,
|
||||
int HnswBuilderEntity::get_vector(
|
||||
const node_id_t *ids, uint32_t count,
|
||||
std::vector<IndexStorage::MemoryBlock> &vec_blocks) const {
|
||||
const void *vecs[count];
|
||||
get_vector(ids, count, vecs);
|
||||
std::vector<const void *> vecs(count);
|
||||
get_vector(ids, count, vecs.data());
|
||||
for (uint32_t i = 0; i < count; ++i) {
|
||||
vec_blocks.emplace_back(IndexStorage::MemoryBlock((void *)vecs[i]));
|
||||
}
|
||||
|
||||
@@ -15,7 +15,6 @@
|
||||
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <atomic>
|
||||
#include <cstddef>
|
||||
#include <mutex>
|
||||
|
||||
@@ -145,11 +145,11 @@ class HnswDistCalculator {
|
||||
return dist(vec);
|
||||
}
|
||||
|
||||
dist_t operator()(id_t i) {
|
||||
dist_t operator()(node_id_t i) {
|
||||
return dist(i);
|
||||
}
|
||||
|
||||
dist_t operator()(id_t lhs, id_t rhs) {
|
||||
dist_t operator()(node_id_t lhs, node_id_t rhs) {
|
||||
return dist(lhs, rhs);
|
||||
}
|
||||
|
||||
|
||||
@@ -318,7 +318,8 @@ int64_t HnswEntity::dump_upper_neighbors(
|
||||
ailego_assert_with(!!neighbors.data, "invalid neighbors");
|
||||
ailego_assert_with(neighbors.size() <= neighbor_cnt(cur_level),
|
||||
"invalid neighbors");
|
||||
memset(buffer.data(), 0, sizeof(node_id_t) * buffer.size());
|
||||
size_t buffer_bytes = buffer.size() * sizeof(node_id_t);
|
||||
memset(buffer.data(), 0, buffer_bytes);
|
||||
buffer[0] = neighbors.size();
|
||||
if (neighbor_mapping.empty()) {
|
||||
memcpy(&buffer[1], &neighbors[0], neighbors.size() * sizeof(node_id_t));
|
||||
@@ -327,15 +328,13 @@ int64_t HnswEntity::dump_upper_neighbors(
|
||||
buffer[i + 1] = neighbor_mapping[neighbors[i]];
|
||||
}
|
||||
}
|
||||
if (dumper->write(buffer.data(), sizeof(node_id_t) * buffer.size()) !=
|
||||
sizeof(node_id_t) * buffer.size()) {
|
||||
if (dumper->write(buffer.data(), buffer_bytes) != buffer_bytes) {
|
||||
LOG_ERROR("Dump graph neighbor id=%u failed, size %lu", id,
|
||||
sizeof(node_id_t) * buffer.size());
|
||||
buffer_bytes);
|
||||
return IndexError_WriteData;
|
||||
}
|
||||
crc = ailego::Crc32c::Hash(buffer.data(),
|
||||
sizeof(node_id_t) * buffer.size(), crc);
|
||||
offset += sizeof(node_id_t) * buffer.size();
|
||||
crc = ailego::Crc32c::Hash(buffer.data(), buffer_bytes, crc);
|
||||
offset += buffer_bytes;
|
||||
}
|
||||
}
|
||||
size_t padding_size = 0;
|
||||
|
||||
@@ -138,7 +138,11 @@ struct HNSWHeader {
|
||||
|
||||
struct NeighborsHeader {
|
||||
uint32_t neighbor_cnt;
|
||||
#ifdef _MSC_VER
|
||||
node_id_t neighbors[];
|
||||
#else
|
||||
node_id_t neighbors[0];
|
||||
#endif
|
||||
};
|
||||
|
||||
struct Neighbors {
|
||||
|
||||
@@ -148,8 +148,8 @@ int HnswSearcherEntity::get_vector(const node_id_t *ids, uint32_t count,
|
||||
int HnswSearcherEntity::get_vector(
|
||||
const node_id_t *ids, uint32_t count,
|
||||
std::vector<IndexStorage::MemoryBlock> &vec_blocks) const {
|
||||
const void *vecs[count];
|
||||
get_vector(ids, count, vecs);
|
||||
std::vector<const void *> vecs(count);
|
||||
get_vector(ids, count, vecs.data());
|
||||
for (uint32_t i = 0; i < count; ++i) {
|
||||
vec_blocks.emplace_back(IndexStorage::MemoryBlock((void *)vecs[i]));
|
||||
}
|
||||
|
||||
@@ -89,7 +89,7 @@ int HnswStreamerEntity::update_neighbors(
|
||||
}
|
||||
|
||||
auto loc = get_neighbor_chunk_loc(level, id);
|
||||
size_t size = reinterpret_cast<char *>(&hd->neighbors[i]) - &buffer[0];
|
||||
size_t size = reinterpret_cast<char *>(&hd->neighbors[i]) - buffer.data();
|
||||
size_t ret = loc.first->write(loc.second, hd, size);
|
||||
if (ailego_unlikely(ret != size)) {
|
||||
LOG_ERROR("Write neighbor header failed, ret=%zu", ret);
|
||||
@@ -485,7 +485,9 @@ int HnswStreamerEntity::check_hnsw_index(const HNSWHeader *hd) const {
|
||||
int HnswStreamerEntity::add_vector(level_t level, key_t key, const void *vec,
|
||||
node_id_t *id) {
|
||||
Chunk::Pointer node_chunk;
|
||||
size_t chunk_offset = -1UL;
|
||||
// On MSVC, unsigned long is 32-bit, so -1UL is 0xFFFFFFFF not
|
||||
// 0xFFFFFFFFFFFFFFFF.
|
||||
size_t chunk_offset = static_cast<size_t>(-1);
|
||||
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
// duplicate check
|
||||
@@ -558,7 +560,7 @@ int HnswStreamerEntity::add_vector(level_t level, key_t key, const void *vec,
|
||||
int HnswStreamerEntity::add_vector_with_id(level_t level, node_id_t id,
|
||||
const void *vec) {
|
||||
Chunk::Pointer node_chunk;
|
||||
size_t chunk_offset = -1UL;
|
||||
size_t chunk_offset = static_cast<size_t>(-1);
|
||||
key_t key = id;
|
||||
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
|
||||
@@ -331,10 +331,10 @@ class HnswStreamerEntity : public HnswEntity {
|
||||
return 0;
|
||||
}
|
||||
Chunk::Pointer chunk;
|
||||
uint64_t chunk_offset = -1UL;
|
||||
uint64_t chunk_offset = UINT64_MAX;
|
||||
size_t neighbors_size = get_total_upper_neighbors_size(level);
|
||||
uint64_t chunk_index = upper_neighbor_chunks_.size() - 1UL;
|
||||
if (chunk_index == -1UL ||
|
||||
uint64_t chunk_index = upper_neighbor_chunks_.size() - 1ULL;
|
||||
if (chunk_index == UINT64_MAX ||
|
||||
(upper_neighbor_chunks_[chunk_index]->padding_size() <
|
||||
neighbors_size)) { // no space left and need to alloc
|
||||
chunk_index++;
|
||||
|
||||
@@ -170,8 +170,8 @@ int HnswSparseBuilderEntity::get_vector_metas(const node_id_t *ids,
|
||||
int HnswSparseBuilderEntity::get_vector_metas(
|
||||
const node_id_t *ids, uint32_t count,
|
||||
std::vector<IndexStorage::MemoryBlock> &block_vecs) const {
|
||||
const void *vecs[count];
|
||||
get_vector_metas(ids, count, vecs);
|
||||
std::vector<const void *> vecs(count);
|
||||
get_vector_metas(ids, count, vecs.data());
|
||||
for (uint32_t i = 0; i < count; ++i) {
|
||||
block_vecs.emplace_back(IndexStorage::MemoryBlock((void *)vecs[i]));
|
||||
}
|
||||
|
||||
@@ -15,7 +15,6 @@
|
||||
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <atomic>
|
||||
#include <cstddef>
|
||||
#include <mutex>
|
||||
|
||||
@@ -138,11 +138,11 @@ class HnswSparseDistCalculator {
|
||||
return dist(vec);
|
||||
}
|
||||
|
||||
dist_t operator()(id_t i) {
|
||||
dist_t operator()(node_id_t i) {
|
||||
return dist(i);
|
||||
}
|
||||
|
||||
dist_t operator()(id_t lhs, id_t rhs) {
|
||||
dist_t operator()(node_id_t lhs, node_id_t rhs) {
|
||||
return dist(lhs, rhs);
|
||||
}
|
||||
|
||||
|
||||
@@ -13,6 +13,7 @@
|
||||
// limitations under the License.
|
||||
|
||||
#include "hnsw_sparse_entity.h"
|
||||
#include <numeric>
|
||||
|
||||
namespace zvec {
|
||||
namespace core {
|
||||
@@ -381,6 +382,7 @@ int64_t HnswSparseEntity::dump_upper_neighbors(
|
||||
size_t offset = 0;
|
||||
uint32_t crc = 0;
|
||||
std::vector<node_id_t> buffer(upper_neighbor_cnt() + 1);
|
||||
size_t buffer_bytes = buffer.size() * sizeof(node_id_t);
|
||||
for (node_id_t id = 0; id < doc_cnt(); ++id) {
|
||||
node_id_t new_id = reorder_mapping.empty() ? id : reorder_mapping[id];
|
||||
auto level = get_level(new_id);
|
||||
@@ -395,7 +397,7 @@ int64_t HnswSparseEntity::dump_upper_neighbors(
|
||||
ailego_assert_with(!!neighbors.data, "invalid neighbors");
|
||||
ailego_assert_with(neighbors.size() <= neighbor_cnt(cur_level),
|
||||
"invalid neighbors");
|
||||
memset(buffer.data(), 0, sizeof(node_id_t) * buffer.size());
|
||||
memset(buffer.data(), 0, buffer_bytes);
|
||||
buffer[0] = neighbors.size();
|
||||
if (neighbor_mapping.empty()) {
|
||||
memcpy(&buffer[1], &neighbors[0], neighbors.size() * sizeof(node_id_t));
|
||||
@@ -404,15 +406,13 @@ int64_t HnswSparseEntity::dump_upper_neighbors(
|
||||
buffer[i + 1] = neighbor_mapping[neighbors[i]];
|
||||
}
|
||||
}
|
||||
if (dumper->write(buffer.data(), sizeof(node_id_t) * buffer.size()) !=
|
||||
sizeof(node_id_t) * buffer.size()) {
|
||||
if (dumper->write(buffer.data(), buffer_bytes) != buffer_bytes) {
|
||||
LOG_ERROR("Dump graph neighbor id=%u failed, size %lu", id,
|
||||
sizeof(node_id_t) * buffer.size());
|
||||
buffer_bytes);
|
||||
return IndexError_WriteData;
|
||||
}
|
||||
crc = ailego::Crc32c::Hash(buffer.data(),
|
||||
sizeof(node_id_t) * buffer.size(), crc);
|
||||
offset += sizeof(node_id_t) * buffer.size();
|
||||
crc = ailego::Crc32c::Hash(buffer.data(), buffer_bytes, crc);
|
||||
offset += buffer_bytes;
|
||||
}
|
||||
}
|
||||
size_t padding_size = 0;
|
||||
|
||||
@@ -161,7 +161,11 @@ struct HNSWSparseHeader {
|
||||
|
||||
struct NeighborsHeader {
|
||||
uint32_t neighbor_cnt;
|
||||
#ifdef _MSC_VER
|
||||
node_id_t neighbors[];
|
||||
#else
|
||||
node_id_t neighbors[0];
|
||||
#endif
|
||||
};
|
||||
|
||||
struct Neighbors {
|
||||
|
||||
@@ -155,8 +155,8 @@ int HnswSparseSearcherEntity::get_vector_metas(const node_id_t *ids,
|
||||
int HnswSparseSearcherEntity::get_vector_metas(
|
||||
const node_id_t *ids, uint32_t count,
|
||||
std::vector<IndexStorage::MemoryBlock> &block_vecs) const {
|
||||
const void *vecs[count];
|
||||
get_vector_metas(ids, count, vecs);
|
||||
std::vector<const void *> vecs(count);
|
||||
get_vector_metas(ids, count, vecs.data());
|
||||
for (uint32_t i = 0; i < count; ++i) {
|
||||
block_vecs.emplace_back(IndexStorage::MemoryBlock((void *)vecs[i]));
|
||||
}
|
||||
|
||||
@@ -93,7 +93,7 @@ int HnswSparseStreamerEntity::update_neighbors(
|
||||
}
|
||||
|
||||
auto loc = get_neighbor_chunk_loc(level, id);
|
||||
size_t size = reinterpret_cast<char *>(&hd->neighbors[i]) - &buffer[0];
|
||||
size_t size = reinterpret_cast<char *>(&hd->neighbors[i]) - buffer.data();
|
||||
size_t ret = loc.first->write(loc.second, hd, size);
|
||||
if (ailego_unlikely(ret != size)) {
|
||||
LOG_ERROR("Write neighbor header failed, ret=%zu", ret);
|
||||
@@ -607,8 +607,8 @@ int HnswSparseStreamerEntity::add_vector(level_t level, key_t key,
|
||||
SparseChunk::Pointer node_chunk;
|
||||
SparseChunk::Pointer sparse_node_chunk;
|
||||
|
||||
size_t chunk_offset = -1UL;
|
||||
size_t sparse_chunk_offset = -1UL;
|
||||
size_t chunk_offset = static_cast<size_t>(-1);
|
||||
size_t sparse_chunk_offset = static_cast<size_t>(-1);
|
||||
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
// duplicate check
|
||||
@@ -744,8 +744,8 @@ int HnswSparseStreamerEntity::add_vector_with_id(level_t level, node_id_t id,
|
||||
key_t key = id;
|
||||
SparseChunk::Pointer node_chunk;
|
||||
SparseChunk::Pointer sparse_node_chunk;
|
||||
size_t chunk_offset = -1UL;
|
||||
size_t sparse_chunk_offset = -1UL;
|
||||
size_t chunk_offset = static_cast<size_t>(-1);
|
||||
size_t sparse_chunk_offset = static_cast<size_t>(-1);
|
||||
|
||||
// allocat sparse chunk
|
||||
uint32_t sparse_vector_len = sparse_vec.size();
|
||||
|
||||
@@ -360,10 +360,10 @@ class HnswSparseStreamerEntity : public HnswSparseEntity {
|
||||
return 0;
|
||||
}
|
||||
SparseChunk::Pointer chunk;
|
||||
uint64_t chunk_offset = -1UL;
|
||||
uint64_t chunk_offset = UINT64_MAX;
|
||||
size_t neighbors_size = get_total_upper_neighbors_size(level);
|
||||
uint64_t chunk_index = upper_neighbor_chunks_.size() - 1UL;
|
||||
if (chunk_index == -1UL ||
|
||||
uint64_t chunk_index = upper_neighbor_chunks_.size() - 1ULL;
|
||||
if (chunk_index == UINT64_MAX ||
|
||||
(upper_neighbor_chunks_[chunk_index]->padding_size() <
|
||||
neighbors_size)) { // no space left and need to alloc
|
||||
chunk_index++;
|
||||
|
||||
@@ -249,11 +249,11 @@ class IVFDumper {
|
||||
size_t block_vector_count_{kDefaultBlockCount};
|
||||
std::vector<InvertedListMeta> inverted_lists_meta_{};
|
||||
std::vector<uint64_t> keys_{};
|
||||
InvertedIndexHeader header_{};
|
||||
uint32_t cur_list_id_{0};
|
||||
uint32_t dumped_feature_count_{0};
|
||||
size_t dumped_features_size_{0};
|
||||
mutable size_t dumped_size_{0};
|
||||
InvertedIndexHeader header_;
|
||||
};
|
||||
|
||||
} // namespace core
|
||||
|
||||
@@ -333,7 +333,6 @@ class IVFEntity {
|
||||
IndexMeta meta_{};
|
||||
mutable IVFReformerWrapper reformer_{};
|
||||
IVFDistanceCalculator::Pointer calculator_{};
|
||||
InvertedIndexHeader header_{};
|
||||
IndexStorage::Pointer container_{};
|
||||
IndexStorage::Segment::Pointer inverted_{};
|
||||
IndexStorage::Segment::Pointer inverted_meta_{};
|
||||
@@ -345,6 +344,7 @@ class IVFEntity {
|
||||
mutable std::string vector_{}; // temporary buffer for colomn major order
|
||||
float norm_value_{0.0f}; // normalize the inverted vector to orignal score
|
||||
bool norm_value_sqrt_{false}; // does the norm value need to sqrt
|
||||
InvertedIndexHeader header_;
|
||||
};
|
||||
|
||||
} // namespace core
|
||||
|
||||
@@ -19,6 +19,9 @@
|
||||
namespace zvec {
|
||||
namespace core {
|
||||
|
||||
using node_id_t = uint32_t;
|
||||
using key_t = uint64_t;
|
||||
|
||||
static constexpr uint64_t kInvalidKey = std::numeric_limits<uint64_t>::max();
|
||||
|
||||
/*! Index Format of Inverted Index Header
|
||||
@@ -33,7 +36,11 @@ struct InvertedIndexHeader {
|
||||
uint32_t block_count{0};
|
||||
uint32_t index_meta_size{0};
|
||||
char reserved_[28];
|
||||
#ifdef _MSC_VER
|
||||
char index_meta[];
|
||||
#else
|
||||
char index_meta[0];
|
||||
#endif
|
||||
};
|
||||
|
||||
/*! Index Format of Inverted Index Meta for each Inverted list
|
||||
|
||||
@@ -35,22 +35,22 @@ namespace core {
|
||||
#endif
|
||||
|
||||
#ifndef ivf_check_with_msg
|
||||
#define ivf_check_with_msg(code, fmt, args...) \
|
||||
do { \
|
||||
if (ailego_unlikely((code) != 0)) { \
|
||||
LOG_ERROR(fmt, ##args); \
|
||||
return code; \
|
||||
} \
|
||||
#define ivf_check_with_msg(code, fmt, ...) \
|
||||
do { \
|
||||
if (ailego_unlikely((code) != 0)) { \
|
||||
LOG_ERROR(fmt, ##__VA_ARGS__); \
|
||||
return code; \
|
||||
} \
|
||||
} while (0)
|
||||
#endif
|
||||
|
||||
#ifndef ivf_assert_with_msg
|
||||
#define ivf_assert_with_msg(cond, err, fmt, args...) \
|
||||
do { \
|
||||
if (ailego_unlikely(!(cond))) { \
|
||||
LOG_ERROR(fmt, ##args); \
|
||||
return err; \
|
||||
} \
|
||||
#define ivf_assert_with_msg(cond, err, fmt, ...) \
|
||||
do { \
|
||||
if (ailego_unlikely(!(cond))) { \
|
||||
LOG_ERROR(fmt, ##__VA_ARGS__); \
|
||||
return err; \
|
||||
} \
|
||||
} while (0)
|
||||
#endif
|
||||
|
||||
|
||||
Reference in New Issue
Block a user