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

Remove accelerate.prepare hooks and FP32 conversion #858

Closed
DavidePaglieri opened this issue Nov 16, 2022 · 5 comments · Fixed by #860
Closed

Remove accelerate.prepare hooks and FP32 conversion #858

DavidePaglieri opened this issue Nov 16, 2022 · 5 comments · Fixed by #860
Assignees
Labels
enhancement New feature or request feature request Request for a new feature to be added to Accelerate

Comments

@DavidePaglieri
Copy link

DavidePaglieri commented Nov 16, 2022

When I use accelerator.prepare(model) the model gets some hooks used in accelerate. After I'm done with the training and want to save the model, those hooks are still somewhere in the .state_dict() of the model and I can't figure out where exactly and how to remove them.

This is a problem especially when I later want to load the model without accelerate and run it in FP16 with other models, because despite the model being loaded in FP16, I have the following accelerate decorator still somewhere moving the output in FP32:

class ConvertOutputsToFp32:
    """
    Decorator to apply to a function outputing tensors (like a model forward pass) that ensures the outputs in FP16
    precision will be convert back to FP32.

    Use a class instead of a decorator because otherwise, the prepared model can no longer be pickled (issue #273).

    Args:
        model_forward (`Callable`):
            The function which outputs we want to treat.

    Returns:
        The same function as `model_forward` but with converted outputs.
    """

    def __init__(self, model_forward):
        self.model_forward = model_forward
        update_wrapper(self, model_forward)

    def __call__(self, *args, **kwargs):
        return convert_to_fp32(self.model_forward(*args, **kwargs))

Is there a way I can remove all such hooks from the model trained with accelerate? I have tried remove_hook_from_module but it doesn't work.

Thank you!

@DavidePaglieri DavidePaglieri changed the title Accelerate.prepare hooks and FP32 conversion Remove accelerate.prepare hooks and FP32 conversion Nov 16, 2022
@sgugger
Copy link
Collaborator

sgugger commented Nov 16, 2022

There is no util to remove those for now (it's not the Hooks, which we use for big model inference, but more wrappers around the forward) but I see why you'd want a util like this! I think the unwrap_model method should remove them.

@muellerzr Would you have time to work on that?

@muellerzr
Copy link
Collaborator

@sgugger yep! Will assign myself

@muellerzr muellerzr self-assigned this Nov 16, 2022
@DavidePaglieri
Copy link
Author

Hi @sgugger thank you for your answer! I actually use:

model = accelerator.unwrap_model(model).to(dtype=dtype)

before saving it, still this ConvertOutputToFp32 remains somewhere in the state_dict of the model

@sgugger
Copy link
Collaborator

sgugger commented Nov 16, 2022

Yes, like I said there is nothing to remove it yet. The goal would be for this snippet of code to work once Zach is done :-)

@muellerzr muellerzr added enhancement New feature or request feature request Request for a new feature to be added to Accelerate labels Nov 16, 2022
@muellerzr
Copy link
Collaborator

Hi @DavidePaglieri, should be fixed with #860!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request feature request Request for a new feature to be added to Accelerate
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants