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 BertScore on GPU #912

Merged
merged 11 commits into from
Mar 31, 2022
Merged

Fix BertScore on GPU #912

merged 11 commits into from
Mar 31, 2022

Conversation

SkafteNicki
Copy link
Member

@SkafteNicki SkafteNicki commented Mar 24, 2022

What does this PR do?

Fixes #909

Before submitting

  • Was this discussed/approved via a Github issue? (no need for typos and docs improvements)
  • Did you read the contributor guideline, Pull Request section?
  • Did you make sure to update the docs?
  • Did you write any new necessary tests?

PR review

Anyone in the community is free to review the PR once the tests have passed.
If we didn't discuss your PR in Github issues there's a high chance it will not be merged.

Did you have fun?

Make sure you had fun coding 🙃

@SkafteNicki SkafteNicki added the bug / fix Something isn't working label Mar 24, 2022
@SkafteNicki SkafteNicki added this to the v0.8 milestone Mar 24, 2022
@codecov
Copy link

codecov bot commented Mar 24, 2022

Codecov Report

Merging #912 (55770a1) into master (f055686) will increase coverage by 0%.
The diff coverage is 100%.

@@          Coverage Diff          @@
##           master   #912   +/-   ##
=====================================
  Coverage      95%    95%           
=====================================
  Files         173    173           
  Lines        7344   7344           
=====================================
+ Hits         6946   6963   +17     
+ Misses        398    381   -17     

@mergify mergify bot added the ready label Mar 24, 2022
@Borda Borda assigned SkafteNicki and stancld and unassigned SkafteNicki Mar 24, 2022
@stancld
Copy link
Contributor

stancld commented Mar 24, 2022

@SkafteNicki @Borda Won't be there any problem with GPU memory when the embeddings are going to be stored in its RAM?

@SkafteNicki
Copy link
Member Author

@stancld true, i guess the we need to move to GPU at some later point in the computation?

@Borda Borda self-requested a review March 24, 2022 16:36
@Borda Borda marked this pull request as draft March 25, 2022 01:11
@stancld
Copy link
Contributor

stancld commented Mar 25, 2022

Hi @SkafteNicki, I'd rather suggest the following :]. I can see that input_ids_idf wasn't enforced to move to cpu.

out = torch.einsum("blsd, bs -> blsd", out, processed_attention_mask)
        embeddings_list.append(out.cpu())

        # Calculate weighted (w.r.t. sentence length) input_ids IDF matrix
        input_ids_idf = (
            batch["input_ids_idf"] * processed_attention_mask if idf else processed_attention_mask.type(out.dtype)
        )
        input_ids_idf /= input_ids_idf.sum(-1, keepdim=True)
-         idf_scale_list.append(input_ids_idf)
+         idf_scale_list.append(input_ids_idf.cpu())

@stancld
Copy link
Contributor

stancld commented Mar 29, 2022

@SkafteNicki @Borda Please, have a look at minor change I made. I believe it can be marked as ready for review :)

@stancld stancld marked this pull request as ready for review March 29, 2022 16:39
Copy link
Member

@Borda Borda left a comment

Choose a reason for hiding this comment

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

can we add a test to cover this case? 🐰

@stancld
Copy link
Contributor

stancld commented Mar 29, 2022

can we add a test to cover this case? 🐰

Should be tested thanks to a test parameter added by @SkafteNicki:

@pytest.mark.parametrize("device", ["cpu", "cuda"])

Tests fail without our code change and with device="cuda".

@Borda Borda enabled auto-merge (squash) March 31, 2022 04:13
Copy link
Member

@justusschock justusschock left a comment

Choose a reason for hiding this comment

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

LGTM! Haven't looked at the implementation, but I assume it relies on .to(device=device), where we should also test for no explicit device

Co-authored-by: Justus Schock <[email protected]>
@Borda Borda requested a review from justusschock March 31, 2022 07:28
@Borda Borda merged commit 9e2a226 into master Mar 31, 2022
@Borda Borda deleted the bugfix/bert_gpu branch March 31, 2022 08:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug / fix Something isn't working ready topic: Text
Projects
None yet
Development

Successfully merging this pull request may close these issues.

BERTScore raises RuntimeError when device="cuda"
5 participants