Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dump v #200

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
6 changes: 5 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -163,4 +163,8 @@ TARGET_LINK_LIBRARIES(find_with_missed_keys_test gtest_main)
add_executable(reserved_keys_test tests/reserved_keys_test.cc.cu)
target_compile_features(reserved_keys_test PUBLIC cxx_std_14)
set_target_properties(reserved_keys_test PROPERTIES CUDA_ARCHITECTURES OFF)
TARGET_LINK_LIBRARIES(reserved_keys_test gtest_main)
TARGET_LINK_LIBRARIES(reserved_keys_test gtest_main)

add_executable(export_batch_if_test tests/export_batch_if_test.cc.cu)
target_compile_features(export_batch_if_test PUBLIC cxx_std_14)
set_target_properties(export_batch_if_test PROPERTIES CUDA_ARCHITECTURES OFF)
101 changes: 101 additions & 0 deletions include/merlin/core_kernels.cuh
Original file line number Diff line number Diff line change
Expand Up @@ -910,5 +910,106 @@ __global__ void dump_kernel(const Table<K, V, S>* __restrict table,
}
}

template <class K, class V, class S, class VecV,
template <typename, typename> class PredFunctor, int TILE_SIZE>
__global__ void dump_kernel_v2(const Table<K, V, S>* __restrict table,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No call to this kernel?

Bucket<K, V, S>* buckets, const K pattern,
const S threshold, K* d_key, V* __restrict d_val,
S* __restrict d_score, const size_t offset,
const size_t search_length,
size_t* d_dump_counter) {
const size_t bucket_max_size = table->bucket_max_size;
int vec_dim = table->dim * sizeof(V) / sizeof(VecV);
auto g = cg::tiled_partition<TILE_SIZE>(cg::this_thread_block());

PredFunctor<K, S> pred;
size_t tid = static_cast<size_t>(blockIdx.x * blockDim.x + threadIdx.x);

for (size_t i = tid; i < search_length; i += gridDim.x * blockDim.x) {
size_t bkt_idx = (i + offset) / bucket_max_size;
size_t key_idx = (i + offset) % bucket_max_size;
size_t leading_key_idx = key_idx / TILE_SIZE * TILE_SIZE;
Bucket<K, V, S>* bucket = &(buckets[bkt_idx]);

const K key =
(bucket->keys(key_idx))->load(cuda::std::memory_order_relaxed);
S score = bucket->scores(key_idx)->load(cuda::std::memory_order_relaxed);

bool match =
(!IS_RESERVED_KEY<K>(key)) && pred(key, score, pattern, threshold);
unsigned int vote = g.ballot(match);
int tile_cnt = __popc(vote);
size_t tile_offset = 0;
if (g.thread_rank() == 0) {
tile_offset = atomicAdd(d_dump_counter, static_cast<size_t>(tile_cnt));
}
tile_offset = g.shfl(tile_offset, 0);
int bias_g = tile_cnt - __popc(vote >> (key_idx % TILE_SIZE));

if (match) {
d_key[tile_offset + bias_g] = key;
if (d_score) {
d_score[tile_offset + bias_g] = score;
}
}

#pragma unroll
for (int r = 0; r < TILE_SIZE; r++) {
unsigned int biased_vote = vote >> r;
bool cur_match = biased_vote & 1;
if (cur_match) {
int bias = tile_cnt - __popc(biased_vote);
size_t cur_idx = leading_key_idx + r;

VecV* d_val_vec = reinterpret_cast<VecV*>(d_val);
VecV* vec = reinterpret_cast<VecV*>(bucket->vectors);
for (int j = g.thread_rank(); j < vec_dim; j += TILE_SIZE) {
d_val_vec[(tile_offset + bias) * vec_dim + j] = vec[cur_idx * vec_dim + j];
}
}
}
}
}

