{"about":{"non_affiliation":"Code with Papers and Syntology are not affiliated with, endorsed by, or sponsored by Papers with Code, Meta, or the pwc-archive mirror.","site":"https://codewithpapers.app","licence":"CC BY-SA 4.0","licence_url":"https://creativecommons.org/licenses/by-sa/4.0/legalcode","attribution":"https://codewithpapers.app/attribution","modified":"archive material modified by Syntology; see the attribution page","syntology":{"site":"https://syntology.ai","developers":"https://syntology.ai/developers","mcp":{"server":"https://syntology.ai/mcp","transport":"streamable-http","server_card":"https://syntology.ai/.well-known/mcp/server-card.json","auth":{"type":"trial token, no account","trial_token":"https://syntology.ai/api/oauth/trial/token","method":"POST","docs":"https://syntology.ai/developers"}},"have":"https://syntology.ai/api/graph/have?x=<method, arXiv id or title> (free, answers coverage only)","paper_base":"https://syntology.ai/paper/","atlas_base":"https://app.syntology.ai/?focus="},"machine_readable":[{"url":"https://codewithpapers.app/llms.txt","what":"the machine catalog: every machine-readable file, counted"},{"url":"https://codewithpapers.app/index/manifest.json","what":"paper-to-code index by arXiv id, with Syntology's counts"},{"url":"https://codewithpapers.app/search/manifest.json","what":"site search index (titles, authors) and its files"},{"url":"https://codewithpapers.app/download","what":"bulk files: Syntology's layer, described there"},{"url":"https://codewithpapers.app/build_manifest.json","what":"the build record: inputs, counts, exclusions, probes"}]},"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","slug":"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,"papers_order":"date (newest first), then slug","paper_counts":{"archive_papers_tagged":1,"with_a_code_link":1,"where_syntology_ran_a_sample":0,"not_listed_spam_title":0,"listed":1,"listed_where_code_ran":0,"where_syntology_ran_a_sample_split":{"with_a_run_with_no_instrument_failure":0,"every_run_a_failure_of_syntologys_instrument":0,"listed_with_a_run_with_no_instrument_failure":0,"listed_every_run_a_failure_of_syntologys_instrument":0,"filter":{"states":["a run with no instrument failure","any run, instrument failures included"],"default":"a run with no instrument failure","note":"on the 'only where code ran' pages the default hides, in the browser, the rows where every run was a failure of Syntology's instrument; the second state shows them again. Rows are hidden, never re-ordered; these twins list every row"}},"definition":"distinct papers the archive tags; 'where Syntology ran a sample' counts papers with at least one harvested sample that ran, which is not a correctness claim"},"papers_where_code_ran":0,"papers_where_code_ran_first_page":null,"papers_where_code_ran_caption":"We ran code from the paper's repository; we did not isolate this method inside it.","pages":[],"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-10-05T00:36:11+00:00","syntology":{"source":"Syntology's have check for this name (the free coverage lookup), read from Syntology's graph","have_url":"https://syntology.ai/api/graph/have?x=WERSA","read_at":"2026-10-05T01:03:55+00:00","query":"WERSA","kind":"method","have":"unknown_name","tier":null,"verified_implementation":null,"level":null,"level_meaning":"the served verification level, per answer; never summed across methods","resolved_to":null,"resolution":null,"resolved_name_differs":false,"absence_asserted":false,"absence_unchecked":null,"no_code_is_about_this_method":false,"subject":null,"archive_paper_arxiv_id":"2507.08637","repositories":null,"retrieve_with":null,"name_competition":{"verdict":"none","may_be_wrong_sense":false,"bearers_found":0,"served":null,"leading_other_bearer":null,"n_other_bearers_listed":0,"note":"Nothing in this corpus is named 'WERSA' -- no method node, no paper announcing it in its title, no expanded form declaring it as an acronym."},"name_competition_text":null,"near_matches":[],"code_nearby":[],"code_nearby_caption":null,"origin_paper":null,"line":"name not resolved (unknown_name). The lookup matched no method, paper or venue by this name; that asserts nothing about whether code exists.","level_legend":null},"syntology_have_state":"loaded","record_sha256":"de7823aec1d8b8bf5738ee7043f1dfaecde24f032482a3e20a9438c999b39618","record_changed_at":"2026-10-05","record_changed_at_basis":"changed"}