Causal Attention Mask Lab

See exactly which key positions each query can attend to. Shift the causal boundary, add a sliding window, switch PyTorch mask representations, and copy code backed by exact full-matrix counts.

Mask configuration

Whole number from 1 to 4,096.
Positive reveals future keys; negative delays access.
0 means no window limit.
Mask representation
Allowed
10
62.5%
Blocked
6
37.5%
Total cells
16
Exact full matrix

Visual preview

Preview: complete 4 × 4 mask. Rows are queries; columns are keys.
true · allowed false · blocked

Preview rendering is capped at 24 × 24. Counts always cover the requested full sequence.

PyTorch


   

JSON


   

Everything runs locally in your browser. Nothing is uploaded.

Verified fixtures

These deterministic cases run in your browser against the same validation, counting, and preview logic used above.

Mask rule and PyTorch semantics

For query row q and key column k, this lab allows a cell when k ≤ q + offset. If the local window is nonzero, it also requires k ≥ q + offset − window + 1. Both rules are intersected with valid sequence positions. Offset 0 and no local window produce the familiar lower-triangular causal mask.

PyTorch scaled dot-product attention treats true in a boolean attention mask as allowed. An additive mask expresses the same rule with 0 at allowed cells and negative infinity at blocked cells. See the official PyTorch scaled_dot_product_attention documentation for the API's mask behavior. Pass either generated mask as attn_mask; do not also set is_causal=True.

How to read the preview

Each row represents one query position and each column represents one key position. An allowed cell means that query can use the corresponding key when attention weights are computed. A blocked cell receives no probability mass after masking. Moving the offset changes the diagonal boundary. Adding a local window keeps the causal boundary but removes keys that fall too far behind it, producing a diagonal band instead of a full lower triangle.

The preview is deliberately bounded so a long sequence cannot create an unresponsive page. When the requested sequence exceeds the preview limit, the lab renders the leading square only. The allowed, blocked, and total counters still describe the full requested matrix because they are calculated row by row with exact integer arithmetic, independently of the rendered cells.

Boolean versus additive masks

The two exports encode the same allowed-cell predicate. Boolean mode is easier to inspect because every cell is explicitly true or false. Additive mode is useful when an attention implementation expects a bias tensor: allowed positions add zero while blocked positions add negative infinity before softmax. Dtype, device, batch, and head broadcasting remain the caller's responsibility; the generated snippet demonstrates the mask rule rather than guessing those runtime details.

Method and limitations

This utility is a deterministic mask constructor, not a runtime benchmark. It does not predict kernel choice, memory use, latency, or numerical behavior. Framework behavior can change by version, so verify the generated shape and mask convention against the linked primary documentation for the version you run. The fixtures exercise the same validation and counting functions as the interactive controls, including an invalid input and a truncated-preview case.

TENSOR PREFLIGHT · DOWNLOADABLE PYTORCH TOOLKIT

Take the next step in your own PyTorch model.

Trace a local forward pass, inspect tensor shapes in an HTML report, and turn a repair into a regression check. Includes eight broken-and-fixed workflows, source code and setup instructions.

Is this toolkit right for my problem?

Use the free calculator for a layer formula or a quick shape check.

Consider Tensor Preflight when you have a local PyTorch model and want a forward-pass report plus repeatable checks. Python 3.9+ and PyTorch 2.8+ are required. It does not run your model in this website or automatically repair it.

Open the sample report before buying to check the output format.

Python 3.9+ · PyTorch 2.8+ · Runs locally

Get Tensor Preflight — $29 One-time payment · ZIP download See a sample report