-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding function to compute codes for embeddings.
- Loading branch information
1 parent
beb65be
commit 76a68ef
Showing
1 changed file
with
18 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,25 @@ | ||
using ..ColBERT: ColBERTConfig | ||
using ProtoStructs | ||
|
||
@proto mutable struct ResidualCodec | ||
mutable struct ResidualCodec | ||
config::ColBERTConfig | ||
centroids::Matrix{Float64} | ||
avg_residual:: Float64 | ||
avg_residual::Float64 | ||
bucket_cutoffs::Vector{Float64} | ||
bucket_weights::Vector{Float64} | ||
end | ||
|
||
function compress_into_codes(codec::ResidualCodec, embs::Matrix{Float64}) | ||
codes = [] | ||
|
||
bsize = Int(floor((1 << 29) / size(codec.centroids)[2])) | ||
offset = 1 | ||
while (offset <= size(embs)[2]) # batch on the second dimension | ||
dot_products = transpose(embs[:, offset:min(size(embs)[2], offset + bsize - 1)]) * codec.centroids | ||
indices = (cartesian_index -> cartesian_index.I[2]).(argmax(dot_products, dims = 2)[:, 1]) | ||
append!(codes, indices) | ||
offset += bsize | ||
end | ||
|
||
codes | ||
end | ||
|