Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[RFC] Fuse elementwise operations in RWKV layers #165

Open
sustcsonglin opened this issue Feb 5, 2025 · 2 comments
Open

[RFC] Fuse elementwise operations in RWKV layers #165

sustcsonglin opened this issue Feb 5, 2025 · 2 comments
Labels
enhancement New feature or request

Comments

@sustcsonglin
Copy link
Collaborator

Proposal

e.g. https://github.com/fla-org/flash-linear-attention/blob/main/fla/layers/rwkv7.py#L147C1-L158C1

Either write a fused triton kernel or use torch.jit or whatever to reduce the I/O cost

Rationale

these elementwise ops are super slow.

@sustcsonglin sustcsonglin added the enhancement New feature or request label Feb 5, 2025
@Triang-jyed-driung
Copy link
Contributor

Can this be fused, really?
Maybe at some point in the future we write Triton for everything

@uniartisan
Copy link
Contributor

uniartisan commented Feb 9, 2025

Can this be fused, really? Maybe at some point in the future we write Triton for everything

There are a lot of things that we can fuse.

For example, xr = hidden_states + xx * self.x_r, and xw, xk, xv, xa, xg...

  1. We can use torch.addcmul. It will use FP32 (instead of BF16) and is faster than the original implementation (even faster than Triton in most situations).
  2. I opened a PR to implement the fused kernel.

There are many other things that can be fused and implemented with Triton, but we must be careful because in many cases, an unoptimized fused PyTorch kernel is faster.

yzhangcs added a commit that referenced this issue Feb 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants