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
fix algorithm of spreading vectors over shards #3374
base: main
Are you sure you want to change the base?
Conversation
Hi @simshi! Thank you for your pull request and welcome to our community. Action RequiredIn order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you. ProcessIn order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA. Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with If you have received this in error or have any questions, please contact us at cla@meta.com. Thanks! |
Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks! |
@@ -82,7 +82,7 @@ void GpuIcmEncoder::encode( | |||
size_t n, | |||
size_t ils_iters) const { | |||
size_t nshards = shards->size(); | |||
size_t shard_size = (n + nshards - 1) / nshards; | |||
size_t base_shard_size = n / nshards; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is it (n / nshards) or (n - 1)/ nshards?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
integer computing, round down already. remain = n - nshards * base_shard_size
, then 0 <= remain < nshards
, so each shard takes at least base_shard_size
vectors, and the first remain
shards takes one extra vector each.
@@ -94,8 +94,14 @@ void GpuIcmEncoder::encode( | |||
|
|||
// split input data | |||
auto fn = [=](int idx, IcmEncoderImpl* encoder) { | |||
size_t i0 = idx * shard_size; | |||
size_t ni = std::min(shard_size, n - i0); | |||
size_t i0 = idx * base_shard_size + std::min(size_t(idx), n % nshards); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why is it std::min(size_t(idx), n % nshards)
? Btw, should be (n-1) % nshards
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
as my previous comment, you can verify it over the case n % nshards == 0
, then (n -1) % nshards
is meaningless
simple math:
root cause:
integer cause precision loss,
idx * shard_size
overflows, because(n + nshards - 1) / nshards
is roundupmy solution:
each shard takes at least
base_shard_size = n / nshards
, thenremain = n % nshards
, we know0 <= remain < nshards
, next, assign those remain vectors to firstremain
shards, i.e. firstremain
shards take one more vector each.simplify above code:
i0 = idx * base_shard_size + std::min(size_t(idx), n % nshards);