Skip to content

Conversation

@ZeyuChen
Copy link
Member

💡 What: Cached the calculation of inv_freq (inverse frequency) tensors in Rotary Embedding layers using functools.lru_cache.

🎯 Why: The inv_freq tensor depends only on rotary_dim, base, and the device. In the current implementation, it is recomputed every time __call__ is invoked (for some layers) or every time a new layer instance is created (which happens frequently for multimodal models in GPUModelRunner._process_mm_features). Caching this computation saves tensor allocation and arithmetic operations.

📊 Impact: Reduces overhead in the hot path of rotary embedding calculation. For multimodal models, get_rope is called for every batch, so this avoids O(N) operations per batch.

🔬 Measurement: Verified by code inspection that redundant calculations are replaced by a cached function call. The cached function ensures the tensor is returned on the correct device.


PR created automatically by Jules for task 13163577974838169073 started by @ZeyuChen

This change optimizes the calculation of `inv_freq` in rotary embedding layers
by caching the result using `functools.lru_cache`. This avoids redundant
tensor creation and mathematical operations (pow, division) during every
forward pass or batch initialization, particularly benefiting multimodal
models where these layers are frequently instantiated or called.

- Introduce `get_inv_freq` cached function in `fastdeploy/model_executor/layers/rotary_embedding.py`.
- Update `ErnieRotaryEmbedding`, `GlmRotaryEmbedding`, `QwenRotaryEmbedding` to use cached `inv_freq`.
- Update `ErnieVlRotaryEmbedding3D` and `QwenVlRotaryEmbedding3D` to use cached `inv_freq`.

Co-authored-by: ZeyuChen <1371212+ZeyuChen@users.noreply.github.com>
@google-labs-jules
Copy link
Contributor

👋 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.

@CLAassistant
Copy link

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

@paddle-bot
Copy link

paddle-bot bot commented Feb 12, 2026

Thanks for your contribution!

This change optimizes the calculation of `inv_freq` in rotary embedding layers
by caching the result using `functools.lru_cache`. This avoids redundant
tensor creation and mathematical operations (pow, division) during every
forward pass or batch initialization, particularly benefiting multimodal
models where these layers are frequently instantiated or called.

- Introduce `get_inv_freq` cached function in `fastdeploy/model_executor/layers/rotary_embedding.py`.
- Update `ErnieRotaryEmbedding`, `GlmRotaryEmbedding`, `QwenRotaryEmbedding` to use cached `inv_freq`.
- Update `ErnieVlRotaryEmbedding3D` and `QwenVlRotaryEmbedding3D` to use cached `inv_freq`.

Co-authored-by: ZeyuChen <1371212+ZeyuChen@users.noreply.github.com>
This change optimizes the calculation of `inv_freq` in rotary embedding layers
by caching the result using `functools.lru_cache`. This avoids redundant
tensor creation and mathematical operations (pow, division) during every
forward pass or batch initialization, particularly benefiting multimodal
models where these layers are frequently instantiated or called.

- Introduce `get_inv_freq` cached function in `fastdeploy/model_executor/layers/rotary_embedding.py`.
- Update `ErnieRotaryEmbedding`, `GlmRotaryEmbedding`, `QwenRotaryEmbedding` to use cached `inv_freq`.
- Update `ErnieVlRotaryEmbedding3D` and `QwenVlRotaryEmbedding3D` to use cached `inv_freq`.

Co-authored-by: ZeyuChen <1371212+ZeyuChen@users.noreply.github.com>
This change optimizes the calculation of `inv_freq` in rotary embedding layers
by caching the result using `functools.lru_cache`. This avoids redundant
tensor creation and mathematical operations (pow, division) during every
forward pass or batch initialization, particularly benefiting multimodal
models where these layers are frequently instantiated or called.

- Introduce `get_inv_freq` cached function in `fastdeploy/model_executor/layers/rotary_embedding.py`.
- Update `ErnieRotaryEmbedding`, `GlmRotaryEmbedding`, `QwenRotaryEmbedding` to use cached `inv_freq`.
- Update `ErnieVlRotaryEmbedding3D` and `QwenVlRotaryEmbedding3D` to use cached `inv_freq`.

Co-authored-by: ZeyuChen <1371212+ZeyuChen@users.noreply.github.com>
This change optimizes the calculation of `inv_freq` in rotary embedding layers
by caching the result using `functools.lru_cache`. This avoids redundant
tensor creation and mathematical operations (pow, division) during every
forward pass or batch initialization, particularly benefiting multimodal
models where these layers are frequently instantiated or called.

- Introduce `get_inv_freq` cached function in `fastdeploy/model_executor/layers/rotary_embedding.py`.
- Update `ErnieRotaryEmbedding`, `GlmRotaryEmbedding`, `QwenRotaryEmbedding` to use cached `inv_freq`.
- Update `ErnieVlRotaryEmbedding3D` and `QwenVlRotaryEmbedding3D` to use cached `inv_freq`.
- Fix: Ensure cached tensors are created on the correct device by using `position_ids.place` as cache key.

