{"id":18423322,"url":"https://github.com/webis-de/pytorch-window-matmul","last_synced_at":"2025-10-31T04:30:26.289Z","repository":{"id":173651332,"uuid":"638215554","full_name":"webis-de/pytorch-window-matmul","owner":"webis-de","description":"a custom CUDA kernel for windowed matrix multiplication","archived":false,"fork":false,"pushed_at":"2024-01-03T16:27:47.000Z","size":75,"stargazers_count":4,"open_issues_count":0,"forks_count":0,"subscribers_count":20,"default_branch":"main","last_synced_at":"2024-12-24T18:49:43.711Z","etag":null,"topics":["cuda","cuda-kernel","pytorch"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/webis-de.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-05-09T10:19:34.000Z","updated_at":"2024-11-21T00:14:27.000Z","dependencies_parsed_at":null,"dependency_job_id":"ae90ae0e-eecd-4365-adef-0b3f2ceb3f67","html_url":"https://github.com/webis-de/pytorch-window-matmul","commit_stats":null,"previous_names":["webis-de/pytorch-window-matmul"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/webis-de%2Fpytorch-window-matmul","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/webis-de%2Fpytorch-window-matmul/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/webis-de%2Fpytorch-window-matmul/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/webis-de%2Fpytorch-window-matmul/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/webis-de","download_url":"https://codeload.github.com/webis-de/pytorch-window-matmul/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":239102855,"owners_count":19582057,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cuda","cuda-kernel","pytorch"],"created_at":"2024-11-06T04:36:48.060Z","updated_at":"2025-10-31T04:30:20.978Z","avatar_url":"https://github.com/webis-de.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Pytorch Window Matmul\n\n## Description\n\nThis repository contains a custom PyTorch CUDA kernel for batched windowed matrix multiplication. This is particularly useful for the windowed local attention in sparse attention transformer models such as [BigBird](https://proceedings.neurips.cc/paper/2020/hash/c8512d142a2d849725f31a9a7a361ab9-Abstract.html) and [Longformer](https://arxiv.org/abs/2004.05150). Given two matrices $Q$ and $K$ of size (B, M, N) and (B, N, M) it computes the windowed diagonal of the batched matrix product $QK = A$ for a window size $w$. An entry $a_{i,j} = \\sum_{k=1}^N Q_{i,k} \\cdot K_{k,i + j - w}$ and $A$ has size (B, M, 2w + 1). The image below gives an example for M = 10 and $w$ = 2. The window extends $w$ entries to left and right of the diagonal. Any entries outside the window are set to zero.\n\n![example diagonal matrix](assets/attention.svg)\n\nTo complete the windowed attention operation, the attention matrix $A$ needs to be multiplied with another matrix $V$. Here the windowed matrix needs to unraveled and multiplied with the correct windowed entries in $V$. We call this the \"unwindow\" operation. An entry of the output matrix $O$ is given by $o_{i,k} = \\sum_{j=1}^{2w + 1} a_{i,j} \\cdot v_{i + j - w, k}$ and $O$ has size (B, M, N).\n\n\n## Installation and Usage\n\nBe sure to have the cudatoolkit installed before running pip install. We recommend installing the cudatoolkit using conda.\n\n```bash\nconda install -c nvidia cuda-toolkit\n```\n\nTo install the package run\n\n```bash\npip install git+https://github.com/webis-de/pytorch-window-matmul.git\n```\n\nAn example on how to use the kernel:\n\n```python\nimport torch\nimport window_matmul\n\n# create some random matrices\nbatch_size = 2\nseq_len = 10\nhidden_size = 5\nwindow_size = 2\nq = torch.rand(batch_size, seq_len, hidden_size)\nk = torch.rand(batch_size, hidden_size, seq_len)\nv = torch.rand(batch_size, seq_len, hidden_size)\n\n# compute windowed attention\na = window_matmul.window_matmul(q, k, window_size)\n\nassert a.shape[-1] == 2 * window_size + 1\n\n# compute output\no = window_matmul.unwindow_matmul(a, v, window_size)\n\n```\n\nNOTE: The cpu version is not optimized and is only for reference. The cuda version is optimized and is the one to use.\n\n## Benchmark\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwebis-de%2Fpytorch-window-matmul","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fwebis-de%2Fpytorch-window-matmul","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwebis-de%2Fpytorch-window-matmul/lists"}