local-attention is a Python library by lucidrains that implements local attention mechanisms with configurable windowing and lookback/lookforward options, primarily for language modeling tasks. It leverages PyTorch for efficient computation and is actively maintained with frequent minor and patch releases.
pip install local-attentionVerified import paths — ran on the pinned version, not inferred.
Initializes a `LocalAttention` module with specified windowing parameters and applies it to a dummy input tensor. This example demonstrates a causal attention setup suitable for autoregressive models.
Carefully consult the documentation and examples for each parameter. Test with small synthetic inputs to verify the attention mask behavior, especially for autoregressive tasks where strict causality is essential.
Explicitly move tensors to the target device (e.g., `tensor.to(device)`) and ensure `dtype` consistency, especially when mixing `local-attention` with other PyTorch components or models loaded with different dtypes.
Verify your input tensor's dimensions before passing it to `LocalAttention`. If necessary, use `tensor.transpose(1, 2)` or `einops.rearrange` to correct the shape.
Review custom implementations that might interact with the internal `look_around` logic. For standard usage, this update is a transparent improvement.
Change the import statement to `from local_attention import LocalAttention`.
Convert the input data to a `torch.Tensor` using `torch.tensor()` or `torch.from_numpy()` before passing it to the attention module. Example: `x = torch.randn(1, 1024, 512)` or `x = torch.tensor(your_list_or_array, dtype=torch.float32)`.
Ensure the input `x` is a `torch.Tensor`. If it's a tuple of tensors, concatenate them, or if it's a simple tuple of data, convert it to a tensor. Example: `x = torch.tensor(your_tuple_data, dtype=torch.float32)` or `x = torch.cat(your_tuple_of_tensors, dim=-1)`.