template <class K, class V, class S,
template <typename, typename> class PredFunctor>
__global__ void size_if_kernel(const Table<K, V, S>* __restrict table,
Bucket<K, V, S>* buckets, const K pattern,
const S threshold, size_t* d_counter) {
extern __shared__ unsigned char s[];
KVM<K, V, S>* const block_tuples{reinterpret_cast<KVM<K, V, S>*>(s)};

const size_t bucket_max_size{table->bucket_max_size};

size_t local_acc = 0;
__shared__ size_t block_acc;
PredFunctor<K, S> pred;

const size_t tid{blockIdx.x * blockDim.x + threadIdx.x};

if (threadIdx.x == 0) {
block_acc = 0;
}
__syncthreads();

for (size_t i = tid; i < table->capacity; i += blockDim.x * gridDim.x) {
Bucket<K, V, S>* const bucket{&buckets[i / bucket_max_size]};

const int key_idx{static_cast<int>(i % bucket_max_size)};
const K key{(bucket->keys(key_idx))->load(cuda::std::memory_order_relaxed)};
S score = bucket->scores(key_idx)->load(cuda::std::memory_order_relaxed);

if ((!IS_RESERVED_KEY(key)) && pred(key, score, pattern, threshold)) {
++local_acc;
}
}
atomicAdd(&block_acc, local_acc);
__syncthreads();

if (threadIdx.x == 0) {
atomicAdd(d_counter, block_acc);
}
}

} // namespace merlin
} // namespace nv
94 changes: 80 additions & 14 deletions include/merlin_hashtable.cuh
Original file line number Diff line number Diff line change
Expand Up @@ -917,6 +917,8 @@ class HashTable : public HashTableBase<K, V, S> {
cudaDeviceProp deviceProp;
CUDA_CHECK(cudaGetDeviceProperties(&deviceProp, options_.device_id));
shared_mem_size_ = deviceProp.sharedMemPerBlock;
sm_cnt_ = deviceProp.multiProcessorCount;
max_threads_per_block_ = deviceProp.maxThreadsPerBlock;
create_table<key_type, value_type, score_type>(
&table_, allocator_, options_.dim, options_.init_capacity,
options_.max_capacity, options_.max_hbm_for_vectors,
Expand Down Expand Up @@ -2611,22 +2613,62 @@ class HashTable : public HashTableBase<K, V, S> {
return;
}
n = std::min(table_->capacity - offset, n);
if (n == 0) {
return;
}

const size_t score_size = scores ? sizeof(score_type) : 0;
const size_t kvm_size =
sizeof(key_type) + sizeof(value_type) * dim() + score_size;
const size_t block_size = std::min(shared_mem_size_ / 2 / kvm_size, 1024UL);
MERLIN_CHECK(
block_size > 0,
"[HierarchicalKV] block_size <= 0, the K-V-S size may be too large!");

const size_t shared_size = kvm_size * block_size;
const size_t grid_size = SAFE_GET_GRID_SIZE(n, block_size);
bool match_fast_cond = true;
const size_t value_size = sizeof(V) * dim();
auto check_tile_size = [&](int tile_size) {
return options_.max_bucket_size % tile_size == 0 &&
options_.max_bucket_size >= tile_size && offset % tile_size == 0 &&
n % tile_size == 0;
};
auto select_tile_size = [&](auto vec) {
using VecV = decltype(vec);
size_t vec_dim = value_size / sizeof(VecV);
if (vec_dim >= 32 && check_tile_size(32)) {
return dump_kernel_v2<key_type, value_type, score_type, VecV, PredFunctor, 32>;
} else if (vec_dim >= 16 && check_tile_size(16)) {
return dump_kernel_v2<key_type, value_type, score_type, VecV, PredFunctor, 16>;
} else if (vec_dim >= 8 && check_tile_size(8)) {
return dump_kernel_v2<key_type, value_type, score_type, VecV, PredFunctor, 8>;
}
match_fast_cond = false;
return dump_kernel<key_type, value_type, score_type, PredFunctor>;
};
auto kernel = [&] {
if (value_size >= sizeof(float4) * 8 && value_size % sizeof(float4) == 0) {
return select_tile_size(float4{});
} else if (value_size >= sizeof(float2) * 8 && value_size % sizeof(float2) == 0) {
return select_tile_size(float2{});
} else if (value_size >= sizeof(float) * 8 && value_size % sizeof(float) == 0) {
return select_tile_size(float{});
} else if (value_size >= sizeof(uint16_t) * 8 && value_size % sizeof(uint16_t) == 0) {
return select_tile_size(uint16_t{});
}
return select_tile_size(V{});
}();
size_t grid_size = 0, block_size = 0, shared_size = 0;
if (match_fast_cond) {
block_size = options_.block_size;
grid_size = std::min(sm_cnt_ * max_threads_per_block_ / block_size,
SAFE_GET_GRID_SIZE(n, block_size));
} else {
const size_t score_size = scores ? sizeof(score_type) : 0;
const size_t kvm_size =
sizeof(key_type) + sizeof(value_type) * dim() + score_size;
block_size = std::min(shared_mem_size_ / 2 / kvm_size, 1024UL);
MERLIN_CHECK(
block_size > 0,
"[HierarchicalKV] block_size <= 0, the K-V-S size may be too large!");

dump_kernel<key_type, value_type, score_type, PredFunctor>
<<<grid_size, block_size, shared_size, stream>>>(
d_table_, table_->buckets, pattern, threshold, keys, values, scores,
offset, n, d_counter);
shared_size = kvm_size * block_size;
grid_size = SAFE_GET_GRID_SIZE(n, block_size);
}
kernel<<<grid_size, block_size, shared_size, stream>>>(
d_table_, table_->buckets, pattern, threshold, keys, values,
scores, offset, n, d_counter);

CudaCheckError();
}
Expand Down Expand Up @@ -2668,6 +2710,28 @@ class HashTable : public HashTableBase<K, V, S> {
return h_size;
}

/**
* @brief Returns the number of keys if meet PredFunctor.
*
* @param stream The CUDA stream that is used to execute the operation.
* @return The table size match condiction of PredFunctor.
*/
template <template <typename, typename> class PredFunctor>
void size_if(const key_type& pattern, const score_type& threshold,
size_type* d_counter, cudaStream_t stream = 0) const {
read_shared_lock lock(mutex_, stream);
CUDA_CHECK(cudaMemsetAsync(d_counter, 0, sizeof(size_type), stream));

size_t grid_size = SAFE_GET_GRID_SIZE(capacity(), options_.block_size);
grid_size = std::min(grid_size,
static_cast<size_t>(sm_cnt_ * max_threads_per_block_ /
options_.block_size));
size_if_kernel<key_type, value_type, score_type, PredFunctor>
<<<grid_size, options_.block_size, 0, stream>>>(
d_table_, table_->buckets, pattern, threshold, d_counter);
CudaCheckError();
}

/**
* @brief Returns the hash table capacity.
*
Expand Down Expand Up @@ -3037,6 +3101,8 @@ class HashTable : public HashTableBase<K, V, S> {
TableCore* table_ = nullptr;
TableCore* d_table_ = nullptr;
size_t shared_mem_size_ = 0;
int sm_cnt_ = 0;
int max_threads_per_block_ = 0;
std::atomic_bool reach_max_capacity_{false};
bool initialized_ = false;
mutable group_shared_mutex mutex_;
Expand Down
Loading
Loading