Skip to content

⚡ Optimize time embedding by precomputing static frequencies#2

Open
priths7 wants to merge 1 commit into
mainfrom
performance-opt-time-embedding-13315484921489277927
Open

⚡ Optimize time embedding by precomputing static frequencies#2
priths7 wants to merge 1 commit into
mainfrom
performance-opt-time-embedding-13315484921489277927

Conversation

@priths7
Copy link
Copy Markdown
Owner

@priths7 priths7 commented May 6, 2026

💡 What: This optimization precomputes the static frequency tensor used for time embeddings once outside the main inference loop in sd/pipeline.py.

🎯 Why: In the original implementation, the freqs tensor was recalculated, reallocated, and moved to the device in every single iteration of the sampling loop (typically 50 times per image generation). Since these frequencies only depend on constant values (10000 and 160), they are static throughout the entire inference process.

📊 Measured Improvement: While environmental limitations (lack of torch in the available environment's search path) prevented running a live benchmark, this change provides a direct efficiency boost by:

  1. Reducing tensor allocations from $N$ to 1.
  2. Reducing the number of mathematical operations (arange, division, pow) from $N$ to 1.
  3. Eliminating $N$ redundant .to(device) calls within the loop.
    For a standard 50-step inference, this removes 49 redundant sets of computations and allocations.

PR created automatically by Jules for task 13315484921489277927 started by @priths7

- Precompute `freqs` tensor in `generate` before the inference loop.
- Update `get_time_embedding` to accept optional `freqs` parameter for backward compatibility.
- Create `freqs` directly on the target device to avoid redundant transfers.
- Update benchmark script with the new signature.

Co-authored-by: priths7 <45918183+priths7@users.noreply.github.com>
@google-labs-jules
Copy link
Copy Markdown

👋 Jules, reporting for duty! I'm here to lend a hand with this pull request.

When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down.

I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job!

For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with @jules. You can find this option in the Pull Request section of your global Jules UI settings. You can always switch back!

New to Jules? Learn more at jules.google/docs.


For security, I will only act on instructions from the user who triggered this task.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant