Fix --rewrite default value and reduce redundant tensor allocations in inference pipelines#55
Open
Wizard-Guido wants to merge 1 commit intoTencent-Hunyuan:mainfrom
Conversation
7c46dd3 to
326a222
Compare
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
Bug Fix
--rewritedefault value: The--rewriteargument ingenerate.pyhaddefault=False, contradicting the README documentation, the help text ("default: true"), and the original design intent (the initial commit's README referenced a--disable_rewriteflag, indicating rewrite was meant to be on by default). Additionally, the code logs a warning when rewrite is disabled ("may affect the quality"), further confirming that enabled is the intended default. Changeddefault=False→default=Trueto match documented behavior.Performance Optimizations
device/dtypeat creation time instead oftorch.zeros(...).to(device)_prepare_cond_latentswhere three tensors were moved to CPU formerge_tensor_by_maskthen back to GPU — all operations can run directly on GPUt.repeat(n)with zero-copyt.expand(n)for read-only timestep broadcasting in the denoising looptorch.tensor([val]*n, dtype=float32).to(target_dtype) * 1000.0with a singletorch.full(...)call, avoiding Python list construction, intermediate tensor allocation, and redundant dtype conversionChanged Files
generate.py— fix--rewritedefaulthyvideo/pipelines/hunyuan_video_pipeline.py— tensor allocation optimizationshyvideo/pipelines/hunyuan_video_sr_pipeline.py— tensor allocation optimizationsOptimization Details
torch.zeros(...).to(device)torch.zeros(..., device=, dtype=)/zeros_like().cpu()+ merge +.to(device)t.repeat(n)t.expand(n)torch.tensor([v]*n)+.to(dtype)+*1000torch.full((n,), v*1000, dtype=)Test Plan
expandcompatibility (PyTorch ≥ 2.2), and zero-copy memory sharing