Co-authored-by: ZeyuChen <1371212+ZeyuChen@users.noreply.github.com>
This change optimizes the calculation of `inv_freq` in rotary embedding layers
by caching the result using `functools.lru_cache`. This avoids redundant
tensor creation and mathematical operations (pow, division) during every
forward pass or batch initialization, particularly benefiting multimodal
models where these layers are frequently instantiated or called.

Additionally, this change fixes the HPU CI failure by guarding the access
to `paddle.compat.enable_torch_proxy` in `fastdeploy/__init__.py`.

- Introduce `get_inv_freq` cached function in `fastdeploy/model_executor/layers/rotary_embedding.py`.
- Update `ErnieRotaryEmbedding`, `GlmRotaryEmbedding`, `QwenRotaryEmbedding` to use cached `inv_freq`.
- Update `ErnieVlRotaryEmbedding3D` and `QwenVlRotaryEmbedding3D` to use cached `inv_freq`.
- Fix: Ensure cached tensors are created on the correct device by using `position_ids.place` as cache key.
- Fix: Add guard for `paddle.compat` in `fastdeploy/__init__.py`.

Co-authored-by: ZeyuChen <1371212+ZeyuChen@users.noreply.github.com>
This change optimizes the calculation of `inv_freq` in rotary embedding layers
by caching the result using `functools.lru_cache`. This avoids redundant
tensor creation and mathematical operations (pow, division) during every
forward pass or batch initialization, particularly benefiting multimodal
models where these layers are frequently instantiated or called.

Additionally, this change fixes the HPU CI failure by guarding the access
to `paddle.compat.enable_torch_proxy` in `fastdeploy/__init__.py`.

- Introduce `get_inv_freq` cached function in `fastdeploy/model_executor/layers/rotary_embedding.py`.
- Update `ErnieRotaryEmbedding`, `GlmRotaryEmbedding`, `QwenRotaryEmbedding` to use cached `inv_freq`.
- Update `ErnieVlRotaryEmbedding3D` and `QwenVlRotaryEmbedding3D` to use cached `inv_freq`.
- Fix: Ensure cached tensors are created on the correct device by using `position_ids.place` as cache key.
- Fix: Add guard for `paddle.compat` in `fastdeploy/__init__.py`.

Co-authored-by: ZeyuChen <1371212+ZeyuChen@users.noreply.github.com>
- Cached `inv_freq` calculation in `fastdeploy/model_executor/layers/rotary_embedding.py`.
- Guarded `paddle.compat` usage to fix HPU CI failures.

Co-authored-by: ZeyuChen <1371212+ZeyuChen@users.noreply.github.com>
- Cached `inv_freq` calculation in `fastdeploy/model_executor/layers/rotary_embedding.py` for standard implementations (`ErnieRotaryEmbedding`, `GlmRotaryEmbedding`, `QwenRotaryEmbedding`).
- Guarded `paddle.compat` usage across the codebase to fix CI failures on environments missing `paddle.compat`.
- Retained original logic for `ErnieVlRotaryEmbedding3D` and `QwenVlRotaryEmbedding3D` to ensure stability on MetaX/MACA.

Co-authored-by: ZeyuChen <1371212+ZeyuChen@users.noreply.github.com>
- Cached `inv_freq` calculation in `fastdeploy/model_executor/layers/rotary_embedding.py` for standard implementations (`ErnieRotaryEmbedding`, `GlmRotaryEmbedding`, `QwenRotaryEmbedding`).
- Guarded `paddle.compat` usage across the codebase to fix CI failures on environments missing `paddle.compat`.
- Retained original logic for `ErnieVlRotaryEmbedding3D` and `QwenVlRotaryEmbedding3D` to ensure stability on MetaX/MACA.

Co-authored-by: ZeyuChen <1371212+ZeyuChen@users.noreply.github.com>
- Cached `inv_freq` calculation in `fastdeploy/model_executor/layers/rotary_embedding.py` for `GlmRotaryEmbedding` and `QwenRotaryEmbedding`.
- Reverted `ErnieRotaryEmbedding`, `ErnieVlRotaryEmbedding3D`, and `QwenVlRotaryEmbedding3D` to inline logic to ensure stability on Iluvatar/MetaX environments.
- Guarded `paddle.compat` usage across the codebase to fix CI failures on environments missing `paddle.compat`.

Co-authored-by: ZeyuChen <1371212+ZeyuChen@users.noreply.github.com>
- Cached `inv_freq` calculation in `fastdeploy/model_executor/layers/rotary_embedding.py` for `GlmRotaryEmbedding` and `QwenRotaryEmbedding`.
- Reverted `ErnieRotaryEmbedding`, `ErnieVlRotaryEmbedding3D`, and `QwenVlRotaryEmbedding3D` to inline logic to ensure stability on Iluvatar/MetaX environments.
- Guarded `paddle.compat` usage across the codebase to fix CI failures on environments missing `paddle.compat`.
- Added fallback for `paddle.nn.functional.swiglu` in `fastdeploy/model_executor/ops/iluvatar/moe_ops.py` to fix Iluvatar CI import errors.

Co-authored-by: ZeyuChen <1371212+ZeyuChen@users.noreply.github.com>
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.

2 participants