perf: expose FlexAttention loader override#6
Open
danieleb1861 wants to merge 1 commit into
Open
Conversation
|
All contributors have signed the CLA ✍️ ✅ |
Author
|
I have read the CLA Document and I hereby sign the CLA |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
This PR turns TiRex2's existing FlexAttention implementation into a testable public configuration option by proposing an optional
use_flex_attentionkeyword argument toload_model, while preserving existing defaults.Performance, compilation, batching, and numerical trade offs are documented with a micro-benchmark.
Context
During large scale zero-shot evaluation of independent multivariate forecasting windows on a personal dataset, the cost of dense grouped variate attention grows with the total number of packed variates, even though interactions between unrelated timeseries groups are masked.
TiRex2 already provides a block-sparse FlexAttention path capable of exploiting this group structure. However, the backend is currently not selectable through the public
load_modelinterface. Enabling it requires modifying checkpoint configuration or mutating internal model attributes after loading.To my personal knowledge, this makes an existing performance capability difficult to discover, configure, test, and reproduce from user code.
Proposed interface
The contribution exposes the existing backend through an additive keyword-only argument:
The behavior is explicit:
None, the default, preserves the checkpoint setting and current package behavior;Trueenables FlexAttention in every variate mixer;Falseexplicitly forces dense attention.Existing callers therefore maintain the same behavior without code or checkpoint changes.
Rationale
FlexAttention is deliberately exposed as an opt-in backend rather than enabled globally.
The measurements show that backend selection depends on workload size, as already known by Authors:
However, very large batches can become memory-bound before attention throughput is fully utilized and FlexAttention introduces first-use CUDA compilation overhead.
Keeping the existing default avoids changing latency, compilation behavior, or numerical reproducibility for current users.
Performance characterization
Benchmarks were run with on the released TiRex2 checkpoint with the following HW and SW setup:
Runs with other context/prediction length combinations showed larger FlexAttention gains; the tables below are representative of a single configuration rather than a best-case result.
Measurement methodology
Two complementary measurements were used.
Variate-attention microbenchmark
First-use FlexAttention compilation was excluded from warmed latency and evaluated separately. The reported values of the latency is the median of the five timed executions.
Forecasting benchmark
time.perf_counter().torch.cuda.synchronize()was called before stopping the timer.number_of_windows / elapsed_seconds.torch.cuda.max_memory_allocated()after resetting peak statistics.The measurements were collected on a single NVIDIA GeForce RTX 4070 Laptop GPU without fixed GPU clocks. They should therefore be interpreted as workload-specific results from the user point of view rather than hardware-independent performances.
End-to-end forecasting throughput
At batch size 64, FlexAttention improved end-to-end throughput by approximately 14.3% on this hardware and workload.
Variate-attention layer
For 128 independent groups with 17 variates each, corresponding to 2,176 packed variate rows:
This is a 3.16× reduction in variate-attention latency for that isolated workload.
The smaller end-to-end improvement is expected because TiRex2's recurrent time-mixing blocks must still process every target and covariate. This PR therefore addresses the grouped variate-attention component rather than claiming a universal model-level speedup, that was not benchmarked.
However, improvements from the proposed implementation can nonetheless be meaningful from a user perspective, as seen above.
Numerical behavior
Dense attention and FlexAttention are numerically close but not bit-identical.
On a 64-window batch of a held-out navigation forecasting benchmark from my own work:
The documentation explicitly recommends re-evaluating forecast metrics when changing attention backends in reproducible experiments.
Compatibility
The change is intentionally small and additive:
Validation
The finalized change was checked with:
Scope
This PR does not introduce a new sparse-attention algorithm. It provides a supported public interface for TiRex2's existing FlexAttention implementation and documents the workload conditions under which it may be useful.
It also does not attempt to remove the fundamental cost of processing multiple covariates through the recurrent time-mixing stack.
Thank you for open-sourcing TiRex-2!
I'm using it in my research on shipboard energy management. Happy to contribute further as that work progresses.