### Install Strassen Attention Source: https://github.com/lucidrains/strassen-attention/blob/main/README.md Install the package via pip. ```shell $ pip install strassen-attention ``` -------------------------------- ### Causal StrassenTransformer for Language Modeling Source: https://context7.com/lucidrains/strassen-attention/llms.txt Configure a causal StrassenTransformer with rotary embeddings and grouped-query attention for language modeling. Ensure PyTorch is installed. ```python import torch from strassen_attention import StrassenTransformer causal_transformer = StrassenTransformer( dim=768, depth=12, heads=12, dim_head=64, causal=True, rotary_embed=True, ff_expansion=4.0, final_norm=True, # Apply final RMSNorm attn_kwargs=dict( qk_rmsnorm=True, attn_logits_clamp_value=40., kv_heads=4, # Grouped-query attention dim_head_values=64 ) ) lm_input = torch.randn(1, 2048, 768) lm_output = causal_transformer(lm_input) ``` -------------------------------- ### Initialize Strassen Transformer Source: https://context7.com/lucidrains/strassen-attention/llms.txt Import the StrassenTransformer module for building full encoder/decoder stacks. ```python import torch from strassen_attention import StrassenTransformer ``` -------------------------------- ### Simple Language Model Head with StrassenTransformer Source: https://context7.com/lucidrains/strassen-attention/llms.txt Implement a basic language model using StrassenTransformer and a linear layer for logits. Requires PyTorch. ```python import torch from strassen_attention import StrassenTransformer class StrassenLM(torch.nn.Module): def __init__(self, vocab_size, dim, depth, heads): super().__init__() self.embed = torch.nn.Embedding(vocab_size, dim) self.transformer = StrassenTransformer( dim=dim, depth=depth, heads=heads, causal=True, rotary_embed=True ) self.to_logits = torch.nn.Linear(dim, vocab_size, bias=False) def forward(self, token_ids): x = self.embed(token_ids) x = self.transformer(x) return self.to_logits(x) model = StrassenLM(vocab_size=32000, dim=512, depth=6, heads=8) tokens = torch.randint(0, 32000, (2, 128)) logits = model(tokens) print(f"Logits shape: {logits.shape}") # torch.Size([2, 128, 32000]) ``` -------------------------------- ### Basic StrassenTransformer for Sequence Modeling Source: https://context7.com/lucidrains/strassen-attention/llms.txt Instantiate a bidirectional StrassenTransformer for general sequence modeling tasks. Requires PyTorch. ```python import torch from strassen_attention import StrassenTransformer transformer = StrassenTransformer( dim=512, depth=6, # Number of transformer layers heads=8, dim_head=64, causal=False, # Bidirectional (encoder-style) ff_expansion=4.0 # Feedforward expansion factor ) x = torch.randn(2, 256, 512) # (batch, seq_len, dim) output = transformer(x) print(f"Transformer output: {output.shape}") # torch.Size([2, 256, 512]) ``` -------------------------------- ### Integration in a U-Net Style Architecture Source: https://context7.com/lucidrains/strassen-attention/llms.txt Demonstrates integrating FeatureMapWrapper within a U-Net block for residual attention. Requires PyTorch. ```python import torch from strassen_attention import StrassenTransformer, FeatureMapWrapper # Integration in a U-Net style architecture class UNetBlock(torch.nn.Module): def __init__(self, channels, transformer_depth=2): super().__init__() self.conv = torch.nn.Conv2d(channels, channels, 3, padding=1) self.attn = FeatureMapWrapper( StrassenTransformer(dim=channels, depth=transformer_depth, heads=8) ) def forward(self, x): x = self.conv(x) x = self.attn(x) + x # Residual attention return x unet_block = UNetBlock(channels=128, transformer_depth=2) fmap = torch.randn(4, 128, 64, 64) output = unet_block(fmap) print(f"U-Net block output: {output.shape}") # torch.Size([4, 128, 64, 64]) ``` -------------------------------- ### Initialize Strassen Transformer Source: https://github.com/lucidrains/strassen-attention/blob/main/README.md Create a transformer model using the StrassenTransformer class. ```python import torch from strassen_attention.strassen_transformer import StrassenTransformer transformer = StrassenTransformer(dim = 512, depth = 4) x = torch.randn(1, 16 * 16, 512) assert transformer(x).shape == x.shape ``` -------------------------------- ### Initialize Strassen Multi-Head Attention Source: https://github.com/lucidrains/strassen-attention/blob/main/README.md Use the StrassenMHA module for multi-head attention with optional causal masking. ```python import torch from strassen_attention.strassen_mha import StrassenMHA mha = StrassenMHA(dim = 512, causal = True) tokens = torch.randn(1, 256, 512) assert mha(tokens).shape == tokens.shape ``` -------------------------------- ### FeatureMapWrapper for Vision and U-Net Architectures Source: https://context7.com/lucidrains/strassen-attention/llms.txt Adapt StrassenTransformer for multi-dimensional feature maps using FeatureMapWrapper. Handles reshaping for image and video data. Requires PyTorch. ```python import torch from strassen_attention import StrassenTransformer, FeatureMapWrapper # Create transformer and wrap it for feature map processing transformer = StrassenTransformer( dim=256, depth=4, heads=8, dim_head=32, causal=False ) wrapped = FeatureMapWrapper(transformer) # 2D feature maps (images): (batch, channels, height, width) image_features = torch.randn(2, 256, 32, 32) output_2d = wrapped(image_features) print(f"2D output: {output_2d.shape}") # torch.Size([2, 256, 32, 32]) # 3D feature maps (video): (batch, channels, depth, height, width) video_features = torch.randn(1, 256, 8, 16, 16) output_3d = wrapped(video_features) print(f"3D output: {output_3d.shape}") # torch.Size([1, 256, 8, 16, 16]) ``` -------------------------------- ### Citation BibTeX Source: https://github.com/lucidrains/strassen-attention/blob/main/README.md BibTeX entries for citing the relevant research papers. ```bibtex @misc{kozachinskiy2025strassenattentionunlockingcompositional, title = {Strassen Attention: Unlocking Compositional Abilities in Transformers Based on a New Lower Bound Method}, author = {Alexander Kozachinskiy and Felipe Urrutia and Hector Jimenez and Tomasz Steifer and Germán Pizarro and Matías Fuentes and Francisco Meza and Cristian B. Calderon and Cristóbal Rojas}, year = {2025}, eprint = {2501.19215}, archivePrefix = {arXiv}, primaryClass = {cs.LG}, url = {https://arxiv.org/abs/2501.19215}, } ``` ```bibtex @article{Peng2024OnLO, title = {On Limitations of the Transformer Architecture}, author = {Binghui Peng and Srini Narayanan and Christos Papadimitriou}, journal = {ArXiv}, year = {2024}, volume = {abs/2402.08164}, url = {https://api.semanticscholar.org/CorpusID:267636545} } ``` -------------------------------- ### Use Strassen Attention Function Source: https://github.com/lucidrains/strassen-attention/blob/main/README.md Apply the strassen_attend function to query, key, and value tensors. ```python import torch from strassen_attention import strassen_attend q = torch.randn(1, 8, 32, 16) k = torch.randn(1, 8, 32, 16) v = torch.randn(1, 8, 32, 16) attended = strassen_attend( q, k, k.clone(), v, v.clone() ) assert attended.shape == q.shape ``` -------------------------------- ### Compute Strassen Attention Source: https://context7.com/lucidrains/strassen-attention/llms.txt The core attention function supports bidirectional or causal modes, cross-attention, and custom activation functions. ```python import torch from strassen_attention import strassen_attend # Basic self-attention usage # Input shapes: (batch, heads, sequence_length, head_dim) q = torch.randn(2, 8, 128, 64) # queries k1 = torch.randn(2, 8, 128, 64) # first set of keys k2 = torch.randn(2, 8, 128, 64) # second set of keys v1 = torch.randn(2, 8, 128, 64) # first set of values v2 = torch.randn(2, 8, 128, 64) # second set of values # Non-causal attention (bidirectional) output = strassen_attend(q, k1, k2, v1, v2) print(f"Output shape: {output.shape}") # torch.Size([2, 8, 128, 64]) # Causal attention for autoregressive models causal_output = strassen_attend( q, k1, k2, v1, v2, causal=True, sim_clamp_value=30.0 # Soft-clamp similarities for numerical stability ) # Cross-attention with different key/value sequence lengths q_cross = torch.randn(2, 8, 64, 64) # 64 query positions k1_cross = torch.randn(2, 8, 256, 64) # 256 key positions k2_cross = torch.randn(2, 8, 256, 64) v1_cross = torch.randn(2, 8, 256, 64) v2_cross = torch.randn(2, 8, 256, 64) cross_output = strassen_attend(q_cross, k1_cross, k2_cross, v1_cross, v2_cross) print(f"Cross-attention output: {cross_output.shape}") # torch.Size([2, 8, 64, 64]) # Custom activation function (default is torch.exp) custom_output = strassen_attend( q, k1, k2, v1, v2, activate_fn=torch.nn.functional.softplus ) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.