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 get_backend bug and add clear_device_cache function #2857

Merged
merged 2 commits into from
Jul 3, 2024

Conversation

NurmaU
Copy link
Contributor

@NurmaU NurmaU commented Jun 14, 2024

What does this PR do?

This PR introduces 2 updates:

  1. adds new clear_device_cache function in memory.py to replace repetitive code. This also adds check for mps devices in function find_executable_batch_size. This feature was tested with tests/test_memory_utils.py, which had bug.
  2. fixes bug in get_backend function where function's 3rd outputs must be callable object. This bug caused failures in tests/test_memory_utils.py on MacOS.

Before submitting

  • This PR fixes a typo or improves the docs (you can dismiss the other checks if that's the case).
  • Did you read the contributor guideline,
    Pull Request section?
  • Was this discussed/approved via a Github issue or the forum? Please add a link
    to it if that's the case.
  • Did you make sure to update the documentation with your changes? Here are the
    documentation guidelines, and
    here are tips on formatting docstrings.
  • Did you write any new necessary tests?

Who can review?

Anyone in the community is free to review the PR once the tests have passed. Feel free to tag
members/contributors who may be interested in your PR.

@HuggingFaceDocBuilderDev

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

@@ -64,7 +64,7 @@ def get_backend():
elif is_cuda_available():
return "cuda", torch.cuda.device_count(), torch.cuda.memory_allocated
elif is_mps_available(min_version="2.0"):
return "mps", 1, torch.mps.current_allocated_memory()
return "mps", 1, torch.mps.current_allocated_memory
Copy link
Member

Choose a reason for hiding this comment

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

Nice catch. But doesn't this mean that the return values below "mps", 1, 0 (for old mps) and "cpu", 1, 0 (for CPU) are also broken? Should they return lambda: 0 instead?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Agreed, we should use a lambda for those

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, you're right. Added lambda: 0. Thanks for pointing that out!

Copy link
Collaborator

@muellerzr muellerzr left a comment

Choose a reason for hiding this comment

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

Thanks for the refactor, and sorry for the delay in getting this in!

@muellerzr muellerzr merged commit 8330b37 into huggingface:main Jul 3, 2024
23 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants