-
Notifications
You must be signed in to change notification settings - Fork 11
compare host vs device-side chunk metadata computation #84
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
Draft
learning-chip
wants to merge
14
commits into
main
Choose a base branch
from
inverse_varlen_hostmeta
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
c9790d2
standalone fast inverse for quick hacking
learning-chip 06b6a29
also test bsnd branch
learning-chip cf961b0
support varlen version of bsnd inverse
learning-chip 0a61a72
add bandwidth benchmark for varlen inverse kernel
learning-chip 04c33df
paritial load/store in kernel to avoid slow torch padding
learning-chip dc1c5f4
fix kernel synchornization for large-size benchmarks
learning-chip a43f974
compute chunk_metadata from cu_seqlens
learning-chip 496e6c4
add gitignore for fast_inverse example
learning-chip 16496b1
compute chunk metadata inside NPU kernel using scalar core unit
learning-chip 87d0a14
unit test mirror FLA triton repo
learning-chip bd54017
also change benchmark script to use triton-like input preparation
learning-chip de06b4a
compare host vs device-side chunk metadata computation
learning-chip aa9c099
use prefix trick to speed-up computation
learning-chip 8da6f13
Add description for BSND
asobczyk 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,2 @@ | ||
| benchmark_results | ||
| *.so |
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,111 @@ | ||
| ## fast_inverse — JIT triangular matrix inverse (recursive unroll) | ||
|
|
||
| JIT-compiled example of `kernel_tri_inv_rec_unroll`, which inverts a batch of | ||
| upper-triangular fp16 matrices stored in a multi-dimensional tensor. | ||
|
|
||
| ### Algorithm | ||
|
|
||
| Given an input tensor whose last two dimensions form an n×n upper-triangular | ||
| matrix U (off-diagonal part only; the diagonal is assumed to be all-ones), the | ||
| kernel computes the inverse of (U + I) for every matrix in the batch. | ||
|
|
||
| The implementation uses a two-phase recursive approach on Ascend cube cores: | ||
|
|
||
| 1. **Inv-trick phase** – inverts each 16×16 diagonal fractal block via a | ||
| Neumann-series expansion (`X = (I − M) + (I − M)·M + …`). | ||
| 2. **Unrolled recursion phase** – assembles partial inverses of progressively | ||
| larger sub-blocks until the full matrix is inverted. | ||
|
|
||
| ### Files | ||
|
|
||
| | File | Purpose | | ||
| |------|---------| | ||
| | `fast_inverse.cpp` | Thin JIT wrapper: includes the kernel and exposes `call_kernel` | | ||
| | `jit_util_fast_inverse.py` | Compiles the kernel with `bisheng` and loads it via `ctypes` | | ||
| | `run_fast_inverse.py` | Correctness test suite, including aligned and varlen BSND coverage | | ||
| | `run_fast_inverse_varlen_like_triton.py` | Standalone varlen runner that mirrors the Triton `test_solve_tril_varlen` input generation in pure PyTorch | | ||
| | `benchmark_bsnd_fast_inverse.py` | Benchmarks fixed BSND vs varlen-uniform BSND and plots effective bandwidth | | ||
|
|
||
| ### Usage | ||
|
|
||
| ```bash | ||
| export PTO_LIB_PATH=/sources/pto-isa/ # need latest header, not CANN 8.5.0 default | ||
|
|
||
| cd examples/jit_cpp/fast_inverse | ||
| python run_fast_inverse.py | ||
| ``` | ||
|
|
||
| The script compiles `fast_inverse.cpp` on first run (takes ~60 s), then | ||
| executes correctness checks across a range of matrix sizes (16, 32, 64, 128) | ||
| and batch configurations. | ||
|
|
||
| To run the standalone Triton-like varlen coverage: | ||
|
|
||
| ```bash | ||
| export PTO_LIB_PATH=/sources/pto-isa/ | ||
|
|
||
| cd examples/jit_cpp/fast_inverse | ||
| python run_fast_inverse_varlen_like_triton.py | ||
| ``` | ||
|
|
||
| That script: | ||
|
|
||
| - uses the same varlen case list and input-generation structure as | ||
| `flash-linear-attention/tests/ops/test_solve_tril.py::test_solve_tril_varlen` | ||
| - keeps PTO inputs in `float16` | ||
| - emulates `chunk_scaled_dot_kkt_fwd` in PyTorch because Triton is not available | ||
| - prints a simple pytest-like `PASS` / `FAIL` report plus a final summary | ||
|
|
||
| ### Supported matrix sizes | ||
|
|
||
| `matrix_size` (last dimension of the input tensor) must be one of: **16, 32, | ||
| 64, 128**. | ||
|
|
||
| ### Layout conventions | ||
|
|
||
| In general, the input to the `fast_inverse` kernels is a set of `D × D` sized triangular matrices. Depending on how these matrices are stored in memory, we might have `contiguous` layout, or the so-called `BSND` layout. The main input is a batch of sequences, and each sequence is then split in "chunks" of length `chunk_size`. This `chunk_size` is the same as the matrix size `D`. | ||
|
|
||
| Both layouts depend on the following parameters: | ||
| - The parameter `B` denotes the batch-size (or batch-dimension). This is always the first dimension of the input tensor. | ||
| - The parameter `N` or `H` (used interchangeably) is the number of heads. | ||
| - `D` is equal to the `chunk_size`. | ||
| - `S` is the total sum of all sequence lengths combined. | ||
| `BSND` can be thought of as the "raw" input tensor. The `contiguous` layout can be obtained, for example, by transposing the `N` and `S` dimensions, and by "chunking" the `S` dimension to chunks of size `S`. The final tensor will be transformed from shape `(B,S,N,D)` to `->(B,N,S/D,D)`, where we assumed that `D` divides `S` for simplicity. | ||
|
|
||
| The actual kernel can verify if the input is in `BSND` layout or in `contiguous` layout by specifying the input argument `num_bsnd_heads`. If it is equal to zero, then the format is assumed to be `contiguous` | ||
|
|
||
| | `num_bsnd_heads` | Memory layout | | ||
| |-----------------|---------------| | ||
| | `0` (default) | Each matrix stored consecutively in row-major order (`B × … × D × D`) | | ||
| | `> 0` | BSND layout: `(B, S, N, D)` where `S` is chunked into tiles of size D and N heads are interleaved | | ||
|
|
||
| ### Varlen BSND mode | ||
|
|
||
| The standalone example also supports variable-length BSND inputs with the same | ||
| external signature as the Triton reference path: callers provide packed BSND | ||
| data plus `cu_seqlens`, and the PTO kernel derives each chunk row-start and | ||
| tail size internally on NPU. The kernel still inverts dense `D x D` tiles, but | ||
| tail chunks load/store only their valid prefix. | ||
|
|
||
| ### Benchmark | ||
|
|
||
| To compare the original fixed-length BSND path against the new varlen path in a | ||
| matched-size sanity check: | ||
|
|
||
| ```bash | ||
| export PTO_LIB_PATH=/sources/pto-isa/ | ||
|
|
||
| cd examples/jit_cpp/fast_inverse | ||
| python benchmark_bsnd_fast_inverse.py --chunk-size 64 | ||
| ``` | ||
|
|
||
| The benchmark script: | ||
|
|
||
| - runs only the PTO-ISA BSND kernel | ||
| - compares `bsnd-fixed` against `bsnd-varlen-uniform` | ||
| - uses uniform `cu_seqlens=[0, T, 2T, ...]` so both paths process the same | ||
| total data size | ||
| - reports numerical agreement between the two outputs | ||
| - also generates a true-varlen benchmark that plots scattered bandwidth points | ||
| against aggregated sequence length | ||
| - writes all CSV and PNG artifacts into `examples/jit_cpp/fast_inverse/benchmark_results/` | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In FLA convention
Dis thehidden_dim, andchunk_sizeshould probably be namedC....