-
Notifications
You must be signed in to change notification settings - Fork 47
[Perf][Linear-Attn] Add optimized Gated DeltaNet prefill op #1596
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
Merged
Merged
Changes from 7 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
b08bdbe
[Linear-Attn] Add optimized GDN prefill implementation
superAngGao 067edc7
[Linear-Attn] Add GDN prefill tests and benchmark
superAngGao 9282b2b
[Linear-Attn] Productionize GDN partitioned prefill
superAngGao 7273c1d
Merge remote-tracking branch 'upstream/main' into ako/gdn-prefill-fla…
superAngGao a329a6c
[Chore][Lint] Fix GDN prefill pre-commit
superAngGao 7fb5f35
[Fix] Avoid GDN h recurrence scalar aliases
superAngGao 090c7d1
Fix GDN prefill roofline layout handling
superAngGao 0559ab1
Clarify GDN prefill third-party notices
superAngGao 95c21a0
Refine GDN prefill attribution notice
superAngGao 70ddca4
Tighten GDN prefill utility attribution
superAngGao File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,151 @@ | ||
| """Benchmark: TileOPs Gated DeltaNet inference prefill. | ||
|
|
||
| When FLA is installed, record it as the independent baseline. Otherwise fall | ||
| back to a pure-torch reference so every benchmark row has a non-TileOps entry. | ||
|
|
||
| The benchmark measures the serving-oriented BTHD layout because that is the | ||
| production fast path used by FLA/Qwen-style inference prefill. | ||
| """ | ||
|
|
||
| import inspect | ||
| from typing import Any | ||
|
|
||
| import pytest | ||
| import torch | ||
|
|
||
| from benchmarks.benchmark_base import BenchmarkReport, ManifestBenchmark | ||
| from benchmarks.ops.attention.manifest_params import manifest_params | ||
| from benchmarks.ops.bench_gated_deltanet import ( | ||
| _to_fla_layout, | ||
| compute_w_u_torch, | ||
| kernel2_gated_deltanet_torch, | ||
| prepare_wy_repr_gated_torch, | ||
| ) | ||
| from tileops.manifest import load_workloads | ||
| from tileops.ops import GatedDeltaNetPrefillFwdOp | ||
| from workloads.gated_deltanet import GatedDeltaNetPrefillFwdTest | ||
|
|
||
| _OP_NAME = "GatedDeltaNetPrefillFwdOp" | ||
|
|
||
|
|
||
| try: | ||
| from fla.ops.gated_delta_rule import chunk_gated_delta_rule | ||
| except ImportError: | ||
| chunk_gated_delta_rule = None | ||
|
|
||
|
|
||
| class _GatedDeltaNetPrefillFwdTestBaseline(GatedDeltaNetPrefillFwdTest): | ||
| """Adds a pure-torch fallback baseline for benchmark profiling.""" | ||
|
|
||
| def ref_program( | ||
| self, | ||
| q: torch.Tensor, | ||
| k: torch.Tensor, | ||
| v: torch.Tensor, | ||
| g: torch.Tensor, | ||
| beta: torch.Tensor, | ||
| ) -> tuple[torch.Tensor, torch.Tensor]: | ||
| batch, heads, seq_len, dim_k = k.shape | ||
| dim_v = v.shape[-1] | ||
| chunk_size = self.chunk_size | ||
| num_chunks = seq_len // chunk_size | ||
| g_cum = ( | ||
| g.float() | ||
| .reshape(batch, heads, num_chunks, chunk_size) | ||
| .cumsum(-1) | ||
| .reshape(batch, heads, seq_len) | ||
| .to(g.dtype) | ||
| ) | ||
| aw, au = prepare_wy_repr_gated_torch(k, g_cum, beta, chunk_size) | ||
| w, u = compute_w_u_torch(aw, au, k, v, beta, chunk_size) | ||
| initial_state = torch.zeros( | ||
| batch, heads, dim_k, dim_v, dtype=torch.float32, device=q.device | ||
| ) | ||
| final_state, o = kernel2_gated_deltanet_torch( | ||
| q, k, g_cum, w, u, initial_state, chunk_size | ||
| ) | ||
| return o.to(self.dtype), final_state.to(self.dtype) | ||
|
|
||
|
|
||
| def _fla_prefill_fwd(): | ||
| """Return the FLA prefill baseline callable, or None if unavailable.""" | ||
| if chunk_gated_delta_rule is None: | ||
| return None | ||
|
|
||
| signature = inspect.signature(chunk_gated_delta_rule) | ||
| supports_output_final_state = "output_final_state" in signature.parameters | ||
|
|
||
| def baseline_fn( | ||
| q: torch.Tensor, | ||
| k: torch.Tensor, | ||
| v: torch.Tensor, | ||
| g: torch.Tensor, | ||
| beta: torch.Tensor, | ||
| ): | ||
| kwargs: dict[str, Any] = {"scale": 1.0} | ||
| if supports_output_final_state: | ||
| kwargs["output_final_state"] = True | ||
| return chunk_gated_delta_rule(q, k, v, g, beta, **kwargs) | ||
|
|
||
| return baseline_fn | ||
|
|
||
|
|
||
| def _gdn_prefill_args(workload: dict[str, Any]) -> tuple[int, int, int, int, int, int]: | ||
| batch, seq_len, heads, dim_k = workload["q_shape"] | ||
| _, v_seq_len, v_heads, dim_v = workload["v_shape"] | ||
| if v_seq_len != seq_len: | ||
| raise ValueError("GDN prefill q_shape and v_shape must share seq_len") | ||
| if v_heads != heads: | ||
| raise ValueError("GDN prefill q_shape and v_shape must share heads") | ||
| return batch, heads, seq_len, dim_k, dim_v, workload.get("chunk_size", 64) | ||
|
|
||
|
|
||
| _BENCH_PARAMS = manifest_params(load_workloads(_OP_NAME), _gdn_prefill_args, tune=False) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "batch, heads, seq_len, dim_k, dim_v, chunk_size, dtype, tune", | ||
| _BENCH_PARAMS, | ||
| ) | ||
| def test_gated_deltanet_prefill_fwd_bench( | ||
| batch: int, | ||
| heads: int, | ||
| seq_len: int, | ||
| dim_k: int, | ||
| dim_v: int, | ||
| chunk_size: int, | ||
| dtype: torch.dtype, | ||
| tune: bool, | ||
| ) -> None: | ||
| test = _GatedDeltaNetPrefillFwdTestBaseline( | ||
| batch, heads, seq_len, dim_k, dim_v, chunk_size, dtype | ||
| ) | ||
| inputs = test.gen_inputs() | ||
|
|
||
| op = GatedDeltaNetPrefillFwdOp( | ||
| batch, | ||
| heads, | ||
| seq_len, | ||
| dim_k, | ||
| dim_v, | ||
| chunk_size, | ||
| dtype, | ||
| layout="bthd", | ||
| tune=tune, | ||
| ) | ||
| bm = ManifestBenchmark(_OP_NAME, op, test) | ||
| bthd_inputs = _to_fla_layout(*inputs) | ||
| result = bm.profile(op, *bthd_inputs) | ||
| BenchmarkReport.record(op, locals(), result, tag="tileops") | ||
|
|
||
| fla_fn = _fla_prefill_fwd() | ||
| if fla_fn is not None: | ||
| result_fla = bm.profile(fla_fn, *bthd_inputs) | ||
| BenchmarkReport.record(op, locals(), result_fla, tag="fla") | ||
| else: | ||
| result_ref = bm.profile(test.ref_program, *inputs) | ||
| BenchmarkReport.record(op, locals(), result_ref, tag="torch-ref") | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| pytest.main([__file__, "-vvs"]) | ||
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.