fix: wrap iterative generation in torch.inference_mode() to avoid autograd overhead#219
Open
LeSoviet wants to merge 1 commit into
Open
fix: wrap iterative generation in torch.inference_mode() to avoid autograd overhead#219LeSoviet wants to merge 1 commit into
LeSoviet wants to merge 1 commit into
Conversation
LeSoviet
force-pushed
the
fix/inference-mode
branch
from
July 11, 2026 10:46
763c75c to
d8b8969
Compare
Author
|
Note: the cross-reference to my fork is for personal tracking only, not related to this fix. |
Collaborator
|
The generate() function is already decorated with @torch.inference_mode(). Therefore, wrapping _generate_iterative again is unlikely to yield additional speedups. The observed speed gain may stem from random measurement noise. To obtain more reliable results, we should benchmark the RTF on a list of samples with proper warm-up runs. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
The iterative generation loop in
_generate_iterativeruns N forward passes of the transformer withouttorch.inference_mode(). Since inference never needs gradients, PyTorch builds the autograd graph on every step, adding unnecessary VRAM overhead and slowing down each forward pass.This affects all users on any hardware (not AMD-specific).
Problem
In
_generate_iterative(omnivoice/models/omnivoice.py, ~line 1147), the decoding loop callsself(...)(forward of the transformer) for each of thenum_stepiterations. No inference context is active, so autograd tracks operations and reserves gradient memory on every step.The library already uses
@torch.inference_mode()onfrom_pretrained(line 324) andgenerate(line 476), but it's missing on_generate_iterative.Fix
Add
@torch.inference_mode()decorator to_generate_iterative:_generate_chunkedcalls_generate_iterativeinternally, so it's covered automaticallyBenchmarks
Same text (~160 chars),
num_step=48,class_temperature=0.0,guidance_scale=2.0:Environment
k2-fsa/OmniVoice, dtype=float16Related issue: #218