Skip to content

Hooks for calling FastLanguageModel.for_inference() when model.eval() is called #392

Closed

Description

@lapp0

It makes some intuitive sense to either have this hook by default or at least allow users to patch their model so this behavior exists. Would be great to be able to benefit from unsloths fast inference implementation without needing to update various trainers.

I've experimented with this in a modified DPO script and it works well.

https://gist.github.com/lapp0/e7d17884ed76669194c36e7fb3f64040#file-gistfile1-txt-L31-L44

Activity

  1. danielhanchen commented on May 1, 2024

    @danielhanchen
    Member

    @lapp0 Oh I think we internally use torch.inference_mode which will mimic .eval()

  2. added
    Discussion
    on May 1, 2024
  3. lapp0 commented on May 1, 2024

    @lapp0
    Author

    Sorry if I wasn't clear.

    I'm working on creating a generalized base class for trl which should work with PPOTrainer, DPOTrainer, KTOTrainer, ORPOTrainer, and RLOOTrainer. (But to start it should only impact PPOTrainer and `RLOOTrainer) huggingface/trl#1586

    Currently the plan is to check is_unsloth_available, and if so, call

    model.eval()
    if is_unsloth_available():
        FastLanguageModel.for_inference(model)
    model.generate(...)
    model.train()
    if is_unsloth_available()
        FastLanguageModel.for_training(model)
    

    Just thought it might make sense on your end to add a hook to model.eval() so your fast inference implementation is automatically applied for every trainer using your library which calls model.eval().

    Although on second thought, if you're calling torch.inference_mode(), that may break use cases where gradients are intended to be enabled during model.eval().

  4. danielhanchen commented on May 4, 2024

    @danielhanchen
    Member

    Oh interesting - fair point I actually did not know on eval being able to store gradients so its just like torch.no_grad() Also very cool with the PR!

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Discussion

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions