{"url":"/method/wersa","slug":"wersa","name":"WERSA","full_name":"Wavelet-Enhanced Random Spectral Attention","full_name_withheld":false,"description_markdown":"# WERSA: Wavelet-Enhanced Random Spectral Attention\r\n\r\nThis repository provides the official implementation of WERSA, a novel attention mechanism with linear O(n) time complexity, designed to scale Transformer models to very long sequences without a performance trade-off.\r\n\r\nOur paper, \"Scaling Attention to Very Long Sequences in Linear Time with Wavelet-Enhanced Random Spectral Attention (WERSA)\", is available on [arXiv:2507.08637](https://arxiv.org/abs/2507.08637).\r\n\r\n## 🔬 The Science Behind WERSA\r\n\r\nStandard attention mechanisms have a quadratic (O(n²)) complexity that makes processing long sequences impractical. WERSA solves this by combining several powerful principles to achieve linear (O(n)) efficiency while maintaining high performance.\r\n\r\n- **Multi-Resolution Analysis**: Uses Haar wavelet transforms to decompose the input into multiple scales, capturing both local details and global context.\r\n- **Adaptive Filtering**: An MLP generates input-dependent filters and learnable scale_weights modulate each wavelet level, allowing the model to dynamically prioritize the most informative frequency components.\r\n- **Linear Complexity via Random Features**: Uses random feature projection to approximate the softmax kernel, avoiding the computation of the full quadratic attention matrix.\r\n\r\n## ⚙️ Installation\r\n\r\nFirst, ensure you have PyTorch and Hugging Face Transformers installed. Then, install the wersa package directly from this repository.\r\n\r\n```bash\r\n# 1. Install core dependencies (example for CUDA 12.1)\r\npip install torch --index-url https://download.pytorch.org/whl/cu121\r\npip install transformers\r\n\r\n# 2. Install the WERSA package from this repository\r\npip install git+https://github.com/vincenzodentamaro/wersa.git\r\n```\r\n\r\n## 🚀 Quickstart: Building a Qwen-like Model with WERSA\r\n\r\nYou can easily build a Qwen-style causal language model with WERSA attention by importing the `WersaConfig` and `WersaForCausalLM` classes from the package.\r\n\r\n### Building an 8B Parameter Model\r\n\r\nThis snippet creates an ~8B parameter model with a configuration similar to state-of-the-art models like Qwen2-7B.\r\n\r\n```python\r\nfrom wersa import WersaConfig, WersaForCausalLM\r\nfrom transformers import AutoTokenizer\r\n\r\n# Load a compatible tokenizer\r\ntokenizer = AutoTokenizer.from_pretrained(\"Qwen/Qwen2-7B\")\r\n\r\n# Define the configuration for the 8B model\r\nconfig_8b = WersaConfig(\r\n    vocab_size=len(tokenizer),\r\n    pad_token_id=tokenizer.pad_token_id,\r\n    hidden_size=4096,\r\n    num_hidden_layers=32,\r\n    num_attention_heads=32,\r\n    intermediate_size=11008,\r\n    max_position_embeddings=4096\r\n)\r\n\r\n# Instantiate the model\r\nmodel_8b = WersaForCausalLM(config_8b)\r\nprint(f\"8B Model created with ~{model_8b.num_parameters() / 1e9:.2f}B parameters.\")\r\n```\r\n\r\n### Building a 0.6B Parameter Model\r\n\r\nThis snippet creates a smaller ~0.6B parameter model, perfect for faster experiments or deployment on more constrained hardware.\r\n\r\n```python\r\nfrom wersa import WersaConfig, WersaForCausalLM\r\nfrom transformers import AutoTokenizer\r\n\r\n# Load a compatible tokenizer\r\ntokenizer = AutoTokenizer.from_pretrained(\"Qwen/Qwen2-1.5B\")\r\n\r\n# Define the configuration for the 0.6B model\r\nconfig_0_6b = WersaConfig(\r\n    vocab_size=len(tokenizer),\r\n    pad_token_id=tokenizer.pad_token_id,\r\n    hidden_size=1024,\r\n    num_hidden_layers=24,\r\n    num_attention_heads=16,\r\n    intermediate_size=2816,\r\n    max_position_embeddings=1024\r\n)\r\n\r\n# Instantiate the model\r\nmodel_0_6b = WersaForCausalLM(config_0_6b)\r\nprint(f\"0.6B Model created with ~{model_0_6b.num_parameters() / 1e9:.2f}B parameters.\")\r\n```\r\n\r\n## 📖 Training and Examples\r\n\r\nThis repository includes complete scripts to demonstrate how to pre-train these models from scratch and test their generation capabilities.\r\n\r\n- `train_and_generate_1b.py`: A full example for training a ~1B parameter model.\r\n- `train_and_generate_8b.py`: A full example for training the 8B parameter model.\r\n\r\n## 📜 Citation\r\n\r\nIf you find WERSA useful in your research, please consider citing our paper:\r\n\r\n```bibtex\r\n@misc{dentamaro2025scaling,\r\n      title={Scaling Attention to Very Long Sequences in Linear Time with Wavelet-Enhanced Random Spectral Attention (WERSA)}, \r\n      author={Vincenzo Dentamaro},\r\n      year={2025},\r\n      eprint={2507.08637},\r\n      archivePrefix={arXiv},\r\n      primaryClass={cs.LG}\r\n}\r\n```\r\n\r\n## 📄 License\r\n\r\nThis project is licensed under the Apache License 2.0.","description_state":"present","introduced_year":null,"introduced_by":{"title":"Scaling Attention to Very Long Sequences in Linear Time with Wavelet-Enhanced Random Spectral Attention (WERSA)","paper":"/paper/scaling-attention-to-very-long-sequences-in","first_author":"Vincenzo Dentamaro","n_authors":1,"url_abs":null,"archive_paper_url":"https://paperswithcode.com/paper/scaling-attention-to-very-long-sequences-in"},"source":{"url":"https://arxiv.org/abs/2507.08637v1","title":"Scaling Attention to Very Long Sequences in Linear Time with Wavelet-Enhanced Random Spectral Attention (WERSA)","url_on_a_paper_host":true},"code_snippet_url":null,"code_snippet_url_on_a_code_host":false,"categories":[{"area":"General","area_id":"general","collection":"Attention Mechanisms","url":"/methods/category/attention-mechanisms","pwc_aliases":["attention-mechanisms-1"]}],"n_papers_tagged":1,"archive_num_papers":0,"papers_newest_first":[{"paper":"/paper/scaling-attention-to-very-long-sequences-in","title":"Scaling Attention to Very Long Sequences in Linear Time with Wavelet-Enhanced Random Spectral Attention (WERSA)","date":"2025-07-11","arxiv_id":"2507.08637","n_code_links":1,"syntology":null}],"papers_shown":1,"tasks":[{"task":null,"name":"GPU","papers":1}],"tasks_shown":1,"n_tasks":1,"usage_by_year":[{"year":"2025","papers":1}],"row_source":"methods_table","archive":{"source":"pwc-archive (Hugging Face), CC BY-SA 4.0","snapshot":"2025-07-28","archive_url":"https://paperswithcode.com/method/wersa"},"syntology_read_at":"2026-09-24T18:15:14+00:00"}