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

Add is_initialized method and refactor #949

Merged
merged 5 commits into from
Jan 3, 2023
Merged

Add is_initialized method and refactor #949

merged 5 commits into from
Jan 3, 2023

Conversation

muellerzr
Copy link
Collaborator

Add a static is_initialized method and refactor existing logic for initialization check

What does this add?

This PR introduces a static is_initialized method which will check if the state's dictionary is empty or not. It also refactors current methods to have initialized be a @property check of the same to increase code clarity towards what is happening under the hood.

Who is it for?

Closes #933

Why is it needed?

The feature request asked for a similar API to what torch uses for cuda, i.e. torch.cuda.is_initialized() for Accelerate, so a user can do import accelerate; accelerate.state.is_initialized()

What parts of the API does this impact?

User-facing:

A new is_initialized method was added to the accelerate.state module

Internal structure:

initialized was changed to be a dictionary check to mimic how the new is_initialized() function operates

Basic Usage Example(s):

# One way:
import accelerate
accelerate.state.is_initialized()

# Or:
from accelerate import Accelerator
accelerator = Accelerator()
accelerator.state.initialized

When would I use it, and when wouldn't I?

When a static method is needed to check if the AcceleratorState has been initialized without doing a getattr, or a cleaner interface to present to the user

@muellerzr muellerzr added the enhancement New feature or request label Jan 3, 2023
@muellerzr muellerzr requested a review from sgugger January 3, 2023 14:33
@HuggingFaceDocBuilderDev
Copy link

HuggingFaceDocBuilderDev commented Jan 3, 2023

The documentation is not available anymore as the PR was closed or merged.

Copy link
Collaborator

@sgugger sgugger 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 adding this!

@muellerzr muellerzr merged commit bf8fe03 into main Jan 3, 2023
@muellerzr muellerzr deleted the isinit branch January 3, 2023 15:13
@muellerzr muellerzr mentioned this pull request Jan 3, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[feature request] add accelerate.state.is_initialized()
3 participants