You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Hooks for calling FastLanguageModel.for_inference() when model.eval() is called #392
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.
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().
Activity
danielhanchen commentedon May 1, 2024
@lapp0 Oh I think we internally use
torch.inference_modewhich will mimic.eval()lapp0 commentedon May 1, 2024
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, andRLOOTrainer. (But to start it should only impactPPOTrainerand `RLOOTrainer) huggingface/trl#1586Currently the plan is to check
is_unsloth_available, and if so, callJust 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 callsmodel.eval().Although on second thought, if you're calling
torch.inference_mode(), that may break use cases where gradients are intended to be enabled duringmodel.eval().danielhanchen commentedon May 4, 2024
Oh interesting - fair point I actually did not know on
evalbeing able to store gradients so its just liketorch.no_grad()Also very cool with the PR!