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

fix algorithm of spreading vectors over shards #3374

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

simshi
Copy link

@simshi simshi commented Apr 19, 2024

simple math:

input n input nshards shard_size idx i0 ni
19 6 4 5 20 -1
1000 37 28 36 1008 -8
1000 64 16 63 1008 -8

root cause:
integer cause precision loss, idx * shard_size overflows, because (n + nshards - 1) / nshards is roundup

my solution:
each shard takes at least base_shard_size = n / nshards, then remain = n % nshards, we know 0 <= remain < nshards, next, assign those remain vectors to first remain shards, i.e. first remain shards take one more vector each.

auto i0 = idx * base_shard_size;
if (i0 < remain) {
  // if current idx is one of the first `remain` shards
  i0 += idx;
} else {
  i0 += remain;
}

simplify above code: i0 = idx * base_shard_size + std::min(size_t(idx), n % nshards);

@facebook-github-bot
Copy link
Contributor

Hi @simshi!

Thank you for your pull request and welcome to our community.

Action Required

In 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.

Process

In 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 CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at cla@meta.com. Thanks!

@facebook-github-bot
Copy link
Contributor

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;
Copy link
Contributor

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?

Copy link
Author

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);
Copy link
Contributor

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?

Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants