(T, provider)
| 35 | ) |
| 36 | ) |
| 37 | def benchmark(T, provider): |
| 38 | # TODO: also add bwd pass benchmark |
| 39 | device = 'cuda' |
| 40 | dtype = torch.bfloat16 |
| 41 | B, H, D = 16, 8, 128 |
| 42 | # TODO: test more shapes |
| 43 | # TODO: different values for D_V and D_QK |
| 44 | # TODO: different values for H_Q and H_KV |
| 45 | final_state = False # does not impact performance |
| 46 | |
| 47 | # initialize Mamba2-format inputs |
| 48 | X_mamba = 0.1 * torch.randn(B, T, H, D, dtype=dtype, device=device) |
| 49 | dt_mamba = torch.ones(B, T, H, dtype=dtype, device=device) |
| 50 | A_mamba = -0.1 * torch.rand(H, dtype=dtype, device=device) |
| 51 | B_mamba = 0.1 * torch.randn(B, T, H, D, dtype=dtype, device=device) |
| 52 | C_mamba = 0.1 * torch.randn(B, T, H, D, dtype=dtype, device=device) |
| 53 | |
| 54 | quantiles = [0.5, 0.2, 0.8] |
| 55 | if provider == 'chunk_simple_gla': |
| 56 | # mapping inputs Mamba2 -> FLA |
| 57 | # C, B, X: [B, T, H, D] -> [B, H, T, D] |
| 58 | # g: [B, T, H] -> [B, H, T] |
| 59 | q = C_mamba.transpose(1, 2).contiguous() |
| 60 | k = B_mamba.transpose(1, 2).contiguous() |
| 61 | v = X_mamba.transpose(1, 2).contiguous() |
| 62 | g = (A_mamba * dt_mamba).transpose(1, 2).contiguous() |
| 63 | # NOTE: whether to include the memory-copy cost of `contiguous()`? |
| 64 | # this depends on the memory layout used by surrounding non-SSM layers |
| 65 | |
| 66 | results = triton.testing.do_bench( |
| 67 | lambda: chunk_simple_gla( |
| 68 | q, k, v, g, scale=1.0, output_final_state=final_state |
| 69 | ), quantiles=quantiles |
| 70 | ) |
| 71 | |
| 72 | elif provider == 'mamba2_ssd': |
| 73 | # NOTE: `chunk_size` is configurable in mamba2 kernel |
| 74 | # here sets to the same hard-coded `BT = 64` as in simple_gla kernel |
| 75 | # TODO: benchmark different chunk sizes |
| 76 | results = triton.testing.do_bench( |
| 77 | lambda: mamba_chunk_scan_combined( |
| 78 | X_mamba, dt_mamba, A_mamba, B_mamba, C_mamba, |
| 79 | chunk_size=64, D=None, return_final_states=final_state |
| 80 | ), |
| 81 | quantiles=quantiles |
| 82 | ) |
| 83 | return results |
| 84 | |
| 85 | if __name__ == '__main__': |
| 86 | benchmark.run(print_data=True, save_path='.') |
nothing calls this directly
no outgoing calls
no test coverage detected