Skip to content

Pre-compute igates for XPU LSTM to reduce per-step kernel launches - #185507

Closed
jianyizh wants to merge 1 commit into
pytorch:mainfrom
jianyizh:jianyi/lstm
Closed

jianyizh wants to merge 1 commit into
pytorch:mainfrom
jianyizh:jianyi/lstm

Conversation

@jianyizh

@jianyizh jianyizh commented May 28, 2026 •

Copy link
Copy Markdown
Contributor

Summary

On XPU, batch-compute input gates (linear_ih) for the entire sequence upfront, then pass pre-computed results to _thnn_fused_lstm_cell per time step. This reduces kernel launch overhead from O(T) GEMM launches to a single large GEMM.

Correctness

Standard LSTM formula per time step:

gates_t = W_ih @ x_t + b_ih + W_hh @ h_{t-1} + b_hh

The input transform W_ih @ x_t + b_ih depends only on x_t, not on the hidden state h_{t-1}. Since all x_t are known at sequence start, we can batch them:

igates = X @ W_ih.T + b_ih    # X is [T, batch, input_size], computed once

Then per step:

gates_t = igates[t] + W_hh @ h_{t-1} + b_hh

This is mathematically equivalent. Since b_ih is already in igates, we pass b_ih=None and b_hh=b_hh to the fused cell kernel.

Dependencies

Scope

  • Only affects XPU path. CUDA and CPU paths are unchanged.
  • Applies to FullLayer, FullBidirectionalLayer, PackedLayer, and ReversedPackedLayer.

cc @jgong5 @mingfeima @XiaobingSuper @sanchitintel @ashokei @jingxu10 @aditew01

@pytorch-bot

pytorch-bot Bot commented May 28, 2026 •

Copy link
Copy Markdown

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/185507

Note: Links to docs will display an error until the docs builds have been completed.

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@pytorch-bot pytorch-bot Bot added the module: cpu CPU specific problem (e.g., perf, algorithm) label May 28, 2026
@pytorch-bot

pytorch-bot Bot commented May 28, 2026

Copy link
Copy Markdown

This PR needs a release notes: label

If your changes are user facing and intended to be a part of release notes, please use a label starting with release notes:.

If not, please add the topic: not user facing label.

To add a label, you can comment to pytorchbot, for example
@pytorchbot label "topic: not user facing"

For more information, see
https://github.com/pytorch/pytorch/wiki/PyTorch-AutoLabel-Bot#why-categorize-for-release-notes-and-how-does-it-work.

@jianyizh jianyizh closed this May 28, 2026
@jianyizh jianyizh reopened this May 28, 2026
@jianyizh
jianyizh marked this pull request as ready for review June 2, 2026 07:00
Comment thread aten/src/ATen/native/RNN.cpp Outdated

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FullLayer and PackedLayer are modified to do the pre-compute for xpu, do we need to modify the code in the GRUCell as well similar to what you did in LSTMCell for pre-compute?

@jianyizh jianyizh Jun 2, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have updated this PR, now it depends on intel/torch-xpu-ops#3826 and torch-xpu-ops pin update

On XPU, add a separate LSTMCell path that supports pre_compute_input with
the fused lstm cell kernel. The Layer/PackedLayer classes now use linear_ih
to batch-compute igates (with bias) for the entire sequence, and pass empty
b_ih to _thnn_fused_lstm_cell since bias is already included. CUDA and CPU
paths are unchanged.

Depends on intel/torch-xpu-ops#3770 for correct handling of (None, b_hh)
in the fused LSTM cell kernel.
@soulitzer soulitzer added the triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module label Jun 3, 2026
@EikanWang

EikanWang commented Jun 9, 2026 •

Copy link
Copy Markdown
Collaborator

@jianyizh , I suppose this PR is not necessary now, right?

@jianyizh

jianyizh commented Jun 9, 2026

Copy link
Copy Markdown
Contributor Author

@jianyizh , I suppose this PR is not necessary now, right?

This pr is not urgent. We may review and merge it later. oneDNN LSTM cannot cover all cases. This PR optimize the fallback path.

@github-actions

github-actions Bot commented Aug 8, 2026

Copy link
Copy Markdown
Contributor

Looks like this PR hasn't been updated in a while so we're going to go ahead and mark this as Stale.
Feel free to remove the Stale label if you feel this was a mistake.
If you are unable to remove the Stale label please contact a maintainer in order to do so.
If you want the bot to never mark this PR stale again, add the no-stale label.
Stale pull requests will automatically be closed after 30 days of inactivity.

@github-actions github-actions Bot added the Stale label Aug 8, 2026
@github-actions github-actions Bot closed this Sep 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

module: cpu CPU specific problem (e.g., perf, algorithm) open source Stale triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants