python -m pip install "fast-attnres==2.2.0"Fast-AttnRes 2.2 adds block IDLR-AttnRes and the configurable routing API. It includes standard, sliced, multi-query and selected-query reads.
IDLRBlock groups CUDA BF16 sublayers. Its default queries read a shared boundary.
Each sublayer takes its routed input and returns a raw residual write. Register
one router per group when constructing the model. Reusing a route configuration
creates separate query and key parameters in every group.
from torch import nn
from attnres import IDLRBlock, IDLRRoute, IDLRState
# In __init__
affine = IDLRRoute(gain="vector")
self.routers = nn.ModuleList(
IDLRBlock(rank=64, reads=len(group), query=affine, key=affine)
for group in self.sublayer_groups
)
# In forward
state = IDLRState.from_embedding(embedding, storage="list")
for router, group in zip(self.routers, self.sublayer_groups):
state = router.step(state, group)With storage="list", the next group receives a tuple containing the same
source tensors and the new block sum. You can also pass a Python list or tuple
directly to router(history, boundary, group). The default constructor uses
packed tensor storage. Each group appends its running sum to state.history.
Pass history_mode="full" to append each write in order. In both modes
state.boundary_write is the final raw sublayer output, which supplies the
next block's query features. The initial history is a one-source view of the
embedding. The tensor API remains history, boundary = router(history, boundary, group).
The query is RMSNorm(a_i * boundary[..., -2*r:-r] + b_i) and the key is
RMSNorm(key_gain * value[..., -r:] + key_bias). The adjacent slices require
D >= 2*r. RMSNorm takes its mean over the rank. The default logit scale
is 1. Each read learns its own bias and starts with zero query gain. Gains can
be scalar or vector. Use shared_bias=True for one static query per block,
shared_gain=True to tie gains within a block, or assign
self.routers[1].query_gain = self.routers[0].query_gain before constructing
the optimizer to tie them across blocks. Keys start with unit gain and zero bias.
The first read of the first block returns the embedding.
Configure the two routes independently with IDLRRoute. For example, each
read below projects its latest raw write, while every key uses the last 64
coordinates of its own value directly.
from attnres import IDLRBlock, IDLRRoute
router = IDLRBlock(
rank=64, reads=4,
query=IDLRRoute(source="latest", project_from=1024, gain=None, bias=True),
key=IDLRRoute(source="value", gain=None, bias=False, rms_norm=False),
)A disabled gain uses a multiplier of one. A disabled bias uses zero. Neither
allocates a parameter. A learned projection has shape [rank, project_from]
and runs before the affine transform. See the source and scheduling
reference for static,
boundary, latest-write and value-summary queries.
Training uses direct reads. Compile the model with torch.compile(model) to
use the fused H100 block training kernel at supported four- and eight-read sizes.
With boundary or static queries, 2,048 tokens, width 1,024, rank 256 and up to
12 completed contiguous sources, the native source-list kernel uses one history
gradient reduction per four- or eight-read group.
For inference, call model.eval() and enter
torch.inference_mode(). The default strategy="auto" selects
between direct and two-phase reads. Use "two_phase" to batch the historical
reads and merge local writes as they become available, or "direct" to
recompute each read. Latest-write and value-summary queries use fresh direct reads.
The H100 training comparison times both learned affine transforms against v2.1.0 in a 24-layer language model.
Fast Attention Residuals (Fast-AttnRes) makes
Attention Residuals a single PyTorch
operation: pass ordered full-width residual sources and one learned query, get
one full-width residual back. The same attnres(values, query) call handles
standard and sliced low-rank AttnRes in Full and Block schedules, with packed
tensors or ordered source lists.
Start with the standard quickstart, choose a Full or Block schedule, and try sliced LR-AttnRes for a smaller routing query.
- One PyTorch call: full-width output and ordinary first-order autograd, including gradients through routing keys and shared residual sources.
- One schedule primitive: Full and Block use the same operator; the caller controls source order, block sums, and learned queries.
- BF16 training: CUDA BF16 values and queries, FP32 internal reductions,
torch.compile, and CUDA Graph replay on H100 and B200.
On the 24-layer Full workload, Fast-AttnRes reduces step latency by 5.95% on H100 SXM and 22.74% on B200 against native FLA Triton checkpoint 1 (median of three paired seed estimates).
The figures measure complete BF16 CUDA Graph training steps on H100 SXM and B200, including forward, backward, and the optimizer update. Full and Block use the same per-read Fast-AttnRes backend.
The 24-layer headline uses three seeds with 120 paired rounds each. The 8-layer comparisons use one seed with 40 paired rounds per configuration. See the benchmark protocol and reproducible results for workloads, source versions, confidence intervals, and reproduction commands.
Comparators are native FLA Triton checkpoint 1, Liger 0.8.2, and Catswe phase 1. Unsupported and failed arms remain labelled. Quarter-rank comparisons against standard FLA compare different routing equations. The D2048 results retain disclosed normalization-rounding deviations. On H100, standard D2048 is 0.28% slower than FLA, within the declared 1% parity band.
These figures compare our R=D/4 kernel with our R=D kernel on each workload.
Values and outputs retain width D; only the routing rank changes.
Quarter rank reduces step latency in all ten measured device/workload pairs.
Install the release with the pinned CUDA runtime:
python -m pip install --index-url https://download.pytorch.org/whl/cu126 torch==2.13.0
python -m pip install "fast-attnres[cuda]==2.2.0"For development, clone the repository and run python -m pip install -e ".[cuda]".
Version 2 requires CUDA BF16 tensors; the CPU/FP32 execution and exported reference from version 1 are no longer part of the public API.
The minimum PyTorch version is 2.10. The pinned H100 runtime is Python 3.11,
PyTorch 2.13.0 with CUDA 12.6, and Triton 3.7.1.
On B200, use cu130 instead of cu126; multi-query qualification here covers H100.
import torch
from attnres import attnres
values = torch.randn(8, 2, 1024, device="cuda", dtype=torch.bfloat16, requires_grad=True)
source_list = tuple(values.unbind(0))
query = torch.randn(1024, device="cuda", dtype=torch.bfloat16, requires_grad=True)
read = torch.compile(attnres, fullgraph=True, dynamic=False)
output = read(source_list, query)
output.square().mean().backward()The public signature is attnres(values, query, *, eps=2**-23, scale=1.0).
values is either packed [S, ..., D] or an ordered list/tuple of [ ..., D ]
tensors. The query is [R], with 1 <= R <= D; the output retains width D.
Values, query, output, and first-order operator gradients are CUDA BF16. Internal
FP32 accumulators may be used for normalization, logits, softmax, and reductions.
attnres_multi reads the same sources with multiple learned queries. Each query
has its own source weights and full-width output:
from attnres import attnres_multi
queries = torch.randn(2, 64, device="cuda", dtype=torch.bfloat16, requires_grad=True)
read_multi = torch.compile(attnres_multi, fullgraph=True, dynamic=False)
outputs = read_multi(source_list, queries) # [2, ..., 1024]
outputs.float().square().mean().backward()Queries have shape [M, R], with 1 <= M <= 16; R=D gives standard routing
and R<D uses the final R source coordinates. Packed, list and tuple sources
use the same API. Set output_layout="token_major" for [..., M, D] output.
For per-token query selection, attnres_selected(sources, query_bank, ids) takes
an [E, R] bank and integer [..., K] IDs, returning [..., K, D].
The four-layer QK/V fixture measures compiled forward, loss, backward and AdamW on synthetic tokens. One query shares a read between Q/K and V; two queries route those roles separately. The two independent reads provide the matched baseline for fusion. These are fixture timings, not the 24-layer results above. Bars are medians of 40 randomized rounds; the reduction uses paired time ratios. See the measurements for the workload, runtime and raw samples. Performance depends on shape.
Available since 2.1.0. The graph uses PyTorch 2.10.0 / Triton 3.6.0 on H100. Small packed and selected-query reads can be slower than separate compiled reads; benchmark your shapes before adopting fusion.
For source value v_s, take its final R coordinates as the implicit key:
t_s = v_s[..., D-R:D]
r_s = sqrt(mean(t_s ** 2) + eps)
k_s = t_s / r_s
score_s = scale * dot(k_s, query)
p_s = softmax(score, axis=source)_s
output = sum_s p_s * v_s
Normalization and softmax run independently at each carried batch or token
position. The output is not normalized, source-count weighted, or source averaged.
See docs/equation.md for the complete contract.
Full supplies the embedding and every preceding writer output. Block supplies the embedding, completed block sums, and an optional current partial sum. The caller owns block boundaries and sums a partial block before the read; it is passed as one ordinary source and is not averaged.
full_output = attnres((embedding, *writers), query)
completed = (embedding, first_block, second_block)
block_sources = completed if partial is None else completed + (partial,)
block_output = attnres(block_sources, query)Every read evaluates its routing weights and mixture from the supplied sources.
Both schedules use the same query, eps, scale, and operator contract.
Sliced LR-AttnRes keeps full-width values and output while using a shorter implicit key and query:
rank = 64
query = torch.randn(rank, device="cuda", dtype=torch.bfloat16)
output = attnres(source_list, query) # [..., D], BF16For a trainable static query, use LearnedQuery(rank):
from attnres import LearnedQuery
learned_query = LearnedQuery(rank).to(device="cuda", dtype=torch.bfloat16)
output = attnres(source_list, learned_query())Standard AttnRes is the R == D case; sliced routing uses R < D and the final
R value coordinates. Projected keys, routing priors, and architectural changes
are outside this package contract.
Optimization targets common ranks: powers of two from 16 upward, plus widths such as 384 and 640. Other ranks retain the same mathematical support.
Correctness checks compare BF16 outputs and first-order gradients with an
independent BF16 PyTorch reference with FP32 internal accumulation at
rtol=0.05 and atol=0.05. Coverage
includes packed/list sources, repeated reads, partial Blocks, changed inputs,
non-contiguous layouts, shared sources, and compiled replay. See the
validation protocol.
Timing reports identify the exact source, device, runtime, workload, and measurement boundary. Failed, incomplete, and inconclusive comparisons remain visible in the results.
Fast-AttnRes is released under the MIT License. FLA-derived source
list attribution remains in NOTICE.





