ZTWHHH commited on
Commit
f343e24
·
verified ·
1 Parent(s): 84c8a96

Add files using upload-large-folder tool

Browse files
This view is limited to 50 files because it contains too many changes.   See raw diff
Files changed (50) hide show
  1. evalkit_tf446/lib/python3.10/site-packages/attrs-24.2.0.dist-info/INSTALLER +1 -0
  2. evalkit_tf446/lib/python3.10/site-packages/attrs-24.2.0.dist-info/METADATA +242 -0
  3. evalkit_tf446/lib/python3.10/site-packages/attrs-24.2.0.dist-info/RECORD +56 -0
  4. evalkit_tf446/lib/python3.10/site-packages/attrs-24.2.0.dist-info/REQUESTED +0 -0
  5. evalkit_tf446/lib/python3.10/site-packages/attrs-24.2.0.dist-info/WHEEL +4 -0
  6. evalkit_tf446/lib/python3.10/site-packages/attrs-24.2.0.dist-info/licenses/LICENSE +21 -0
  7. evalkit_tf446/lib/python3.10/site-packages/ezcolorlog/__init__.py +63 -0
  8. evalkit_tf446/lib/python3.10/site-packages/ezcolorlog/__pycache__/__init__.cpython-310.pyc +0 -0
  9. evalkit_tf446/lib/python3.10/site-packages/ezcolorlog/__pycache__/config.cpython-310.pyc +0 -0
  10. evalkit_tf446/lib/python3.10/site-packages/ezcolorlog/__pycache__/format.cpython-310.pyc +0 -0
  11. evalkit_tf446/lib/python3.10/site-packages/ezcolorlog/__pycache__/logfile.cpython-310.pyc +0 -0
  12. evalkit_tf446/lib/python3.10/site-packages/ezcolorlog/__pycache__/notebook.cpython-310.pyc +0 -0
  13. evalkit_tf446/lib/python3.10/site-packages/ezcolorlog/__pycache__/patch.cpython-310.pyc +0 -0
  14. evalkit_tf446/lib/python3.10/site-packages/ezcolorlog/__pycache__/wrapper.cpython-310.pyc +0 -0
  15. evalkit_tf446/lib/python3.10/site-packages/ezcolorlog/config.py +32 -0
  16. evalkit_tf446/lib/python3.10/site-packages/ezcolorlog/defaults.yaml +35 -0
  17. evalkit_tf446/lib/python3.10/site-packages/ezcolorlog/format.py +31 -0
  18. evalkit_tf446/lib/python3.10/site-packages/ezcolorlog/logfile.py +66 -0
  19. evalkit_tf446/lib/python3.10/site-packages/ezcolorlog/notebook.py +64 -0
  20. evalkit_tf446/lib/python3.10/site-packages/ezcolorlog/patch.py +42 -0
  21. evalkit_tf446/lib/python3.10/site-packages/ezcolorlog/wrapper.py +115 -0
  22. evalkit_tf446/lib/python3.10/site-packages/flash_attn/__init__.py +11 -0
  23. evalkit_tf446/lib/python3.10/site-packages/flash_attn/bert_padding.py +218 -0
  24. evalkit_tf446/lib/python3.10/site-packages/flash_attn/flash_attn_interface.py +1574 -0
  25. evalkit_tf446/lib/python3.10/site-packages/flash_attn/flash_attn_triton.py +1160 -0
  26. evalkit_tf446/lib/python3.10/site-packages/flash_attn/flash_attn_triton_amd/__pycache__/__init__.cpython-310.pyc +0 -0
  27. evalkit_tf446/lib/python3.10/site-packages/flash_attn/flash_attn_triton_amd/__pycache__/bench.cpython-310.pyc +0 -0
  28. evalkit_tf446/lib/python3.10/site-packages/flash_attn/flash_attn_triton_amd/__pycache__/fwd_prefill.cpython-310.pyc +0 -0
  29. evalkit_tf446/lib/python3.10/site-packages/flash_attn/flash_attn_triton_amd/__pycache__/fwd_ref.cpython-310.pyc +0 -0
  30. evalkit_tf446/lib/python3.10/site-packages/flash_attn/flash_attn_triton_amd/__pycache__/interface_fa.cpython-310.pyc +0 -0
  31. evalkit_tf446/lib/python3.10/site-packages/flash_attn/flash_attn_triton_amd/fwd_prefill.py +652 -0
  32. evalkit_tf446/lib/python3.10/site-packages/flash_attn/flash_attn_triton_amd/test.py +724 -0
  33. evalkit_tf446/lib/python3.10/site-packages/flash_attn/flash_attn_triton_og.py +365 -0
  34. evalkit_tf446/lib/python3.10/site-packages/flash_attn/flash_blocksparse_attention.py +197 -0
  35. evalkit_tf446/lib/python3.10/site-packages/flash_attn/flash_blocksparse_attn_interface.py +200 -0
  36. evalkit_tf446/lib/python3.10/site-packages/flash_attn/fused_softmax.py +201 -0
  37. evalkit_tf446/lib/python3.10/site-packages/flash_attn/layers/__init__.py +0 -0
  38. evalkit_tf446/lib/python3.10/site-packages/flash_attn/layers/__pycache__/patch_embed.cpython-310.pyc +0 -0
  39. evalkit_tf446/lib/python3.10/site-packages/flash_attn/layers/patch_embed.py +67 -0
  40. evalkit_tf446/lib/python3.10/site-packages/flash_attn/layers/rotary.py +528 -0
  41. evalkit_tf446/lib/python3.10/site-packages/flash_attn/modules/__init__.py +0 -0
  42. evalkit_tf446/lib/python3.10/site-packages/flash_attn/modules/__pycache__/__init__.cpython-310.pyc +0 -0
  43. evalkit_tf446/lib/python3.10/site-packages/flash_attn/modules/__pycache__/block.cpython-310.pyc +0 -0
  44. evalkit_tf446/lib/python3.10/site-packages/flash_attn/modules/__pycache__/embedding.cpython-310.pyc +0 -0
  45. evalkit_tf446/lib/python3.10/site-packages/flash_attn/modules/__pycache__/mha.cpython-310.pyc +0 -0
  46. evalkit_tf446/lib/python3.10/site-packages/flash_attn/modules/__pycache__/mlp.cpython-310.pyc +0 -0
  47. evalkit_tf446/lib/python3.10/site-packages/flash_attn/modules/block.py +397 -0
  48. evalkit_tf446/lib/python3.10/site-packages/flash_attn/modules/embedding.py +216 -0
  49. evalkit_tf446/lib/python3.10/site-packages/flash_attn/modules/mha.py +1020 -0
  50. evalkit_tf446/lib/python3.10/site-packages/flash_attn/modules/mlp.py +191 -0
evalkit_tf446/lib/python3.10/site-packages/attrs-24.2.0.dist-info/INSTALLER ADDED
@@ -0,0 +1 @@
 
 
1
+ pip
evalkit_tf446/lib/python3.10/site-packages/attrs-24.2.0.dist-info/METADATA ADDED
@@ -0,0 +1,242 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ Metadata-Version: 2.3
2
+ Name: attrs
3
+ Version: 24.2.0
4
+ Summary: Classes Without Boilerplate
5
+ Project-URL: Documentation, https://www.attrs.org/
6
+ Project-URL: Changelog, https://www.attrs.org/en/stable/changelog.html
7
+ Project-URL: GitHub, https://github.com/python-attrs/attrs
8
+ Project-URL: Funding, https://github.com/sponsors/hynek
9
+ Project-URL: Tidelift, https://tidelift.com/subscription/pkg/pypi-attrs?utm_source=pypi-attrs&utm_medium=pypi
10
+ Author-email: Hynek Schlawack <hs@ox.cx>
11
+ License-Expression: MIT
12
+ License-File: LICENSE
13
+ Keywords: attribute,boilerplate,class
14
+ Classifier: Development Status :: 5 - Production/Stable
15
+ Classifier: License :: OSI Approved :: MIT License
16
+ Classifier: Programming Language :: Python :: 3.7
17
+ Classifier: Programming Language :: Python :: 3.8
18
+ Classifier: Programming Language :: Python :: 3.9
19
+ Classifier: Programming Language :: Python :: 3.10
20
+ Classifier: Programming Language :: Python :: 3.11
21
+ Classifier: Programming Language :: Python :: 3.12
22
+ Classifier: Programming Language :: Python :: 3.13
23
+ Classifier: Programming Language :: Python :: Implementation :: CPython
24
+ Classifier: Programming Language :: Python :: Implementation :: PyPy
25
+ Classifier: Typing :: Typed
26
+ Requires-Python: >=3.7
27
+ Requires-Dist: importlib-metadata; python_version < '3.8'
28
+ Provides-Extra: benchmark
29
+ Requires-Dist: cloudpickle; (platform_python_implementation == 'CPython') and extra == 'benchmark'
30
+ Requires-Dist: hypothesis; extra == 'benchmark'
31
+ Requires-Dist: mypy>=1.11.1; (platform_python_implementation == 'CPython' and python_version >= '3.9') and extra == 'benchmark'
32
+ Requires-Dist: pympler; extra == 'benchmark'
33
+ Requires-Dist: pytest-codspeed; extra == 'benchmark'
34
+ Requires-Dist: pytest-mypy-plugins; (platform_python_implementation == 'CPython' and python_version >= '3.9' and python_version < '3.13') and extra == 'benchmark'
35
+ Requires-Dist: pytest-xdist[psutil]; extra == 'benchmark'
36
+ Requires-Dist: pytest>=4.3.0; extra == 'benchmark'
37
+ Provides-Extra: cov
38
+ Requires-Dist: cloudpickle; (platform_python_implementation == 'CPython') and extra == 'cov'
39
+ Requires-Dist: coverage[toml]>=5.3; extra == 'cov'
40
+ Requires-Dist: hypothesis; extra == 'cov'
41
+ Requires-Dist: mypy>=1.11.1; (platform_python_implementation == 'CPython' and python_version >= '3.9') and extra == 'cov'
42
+ Requires-Dist: pympler; extra == 'cov'
43
+ Requires-Dist: pytest-mypy-plugins; (platform_python_implementation == 'CPython' and python_version >= '3.9' and python_version < '3.13') and extra == 'cov'
44
+ Requires-Dist: pytest-xdist[psutil]; extra == 'cov'
45
+ Requires-Dist: pytest>=4.3.0; extra == 'cov'
46
+ Provides-Extra: dev
47
+ Requires-Dist: cloudpickle; (platform_python_implementation == 'CPython') and extra == 'dev'
48
+ Requires-Dist: hypothesis; extra == 'dev'
49
+ Requires-Dist: mypy>=1.11.1; (platform_python_implementation == 'CPython' and python_version >= '3.9') and extra == 'dev'
50
+ Requires-Dist: pre-commit; extra == 'dev'
51
+ Requires-Dist: pympler; extra == 'dev'
52
+ Requires-Dist: pytest-mypy-plugins; (platform_python_implementation == 'CPython' and python_version >= '3.9' and python_version < '3.13') and extra == 'dev'
53
+ Requires-Dist: pytest-xdist[psutil]; extra == 'dev'
54
+ Requires-Dist: pytest>=4.3.0; extra == 'dev'
55
+ Provides-Extra: docs
56
+ Requires-Dist: cogapp; extra == 'docs'
57
+ Requires-Dist: furo; extra == 'docs'
58
+ Requires-Dist: myst-parser; extra == 'docs'
59
+ Requires-Dist: sphinx; extra == 'docs'
60
+ Requires-Dist: sphinx-notfound-page; extra == 'docs'
61
+ Requires-Dist: sphinxcontrib-towncrier; extra == 'docs'
62
+ Requires-Dist: towncrier<24.7; extra == 'docs'
63
+ Provides-Extra: tests
64
+ Requires-Dist: cloudpickle; (platform_python_implementation == 'CPython') and extra == 'tests'
65
+ Requires-Dist: hypothesis; extra == 'tests'
66
+ Requires-Dist: mypy>=1.11.1; (platform_python_implementation == 'CPython' and python_version >= '3.9') and extra == 'tests'
67
+ Requires-Dist: pympler; extra == 'tests'
68
+ Requires-Dist: pytest-mypy-plugins; (platform_python_implementation == 'CPython' and python_version >= '3.9' and python_version < '3.13') and extra == 'tests'
69
+ Requires-Dist: pytest-xdist[psutil]; extra == 'tests'
70
+ Requires-Dist: pytest>=4.3.0; extra == 'tests'
71
+ Provides-Extra: tests-mypy
72
+ Requires-Dist: mypy>=1.11.1; (platform_python_implementation == 'CPython' and python_version >= '3.9') and extra == 'tests-mypy'
73
+ Requires-Dist: pytest-mypy-plugins; (platform_python_implementation == 'CPython' and python_version >= '3.9' and python_version < '3.13') and extra == 'tests-mypy'
74
+ Description-Content-Type: text/markdown
75
+
76
+ <p align="center">
77
+ <a href="https://www.attrs.org/">
78
+ <img src="https://raw.githubusercontent.com/python-attrs/attrs/main/docs/_static/attrs_logo.svg" width="35%" alt="attrs" />
79
+ </a>
80
+ </p>
81
+
82
+
83
+ *attrs* is the Python package that will bring back the **joy** of **writing classes** by relieving you from the drudgery of implementing object protocols (aka [dunder methods](https://www.attrs.org/en/latest/glossary.html#term-dunder-methods)).
84
+ [Trusted by NASA](https://docs.github.com/en/account-and-profile/setting-up-and-managing-your-github-profile/customizing-your-profile/personalizing-your-profile#list-of-qualifying-repositories-for-mars-2020-helicopter-contributor-achievement) for Mars missions since 2020!
85
+
86
+ Its main goal is to help you to write **concise** and **correct** software without slowing down your code.
87
+
88
+
89
+ ## Sponsors
90
+
91
+ *attrs* would not be possible without our [amazing sponsors](https://github.com/sponsors/hynek).
92
+ Especially those generously supporting us at the *The Organization* tier and higher:
93
+
94
+ <!-- sponsor-break-begin -->
95
+
96
+ <p align="center">
97
+
98
+ <!-- [[[cog
99
+ import pathlib, tomllib
100
+
101
+ for sponsor in tomllib.loads(pathlib.Path("pyproject.toml").read_text())["tool"]["sponcon"]["sponsors"]:
102
+ print(f'<a href="{sponsor["url"]}"><img title="{sponsor["title"]}" src="https://www.attrs.org/en/24.2.0/_static/sponsors/{sponsor["img"]}" width="190" /></a>')
103
+ ]]] -->
104
+ <a href="https://www.variomedia.de/"><img title="Variomedia AG" src="https://www.attrs.org/en/24.2.0/_static/sponsors/Variomedia.svg" width="190" /></a>
105
+ <a href="https://tidelift.com/?utm_source=lifter&utm_medium=referral&utm_campaign=hynek"><img title="Tidelift" src="https://www.attrs.org/en/24.2.0/_static/sponsors/Tidelift.svg" width="190" /></a>
106
+ <a href="https://klaviyo.com/"><img title="Klaviyo" src="https://www.attrs.org/en/24.2.0/_static/sponsors/Klaviyo.svg" width="190" /></a>
107
+ <a href="https://filepreviews.io/"><img title="FilePreviews" src="https://www.attrs.org/en/24.2.0/_static/sponsors/FilePreviews.svg" width="190" /></a>
108
+ <!-- [[[end]]] -->
109
+
110
+ </p>
111
+
112
+ <!-- sponsor-break-end -->
113
+
114
+ <p align="center">
115
+ <strong>Please consider <a href="https://github.com/sponsors/hynek">joining them</a> to help make <em>attrs</em>’s maintenance more sustainable!</strong>
116
+ </p>
117
+
118
+ <!-- teaser-end -->
119
+
120
+ ## Example
121
+
122
+ *attrs* gives you a class decorator and a way to declaratively define the attributes on that class:
123
+
124
+ <!-- code-begin -->
125
+
126
+ ```pycon
127
+ >>> from attrs import asdict, define, make_class, Factory
128
+
129
+ >>> @define
130
+ ... class SomeClass:
131
+ ... a_number: int = 42
132
+ ... list_of_numbers: list[int] = Factory(list)
133
+ ...
134
+ ... def hard_math(self, another_number):
135
+ ... return self.a_number + sum(self.list_of_numbers) * another_number
136
+
137
+
138
+ >>> sc = SomeClass(1, [1, 2, 3])
139
+ >>> sc
140
+ SomeClass(a_number=1, list_of_numbers=[1, 2, 3])
141
+
142
+ >>> sc.hard_math(3)
143
+ 19
144
+ >>> sc == SomeClass(1, [1, 2, 3])
145
+ True
146
+ >>> sc != SomeClass(2, [3, 2, 1])
147
+ True
148
+
149
+ >>> asdict(sc)
150
+ {'a_number': 1, 'list_of_numbers': [1, 2, 3]}
151
+
152
+ >>> SomeClass()
153
+ SomeClass(a_number=42, list_of_numbers=[])
154
+
155
+ >>> C = make_class("C", ["a", "b"])
156
+ >>> C("foo", "bar")
157
+ C(a='foo', b='bar')
158
+ ```
159
+
160
+ After *declaring* your attributes, *attrs* gives you:
161
+
162
+ - a concise and explicit overview of the class's attributes,
163
+ - a nice human-readable `__repr__`,
164
+ - equality-checking methods,
165
+ - an initializer,
166
+ - and much more,
167
+
168
+ *without* writing dull boilerplate code again and again and *without* runtime performance penalties.
169
+
170
+ ---
171
+
172
+ This example uses *attrs*'s modern APIs that have been introduced in version 20.1.0, and the *attrs* package import name that has been added in version 21.3.0.
173
+ The classic APIs (`@attr.s`, `attr.ib`, plus their serious-business aliases) and the `attr` package import name will remain **indefinitely**.
174
+
175
+ Check out [*On The Core API Names*](https://www.attrs.org/en/latest/names.html) for an in-depth explanation!
176
+
177
+
178
+ ### Hate Type Annotations!?
179
+
180
+ No problem!
181
+ Types are entirely **optional** with *attrs*.
182
+ Simply assign `attrs.field()` to the attributes instead of annotating them with types:
183
+
184
+ ```python
185
+ from attrs import define, field
186
+
187
+ @define
188
+ class SomeClass:
189
+ a_number = field(default=42)
190
+ list_of_numbers = field(factory=list)
191
+ ```
192
+
193
+
194
+ ## Data Classes
195
+
196
+ On the tin, *attrs* might remind you of `dataclasses` (and indeed, `dataclasses` [are a descendant](https://hynek.me/articles/import-attrs/) of *attrs*).
197
+ In practice it does a lot more and is more flexible.
198
+ For instance, it allows you to define [special handling of NumPy arrays for equality checks](https://www.attrs.org/en/stable/comparison.html#customization), allows more ways to [plug into the initialization process](https://www.attrs.org/en/stable/init.html#hooking-yourself-into-initialization), has a replacement for `__init_subclass__`, and allows for stepping through the generated methods using a debugger.
199
+
200
+ For more details, please refer to our [comparison page](https://www.attrs.org/en/stable/why.html#data-classes), but generally speaking, we are more likely to commit crimes against nature to make things work that one would expect to work, but that are quite complicated in practice.
201
+
202
+
203
+ ## Project Information
204
+
205
+ - [**Changelog**](https://www.attrs.org/en/stable/changelog.html)
206
+ - [**Documentation**](https://www.attrs.org/)
207
+ - [**PyPI**](https://pypi.org/project/attrs/)
208
+ - [**Source Code**](https://github.com/python-attrs/attrs)
209
+ - [**Contributing**](https://github.com/python-attrs/attrs/blob/main/.github/CONTRIBUTING.md)
210
+ - [**Third-party Extensions**](https://github.com/python-attrs/attrs/wiki/Extensions-to-attrs)
211
+ - **Get Help**: use the `python-attrs` tag on [Stack Overflow](https://stackoverflow.com/questions/tagged/python-attrs)
212
+
213
+
214
+ ### *attrs* for Enterprise
215
+
216
+ Available as part of the Tidelift Subscription.
217
+
218
+ The maintainers of *attrs* and thousands of other packages are working with Tidelift to deliver commercial support and maintenance for the open source packages you use to build your applications.
219
+ Save time, reduce risk, and improve code health, while paying the maintainers of the exact packages you use.
220
+ [Learn more](https://tidelift.com/?utm_source=lifter&utm_medium=referral&utm_campaign=hynek).
221
+
222
+ ## Release Information
223
+
224
+ ### Deprecations
225
+
226
+ - Given the amount of warnings raised in the broader ecosystem, we've decided to only soft-deprecate the *hash* argument to `@define` / `@attr.s`.
227
+ Please don't use it in new code, but we don't intend to remove it anymore.
228
+ [#1330](https://github.com/python-attrs/attrs/issues/1330)
229
+
230
+
231
+ ### Changes
232
+
233
+ - `attrs.converters.pipe()` (and its syntactic sugar of passing a list for `attrs.field()`'s / `attr.ib()`'s *converter* argument) works again when passing `attrs.setters.convert` to *on_setattr* (which is default for `attrs.define`).
234
+ [#1328](https://github.com/python-attrs/attrs/issues/1328)
235
+ - Restored support for PEP [649](https://peps.python.org/pep-0649/) / [749](https://peps.python.org/pep-0749/)-implementing Pythons -- currently 3.14-dev.
236
+ [#1329](https://github.com/python-attrs/attrs/issues/1329)
237
+
238
+
239
+
240
+ ---
241
+
242
+ [Full changelog →](https://www.attrs.org/en/stable/changelog.html)
evalkit_tf446/lib/python3.10/site-packages/attrs-24.2.0.dist-info/RECORD ADDED
@@ -0,0 +1,56 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ attr/__init__.py,sha256=l8Ewh5KZE7CCY0i1iDfSCnFiUTIkBVoqsXjX9EZnIVA,2087
2
+ attr/__init__.pyi,sha256=aTVHBPX6krCGvbQvOl_UKqEzmi2HFsaIVm2WKmAiqVs,11434
3
+ attr/__pycache__/__init__.cpython-310.pyc,,
4
+ attr/__pycache__/_cmp.cpython-310.pyc,,
5
+ attr/__pycache__/_compat.cpython-310.pyc,,
6
+ attr/__pycache__/_config.cpython-310.pyc,,
7
+ attr/__pycache__/_funcs.cpython-310.pyc,,
8
+ attr/__pycache__/_make.cpython-310.pyc,,
9
+ attr/__pycache__/_next_gen.cpython-310.pyc,,
10
+ attr/__pycache__/_version_info.cpython-310.pyc,,
11
+ attr/__pycache__/converters.cpython-310.pyc,,
12
+ attr/__pycache__/exceptions.cpython-310.pyc,,
13
+ attr/__pycache__/filters.cpython-310.pyc,,
14
+ attr/__pycache__/setters.cpython-310.pyc,,
15
+ attr/__pycache__/validators.cpython-310.pyc,,
16
+ attr/_cmp.py,sha256=3umHiBtgsEYtvNP_8XrQwTCdFoZIX4DEur76N-2a3X8,4123
17
+ attr/_cmp.pyi,sha256=U-_RU_UZOyPUEQzXE6RMYQQcjkZRY25wTH99sN0s7MM,368
18
+ attr/_compat.py,sha256=n2Uk3c-ywv0PkFfGlvqR7SzDXp4NOhWmNV_ZK6YfWoM,2958
19
+ attr/_config.py,sha256=z81Vt-GeT_2taxs1XZfmHx9TWlSxjPb6eZH1LTGsS54,843
20
+ attr/_funcs.py,sha256=SGDmNlED1TM3tgO9Ap2mfRfVI24XEAcrNQs7o2eBXHQ,17386
21
+ attr/_make.py,sha256=BjENJz5eJoojJVbCoupWjXLLEZJ7VID89lisLbQUlmQ,91479
22
+ attr/_next_gen.py,sha256=dhGb96VFg4kXBkS9Zdz1A2uxVJ99q_RT1hw3kLA9-uI,24630
23
+ attr/_typing_compat.pyi,sha256=XDP54TUn-ZKhD62TOQebmzrwFyomhUCoGRpclb6alRA,469
24
+ attr/_version_info.py,sha256=exSqb3b5E-fMSsgZAlEw9XcLpEgobPORCZpcaEglAM4,2121
25
+ attr/_version_info.pyi,sha256=x_M3L3WuB7r_ULXAWjx959udKQ4HLB8l-hsc1FDGNvk,209
26
+ attr/converters.py,sha256=vNa58pZi9V6uxBzl4t1QrHbQfkT4iRFAodyXe7lcgg0,3506
27
+ attr/converters.pyi,sha256=mpDoVFO3Cpx8xYSSV0iZFl7IAHuoNBglxKfxHvLj_sY,410
28
+ attr/exceptions.py,sha256=HRFq4iybmv7-DcZwyjl6M1euM2YeJVK_hFxuaBGAngI,1977
29
+ attr/exceptions.pyi,sha256=zZq8bCUnKAy9mDtBEw42ZhPhAUIHoTKedDQInJD883M,539
30
+ attr/filters.py,sha256=ZBiKWLp3R0LfCZsq7X11pn9WX8NslS2wXM4jsnLOGc8,1795
31
+ attr/filters.pyi,sha256=3J5BG-dTxltBk1_-RuNRUHrv2qu1v8v4aDNAQ7_mifA,208
32
+ attr/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
33
+ attr/setters.py,sha256=faMQeiBo_nbXYnPaQ1pq8PXeA7Zr-uNsVsPMiKCmxhc,1619
34
+ attr/setters.pyi,sha256=NnVkaFU1BB4JB8E4JuXyrzTUgvtMpj8p3wBdJY7uix4,584
35
+ attr/validators.py,sha256=985eTP6RHyon61YEauMJgyNy1rEOhJWiSXMJgRxPtrQ,20045
36
+ attr/validators.pyi,sha256=LjKf7AoXZfvGSfT3LRs61Qfln94konYyMUPoJJjOxK4,2502
37
+ attrs-24.2.0.dist-info/INSTALLER,sha256=zuuue4knoyJ-UwPPXg8fezS7VCrXJQrAP7zeNuwvFQg,4
38
+ attrs-24.2.0.dist-info/METADATA,sha256=3Jgk4lr9Y1SAqAcwOLPN_mpW0wc6VOGm-yHt1LsPIHw,11524
39
+ attrs-24.2.0.dist-info/RECORD,,
40
+ attrs-24.2.0.dist-info/REQUESTED,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
41
+ attrs-24.2.0.dist-info/WHEEL,sha256=1yFddiXMmvYK7QYTqtRNtX66WJ0Mz8PYEiEUoOUUxRY,87
42
+ attrs-24.2.0.dist-info/licenses/LICENSE,sha256=iCEVyV38KvHutnFPjsbVy8q_Znyv-HKfQkINpj9xTp8,1109
43
+ attrs/__init__.py,sha256=5FHo-EMFOX-g4ialSK4fwOjuoHzLISJDZCwoOl02Ty8,1071
44
+ attrs/__init__.pyi,sha256=o3l92VsD9kHz8sldEtb_tllBTs3TeL-vIBMTxo2Zc_4,7703
45
+ attrs/__pycache__/__init__.cpython-310.pyc,,
46
+ attrs/__pycache__/converters.cpython-310.pyc,,
47
+ attrs/__pycache__/exceptions.cpython-310.pyc,,
48
+ attrs/__pycache__/filters.cpython-310.pyc,,
49
+ attrs/__pycache__/setters.cpython-310.pyc,,
50
+ attrs/__pycache__/validators.cpython-310.pyc,,
51
+ attrs/converters.py,sha256=8kQljrVwfSTRu8INwEk8SI0eGrzmWftsT7rM0EqyohM,76
52
+ attrs/exceptions.py,sha256=ACCCmg19-vDFaDPY9vFl199SPXCQMN_bENs4DALjzms,76
53
+ attrs/filters.py,sha256=VOUMZug9uEU6dUuA0dF1jInUK0PL3fLgP0VBS5d-CDE,73
54
+ attrs/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
55
+ attrs/setters.py,sha256=eL1YidYQV3T2h9_SYIZSZR1FAcHGb1TuCTy0E0Lv2SU,73
56
+ attrs/validators.py,sha256=xcy6wD5TtTkdCG1f4XWbocPSO0faBjk5IfVJfP6SUj0,76
evalkit_tf446/lib/python3.10/site-packages/attrs-24.2.0.dist-info/REQUESTED ADDED
File without changes
evalkit_tf446/lib/python3.10/site-packages/attrs-24.2.0.dist-info/WHEEL ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ Wheel-Version: 1.0
2
+ Generator: hatchling 1.25.0
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
evalkit_tf446/lib/python3.10/site-packages/attrs-24.2.0.dist-info/licenses/LICENSE ADDED
@@ -0,0 +1,21 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Hynek Schlawack and the attrs contributors
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
evalkit_tf446/lib/python3.10/site-packages/ezcolorlog/__init__.py ADDED
@@ -0,0 +1,63 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Easy Logging
3
+
4
+ - if in notebook, use a simple colored logger
5
+ - if in terminal, use a more complex colored logger
6
+ """
7
+
8
+ __version__ = "1.0.3"
9
+
10
+
11
+ import logging
12
+ import sys
13
+
14
+ import coloredlogs
15
+
16
+ from .format import get_log_fmt
17
+ from .patch import monkeypatched, patch_tqdm
18
+ from .logfile import setup_logfile_handlers
19
+ from .notebook import isnotebook, ColoredFormatter
20
+ from .wrapper import log_stdout
21
+ from .config import load_cfg
22
+
23
+
24
+ def setup_logging(logger=logging.root, cfg_file=None):
25
+ """
26
+ Setup logging for the given logger.
27
+
28
+ :param logger: the logger to setup
29
+ :param cfg_file: the path to the config file containing default overrides
30
+ """
31
+ user, fmts, style = load_cfg(cfg_file)
32
+ log_fmt = user.get("log_fmt", "standard")
33
+
34
+ if isnotebook() or log_fmt == "notebook":
35
+ handler = logging.StreamHandler(sys.stdout)
36
+ handler.setFormatter(ColoredFormatter())
37
+ logger.handlers[:] = []
38
+ logger.addHandler(handler)
39
+ logger.setLevel(logging.INFO)
40
+ logger.info("Notebook logger initialized.")
41
+ else:
42
+ # display pretty colored logs to console at the info level
43
+ clfmt = get_log_fmt(log_fmt, fmts=fmts)
44
+
45
+ coloredlogs.install(
46
+ logger=logger, level=logging.INFO, fmt=clfmt, field_styles=style
47
+ )
48
+ logger.setLevel(logging.DEBUG)
49
+ logger.info(f"'{log_fmt}' logger initialized.")
50
+
51
+
52
+ # logging is setup for the root logger by default
53
+ root_logger = logging.root
54
+ setup_logging(root_logger)
55
+
56
+
57
+ __all__ = (
58
+ "logger",
59
+ "patch_tqdm",
60
+ "monkeypatched",
61
+ "setup_logfile_handlers",
62
+ "log_stdout",
63
+ )
evalkit_tf446/lib/python3.10/site-packages/ezcolorlog/__pycache__/__init__.cpython-310.pyc ADDED
Binary file (1.57 kB). View file
 
evalkit_tf446/lib/python3.10/site-packages/ezcolorlog/__pycache__/config.cpython-310.pyc ADDED
Binary file (946 Bytes). View file
 
evalkit_tf446/lib/python3.10/site-packages/ezcolorlog/__pycache__/format.cpython-310.pyc ADDED
Binary file (966 Bytes). View file
 
evalkit_tf446/lib/python3.10/site-packages/ezcolorlog/__pycache__/logfile.cpython-310.pyc ADDED
Binary file (1.84 kB). View file
 
evalkit_tf446/lib/python3.10/site-packages/ezcolorlog/__pycache__/notebook.cpython-310.pyc ADDED
Binary file (1.8 kB). View file
 
evalkit_tf446/lib/python3.10/site-packages/ezcolorlog/__pycache__/patch.cpython-310.pyc ADDED
Binary file (1.16 kB). View file
 
evalkit_tf446/lib/python3.10/site-packages/ezcolorlog/__pycache__/wrapper.cpython-310.pyc ADDED
Binary file (3.71 kB). View file
 
evalkit_tf446/lib/python3.10/site-packages/ezcolorlog/config.py ADDED
@@ -0,0 +1,32 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os.path as osp
2
+ import yaml
3
+
4
+
5
+ ROOT = osp.dirname(osp.abspath(__file__))
6
+ CFG_FILE = osp.join(ROOT, "defaults.yaml")
7
+
8
+
9
+ def load_cfg(cfg_file=None):
10
+ """
11
+ Load the config file.
12
+
13
+ :param cfg_file: the path to the config file containing default overrides
14
+ :return: the config dictionary
15
+ """
16
+ # load the default config file
17
+ with open(CFG_FILE) as f:
18
+ cfg = yaml.load(f, Loader=yaml.FullLoader)
19
+
20
+ # load the config file if it exists
21
+ if cfg_file is not None:
22
+ if not osp.exists(cfg_file):
23
+ raise ValueError(f"Config file '{cfg_file}' does not exist.")
24
+ with open(cfg_file) as f:
25
+ cfg.update(yaml.load(f, Loader=yaml.FullLoader))
26
+
27
+ # user, format, style
28
+ user = cfg.get("user")
29
+ fmts = cfg.get("format")
30
+ style = cfg.get("style")
31
+
32
+ return user, fmts, style
evalkit_tf446/lib/python3.10/site-packages/ezcolorlog/defaults.yaml ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ user:
2
+ log_fmt: standard
3
+
4
+ format:
5
+ simple: "%(asctime)s %(filename)s:%(lineno)s [%(levelname).1s] → %(message)s"
6
+ standard: "%(asctime)s PID=%(process)d %(filename)s:%(lineno)s %(funcName)s() %(levelname)s → %(message)s"
7
+ verbose: "%(asctime)s %(hostname)s [PID=%(process)d] %(filename)s:%(lineno)s %(funcName)s() %(levelname)s: %(message)s"
8
+ logfile: "%(asctime)s,PID=%(process)d,%(filename)s:%(lineno)s,%(funcName)s(),%(levelname)s,%(message)s"
9
+ notebook: "{asctime} {color}{filename}:{lineno} [{levelname:.1s}]{reset} → {message}"
10
+
11
+
12
+ style:
13
+ asctime:
14
+ color: green
15
+ hostname:
16
+ color: magenta
17
+ levelname:
18
+ color: black
19
+ bold: true
20
+ name:
21
+ color: blue
22
+ message:
23
+ color: white
24
+ programname:
25
+ color: cyan
26
+ username:
27
+ color: yellow
28
+ filename:
29
+ color: blue
30
+ bold: true
31
+ funcName:
32
+ color: blue
33
+ process:
34
+ color: yellow
35
+ faint: true
evalkit_tf446/lib/python3.10/site-packages/ezcolorlog/format.py ADDED
@@ -0,0 +1,31 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from warnings import warn
2
+
3
+ from .config import load_cfg
4
+
5
+ # load the default config file
6
+ _, defaults, _ = load_cfg()
7
+
8
+
9
+ def get_log_fmt(log_fmt: str, fmts: dict = dict()) -> str:
10
+ """
11
+ Return a string to be used as a format string for logging.
12
+
13
+ :param log_fmt: the name of the format to use
14
+ :param fmts: a dictionary of format string overrides
15
+ :return: a string to be used as a format string for logging
16
+ """
17
+
18
+ # override the defaults with the passed in formats
19
+ defaults.update(fmts)
20
+ fmts = defaults
21
+
22
+ assert "standard" in fmts, "No 'standard' format found in config file."
23
+
24
+ if log_fmt.lower() in fmts:
25
+ return fmts[log_fmt]
26
+ else:
27
+ warn(
28
+ f"Invalid log style: {log_fmt}. Check your config.\n"
29
+ f"Valid styles: {list(fmts.keys())}\nDefaulting to 'standard'."
30
+ )
31
+ return get_log_fmt("standard")
evalkit_tf446/lib/python3.10/site-packages/ezcolorlog/logfile.py ADDED
@@ -0,0 +1,66 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Logfile Handling
3
+
4
+ TODO: could compress logfiles after rollover
5
+ - https://stackoverflow.com/a/53288524/5487412
6
+ TODO: logfile rotation is not working correctly, rotating WAY too often...
7
+ - debug logs are <100KB ... X 100?
8
+ """
9
+
10
+ import logging
11
+ import os
12
+ import os.path as osp
13
+ from typing import List, Union
14
+
15
+ from .format import get_log_fmt
16
+
17
+ root_logger = logging.root
18
+
19
+
20
+ def _add_logfile_handler(
21
+ logdir: str,
22
+ logger: logging.Logger = root_logger,
23
+ level: Union[int, str] = logging.INFO,
24
+ ):
25
+ """Add a file handler to the logger.
26
+
27
+ :param logger: the logging.Logger to add the handler to
28
+ :param logdir: the directory in which to save the log file
29
+ :param level: the logging level
30
+ """
31
+
32
+ level_int: int = logging._checkLevel(level)
33
+ level_name: str = logging.getLevelName(level_int)
34
+
35
+ # create log folder if it doesn't exist
36
+ os.makedirs(logdir, exist_ok=True)
37
+ logfile = osp.join(logdir, f"{level_name}.log")
38
+ # use rotating file handler to save logs => max 10MB per file
39
+ # fh = RotatingFileHandler(logfile, maxBytes=1e7, backupCount=1000)
40
+ # check if file exists, if so, mark as version 2
41
+ for i in range(1, 1000): # HACK: come up with a better solution
42
+ if not osp.exists(logfile):
43
+ break
44
+ logfile = osp.join(logdir, f"{level_name}_{i}.log")
45
+ fh = logging.FileHandler(logfile)
46
+ fh.setLevel(level_int)
47
+ fmt = get_log_fmt("logfile")
48
+ formatter = logging.Formatter(fmt, "%Y-%m-%d %H:%M:%S")
49
+ fh.setFormatter(formatter)
50
+ logger.addHandler(fh)
51
+ root_logger.info(
52
+ f"Logfile handler (level {level_name}) added to "
53
+ f"{logger.name} logger at '{logfile}'."
54
+ )
55
+
56
+
57
+ def setup_logfile_handlers(
58
+ logdir: str,
59
+ logger: logging.Logger = root_logger,
60
+ levels: List[Union[int, str]] = ["INFO", "DEBUG"],
61
+ ):
62
+ for level in levels:
63
+ _add_logfile_handler(logdir, logger, level)
64
+
65
+
66
+ __all__ = ("setup_logfile_handlers",)
evalkit_tf446/lib/python3.10/site-packages/ezcolorlog/notebook.py ADDED
@@ -0,0 +1,64 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import logging
2
+
3
+ from colorama import Back, Fore, Style
4
+
5
+ from .format import get_log_fmt
6
+
7
+
8
+ def isnotebook() -> bool:
9
+ # https://stackoverflow.com/a/39662359/5487412
10
+ try:
11
+ from IPython import get_ipython
12
+
13
+ shell = get_ipython().__class__.__name__
14
+ if shell == "ZMQInteractiveShell":
15
+ return True # Jupyter notebook or qtconsole
16
+ elif shell == "TerminalInteractiveShell":
17
+ return False # Terminal running IPython
18
+ else:
19
+ return False # Other type (?)
20
+ except NameError:
21
+ return False # Probably standard Python interpreter
22
+
23
+
24
+ class ColoredFormatter(logging.Formatter):
25
+ """Colored log formatter.
26
+
27
+ https://gist.github.com/joshbode/58fac7ababc700f51e2a9ecdebe563ad
28
+ """
29
+
30
+ DEFAULT_COLORS = {
31
+ "DEBUG": Fore.CYAN,
32
+ "INFO": Fore.GREEN,
33
+ "WARNING": Fore.YELLOW,
34
+ "ERROR": Fore.RED,
35
+ "CRITICAL": Fore.RED + Back.WHITE + Style.BRIGHT,
36
+ }
37
+ DEFAULT_FMT = get_log_fmt("notebook")
38
+ DEFAULT_DATEFMT = "%H:%M:%S"
39
+
40
+ def __init__(
41
+ self,
42
+ fmt=DEFAULT_FMT,
43
+ datefmt=DEFAULT_DATEFMT,
44
+ style="{",
45
+ validate=True,
46
+ colors=DEFAULT_COLORS,
47
+ ):
48
+ """Initialize the formatter with specified format strings."""
49
+ super().__init__(fmt, datefmt, style, validate)
50
+ self.colors = colors if colors else {}
51
+
52
+ def format(self, record) -> str:
53
+ """Format the specified record as text."""
54
+
55
+ record.color = self.colors.get(record.levelname, "")
56
+ record.reset = Style.RESET_ALL
57
+
58
+ return super().format(record)
59
+
60
+
61
+ __all__ = (
62
+ "ColoredFormatter",
63
+ "isnotebook",
64
+ )
evalkit_tf446/lib/python3.10/site-packages/ezcolorlog/patch.py ADDED
@@ -0,0 +1,42 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ TQDM Monkey Patch
3
+
4
+ this allows us to pass in arguments to tqdm for subpackages that use tqdm
5
+ e.g., torchvision.datasets
6
+
7
+
8
+ inspiration: https://github.com/tqdm/tqdm/issues/614#issuecomment-939321390
9
+ """
10
+
11
+ import contextlib
12
+ from functools import partialmethod
13
+
14
+
15
+ @contextlib.contextmanager
16
+ def monkeypatched(obj, name, patch):
17
+ """Temporarily monkeypatch."""
18
+ old_attr = getattr(obj, name)
19
+ setattr(obj, name, patch(old_attr))
20
+ try:
21
+ yield
22
+ finally:
23
+ setattr(obj, name, old_attr)
24
+
25
+
26
+ @contextlib.contextmanager
27
+ def patch_tqdm(**kwargs):
28
+ """Context manager to disable tqdm."""
29
+
30
+ def _patch(old_init):
31
+ return partialmethod(old_init, **kwargs)
32
+
33
+ import tqdm
34
+
35
+ with monkeypatched(tqdm.std.tqdm, "__init__", _patch):
36
+ yield
37
+
38
+
39
+ __all__ = (
40
+ "monkeypatched",
41
+ "patch_tqdm",
42
+ )
evalkit_tf446/lib/python3.10/site-packages/ezcolorlog/wrapper.py ADDED
@@ -0,0 +1,115 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Wrapper to redirect stdout to logger
3
+
4
+ This allows us to simply add the `@log_stdout` decorator to any function and
5
+ have all of its print statements redirected to the logger.
6
+ """
7
+
8
+
9
+ import contextlib
10
+ import io
11
+ import logging
12
+ import os
13
+ import platform
14
+ import traceback
15
+ from functools import partial, wraps
16
+ from inspect import currentframe
17
+ from typing import Union
18
+
19
+ root_logger = logging.root
20
+
21
+
22
+ def log_stdout(
23
+ func=None,
24
+ *,
25
+ stacklevel: int = 4,
26
+ level: Union[str, int] = "INFO",
27
+ logger: logging.Logger = root_logger,
28
+ ):
29
+ """Function decorator to redirect print statements to logger."""
30
+ # https://pybit.es/articles/decorator-optional-argument/
31
+ if func is None:
32
+ return partial(log_stdout, stacklevel=stacklevel, level=level, logger=logger)
33
+
34
+ @wraps(func)
35
+ def wrapper(*args, **kwargs):
36
+ # # also redirect console logging to tqdm.write -> prints logs above bars
37
+ # with logging_redirect_tqdm(loggers=[logger]):
38
+ with contextlib.redirect_stdout(
39
+ StdOutLogger(logger=logger, stacklevel=stacklevel, level=level)
40
+ ):
41
+ return func(*args, **kwargs)
42
+
43
+ return wrapper
44
+
45
+
46
+ class StdOutLogger:
47
+ def __init__(
48
+ self,
49
+ logger: logging.Logger = root_logger,
50
+ stacklevel: int = 4,
51
+ level: Union[int, str] = logging.INFO,
52
+ ):
53
+ self.logger = logger
54
+ self.stacklevel = stacklevel
55
+ self.level = logging._checkLevel(level)
56
+
57
+ def write(self, msg):
58
+ if platform.sys.version_info.minor >= 8:
59
+ return self._write_38(msg)
60
+ return self._write_37(msg)
61
+
62
+ def _log(self, msg, **kwargs):
63
+ if msg and not msg.isspace():
64
+ self.logger.log(self.level, msg, **kwargs)
65
+
66
+ def _write_37(self, msg):
67
+ caller = self.logger.findCaller
68
+ self.logger.findCaller = partial(
69
+ self.findCallerStackLevel, stacklevel=self.stacklevel
70
+ )
71
+ self._log(msg)
72
+ self.logger.findCaller = caller
73
+
74
+ def _write_38(self, msg):
75
+ self._log(msg, stacklevel=self.stacklevel)
76
+
77
+ def flush(self):
78
+ pass
79
+
80
+ def isatty(self):
81
+ pass
82
+
83
+ @staticmethod
84
+ def findCallerStackLevel(stack_info=False, stacklevel: int = 4):
85
+ """[Hack for Python < 3.8]
86
+ Find the stack frame of the caller so that we can note the source
87
+ file name, line number and function name.
88
+
89
+ https://stackoverflow.com/a/13591151/5487412
90
+ """
91
+ f = currentframe()
92
+ # On some versions of IronPython, currentframe() returns None if
93
+ # IronPython isn't run with -X:Frames.
94
+ if f is not None:
95
+ f = f.f_back
96
+ rv = "(unknown file)", 0, "(unknown function)", None
97
+ while hasattr(f, "f_code") and (stacklevel > 0):
98
+ co = f.f_code
99
+ filename = os.path.normcase(co.co_filename)
100
+ if filename == logging._srcfile or stacklevel > 1:
101
+ f = f.f_back
102
+ stacklevel -= 1
103
+ continue
104
+ sinfo = None
105
+ if stack_info:
106
+ sio = io.StringIO()
107
+ sio.write("Stack (most recent call last):\n")
108
+ traceback.print_stack(f, file=sio)
109
+ sinfo = sio.getvalue()
110
+ if sinfo[-1] == "\n":
111
+ sinfo = sinfo[:-1]
112
+ sio.close()
113
+ rv = (co.co_filename, f.f_lineno, co.co_name, sinfo)
114
+ break
115
+ return rv
evalkit_tf446/lib/python3.10/site-packages/flash_attn/__init__.py ADDED
@@ -0,0 +1,11 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
+ __version__ = "2.7.2.post1"
2
+
3
+ from flash_attn.flash_attn_interface import (
4
+ flash_attn_func,
5
+ flash_attn_kvpacked_func,
6
+ flash_attn_qkvpacked_func,
7
+ flash_attn_varlen_func,
8
+ flash_attn_varlen_kvpacked_func,
9
+ flash_attn_varlen_qkvpacked_func,
10
+ flash_attn_with_kvcache,
11
+ )
evalkit_tf446/lib/python3.10/site-packages/flash_attn/bert_padding.py ADDED
@@ -0,0 +1,218 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Adapted from https://github.com/mlcommons/training_results_v1.1/blob/main/NVIDIA/benchmarks/bert/implementations/pytorch/padding.py
2
+
3
+ import torch
4
+ import torch.nn.functional as F
5
+ from einops import rearrange, repeat
6
+
7
+
8
+ class IndexFirstAxis(torch.autograd.Function):
9
+ @staticmethod
10
+ def forward(ctx, input, indices):
11
+ ctx.save_for_backward(indices)
12
+ assert input.ndim >= 2
13
+ ctx.first_axis_dim, other_shape = input.shape[0], input.shape[1:]
14
+ second_dim = other_shape.numel()
15
+ # TD [2022-03-04] For some reason torch.gather is a bit faster than indexing.
16
+ # return input[indices]
17
+ return torch.gather(
18
+ rearrange(input, "b ... -> b (...)"), 0, repeat(indices, "z -> z d", d=second_dim)
19
+ ).reshape(-1, *other_shape)
20
+
21
+ @staticmethod
22
+ def backward(ctx, grad_output):
23
+ (indices,) = ctx.saved_tensors
24
+ assert grad_output.ndim >= 2
25
+ other_shape = grad_output.shape[1:]
26
+ grad_output = rearrange(grad_output, "b ... -> b (...)")
27
+ grad_input = torch.zeros(
28
+ [ctx.first_axis_dim, grad_output.shape[1]],
29
+ device=grad_output.device,
30
+ dtype=grad_output.dtype,
31
+ )
32
+ # TD [2022-03-04] For some reason torch.scatter is a bit faster than indexing.
33
+ # grad_input[indices] = grad_output
34
+ grad_input.scatter_(0, repeat(indices, "z -> z d", d=grad_output.shape[1]), grad_output)
35
+ return grad_input.reshape(ctx.first_axis_dim, *other_shape), None
36
+
37
+
38
+ index_first_axis = IndexFirstAxis.apply
39
+
40
+
41
+ class IndexPutFirstAxis(torch.autograd.Function):
42
+ @staticmethod
43
+ def forward(ctx, values, indices, first_axis_dim):
44
+ ctx.save_for_backward(indices)
45
+ assert indices.ndim == 1
46
+ assert values.ndim >= 2
47
+ output = torch.zeros(
48
+ first_axis_dim, *values.shape[1:], device=values.device, dtype=values.dtype
49
+ )
50
+ # TD [2022-03-04] For some reason torch.scatter is a bit faster than indexing.
51
+ output[indices] = values
52
+ # output.scatter_(0, repeat(indices, 'z -> z d', d=values.shape[1]), values)
53
+ return output
54
+
55
+ @staticmethod
56
+ def backward(ctx, grad_output):
57
+ (indices,) = ctx.saved_tensors
58
+ # TD [2022-03-04] For some reason torch.gather is a bit faster than indexing.
59
+ grad_values = grad_output[indices]
60
+ # grad_values = torch.gather(grad_output, 0, repeat(indices, 'z -> z d', d=grad_output.shape[1]))
61
+ return grad_values, None, None
62
+
63
+
64
+ index_put_first_axis = IndexPutFirstAxis.apply
65
+
66
+
67
+ class IndexFirstAxisResidual(torch.autograd.Function):
68
+ @staticmethod
69
+ def forward(ctx, input, indices):
70
+ ctx.save_for_backward(indices)
71
+ assert input.ndim >= 2
72
+ ctx.first_axis_dim, other_shape = input.shape[0], input.shape[1:]
73
+ second_dim = other_shape.numel()
74
+ # TD [2022-03-04] For some reason torch.gather is a bit faster than indexing.
75
+ output = input[indices]
76
+ # We don't want to reshape input (b ... -> b (...)) since it could change the channel_last
77
+ # memory format to channel_first. In other words, input might not be contiguous.
78
+ # If we don't detach, Pytorch complains about output being a view and is being modified inplace
79
+ return output, input.detach()
80
+
81
+ @staticmethod
82
+ def backward(ctx, grad_output, grad_residual):
83
+ (indices,) = ctx.saved_tensors
84
+ assert grad_output.ndim >= 2
85
+ other_shape = grad_output.shape[1:]
86
+ assert grad_residual.shape[1:] == other_shape
87
+ grad_input = grad_residual
88
+ # grad_input[indices] += grad_output
89
+ indices = indices.reshape(indices.shape[0], *((1,) * (grad_output.ndim - 1)))
90
+ indices = indices.expand_as(grad_output)
91
+ grad_input.scatter_add_(0, indices, grad_output)
92
+ return grad_input.reshape(ctx.first_axis_dim, *other_shape), None
93
+
94
+
95
+ index_first_axis_residual = IndexFirstAxisResidual.apply
96
+
97
+
98
+ def unpad_input(hidden_states, attention_mask, unused_mask=None):
99
+ """
100
+ Arguments:
101
+ hidden_states: (batch, seqlen, ...)
102
+ attention_mask: (batch, seqlen), bool / int, 1 means valid and 0 means not valid.
103
+ unused_mask: (batch, seqlen), bool / int, 1 means the element is allocated but unused.
104
+ Return:
105
+ hidden_states: (total_nnz, ...), where total_nnz = number of tokens selected in attention_mask + unused_mask.
106
+ indices: (total_nnz), the indices of masked tokens from the flattened input sequence.
107
+ cu_seqlens: (batch + 1), the cumulative sequence lengths, used to index into hidden_states.
108
+ max_seqlen_in_batch: int
109
+ seqused: (batch), returns the number of tokens selected in attention_mask + unused_mask.
110
+ """
111
+ all_masks = (attention_mask + unused_mask) if unused_mask is not None else attention_mask
112
+ seqlens_in_batch = all_masks.sum(dim=-1, dtype=torch.int32)
113
+ used_seqlens_in_batch = attention_mask.sum(dim=-1, dtype=torch.int32)
114
+ indices = torch.nonzero(all_masks.flatten(), as_tuple=False).flatten()
115
+ max_seqlen_in_batch = seqlens_in_batch.max().item()
116
+ cu_seqlens = F.pad(torch.cumsum(seqlens_in_batch, dim=0, dtype=torch.int32), (1, 0))
117
+ # TD [2022-03-04] We don't want to index with a bool mask, because Pytorch will expand the
118
+ # bool mask, then call nonzero to get the indices, then index with those. The indices is @dim
119
+ # times larger than it needs to be, wasting memory. It's faster and more memory-efficient to
120
+ # index with integer indices. Moreover, torch's index is a bit slower than it needs to be,
121
+ # so we write custom forward and backward to make it a bit faster.
122
+ return (
123
+ index_first_axis(rearrange(hidden_states, "b s ... -> (b s) ..."), indices),
124
+ indices,
125
+ cu_seqlens,
126
+ max_seqlen_in_batch,
127
+ used_seqlens_in_batch,
128
+ )
129
+
130
+
131
+ def unpad_input_for_concatenated_sequences(hidden_states, attention_mask_in_length):
132
+ """
133
+ Supports concatenating short samples in one sequence. The attention_mask_in_length is utilized to mask other short samples. It helps efficient training of variant lengths-based samples (e.g., the supervised fine-tuning task in large language model).
134
+ The motivation for this function is explained [here](https://github.com/Dao-AILab/flash-attention/issues/432#issuecomment-1668822286).
135
+
136
+ For example, if batch = 3 and seqlen = 6, the attention_mask_in_length is:
137
+ ```
138
+ [
139
+ [2, 3, 0, 0, 0, 0],
140
+ [3, 2, 0, 0, 0, 0],
141
+ [6, 0, 0, 0, 0, 0]
142
+ ]
143
+ ```
144
+ , which refers to the 3D-attention mask:
145
+ ```
146
+ [
147
+ [
148
+ [1, 0, 0, 0, 0, 0],
149
+ [1, 1, 0, 0, 0, 0],
150
+ [0, 0, 1, 0, 0, 0],
151
+ [0, 0, 1, 1, 0, 0],
152
+ [0, 0, 1, 1, 1, 0],
153
+ [0, 0, 0, 0, 0, 1]
154
+ ],
155
+ [
156
+ [1, 0, 0, 0, 0, 0],
157
+ [1, 1, 0, 0, 0, 0],
158
+ [1, 1, 1, 0, 0, 0],
159
+ [0, 0, 0, 1, 0, 0],
160
+ [0, 0, 0, 1, 1, 0],
161
+ [0, 0, 0, 0, 0, 1]
162
+ ],
163
+ [
164
+ [1, 0, 0, 0, 0, 0],
165
+ [1, 1, 0, 0, 0, 0],
166
+ [1, 1, 1, 0, 0, 0],
167
+ [1, 1, 1, 1, 0, 0],
168
+ [1, 1, 1, 1, 1, 0],
169
+ [1, 1, 1, 1, 1, 1]
170
+ ]
171
+ ]
172
+ ```.
173
+
174
+ Arguments:
175
+ hidden_states: (batch, seqlen, ...)
176
+ attention_mask_in_length: (batch, seqlen), int, a nonzero number (e.g., 1, 2, 3, etc.) means length of concatenated sequence in b-th batch, and 0 means none.
177
+ Return:
178
+ hidden_states: (total_nnz, ...), where total_nnz = number of tokens in selected in attention_mask.
179
+ indices: (total_nnz), the indices of non-masked tokens from the flattened input sequence.
180
+ cu_seqlens: (batch + 1), the cumulative sequence lengths, used to index into hidden_states.
181
+ max_seqlen_in_batch: int
182
+ """
183
+ length = attention_mask_in_length.sum(dim=-1)
184
+ seqlen = attention_mask_in_length.size(-1)
185
+ attention_mask_2d = torch.arange(seqlen, device=length.device, dtype=length.dtype).expand(len(length), seqlen) < length.unsqueeze(1)
186
+ real_indices_idx = torch.nonzero(attention_mask_in_length.flatten(), as_tuple=False).flatten()
187
+ seqlens_in_batch = attention_mask_in_length.flatten()[real_indices_idx]
188
+ indices = torch.nonzero(attention_mask_2d.flatten(), as_tuple=False).flatten()
189
+ max_seqlen_in_batch = seqlens_in_batch.max().item()
190
+ cu_seqlens = F.pad(torch.cumsum(seqlens_in_batch, dim=0, dtype=torch.int32), (1, 0))
191
+ # TD [2022-03-04] We don't want to index with a bool mask, because Pytorch will expand the
192
+ # bool mask, then call nonzero to get the indices, then index with those. The indices is @dim
193
+ # times larger than it needs to be, wasting memory. It's faster and more memory-efficient to
194
+ # index with integer indices. Moreover, torch's index is a bit slower than it needs to be,
195
+ # so we write custom forward and backward to make it a bit faster.
196
+ return (
197
+ index_first_axis(rearrange(hidden_states, "b s ... -> (b s) ..."), indices),
198
+ indices,
199
+ cu_seqlens,
200
+ max_seqlen_in_batch,
201
+ )
202
+
203
+
204
+ def pad_input(hidden_states, indices, batch, seqlen):
205
+ """
206
+ Arguments:
207
+ hidden_states: (total_nnz, ...), where total_nnz = number of tokens in selected in attention_mask.
208
+ indices: (total_nnz), the indices that represent the non-masked tokens of the original padded input sequence.
209
+ batch: int, batch size for the padded sequence.
210
+ seqlen: int, maximum sequence length for the padded sequence.
211
+ Return:
212
+ hidden_states: (batch, seqlen, ...)
213
+ """
214
+ dim = hidden_states.shape[-1]
215
+ # output = torch.zeros((batch * seqlen), dim, device=hidden_states.device, dtype=hidden_states.dtype)
216
+ # output[indices] = hidden_states
217
+ output = index_put_first_axis(hidden_states, indices, batch * seqlen)
218
+ return rearrange(output, "(b s) ... -> b s ...", b=batch)
evalkit_tf446/lib/python3.10/site-packages/flash_attn/flash_attn_interface.py ADDED
@@ -0,0 +1,1574 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Copyright (c) 2023, Tri Dao.
2
+
3
+ from typing import Optional, Sequence, Tuple, Union
4
+
5
+ import torch
6
+ import torch.nn as nn
7
+ import os
8
+
9
+ # isort: off
10
+ # We need to import the CUDA kernels after importing torch
11
+ USE_TRITON_ROCM = os.getenv("FLASH_ATTENTION_TRITON_AMD_ENABLE", "FALSE") == "TRUE"
12
+ if USE_TRITON_ROCM:
13
+ from .flash_attn_triton_amd import interface_fa as flash_attn_gpu
14
+ else:
15
+ import flash_attn_2_cuda as flash_attn_gpu
16
+
17
+ # isort: on
18
+
19
+ def maybe_contiguous(x):
20
+ return x.contiguous() if x is not None and x.stride(-1) != 1 else x
21
+
22
+
23
+ def _get_block_size_n(device, head_dim, is_dropout, is_causal):
24
+ # This should match the block sizes in the CUDA kernel
25
+ assert head_dim <= 256
26
+ major, minor = torch.cuda.get_device_capability(device)
27
+ is_sm8x = major == 8 and minor > 0 # Only include sm86 and sm89, exclude sm80 (A100)
28
+ is_sm80 = major == 8 and minor == 0
29
+ is_sm90 = major == 9 and minor == 0
30
+ if head_dim <= 32:
31
+ return 128
32
+ if head_dim <= 64:
33
+ return 128 if not is_dropout else 64
34
+ elif head_dim <= 96:
35
+ return 64
36
+ elif head_dim <= 128:
37
+ if is_sm8x:
38
+ return 64 if (not is_dropout and is_causal) else 32
39
+ else:
40
+ return 64 if not is_dropout else 32
41
+ elif head_dim <= 160:
42
+ if is_sm8x:
43
+ return 64
44
+ else:
45
+ return 32
46
+ elif head_dim <= 192:
47
+ return 64
48
+ elif head_dim <= 224:
49
+ return 64
50
+ elif head_dim <= 256:
51
+ return 64
52
+
53
+
54
+ def round_multiple(x, m):
55
+ return (x + m - 1) // m * m
56
+
57
+
58
+ # torch.compile() support is only enabled for pytorch >= 2.4
59
+ # The reason for this is that we are using the new custom_op and register_fake
60
+ # APIs, which support inplace modification of inputs in the function itself
61
+ if torch.__version__ >= "2.4.0":
62
+ _torch_custom_op_wrapper = torch.library.custom_op
63
+ _torch_register_fake_wrapper = torch.library.register_fake
64
+ else:
65
+ def noop_custom_op_wrapper(name, fn=None, /, *, mutates_args, device_types=None, schema=None):
66
+ def wrap(func):
67
+ return func
68
+ if fn is None:
69
+ return wrap
70
+ return fn
71
+ def noop_register_fake_wrapper(op, fn=None, /, *, lib=None, _stacklevel=1):
72
+ def wrap(func):
73
+ return func
74
+ if fn is None:
75
+ return wrap
76
+ return fn
77
+ _torch_custom_op_wrapper = noop_custom_op_wrapper
78
+ _torch_register_fake_wrapper = noop_register_fake_wrapper
79
+
80
+
81
+ @_torch_custom_op_wrapper("flash_attn::_flash_attn_forward", mutates_args=(), device_types="cuda")
82
+ def _flash_attn_forward(
83
+ q: torch.Tensor,
84
+ k: torch.Tensor,
85
+ v: torch.Tensor,
86
+ dropout_p: float,
87
+ softmax_scale: float,
88
+ causal: bool,
89
+ window_size_left: int,
90
+ window_size_right: int,
91
+ softcap: float,
92
+ alibi_slopes: Optional[torch.Tensor],
93
+ return_softmax: bool
94
+ ) -> Tuple[torch.Tensor, torch.Tensor, torch.Tensor, torch.Tensor]:
95
+ q, k, v = [maybe_contiguous(x) for x in (q, k, v)]
96
+ out, softmax_lse, S_dmask, rng_state = flash_attn_gpu.fwd(
97
+ q,
98
+ k,
99
+ v,
100
+ None,
101
+ alibi_slopes,
102
+ dropout_p,
103
+ softmax_scale,
104
+ causal,
105
+ window_size_left,
106
+ window_size_right,
107
+ softcap,
108
+ return_softmax,
109
+ None,
110
+ )
111
+ return out, softmax_lse, S_dmask, rng_state
112
+
113
+
114
+ @_torch_register_fake_wrapper("flash_attn::_flash_attn_forward")
115
+ def _flash_attn_forward_fake(
116
+ q: torch.Tensor,
117
+ k: torch.Tensor,
118
+ v: torch.Tensor,
119
+ dropout_p: float,
120
+ softmax_scale: float,
121
+ causal: bool,
122
+ window_size_left: int,
123
+ window_size_right: int,
124
+ softcap: float,
125
+ alibi_slopes: Optional[torch.Tensor],
126
+ return_softmax: bool
127
+ ) -> Tuple[torch.Tensor, torch.Tensor, torch.Tensor, torch.Tensor]:
128
+ q, k, v = [maybe_contiguous(x) for x in (q, k, v)]
129
+ batch_size, seqlen_q, num_heads, head_size = q.shape
130
+ seqlen_k = k.shape[1]
131
+ out = torch.empty_like(q)
132
+ softmax_lse = torch.empty((batch_size, num_heads, seqlen_q), dtype=torch.float32, device=q.device, layout=q.layout)
133
+ p = torch.empty((0,), dtype=q.dtype, device=q.device, layout=q.layout)
134
+ if return_softmax:
135
+ p = torch.empty((batch_size, num_heads, round_multiple(seqlen_q, 128), round_multiple(seqlen_k, 128)), dtype=q.dtype, device=q.device, layout=q.layout)
136
+ rng_state = torch.empty((2,), dtype=torch.int64, device=q.device)
137
+
138
+ return out, softmax_lse, p, rng_state
139
+
140
+
141
+ if torch.__version__ >= "2.4.0":
142
+ _wrapped_flash_attn_forward = torch.ops.flash_attn._flash_attn_forward
143
+ else:
144
+ _wrapped_flash_attn_forward = _flash_attn_forward
145
+
146
+
147
+ @_torch_custom_op_wrapper("flash_attn::_flash_attn_varlen_forward", mutates_args=(), device_types="cuda")
148
+ def _flash_attn_varlen_forward(
149
+ q: torch.Tensor,
150
+ k: torch.Tensor,
151
+ v: torch.Tensor,
152
+ cu_seqlens_q: torch.Tensor,
153
+ cu_seqlens_k: torch.Tensor,
154
+ max_seqlen_q: int,
155
+ max_seqlen_k: int,
156
+ dropout_p: float,
157
+ softmax_scale: float,
158
+ causal: bool,
159
+ window_size_left: int = -1,
160
+ window_size_right: int = -1,
161
+ softcap: float = 0.0,
162
+ alibi_slopes: Optional[torch.Tensor] = None,
163
+ return_softmax: bool = False,
164
+ block_table: Optional[torch.Tensor] = None,
165
+ leftpad_k: Optional[torch.Tensor] = None,
166
+ seqused_k: Optional[torch.Tensor] = None,
167
+ ) -> Tuple[torch.Tensor, torch.Tensor, torch.Tensor, torch.Tensor]:
168
+ q, k, v = [maybe_contiguous(x) for x in (q, k, v)]
169
+ out, softmax_lse, S_dmask, rng_state = flash_attn_gpu.varlen_fwd(
170
+ q,
171
+ k,
172
+ v,
173
+ None,
174
+ cu_seqlens_q,
175
+ cu_seqlens_k,
176
+ seqused_k,
177
+ leftpad_k,
178
+ block_table,
179
+ alibi_slopes,
180
+ max_seqlen_q,
181
+ max_seqlen_k,
182
+ dropout_p,
183
+ softmax_scale,
184
+ False,
185
+ causal,
186
+ window_size_left,
187
+ window_size_right,
188
+ softcap,
189
+ return_softmax,
190
+ None,
191
+ )
192
+ # if out.isnan().any() or softmax_lse.isnan().any():
193
+ # breakpoint()
194
+ return out, softmax_lse, S_dmask, rng_state
195
+
196
+
197
+ @_torch_register_fake_wrapper("flash_attn::_flash_attn_varlen_forward")
198
+ def _flash_attn_varlen_forward_fake(
199
+ q: torch.Tensor,
200
+ k: torch.Tensor,
201
+ v: torch.Tensor,
202
+ cu_seqlens_q: torch.Tensor,
203
+ cu_seqlens_k: torch.Tensor,
204
+ max_seqlen_q: int,
205
+ max_seqlen_k: int,
206
+ dropout_p: float,
207
+ softmax_scale: float,
208
+ causal: bool,
209
+ window_size_left: int = -1,
210
+ window_size_right: int = -1,
211
+ softcap: float = 0.0,
212
+ alibi_slopes: Optional[torch.Tensor] = None,
213
+ return_softmax: bool = False,
214
+ block_table: Optional[torch.Tensor] = None,
215
+ leftpad_k: Optional[torch.Tensor] = None,
216
+ seqused_k: Optional[torch.Tensor] = None,
217
+ ) -> Tuple[torch.Tensor, torch.Tensor, torch.Tensor, torch.Tensor]:
218
+ q, k, v = [maybe_contiguous(x) for x in (q, k, v)]
219
+ paged_kv = block_table is not None
220
+ batch_size = cu_seqlens_q.numel() - 1
221
+ total_q, num_heads, _ = q.shape
222
+
223
+ out = torch.empty_like(q)
224
+ softmax_lse = torch.empty((num_heads, total_q), dtype=torch.float32, device=q.device, layout=q.layout)
225
+ p = torch.empty((0,), dtype=q.dtype, device=q.device, layout=q.layout)
226
+ seqlen_q_rounded = round_multiple(max_seqlen_q, 128)
227
+ seqlen_k_rounded = round_multiple(max_seqlen_k, 128)
228
+ if return_softmax:
229
+ p = torch.empty((batch_size, num_heads, seqlen_q_rounded, seqlen_k_rounded), dtype=q.dtype, device=q.device, layout=q.layout)
230
+ rng_state = torch.empty((2,), dtype=torch.int64, device=q.device)
231
+ return out, softmax_lse, p, rng_state
232
+
233
+
234
+ if torch.__version__ >= "2.4.0":
235
+ _wrapped_flash_attn_varlen_forward = torch.ops.flash_attn._flash_attn_varlen_forward
236
+ else:
237
+ _wrapped_flash_attn_varlen_forward = _flash_attn_varlen_forward
238
+
239
+
240
+ @_torch_custom_op_wrapper("flash_attn::_flash_attn_backward", mutates_args=("dq", "dk", "dv"), device_types="cuda")
241
+ def _flash_attn_backward(
242
+ dout: torch.Tensor,
243
+ q: torch.Tensor,
244
+ k: torch.Tensor,
245
+ v: torch.Tensor,
246
+ out: torch.Tensor,
247
+ softmax_lse: torch.Tensor,
248
+ dq: Optional[torch.Tensor],
249
+ dk: Optional[torch.Tensor],
250
+ dv: Optional[torch.Tensor],
251
+ dropout_p: float,
252
+ softmax_scale: float,
253
+ causal: bool,
254
+ window_size_left: int,
255
+ window_size_right: int,
256
+ softcap: float,
257
+ alibi_slopes: Optional[torch.Tensor],
258
+ deterministic: bool,
259
+ rng_state: Optional[torch.Tensor] = None,
260
+ ) -> torch.Tensor:
261
+ # dq, dk, dv are allocated by us so they should already be contiguous
262
+ dout, q, k, v, out = [maybe_contiguous(x) for x in (dout, q, k, v, out)]
263
+ (
264
+ dq,
265
+ dk,
266
+ dv,
267
+ softmax_d,
268
+ ) = flash_attn_gpu.bwd(
269
+ dout,
270
+ q,
271
+ k,
272
+ v,
273
+ out,
274
+ softmax_lse,
275
+ dq,
276
+ dk,
277
+ dv,
278
+ alibi_slopes,
279
+ dropout_p,
280
+ softmax_scale,
281
+ causal,
282
+ window_size_left,
283
+ window_size_right,
284
+ softcap,
285
+ deterministic,
286
+ None,
287
+ rng_state,
288
+ )
289
+ return softmax_d
290
+
291
+
292
+ @_torch_register_fake_wrapper("flash_attn::_flash_attn_backward")
293
+ def _flash_attn_backward_fake(
294
+ dout: torch.Tensor,
295
+ q: torch.Tensor,
296
+ k: torch.Tensor,
297
+ v: torch.Tensor,
298
+ out: torch.Tensor,
299
+ softmax_lse: torch.Tensor,
300
+ dq: Optional[torch.Tensor],
301
+ dk: Optional[torch.Tensor],
302
+ dv: Optional[torch.Tensor],
303
+ dropout_p: float,
304
+ softmax_scale: float,
305
+ causal: bool,
306
+ window_size_left: int,
307
+ window_size_right: int,
308
+ softcap: float,
309
+ alibi_slopes: Optional[torch.Tensor],
310
+ deterministic: bool,
311
+ rng_state: Optional[torch.Tensor] = None,
312
+ ) -> torch.Tensor:
313
+ dout, q, k, v, out = [maybe_contiguous(x) for x in (dout, q, k, v, out)]
314
+ if dq is None:
315
+ dq = torch.empty_like(q)
316
+ if dk is None:
317
+ dk = torch.empty_like(k)
318
+ if dv is None:
319
+ dv = torch.empty_like(v)
320
+ batch_size, seqlen_q, num_heads, _ = q.shape
321
+ softmax_d = torch.empty((batch_size, num_heads, round_multiple(seqlen_q, 128)), device=q.device, dtype=torch.float32)
322
+
323
+ return softmax_d
324
+
325
+
326
+ if torch.__version__ >= "2.4.0":
327
+ _wrapped_flash_attn_backward = torch.ops.flash_attn._flash_attn_backward
328
+ else:
329
+ _wrapped_flash_attn_backward = _flash_attn_backward
330
+
331
+
332
+ @_torch_custom_op_wrapper("flash_attn::_flash_attn_varlen_backward", mutates_args=("dq", "dk", "dv"), device_types="cuda")
333
+ def _flash_attn_varlen_backward(
334
+ dout: torch.Tensor,
335
+ q: torch.Tensor,
336
+ k: torch.Tensor,
337
+ v: torch.Tensor,
338
+ out: torch.Tensor,
339
+ softmax_lse: torch.Tensor,
340
+ dq: Optional[torch.Tensor],
341
+ dk: Optional[torch.Tensor],
342
+ dv: Optional[torch.Tensor],
343
+ cu_seqlens_q: torch.Tensor,
344
+ cu_seqlens_k: torch.Tensor,
345
+ max_seqlen_q: int,
346
+ max_seqlen_k: int,
347
+ dropout_p: float,
348
+ softmax_scale: float,
349
+ causal: bool,
350
+ window_size_left: int,
351
+ window_size_right: int,
352
+ softcap: float,
353
+ alibi_slopes: Optional[torch.Tensor],
354
+ deterministic: bool,
355
+ rng_state: Optional[torch.Tensor] = None,
356
+ ) -> torch.Tensor:
357
+ # dq, dk, dv are allocated by us so they should already be contiguous
358
+ dout, q, k, v, out = [maybe_contiguous(x) for x in (dout, q, k, v, out)]
359
+ (
360
+ dq,
361
+ dk,
362
+ dv,
363
+ softmax_d,
364
+ ) = flash_attn_gpu.varlen_bwd(
365
+ dout,
366
+ q,
367
+ k,
368
+ v,
369
+ out,
370
+ softmax_lse,
371
+ dq,
372
+ dk,
373
+ dv,
374
+ cu_seqlens_q,
375
+ cu_seqlens_k,
376
+ alibi_slopes,
377
+ max_seqlen_q,
378
+ max_seqlen_k,
379
+ dropout_p,
380
+ softmax_scale,
381
+ False,
382
+ causal,
383
+ window_size_left,
384
+ window_size_right,
385
+ softcap,
386
+ deterministic,
387
+ None,
388
+ rng_state,
389
+ )
390
+ # if dk.isnan().any() or dk.isnan().any() or dv.isnan().any() or softmax_d.isnan().any():
391
+ # breakpoint()
392
+ return softmax_d
393
+
394
+
395
+ @_torch_register_fake_wrapper("flash_attn::_flash_attn_varlen_backward")
396
+ def _flash_attn_varlen_backward_fake(
397
+ dout: torch.Tensor,
398
+ q: torch.Tensor,
399
+ k: torch.Tensor,
400
+ v: torch.Tensor,
401
+ out: torch.Tensor,
402
+ softmax_lse: torch.Tensor,
403
+ dq: Optional[torch.Tensor],
404
+ dk: Optional[torch.Tensor],
405
+ dv: Optional[torch.Tensor],
406
+ cu_seqlens_q: torch.Tensor,
407
+ cu_seqlens_k: torch.Tensor,
408
+ max_seqlen_q: int,
409
+ max_seqlen_k: int,
410
+ dropout_p: float,
411
+ softmax_scale: float,
412
+ causal: bool,
413
+ window_size_left: int,
414
+ window_size_right: int,
415
+ softcap: float,
416
+ alibi_slopes: Optional[torch.Tensor],
417
+ deterministic: bool,
418
+ rng_state: Optional[torch.Tensor] = None,
419
+ ) -> torch.Tensor:
420
+ dout, q, k, v, out = [maybe_contiguous(x) for x in (dout, q, k, v, out)]
421
+ batch_size = cu_seqlens_q.numel() - 1
422
+ total_q, num_heads, _ = q.shape
423
+
424
+ if dq is None:
425
+ dq = torch.empty_like(q)
426
+ if dk is None:
427
+ dk = torch.empty_like(k)
428
+ if dv is None:
429
+ dv = torch.empty_like(v)
430
+ softmax_d = torch.empty((num_heads, total_q + 128 * batch_size), device=q.device, dtype=torch.float32)
431
+
432
+ return softmax_d
433
+
434
+
435
+ if torch.__version__ >= "2.4.0":
436
+ _wrapped_flash_attn_varlen_backward = torch.ops.flash_attn._flash_attn_varlen_backward
437
+ else:
438
+ _wrapped_flash_attn_varlen_backward = _flash_attn_varlen_backward
439
+
440
+
441
+ class FlashAttnQKVPackedFunc(torch.autograd.Function):
442
+ @staticmethod
443
+ def forward(
444
+ ctx,
445
+ qkv,
446
+ dropout_p,
447
+ softmax_scale,
448
+ causal,
449
+ window_size,
450
+ softcap,
451
+ alibi_slopes,
452
+ deterministic,
453
+ return_softmax,
454
+ ):
455
+ if softmax_scale is None:
456
+ softmax_scale = qkv.shape[-1] ** (-0.5)
457
+ q, k, v = qkv[:, :, 0].detach(), qkv[:, :, 1].detach(), qkv[:, :, 2].detach()
458
+ head_size_og = q.size(3)
459
+ if head_size_og % 8 != 0:
460
+ q = torch.nn.functional.pad(q, [0, 8 - head_size_og % 8])
461
+ k = torch.nn.functional.pad(k, [0, 8 - head_size_og % 8])
462
+ v = torch.nn.functional.pad(v, [0, 8 - head_size_og % 8])
463
+ out_padded, softmax_lse, S_dmask, rng_state = _wrapped_flash_attn_forward(
464
+ q,
465
+ k,
466
+ v,
467
+ dropout_p,
468
+ softmax_scale,
469
+ causal=causal,
470
+ window_size_left=window_size[0],
471
+ window_size_right=window_size[1],
472
+ softcap=softcap,
473
+ alibi_slopes=alibi_slopes,
474
+ return_softmax=return_softmax and dropout_p > 0,
475
+ )
476
+ ctx.save_for_backward(q, k, v, out_padded, softmax_lse, rng_state)
477
+ ctx.dropout_p = dropout_p
478
+ ctx.softmax_scale = softmax_scale
479
+ ctx.causal = causal
480
+ ctx.window_size = window_size
481
+ ctx.softcap = softcap
482
+ ctx.alibi_slopes = alibi_slopes
483
+ ctx.deterministic = deterministic
484
+ out = out_padded[..., :head_size_og]
485
+ return out if not return_softmax else (out, softmax_lse, S_dmask)
486
+
487
+ @staticmethod
488
+ def backward(ctx, dout, *args):
489
+ q, k, v, out, softmax_lse, rng_state = ctx.saved_tensors
490
+ qkv_shape = q.shape[:-2] + (3, *q.shape[-2:])
491
+ dqkv = torch.empty(qkv_shape, dtype=q.dtype, device=q.device)
492
+ head_size_og = dout.size(3)
493
+ dout_padded = dout
494
+ if head_size_og % 8 != 0:
495
+ dout_padded = torch.nn.functional.pad(dout, [0, 8 - head_size_og % 8])
496
+ _wrapped_flash_attn_backward(
497
+ dout_padded,
498
+ q,
499
+ k,
500
+ v,
501
+ out,
502
+ softmax_lse,
503
+ dqkv[:, :, 0],
504
+ dqkv[:, :, 1],
505
+ dqkv[:, :, 2],
506
+ ctx.dropout_p,
507
+ ctx.softmax_scale,
508
+ ctx.causal,
509
+ ctx.window_size[0],
510
+ ctx.window_size[1],
511
+ ctx.softcap,
512
+ ctx.alibi_slopes,
513
+ ctx.deterministic,
514
+ rng_state=rng_state,
515
+ )
516
+ dqkv = dqkv[..., : dout.shape[-1]] # We could have padded the head dimension
517
+ return dqkv, None, None, None, None, None, None, None, None
518
+
519
+
520
+ class FlashAttnVarlenQKVPackedFunc(torch.autograd.Function):
521
+ @staticmethod
522
+ def forward(
523
+ ctx,
524
+ qkv,
525
+ cu_seqlens,
526
+ max_seqlen,
527
+ dropout_p,
528
+ softmax_scale,
529
+ causal,
530
+ window_size,
531
+ softcap,
532
+ alibi_slopes,
533
+ deterministic,
534
+ return_softmax,
535
+ ):
536
+ if softmax_scale is None:
537
+ softmax_scale = qkv.shape[-1] ** (-0.5)
538
+ q, k, v = qkv[:, 0].detach(), qkv[:, 1].detach(), qkv[:, 2].detach()
539
+ head_size_og = q.size(2)
540
+ if head_size_og % 8 != 0:
541
+ q = torch.nn.functional.pad(q, [0, 8 - head_size_og % 8])
542
+ k = torch.nn.functional.pad(k, [0, 8 - head_size_og % 8])
543
+ v = torch.nn.functional.pad(v, [0, 8 - head_size_og % 8])
544
+ out_padded, softmax_lse, S_dmask, rng_state = _wrapped_flash_attn_varlen_forward(
545
+ q,
546
+ k,
547
+ v,
548
+ cu_seqlens,
549
+ cu_seqlens,
550
+ max_seqlen,
551
+ max_seqlen,
552
+ dropout_p,
553
+ softmax_scale,
554
+ causal=causal,
555
+ window_size_left=window_size[0],
556
+ window_size_right=window_size[1],
557
+ softcap=softcap,
558
+ alibi_slopes=alibi_slopes,
559
+ return_softmax=return_softmax and dropout_p > 0,
560
+ block_table=None,
561
+ )
562
+ ctx.save_for_backward(q, k, v, out_padded, softmax_lse, cu_seqlens, rng_state)
563
+ ctx.dropout_p = dropout_p
564
+ ctx.max_seqlen = max_seqlen
565
+ ctx.softmax_scale = softmax_scale
566
+ ctx.causal = causal
567
+ ctx.window_size = window_size
568
+ ctx.softcap = softcap
569
+ ctx.alibi_slopes = alibi_slopes
570
+ ctx.deterministic = deterministic
571
+ out = out_padded[..., :head_size_og]
572
+ return out if not return_softmax else (out, softmax_lse, S_dmask)
573
+
574
+ @staticmethod
575
+ def backward(ctx, dout, *args):
576
+ q, k, v, out, softmax_lse, cu_seqlens, rng_state = ctx.saved_tensors
577
+ qkv_shape = q.shape[:-2] + (3, *q.shape[-2:])
578
+ dqkv = torch.empty(qkv_shape, dtype=q.dtype, device=q.device)
579
+ head_size_og = dout.size(2)
580
+ dout_padded = dout
581
+ if head_size_og % 8 != 0:
582
+ dout_padded = torch.nn.functional.pad(dout, [0, 8 - head_size_og % 8])
583
+ _wrapped_flash_attn_varlen_backward(
584
+ dout_padded,
585
+ q,
586
+ k,
587
+ v,
588
+ out,
589
+ softmax_lse,
590
+ dqkv[:, 0],
591
+ dqkv[:, 1],
592
+ dqkv[:, 2],
593
+ cu_seqlens,
594
+ cu_seqlens,
595
+ ctx.max_seqlen,
596
+ ctx.max_seqlen,
597
+ ctx.dropout_p,
598
+ ctx.softmax_scale,
599
+ ctx.causal,
600
+ ctx.window_size[0],
601
+ ctx.window_size[1],
602
+ ctx.softcap,
603
+ ctx.alibi_slopes,
604
+ ctx.deterministic,
605
+ rng_state=rng_state,
606
+ )
607
+ dqkv = dqkv[..., : dout.shape[-1]] # We could have padded the head dimension
608
+ return dqkv, None, None, None, None, None, None, None, None, None, None
609
+
610
+
611
+ class FlashAttnKVPackedFunc(torch.autograd.Function):
612
+ @staticmethod
613
+ def forward(
614
+ ctx,
615
+ q,
616
+ kv,
617
+ dropout_p,
618
+ softmax_scale,
619
+ causal,
620
+ window_size,
621
+ softcap,
622
+ alibi_slopes,
623
+ deterministic,
624
+ return_softmax,
625
+ ):
626
+ if softmax_scale is None:
627
+ softmax_scale = q.shape[-1] ** (-0.5)
628
+ k, v = kv[:, :, 0].detach(), kv[:, :, 1].detach()
629
+ head_size_og = q.size(3)
630
+ if head_size_og % 8 != 0:
631
+ q = torch.nn.functional.pad(q, [0, 8 - head_size_og % 8])
632
+ k = torch.nn.functional.pad(k, [0, 8 - head_size_og % 8])
633
+ v = torch.nn.functional.pad(v, [0, 8 - head_size_og % 8])
634
+ out_padded, softmax_lse, S_dmask, rng_state = _wrapped_flash_attn_forward(
635
+ q,
636
+ k,
637
+ v,
638
+ dropout_p,
639
+ softmax_scale,
640
+ causal=causal,
641
+ window_size_left=window_size[0],
642
+ window_size_right=window_size[1],
643
+ softcap=softcap,
644
+ alibi_slopes=alibi_slopes,
645
+ return_softmax=return_softmax and dropout_p > 0,
646
+ )
647
+ ctx.save_for_backward(q, k, v, out_padded, softmax_lse, rng_state)
648
+ ctx.dropout_p = dropout_p
649
+ ctx.softmax_scale = softmax_scale
650
+ ctx.causal = causal
651
+ ctx.window_size = window_size
652
+ ctx.softcap = softcap
653
+ ctx.alibi_slopes = alibi_slopes
654
+ ctx.deterministic = deterministic
655
+ out = out_padded[..., :head_size_og]
656
+ return out if not return_softmax else (out, softmax_lse, S_dmask)
657
+
658
+ @staticmethod
659
+ def backward(ctx, dout, *args):
660
+ q, k, v, out, softmax_lse, rng_state = ctx.saved_tensors
661
+ dq = torch.empty_like(q)
662
+ kv_shape = k.shape[:-2] + (2, *k.shape[-2:])
663
+ dkv = torch.empty(kv_shape, dtype=k.dtype, device=k.device)
664
+ head_size_og = dout.size(3)
665
+ dout_padded = dout
666
+ if head_size_og % 8 != 0:
667
+ dout_padded = torch.nn.functional.pad(dout, [0, 8 - head_size_og % 8])
668
+ _wrapped_flash_attn_backward(
669
+ dout_padded,
670
+ q,
671
+ k,
672
+ v,
673
+ out,
674
+ softmax_lse,
675
+ dq,
676
+ dkv[:, :, 0],
677
+ dkv[:, :, 1],
678
+ ctx.dropout_p,
679
+ ctx.softmax_scale,
680
+ ctx.causal,
681
+ ctx.window_size[0],
682
+ ctx.window_size[1],
683
+ ctx.softcap,
684
+ ctx.alibi_slopes,
685
+ ctx.deterministic,
686
+ rng_state=rng_state,
687
+ )
688
+ dq = dq[..., : dout.shape[-1]] # We could have padded the head dimension
689
+ dkv = dkv[..., : dout.shape[-1]]
690
+ return dq, dkv, None, None, None, None, None, None, None, None
691
+
692
+
693
+ class FlashAttnVarlenKVPackedFunc(torch.autograd.Function):
694
+ @staticmethod
695
+ def forward(
696
+ ctx,
697
+ q,
698
+ kv,
699
+ cu_seqlens_q,
700
+ cu_seqlens_k,
701
+ max_seqlen_q,
702
+ max_seqlen_k,
703
+ dropout_p,
704
+ softmax_scale,
705
+ causal,
706
+ window_size,
707
+ softcap,
708
+ alibi_slopes,
709
+ deterministic,
710
+ return_softmax,
711
+ ):
712
+ if softmax_scale is None:
713
+ softmax_scale = q.shape[-1] ** (-0.5)
714
+ k, v = kv[:, 0].detach(), kv[:, 1].detach()
715
+ head_size_og = q.size(2)
716
+ if head_size_og % 8 != 0:
717
+ q = torch.nn.functional.pad(q, [0, 8 - head_size_og % 8])
718
+ k = torch.nn.functional.pad(k, [0, 8 - head_size_og % 8])
719
+ v = torch.nn.functional.pad(v, [0, 8 - head_size_og % 8])
720
+ out_padded, softmax_lse, S_dmask, rng_state = _wrapped_flash_attn_varlen_forward(
721
+ q,
722
+ k,
723
+ v,
724
+ cu_seqlens_q,
725
+ cu_seqlens_k,
726
+ max_seqlen_q,
727
+ max_seqlen_k,
728
+ dropout_p,
729
+ softmax_scale,
730
+ causal=causal,
731
+ window_size_left=window_size[0],
732
+ window_size_right=window_size[1],
733
+ softcap=softcap,
734
+ alibi_slopes=alibi_slopes,
735
+ return_softmax=return_softmax and dropout_p > 0,
736
+ block_table=None,
737
+ )
738
+ ctx.save_for_backward(
739
+ q, k, v, out_padded, softmax_lse, cu_seqlens_q, cu_seqlens_k, rng_state
740
+ )
741
+ ctx.dropout_p = dropout_p
742
+ ctx.max_seqlen_q = max_seqlen_q
743
+ ctx.max_seqlen_k = max_seqlen_k
744
+ ctx.softmax_scale = softmax_scale
745
+ ctx.causal = causal
746
+ ctx.window_size = window_size
747
+ ctx.softcap = softcap
748
+ ctx.alibi_slopes = alibi_slopes
749
+ ctx.deterministic = deterministic
750
+ out = out_padded[..., :head_size_og]
751
+ return out if not return_softmax else (out, softmax_lse, S_dmask)
752
+
753
+ @staticmethod
754
+ def backward(ctx, dout, *args):
755
+ q, k, v, out, softmax_lse, cu_seqlens_q, cu_seqlens_k, rng_state = ctx.saved_tensors
756
+ dq = torch.empty_like(q)
757
+ kv_shape = k.shape[:-2] + (2, *k.shape[-2:])
758
+ dkv = torch.empty(kv_shape, dtype=k.dtype, device=k.device)
759
+ head_size_og = dout.size(2)
760
+ dout_padded = dout
761
+ if head_size_og % 8 != 0:
762
+ dout_padded = torch.nn.functional.pad(dout, [0, 8 - head_size_og % 8])
763
+ _wrapped_flash_attn_varlen_backward(
764
+ dout_padded,
765
+ q,
766
+ k,
767
+ v,
768
+ out,
769
+ softmax_lse,
770
+ dq,
771
+ dkv[:, 0],
772
+ dkv[:, 1],
773
+ cu_seqlens_q,
774
+ cu_seqlens_k,
775
+ ctx.max_seqlen_q,
776
+ ctx.max_seqlen_k,
777
+ ctx.dropout_p,
778
+ ctx.softmax_scale,
779
+ ctx.causal,
780
+ ctx.window_size[0],
781
+ ctx.window_size[1],
782
+ ctx.softcap,
783
+ ctx.alibi_slopes,
784
+ ctx.deterministic,
785
+ rng_state=rng_state,
786
+ )
787
+ dq = dq[..., : dout.shape[-1]] # We could have padded the head dimension
788
+ dkv = dkv[..., : dout.shape[-1]]
789
+ return dq, dkv, None, None, None, None, None, None, None, None, None, None, None, None
790
+
791
+
792
+ class FlashAttnFunc(torch.autograd.Function):
793
+ @staticmethod
794
+ def forward(
795
+ ctx,
796
+ q,
797
+ k,
798
+ v,
799
+ dropout_p,
800
+ softmax_scale,
801
+ causal,
802
+ window_size,
803
+ softcap,
804
+ alibi_slopes,
805
+ deterministic,
806
+ return_softmax,
807
+ ):
808
+ if softmax_scale is None:
809
+ softmax_scale = q.shape[-1] ** (-0.5)
810
+ head_size_og = q.size(3)
811
+ if head_size_og % 8 != 0:
812
+ q = torch.nn.functional.pad(q, [0, 8 - head_size_og % 8])
813
+ k = torch.nn.functional.pad(k, [0, 8 - head_size_og % 8])
814
+ v = torch.nn.functional.pad(v, [0, 8 - head_size_og % 8])
815
+ out_padded, softmax_lse, S_dmask, rng_state = _wrapped_flash_attn_forward(
816
+ q,
817
+ k,
818
+ v,
819
+ dropout_p,
820
+ softmax_scale,
821
+ causal=causal,
822
+ window_size_left=window_size[0],
823
+ window_size_right=window_size[1],
824
+ softcap=softcap,
825
+ alibi_slopes=alibi_slopes,
826
+ return_softmax=return_softmax and dropout_p > 0,
827
+ )
828
+ ctx.save_for_backward(q, k, v, out_padded, softmax_lse, rng_state)
829
+ ctx.dropout_p = dropout_p
830
+ ctx.softmax_scale = softmax_scale
831
+ ctx.causal = causal
832
+ ctx.window_size = window_size
833
+ ctx.softcap = softcap
834
+ ctx.alibi_slopes = alibi_slopes
835
+ ctx.deterministic = deterministic
836
+ out = out_padded[..., :head_size_og]
837
+ return out if not return_softmax else (out, softmax_lse, S_dmask)
838
+
839
+ @staticmethod
840
+ def backward(ctx, dout, *args):
841
+ q, k, v, out, softmax_lse, rng_state = ctx.saved_tensors
842
+ dq, dk, dv = torch.empty_like(q), torch.empty_like(k), torch.empty_like(v)
843
+ head_size_og = dout.size(3)
844
+ dout_padded = dout
845
+ if head_size_og % 8 != 0:
846
+ dout_padded = torch.nn.functional.pad(dout, [0, 8 - head_size_og % 8])
847
+ _wrapped_flash_attn_backward(
848
+ dout_padded,
849
+ q,
850
+ k,
851
+ v,
852
+ out,
853
+ softmax_lse,
854
+ dq,
855
+ dk,
856
+ dv,
857
+ ctx.dropout_p,
858
+ ctx.softmax_scale,
859
+ ctx.causal,
860
+ ctx.window_size[0],
861
+ ctx.window_size[1],
862
+ ctx.softcap,
863
+ ctx.alibi_slopes,
864
+ ctx.deterministic,
865
+ rng_state=rng_state,
866
+ )
867
+ dq = dq[..., : dout.shape[-1]] # We could have padded the head dimension
868
+ dk = dk[..., : dout.shape[-1]]
869
+ dv = dv[..., : dout.shape[-1]]
870
+ return dq, dk, dv, None, None, None, None, None, None, None, None
871
+
872
+
873
+ class FlashAttnVarlenFunc(torch.autograd.Function):
874
+ @staticmethod
875
+ def forward(
876
+ ctx,
877
+ q,
878
+ k,
879
+ v,
880
+ cu_seqlens_q,
881
+ cu_seqlens_k,
882
+ max_seqlen_q,
883
+ max_seqlen_k,
884
+ dropout_p,
885
+ softmax_scale,
886
+ causal,
887
+ window_size,
888
+ softcap,
889
+ alibi_slopes,
890
+ deterministic,
891
+ return_softmax,
892
+ block_table,
893
+ ):
894
+ if softmax_scale is None:
895
+ softmax_scale = q.shape[-1] ** (-0.5)
896
+ head_size_og = q.size(2)
897
+ if head_size_og % 8 != 0:
898
+ q = torch.nn.functional.pad(q, [0, 8 - head_size_og % 8])
899
+ k = torch.nn.functional.pad(k, [0, 8 - head_size_og % 8])
900
+ v = torch.nn.functional.pad(v, [0, 8 - head_size_og % 8])
901
+ out_padded, softmax_lse, S_dmask, rng_state = _wrapped_flash_attn_varlen_forward(
902
+ q,
903
+ k,
904
+ v,
905
+ cu_seqlens_q,
906
+ cu_seqlens_k,
907
+ max_seqlen_q,
908
+ max_seqlen_k,
909
+ dropout_p,
910
+ softmax_scale,
911
+ causal=causal,
912
+ window_size_left=window_size[0],
913
+ window_size_right=window_size[1],
914
+ softcap=softcap,
915
+ alibi_slopes=alibi_slopes,
916
+ return_softmax=return_softmax and dropout_p > 0,
917
+ block_table=block_table,
918
+ )
919
+ ctx.save_for_backward(
920
+ q, k, v, out_padded, softmax_lse, cu_seqlens_q, cu_seqlens_k, rng_state
921
+ )
922
+ ctx.dropout_p = dropout_p
923
+ ctx.max_seqlen_q = max_seqlen_q
924
+ ctx.max_seqlen_k = max_seqlen_k
925
+ ctx.softmax_scale = softmax_scale
926
+ ctx.causal = causal
927
+ ctx.window_size = window_size
928
+ ctx.softcap = softcap
929
+ ctx.alibi_slopes = alibi_slopes
930
+ ctx.deterministic = deterministic
931
+ out = out_padded[..., :head_size_og]
932
+ return out if not return_softmax else (out, softmax_lse, S_dmask)
933
+
934
+ @staticmethod
935
+ def backward(ctx, dout, *args):
936
+ q, k, v, out, softmax_lse, cu_seqlens_q, cu_seqlens_k, rng_state = ctx.saved_tensors
937
+ dq, dk, dv = torch.empty_like(q), torch.empty_like(k), torch.empty_like(v)
938
+ head_size_og = dout.size(2)
939
+ dout_padded = dout
940
+ if head_size_og % 8 != 0:
941
+ dout_padded = torch.nn.functional.pad(dout, [0, 8 - head_size_og % 8])
942
+ _wrapped_flash_attn_varlen_backward(
943
+ dout_padded,
944
+ q,
945
+ k,
946
+ v,
947
+ out,
948
+ softmax_lse,
949
+ dq,
950
+ dk,
951
+ dv,
952
+ cu_seqlens_q,
953
+ cu_seqlens_k,
954
+ ctx.max_seqlen_q,
955
+ ctx.max_seqlen_k,
956
+ ctx.dropout_p,
957
+ ctx.softmax_scale,
958
+ ctx.causal,
959
+ ctx.window_size[0],
960
+ ctx.window_size[1],
961
+ ctx.softcap,
962
+ ctx.alibi_slopes,
963
+ ctx.deterministic,
964
+ rng_state=rng_state,
965
+ )
966
+ dq = dq[..., : dout.shape[-1]] # We could have padded the head dimension
967
+ dk = dk[..., : dout.shape[-1]]
968
+ dv = dv[..., : dout.shape[-1]]
969
+ return dq, dk, dv, None, None, None, None, None, None, None, None, None, None, None, None, None
970
+
971
+
972
+ def flash_attn_qkvpacked_func(
973
+ qkv,
974
+ dropout_p=0.0,
975
+ softmax_scale=None,
976
+ causal=False,
977
+ window_size=(-1, -1), # -1 means infinite context window
978
+ softcap=0.0, # <=0.0 means deactivate
979
+ alibi_slopes=None,
980
+ deterministic=False,
981
+ return_attn_probs=False,
982
+ ):
983
+ """dropout_p should be set to 0.0 during evaluation
984
+ If Q, K, V are already stacked into 1 tensor, this function will be faster than
985
+ calling flash_attn_func on Q, K, V since the backward pass avoids explicit concatenation
986
+ of the gradients of Q, K, V.
987
+ For multi-query and grouped-query attention (MQA/GQA), please see
988
+ flash_attn_kvpacked_func and flash_attn_func.
989
+
990
+ If window_size != (-1, -1), implements sliding window local attention. Query at position i
991
+ will only attend to keys between [i - window_size[0], i + window_size[1]] inclusive.
992
+
993
+ Arguments:
994
+ qkv: (batch_size, seqlen, 3, nheads, headdim)
995
+ dropout_p: float. Dropout probability.
996
+ softmax_scale: float. The scaling of QK^T before applying softmax.
997
+ Default to 1 / sqrt(headdim).
998
+ causal: bool. Whether to apply causal attention mask (e.g., for auto-regressive modeling).
999
+ window_size: (left, right). If not (-1, -1), implements sliding window local attention.
1000
+ softcap: float. Anything > 0 activates softcapping attention.
1001
+ alibi_slopes: (nheads,) or (batch_size, nheads), fp32. A bias of (-alibi_slope * |i - j|) is added to
1002
+ the attention score of query i and key j.
1003
+ deterministic: bool. Whether to use the deterministic implementation of the backward pass,
1004
+ which is slightly slower and uses more memory. The forward pass is always deterministic.
1005
+ return_attn_probs: bool. Whether to return the attention probabilities. This option is for
1006
+ testing only. The returned probabilities are not guaranteed to be correct
1007
+ (they might not have the right scaling).
1008
+ Return:
1009
+ out: (batch_size, seqlen, nheads, headdim).
1010
+ softmax_lse [optional, if return_attn_probs=True]: (batch_size, nheads, seqlen). The
1011
+ logsumexp of each row of the matrix QK^T * scaling (e.g., log of the softmax
1012
+ normalization factor).
1013
+ S_dmask [optional, if return_attn_probs=True]: (batch_size, nheads, seqlen, seqlen).
1014
+ The output of softmax (possibly with different scaling). It also encodes the dropout
1015
+ pattern (negative means that location was dropped, nonnegative means it was kept).
1016
+ """
1017
+ return FlashAttnQKVPackedFunc.apply(
1018
+ qkv,
1019
+ dropout_p,
1020
+ softmax_scale,
1021
+ causal,
1022
+ window_size,
1023
+ softcap,
1024
+ alibi_slopes,
1025
+ deterministic,
1026
+ return_attn_probs,
1027
+ )
1028
+
1029
+
1030
+ def flash_attn_kvpacked_func(
1031
+ q,
1032
+ kv,
1033
+ dropout_p=0.0,
1034
+ softmax_scale=None,
1035
+ causal=False,
1036
+ window_size=(-1, -1), # -1 means infinite context window
1037
+ softcap=0.0, # 0.0 means deactivated
1038
+ alibi_slopes=None,
1039
+ deterministic=False,
1040
+ return_attn_probs=False,
1041
+ ):
1042
+ """dropout_p should be set to 0.0 during evaluation
1043
+ If K, V are already stacked into 1 tensor, this function will be faster than
1044
+ calling flash_attn_func on Q, K, V since the backward pass avoids explicit concatenation
1045
+ of the gradients of K, V.
1046
+ Supports multi-query and grouped-query attention (MQA/GQA) by passing in KV with fewer heads
1047
+ than Q. Note that the number of heads in Q must be divisible by the number of heads in KV.
1048
+ For example, if Q has 6 heads and K, V have 2 heads, head 0, 1, 2 of Q will attention to head
1049
+ 0 of K, V, and head 3, 4, 5 of Q will attention to head 1 of K, V.
1050
+
1051
+ If causal=True, the causal mask is aligned to the bottom right corner of the attention matrix.
1052
+ For example, if seqlen_q = 2 and seqlen_k = 5, the causal mask (1 = keep, 0 = masked out) is:
1053
+ 1 1 1 1 0
1054
+ 1 1 1 1 1
1055
+ If seqlen_q = 5 and seqlen_k = 2, the causal mask is:
1056
+ 0 0
1057
+ 0 0
1058
+ 0 0
1059
+ 1 0
1060
+ 1 1
1061
+ If the row of the mask is all zero, the output will be zero.
1062
+
1063
+ If window_size != (-1, -1), implements sliding window local attention. Query at position i
1064
+ will only attend to keys between
1065
+ [i + seqlen_k - seqlen_q - window_size[0], i + seqlen_k - seqlen_q + window_size[1]] inclusive.
1066
+
1067
+ Arguments:
1068
+ q: (batch_size, seqlen, nheads, headdim)
1069
+ kv: (batch_size, seqlen, 2, nheads_k, headdim)
1070
+ dropout_p: float. Dropout probability.
1071
+ softmax_scale: float. The scaling of QK^T before applying softmax.
1072
+ Default to 1 / sqrt(headdim).
1073
+ causal: bool. Whether to apply causal attention mask (e.g., for auto-regressive modeling).
1074
+ window_size: (left, right). If not (-1, -1), implements sliding window local attention.
1075
+ softcap: float. Anything > 0 activates softcapping attention.
1076
+ alibi_slopes: (nheads,) or (batch_size, nheads), fp32. A bias of
1077
+ (-alibi_slope * |i + seqlen_k - seqlen_q - j|)
1078
+ is added to the attention score of query i and key j.
1079
+ deterministic: bool. Whether to use the deterministic implementation of the backward pass,
1080
+ which is slightly slower and uses more memory. The forward pass is always deterministic.
1081
+ return_attn_probs: bool. Whether to return the attention probabilities. This option is for
1082
+ testing only. The returned probabilities are not guaranteed to be correct
1083
+ (they might not have the right scaling).
1084
+ Return:
1085
+ out: (batch_size, seqlen, nheads, headdim).
1086
+ softmax_lse [optional, if return_attn_probs=True]: (batch_size, nheads, seqlen). The
1087
+ logsumexp of each row of the matrix QK^T * scaling (e.g., log of the softmax
1088
+ normalization factor).
1089
+ S_dmask [optional, if return_attn_probs=True]: (batch_size, nheads, seqlen, seqlen).
1090
+ The output of softmax (possibly with different scaling). It also encodes the dropout
1091
+ pattern (negative means that location was dropped, nonnegative means it was kept).
1092
+ """
1093
+ return FlashAttnKVPackedFunc.apply(
1094
+ q,
1095
+ kv,
1096
+ dropout_p,
1097
+ softmax_scale,
1098
+ causal,
1099
+ window_size,
1100
+ softcap,
1101
+ alibi_slopes,
1102
+ deterministic,
1103
+ return_attn_probs,
1104
+ )
1105
+
1106
+
1107
+ def flash_attn_func(
1108
+ q,
1109
+ k,
1110
+ v,
1111
+ dropout_p=0.0,
1112
+ softmax_scale=None,
1113
+ causal=False,
1114
+ window_size=(-1, -1), # -1 means infinite context window
1115
+ softcap=0.0, # 0.0 means deactivated
1116
+ alibi_slopes=None,
1117
+ deterministic=False,
1118
+ return_attn_probs=False,
1119
+ ):
1120
+ """dropout_p should be set to 0.0 during evaluation
1121
+ Supports multi-query and grouped-query attention (MQA/GQA) by passing in KV with fewer heads
1122
+ than Q. Note that the number of heads in Q must be divisible by the number of heads in KV.
1123
+ For example, if Q has 6 heads and K, V have 2 heads, head 0, 1, 2 of Q will attention to head
1124
+ 0 of K, V, and head 3, 4, 5 of Q will attention to head 1 of K, V.
1125
+
1126
+ If causal=True, the causal mask is aligned to the bottom right corner of the attention matrix.
1127
+ For example, if seqlen_q = 2 and seqlen_k = 5, the causal mask (1 = keep, 0 = masked out) is:
1128
+ 1 1 1 1 0
1129
+ 1 1 1 1 1
1130
+ If seqlen_q = 5 and seqlen_k = 2, the causal mask is:
1131
+ 0 0
1132
+ 0 0
1133
+ 0 0
1134
+ 1 0
1135
+ 1 1
1136
+ If the row of the mask is all zero, the output will be zero.
1137
+
1138
+ If window_size != (-1, -1), implements sliding window local attention. Query at position i
1139
+ will only attend to keys between
1140
+ [i + seqlen_k - seqlen_q - window_size[0], i + seqlen_k - seqlen_q + window_size[1]] inclusive.
1141
+
1142
+ Arguments:
1143
+ q: (batch_size, seqlen, nheads, headdim)
1144
+ k: (batch_size, seqlen, nheads_k, headdim)
1145
+ v: (batch_size, seqlen, nheads_k, headdim)
1146
+ dropout_p: float. Dropout probability.
1147
+ softmax_scale: float. The scaling of QK^T before applying softmax.
1148
+ Default to 1 / sqrt(headdim).
1149
+ causal: bool. Whether to apply causal attention mask (e.g., for auto-regressive modeling).
1150
+ window_size: (left, right). If not (-1, -1), implements sliding window local attention.
1151
+ alibi_slopes: (nheads,) or (batch_size, nheads), fp32. A bias of
1152
+ (-alibi_slope * |i + seqlen_k - seqlen_q - j|)
1153
+ is added to the attention score of query i and key j.
1154
+ deterministic: bool. Whether to use the deterministic implementation of the backward pass,
1155
+ which is slightly slower and uses more memory. The forward pass is always deterministic.
1156
+ return_attn_probs: bool. Whether to return the attention probabilities. This option is for
1157
+ testing only. The returned probabilities are not guaranteed to be correct
1158
+ (they might not have the right scaling).
1159
+ Return:
1160
+ out: (batch_size, seqlen, nheads, headdim).
1161
+ softmax_lse [optional, if return_attn_probs=True]: (batch_size, nheads, seqlen). The
1162
+ logsumexp of each row of the matrix QK^T * scaling (e.g., log of the softmax
1163
+ normalization factor).
1164
+ S_dmask [optional, if return_attn_probs=True]: (batch_size, nheads, seqlen, seqlen).
1165
+ The output of softmax (possibly with different scaling). It also encodes the dropout
1166
+ pattern (negative means that location was dropped, nonnegative means it was kept).
1167
+ """
1168
+ return FlashAttnFunc.apply(
1169
+ q,
1170
+ k,
1171
+ v,
1172
+ dropout_p,
1173
+ softmax_scale,
1174
+ causal,
1175
+ window_size,
1176
+ softcap,
1177
+ alibi_slopes,
1178
+ deterministic,
1179
+ return_attn_probs,
1180
+ )
1181
+
1182
+
1183
+ def flash_attn_varlen_qkvpacked_func(
1184
+ qkv,
1185
+ cu_seqlens,
1186
+ max_seqlen,
1187
+ dropout_p=0.0,
1188
+ softmax_scale=None,
1189
+ causal=False,
1190
+ window_size=(-1, -1), # -1 means infinite context window
1191
+ softcap=0.0, # 0.0 means deactivated
1192
+ alibi_slopes=None,
1193
+ deterministic=False,
1194
+ return_attn_probs=False,
1195
+ ):
1196
+ """dropout_p should be set to 0.0 during evaluation
1197
+ If Q, K, V are already stacked into 1 tensor, this function will be faster than
1198
+ calling flash_attn_varlen_func on Q, K, V since the backward pass avoids explicit concatenation
1199
+ of the gradients of Q, K, V.
1200
+ For multi-query and grouped-query attention (MQA/GQA), please see
1201
+ flash_attn_varlen_kvpacked_func and flash_attn_varlen_func.
1202
+
1203
+ If window_size != (-1, -1), implements sliding window local attention. Query at position i
1204
+ will only attend to keys between [i - window_size[0], i + window_size[1]] inclusive.
1205
+
1206
+ Arguments:
1207
+ qkv: (total, 3, nheads, headdim), where total = total number of tokens in the batch.
1208
+ cu_seqlens: (batch_size + 1,), dtype torch.int32. The cumulative sequence lengths
1209
+ of the sequences in the batch, used to index into qkv.
1210
+ max_seqlen: int. Maximum sequence length in the batch.
1211
+ dropout_p: float. Dropout probability.
1212
+ softmax_scale: float. The scaling of QK^T before applying softmax.
1213
+ Default to 1 / sqrt(headdim).
1214
+ causal: bool. Whether to apply causal attention mask (e.g., for auto-regressive modeling).
1215
+ window_size: (left, right). If not (-1, -1), implements sliding window local attention.
1216
+ softcap: float. Anything > 0 activates softcapping attention.
1217
+ alibi_slopes: (nheads,) or (batch_size, nheads), fp32. A bias of (-alibi_slope * |i - j|)
1218
+ is added to the attention score of query i and key j.
1219
+ deterministic: bool. Whether to use the deterministic implementation of the backward pass,
1220
+ which is slightly slower and uses more memory. The forward pass is always deterministic.
1221
+ return_attn_probs: bool. Whether to return the attention probabilities. This option is for
1222
+ testing only. The returned probabilities are not guaranteed to be correct
1223
+ (they might not have the right scaling).
1224
+ Return:
1225
+ out: (total, nheads, headdim).
1226
+ softmax_lse [optional, if return_attn_probs=True]: (nheads, total_q_seqlen). The
1227
+ logsumexp of each row of the matrix QK^T * scaling (e.g., log of the softmax
1228
+ normalization factor).
1229
+ S_dmask [optional, if return_attn_probs=True]: (batch_size, nheads, seqlen, seqlen).
1230
+ The output of softmax (possibly with different scaling). It also encodes the dropout
1231
+ pattern (negative means that location was dropped, nonnegative means it was kept).
1232
+ """
1233
+ return FlashAttnVarlenQKVPackedFunc.apply(
1234
+ qkv,
1235
+ cu_seqlens,
1236
+ max_seqlen,
1237
+ dropout_p,
1238
+ softmax_scale,
1239
+ causal,
1240
+ window_size,
1241
+ softcap,
1242
+ alibi_slopes,
1243
+ deterministic,
1244
+ return_attn_probs,
1245
+ )
1246
+
1247
+
1248
+ def flash_attn_varlen_kvpacked_func(
1249
+ q,
1250
+ kv,
1251
+ cu_seqlens_q,
1252
+ cu_seqlens_k,
1253
+ max_seqlen_q,
1254
+ max_seqlen_k,
1255
+ dropout_p=0.0,
1256
+ softmax_scale=None,
1257
+ causal=False,
1258
+ window_size=(-1, -1), # -1 means infinite context window
1259
+ softcap=0.0, # 0.0 means deactivated
1260
+ alibi_slopes=None,
1261
+ deterministic=False,
1262
+ return_attn_probs=False,
1263
+ ):
1264
+ """dropout_p should be set to 0.0 during evaluation
1265
+ If K, V are already stacked into 1 tensor, this function will be faster than
1266
+ calling flash_attn_func on Q, K, V since the backward pass avoids explicit concatenation
1267
+ of the gradients of K, V.
1268
+ Supports multi-query and grouped-query attention (MQA/GQA) by passing in KV with fewer heads
1269
+ than Q. Note that the number of heads in Q must be divisible by the number of heads in KV.
1270
+ For example, if Q has 6 heads and K, V have 2 heads, head 0, 1, 2 of Q will attention to head
1271
+ 0 of K, V, and head 3, 4, 5 of Q will attention to head 1 of K, V.
1272
+
1273
+ If causal=True, the causal mask is aligned to the bottom right corner of the attention matrix.
1274
+ For example, if seqlen_q = 2 and seqlen_k = 5, the causal mask (1 = keep, 0 = masked out) is:
1275
+ 1 1 1 1 0
1276
+ 1 1 1 1 1
1277
+ If seqlen_q = 5 and seqlen_k = 2, the causal mask is:
1278
+ 0 0
1279
+ 0 0
1280
+ 0 0
1281
+ 1 0
1282
+ 1 1
1283
+ If the row of the mask is all zero, the output will be zero.
1284
+
1285
+ If window_size != (-1, -1), implements sliding window local attention. Query at position i
1286
+ will only attend to keys between
1287
+ [i + seqlen_k - seqlen_q - window_size[0], i + seqlen_k - seqlen_q + window_size[1]] inclusive.
1288
+
1289
+ Arguments:
1290
+ q: (total_q, nheads, headdim), where total_q = total number of query tokens in the batch.
1291
+ kv: (total_k, 2, nheads_k, headdim), where total_k = total number of key tokens in the batch.
1292
+ cu_seqlens_q: (batch_size + 1,), dtype torch.int32. The cumulative sequence lengths
1293
+ of the sequences in the batch, used to index into q.
1294
+ cu_seqlens_k: (batch_size + 1,), dtype torch.int32. The cumulative sequence lengths
1295
+ of the sequences in the batch, used to index into kv.
1296
+ max_seqlen_q: int. Maximum query sequence length in the batch.
1297
+ max_seqlen_k: int. Maximum key sequence length in the batch.
1298
+ dropout_p: float. Dropout probability.
1299
+ softmax_scale: float. The scaling of QK^T before applying softmax.
1300
+ Default to 1 / sqrt(headdim).
1301
+ causal: bool. Whether to apply causal attention mask (e.g., for auto-regressive modeling).
1302
+ window_size: (left, right). If not (-1, -1), implements sliding window local attention.
1303
+ softcap: float. Anything > 0 activates softcapping attention.
1304
+ alibi_slopes: (nheads,) or (batch_size, nheads), fp32. A bias of
1305
+ (-alibi_slope * |i + seqlen_k - seqlen_q - j|)
1306
+ is added to the attention score of query i and key j.
1307
+ deterministic: bool. Whether to use the deterministic implementation of the backward pass,
1308
+ which is slightly slower and uses more memory. The forward pass is always deterministic.
1309
+ return_attn_probs: bool. Whether to return the attention probabilities. This option is for
1310
+ testing only. The returned probabilities are not guaranteed to be correct
1311
+ (they might not have the right scaling).
1312
+ Return:
1313
+ out: (total, nheads, headdim).
1314
+ softmax_lse [optional, if return_attn_probs=True]: (nheads, total_q_seqlen). The
1315
+ logsumexp of each row of the matrix QK^T * scaling (e.g., log of the softmax
1316
+ normalization factor).
1317
+ S_dmask [optional, if return_attn_probs=True]: (batch_size, nheads, seqlen, seqlen).
1318
+ The output of softmax (possibly with different scaling). It also encodes the dropout
1319
+ pattern (negative means that location was dropped, nonnegative means it was kept).
1320
+ """
1321
+ return FlashAttnVarlenKVPackedFunc.apply(
1322
+ q,
1323
+ kv,
1324
+ cu_seqlens_q,
1325
+ cu_seqlens_k,
1326
+ max_seqlen_q,
1327
+ max_seqlen_k,
1328
+ dropout_p,
1329
+ softmax_scale,
1330
+ causal,
1331
+ window_size,
1332
+ softcap,
1333
+ alibi_slopes,
1334
+ deterministic,
1335
+ return_attn_probs,
1336
+ )
1337
+
1338
+
1339
+ def flash_attn_varlen_func(
1340
+ q,
1341
+ k,
1342
+ v,
1343
+ cu_seqlens_q,
1344
+ cu_seqlens_k,
1345
+ max_seqlen_q,
1346
+ max_seqlen_k,
1347
+ dropout_p=0.0,
1348
+ softmax_scale=None,
1349
+ causal=False,
1350
+ window_size=(-1, -1), # -1 means infinite context window
1351
+ softcap=0.0, # 0.0 means deactivated
1352
+ alibi_slopes=None,
1353
+ deterministic=False,
1354
+ return_attn_probs=False,
1355
+ block_table=None,
1356
+ ):
1357
+ """dropout_p should be set to 0.0 during evaluation
1358
+ Supports multi-query and grouped-query attention (MQA/GQA) by passing in K, V with fewer heads
1359
+ than Q. Note that the number of heads in Q must be divisible by the number of heads in KV.
1360
+ For example, if Q has 6 heads and K, V have 2 heads, head 0, 1, 2 of Q will attention to head
1361
+ 0 of K, V, and head 3, 4, 5 of Q will attention to head 1 of K, V.
1362
+
1363
+ If causal=True, the causal mask is aligned to the bottom right corner of the attention matrix.
1364
+ For example, if seqlen_q = 2 and seqlen_k = 5, the causal mask (1 = keep, 0 = masked out) is:
1365
+ 1 1 1 1 0
1366
+ 1 1 1 1 1
1367
+ If seqlen_q = 5 and seqlen_k = 2, the causal mask is:
1368
+ 0 0
1369
+ 0 0
1370
+ 0 0
1371
+ 1 0
1372
+ 1 1
1373
+ If the row of the mask is all zero, the output will be zero.
1374
+
1375
+ If window_size != (-1, -1), implements sliding window local attention. Query at position i
1376
+ will only attend to keys between
1377
+ [i + seqlen_k - seqlen_q - window_size[0], i + seqlen_k - seqlen_q + window_size[1]] inclusive.
1378
+
1379
+ Arguments:
1380
+ q: (total_q, nheads, headdim), where total_q = total number of query tokens in the batch.
1381
+ k: (total_k, nheads_k, headdim), where total_k = total number of key tokens in the batch.
1382
+ v: (total_k, nheads_k, headdim), where total_k = total number of key tokens in the batch.
1383
+ cu_seqlens_q: (batch_size + 1,), dtype torch.int32. The cumulative sequence lengths
1384
+ of the sequences in the batch, used to index into q.
1385
+ cu_seqlens_k: (batch_size + 1,), dtype torch.int32. The cumulative sequence lengths
1386
+ of the sequences in the batch, used to index into kv.
1387
+ max_seqlen_q: int. Maximum query sequence length in the batch.
1388
+ max_seqlen_k: int. Maximum key sequence length in the batch.
1389
+ dropout_p: float. Dropout probability.
1390
+ softmax_scale: float. The scaling of QK^T before applying softmax.
1391
+ Default to 1 / sqrt(headdim).
1392
+ causal: bool. Whether to apply causal attention mask (e.g., for auto-regressive modeling).
1393
+ window_size: (left, right). If not (-1, -1), implements sliding window local attention.
1394
+ softcap: float. Anything > 0 activates softcapping attention.
1395
+ alibi_slopes: (nheads,) or (batch_size, nheads), fp32. A bias of
1396
+ (-alibi_slope * |i + seqlen_k - seqlen_q - j|)
1397
+ is added to the attention score of query i and key j.
1398
+ deterministic: bool. Whether to use the deterministic implementation of the backward pass,
1399
+ which is slightly slower and uses more memory. The forward pass is always deterministic.
1400
+ return_attn_probs: bool. Whether to return the attention probabilities. This option is for
1401
+ testing only. The returned probabilities are not guaranteed to be correct
1402
+ (they might not have the right scaling).
1403
+ Return:
1404
+ out: (total, nheads, headdim).
1405
+ softmax_lse [optional, if return_attn_probs=True]: (nheads, total_q_seqlen). The
1406
+ logsumexp of each row of the matrix QK^T * scaling (e.g., log of the softmax
1407
+ normalization factor).
1408
+ S_dmask [optional, if return_attn_probs=True]: (batch_size, nheads, seqlen, seqlen).
1409
+ The output of softmax (possibly with different scaling). It also encodes the dropout
1410
+ pattern (negative means that location was dropped, nonnegative means it was kept).
1411
+ """
1412
+ return FlashAttnVarlenFunc.apply(
1413
+ q,
1414
+ k,
1415
+ v,
1416
+ cu_seqlens_q,
1417
+ cu_seqlens_k,
1418
+ max_seqlen_q,
1419
+ max_seqlen_k,
1420
+ dropout_p,
1421
+ softmax_scale,
1422
+ causal,
1423
+ window_size,
1424
+ softcap,
1425
+ alibi_slopes,
1426
+ deterministic,
1427
+ return_attn_probs,
1428
+ block_table,
1429
+ )
1430
+
1431
+
1432
+ def flash_attn_with_kvcache(
1433
+ q,
1434
+ k_cache,
1435
+ v_cache,
1436
+ k=None,
1437
+ v=None,
1438
+ rotary_cos=None,
1439
+ rotary_sin=None,
1440
+ cache_seqlens: Optional[Union[(int, torch.Tensor)]] = None,
1441
+ cache_batch_idx: Optional[torch.Tensor] = None,
1442
+ cache_leftpad: Optional[torch.Tensor] = None,
1443
+ block_table: Optional[torch.Tensor] = None,
1444
+ softmax_scale=None,
1445
+ causal=False,
1446
+ window_size=(-1, -1), # -1 means infinite context window
1447
+ softcap=0.0, # 0.0 means deactivated
1448
+ rotary_interleaved=True,
1449
+ alibi_slopes=None,
1450
+ num_splits=0,
1451
+ return_softmax_lse=False,
1452
+ ):
1453
+ """
1454
+ If k and v are not None, k_cache and v_cache will be updated *inplace* with the new values from
1455
+ k and v. This is useful for incremental decoding: you can pass in the cached keys/values from
1456
+ the previous step, and update them with the new keys/values from the current step, and do
1457
+ attention with the updated cache, all in 1 kernel.
1458
+
1459
+ If you pass in k / v, you must make sure that the cache is large enough to hold the new values.
1460
+ For example, the KV cache could be pre-allocated with the max sequence length, and you can use
1461
+ cache_seqlens to keep track of the current sequence lengths of each sequence in the batch.
1462
+
1463
+ Also apply rotary embedding if rotary_cos and rotary_sin are passed in. The key @k will be
1464
+ rotated by rotary_cos and rotary_sin at indices cache_seqlens, cache_seqlens + 1, etc.
1465
+ If causal or local (i.e., window_size != (-1, -1)), the query @q will be rotated by rotary_cos
1466
+ and rotary_sin at indices cache_seqlens, cache_seqlens + 1, etc.
1467
+ If not causal and not local, the query @q will be rotated by rotary_cos and rotary_sin at
1468
+ indices cache_seqlens only (i.e. we consider all tokens in @q to be at position cache_seqlens).
1469
+
1470
+ See tests/test_flash_attn.py::test_flash_attn_kvcache for examples of how to use this function.
1471
+
1472
+ Supports multi-query and grouped-query attention (MQA/GQA) by passing in KV with fewer heads
1473
+ than Q. Note that the number of heads in Q must be divisible by the number of heads in KV.
1474
+ For example, if Q has 6 heads and K, V have 2 heads, head 0, 1, 2 of Q will attention to head
1475
+ 0 of K, V, and head 3, 4, 5 of Q will attention to head 1 of K, V.
1476
+
1477
+ If causal=True, the causal mask is aligned to the bottom right corner of the attention matrix.
1478
+ For example, if seqlen_q = 2 and seqlen_k = 5, the causal mask (1 = keep, 0 = masked out) is:
1479
+ 1 1 1 1 0
1480
+ 1 1 1 1 1
1481
+ If seqlen_q = 5 and seqlen_k = 2, the causal mask is:
1482
+ 0 0
1483
+ 0 0
1484
+ 0 0
1485
+ 1 0
1486
+ 1 1
1487
+ If the row of the mask is all zero, the output will be zero.
1488
+
1489
+ If window_size != (-1, -1), implements sliding window local attention. Query at position i
1490
+ will only attend to keys between
1491
+ [i + seqlen_k - seqlen_q - window_size[0], i + seqlen_k - seqlen_q + window_size[1]] inclusive.
1492
+
1493
+ Note: Does not support backward pass.
1494
+
1495
+ Arguments:
1496
+ q: (batch_size, seqlen, nheads, headdim)
1497
+ k_cache: (batch_size_cache, seqlen_cache, nheads_k, headdim) if there's no block_table,
1498
+ or (num_blocks, page_block_size, nheads_k, headdim) if there's a block_table (i.e. paged KV cache)
1499
+ page_block_size must be a multiple of 256.
1500
+ v_cache: (batch_size_cache, seqlen_cache, nheads_k, headdim) if there's no block_table,
1501
+ or (num_blocks, page_block_size, nheads_k, headdim) if there's a block_table (i.e. paged KV cache)
1502
+ k [optional]: (batch_size, seqlen_new, nheads_k, headdim). If not None, we concatenate
1503
+ k with k_cache, starting at the indices specified by cache_seqlens.
1504
+ v [optional]: (batch_size, seqlen_new, nheads_k, headdim). Similar to k.
1505
+ rotary_cos [optional]: (seqlen_ro, rotary_dim / 2). If not None, we apply rotary embedding
1506
+ to k and q. Only applicable if k and v are passed in. rotary_dim must be divisible by 16.
1507
+ rotary_sin [optional]: (seqlen_ro, rotary_dim / 2). Similar to rotary_cos.
1508
+ cache_seqlens: int, or (batch_size,), dtype torch.int32. The sequence lengths of the
1509
+ KV cache.
1510
+ cache_batch_idx: (batch_size,), dtype torch.int32. The indices used to index into the KV cache.
1511
+ If None, we assume that the batch indices are [0, 1, 2, ..., batch_size - 1].
1512
+ If the indices are not distinct, and k and v are provided, the values updated in the cache
1513
+ might come from any of the duplicate indices.
1514
+ cache_leftpad: (batch_size,), dtype torch.int32. The index that the KV cache starts. If None, assume 0.
1515
+ block_table [optional]: (batch_size, max_num_blocks_per_seq), dtype torch.int32.
1516
+ softmax_scale: float. The scaling of QK^T before applying softmax.
1517
+ Default to 1 / sqrt(headdim).
1518
+ causal: bool. Whether to apply causal attention mask (e.g., for auto-regressive modeling).
1519
+ window_size: (left, right). If not (-1, -1), implements sliding window local attention.
1520
+ softcap: float. Anything > 0 activates softcapping attention.
1521
+ rotary_interleaved: bool. Only applicable if rotary_cos and rotary_sin are passed in.
1522
+ If True, rotary embedding will combine dimensions 0 & 1, 2 & 3, etc. If False,
1523
+ rotary embedding will combine dimensions 0 & rotary_dim / 2, 1 & rotary_dim / 2 + 1
1524
+ (i.e. GPT-NeoX style).
1525
+ alibi_slopes: (nheads,) or (batch_size, nheads), fp32. A bias of
1526
+ (-alibi_slope * |i + seqlen_k - seqlen_q - j|)
1527
+ is added to the attention score of query i and key j.
1528
+ num_splits: int. If > 1, split the key/value into this many chunks along the sequence.
1529
+ If num_splits == 1, we don't split the key/value. If num_splits == 0, we use a heuristic
1530
+ to automatically determine the number of splits.
1531
+ Don't change this unless you know what you are doing.
1532
+ return_softmax_lse: bool. Whether to return the logsumexp of the attention scores.
1533
+
1534
+ Return:
1535
+ out: (batch_size, seqlen, nheads, headdim).
1536
+ softmax_lse [optional, if return_softmax_lse=True]: (batch_size, nheads, seqlen). The
1537
+ logsumexp of each row of the matrix QK^T * scaling (e.g., log of the softmax
1538
+ normalization factor).
1539
+ """
1540
+ assert k_cache.stride(-1) == 1, "k_cache must have contiguous last dimension"
1541
+ assert v_cache.stride(-1) == 1, "v_cache must have contiguous last dimension"
1542
+ q, k, v = [maybe_contiguous(x) for x in (q, k, v)]
1543
+ if softmax_scale is None:
1544
+ softmax_scale = q.shape[-1] ** (-0.5)
1545
+ if cache_seqlens is not None and isinstance(cache_seqlens, int):
1546
+ cache_seqlens = torch.full(
1547
+ (k_cache.shape[0],), cache_seqlens, dtype=torch.int32, device=k_cache.device
1548
+ )
1549
+ cache_seqlens = maybe_contiguous(cache_seqlens)
1550
+ cache_batch_idx = maybe_contiguous(cache_batch_idx)
1551
+ block_table = maybe_contiguous(block_table)
1552
+ out, softmax_lse = flash_attn_gpu.fwd_kvcache(
1553
+ q,
1554
+ k_cache,
1555
+ v_cache,
1556
+ k,
1557
+ v,
1558
+ cache_seqlens,
1559
+ rotary_cos,
1560
+ rotary_sin,
1561
+ cache_batch_idx,
1562
+ cache_leftpad,
1563
+ block_table,
1564
+ alibi_slopes,
1565
+ None,
1566
+ softmax_scale,
1567
+ causal,
1568
+ window_size[0],
1569
+ window_size[1],
1570
+ softcap,
1571
+ rotary_interleaved,
1572
+ num_splits,
1573
+ )
1574
+ return (out, softmax_lse) if return_softmax_lse else out
evalkit_tf446/lib/python3.10/site-packages/flash_attn/flash_attn_triton.py ADDED
@@ -0,0 +1,1160 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ *Experimental* implementation of FlashAttention in Triton.
3
+ Tested with triton==2.0.0.dev20221202.
4
+ Triton 2.0 has a new backend (MLIR) but seems like it doesn't yet work for head dimensions
5
+ other than 64:
6
+ https://github.com/openai/triton/blob/d376020f90002757eea3ea9475d4f7cfc2ec5ead/python/triton/ops/flash_attention.py#L207
7
+ We'll update this implementation with the new Triton backend once this is fixed.
8
+
9
+ We use the FlashAttention implementation from Phil Tillet a starting point.
10
+ https://github.com/openai/triton/blob/master/python/tutorials/06-fused-attention.py
11
+
12
+ Changes:
13
+ - Implement both causal and non-causal attention.
14
+ - Implement both self-attention and cross-attention.
15
+ - Support arbitrary seqlens (not just multiples of 128), for both forward and backward.
16
+ - Support all head dimensions up to 128 (not just 16, 32, 64, 128), for both forward and backward.
17
+ - Support attention bias.
18
+ - Speed up the forward pass a bit, and only store the LSE instead of m and l.
19
+ - Make the backward for d=128 much faster by reducing register spilling.
20
+ - Optionally parallelize the backward pass across seqlen_k, to deal with the case of
21
+ small batch size * nheads.
22
+
23
+ Caution:
24
+ - This is an *experimental* implementation. The forward pass should be quite robust but
25
+ I'm not 100% sure that the backward pass doesn't have race conditions (due to the Triton compiler).
26
+ - This implementation has only been tested on A100.
27
+ - If you plan to use headdim other than 64 and 128, you should test for race conditions
28
+ (due to the Triton compiler), as done in tests/test_flash_attn.py
29
+ "test_flash_attn_triton_race_condition". I've tested and fixed many race conditions
30
+ for different head dimensions (40, 48, 64, 128, 80, 88, 96), but I'm still not 100% confident
31
+ that there are none left for other head dimensions.
32
+
33
+ Differences between this Triton version and the CUDA version:
34
+ - Triton version doesn't support dropout.
35
+ - Triton forward is generally faster than CUDA forward, while Triton backward is
36
+ generally slower than CUDA backward. Overall Triton forward + backward is slightly slower
37
+ than CUDA forward + backward.
38
+ - Triton version doesn't support different sequence lengths in a batch (i.e., RaggedTensor/NestedTensor).
39
+ - Triton version supports attention bias, while CUDA version doesn't.
40
+ """
41
+
42
+ import math
43
+
44
+ import torch
45
+ import triton
46
+ import triton.language as tl
47
+
48
+
49
+ # Disabling autotune for now, set num_warps=4 if headdim=64 and num_warps=8 if headdim=128
50
+ # @triton.autotune(
51
+ # configs=[
52
+ # triton.Config({"BLOCK_M": 128, "BLOCK_N": 128}, num_warps=4, num_stages=1),
53
+ # # This config has a race condition when EVEN_M == False, disabling it for now.
54
+ # # triton.Config({"BLOCK_M": 64, "BLOCK_N": 64}, num_warps=4, num_stages=1),
55
+ # ],
56
+ # key=['CACHE_KEY_SEQLEN_Q', 'CACHE_KEY_SEQLEN_K', 'BIAS_TYPE', 'IS_CAUSAL', 'BLOCK_HEADDIM']
57
+ # )
58
+ @triton.heuristics(
59
+ {
60
+ "EVEN_M": lambda args: args["seqlen_q"] % args["BLOCK_M"] == 0,
61
+ "EVEN_N": lambda args: args["seqlen_k"] % args["BLOCK_N"] == 0,
62
+ "EVEN_HEADDIM": lambda args: args["headdim"] == args["BLOCK_HEADDIM"],
63
+ }
64
+ )
65
+ @triton.jit
66
+ def _fwd_kernel(
67
+ Q,
68
+ K,
69
+ V,
70
+ Bias,
71
+ Out,
72
+ Lse,
73
+ TMP, # NOTE: TMP is a scratchpad buffer to workaround a compiler bug
74
+ softmax_scale,
75
+ stride_qb,
76
+ stride_qh,
77
+ stride_qm,
78
+ stride_kb,
79
+ stride_kh,
80
+ stride_kn,
81
+ stride_vb,
82
+ stride_vh,
83
+ stride_vn,
84
+ stride_bb,
85
+ stride_bh,
86
+ stride_bm,
87
+ stride_ob,
88
+ stride_oh,
89
+ stride_om,
90
+ nheads,
91
+ seqlen_q,
92
+ seqlen_k,
93
+ seqlen_q_rounded,
94
+ headdim,
95
+ CACHE_KEY_SEQLEN_Q,
96
+ CACHE_KEY_SEQLEN_K,
97
+ BIAS_TYPE: tl.constexpr,
98
+ IS_CAUSAL: tl.constexpr,
99
+ BLOCK_HEADDIM: tl.constexpr,
100
+ EVEN_M: tl.constexpr,
101
+ EVEN_N: tl.constexpr,
102
+ EVEN_HEADDIM: tl.constexpr,
103
+ BLOCK_M: tl.constexpr,
104
+ BLOCK_N: tl.constexpr,
105
+ ):
106
+ start_m = tl.program_id(0)
107
+ off_hb = tl.program_id(1)
108
+ off_b = off_hb // nheads
109
+ off_h = off_hb % nheads
110
+ # off_b = tl.program_id(1)
111
+ # off_h = tl.program_id(2)
112
+ # off_hb = off_b * nheads + off_h
113
+ # initialize offsets
114
+ offs_m = start_m * BLOCK_M + tl.arange(0, BLOCK_M)
115
+ offs_n = tl.arange(0, BLOCK_N)
116
+ offs_d = tl.arange(0, BLOCK_HEADDIM)
117
+ # Initialize pointers to Q, K, V
118
+ # Adding parenthesis around indexing might use int32 math instead of int64 math?
119
+ # https://github.com/openai/triton/issues/741
120
+ # I'm seeing a tiny bit of difference (5-7us)
121
+ q_ptrs = (
122
+ Q + off_b * stride_qb + off_h * stride_qh + (offs_m[:, None] * stride_qm + offs_d[None, :])
123
+ )
124
+ k_ptrs = (
125
+ K + off_b * stride_kb + off_h * stride_kh + (offs_n[:, None] * stride_kn + offs_d[None, :])
126
+ )
127
+ v_ptrs = (
128
+ V + off_b * stride_vb + off_h * stride_vh + (offs_n[:, None] * stride_vn + offs_d[None, :])
129
+ )
130
+ if BIAS_TYPE == "vector":
131
+ b_ptrs = Bias + off_b * stride_bb + off_h * stride_bh + offs_n
132
+ elif BIAS_TYPE == "matrix":
133
+ b_ptrs = (
134
+ Bias
135
+ + off_b * stride_bb
136
+ + off_h * stride_bh
137
+ + (offs_m[:, None] * stride_bm + offs_n[None, :])
138
+ )
139
+ # initialize pointer to m and l
140
+ t_ptrs = TMP + off_hb * seqlen_q_rounded + offs_m
141
+ lse_i = tl.zeros([BLOCK_M], dtype=tl.float32) - float("inf")
142
+ m_i = tl.zeros([BLOCK_M], dtype=tl.float32) - float("inf")
143
+ acc_o = tl.zeros([BLOCK_M, BLOCK_HEADDIM], dtype=tl.float32)
144
+ # load q: it will stay in SRAM throughout
145
+ # [2022-10-30] TD: Triton bug - in the case of EVEN_M=True and EVEN_N=False, if we just call
146
+ # tl.load(q_ptrs), we get the wrong output!
147
+ if EVEN_M & EVEN_N:
148
+ if EVEN_HEADDIM:
149
+ q = tl.load(q_ptrs)
150
+ else:
151
+ q = tl.load(q_ptrs, mask=offs_d[None, :] < headdim, other=0.0)
152
+ else:
153
+ if EVEN_HEADDIM:
154
+ q = tl.load(q_ptrs, mask=offs_m[:, None] < seqlen_q, other=0.0)
155
+ else:
156
+ q = tl.load(
157
+ q_ptrs, mask=(offs_m[:, None] < seqlen_q) & (offs_d[None, :] < headdim), other=0.0
158
+ )
159
+ # loop over k, v and update accumulator
160
+ end_n = seqlen_k if not IS_CAUSAL else tl.minimum((start_m + 1) * BLOCK_M, seqlen_k)
161
+ for start_n in range(0, end_n, BLOCK_N):
162
+ start_n = tl.multiple_of(start_n, BLOCK_N)
163
+ # -- compute qk ----
164
+ if EVEN_N & EVEN_M: # If we just do "if EVEN_N", there seems to be some race condition
165
+ if EVEN_HEADDIM:
166
+ k = tl.load(k_ptrs + start_n * stride_kn)
167
+ else:
168
+ k = tl.load(k_ptrs + start_n * stride_kn, mask=offs_d[None, :] < headdim, other=0.0)
169
+ else:
170
+ if EVEN_HEADDIM:
171
+ k = tl.load(
172
+ k_ptrs + start_n * stride_kn,
173
+ mask=(start_n + offs_n)[:, None] < seqlen_k,
174
+ other=0.0,
175
+ )
176
+ else:
177
+ k = tl.load(
178
+ k_ptrs + start_n * stride_kn,
179
+ mask=((start_n + offs_n)[:, None] < seqlen_k) & (offs_d[None, :] < headdim),
180
+ other=0.0,
181
+ )
182
+ qk = tl.zeros([BLOCK_M, BLOCK_N], dtype=tl.float32)
183
+ qk += tl.dot(q, k, trans_b=True)
184
+ # Trying to combine the two masks seem to make the result wrong
185
+ if not EVEN_N: # Need to mask out otherwise the softmax is wrong
186
+ qk += tl.where((start_n + offs_n)[None, :] < seqlen_k, 0, float("-inf"))
187
+ if IS_CAUSAL:
188
+ qk += tl.where(offs_m[:, None] >= (start_n + offs_n)[None, :], 0, float("-inf"))
189
+ if BIAS_TYPE != "none":
190
+ if BIAS_TYPE == "vector":
191
+ if EVEN_N:
192
+ bias = tl.load(b_ptrs + start_n).to(tl.float32)
193
+ else:
194
+ bias = tl.load(
195
+ b_ptrs + start_n, mask=(start_n + offs_n) < seqlen_k, other=0.0
196
+ ).to(tl.float32)
197
+ bias = bias[None, :]
198
+ elif BIAS_TYPE == "matrix":
199
+ if EVEN_M & EVEN_N:
200
+ bias = tl.load(b_ptrs + start_n).to(tl.float32)
201
+ else:
202
+ bias = tl.load(
203
+ b_ptrs + start_n,
204
+ mask=(offs_m[:, None] < seqlen_q)
205
+ & ((start_n + offs_n)[None, :] < seqlen_k),
206
+ other=0.0,
207
+ ).to(tl.float32)
208
+ # Slightly faster to multiply the softmax_scale in the tl.exp below since the compiler
209
+ # can then fuse the mult and add into an fma instruction. But if we have bias we need to
210
+ # to multiply with softmax_scale here.
211
+ qk = qk * softmax_scale + bias
212
+ m_ij = tl.maximum(tl.max(qk, 1), lse_i)
213
+ p = tl.exp(qk - m_ij[:, None])
214
+ else:
215
+ m_ij = tl.maximum(tl.max(qk, 1) * softmax_scale, lse_i)
216
+ p = tl.exp(qk * softmax_scale - m_ij[:, None])
217
+ l_ij = tl.sum(p, 1)
218
+
219
+ # scale acc_o
220
+ acc_o_scale = tl.exp(m_i - m_ij)
221
+
222
+ # # -- update output accumulator --
223
+ # BUG: have to store and immediately load
224
+ tl.store(t_ptrs, acc_o_scale)
225
+ acc_o_scale = tl.load(t_ptrs)
226
+ acc_o = acc_o * acc_o_scale[:, None]
227
+ # update acc_o
228
+ if EVEN_N & EVEN_M: # If we just do "if EVEN_N", there seems to be some race condition
229
+ if EVEN_HEADDIM:
230
+ v = tl.load(v_ptrs + start_n * stride_vn)
231
+ else:
232
+ v = tl.load(v_ptrs + start_n * stride_vn, mask=offs_d[None, :] < headdim, other=0.0)
233
+ else:
234
+ if EVEN_HEADDIM:
235
+ v = tl.load(
236
+ v_ptrs + start_n * stride_vn,
237
+ mask=(start_n + offs_n)[:, None] < seqlen_k,
238
+ other=0.0,
239
+ )
240
+ else:
241
+ v = tl.load(
242
+ v_ptrs + start_n * stride_vn,
243
+ mask=((start_n + offs_n)[:, None] < seqlen_k) & (offs_d[None, :] < headdim),
244
+ other=0.0,
245
+ )
246
+ p = p.to(v.dtype)
247
+ acc_o += tl.dot(p, v)
248
+
249
+ # -- update statistics
250
+ m_i = m_ij
251
+ l_i_new = tl.exp(lse_i - m_ij) + l_ij
252
+ lse_i = m_ij + tl.log(l_i_new)
253
+
254
+ o_scale = tl.exp(m_i - lse_i)
255
+ # BUG: have to store and immediately load
256
+ tl.store(t_ptrs, o_scale)
257
+ o_scale = tl.load(t_ptrs)
258
+ acc_o = acc_o * o_scale[:, None]
259
+ # rematerialize offsets to save registers
260
+ start_m = tl.program_id(0)
261
+ offs_m = start_m * BLOCK_M + tl.arange(0, BLOCK_M)
262
+ # write back l and m
263
+ lse_ptrs = Lse + off_hb * seqlen_q_rounded + offs_m
264
+ tl.store(lse_ptrs, lse_i)
265
+ # initialize pointers to output
266
+ offs_d = tl.arange(0, BLOCK_HEADDIM)
267
+ out_ptrs = (
268
+ Out
269
+ + off_b * stride_ob
270
+ + off_h * stride_oh
271
+ + (offs_m[:, None] * stride_om + offs_d[None, :])
272
+ )
273
+ if EVEN_M:
274
+ if EVEN_HEADDIM:
275
+ tl.store(out_ptrs, acc_o)
276
+ else:
277
+ tl.store(out_ptrs, acc_o, mask=offs_d[None, :] < headdim)
278
+ else:
279
+ if EVEN_HEADDIM:
280
+ tl.store(out_ptrs, acc_o, mask=offs_m[:, None] < seqlen_q)
281
+ else:
282
+ tl.store(
283
+ out_ptrs, acc_o, mask=(offs_m[:, None] < seqlen_q) & (offs_d[None, :] < headdim)
284
+ )
285
+
286
+
287
+ @triton.jit
288
+ def _bwd_preprocess_do_o_dot(
289
+ Out,
290
+ DO,
291
+ Delta,
292
+ stride_ob,
293
+ stride_oh,
294
+ stride_om,
295
+ stride_dob,
296
+ stride_doh,
297
+ stride_dom,
298
+ nheads,
299
+ seqlen_q,
300
+ seqlen_q_rounded,
301
+ headdim,
302
+ BLOCK_M: tl.constexpr,
303
+ BLOCK_HEADDIM: tl.constexpr,
304
+ ):
305
+ start_m = tl.program_id(0)
306
+ off_hb = tl.program_id(1)
307
+ off_b = off_hb // nheads
308
+ off_h = off_hb % nheads
309
+ # initialize offsets
310
+ offs_m = start_m * BLOCK_M + tl.arange(0, BLOCK_M)
311
+ offs_d = tl.arange(0, BLOCK_HEADDIM)
312
+ # load
313
+ o = tl.load(
314
+ Out + off_b * stride_ob + off_h * stride_oh + offs_m[:, None] * stride_om + offs_d[None, :],
315
+ mask=(offs_m[:, None] < seqlen_q) & (offs_d[None, :] < headdim),
316
+ other=0.0,
317
+ ).to(tl.float32)
318
+ do = tl.load(
319
+ DO
320
+ + off_b * stride_dob
321
+ + off_h * stride_doh
322
+ + offs_m[:, None] * stride_dom
323
+ + offs_d[None, :],
324
+ mask=(offs_m[:, None] < seqlen_q) & (offs_d[None, :] < headdim),
325
+ other=0.0,
326
+ ).to(tl.float32)
327
+ delta = tl.sum(o * do, axis=1)
328
+ # write-back
329
+ tl.store(Delta + off_hb * seqlen_q_rounded + offs_m, delta)
330
+
331
+
332
+ @triton.jit
333
+ def _bwd_store_dk_dv(
334
+ dk_ptrs,
335
+ dv_ptrs,
336
+ dk,
337
+ dv,
338
+ offs_n,
339
+ offs_d,
340
+ seqlen_k,
341
+ headdim,
342
+ EVEN_M: tl.constexpr,
343
+ EVEN_N: tl.constexpr,
344
+ EVEN_HEADDIM: tl.constexpr,
345
+ ):
346
+ # [2022-11-01] TD: Same bug. In the case of EVEN_N=True and EVEN_M=False,
347
+ # if we just call tl.store(dv_ptrs), there's a race condition
348
+ if EVEN_N & EVEN_M:
349
+ if EVEN_HEADDIM:
350
+ tl.store(dv_ptrs, dv)
351
+ tl.store(dk_ptrs, dk)
352
+ else:
353
+ tl.store(dv_ptrs, dv, mask=offs_d[None, :] < headdim)
354
+ tl.store(dk_ptrs, dk, mask=offs_d[None, :] < headdim)
355
+ else:
356
+ if EVEN_HEADDIM:
357
+ tl.store(dv_ptrs, dv, mask=offs_n[:, None] < seqlen_k)
358
+ tl.store(dk_ptrs, dk, mask=offs_n[:, None] < seqlen_k)
359
+ else:
360
+ tl.store(dv_ptrs, dv, mask=(offs_n[:, None] < seqlen_k) & (offs_d[None, :] < headdim))
361
+ tl.store(dk_ptrs, dk, mask=(offs_n[:, None] < seqlen_k) & (offs_d[None, :] < headdim))
362
+
363
+
364
+ @triton.jit
365
+ def _bwd_kernel_one_col_block(
366
+ start_n,
367
+ Q,
368
+ K,
369
+ V,
370
+ Bias,
371
+ DO,
372
+ DQ,
373
+ DK,
374
+ DV,
375
+ LSE,
376
+ D,
377
+ softmax_scale,
378
+ stride_qm,
379
+ stride_kn,
380
+ stride_vn,
381
+ stride_bm,
382
+ stride_dom,
383
+ stride_dqm,
384
+ stride_dkn,
385
+ stride_dvn,
386
+ seqlen_q,
387
+ seqlen_k,
388
+ headdim,
389
+ ATOMIC_ADD: tl.constexpr,
390
+ BIAS_TYPE: tl.constexpr,
391
+ IS_CAUSAL: tl.constexpr,
392
+ BLOCK_HEADDIM: tl.constexpr,
393
+ EVEN_M: tl.constexpr,
394
+ EVEN_N: tl.constexpr,
395
+ EVEN_HEADDIM: tl.constexpr,
396
+ BLOCK_M: tl.constexpr,
397
+ BLOCK_N: tl.constexpr,
398
+ ):
399
+ # We need to make sure begin_m is a multiple of BLOCK_M (not BLOCK_N)
400
+ begin_m = 0 if not IS_CAUSAL else ((start_n * BLOCK_N) // BLOCK_M) * BLOCK_M
401
+ # initialize row/col offsets
402
+ offs_qm = begin_m + tl.arange(0, BLOCK_M)
403
+ offs_n = start_n * BLOCK_N + tl.arange(0, BLOCK_N)
404
+ offs_m = tl.arange(0, BLOCK_M)
405
+ offs_d = tl.arange(0, BLOCK_HEADDIM)
406
+ # initialize pointers to value-like data
407
+ q_ptrs = Q + (offs_qm[:, None] * stride_qm + offs_d[None, :])
408
+ k_ptrs = K + (offs_n[:, None] * stride_kn + offs_d[None, :])
409
+ v_ptrs = V + (offs_n[:, None] * stride_vn + offs_d[None, :])
410
+ do_ptrs = DO + (offs_qm[:, None] * stride_dom + offs_d[None, :])
411
+ dq_ptrs = DQ + (offs_qm[:, None] * stride_dqm + offs_d[None, :])
412
+ if BIAS_TYPE == "vector":
413
+ b_ptrs = Bias + offs_n
414
+ elif BIAS_TYPE == "matrix":
415
+ b_ptrs = Bias + (offs_qm[:, None] * stride_bm + offs_n[None, :])
416
+ # initialize dv and dk
417
+ dv = tl.zeros([BLOCK_N, BLOCK_HEADDIM], dtype=tl.float32)
418
+ dk = tl.zeros([BLOCK_N, BLOCK_HEADDIM], dtype=tl.float32)
419
+ # There seems to be some problem with Triton pipelining that makes results wrong for
420
+ # headdim=64, seqlen=(113, 255), bias_type='matrix'. In this case the for loop
421
+ # may have zero step, and pipelining with the bias matrix could screw it up.
422
+ # So we just exit early.
423
+ if begin_m >= seqlen_q:
424
+ dv_ptrs = DV + (offs_n[:, None] * stride_dvn + offs_d[None, :])
425
+ dk_ptrs = DK + (offs_n[:, None] * stride_dkn + offs_d[None, :])
426
+ _bwd_store_dk_dv(
427
+ dk_ptrs,
428
+ dv_ptrs,
429
+ dk,
430
+ dv,
431
+ offs_n,
432
+ offs_d,
433
+ seqlen_k,
434
+ headdim,
435
+ EVEN_M=EVEN_M,
436
+ EVEN_N=EVEN_N,
437
+ EVEN_HEADDIM=EVEN_HEADDIM,
438
+ )
439
+ return
440
+ # k and v stay in SRAM throughout
441
+ # [2022-10-30] TD: Same bug as the fwd. In the case of EVEN_N=True and EVEN_M=False,
442
+ # if we just call tl.load(k_ptrs), we get the wrong output!
443
+ if EVEN_N & EVEN_M:
444
+ if EVEN_HEADDIM:
445
+ k = tl.load(k_ptrs)
446
+ v = tl.load(v_ptrs)
447
+ else:
448
+ k = tl.load(k_ptrs, mask=offs_d[None, :] < headdim, other=0.0)
449
+ v = tl.load(v_ptrs, mask=offs_d[None, :] < headdim, other=0.0)
450
+ else:
451
+ if EVEN_HEADDIM:
452
+ k = tl.load(k_ptrs, mask=offs_n[:, None] < seqlen_k, other=0.0)
453
+ v = tl.load(v_ptrs, mask=offs_n[:, None] < seqlen_k, other=0.0)
454
+ else:
455
+ k = tl.load(
456
+ k_ptrs, mask=(offs_n[:, None] < seqlen_k) & (offs_d[None, :] < headdim), other=0.0
457
+ )
458
+ v = tl.load(
459
+ v_ptrs, mask=(offs_n[:, None] < seqlen_k) & (offs_d[None, :] < headdim), other=0.0
460
+ )
461
+ # loop over rows
462
+ num_block_m = tl.cdiv(seqlen_q, BLOCK_M)
463
+ for start_m in range(begin_m, num_block_m * BLOCK_M, BLOCK_M):
464
+ start_m = tl.multiple_of(start_m, BLOCK_M)
465
+ offs_m_curr = start_m + offs_m
466
+ # load q, k, v, do on-chip
467
+ # Same bug as below. Otherwise gives wrong result for headdim=40, seqlen=(128, 117)
468
+ if EVEN_M & EVEN_HEADDIM:
469
+ q = tl.load(q_ptrs)
470
+ else:
471
+ if EVEN_HEADDIM:
472
+ q = tl.load(q_ptrs, mask=offs_m_curr[:, None] < seqlen_q, other=0.0)
473
+ else:
474
+ q = tl.load(
475
+ q_ptrs,
476
+ mask=(offs_m_curr[:, None] < seqlen_q) & (offs_d[None, :] < headdim),
477
+ other=0.0,
478
+ )
479
+ # recompute p = softmax(qk, dim=-1).T
480
+ qk = tl.dot(q, k, trans_b=True)
481
+ # Trying to combine the two masks seem to make the result wrong
482
+ if not EVEN_N: # Need to mask out otherwise the softmax is wrong
483
+ qk = tl.where(offs_n[None, :] < seqlen_k, qk, float("-inf"))
484
+ if IS_CAUSAL:
485
+ qk = tl.where(offs_m_curr[:, None] >= (offs_n[None, :]), qk, float("-inf"))
486
+ if BIAS_TYPE != "none":
487
+ tl.debug_barrier() # Race condition otherwise
488
+ if BIAS_TYPE == "vector":
489
+ if EVEN_N:
490
+ bias = tl.load(b_ptrs).to(tl.float32)
491
+ else:
492
+ bias = tl.load(b_ptrs, mask=offs_n < seqlen_k, other=0.0).to(tl.float32)
493
+ bias = bias[None, :]
494
+ elif BIAS_TYPE == "matrix":
495
+ if EVEN_M & EVEN_N:
496
+ bias = tl.load(b_ptrs).to(tl.float32)
497
+ else:
498
+ bias = tl.load(
499
+ b_ptrs,
500
+ mask=(offs_m_curr[:, None] < seqlen_q) & (offs_n[None, :] < seqlen_k),
501
+ other=0.0,
502
+ ).to(tl.float32)
503
+ qk = qk * softmax_scale + bias
504
+ # There seems to be a race condition when headdim=48/96, and dq, dk, dv are wrong.
505
+ # Also wrong for headdim=64.
506
+ if not (EVEN_M & EVEN_HEADDIM):
507
+ tl.debug_barrier()
508
+ lse_i = tl.load(LSE + offs_m_curr)
509
+ if BIAS_TYPE == "none":
510
+ p = tl.exp(qk * softmax_scale - lse_i[:, None])
511
+ else:
512
+ p = tl.exp(qk - lse_i[:, None])
513
+ # compute dv
514
+ # [2022-10-30] TD: A Triton bug: if EVEN_M=True and EVEN_HEADDIM=False, if we call
515
+ # do = tl.load(do_ptrs, mask=offs_d[None, :] < headdim, other=0.0), we get wrong outputs
516
+ # in the case of headdim=48/96, seqlen_q & seqlen_k >= 512. If headdim=40 or seqlen < 512,
517
+ # the output is correct.
518
+ if EVEN_M & EVEN_HEADDIM:
519
+ do = tl.load(do_ptrs)
520
+ else:
521
+ # [2022-11-01] TD: Triton bug, there's a race condition if we just use m_mask and not d_mask.
522
+ do = tl.load(
523
+ do_ptrs,
524
+ mask=(offs_m_curr[:, None] < seqlen_q) & (offs_d[None, :] < headdim),
525
+ other=0.0,
526
+ )
527
+ # if EVEN_M:
528
+ # if EVEN_HEADDIM:
529
+ # do = tl.load(do_ptrs)
530
+ # else:
531
+ # do = tl.load(do_ptrs, mask=offs_d[None, :] < headdim, other=0.0)
532
+ # else:
533
+ # if EVEN_HEADDIM:
534
+ # do = tl.load(do_ptrs, mask=offs_m_curr[:, None] < seqlen_q, other=0.0)
535
+ # else:
536
+ # do = tl.load(do_ptrs, mask=(offs_m_curr[:, None] < seqlen_q)
537
+ # & (offs_d[None, :] < headdim), other=0.0)
538
+ dv += tl.dot(p.to(do.dtype), do, trans_a=True)
539
+ # compute dp = dot(v, do)
540
+ # There seems to be a race condition when headdim=48/96, and dq, dk are wrong.
541
+ # Also wrong for headdim=128, seqlen=(108, 256), and ATOMIC_ADD=True
542
+ # Also wrong for headdim=64, seqlen=(1023, 1024), and ATOMIC_ADD=False
543
+ if not (EVEN_M & EVEN_HEADDIM):
544
+ tl.debug_barrier()
545
+ dp = tl.dot(do, v, trans_b=True)
546
+ # There's a race condition for headdim=48
547
+ if not EVEN_HEADDIM:
548
+ tl.debug_barrier()
549
+ # compute ds = p * (dp - delta[:, None])
550
+ # Putting the subtraction after the dp matmul (instead of before) is slightly faster
551
+ Di = tl.load(D + offs_m_curr)
552
+ # Converting ds to q.dtype here reduces register pressure and makes it much faster
553
+ # for BLOCK_HEADDIM=128
554
+ ds = (p * (dp - Di[:, None]) * softmax_scale).to(q.dtype)
555
+ # compute dk = dot(ds.T, q)
556
+ dk += tl.dot(ds, q, trans_a=True)
557
+ # compute dq
558
+ if not (
559
+ EVEN_M & EVEN_HEADDIM
560
+ ): # Otherewise there's a race condition when BIAS_TYPE='matrix'
561
+ tl.debug_barrier()
562
+ if not ATOMIC_ADD:
563
+ if EVEN_M & EVEN_HEADDIM: # Race condition if we just do EVEN_M
564
+ dq = tl.load(dq_ptrs, eviction_policy="evict_last")
565
+ dq += tl.dot(ds, k)
566
+ tl.store(dq_ptrs, dq, eviction_policy="evict_last")
567
+ else:
568
+ if EVEN_HEADDIM:
569
+ dq = tl.load(
570
+ dq_ptrs,
571
+ mask=offs_m_curr[:, None] < seqlen_q,
572
+ other=0.0,
573
+ eviction_policy="evict_last",
574
+ )
575
+ dq += tl.dot(ds, k)
576
+ tl.store(
577
+ dq_ptrs,
578
+ dq,
579
+ mask=offs_m_curr[:, None] < seqlen_q,
580
+ eviction_policy="evict_last",
581
+ )
582
+ else:
583
+ dq = tl.load(
584
+ dq_ptrs,
585
+ mask=(offs_m_curr[:, None] < seqlen_q) & (offs_d[None, :] < headdim),
586
+ other=0.0,
587
+ eviction_policy="evict_last",
588
+ )
589
+ dq += tl.dot(ds, k)
590
+ tl.store(
591
+ dq_ptrs,
592
+ dq,
593
+ mask=(offs_m_curr[:, None] < seqlen_q) & (offs_d[None, :] < headdim),
594
+ eviction_policy="evict_last",
595
+ )
596
+ else: # If we're parallelizing across the seqlen_k dimension
597
+ dq = tl.dot(ds, k)
598
+ if EVEN_M & EVEN_HEADDIM: # Race condition if we just do EVEN_M
599
+ tl.atomic_add(dq_ptrs, dq)
600
+ else:
601
+ if EVEN_HEADDIM:
602
+ tl.atomic_add(dq_ptrs, dq, mask=offs_m_curr[:, None] < seqlen_q)
603
+ else:
604
+ tl.atomic_add(
605
+ dq_ptrs,
606
+ dq,
607
+ mask=(offs_m_curr[:, None] < seqlen_q) & (offs_d[None, :] < headdim),
608
+ )
609
+ # increment pointers
610
+ dq_ptrs += BLOCK_M * stride_dqm
611
+ q_ptrs += BLOCK_M * stride_qm
612
+ do_ptrs += BLOCK_M * stride_dom
613
+ if BIAS_TYPE == "matrix":
614
+ b_ptrs += BLOCK_M * stride_bm
615
+ # write-back
616
+ dv_ptrs = DV + (offs_n[:, None] * stride_dvn + offs_d[None, :])
617
+ dk_ptrs = DK + (offs_n[:, None] * stride_dkn + offs_d[None, :])
618
+ _bwd_store_dk_dv(
619
+ dk_ptrs,
620
+ dv_ptrs,
621
+ dk,
622
+ dv,
623
+ offs_n,
624
+ offs_d,
625
+ seqlen_k,
626
+ headdim,
627
+ EVEN_M=EVEN_M,
628
+ EVEN_N=EVEN_N,
629
+ EVEN_HEADDIM=EVEN_HEADDIM,
630
+ )
631
+
632
+
633
+ def init_to_zero(name):
634
+ return lambda nargs: nargs[name].zero_()
635
+
636
+
637
+ @triton.autotune(
638
+ configs=[
639
+ triton.Config(
640
+ {"BLOCK_M": 128, "BLOCK_N": 128, "SEQUENCE_PARALLEL": False},
641
+ num_warps=8,
642
+ num_stages=1,
643
+ pre_hook=init_to_zero("DQ"),
644
+ ),
645
+ triton.Config(
646
+ {"BLOCK_M": 128, "BLOCK_N": 128, "SEQUENCE_PARALLEL": True},
647
+ num_warps=8,
648
+ num_stages=1,
649
+ pre_hook=init_to_zero("DQ"),
650
+ ),
651
+ # Other configs seem to give wrong results when seqlen_q % 128 != 0, disabling them for now
652
+ # # Kernel is buggy (give wrong result) if we set BLOCK_m=128, BLOCK_n=64, num_warps=*4*
653
+ # triton.Config({"BLOCK_M": 128, "BLOCK_N": 64, "SEQUENCE_PARALLEL": False}, num_warps=8, num_stages=1, pre_hook=init_to_zero('DQ')),
654
+ # triton.Config({"BLOCK_M": 128, "BLOCK_N": 64, "SEQUENCE_PARALLEL": True}, num_warps=8, num_stages=1, pre_hook=init_to_zero('DQ')),
655
+ # triton.Config({"BLOCK_M": 64, "BLOCK_N": 64, "SEQUENCE_PARALLEL": False}, num_warps=4, num_stages=1, pre_hook=init_to_zero('DQ')),
656
+ # triton.Config({"BLOCK_M": 64, "BLOCK_N": 64, "SEQUENCE_PARALLEL": True}, num_warps=4, num_stages=1, pre_hook=init_to_zero('DQ')),
657
+ ],
658
+ key=["CACHE_KEY_SEQLEN_Q", "CACHE_KEY_SEQLEN_K", "BIAS_TYPE", "IS_CAUSAL", "BLOCK_HEADDIM"],
659
+ )
660
+ @triton.heuristics(
661
+ {
662
+ "EVEN_M": lambda args: args["seqlen_q"] % args["BLOCK_M"] == 0,
663
+ "EVEN_N": lambda args: args["seqlen_k"] % args["BLOCK_N"] == 0,
664
+ "EVEN_HEADDIM": lambda args: args["headdim"] == args["BLOCK_HEADDIM"],
665
+ }
666
+ )
667
+ @triton.jit
668
+ def _bwd_kernel(
669
+ Q,
670
+ K,
671
+ V,
672
+ Bias,
673
+ DO,
674
+ DQ,
675
+ DK,
676
+ DV,
677
+ LSE,
678
+ D,
679
+ softmax_scale,
680
+ stride_qb,
681
+ stride_qh,
682
+ stride_qm,
683
+ stride_kb,
684
+ stride_kh,
685
+ stride_kn,
686
+ stride_vb,
687
+ stride_vh,
688
+ stride_vn,
689
+ stride_bb,
690
+ stride_bh,
691
+ stride_bm,
692
+ stride_dob,
693
+ stride_doh,
694
+ stride_dom,
695
+ stride_dqb,
696
+ stride_dqh,
697
+ stride_dqm,
698
+ stride_dkb,
699
+ stride_dkh,
700
+ stride_dkn,
701
+ stride_dvb,
702
+ stride_dvh,
703
+ stride_dvn,
704
+ nheads,
705
+ seqlen_q,
706
+ seqlen_k,
707
+ seqlen_q_rounded,
708
+ headdim,
709
+ CACHE_KEY_SEQLEN_Q,
710
+ CACHE_KEY_SEQLEN_K,
711
+ BIAS_TYPE: tl.constexpr,
712
+ IS_CAUSAL: tl.constexpr,
713
+ BLOCK_HEADDIM: tl.constexpr,
714
+ SEQUENCE_PARALLEL: tl.constexpr,
715
+ EVEN_M: tl.constexpr,
716
+ EVEN_N: tl.constexpr,
717
+ EVEN_HEADDIM: tl.constexpr,
718
+ BLOCK_M: tl.constexpr,
719
+ BLOCK_N: tl.constexpr,
720
+ ):
721
+ off_hb = tl.program_id(1)
722
+ off_b = off_hb // nheads
723
+ off_h = off_hb % nheads
724
+ # offset pointers for batch/head
725
+ Q += off_b * stride_qb + off_h * stride_qh
726
+ K += off_b * stride_kb + off_h * stride_kh
727
+ V += off_b * stride_vb + off_h * stride_vh
728
+ DO += off_b * stride_dob + off_h * stride_doh
729
+ DQ += off_b * stride_dqb + off_h * stride_dqh
730
+ DK += off_b * stride_dkb + off_h * stride_dkh
731
+ DV += off_b * stride_dvb + off_h * stride_dvh
732
+ if BIAS_TYPE != "none":
733
+ Bias += off_b * stride_bb + off_h * stride_bh
734
+ # pointer to row-wise quantities in value-like data
735
+ D += off_hb * seqlen_q_rounded
736
+ LSE += off_hb * seqlen_q_rounded
737
+ if not SEQUENCE_PARALLEL:
738
+ num_block_n = tl.cdiv(seqlen_k, BLOCK_N)
739
+ for start_n in range(0, num_block_n):
740
+ _bwd_kernel_one_col_block(
741
+ start_n,
742
+ Q,
743
+ K,
744
+ V,
745
+ Bias,
746
+ DO,
747
+ DQ,
748
+ DK,
749
+ DV,
750
+ LSE,
751
+ D,
752
+ softmax_scale,
753
+ stride_qm,
754
+ stride_kn,
755
+ stride_vn,
756
+ stride_bm,
757
+ stride_dom,
758
+ stride_dqm,
759
+ stride_dkn,
760
+ stride_dvn,
761
+ seqlen_q,
762
+ seqlen_k,
763
+ headdim,
764
+ ATOMIC_ADD=False,
765
+ BIAS_TYPE=BIAS_TYPE,
766
+ IS_CAUSAL=IS_CAUSAL,
767
+ BLOCK_HEADDIM=BLOCK_HEADDIM,
768
+ EVEN_M=EVEN_M,
769
+ EVEN_N=EVEN_N,
770
+ EVEN_HEADDIM=EVEN_HEADDIM,
771
+ BLOCK_M=BLOCK_M,
772
+ BLOCK_N=BLOCK_N,
773
+ )
774
+ else:
775
+ start_n = tl.program_id(0)
776
+ _bwd_kernel_one_col_block(
777
+ start_n,
778
+ Q,
779
+ K,
780
+ V,
781
+ Bias,
782
+ DO,
783
+ DQ,
784
+ DK,
785
+ DV,
786
+ LSE,
787
+ D,
788
+ softmax_scale,
789
+ stride_qm,
790
+ stride_kn,
791
+ stride_vn,
792
+ stride_bm,
793
+ stride_dom,
794
+ stride_dqm,
795
+ stride_dkn,
796
+ stride_dvn,
797
+ seqlen_q,
798
+ seqlen_k,
799
+ headdim,
800
+ ATOMIC_ADD=True,
801
+ BIAS_TYPE=BIAS_TYPE,
802
+ IS_CAUSAL=IS_CAUSAL,
803
+ BLOCK_HEADDIM=BLOCK_HEADDIM,
804
+ EVEN_M=EVEN_M,
805
+ EVEN_N=EVEN_N,
806
+ EVEN_HEADDIM=EVEN_HEADDIM,
807
+ BLOCK_M=BLOCK_M,
808
+ BLOCK_N=BLOCK_N,
809
+ )
810
+
811
+
812
+ def _flash_attn_forward(q, k, v, bias=None, causal=False, softmax_scale=None):
813
+ # shape constraints
814
+ batch, seqlen_q, nheads, d = q.shape
815
+ _, seqlen_k, _, _ = k.shape
816
+ assert k.shape == (batch, seqlen_k, nheads, d)
817
+ assert v.shape == (batch, seqlen_k, nheads, d)
818
+ assert d <= 128, "FlashAttention only support head dimensions up to 128"
819
+ assert q.dtype == k.dtype == v.dtype, "All tensors must have the same type"
820
+ assert q.dtype in [torch.float16, torch.bfloat16], "Only support fp16 and bf16"
821
+ assert q.is_cuda and k.is_cuda and v.is_cuda
822
+ softmax_scale = softmax_scale or 1.0 / math.sqrt(d)
823
+
824
+ has_bias = bias is not None
825
+ bias_type = "none"
826
+ if has_bias:
827
+ assert bias.dtype in [q.dtype, torch.float]
828
+ assert bias.is_cuda
829
+ assert bias.dim() == 4
830
+ if bias.stride(-1) != 1:
831
+ bias = bias.contiguous()
832
+ if bias.shape[2:] == (1, seqlen_k):
833
+ bias_type = "vector"
834
+ elif bias.shape[2:] == (seqlen_q, seqlen_k):
835
+ bias_type = "matrix"
836
+ else:
837
+ raise RuntimeError(
838
+ "Last 2 dimensions of bias must be (1, seqlen_k)" " or (seqlen_q, seqlen_k)"
839
+ )
840
+ bias = bias.expand(batch, nheads, seqlen_q, seqlen_k)
841
+ bias_strides = (bias.stride(0), bias.stride(1), bias.stride(2)) if has_bias else (0, 0, 0)
842
+
843
+ seqlen_q_rounded = math.ceil(seqlen_q / 128) * 128
844
+ lse = torch.empty((batch, nheads, seqlen_q_rounded), device=q.device, dtype=torch.float32)
845
+ tmp = torch.empty((batch, nheads, seqlen_q_rounded), device=q.device, dtype=torch.float32)
846
+ o = torch.empty_like(q)
847
+
848
+ BLOCK_HEADDIM = max(triton.next_power_of_2(d), 16)
849
+ BLOCK = 128
850
+ num_warps = 4 if d <= 64 else 8
851
+ grid = lambda META: (triton.cdiv(seqlen_q, META["BLOCK_M"]), batch * nheads)
852
+ _fwd_kernel[grid](
853
+ q,
854
+ k,
855
+ v,
856
+ bias,
857
+ o,
858
+ lse,
859
+ tmp,
860
+ softmax_scale,
861
+ q.stride(0),
862
+ q.stride(2),
863
+ q.stride(1),
864
+ k.stride(0),
865
+ k.stride(2),
866
+ k.stride(1),
867
+ v.stride(0),
868
+ v.stride(2),
869
+ v.stride(1),
870
+ *bias_strides,
871
+ o.stride(0),
872
+ o.stride(2),
873
+ o.stride(1),
874
+ nheads,
875
+ seqlen_q,
876
+ seqlen_k,
877
+ seqlen_q_rounded,
878
+ d,
879
+ seqlen_q // 32,
880
+ seqlen_k // 32, # key for triton cache (limit number of compilations)
881
+ # Can't use kwargs here because triton autotune expects key to be args, not kwargs
882
+ # IS_CAUSAL=causal, BLOCK_HEADDIM=d,
883
+ bias_type,
884
+ causal,
885
+ BLOCK_HEADDIM,
886
+ BLOCK_M=BLOCK,
887
+ BLOCK_N=BLOCK,
888
+ num_warps=num_warps,
889
+ num_stages=1,
890
+ )
891
+ return o, lse, softmax_scale # softmax_scale could have been updated
892
+
893
+
894
+ def _flash_attn_backward(
895
+ do, q, k, v, o, lse, dq, dk, dv, bias=None, causal=False, softmax_scale=None
896
+ ):
897
+ # Make sure that the last dimension is contiguous
898
+ if do.stride(-1) != 1:
899
+ do = do.contiguous()
900
+ batch, seqlen_q, nheads, d = q.shape
901
+ _, seqlen_k, _, _ = k.shape
902
+ # assert d in {16, 32, 64, 128}
903
+ assert d <= 128
904
+ seqlen_q_rounded = math.ceil(seqlen_q / 128) * 128
905
+ assert lse.shape == (batch, nheads, seqlen_q_rounded)
906
+ assert q.stride(-1) == k.stride(-1) == v.stride(-1) == o.stride(-1) == 1
907
+ assert dq.stride(-1) == dk.stride(-1) == dv.stride(-1) == 1
908
+ softmax_scale = softmax_scale or 1.0 / math.sqrt(d)
909
+ # dq_accum = torch.zeros_like(q, dtype=torch.float32)
910
+ dq_accum = torch.empty_like(q, dtype=torch.float32)
911
+ delta = torch.empty_like(lse)
912
+ # delta = torch.zeros_like(lse)
913
+
914
+ BLOCK_HEADDIM = max(triton.next_power_of_2(d), 16)
915
+ grid = lambda META: (triton.cdiv(seqlen_q, META["BLOCK_M"]), batch * nheads)
916
+ _bwd_preprocess_do_o_dot[grid](
917
+ o,
918
+ do,
919
+ delta,
920
+ o.stride(0),
921
+ o.stride(2),
922
+ o.stride(1),
923
+ do.stride(0),
924
+ do.stride(2),
925
+ do.stride(1),
926
+ nheads,
927
+ seqlen_q,
928
+ seqlen_q_rounded,
929
+ d,
930
+ BLOCK_M=128,
931
+ BLOCK_HEADDIM=BLOCK_HEADDIM,
932
+ )
933
+
934
+ has_bias = bias is not None
935
+ bias_type = "none"
936
+ if has_bias:
937
+ assert bias.dtype in [q.dtype, torch.float]
938
+ assert bias.is_cuda
939
+ assert bias.dim() == 4
940
+ assert bias.stride(-1) == 1
941
+ if bias.shape[2:] == (1, seqlen_k):
942
+ bias_type = "vector"
943
+ elif bias.shape[2:] == (seqlen_q, seqlen_k):
944
+ bias_type = "matrix"
945
+ else:
946
+ raise RuntimeError(
947
+ "Last 2 dimensions of bias must be (1, seqlen_k)" " or (seqlen_q, seqlen_k)"
948
+ )
949
+ bias = bias.expand(batch, nheads, seqlen_q, seqlen_k)
950
+ bias_strides = (bias.stride(0), bias.stride(1), bias.stride(2)) if has_bias else (0, 0, 0)
951
+
952
+ # BLOCK_M = 128
953
+ # BLOCK_N = 64
954
+ # num_warps = 4
955
+ grid = lambda META: (
956
+ triton.cdiv(seqlen_k, META["BLOCK_N"]) if META["SEQUENCE_PARALLEL"] else 1,
957
+ batch * nheads,
958
+ )
959
+ _bwd_kernel[grid](
960
+ q,
961
+ k,
962
+ v,
963
+ bias,
964
+ do,
965
+ dq_accum,
966
+ dk,
967
+ dv,
968
+ lse,
969
+ delta,
970
+ softmax_scale,
971
+ q.stride(0),
972
+ q.stride(2),
973
+ q.stride(1),
974
+ k.stride(0),
975
+ k.stride(2),
976
+ k.stride(1),
977
+ v.stride(0),
978
+ v.stride(2),
979
+ v.stride(1),
980
+ *bias_strides,
981
+ do.stride(0),
982
+ do.stride(2),
983
+ do.stride(1),
984
+ dq_accum.stride(0),
985
+ dq_accum.stride(2),
986
+ dq_accum.stride(1),
987
+ dk.stride(0),
988
+ dk.stride(2),
989
+ dk.stride(1),
990
+ dv.stride(0),
991
+ dv.stride(2),
992
+ dv.stride(1),
993
+ nheads,
994
+ seqlen_q,
995
+ seqlen_k,
996
+ seqlen_q_rounded,
997
+ d,
998
+ seqlen_q // 32,
999
+ seqlen_k // 32, # key for triton cache (limit number of compilations)
1000
+ # Can't use kwargs here because triton autotune expects key to be args, not kwargs
1001
+ # IS_CAUSAL=causal, BLOCK_HEADDIM=d,
1002
+ bias_type,
1003
+ causal,
1004
+ BLOCK_HEADDIM,
1005
+ # SEQUENCE_PARALLEL=False,
1006
+ # BLOCK_M=BLOCK_M, BLOCK_N=BLOCK_N,
1007
+ # num_warps=num_warps,
1008
+ # num_stages=1,
1009
+ )
1010
+ dq.copy_(dq_accum)
1011
+
1012
+
1013
+ class FlashAttnQKVPackedFunc(torch.autograd.Function):
1014
+ @staticmethod
1015
+ def forward(ctx, qkv, bias=None, causal=False, softmax_scale=None):
1016
+ """
1017
+ qkv: (batch, seqlen, 3, nheads, headdim)
1018
+ bias: optional, shape broadcastible to (batch, nheads, seqlen, seqlen).
1019
+ For example, ALiBi mask for causal would have shape (1, nheads, 1, seqlen).
1020
+ ALiBi mask for non-causal would have shape (1, nheads, seqlen, seqlen)
1021
+ """
1022
+ # Make sure that the last dimension is contiguous
1023
+ if qkv.stride(-1) != 1:
1024
+ qkv = qkv.contiguous()
1025
+ o, lse, ctx.softmax_scale = _flash_attn_forward(
1026
+ qkv[:, :, 0],
1027
+ qkv[:, :, 1],
1028
+ qkv[:, :, 2],
1029
+ bias=bias,
1030
+ causal=causal,
1031
+ softmax_scale=softmax_scale,
1032
+ )
1033
+ ctx.save_for_backward(qkv, o, lse, bias)
1034
+ ctx.causal = causal
1035
+ return o
1036
+
1037
+ @staticmethod
1038
+ def backward(ctx, do):
1039
+ qkv, o, lse, bias = ctx.saved_tensors
1040
+ assert not ctx.needs_input_grad[1], "FlashAttention does not support bias gradient yet"
1041
+ # Triton's autotune causes the Tensor._version to change, and so Pytorch autograd
1042
+ # does a memcpy. To avoid this we run in inference_mode, which doesn't track the version.
1043
+ with torch.inference_mode():
1044
+ dqkv = torch.empty_like(qkv)
1045
+ _flash_attn_backward(
1046
+ do,
1047
+ qkv[:, :, 0],
1048
+ qkv[:, :, 1],
1049
+ qkv[:, :, 2],
1050
+ o,
1051
+ lse,
1052
+ dqkv[:, :, 0],
1053
+ dqkv[:, :, 1],
1054
+ dqkv[:, :, 2],
1055
+ bias=bias,
1056
+ causal=ctx.causal,
1057
+ softmax_scale=ctx.softmax_scale,
1058
+ )
1059
+ return dqkv, None, None, None
1060
+
1061
+
1062
+ flash_attn_qkvpacked_func = FlashAttnQKVPackedFunc.apply
1063
+
1064
+
1065
+ class FlashAttnKVPackedFunc(torch.autograd.Function):
1066
+ @staticmethod
1067
+ def forward(ctx, q, kv, bias=None, causal=False, softmax_scale=None):
1068
+ """
1069
+ q: (batch, seqlen_q, nheads, headdim)
1070
+ kv: (batch, seqlen_k, 2, nheads, headdim)
1071
+ bias: optional, shape broadcastible to (batch, nheads, seqlen_q, seqlen_k).
1072
+ For example, ALiBi mask for causal would have shape (1, nheads, 1, seqlen_k).
1073
+ ALiBi mask for non-causal would have shape (1, nheads, seqlen_q, seqlen_k)
1074
+ """
1075
+ # Make sure that the last dimension is contiguous
1076
+ q, kv = [x if x.stride(-1) == 1 else x.contiguous() for x in [q, kv]]
1077
+ o, lse, ctx.softmax_scale = _flash_attn_forward(
1078
+ q, kv[:, :, 0], kv[:, :, 1], bias=bias, causal=causal, softmax_scale=softmax_scale
1079
+ )
1080
+ ctx.save_for_backward(q, kv, o, lse, bias)
1081
+ ctx.causal = causal
1082
+ return o
1083
+
1084
+ @staticmethod
1085
+ def backward(ctx, do):
1086
+ q, kv, o, lse, bias = ctx.saved_tensors
1087
+ if len(ctx.needs_input_grad) >= 3:
1088
+ assert not ctx.needs_input_grad[2], "FlashAttention does not support bias gradient yet"
1089
+ # Triton's autotune causes the Tensor._version to change, and so Pytorch autograd
1090
+ # does a memcpy. To avoid this we run in inference_mode, which doesn't track the version.
1091
+ with torch.inference_mode():
1092
+ dq = torch.empty_like(q)
1093
+ dkv = torch.empty_like(kv)
1094
+ _flash_attn_backward(
1095
+ do,
1096
+ q,
1097
+ kv[:, :, 0],
1098
+ kv[:, :, 1],
1099
+ o,
1100
+ lse,
1101
+ dq,
1102
+ dkv[:, :, 0],
1103
+ dkv[:, :, 1],
1104
+ bias=bias,
1105
+ causal=ctx.causal,
1106
+ softmax_scale=ctx.softmax_scale,
1107
+ )
1108
+ return dq, dkv, None, None, None
1109
+
1110
+
1111
+ flash_attn_kvpacked_func = FlashAttnKVPackedFunc.apply
1112
+
1113
+
1114
+ class FlashAttnFunc(torch.autograd.Function):
1115
+ @staticmethod
1116
+ def forward(ctx, q, k, v, bias=None, causal=False, softmax_scale=None):
1117
+ """
1118
+ q: (batch_size, seqlen_q, nheads, headdim)
1119
+ k, v: (batch_size, seqlen_k, nheads, headdim)
1120
+ bias: optional, shape broadcastible to (batch, nheads, seqlen_q, seqlen_k).
1121
+ For example, ALiBi mask for causal would have shape (1, nheads, 1, seqlen_k).
1122
+ ALiBi mask for non-causal would have shape (1, nheads, seqlen_q, seqlen_k)
1123
+ """
1124
+ # Make sure that the last dimension is contiguous
1125
+ q, k, v = [x if x.stride(-1) == 1 else x.contiguous() for x in [q, k, v]]
1126
+ o, lse, ctx.softmax_scale = _flash_attn_forward(
1127
+ q, k, v, bias=bias, causal=causal, softmax_scale=softmax_scale
1128
+ )
1129
+ ctx.save_for_backward(q, k, v, o, lse, bias)
1130
+ ctx.causal = causal
1131
+ return o
1132
+
1133
+ @staticmethod
1134
+ def backward(ctx, do):
1135
+ q, k, v, o, lse, bias = ctx.saved_tensors
1136
+ assert not ctx.needs_input_grad[3], "FlashAttention does not support bias gradient yet"
1137
+ # Triton's autotune causes the Tensor._version to change, and so Pytorch autograd
1138
+ # does a memcpy. To avoid this we run in inference_mode, which doesn't track the version.
1139
+ with torch.inference_mode():
1140
+ dq = torch.empty_like(q)
1141
+ dk = torch.empty_like(k)
1142
+ dv = torch.empty_like(v)
1143
+ _flash_attn_backward(
1144
+ do,
1145
+ q,
1146
+ k,
1147
+ v,
1148
+ o,
1149
+ lse,
1150
+ dq,
1151
+ dk,
1152
+ dv,
1153
+ bias=bias,
1154
+ causal=ctx.causal,
1155
+ softmax_scale=ctx.softmax_scale,
1156
+ )
1157
+ return dq, dk, dv, None, None, None
1158
+
1159
+
1160
+ flash_attn_func = FlashAttnFunc.apply
evalkit_tf446/lib/python3.10/site-packages/flash_attn/flash_attn_triton_amd/__pycache__/__init__.cpython-310.pyc ADDED
Binary file (191 Bytes). View file
 
evalkit_tf446/lib/python3.10/site-packages/flash_attn/flash_attn_triton_amd/__pycache__/bench.cpython-310.pyc ADDED
Binary file (7.37 kB). View file
 
evalkit_tf446/lib/python3.10/site-packages/flash_attn/flash_attn_triton_amd/__pycache__/fwd_prefill.cpython-310.pyc ADDED
Binary file (12.9 kB). View file
 
evalkit_tf446/lib/python3.10/site-packages/flash_attn/flash_attn_triton_amd/__pycache__/fwd_ref.cpython-310.pyc ADDED
Binary file (5.44 kB). View file
 
evalkit_tf446/lib/python3.10/site-packages/flash_attn/flash_attn_triton_amd/__pycache__/interface_fa.cpython-310.pyc ADDED
Binary file (7.02 kB). View file
 
evalkit_tf446/lib/python3.10/site-packages/flash_attn/flash_attn_triton_amd/fwd_prefill.py ADDED
@@ -0,0 +1,652 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import triton
3
+ import triton.language as tl
4
+ from .utils import get_shape_from_layout, get_strides_from_layout, is_cdna, is_rdna, DEBUG, AUTOTUNE
5
+
6
+ @triton.jit
7
+ def cdiv_fn(x, y):
8
+ return (x + y - 1) // y
9
+
10
+ @triton.jit
11
+ def dropout_offsets(philox_seed, philox_offset, dropout_p, m, n, stride):
12
+ ms = tl.arange(0, m)
13
+ ns = tl.arange(0, n)
14
+ return philox_offset + ms[:, None] * stride + ns[None, :]
15
+
16
+
17
+ @triton.jit
18
+ def dropout_rng(philox_seed, philox_offset, dropout_p, m, n, stride):
19
+ rng_offsets = dropout_offsets(philox_seed, philox_offset, dropout_p, m, n, stride).to(tl.uint32)
20
+ # TODO: use tl.randint for better performance
21
+ return tl.rand(philox_seed, rng_offsets)
22
+
23
+
24
+ @triton.jit
25
+ def dropout_mask(philox_seed, philox_offset, dropout_p, m, n, stride):
26
+ rng_output = dropout_rng(philox_seed, philox_offset, dropout_p, m, n, stride)
27
+ rng_keep = rng_output > dropout_p
28
+ return rng_keep
29
+
30
+
31
+ # Convenience function to load with optional boundary checks.
32
+ # "First" is the major dim, "second" is the minor dim.
33
+ @triton.jit
34
+ def load_fn(ptrs, offset_first, offset_second, boundary_first, boundary_second):
35
+ if offset_first is not None and offset_second is not None:
36
+ mask = (offset_first[:, None] < boundary_first) & \
37
+ (offset_second[None, :] < boundary_second)
38
+ tensor = tl.load(ptrs, mask=mask, other=0.0)
39
+ elif offset_first is not None:
40
+ mask = offset_first[:, None] < boundary_first
41
+ tensor = tl.load(ptrs, mask=mask, other=0.0)
42
+ elif offset_second is not None:
43
+ mask = offset_second[None, :] < boundary_second
44
+ tensor = tl.load(ptrs, mask=mask, other=0.0)
45
+ else:
46
+ tensor = tl.load(ptrs)
47
+ return tensor
48
+
49
+
50
+ @triton.jit
51
+ def compute_alibi_block(alibi_slope, seqlen_q, seqlen_k, offs_m, offs_n, transpose=False):
52
+ # when seqlen_k and seqlen_q are different we want the diagonal to stick to the bottom right of the attention matrix
53
+ # for casual mask we want something like this where (1 is kept and 0 is masked)
54
+ # seqlen_q = 2 and seqlen_k = 5
55
+ # 1 1 1 1 0
56
+ # 1 1 1 1 1
57
+ # seqlen_q = 5 and seqlen_k = 2
58
+ # 0 0
59
+ # 0 0
60
+ # 0 0
61
+ # 1 0
62
+ # 1 1
63
+ # for alibi the diagonal is 0 indicating no penalty for attending to that spot and increasing penalty for attending further from the diagonal
64
+ # e.g. alibi_slope = 1, seqlen_q = 2, seqlen_k = 5, offs_m = [0, 1, 2, 3], offs_n = [0, 1, 2, 3, 4], transpose = False
65
+ # 1. offs_m[:,None] = [[0],
66
+ # [1],
67
+ # 2. offs_m[:,None] + seqlen_k = [[5],
68
+ # [6],
69
+ # 3. offs_m[:,None] + seqlen_k - seqlen_q = [[3],
70
+ # [4],
71
+ # 4. offs_m[:,None] + seqlen_k - seqlen_q - offs_n[None,:] = [[3], - [[0, 1, 2, 3, 4]] = [[ 3, 2, 1, 0,-1],
72
+ # [4], [ 4, 3, 2, 1, 0]]
73
+ # 5. -1 * alibi_slope * tl.abs(relative_pos_block) = [[ -3, -2, -1, 0,-1],
74
+ # [ -4, -3, -2, -1, 0]],
75
+ relative_pos_block = offs_m[:, None] + seqlen_k - seqlen_q - offs_n[None, :]
76
+ alibi_block = -1 * alibi_slope * tl.abs(relative_pos_block)
77
+ if transpose:
78
+ return alibi_block.T
79
+ else:
80
+ return alibi_block
81
+
82
+
83
+ @triton.jit
84
+ def _attn_fwd_inner(acc, l_i, m_i, q, k_ptrs, v_ptrs, bias_ptrs, stride_kn, stride_vk, stride_bn, start_m,
85
+ actual_seqlen_k, actual_seqlen_q, dropout_p, philox_seed, batch_philox_offset, exp_scores_ptrs,
86
+ block_min, block_max, offs_n_causal, masked_blocks, n_extra_tokens, alibi_slope, score_ptrs, scores_scaled_shifted_ptrs,
87
+ IS_CAUSAL: tl.constexpr, BLOCK_M: tl.constexpr, BLOCK_DMODEL: tl.constexpr, BLOCK_N: tl.constexpr,
88
+ OFFS_M: tl.constexpr, OFFS_N: tl.constexpr, PRE_LOAD_V: tl.constexpr, MASK_STEPS: tl.constexpr,
89
+ ENABLE_DROPOUT: tl.constexpr, PADDED_HEAD: tl.constexpr,
90
+ ACTUAL_BLOCK_DMODEL: tl.constexpr, SM_SCALE: tl.constexpr, USE_EXP2: tl.constexpr,
91
+ RETURN_SCORES: tl.constexpr):
92
+ if USE_EXP2:
93
+ RCP_LN2: tl.constexpr = 1.4426950408889634
94
+
95
+ # loop over k, v, and update accumulator
96
+ for start_n in range(block_min, block_max, BLOCK_N):
97
+ # For padded blocks, we will overrun the tensor size if
98
+ # we load all BLOCK_N. For others, the blocks are all within range.
99
+ if MASK_STEPS:
100
+ k_offs_n = start_n + tl.arange(0, BLOCK_N)
101
+ else:
102
+ k_offs_n = None
103
+ k_offs_k = None if not PADDED_HEAD else tl.arange(0, BLOCK_DMODEL)
104
+ k = load_fn(k_ptrs, k_offs_k, k_offs_n, ACTUAL_BLOCK_DMODEL, actual_seqlen_k)
105
+ if PRE_LOAD_V:
106
+ # We can use the same offsets as k, just with dims transposed.
107
+ v = load_fn(v_ptrs, k_offs_n, k_offs_k, actual_seqlen_k, ACTUAL_BLOCK_DMODEL)
108
+ qk = tl.zeros([BLOCK_M, BLOCK_N], dtype=tl.float32)
109
+ # We start from end of seqlen_k so only the first iteration would need
110
+ # to be checked for padding if it is not a multiple of block_n
111
+ # TODO: This can be optimized to only be true for the padded block.
112
+ if MASK_STEPS:
113
+ # If this is the last block / iteration, we want to
114
+ # mask if the sequence length is not a multiple of block size
115
+ # a solution is to always do BLOCK_M // BLOCK_N + 1 steps if not is_modulo_mn.
116
+ # last step might get wasted but that is okay. check if this masking works For
117
+ # that case.
118
+ if (start_n + BLOCK_N == block_max) and (n_extra_tokens != 0):
119
+ boundary_m = tl.full([BLOCK_M], actual_seqlen_k, dtype=tl.int32)
120
+ size_n = start_n + OFFS_N[None, :]
121
+ mask = size_n < boundary_m[:, None]
122
+ qk = tl.where(mask, qk, float("-inf"))
123
+
124
+ # -- compute qk ----
125
+ qk += tl.dot(q, k)
126
+ qk_scaled = qk * SM_SCALE
127
+ if RETURN_SCORES:
128
+ score_mask = (OFFS_M[:, None] < actual_seqlen_q) & ((start_n + tl.arange(0, BLOCK_N))[None, :] < actual_seqlen_k)
129
+ tl.store(score_ptrs, qk_scaled, mask=score_mask)
130
+
131
+ if IS_CAUSAL:
132
+ causal_boundary = start_n + offs_n_causal
133
+ causal_mask = OFFS_M[:, None] >= causal_boundary[None, :]
134
+ qk_scaled = tl.where(causal_mask, qk_scaled, float("-inf"))
135
+ if bias_ptrs is not None:
136
+ bias_offs_n = start_n + tl.arange(0, BLOCK_N) if MASK_STEPS else None
137
+ bias = load_fn(bias_ptrs, OFFS_M, bias_offs_n, actual_seqlen_q, actual_seqlen_k)
138
+ qk_scaled += bias
139
+
140
+ if alibi_slope is not None:
141
+ # Compute the global position of each token within the sequence
142
+ global_m_positions = start_m * BLOCK_M + tl.arange(0, BLOCK_M)
143
+ global_n_positions = start_n + tl.arange(0, BLOCK_N)
144
+ alibi_block = compute_alibi_block(alibi_slope, actual_seqlen_q, actual_seqlen_k, global_m_positions,
145
+ global_n_positions)
146
+ qk_scaled += alibi_block
147
+ # get max scores so far
148
+ m_ij = tl.maximum(m_i, tl.max(qk_scaled, 1))
149
+
150
+ # scale and subtract max
151
+ q_shifted = qk_scaled - m_ij[:, None]
152
+ if RETURN_SCORES:
153
+ # NOTE: the returned score is not the same as the reference because we need to adjust as we find new maxes per block. We are not doing that
154
+ scores_scaled_shifted_mask = (OFFS_M[:, None] < actual_seqlen_q) & ((start_n + tl.arange(0, BLOCK_N))[None, :] < actual_seqlen_k)
155
+ tl.store(scores_scaled_shifted_ptrs, q_shifted, mask=scores_scaled_shifted_mask)
156
+
157
+ # Compute scaled QK and softmax probabilities
158
+ if USE_EXP2:
159
+ p = tl.math.exp2(q_shifted * RCP_LN2)
160
+ else:
161
+ p = tl.math.exp(q_shifted)
162
+
163
+ # CAVEAT: Must update l_ij before applying dropout
164
+ l_ij = tl.sum(p, 1)
165
+ if ENABLE_DROPOUT:
166
+ philox_offset = batch_philox_offset + start_m * BLOCK_M * actual_seqlen_k + start_n - BLOCK_N
167
+ keep = dropout_mask(philox_seed, philox_offset, dropout_p, BLOCK_M, BLOCK_N, actual_seqlen_k)
168
+ if RETURN_SCORES:
169
+ # NOTE: the returned score is not the same as the reference because we need to adjust as we find new maxes per block. We are not doing that
170
+ exp_score_mask = (OFFS_M[:, None] < actual_seqlen_q) & ((start_n + tl.arange(0, BLOCK_N))[None, :] < actual_seqlen_k)
171
+ tl.store(exp_scores_ptrs, tl.where(keep, p, -p), mask=exp_score_mask)
172
+ p = tl.where(keep, p, 0.0)
173
+ elif RETURN_SCORES:
174
+ # NOTE: the returned score is not the same as the reference because we need to adjust as we find new maxes per block. We are not doing that
175
+ exp_score_mask = (OFFS_M[:, None] < actual_seqlen_q) & ((start_n + tl.arange(0, BLOCK_N))[None, :] < actual_seqlen_k)
176
+ tl.store(exp_scores_ptrs, p, mask=exp_score_mask)
177
+
178
+ # -- update output accumulator --
179
+ # alpha is an adjustment factor for acc and li as we loop and find new maxes
180
+ # store the diff in maxes to adjust acc and li as we discover new maxes
181
+ m_diff = m_i - m_ij
182
+ if USE_EXP2:
183
+ alpha = tl.math.exp2(m_diff * RCP_LN2)
184
+ else:
185
+ alpha = tl.math.exp(m_diff)
186
+ acc = acc * alpha[:, None]
187
+ if not PRE_LOAD_V:
188
+ v = load_fn(v_ptrs, k_offs_n, k_offs_k, actual_seqlen_k, ACTUAL_BLOCK_DMODEL)
189
+ # -- update m_i and l_i
190
+ l_i = l_i * alpha + l_ij
191
+ # update m_i and l_i
192
+ m_i = m_ij
193
+ acc += tl.dot(p.to(v.type.element_ty), v)
194
+ k_ptrs += BLOCK_N * stride_kn
195
+ v_ptrs += BLOCK_N * stride_vk
196
+ if bias_ptrs is not None:
197
+ bias_ptrs += BLOCK_N * stride_bn
198
+ if RETURN_SCORES:
199
+ score_ptrs += BLOCK_N
200
+ scores_scaled_shifted_ptrs += BLOCK_N
201
+ exp_scores_ptrs += BLOCK_N
202
+ return acc, l_i, m_i
203
+
204
+
205
+ def get_cdna_autotune_configs():
206
+ return [
207
+ triton.Config({'BLOCK_M': 128, 'BLOCK_N': 128, 'waves_per_eu': 2, 'PRE_LOAD_V': False}, num_stages=1,
208
+ num_warps=4),
209
+ triton.Config({'BLOCK_M': 128, 'BLOCK_N': 64, 'waves_per_eu': 2, 'PRE_LOAD_V': False}, num_stages=1,
210
+ num_warps=4),
211
+ triton.Config({'BLOCK_M': 128, 'BLOCK_N': 64, 'waves_per_eu': 3, 'PRE_LOAD_V': False}, num_stages=1,
212
+ num_warps=4),
213
+ triton.Config({'BLOCK_M': 128, 'BLOCK_N': 64, 'waves_per_eu': 1, 'PRE_LOAD_V': False}, num_stages=1,
214
+ num_warps=4),
215
+ triton.Config({'BLOCK_M': 128, 'BLOCK_N': 32, 'waves_per_eu': 2, 'PRE_LOAD_V': False}, num_stages=1,
216
+ num_warps=4),
217
+ triton.Config({'BLOCK_M': 64, 'BLOCK_N': 64, 'waves_per_eu': 1, 'PRE_LOAD_V': False}, num_stages=1,
218
+ num_warps=4),
219
+ # Fall-back config.
220
+ triton.Config({'BLOCK_M': 16, 'BLOCK_N': 16, 'waves_per_eu': 1, 'PRE_LOAD_V': False}, num_stages=1,
221
+ num_warps=4),
222
+ ], ['IS_CAUSAL', 'dropout_p', 'MAX_SEQLENS_Q', 'MAX_SEQLENS_K', 'ACTUAL_BLOCK_DMODEL', 'VARLEN', 'HQ', 'HK']
223
+
224
+
225
+ def get_rdna_autotune_configs():
226
+ return [
227
+ triton.Config({'BLOCK_M': 32, 'BLOCK_N': 32, 'waves_per_eu': 4, 'PRE_LOAD_V': False}, num_stages=1,
228
+ num_warps=2),
229
+ triton.Config({'BLOCK_M': 32, 'BLOCK_N': 32, 'waves_per_eu': 2, 'PRE_LOAD_V': False}, num_stages=1,
230
+ num_warps=2),
231
+ triton.Config({'BLOCK_M': 32, 'BLOCK_N': 16, 'waves_per_eu': 4, 'PRE_LOAD_V': False}, num_stages=1,
232
+ num_warps=2),
233
+ triton.Config({'BLOCK_M': 32, 'BLOCK_N': 16, 'waves_per_eu': 2, 'PRE_LOAD_V': False}, num_stages=1,
234
+ num_warps=2),
235
+ triton.Config({'BLOCK_M': 16, 'BLOCK_N': 16, 'waves_per_eu': 4, 'PRE_LOAD_V': False}, num_stages=1,
236
+ num_warps=2),
237
+ triton.Config({'BLOCK_M': 16, 'BLOCK_N': 16, 'waves_per_eu': 2, 'PRE_LOAD_V': False}, num_stages=1,
238
+ num_warps=2),
239
+ # Fall-back config.
240
+ triton.Config({'BLOCK_M': 16, 'BLOCK_N': 16, 'waves_per_eu': 1, 'PRE_LOAD_V': False}, num_stages=1,
241
+ num_warps=2),
242
+ ], ['IS_CAUSAL', 'dropout_p', 'MAX_SEQLENS_Q', 'MAX_SEQLENS_K', 'ACTUAL_BLOCK_DMODEL', 'VARLEN', 'HQ', 'HK']
243
+
244
+
245
+ def get_autotune_configs():
246
+ if AUTOTUNE:
247
+ if is_rdna():
248
+ return get_rdna_autotune_configs()
249
+ elif is_cdna():
250
+ return get_cdna_autotune_configs()
251
+ else:
252
+ raise ValueError("Unknown Device Type")
253
+ else:
254
+ return [
255
+ triton.Config(
256
+ {"BLOCK_M": 64, "BLOCK_N": 64, "waves_per_eu": 1, "PRE_LOAD_V": False},
257
+ num_stages=1,
258
+ num_warps=4,
259
+ ),
260
+ ], [
261
+ "IS_CAUSAL",
262
+ "dropout_p",
263
+ "MAX_SEQLENS_Q",
264
+ "MAX_SEQLENS_K",
265
+ "ACTUAL_BLOCK_DMODEL",
266
+ "VARLEN",
267
+ "HQ",
268
+ "HK",
269
+ ]
270
+
271
+
272
+ autotune_configs, autotune_keys = get_autotune_configs()
273
+
274
+ @triton.autotune(
275
+ configs=autotune_configs,
276
+ key=autotune_keys,
277
+ use_cuda_graph=True,
278
+ )
279
+ @triton.jit
280
+ def attn_fwd(Q, K, V, bias, SM_SCALE: tl.constexpr, LSE, Out, stride_qz, stride_qh, stride_qm, stride_qk,
281
+ stride_kz, stride_kh, stride_kn, stride_kk, stride_vz, stride_vh, stride_vk, stride_vn,
282
+ stride_oz, stride_oh, stride_om, stride_on, stride_bz, stride_bh, stride_bm, stride_bn, stride_az, stride_ah,
283
+ stride_sz, stride_sh, stride_sm, stride_sn, stride_lse_z, stride_lse_h, stride_lse_m, cu_seqlens_q, cu_seqlens_k,
284
+ dropout_p, philox_seed, philox_offset_base, scores, scores_scaled_shifted, exp_scores, alibi_slopes, HQ: tl.constexpr,
285
+ HK: tl.constexpr, ACTUAL_BLOCK_DMODEL: tl.constexpr, MAX_SEQLENS_Q: tl.constexpr,
286
+ MAX_SEQLENS_K: tl.constexpr, VARLEN: tl.constexpr, IS_CAUSAL: tl.constexpr, BLOCK_M: tl.constexpr,
287
+ BLOCK_DMODEL: tl.constexpr, BLOCK_N: tl.constexpr, PRE_LOAD_V: tl.constexpr, USE_BIAS: tl.constexpr,
288
+ ENABLE_DROPOUT: tl.constexpr, RETURN_SCORES: tl.constexpr, USE_ALIBI: tl.constexpr, USE_EXP2: tl.constexpr):
289
+ start_m = tl.program_id(0)
290
+ off_h_q = tl.program_id(1)
291
+ off_z = tl.program_id(2)
292
+ offs_m = start_m * BLOCK_M + tl.arange(0, BLOCK_M)
293
+ offs_n = tl.arange(0, BLOCK_N)
294
+ offs_d = tl.arange(0, BLOCK_DMODEL)
295
+ if VARLEN:
296
+ cu_seqlens_q_start = tl.load(cu_seqlens_q + off_z)
297
+ cu_seqlens_q_end = tl.load(cu_seqlens_q + off_z + 1)
298
+ # print("cu_seqlens_q_start:", cu_seqlens_q_start)
299
+
300
+ seqlen_q = cu_seqlens_q_end - cu_seqlens_q_start
301
+ # We have a one-size-fits-all grid in id(0). Some seqlens might be too
302
+ # small for all start_m so for those we return early.
303
+ if start_m * BLOCK_M > seqlen_q:
304
+ return
305
+ cu_seqlens_k_start = tl.load(cu_seqlens_k + off_z)
306
+ cu_seqlens_k_end = tl.load(cu_seqlens_k + off_z + 1)
307
+ seqlen_k = cu_seqlens_k_end - cu_seqlens_k_start
308
+ else:
309
+ cu_seqlens_q_start = 0
310
+ cu_seqlens_k_start = 0
311
+ seqlen_q = MAX_SEQLENS_Q
312
+ seqlen_k = MAX_SEQLENS_K
313
+
314
+ # Now we compute whether we need to exit early due to causal masking.
315
+ # This is because for seqlen_q > seqlen_k, M rows of the attn scores
316
+ # are completely masked, resulting in 0s written to the output, and
317
+ # inf written to LSE. We don't need to do any GEMMs in this case.
318
+ # This block of code determines what N is, and if this WG is operating
319
+ # on those M rows.
320
+ n_blocks = cdiv_fn(seqlen_k, BLOCK_N)
321
+ if (IS_CAUSAL):
322
+ # If seqlen_q == seqlen_k, the attn scores are a square matrix.
323
+ # If seqlen_q != seqlen_k, attn scores are rectangular which means
324
+ # the causal mask boundary is bottom right aligned, and ends at either
325
+ # the top edge (seqlen_q < seqlen_k) or left edge.
326
+ # This captures the decrease in n_blocks if we have a rectangular attn matrix
327
+ n_blocks_seqlen = cdiv_fn((start_m + 1) * BLOCK_M + seqlen_k - seqlen_q, BLOCK_N)
328
+ # This is what adjusts the block_max for the current WG, only
329
+ # if IS_CAUSAL. Otherwise we want to always iterate through all n_blocks
330
+ n_blocks = min(n_blocks, n_blocks_seqlen)
331
+ # If we have no blocks after adjusting for seqlen deltas, this WG is part of
332
+ # the blocks that are all 0. We exit early.
333
+ if n_blocks <= 0:
334
+ o_offset = Out + off_z * stride_oz + off_h_q * stride_oh + cu_seqlens_q_start * stride_om
335
+ o_ptrs = o_offset + offs_m[:, None] * stride_om + offs_d[None, :] * stride_on
336
+ acc = tl.zeros([BLOCK_M, BLOCK_DMODEL], dtype=Out.type.element_ty)
337
+ o_ptrs_mask = offs_m[:, None] < seqlen_q
338
+ # We still need to write 0s to the result
339
+ tl.store(o_ptrs, acc, mask=o_ptrs_mask)
340
+ # The tensor allocated for L is based on MAX_SEQLENS_Q as that is
341
+ # statically known.
342
+ l_offset = LSE + off_z * stride_lse_z + off_h_q * stride_lse_h + cu_seqlens_q_start * stride_lse_m
343
+ l_ptrs = l_offset + offs_m * stride_lse_m
344
+
345
+ l = tl.full([BLOCK_M], value=0.0, dtype=tl.float32)
346
+
347
+ # mask_m_offsets = start_m + tl.arange(0, BLOCK_M)
348
+ # lse_mask = mask_m_offsets < causal_start_idx
349
+ # softmax_lse = tl.where(lse_mask, 0.0, softmax_lse)
350
+ l_ptrs_mask = offs_m < MAX_SEQLENS_Q
351
+ tl.store(l_ptrs, l, mask=l_ptrs_mask)
352
+ # TODO: Should dropout and return encoded softmax be handled here too?
353
+ return
354
+
355
+ # If MQA / GQA, set the K and V head offsets appropriately.
356
+ GROUP_SIZE: tl.constexpr = HQ // HK
357
+ if GROUP_SIZE != 1:
358
+ off_h_k = off_h_q // GROUP_SIZE
359
+ else:
360
+ off_h_k = off_h_q
361
+
362
+ n_extra_tokens = 0
363
+ # print("n_extra_tokens:", n_extra_tokens)
364
+ # print("seqlen_k:", seqlen_k)
365
+ # print("BLOCK_N:", BLOCK_N)
366
+ # return
367
+ if seqlen_k < BLOCK_N:
368
+ n_extra_tokens = BLOCK_N - seqlen_k
369
+ elif seqlen_k % BLOCK_N:
370
+ n_extra_tokens = seqlen_k % BLOCK_N
371
+ PADDED_HEAD: tl.constexpr = (ACTUAL_BLOCK_DMODEL != BLOCK_DMODEL)
372
+
373
+ # Compute pointers for all the tensors used in this kernel.
374
+ q_offset = Q + off_z * stride_qz + off_h_q * stride_qh + cu_seqlens_q_start * stride_qm
375
+ q_ptrs = q_offset + offs_m[:, None] * stride_qm + offs_d[None, :] * stride_qk
376
+ k_offset = K + off_z * stride_kz + off_h_k * stride_kh + cu_seqlens_k_start * stride_kn
377
+ k_ptrs = k_offset + offs_d[:, None] * stride_kk + offs_n[None, :] * stride_kn
378
+ v_offset = V + off_z * stride_vz + off_h_k * stride_vh + cu_seqlens_k_start * stride_vk
379
+ v_ptrs = v_offset + offs_n[:, None] * stride_vk + offs_d[None, :] * stride_vn
380
+ if USE_BIAS:
381
+ # Note: this might get large enough to overflow on some configs
382
+ bias_offset = off_h_q * stride_bh
383
+ bias_ptrs = bias + bias_offset + offs_m[:, None] * stride_bm + offs_n[None, :] * stride_bn
384
+ else:
385
+ bias_ptrs = None
386
+
387
+ if USE_ALIBI:
388
+ a_offset = off_z * stride_az + off_h_q * stride_ah
389
+ alibi_slope = tl.load(alibi_slopes + a_offset)
390
+ else:
391
+ alibi_slope = None
392
+
393
+ if RETURN_SCORES:
394
+ scores_offset = scores + off_z * stride_sz + off_h_q * stride_sh + cu_seqlens_q_start * stride_sm
395
+ score_ptrs = scores_offset + offs_m[:, None] * stride_sm + offs_n[None, :] * stride_sn
396
+
397
+ scores_scaled_shifted_offset = scores_scaled_shifted + off_z * stride_sz + off_h_q * stride_sh + cu_seqlens_q_start * stride_sm
398
+ scores_scaled_shifted_ptrs = scores_scaled_shifted_offset + offs_m[:, None] * stride_sm + offs_n[None, :] * stride_sn
399
+
400
+ exp_scores_offset = exp_scores + off_z * stride_sz + off_h_q * stride_sh + cu_seqlens_q_start * stride_sm
401
+ exp_scores_ptrs = exp_scores_offset + offs_m[:, None] * stride_sm + offs_n[None, :] * stride_sn
402
+ else:
403
+ score_ptrs = None
404
+ scores_scaled_shifted_ptrs = None
405
+ exp_scores_ptrs = None
406
+
407
+ if ENABLE_DROPOUT:
408
+ off_hz = off_z * HQ + off_h_q
409
+ batch_philox_offset = philox_offset_base + off_hz * seqlen_q * seqlen_k
410
+ else:
411
+ batch_philox_offset = 0
412
+ # initialize pointer to m and l
413
+ m_i = tl.full([BLOCK_M], float("-inf"), dtype=tl.float32)
414
+ l_i = tl.full([BLOCK_M], 1.0, dtype=tl.float32)
415
+ acc = tl.zeros([BLOCK_M, BLOCK_DMODEL], dtype=tl.float32)
416
+ # Q is loaded once at the beginning and shared by all N blocks.
417
+ q_ptrs_mask = offs_m[:, None] < seqlen_q
418
+ if PADDED_HEAD:
419
+ q_ptrs_mask = q_ptrs_mask & (offs_d[None, :] < ACTUAL_BLOCK_DMODEL)
420
+ q = tl.load(q_ptrs, mask=q_ptrs_mask, other=0.0)
421
+
422
+ # Here we compute how many full and masked blocks we have.
423
+ padded_block_k = n_extra_tokens != 0
424
+ is_modulo_mn = not padded_block_k and (seqlen_q % BLOCK_M == 0)
425
+ if IS_CAUSAL:
426
+ # There are always at least BLOCK_M // BLOCK_N masked blocks.
427
+ # Additionally there might be one more due to dissimilar seqlens.
428
+ masked_blocks = BLOCK_M // BLOCK_N + (not is_modulo_mn)
429
+ else:
430
+ # Padding on Q does not need to be masked in the FA loop.
431
+ masked_blocks = padded_block_k
432
+ # if IS_CAUSAL, not is_modulo_mn does not always result in an additional block.
433
+ # In this case we might exceed n_blocks so pick the min.
434
+ masked_blocks = min(masked_blocks, n_blocks)
435
+ n_full_blocks = n_blocks - masked_blocks
436
+ block_min = 0
437
+ block_max = n_blocks * BLOCK_N
438
+ # Compute for full blocks. Here we set causal to false regardless of its actual
439
+ # value because there is no masking. Similarly we do not need padding.
440
+ if n_full_blocks > 0:
441
+ block_max = (n_blocks - masked_blocks) * BLOCK_N
442
+ acc, l_i, m_i = _attn_fwd_inner(acc, l_i, m_i, q, k_ptrs, v_ptrs, bias_ptrs, stride_kn, stride_vk, stride_bn,
443
+ start_m, seqlen_k, seqlen_q, dropout_p, philox_seed, batch_philox_offset,
444
+ exp_scores_ptrs,
445
+ # _, _, offs_n_causal, masked_blocks, n_extra_tokens, _
446
+ block_min, block_max, 0, 0, 0, alibi_slope, score_ptrs, scores_scaled_shifted_ptrs,
447
+ # IS_CAUSAL, ....
448
+ False, BLOCK_M, BLOCK_DMODEL, BLOCK_N, offs_m, offs_n,
449
+ # _, MASK_STEPS, ...
450
+ PRE_LOAD_V, False, ENABLE_DROPOUT, PADDED_HEAD,
451
+ ACTUAL_BLOCK_DMODEL, SM_SCALE, USE_EXP2=USE_EXP2, RETURN_SCORES=RETURN_SCORES)
452
+ block_min = block_max
453
+ block_max = n_blocks * BLOCK_N
454
+
455
+ tl.debug_barrier()
456
+ # Remaining blocks, if any, are full / not masked.
457
+ if (masked_blocks > 0):
458
+ if IS_CAUSAL:
459
+ offs_n_causal = offs_n + (seqlen_q - seqlen_k)
460
+ else:
461
+ offs_n_causal = 0
462
+ k_ptrs += n_full_blocks * BLOCK_N * stride_kn
463
+ v_ptrs += n_full_blocks * BLOCK_N * stride_vk
464
+ if USE_BIAS:
465
+ bias_ptrs += n_full_blocks * BLOCK_N * stride_bn
466
+ if RETURN_SCORES:
467
+ score_ptrs += n_full_blocks * BLOCK_N
468
+ scores_scaled_shifted_ptrs += n_full_blocks * BLOCK_N
469
+ exp_scores_ptrs += n_full_blocks * BLOCK_N
470
+ acc, l_i, m_i = _attn_fwd_inner(acc, l_i, m_i, q, k_ptrs, v_ptrs, bias_ptrs, stride_kn, stride_vk, stride_bn,
471
+ start_m, seqlen_k, seqlen_q, dropout_p, philox_seed, batch_philox_offset,
472
+ exp_scores_ptrs, block_min, block_max, offs_n_causal, masked_blocks,
473
+ n_extra_tokens, alibi_slope, score_ptrs, scores_scaled_shifted_ptrs,
474
+ IS_CAUSAL, BLOCK_M, BLOCK_DMODEL, BLOCK_N, offs_m, offs_n,
475
+ # _, MASK_STEPS, ...
476
+ PRE_LOAD_V, True, ENABLE_DROPOUT, PADDED_HEAD,
477
+ ACTUAL_BLOCK_DMODEL, SM_SCALE, USE_EXP2=USE_EXP2, RETURN_SCORES=RETURN_SCORES)
478
+ # epilogue
479
+ # This helps the compiler do Newton Raphson on l_i vs on acc which is much larger.
480
+ l_recip = 1 / l_i[:, None]
481
+ acc = acc * l_recip
482
+ if ENABLE_DROPOUT:
483
+ acc = acc / (1 - dropout_p)
484
+ # If seqlen_q > seqlen_k but the delta is not a multiple of BLOCK_M,
485
+ # then we have one block with a row of all NaNs which come from computing
486
+ # softmax over a row of all -infs (-inf - inf = NaN). We check for that here
487
+ # and store 0s where there are NaNs as these rows should've been zeroed out.
488
+ end_m_idx = (start_m + 1) * BLOCK_M
489
+ start_m_idx = start_m * BLOCK_M
490
+ causal_start_idx = seqlen_q - seqlen_k
491
+ acc = acc.to(Out.type.element_ty)
492
+ if IS_CAUSAL:
493
+ if causal_start_idx > start_m_idx and causal_start_idx < end_m_idx:
494
+ out_mask_boundary = tl.full((BLOCK_DMODEL, ), causal_start_idx, dtype=tl.int32)
495
+ mask_m_offsets = start_m_idx + tl.arange(0, BLOCK_M)
496
+ out_ptrs_mask = mask_m_offsets[:, None] >= out_mask_boundary[None, :]
497
+ z = 0.0
498
+ acc = tl.where(out_ptrs_mask, acc, z.to(acc.type.element_ty))
499
+
500
+ # write back LSE(Log Sum Exponents), the log of the normalization constant
501
+ l_offset = LSE + off_z * stride_lse_z + off_h_q * stride_lse_h + cu_seqlens_q_start * stride_lse_m
502
+ l_ptrs = l_offset + offs_m * stride_lse_m
503
+ if USE_EXP2:
504
+ RCP_LN2: tl.constexpr = 1.4426950408889634
505
+ LN2: tl.constexpr = 0.6931471824645996
506
+ # compute log-sum-exp in base 2 units
507
+ mi_base2 = m_i * RCP_LN2
508
+ softmax_lse = mi_base2 + tl.math.log2(l_i)
509
+ # convert back to natural units
510
+ softmax_lse *= LN2
511
+ else:
512
+ softmax_lse = m_i + tl.math.log(l_i)
513
+
514
+ if IS_CAUSAL:
515
+ # zero out nans caused by -infs when doing causal
516
+ lse_mask = (start_m_idx + tl.arange(0, BLOCK_M)) < causal_start_idx
517
+ softmax_lse = tl.where(lse_mask, 0.0, softmax_lse)
518
+
519
+ # If seqlen_q not multiple of BLOCK_M, we need to mask out the last few rows.
520
+ # This is only true for the last M block. For others, overflow_size will be -ve
521
+ overflow_size = end_m_idx - seqlen_q
522
+ if overflow_size > 0:
523
+ boundary = tl.full((BLOCK_M, ), BLOCK_M - overflow_size, dtype=tl.int32)
524
+ l_ptrs_mask = tl.arange(0, BLOCK_M) < boundary
525
+ tl.store(l_ptrs, softmax_lse, mask=l_ptrs_mask) # the log of the normalization constant
526
+ else:
527
+ tl.store(l_ptrs, softmax_lse) # the log of the normalization constant
528
+
529
+ # write back O
530
+ o_offset = Out + off_z * stride_oz + off_h_q * stride_oh + cu_seqlens_q_start * stride_om
531
+ o_ptrs = o_offset + offs_m[:, None] * stride_om + offs_d[None, :] * stride_on
532
+ o_ptrs_mask = tl.full([BLOCK_M, BLOCK_DMODEL], 1, dtype=tl.int1)
533
+ if overflow_size > 0:
534
+ o_ptrs_mask = o_ptrs_mask & (offs_m[:, None] < seqlen_q)
535
+ if PADDED_HEAD:
536
+ o_ptrs_mask = o_ptrs_mask & (offs_d[None, :] < ACTUAL_BLOCK_DMODEL)
537
+ tl.store(o_ptrs, acc.to(Out.dtype.element_ty), mask=o_ptrs_mask)
538
+
539
+
540
+ def attention_prefill_forward_triton_impl(
541
+ q,
542
+ k,
543
+ v,
544
+ o,
545
+ sm_scale,
546
+ alibi_slopes,
547
+ causal,
548
+ bias,
549
+ dropout_p,
550
+ layout,
551
+ cu_seqlens_q,
552
+ cu_seqlens_k,
553
+ max_seqlens_q,
554
+ max_seqlens_k,
555
+ return_scores,
556
+ use_exp2):
557
+
558
+ if DEBUG:
559
+ print()
560
+ print("attention_prefill_forward_triton_impl")
561
+ print("q:", q, q.shape)
562
+ print("k:", k, k.shape)
563
+ print("v:", v, v.shape)
564
+ print("o:", o, o.shape)
565
+ print("sm_scale:", sm_scale)
566
+ print("alibi_slopes:", alibi_slopes)
567
+ print("causal:", causal)
568
+ print("bias:", bias)
569
+ print("dropout_p:", dropout_p)
570
+ print("layout:", layout)
571
+ print("cu_seqlens_q:", cu_seqlens_q)
572
+ print("cu_seqlens_k:", cu_seqlens_k)
573
+ print("max_seqlens_q:", max_seqlens_q)
574
+ print("max_seqlens_k:", max_seqlens_k)
575
+ print("return_scores:", return_scores)
576
+ print("use_exp2:", use_exp2)
577
+
578
+ # check if varlen
579
+ is_varlen = layout == "thd"
580
+
581
+ # NOTE: a large bias tensor leads to overflow during pointer arithmetic
582
+ if (bias is not None):
583
+ assert (bias.numel() < 2**31)
584
+
585
+ batch, nheads_q, nheads_k, head_size, seqlen_q, seqlen_k = get_shape_from_layout(q, k, layout, cu_seqlens_q, cu_seqlens_k, max_seqlens_q, max_seqlens_k)
586
+ q_strides, k_strides, v_strides, o_strides = get_strides_from_layout(q, k, v, o, layout)
587
+
588
+ # Get closest power of 2 over or equal to 32.
589
+ padded_d_model = 1 << (head_size - 1).bit_length()
590
+ # Smallest head_dim supported is 16. If smaller, the tile in the
591
+ # kernel is padded - there is no padding in memory for any dims.
592
+ padded_d_model = max(padded_d_model, 16)
593
+
594
+ grid = lambda META: (triton.cdiv(max_seqlens_q, META['BLOCK_M']), nheads_q, batch)
595
+
596
+ if return_scores:
597
+ scores = torch.zeros((batch, nheads_q, max_seqlens_q, max_seqlens_k), device=q.device,
598
+ dtype=torch.float32)
599
+ scores_scaled_shifted = torch.zeros((batch, nheads_q, max_seqlens_q, max_seqlens_k), device=q.device,
600
+ dtype=torch.float32)
601
+ scores_strides = (scores.stride(0), scores.stride(1), scores.stride(2), scores.stride(3))
602
+ else:
603
+ scores = None
604
+ scores_scaled_shifted = None
605
+ scores_strides = (0, 0 , 0 , 0)
606
+
607
+ # exp_scores is used to validate dropout behavior vs the PyTorch SDPA math backend reference. We zero this out
608
+ # to give a consistent starting point and then populate it with the output of softmax with the sign bit set according
609
+ # to the dropout mask. The resulting return allows this mask to be fed into the reference implementation for testing
610
+ # only. This return holds no useful output aside from debugging.
611
+ if return_scores:
612
+ exp_scores = torch.zeros((batch, nheads_q, max_seqlens_q, max_seqlens_k), device=q.device,
613
+ dtype=torch.float32)
614
+ else:
615
+ exp_scores = None
616
+
617
+ # stores LSE the log of the normalization constant / sum of expoential score(unnormalzied probablities)
618
+ if is_varlen:
619
+ softmax_lse = torch.empty((q.shape[0], nheads_q), device=q.device, dtype=torch.float32)
620
+ stride_lse_m, stride_lse_h = softmax_lse.stride()
621
+ stride_lse_z = 0
622
+ else:
623
+ softmax_lse = torch.empty((batch, nheads_q, max_seqlens_q), device=q.device, dtype=torch.float32)
624
+ stride_lse_z, stride_lse_h, stride_lse_m = softmax_lse.stride()
625
+
626
+ # Seed the RNG so we get reproducible results for testing.
627
+ philox_seed = 0x1BF52
628
+ philox_offset = 0x1D4B42
629
+
630
+ if bias is not None:
631
+ bias_strides = (bias.stride(0), bias.stride(1),bias.stride(2),
632
+ bias.stride(3))
633
+ else:
634
+ bias_strides = (0, 0, 0, 0)
635
+
636
+ if alibi_slopes is not None:
637
+ alibi_strides = (alibi_slopes.stride(0), alibi_slopes.stride(1))
638
+ else:
639
+ alibi_strides = (0, 0)
640
+
641
+
642
+ attn_fwd[grid](q, k, v, bias, sm_scale, softmax_lse, o, *q_strides, *k_strides, *v_strides, *o_strides,
643
+ *bias_strides, *alibi_strides, *scores_strides, stride_lse_z, stride_lse_h, stride_lse_m, cu_seqlens_q, cu_seqlens_k,
644
+ dropout_p=dropout_p, philox_seed=philox_seed, philox_offset_base=philox_offset, scores=scores,
645
+ scores_scaled_shifted=scores_scaled_shifted, exp_scores=exp_scores, alibi_slopes=alibi_slopes,
646
+ HQ=nheads_q, HK=nheads_k, ACTUAL_BLOCK_DMODEL=head_size, MAX_SEQLENS_Q=max_seqlens_q,
647
+ MAX_SEQLENS_K=max_seqlens_k, IS_CAUSAL=causal, VARLEN=is_varlen,
648
+ BLOCK_DMODEL=padded_d_model, USE_BIAS=False if bias is None else True,
649
+ USE_ALIBI=False if alibi_slopes is None else True, ENABLE_DROPOUT=dropout_p
650
+ > 0.0, USE_EXP2=use_exp2, RETURN_SCORES=return_scores)
651
+
652
+ return o, softmax_lse, exp_scores, grid, head_size, philox_seed, philox_offset, scores, scores_scaled_shifted
evalkit_tf446/lib/python3.10/site-packages/flash_attn/flash_attn_triton_amd/test.py ADDED
@@ -0,0 +1,724 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import pytest
3
+
4
+ from .utils import MetaData, get_input_shapes, input_helper, varlen_input_helper, DEBUG
5
+ from .interface_torch import attention_prefill, attention_decode
6
+ from .fwd_ref import attention_forward_pytorch_ref_impl, compute_alibi_tensor_ref
7
+ from .fwd_prefill import attention_prefill_forward_triton_impl
8
+ from .bwd_prefill import attention_prefill_backward_triton_impl
9
+ from .bwd_ref import attention_backward_pytorch_ref_impl
10
+ from .fwd_decode import dequantize_kv_fp16, quantize_kv_int4
11
+
12
+ # defailt fp16 tolerance is ATOL, RTOL = 1e-5, 1e-3. See table https://pytorch.org/docs/stable/testing.html
13
+ ATOL, RTOL = 1e-2, 1e-2 # old standard. maybe to lose.
14
+ # ATOL, RTOL = 1e-3, 1e-3 # catchs fa mismatch issues
15
+ # ATOL, RTOL = 1e-4, 1e-3 # to strict. there will be small diffs
16
+ # ATOL, RTOL = 1e-5, 1e-3 # # default fp16. there will be small diffs
17
+ EQUAL_NAN = True
18
+
19
+ @pytest.mark.parametrize('Z, HQ, HK, N_CTX_Q, N_CTX_K, D_HEAD', [
20
+ (4, 48, 24, 1024, 1024, 64),
21
+ (1, 24, 6, 8192, 8192, 64),
22
+ (1, 4, 2, 16384, 16384, 128),
23
+ (2, 16, 4, 1020, 987, 128),
24
+ (2, 16, 4, 15498, 2, 128),
25
+ (2, 16, 2, 7, 16219, 64),
26
+ (4, 48, 12, 1, 1, 64),
27
+ (4, 48, 48, 1, 1, 128),
28
+ (4, 48, 24, 3, 3, 128),
29
+ (4, 48, 48, 1001, 990, 64),
30
+ (1, 8, 8, 8081, 7099, 64),
31
+ (1, 4, 4, 16330, 15989, 128),
32
+ (4, 4, 1, 1024, 1024, 33),
33
+ (4, 4, 2, 65, 1018, 65),
34
+ (4, 4, 4, 128, 128, 65),
35
+ (4, 4, 4, 113, 123, 1),
36
+ ])
37
+ @pytest.mark.parametrize('causal', [True, False])
38
+ @pytest.mark.parametrize('use_alibi', [True, False])
39
+ @pytest.mark.parametrize('layout', ['bshd', 'bhsd'])
40
+ def test_op_fwd_prefill(Z, HQ, HK, N_CTX_Q, N_CTX_K, D_HEAD, causal, use_alibi, layout, dtype=torch.float16):
41
+ torch.manual_seed(20)
42
+ q, k, v, input_metadata = input_helper(Z, HQ, HK, N_CTX_Q, N_CTX_K, D_HEAD, dtype, layout)
43
+ if causal:
44
+ input_metadata.need_causal()
45
+
46
+ if use_alibi:
47
+ # for n heads the set of slopes is the geometric sequence that starts 2^(-8/n)
48
+ alibi_slopes = torch.tensor([2**(-8 / HQ * i) for i in range(1, HQ + 1)], dtype=torch.float32,
49
+ device="cuda").repeat(Z, 1)
50
+ input_metadata.need_alibi(alibi_slopes, Z, HQ)
51
+ else:
52
+ alibi_slopes = None
53
+
54
+ o = torch.empty_like(q)
55
+
56
+ # triton implementation
57
+ tri_out, _, _ = attention_prefill(q, k, v, o, input_metadata)
58
+
59
+ # Transpose here if layout is bshd so we have same reference code for all layouts
60
+ if layout == 'bshd':
61
+ q = q.transpose(1, 2).clone()
62
+ k = k.transpose(1, 2).clone()
63
+ v = v.transpose(1, 2).clone()
64
+ # Replicate K and V if using MQA/GQA
65
+ if HQ != HK:
66
+ k = k.view(k.shape[0], k.shape[1], -1, k.shape[2],
67
+ k.shape[3]).expand(-1, -1, HQ // HK, -1, -1).reshape(k.shape[0], -1, k.shape[2], k.shape[3])
68
+ v = v.view(v.shape[0], v.shape[1], -1, v.shape[2],
69
+ v.shape[3]).expand(-1, -1, HQ // HK, -1, -1).reshape(v.shape[0], -1, v.shape[2], v.shape[3])
70
+
71
+ scores = torch.einsum('bhqd,bhkd->bhqk', q, k).float() * input_metadata.sm_scale
72
+ if causal:
73
+ mask = torch.tril(torch.ones(N_CTX_Q, N_CTX_K, device="cuda"), diagonal=N_CTX_K - N_CTX_Q)
74
+ scores[:, :, mask == 0] = float("-inf")
75
+ if use_alibi:
76
+ scores += compute_alibi_tensor_ref(alibi_slopes, N_CTX_Q, N_CTX_K)
77
+
78
+ p = torch.softmax(scores, dim=-1)
79
+ if causal:
80
+ # If N_CTX_Q > N_CTX_K, there is at least one row of all -infs going into
81
+ # the softmax. This produces a row of NaNs as -inf - -inf == NaN. So we fix
82
+ # this by converting the NaNs to 0s, which is what they should be out of the softmax.
83
+ nan_mask = torch.isnan(p)
84
+ p[nan_mask == 1] = 0
85
+ ref_out = torch.einsum('bhqk,bhkd->bhqd', p.half(), v)
86
+ # compare
87
+ if layout == 'bshd':
88
+ ref_out = ref_out.transpose(1, 2).clone()
89
+ torch.testing.assert_close(ref_out, tri_out, atol=2e-2, rtol=2e-2)
90
+
91
+
92
+ @pytest.mark.parametrize('Z, H, N_CTX_Q, N_CTX_K, D_HEAD', [
93
+ (4, 48, 1024, 1024, 64),
94
+ (4, 12, 8192, 8192, 64),
95
+ (2, 4, 16384, 16384, 128),
96
+ (2, 16, 15498, 2, 128),
97
+ (2, 4, 7, 16219, 64),
98
+ (4, 48, 1, 1, 64),
99
+ (4, 48, 1, 1, 128),
100
+ (4, 48, 3, 3, 128),
101
+ (4, 48, 1001, 990, 64),
102
+ (1, 8, 8081, 7099, 64),
103
+ (1, 8, 16330, 15989, 128),
104
+ (4, 4, 1024, 1024, 33),
105
+ (4, 4, 65, 1019, 65),
106
+ (4, 4, 128, 128, 65),
107
+ # TODO: This config fails. Disabled until triaged and fixed.
108
+ # (2, 16, 1020, 987, 128),
109
+ # (4, 4, 113, 123, 1),
110
+ ])
111
+ @pytest.mark.parametrize('causal', [True, False])
112
+ @pytest.mark.parametrize('use_bias', [True])
113
+ def test_op_fwd_prefill_bias(Z, H, N_CTX_Q, N_CTX_K, D_HEAD, causal, use_bias, dtype=torch.float16):
114
+ torch.manual_seed(20)
115
+ sm_scale = D_HEAD**-0.5
116
+ input_metadata = MetaData(sm_scale=sm_scale)
117
+ q, k, v, input_metadata = input_helper(Z, H, H, N_CTX_Q, N_CTX_K, D_HEAD, dtype, layout='bhsd')
118
+ if causal:
119
+ input_metadata.need_causal()
120
+ if use_bias:
121
+ bias = torch.randn((1, H, N_CTX_Q, N_CTX_K), dtype=torch.float32, device="cuda")
122
+ input_metadata.need_bias(bias, Z, H, N_CTX_Q, N_CTX_K)
123
+ else:
124
+ bias = None
125
+ o = torch.empty_like(q)
126
+
127
+ # triton implementation
128
+ tri_out, _, _ = attention_prefill(q, k, v, o, input_metadata)
129
+ # reference implementation:171
130
+
131
+ scores = torch.einsum('bhqd,bhkd->bhqk', q, k).float() * sm_scale
132
+ if causal:
133
+ mask = torch.tril(torch.ones(N_CTX_Q, N_CTX_K, device="cuda"), diagonal=N_CTX_K - N_CTX_Q)
134
+ scores[:, :, mask == 0] = float("-inf")
135
+ if use_bias:
136
+ scores += input_metadata.bias
137
+ p = torch.softmax(scores, dim=-1)
138
+ if causal:
139
+ # If N_CTX_Q > N_CTX_K, there is at least one row of all -infs going into
140
+ # the softmax. This produces a row of NaNs as -inf - -inf == NaN. So we fix
141
+ # this by converting the NaNs to 0s, which is what they should be out of the softmax.
142
+ nan_mask = torch.isnan(p)
143
+ p[nan_mask == 1] = 0
144
+ ref_out = torch.einsum('bhqk,bhkd->bhqd', p.half(), v)
145
+ # compare
146
+ torch.testing.assert_close(ref_out, tri_out, atol=2e-2, rtol=2e-2)
147
+
148
+
149
+ @pytest.mark.parametrize('Z, H, N_CTX, D_HEAD', [
150
+ (4, 48, 8192, 64),
151
+ (4, 48, 256, 64),
152
+ (4, 48, 512, 64),
153
+ (4, 48, 1024, 64),
154
+ (8, 48, 4096, 64),
155
+ (4, 48, 8192, 64),
156
+ (4, 48, 128, 128),
157
+ (4, 48, 4096, 128),
158
+ (4, 48, 16384, 128),
159
+ (4, 16, 1024, 128),
160
+ (4, 16, 8192, 128),
161
+ (32, 48, 8192, 128)
162
+ ]
163
+ )
164
+ @pytest.mark.parametrize('causal', [True, False])
165
+ def test_op_varlen_fwd(Z, H, N_CTX, D_HEAD, causal, dtype=torch.float16):
166
+
167
+ q, k, v, input_metadata = varlen_input_helper(Z, H, H, N_CTX, N_CTX, D_HEAD, dtype)
168
+
169
+ tri_out = torch.empty_like(q)
170
+ ref_out = torch.empty_like(q)
171
+
172
+ for i in range(0, input_metadata.num_contexts):
173
+ start_q, start_k = input_metadata.cu_seqlens_q[i], input_metadata.cu_seqlens_k[i]
174
+ end_q, end_k = input_metadata.cu_seqlens_q[i + 1], input_metadata.cu_seqlens_k[i + 1]
175
+ scores = torch.einsum('qhd,khd->qhk', q[start_q:end_q], k[start_k:end_k]).float()
176
+ p = torch.softmax(scores * input_metadata.sm_scale, dim=-1).half()
177
+ ref_out[start_q:end_q] = torch.einsum('qhk,khd->qhd', p, v[start_k:end_k])
178
+ attention_prefill(q, k, v, tri_out, input_metadata)
179
+ torch.testing.assert_close(ref_out, tri_out, atol=ATOL, rtol=RTOL)
180
+
181
+
182
+ @pytest.mark.parametrize('Z, HQ, HK, N_CTX, D_HEAD', [(2, 48, 24, 128, 64), (4, 48, 12, 256, 64), (4, 48, 4, 512, 64),
183
+ (4, 48, 2, 1024, 64), (8, 48, 6, 4096, 64), (4, 48, 8, 16384, 64),
184
+ (4, 64, 16, 128, 128), (4, 64, 4, 4096, 128),
185
+ (4, 64, 8, 16384, 128), (4, 16, 4, 1024, 128),
186
+ (4, 16, 2, 8192, 128), (32, 128, 32, 8192, 128)])
187
+ @pytest.mark.parametrize('causal', [False])
188
+ def test_op_varlen_mqa_fwd(Z, HQ, HK, N_CTX, D_HEAD, causal, dtype=torch.float16):
189
+ q, k, v, input_metadata = varlen_input_helper(Z, HQ, HK, N_CTX, N_CTX, D_HEAD, dtype)
190
+ ref_out = torch.empty_like(q)
191
+ tri_out = torch.empty_like(q)
192
+ # Make KV look like HQ/HK "groups" of HK. Later, we will reshape so the
193
+ # size aligns with Q.
194
+ k_ref = k.view(k.shape[0], k.shape[1], 1, k.shape[2]).expand(-1, -1, HQ // HK, -1)
195
+ v_ref = v.view(v.shape[0], v.shape[1], 1, v.shape[2]).expand(-1, -1, HQ // HK, -1)
196
+ for i in range(0, input_metadata.num_contexts):
197
+ start_q, start_k = input_metadata.cu_seqlens_q[i], input_metadata.cu_seqlens_k[i]
198
+ end_q, end_k = input_metadata.cu_seqlens_q[i + 1], input_metadata.cu_seqlens_k[i + 1]
199
+ k_curr = k_ref[start_k:end_k]
200
+ k_curr = k_curr.reshape(k_curr.shape[0], -1, k_curr.shape[3])
201
+ v_curr = v_ref[start_k:end_k]
202
+ v_curr = v_curr.reshape(v_curr.shape[0], -1, v_curr.shape[3])
203
+ scores = torch.einsum('qhd,khd->qhk', q[start_q:end_q], k_curr).float()
204
+ p = torch.softmax(scores * input_metadata.sm_scale, dim=-1).half()
205
+ ref_out[start_q:end_q] = torch.einsum('qhk,khd->qhd', p, v_curr)
206
+ attention_prefill(q, k, v, tri_out, input_metadata)
207
+ torch.testing.assert_close(ref_out, tri_out, atol=ATOL, rtol=RTOL)
208
+
209
+
210
+ @pytest.mark.parametrize('Z, H, N_CTX_Q, N_CTX_K, D_HEAD', [
211
+ # smallest config test
212
+ (1, 1, 16, 16, 64), # pass on new # fail on old
213
+ (1, 1, 32, 32, 64), # pass on new # fail on old
214
+ (1, 1, 64, 64, 16), # pass # smallest head_size = 16
215
+ (1, 1, 64, 64, 64), # pass # smallest seq len seems to be 64
216
+ (1, 1, 128, 128, 64), # pass
217
+ (1, 1, 256, 256, 64), # pass
218
+ (1, 1, 512, 512, 64), # pass
219
+ # failing FA
220
+ (1, 1, 256, 512, 16),
221
+ # old tests that work
222
+ (4, 48, 1024, 1024, 64), # pass
223
+ (4, 48, 2048, 2048, 64), # pass
224
+ (2, 48, 4096, 4096, 64), # pass
225
+ (1, 16, 1024, 1024, 64), # pass
226
+ (1, 16, 1024, 1024, 128), # pass
227
+ # old tests that were commented out
228
+ # (1, 16, 8192, 8192, 63),
229
+ # (1, 16, 1022, 1022, 64),
230
+ ])
231
+ # @pytest.mark.parametrize('torch_sdpa_test', [False, True])
232
+ @pytest.mark.parametrize('torch_sdpa_test', [False])
233
+ # @pytest.mark.parametrize('causal', [True, False])
234
+ @pytest.mark.parametrize('causal', [False])
235
+ # @pytest.mark.parametrize('use_alibi', [False, True])
236
+ @pytest.mark.parametrize('use_alibi', [False])
237
+ def test_op_bwd(Z, H, N_CTX_Q, N_CTX_K, D_HEAD, causal, torch_sdpa_test, use_alibi, dtype=torch.float16):
238
+ torch.manual_seed(20)
239
+
240
+ DEBUG_INPUT = False
241
+
242
+ # seqlens
243
+ seqlen_q = N_CTX_Q
244
+ seqlen_k = N_CTX_K
245
+
246
+ # setup up metadata
247
+ if DEBUG_INPUT:
248
+ sm_scale = 1
249
+ else:
250
+ sm_scale = D_HEAD**-0.5
251
+ input_metadata = MetaData(sm_scale=sm_scale)
252
+ input_metadata.max_seqlens_q = seqlen_q
253
+ input_metadata.max_seqlens_k = seqlen_k
254
+ input_metadata.layout = "bhsd"
255
+
256
+ dropout_p = 0
257
+ if DEBUG_INPUT:
258
+ q = torch.arange(seqlen_q, dtype=dtype, device="cuda").view(1, 1, seqlen_q, 1).expand(Z, H, seqlen_q, D_HEAD).requires_grad_()
259
+ k = torch.arange(seqlen_k, dtype=dtype, device="cuda").view(1, 1, seqlen_k, 1).expand(Z, H, seqlen_k, D_HEAD).requires_grad_()
260
+ v = torch.arange(seqlen_k, dtype=dtype, device="cuda").view(1, 1, seqlen_k, 1).expand(Z, H, seqlen_k, D_HEAD).requires_grad_()
261
+ o = torch.zeros_like(q)
262
+ else:
263
+ # Generate random inputs
264
+ q = torch.randn(Z, H, N_CTX_Q, D_HEAD, device='cuda', dtype=dtype, requires_grad=True)
265
+ k = torch.randn(Z, H, N_CTX_K, D_HEAD, device='cuda', dtype=dtype, requires_grad=True)
266
+ v = torch.randn(Z, H, N_CTX_K, D_HEAD, device='cuda', dtype=dtype, requires_grad=True)
267
+ o = torch.empty_like(q)
268
+
269
+ if causal:
270
+ input_metadata.need_causal()
271
+
272
+ if use_alibi and not torch_sdpa_test:
273
+ # for n heads the set of slopes is the geometric sequence that starts 2^(-8/n)
274
+ alibi_slopes = torch.tensor([2**(-8 / H * i) for i in range(1, H + 1)], dtype=torch.float32,
275
+ device="cuda").repeat(Z, 1)
276
+ input_metadata.need_alibi(alibi_slopes, Z, H)
277
+
278
+ if DEBUG_INPUT:
279
+ dout = torch.ones_like(q)
280
+ else:
281
+ dout = torch.randn_like(q)
282
+
283
+ # reference implementation
284
+ if torch_sdpa_test:
285
+ ref_out, ref_softmax = torch.ops.aten._scaled_dot_product_attention_math(q, k, v, dropout_p=dropout_p,
286
+ is_causal=causal, scale=sm_scale,
287
+ dropout_mask=None)
288
+ ref_out.backward(dout.to(device=ref_out.device, dtype=ref_out.dtype))
289
+ ref_dv, v.grad = v.grad.clone(), None
290
+ ref_dk, k.grad = k.grad.clone(), None
291
+ ref_dq, q.grad = q.grad.clone(), None
292
+ else:
293
+ M = torch.tril(torch.ones((seqlen_q, seqlen_k), device="cuda"))
294
+ p = torch.matmul(q, k.transpose(2, 3)) * sm_scale
295
+ if use_alibi:
296
+ p += compute_alibi_tensor_ref(alibi_slopes, N_CTX_Q, N_CTX_K)
297
+ if causal:
298
+ p[:, :, M == 0] = float("-inf")
299
+
300
+ p = torch.softmax(p.float(), dim=-1).type(dtype=p.dtype)
301
+ ref_out = torch.matmul(p, v)
302
+ ref_out.backward(dout)
303
+ ref_dv, v.grad = v.grad.clone(), None
304
+ ref_dk, k.grad = k.grad.clone(), None
305
+ ref_dq, q.grad = q.grad.clone(), None
306
+
307
+ # # triton implementation
308
+ tri_out, _, _ = attention_prefill(q, k, v, o, input_metadata)
309
+ tri_out.backward(dout)
310
+ tri_dv, v.grad = v.grad.clone(), None
311
+ tri_dk, k.grad = k.grad.clone(), None
312
+ tri_dq, q.grad = q.grad.clone(), None
313
+ # compare
314
+ if DEBUG:
315
+ print("tri_out:", tri_out)
316
+ print("ref_out:",ref_out )
317
+ torch.testing.assert_close(ref_out, tri_out, atol=1e-2, rtol=0)
318
+
319
+ # The current block size for MI200 series is 64x64. This results in
320
+ # larger differences in float results due to rounding.
321
+ if dtype == torch.bfloat16:
322
+ ATOL = 1e-1 * max(1.0, (seqlen_q + D_HEAD) / 64.0)
323
+ if dtype == torch.float32:
324
+ ATOL = 1e-3 * max(1.0, (seqlen_q + D_HEAD) / 64.0)
325
+ else:
326
+ ATOL = 1e-1 * max(1.0, (seqlen_q + D_HEAD) / 64.0)
327
+
328
+ RTOL = 0
329
+
330
+ if DEBUG:
331
+ print("ref_dv:", ref_dv)
332
+ print("tri_dv:", tri_dv)
333
+ print("ref_dk:", ref_dk)
334
+ print("tri_dk:", tri_dk)
335
+ print("ref_dq:", ref_dq)
336
+ print("tri_dq:", tri_dq)
337
+
338
+ torch.testing.assert_close(ref_dv, tri_dv, atol=ATOL, rtol=RTOL)
339
+ torch.testing.assert_close(ref_dk, tri_dk, atol=ATOL, rtol=RTOL)
340
+ torch.testing.assert_close(ref_dq, tri_dq, atol=ATOL, rtol=RTOL)
341
+
342
+
343
+ @pytest.mark.parametrize('Z, H, N_CTX_Q, N_CTX_K, D_HEAD', [
344
+ (1, 1, 1, 1, 1),
345
+ (1, 1, 2, 4, 16),
346
+ (1, 1, 4, 2, 16),
347
+ (1, 1, 4, 4, 16),
348
+ (1, 2, 4, 4, 16),
349
+ (2, 1, 4, 4, 16),
350
+ (2, 2, 4, 4, 16),
351
+ (1, 1, 128, 64, 16),
352
+ (2, 2, 2, 128, 1),
353
+ (2, 3, 2, 128, 16),
354
+ (3, 2, 256, 512, 16),
355
+ (3, 3, 128, 128, 64),
356
+ (2, 4, 1024, 1024, 64),
357
+ (4, 6, 108, 256, 224),
358
+ (4, 8, 2048, 2048, 128),
359
+ (4, 16, 4096, 4096, 64),
360
+ (2, 4, 8192, 8192, 32),
361
+ # # fa configs
362
+ (4, 6, 113, 203, 256),
363
+ (4, 6, 128, 217, 256),
364
+ (4, 6, 113, 211, 128),
365
+ (4, 6, 108, 256, 128),
366
+ (4, 6, 256, 512, 64),
367
+ (4, 6, 512, 256, 64),
368
+ (4, 6, 1024, 1024, 32),
369
+ (4, 6, 1023, 1024, 32),
370
+ (4, 6, 1024, 1023, 32),
371
+ (4, 6, 2048, 2048, 32),
372
+ ])
373
+ @pytest.mark.parametrize('causal', [True, False])
374
+ @pytest.mark.parametrize('return_scores', [False])
375
+ @pytest.mark.parametrize('layout', ["bhsd", "bshd", "thd"])
376
+ @pytest.mark.parametrize('use_exp2', [True, False]) # works when use_exp2 is false
377
+ @pytest.mark.parametrize('DEBUG_INPUT', [False]) # NOTE: debug input can overflow when the tensors are large. Just use to figure out issues
378
+ def test_op_prefill_fwd_impl(Z, H, N_CTX_Q, N_CTX_K, D_HEAD, causal, return_scores, layout, use_exp2, DEBUG_INPUT):
379
+ dtype = torch.float16
380
+ torch.manual_seed(0)
381
+ alibi_slopes = None
382
+ dropout_p = 0.0
383
+ device = "cuda"
384
+
385
+ if layout == "thd":
386
+ q, k, v, metadata = varlen_input_helper(Z, H, H, N_CTX_Q, N_CTX_K, D_HEAD, dtype, device=device, DEBUG_INPUT=DEBUG_INPUT)
387
+ else:
388
+ q, k, v, metadata = input_helper(Z, H, H, N_CTX_Q, N_CTX_K, D_HEAD, dtype, layout, device=device, DEBUG_INPUT=DEBUG_INPUT)
389
+ if DEBUG_INPUT:
390
+ output_triton = torch.zeros_like(q).contiguous()
391
+ else:
392
+ output_triton = torch.empty_like(q)
393
+
394
+ # update metadata
395
+ metadata.use_exp2 = use_exp2
396
+ if causal:
397
+ metadata.need_causal()
398
+
399
+ # NOTE: the returned score is not the same as the reference because we need to adjust as we find new maxes per block. We are not doing that
400
+ if return_scores:
401
+ metadata.return_scores = True
402
+
403
+ # call Triton's forward implementation directly
404
+ ( output_triton,
405
+ softmax_lse_triton,
406
+ exp_scores_triton,
407
+ _,
408
+ _,
409
+ _,
410
+ _,
411
+ _,
412
+ _) = attention_prefill_forward_triton_impl(
413
+ q,
414
+ k,
415
+ v,
416
+ output_triton,
417
+ metadata.sm_scale,
418
+ metadata.alibi_slopes,
419
+ metadata.causal,
420
+ metadata.bias,
421
+ metadata.dropout_p,
422
+ metadata.layout,
423
+ metadata.cu_seqlens_q,
424
+ metadata.cu_seqlens_k,
425
+ metadata.max_seqlens_q,
426
+ metadata.max_seqlens_k,
427
+ metadata.return_scores,
428
+ metadata.use_exp2)
429
+
430
+ (
431
+ output_ref,
432
+ softmax_lse_ref,
433
+ exp_scores_ref,
434
+ softmax_ref,
435
+ attention_shifted_scaled_scores_ref,
436
+ attention_scaled_scores_ref,
437
+ attention_scores_ref,
438
+ ) = attention_forward_pytorch_ref_impl(
439
+ q.clone(),
440
+ k.clone(),
441
+ v.clone(),
442
+ metadata.sm_scale,
443
+ causal,
444
+ layout,
445
+ metadata.cu_seqlens_q,
446
+ metadata.cu_seqlens_k,
447
+ metadata.max_seqlens_q,
448
+ metadata.max_seqlens_k,
449
+ use_exp2
450
+ )
451
+
452
+ if DEBUG:
453
+ print("softmax_lse_triton:", softmax_lse_triton, softmax_lse_triton.shape)
454
+ print("softmax_lse_ref:", softmax_lse_ref, softmax_lse_ref.shape)
455
+ torch.testing.assert_close(softmax_lse_triton, softmax_lse_ref, atol=ATOL, rtol=RTOL)
456
+
457
+ if layout != "thd":
458
+ # use trick with lse to get the softmax. you need the scores but is it
459
+ softmax_triton = torch.exp(attention_scaled_scores_ref - softmax_lse_triton.unsqueeze(-1))
460
+ if DEBUG:
461
+ print("attention_scaled_scores_ref:", attention_scaled_scores_ref, attention_scaled_scores_ref.shape)
462
+ print("softmax_lse_triton:", softmax_lse_triton, softmax_lse_triton.shape)
463
+ print("softmax_triton:", softmax_triton, softmax_triton.shape)
464
+ print("softmax_ref:", softmax_ref, softmax_ref.shape)
465
+ torch.testing.assert_close(softmax_triton, softmax_ref, atol=ATOL, rtol=RTOL)
466
+
467
+ if DEBUG:
468
+ print("output_triton:", output_triton, output_triton.shape)
469
+ print("output_ref:", output_ref, output_ref.shape)
470
+ torch.testing.assert_close(output_triton, output_ref, atol=ATOL, rtol=RTOL)
471
+
472
+
473
+ # compare with pytorch expect thd and causal impl is different
474
+ if False and layout in ["bhsd", "bshd"] and not causal:
475
+ out_pytorch, softmax_pytorch = torch.ops.aten._scaled_dot_product_attention_math(
476
+ q.transpose(1, 2) if layout == "bshd" else q ,
477
+ k.transpose(1, 2) if layout == "bshd" else k,
478
+ v.transpose(1, 2) if layout == "bshd" else v,
479
+ dropout_p=dropout_p,
480
+ is_causal=causal, scale=metadata.sm_scale,
481
+ dropout_mask=None)
482
+ out_pytorch = out_pytorch.transpose(1, 2) if layout == "bshd" else out_pytorch
483
+
484
+ if DEBUG:
485
+ print("o:", output_triton, output_triton.shape)
486
+ print("out_pytorch:", out_pytorch, out_pytorch.shape)
487
+ torch.testing.assert_close(output_triton, out_pytorch, atol=ATOL, rtol=RTOL)
488
+
489
+ # compare with pytorch output
490
+ if DEBUG:
491
+ print("softmax_triton:", softmax_triton, softmax_triton.shape)
492
+ print("softmax_pytorch:", softmax_pytorch, softmax_pytorch.shape)
493
+ torch.testing.assert_close(softmax_triton, softmax_pytorch.to(torch.float32), atol=ATOL, rtol=RTOL)
494
+
495
+
496
+ @pytest.mark.parametrize('Z, H, N_CTX_Q, N_CTX_K, D_HEAD', [
497
+ (1, 1, 1, 1, 1),
498
+ (1, 1, 4, 4, 4),
499
+ (2, 1, 4, 4, 16),
500
+ (1, 2, 4, 4, 16),
501
+ (2, 2, 4, 4, 16),
502
+ (1, 1, 4, 4, 16),
503
+ (2, 1, 4, 4 , 16),
504
+ (4, 6, 8, 8 , 16),
505
+ (1, 1, 4, 4, 32),
506
+ (1, 1, 16, 16, 16),
507
+ (1, 1, 32, 32, 16),
508
+ (1, 1, 64, 64, 16),
509
+ (1, 1, 64, 64, 64),
510
+ (1, 1, 64, 128, 32),
511
+ (1, 1, 128, 128, 64),
512
+ (1, 1, 128, 256, 45),
513
+ (1, 1, 113, 203, 192),
514
+ (1, 1, 256, 256, 64),
515
+ (1, 1, 256, 512, 16),
516
+ (1, 1, 512, 512, 64),
517
+ (1, 1, 1024, 1024, 64),
518
+ # fa configs
519
+ (2, 2, 128, 128, 65),
520
+ (2, 2, 128, 128, 224),
521
+ (4, 6, 108, 256, 224),
522
+ (1, 1, 256, 512, 16),
523
+ # old tests that work
524
+ (4, 48, 1024, 1024, 73),
525
+ (4, 48, 1024, 1024, 64),
526
+ (4, 48, 2048, 2048, 64),
527
+ (1, 24, 4096, 4096, 64),
528
+ (1, 16, 1024, 1024, 64),
529
+ (1, 16, 1024, 1024, 128),
530
+ ])
531
+ @pytest.mark.parametrize('causal', [True, False])
532
+ @pytest.mark.parametrize('use_exp2', [False]) # FIXME: using exp2 causes issue when used with causal
533
+ @pytest.mark.parametrize('layout', ["bhsd", "bshd", "thd"])
534
+ @pytest.mark.parametrize('sequence_parallel', [True, False])
535
+ @pytest.mark.parametrize('DEBUG_INPUT', [False]) # debug output causes nans in both new and old backend
536
+ def test_op_prefill_bwd_impl(Z, H, N_CTX_Q, N_CTX_K, D_HEAD, causal, use_exp2, layout, sequence_parallel, DEBUG_INPUT):
537
+ dtype = torch.float16
538
+ torch.manual_seed(20) # seed from test_op_bwd
539
+
540
+ alibi_slopes = None
541
+ if layout == "thd":
542
+ q, k, v, metadata = varlen_input_helper(Z, H, H, N_CTX_Q, N_CTX_K, D_HEAD, dtype, DEBUG_INPUT=DEBUG_INPUT)
543
+ else:
544
+ q, k, v, metadata = input_helper(Z, H, H, N_CTX_Q, N_CTX_K, D_HEAD, dtype, layout, DEBUG_INPUT=DEBUG_INPUT)
545
+ if DEBUG_INPUT:
546
+ do = torch.ones_like(q).contiguous()
547
+ else:
548
+ do = torch.randn_like(q)
549
+
550
+ # =============================================== Reference ==============================================================
551
+ q_ref = q.clone()
552
+ k_ref = k.clone()
553
+ v_ref = v.clone()
554
+ (
555
+ o_ref,
556
+ softmax_lse_ref,
557
+ _,
558
+ _,
559
+ _,
560
+ _,
561
+ _,
562
+ ) = attention_forward_pytorch_ref_impl(
563
+ q_ref,
564
+ k_ref,
565
+ v_ref,
566
+ metadata.sm_scale,
567
+ causal,
568
+ layout,
569
+ metadata.cu_seqlens_q,
570
+ metadata.cu_seqlens_k,
571
+ metadata.max_seqlens_q,
572
+ metadata.max_seqlens_k,
573
+ use_exp2
574
+ )
575
+
576
+ dq = torch.zeros_like(q, dtype=q.dtype) # NOTE: the kernel does inplace accumlation on dq so dq has to be zeros
577
+ if DEBUG_INPUT:
578
+ dk = torch.zeros_like(k, dtype=k.dtype)
579
+ dv = torch.zeros_like(v, dtype=v.dtype)
580
+ else:
581
+ dk = torch.empty_like(k, dtype=k.dtype)
582
+ dv = torch.empty_like(v, dtype=v.dtype)
583
+
584
+ do_ref = do.clone()
585
+ dq_ref, dk_ref, dv_ref, delta_ref = attention_backward_pytorch_ref_impl(
586
+ do_ref,
587
+ q_ref,
588
+ k_ref,
589
+ v_ref,
590
+ o_ref,
591
+ softmax_lse_ref,
592
+ metadata.sm_scale,
593
+ causal,
594
+ layout,
595
+ metadata.cu_seqlens_q,
596
+ metadata.cu_seqlens_k,
597
+ metadata.max_seqlens_q,
598
+ metadata.max_seqlens_k,
599
+ use_exp2
600
+ )
601
+
602
+ # =============================================== Triton ==============================================================
603
+ o = o_ref.clone().contiguous()
604
+ softmax_lse = softmax_lse_ref.clone().contiguous()
605
+ dq_triton, dk_triton, dv_triton, delta_triton, _, _ = attention_prefill_backward_triton_impl(
606
+ do,
607
+ q,
608
+ k,
609
+ v,
610
+ o,
611
+ softmax_lse,
612
+ dq,
613
+ dk,
614
+ dv,
615
+ metadata.sm_scale,
616
+ alibi_slopes,
617
+ causal,
618
+ layout,
619
+ metadata.cu_seqlens_q,
620
+ metadata.cu_seqlens_k,
621
+ metadata.max_seqlens_q,
622
+ metadata.max_seqlens_k,
623
+ use_exp2,
624
+ sequence_parallel=sequence_parallel
625
+ )
626
+
627
+ # =============================================== Check ==============================================================
628
+ if DEBUG:
629
+ print()
630
+ if DEBUG:
631
+ print("delta_triton:", delta_triton, delta_triton.shape)
632
+ print("delta_ref:", delta_ref, delta_ref.shape)
633
+ torch.testing.assert_close(delta_triton, delta_ref, atol=ATOL, rtol=RTOL, equal_nan=EQUAL_NAN)
634
+
635
+ if DEBUG:
636
+ print("dv_triton:", dv_triton, dv_triton.shape)
637
+ print("dv_ref:", dv_ref, dv_ref.shape)
638
+ torch.testing.assert_close(dv_triton, dv_ref, atol=ATOL, rtol=RTOL, equal_nan=EQUAL_NAN)
639
+
640
+ if DEBUG:
641
+ print("dk_triton:", dk_triton, dk_triton.shape)
642
+ print("dk_ref:", dk_ref, dk_ref.shape)
643
+ torch.testing.assert_close(dk_triton, dk_ref, atol=ATOL, rtol=RTOL, equal_nan=EQUAL_NAN)
644
+
645
+ if DEBUG:
646
+ print("dq_triton:", dq_triton, dq_triton.shape)
647
+ print("dq_ref:", dq_ref, dq_ref.shape)
648
+ torch.testing.assert_close(dq_triton, dq_ref, atol=ATOL, rtol=RTOL, equal_nan=EQUAL_NAN)
649
+
650
+
651
+ @pytest.mark.parametrize('batch_size, seqlen_q, seqlen_k, group_q, group_k, dim', get_input_shapes())
652
+ def test_op_fwd_decode(batch_size, seqlen_q, seqlen_k, group_q, group_k, dim, dtype=torch.bfloat16):
653
+ if DEBUG:
654
+ print()
655
+ print(f"batch_size = {batch_size}, seqlen_q = {seqlen_q}, seqlen_k = {seqlen_k}, group_q = {group_q}, group_k = {group_k}, dim = {dim}")
656
+ torch.manual_seed(20)
657
+ query_group_head_size = (group_q + group_k - 1) // group_k
658
+ q = (torch.empty((batch_size, seqlen_q, group_k, query_group_head_size, dim), dtype=dtype,
659
+ device="cuda").normal_(mean=0., std=0.5).requires_grad_())
660
+ k = (torch.empty((batch_size, seqlen_k, group_k, 1, dim), dtype=dtype,
661
+ device="cuda").normal_(mean=0.,
662
+ std=0.5).requires_grad_()).expand(-1, -1, -1, query_group_head_size, -1)
663
+ v = (torch.empty((batch_size, seqlen_k, group_k, 1, dim), dtype=dtype,
664
+ device="cuda").normal_(mean=0.,
665
+ std=0.5).requires_grad_()).expand(-1, -1, -1, query_group_head_size, -1)
666
+ scale = 1 / dim**0.5
667
+ input_metadata = MetaData(sm_scale=scale)
668
+ input_metadata.layout = "bsghd"
669
+ tri_out, _ = attention_decode(q, k, v, input_metadata)
670
+
671
+ q = q.reshape([batch_size, seqlen_q, -1, dim]).permute(0, 2, 1, 3)
672
+ k = k.reshape([batch_size, seqlen_k, -1, dim]).permute(0, 2, 1, 3)
673
+ v = v.reshape([batch_size, seqlen_k, -1, dim]).permute(0, 2, 1, 3)
674
+ attn = (q @ k.transpose(-1, -2) * scale).softmax(-1)
675
+ ref_out = attn @ v
676
+
677
+ # compare
678
+ torch.testing.assert_close(ref_out, tri_out, atol=1e-3, rtol=0)
679
+
680
+ def test_quantization():
681
+ a = torch.randn((2, 4, 32), dtype=torch.float16, device='cuda')
682
+ qa = quantize_kv_int4(a, num_groups=4)
683
+ dqa = dequantize_kv_fp16(qa, num_groups=4)
684
+ torch.testing.assert_close(a, dqa, atol=1.5e-1, rtol=1e-1)
685
+
686
+ @pytest.mark.parametrize('B, Mq, Mkv, Hq, Hkv, K', get_input_shapes())
687
+ def test_op_fwd_decode_int4_kv(B, Mq, Mkv, Hq, Hkv, K, dtype=torch.float16):
688
+ pytest.skip("Decode kernel doesnot support quantization yet")
689
+ torch.manual_seed(2)
690
+ q = (torch.empty((B, Mq, Hkv, (Hq + Hkv - 1) // Hkv, K), dtype=dtype,
691
+ device="cuda").normal_(mean=1.0, std=0.5).requires_grad_())
692
+ k = (torch.empty((B, Mkv, Hkv, 1, K), dtype=dtype,
693
+ device="cuda").normal_(mean=1.0,
694
+ std=0.5).requires_grad_()).expand(-1, -1, -1, (Hq + Hkv - 1) // Hkv, -1)
695
+ v = (torch.empty((B, Mkv, Hkv, 1, K), dtype=dtype,
696
+ device="cuda").normal_(mean=1.0,
697
+ std=0.5).requires_grad_()).expand(-1, -1, -1, (Hq + Hkv - 1) // Hkv, -1)
698
+
699
+ num_groups = 1
700
+ quant_k = (quantize_kv_int4(k, num_groups=num_groups).contiguous().view(torch.int32))
701
+ quant_v = (quantize_kv_int4(v, num_groups=num_groups).contiguous().view(torch.int32))
702
+ scale = 1 / K**0.5
703
+ input_metadata = MetaData(sm_scale=scale)
704
+ input_metadata.layout = "bsghd"
705
+ tri_out, _ = attention_decode(q, quant_k, quant_v, input_metadata)
706
+
707
+ q = q.reshape([B, Mq, -1, K]).permute(0, 2, 1, 3)
708
+ k = k.reshape([B, Mkv, -1, K]).permute(0, 2, 1, 3)
709
+ v = v.reshape([B, Mkv, -1, K]).permute(0, 2, 1, 3)
710
+ attn = (q @ k.transpose(-1, -2) * scale).softmax(-1)
711
+ ref_out = attn @ v
712
+ # compare
713
+ torch.testing.assert_close(ref_out, tri_out, atol=2.1e-2, rtol=0)
714
+
715
+ # since quantization introduces rounding error, use the
716
+ # dequantized kv as inputs to the ref implementation to reduce
717
+ # the tolerance to 1e-3
718
+ dqk = dequantize_kv_fp16(quant_k, num_groups=num_groups)
719
+ dqv = dequantize_kv_fp16(quant_v, num_groups=num_groups)
720
+ dqk = dqk.reshape([B, Mkv, -1, K]).permute(0, 2, 1, 3)
721
+ dqv = dqv.reshape([B, Mkv, -1, K]).permute(0, 2, 1, 3)
722
+ dq_attn = (q @ dqk.transpose(-1, -2) * scale).softmax(-1)
723
+ dq_ref_out = dq_attn @ dqv
724
+ torch.testing.assert_close(dq_ref_out, tri_out, atol=1e-3, rtol=0)
evalkit_tf446/lib/python3.10/site-packages/flash_attn/flash_attn_triton_og.py ADDED
@@ -0,0 +1,365 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # [2022-10-23] Downloaded from https://github.com/openai/triton/blob/master/python/tutorials/06-fused-attention.py
2
+ # for benchmarking.
3
+ # We fixed a few dtype cast to make it work for bf16
4
+
5
+ """
6
+ Fused Attention
7
+ ===============
8
+ This is a Triton implementation of the Flash Attention algorithm
9
+ (see: Dao et al., https://arxiv.org/pdf/2205.14135v2.pdf; Rabe and Staats https://arxiv.org/pdf/2112.05682v2.pdf)
10
+ """
11
+
12
+ import pytest
13
+ import torch
14
+ import triton
15
+ import triton.language as tl
16
+
17
+
18
+ @triton.jit
19
+ def _fwd_kernel(
20
+ Q,
21
+ K,
22
+ V,
23
+ sm_scale,
24
+ TMP,
25
+ L,
26
+ M, # NOTE: TMP is a scratchpad buffer to workaround a compiler bug
27
+ Out,
28
+ stride_qz,
29
+ stride_qh,
30
+ stride_qm,
31
+ stride_qk,
32
+ stride_kz,
33
+ stride_kh,
34
+ stride_kn,
35
+ stride_kk,
36
+ stride_vz,
37
+ stride_vh,
38
+ stride_vk,
39
+ stride_vn,
40
+ stride_oz,
41
+ stride_oh,
42
+ stride_om,
43
+ stride_on,
44
+ Z,
45
+ H,
46
+ N_CTX,
47
+ BLOCK_M: tl.constexpr,
48
+ BLOCK_DMODEL: tl.constexpr,
49
+ BLOCK_N: tl.constexpr,
50
+ ):
51
+ start_m = tl.program_id(0)
52
+ off_hz = tl.program_id(1)
53
+ # initialize offsets
54
+ offs_m = start_m * BLOCK_M + tl.arange(0, BLOCK_M)
55
+ offs_n = tl.arange(0, BLOCK_N)
56
+ offs_d = tl.arange(0, BLOCK_DMODEL)
57
+ off_q = off_hz * stride_qh + offs_m[:, None] * stride_qm + offs_d[None, :] * stride_qk
58
+ off_k = off_hz * stride_qh + offs_n[:, None] * stride_kn + offs_d[None, :] * stride_kk
59
+ off_v = off_hz * stride_qh + offs_n[:, None] * stride_qm + offs_d[None, :] * stride_qk
60
+ # Initialize pointers to Q, K, V
61
+ q_ptrs = Q + off_q
62
+ k_ptrs = K + off_k
63
+ v_ptrs = V + off_v
64
+ # initialize pointer to m and l
65
+ t_ptrs = TMP + off_hz * N_CTX + offs_m
66
+ m_i = tl.zeros([BLOCK_M], dtype=tl.float32) - float("inf")
67
+ l_i = tl.zeros([BLOCK_M], dtype=tl.float32)
68
+ acc = tl.zeros([BLOCK_M, BLOCK_DMODEL], dtype=tl.float32)
69
+ # load q: it will stay in SRAM throughout
70
+ q = tl.load(q_ptrs)
71
+ # loop over k, v and update accumulator
72
+ for start_n in range(0, (start_m + 1) * BLOCK_M, BLOCK_N):
73
+ start_n = tl.multiple_of(start_n, BLOCK_N)
74
+ # -- compute qk ----
75
+ k = tl.load(k_ptrs + start_n * stride_kn)
76
+ qk = tl.zeros([BLOCK_M, BLOCK_N], dtype=tl.float32)
77
+ qk += tl.dot(q, k, trans_b=True)
78
+ qk *= sm_scale
79
+ qk += tl.where(offs_m[:, None] >= (start_n + offs_n[None, :]), 0, float("-inf"))
80
+ # -- compute m_ij, p, l_ij
81
+ m_ij = tl.max(qk, 1)
82
+ p = tl.exp(qk - m_ij[:, None])
83
+ l_ij = tl.sum(p, 1)
84
+ # -- update m_i and l_i
85
+ m_i_new = tl.maximum(m_i, m_ij)
86
+ alpha = tl.exp(m_i - m_i_new)
87
+ beta = tl.exp(m_ij - m_i_new)
88
+ l_i_new = alpha * l_i + beta * l_ij
89
+ # -- update output accumulator --
90
+ # scale p
91
+ p_scale = beta / l_i_new
92
+ p = p * p_scale[:, None]
93
+ # scale acc
94
+ acc_scale = l_i / l_i_new * alpha
95
+ tl.store(t_ptrs, acc_scale)
96
+ acc_scale = tl.load(t_ptrs) # BUG: have to store and immediately load
97
+ acc = acc * acc_scale[:, None]
98
+ # update acc
99
+ v = tl.load(v_ptrs + start_n * stride_vk)
100
+ p = p.to(v.dtype)
101
+ acc += tl.dot(p, v)
102
+ # update m_i and l_i
103
+ l_i = l_i_new
104
+ m_i = m_i_new
105
+ # rematerialize offsets to save registers
106
+ start_m = tl.program_id(0)
107
+ offs_m = start_m * BLOCK_M + tl.arange(0, BLOCK_M)
108
+ # write back l and m
109
+ l_ptrs = L + off_hz * N_CTX + offs_m
110
+ m_ptrs = M + off_hz * N_CTX + offs_m
111
+ tl.store(l_ptrs, l_i)
112
+ tl.store(m_ptrs, m_i)
113
+ # initialize pointers to output
114
+ offs_n = tl.arange(0, BLOCK_DMODEL)
115
+ off_o = off_hz * stride_oh + offs_m[:, None] * stride_om + offs_n[None, :] * stride_on
116
+ out_ptrs = Out + off_o
117
+ tl.store(out_ptrs, acc)
118
+
119
+
120
+ @triton.jit
121
+ def _bwd_preprocess(
122
+ Out,
123
+ DO,
124
+ L,
125
+ NewDO,
126
+ Delta,
127
+ BLOCK_M: tl.constexpr,
128
+ D_HEAD: tl.constexpr,
129
+ ):
130
+ off_m = tl.program_id(0) * BLOCK_M + tl.arange(0, BLOCK_M)
131
+ off_n = tl.arange(0, D_HEAD)
132
+ # load
133
+ o = tl.load(Out + off_m[:, None] * D_HEAD + off_n[None, :]).to(tl.float32)
134
+ do = tl.load(DO + off_m[:, None] * D_HEAD + off_n[None, :]).to(tl.float32)
135
+ denom = tl.load(L + off_m).to(tl.float32)
136
+ # compute
137
+ do = do / denom[:, None]
138
+ delta = tl.sum(o * do, axis=1)
139
+ # write-back
140
+ tl.store(NewDO + off_m[:, None] * D_HEAD + off_n[None, :], do)
141
+ tl.store(Delta + off_m, delta)
142
+
143
+
144
+ @triton.jit
145
+ def _bwd_kernel(
146
+ Q,
147
+ K,
148
+ V,
149
+ sm_scale,
150
+ Out,
151
+ DO,
152
+ DQ,
153
+ DK,
154
+ DV,
155
+ L,
156
+ M,
157
+ D,
158
+ stride_qz,
159
+ stride_qh,
160
+ stride_qm,
161
+ stride_qk,
162
+ stride_kz,
163
+ stride_kh,
164
+ stride_kn,
165
+ stride_kk,
166
+ stride_vz,
167
+ stride_vh,
168
+ stride_vk,
169
+ stride_vn,
170
+ Z,
171
+ H,
172
+ N_CTX,
173
+ num_block,
174
+ BLOCK_M: tl.constexpr,
175
+ BLOCK_DMODEL: tl.constexpr,
176
+ BLOCK_N: tl.constexpr,
177
+ ):
178
+ off_hz = tl.program_id(0)
179
+ off_z = off_hz // H
180
+ off_h = off_hz % H
181
+ # offset pointers for batch/head
182
+ Q += off_z * stride_qz + off_h * stride_qh
183
+ K += off_z * stride_qz + off_h * stride_qh
184
+ V += off_z * stride_qz + off_h * stride_qh
185
+ DO += off_z * stride_qz + off_h * stride_qh
186
+ DQ += off_z * stride_qz + off_h * stride_qh
187
+ DK += off_z * stride_qz + off_h * stride_qh
188
+ DV += off_z * stride_qz + off_h * stride_qh
189
+ for start_n in range(0, num_block):
190
+ lo = start_n * BLOCK_M
191
+ # initialize row/col offsets
192
+ offs_qm = lo + tl.arange(0, BLOCK_M)
193
+ offs_n = start_n * BLOCK_M + tl.arange(0, BLOCK_M)
194
+ offs_m = tl.arange(0, BLOCK_N)
195
+ offs_k = tl.arange(0, BLOCK_DMODEL)
196
+ # initialize pointers to value-like data
197
+ q_ptrs = Q + (offs_qm[:, None] * stride_qm + offs_k[None, :] * stride_qk)
198
+ k_ptrs = K + (offs_n[:, None] * stride_kn + offs_k[None, :] * stride_kk)
199
+ v_ptrs = V + (offs_n[:, None] * stride_qm + offs_k[None, :] * stride_qk)
200
+ do_ptrs = DO + (offs_qm[:, None] * stride_qm + offs_k[None, :] * stride_qk)
201
+ dq_ptrs = DQ + (offs_qm[:, None] * stride_qm + offs_k[None, :] * stride_qk)
202
+ # pointer to row-wise quantities in value-like data
203
+ D_ptrs = D + off_hz * N_CTX
204
+ m_ptrs = M + off_hz * N_CTX
205
+ # initialize dv amd dk
206
+ dv = tl.zeros([BLOCK_M, BLOCK_DMODEL], dtype=tl.float32)
207
+ dk = tl.zeros([BLOCK_M, BLOCK_DMODEL], dtype=tl.float32)
208
+ # k and v stay in SRAM throughout
209
+ k = tl.load(k_ptrs)
210
+ v = tl.load(v_ptrs)
211
+ # loop over rows
212
+ for start_m in range(lo, num_block * BLOCK_M, BLOCK_M):
213
+ offs_m_curr = start_m + offs_m
214
+ # load q, k, v, do on-chip
215
+ q = tl.load(q_ptrs)
216
+ # recompute p = softmax(qk, dim=-1).T
217
+ # NOTE: `do` is pre-divided by `l`; no normalization here
218
+ qk = tl.dot(q, k, trans_b=True)
219
+ qk = tl.where(offs_m_curr[:, None] >= (offs_n[None, :]), qk, float("-inf"))
220
+ m = tl.load(m_ptrs + offs_m_curr)
221
+ p = tl.exp(qk * sm_scale - m[:, None])
222
+ # compute dv
223
+ do = tl.load(do_ptrs)
224
+ dv += tl.dot(p.to(do.dtype), do, trans_a=True)
225
+ # compute dp = dot(v, do)
226
+ Di = tl.load(D_ptrs + offs_m_curr)
227
+ dp = tl.zeros([BLOCK_M, BLOCK_N], dtype=tl.float32) - Di[:, None]
228
+ dp += tl.dot(do, v, trans_b=True)
229
+ # compute ds = p * (dp - delta[:, None])
230
+ ds = p * dp * sm_scale
231
+ # compute dk = dot(ds.T, q)
232
+ dk += tl.dot(ds.to(q.dtype), q, trans_a=True)
233
+ # # compute dq
234
+ dq = tl.load(dq_ptrs, eviction_policy="evict_last")
235
+ dq += tl.dot(ds.to(k.dtype), k)
236
+ tl.store(dq_ptrs, dq, eviction_policy="evict_last")
237
+ # # increment pointers
238
+ dq_ptrs += BLOCK_M * stride_qm
239
+ q_ptrs += BLOCK_M * stride_qm
240
+ do_ptrs += BLOCK_M * stride_qm
241
+ # write-back
242
+ dv_ptrs = DV + (offs_n[:, None] * stride_qm + offs_k[None, :] * stride_qk)
243
+ dk_ptrs = DK + (offs_n[:, None] * stride_kn + offs_k[None, :] * stride_kk)
244
+ tl.store(dv_ptrs, dv)
245
+ tl.store(dk_ptrs, dk)
246
+
247
+
248
+ class _attention(torch.autograd.Function):
249
+ @staticmethod
250
+ def forward(ctx, q, k, v, sm_scale):
251
+ BLOCK = 128
252
+ # shape constraints
253
+ Lq, Lk, Lv = q.shape[-1], k.shape[-1], v.shape[-1]
254
+ assert Lq == Lk and Lk == Lv
255
+ assert Lk in {16, 32, 64, 128}
256
+ o = torch.empty_like(q)
257
+ grid = (triton.cdiv(q.shape[2], BLOCK), q.shape[0] * q.shape[1])
258
+ tmp = torch.empty(
259
+ (q.shape[0] * q.shape[1], q.shape[2]), device=q.device, dtype=torch.float32
260
+ )
261
+ L = torch.empty((q.shape[0] * q.shape[1], q.shape[2]), device=q.device, dtype=torch.float32)
262
+ m = torch.empty((q.shape[0] * q.shape[1], q.shape[2]), device=q.device, dtype=torch.float32)
263
+ num_warps = 4 if Lk <= 64 else 8
264
+
265
+ _fwd_kernel[grid](
266
+ q,
267
+ k,
268
+ v,
269
+ sm_scale,
270
+ tmp,
271
+ L,
272
+ m,
273
+ o,
274
+ q.stride(0),
275
+ q.stride(1),
276
+ q.stride(2),
277
+ q.stride(3),
278
+ k.stride(0),
279
+ k.stride(1),
280
+ k.stride(2),
281
+ k.stride(3),
282
+ v.stride(0),
283
+ v.stride(1),
284
+ v.stride(2),
285
+ v.stride(3),
286
+ o.stride(0),
287
+ o.stride(1),
288
+ o.stride(2),
289
+ o.stride(3),
290
+ q.shape[0],
291
+ q.shape[1],
292
+ q.shape[2],
293
+ BLOCK_M=BLOCK,
294
+ BLOCK_N=BLOCK,
295
+ BLOCK_DMODEL=Lk,
296
+ num_warps=num_warps,
297
+ num_stages=1,
298
+ )
299
+ ctx.save_for_backward(q, k, v, o, L, m)
300
+ ctx.BLOCK = BLOCK
301
+ ctx.grid = grid
302
+ ctx.sm_scale = sm_scale
303
+ ctx.BLOCK_DMODEL = Lk
304
+ return o
305
+
306
+ @staticmethod
307
+ def backward(ctx, do):
308
+ q, k, v, o, l, m = ctx.saved_tensors
309
+ do = do.contiguous()
310
+ dq = torch.zeros_like(q, dtype=torch.float32)
311
+ dk = torch.empty_like(k)
312
+ dv = torch.empty_like(v)
313
+ do_scaled = torch.empty_like(do)
314
+ delta = torch.empty_like(l)
315
+ _bwd_preprocess[(ctx.grid[0] * ctx.grid[1],)](
316
+ o,
317
+ do,
318
+ l,
319
+ do_scaled,
320
+ delta,
321
+ BLOCK_M=ctx.BLOCK,
322
+ D_HEAD=ctx.BLOCK_DMODEL,
323
+ )
324
+
325
+ # NOTE: kernel currently buggy for other values of `num_warps`
326
+ num_warps = 8
327
+ _bwd_kernel[(ctx.grid[1],)](
328
+ q,
329
+ k,
330
+ v,
331
+ ctx.sm_scale,
332
+ o,
333
+ do_scaled,
334
+ dq,
335
+ dk,
336
+ dv,
337
+ l,
338
+ m,
339
+ delta,
340
+ q.stride(0),
341
+ q.stride(1),
342
+ q.stride(2),
343
+ q.stride(3),
344
+ k.stride(0),
345
+ k.stride(1),
346
+ k.stride(2),
347
+ k.stride(3),
348
+ v.stride(0),
349
+ v.stride(1),
350
+ v.stride(2),
351
+ v.stride(3),
352
+ q.shape[0],
353
+ q.shape[1],
354
+ q.shape[2],
355
+ ctx.grid[0],
356
+ BLOCK_M=ctx.BLOCK,
357
+ BLOCK_N=ctx.BLOCK,
358
+ BLOCK_DMODEL=ctx.BLOCK_DMODEL,
359
+ num_warps=num_warps,
360
+ num_stages=1,
361
+ )
362
+ return dq.to(q.dtype), dk, dv, None
363
+
364
+
365
+ attention = _attention.apply
evalkit_tf446/lib/python3.10/site-packages/flash_attn/flash_blocksparse_attention.py ADDED
@@ -0,0 +1,197 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import math
2
+
3
+ import hydra
4
+ import torch
5
+ import torch.nn as nn
6
+ from einops import rearrange
7
+
8
+ from flash_attn.bert_padding import index_first_axis, pad_input, unpad_input
9
+ from flash_attn.flash_blocksparse_attn_interface import (
10
+ convert_blockmask,
11
+ flash_blocksparse_attn_func,
12
+ )
13
+
14
+
15
+ class FlashBlocksparseAttention(nn.Module):
16
+ """Implement the scaled dot product attention with softmax.
17
+ Arguments
18
+ ---------
19
+ softmax_temp: The temperature to use for the softmax attention.
20
+ (default: 1/sqrt(d_keys) where d_keys is computed at
21
+ runtime)
22
+ attention_dropout: The dropout rate to apply to the attention
23
+ (default: 0.1)
24
+ """
25
+
26
+ def __init__(
27
+ self,
28
+ sparsity_config,
29
+ softmax_temp=None,
30
+ attention_dropout=0.0,
31
+ max_seq_length=2048,
32
+ device=None,
33
+ dtype=None,
34
+ ):
35
+ super().__init__()
36
+ self.sparsity_config = hydra.utils.instantiate(sparsity_config)
37
+ self.softmax_temp = softmax_temp
38
+ self.dropout_p = attention_dropout
39
+
40
+ # initialize sparse layout and register as buffer
41
+ max_seq_length = ((max_seq_length + 256 - 1) // 256) * 256
42
+ layout = self.sparsity_config.make_layout(max_seq_length)
43
+ self.register_buffer("layout", layout)
44
+ blockmask_converted = convert_blockmask(self.layout, causal=False)
45
+ self.register_buffer("blockmask_converted", blockmask_converted)
46
+ # logger.info(f'Attention class {self.__class__}: saving={self.layout.float().mean()}')
47
+
48
+ def forward(
49
+ self,
50
+ qkv,
51
+ attn_mask=None,
52
+ key_padding_mask=None,
53
+ causal=False,
54
+ cu_seqlens=None,
55
+ max_s=None,
56
+ need_weights=False,
57
+ convert_mask=True,
58
+ ):
59
+ """Implements the multihead softmax attention.
60
+ Arguments
61
+ ---------
62
+ qkv: The tensor containing the query, key, and value. (B, S, 3, H, D) if key_padding_mask is None
63
+ attn_mask: An implementation of BaseMask that encodes where each
64
+ query can attend to
65
+ key_padding_mask: An implementation of BaseMask that encodes how
66
+ many query each sequence in the batch consists of
67
+ """
68
+ assert not need_weights
69
+ assert attn_mask is None
70
+ assert qkv.dtype == torch.float16
71
+ assert qkv.is_cuda
72
+
73
+ if cu_seqlens is None:
74
+ batch_size = qkv.shape[0]
75
+ seqlen = qkv.shape[1]
76
+ # Convert mask to take a subset
77
+ seqlen_rounded = ((seqlen + 256 - 1) // 256) * 256
78
+ assert seqlen_rounded // 16 <= self.layout.shape[0], (
79
+ seqlen_rounded // 256 <= self.layout.shape[1]
80
+ )
81
+ blockmask = self.layout[: seqlen_rounded // 16, : seqlen_rounded // 256]
82
+ if key_padding_mask is None:
83
+ qkv = rearrange(qkv, "b s ... -> (b s) ...")
84
+ max_s = seqlen
85
+ cu_seqlens = torch.arange(
86
+ 0, (batch_size + 1) * seqlen, step=seqlen, dtype=torch.int32, device=qkv.device
87
+ )
88
+ output = flash_blocksparse_attn_func(
89
+ qkv,
90
+ cu_seqlens,
91
+ blockmask,
92
+ self.dropout_p if self.training else 0.0,
93
+ max_s,
94
+ softmax_scale=self.softmax_temp,
95
+ causal=causal,
96
+ )
97
+ output = rearrange(output, "(b s) ... -> b s ...", b=batch_size)
98
+ else:
99
+ key_padding_mask_bool = key_padding_mask.bool_matrix
100
+ nheads = qkv.shape[-2]
101
+ x = rearrange(qkv, "b s three h d -> b s (three h d)")
102
+ x_unpad, indices, cu_seqlens, max_s, _ = unpad_input(x, key_padding_mask_bool)
103
+ x_unpad = rearrange(x_unpad, "nnz (three h d) -> nnz three h d", three=3, h=nheads)
104
+ output_unpad = flash_blocksparse_attn_func(
105
+ x_unpad,
106
+ cu_seqlens,
107
+ blockmask,
108
+ self.dropout_p if self.training else 0.0,
109
+ max_s,
110
+ softmax_scale=self.softmax_temp,
111
+ causal=causal,
112
+ )
113
+ output = rearrange(
114
+ pad_input(
115
+ rearrange(output_unpad, "nnz h d -> nnz (h d)"), indices, batch_size, seqlen
116
+ ),
117
+ "b s (h d) -> b s h d",
118
+ h=nheads,
119
+ )
120
+ else:
121
+ assert max_s is not None
122
+ seqlen = max_s
123
+ # Convert mask to take a subset
124
+ seqlen_rounded = ((seqlen + 256 - 1) // 256) * 256
125
+ assert seqlen_rounded // 16 <= self.layout.shape[0], (
126
+ seqlen_rounded // 256 <= self.layout.shape[1]
127
+ )
128
+ blockmask = self.layout[: seqlen_rounded // 16, : seqlen_rounded // 256]
129
+ if convert_mask:
130
+ output = flash_blocksparse_attn_func(
131
+ qkv,
132
+ cu_seqlens,
133
+ blockmask,
134
+ self.dropout_p if self.training else 0.0,
135
+ max_s,
136
+ softmax_scale=self.softmax_temp,
137
+ causal=causal,
138
+ )
139
+ else:
140
+ output = flash_blocksparse_attn_func(
141
+ qkv,
142
+ cu_seqlens,
143
+ self.blockmask_converted,
144
+ self.dropout_p if self.training else 0.0,
145
+ max_s,
146
+ softmax_scale=self.softmax_temp,
147
+ causal=causal,
148
+ convert_mask=False,
149
+ )
150
+
151
+ return output, None
152
+
153
+
154
+ class FlashBlocksparseMHA(nn.Module):
155
+ def __init__(
156
+ self,
157
+ embed_dim,
158
+ num_heads,
159
+ sparsity_config,
160
+ bias=True,
161
+ batch_first=True,
162
+ attention_dropout=0.0,
163
+ causal=False,
164
+ max_seq_length=2048,
165
+ device=None,
166
+ dtype=None,
167
+ **kwargs,
168
+ ) -> None:
169
+ assert batch_first
170
+ factory_kwargs = {"device": device, "dtype": dtype}
171
+ super().__init__()
172
+ self.embed_dim = embed_dim
173
+ self.causal = causal
174
+
175
+ self.num_heads = num_heads
176
+ assert self.embed_dim % num_heads == 0, "self.kdim must be divisible by num_heads"
177
+ self.head_dim = self.embed_dim // num_heads
178
+ assert self.head_dim in [16, 32, 64], "Only support head_dim == 16, 32, or 64"
179
+
180
+ self.Wqkv = nn.Linear(embed_dim, 3 * embed_dim, bias=bias, **factory_kwargs)
181
+ self.inner_attn = FlashBlocksparseAttention(
182
+ sparsity_config,
183
+ attention_dropout=attention_dropout,
184
+ max_seq_length=max_seq_length,
185
+ **factory_kwargs,
186
+ )
187
+ self.out_proj = nn.Linear(embed_dim, embed_dim, bias=bias, **factory_kwargs)
188
+
189
+ def forward(
190
+ self, x, x_ignored_, x_ignored_1_, attn_mask=None, key_padding_mask=None, need_weights=False
191
+ ):
192
+ qkv = self.Wqkv(x)
193
+ qkv = rearrange(qkv, "b s (three h d) -> b s three h d", three=3, h=self.num_heads)
194
+ context, attn_weights = self.inner_attn(
195
+ qkv, key_padding_mask=key_padding_mask, need_weights=need_weights, causal=self.causal
196
+ )
197
+ return self.out_proj(rearrange(context, "b s h d -> b s (h d)")), attn_weights
evalkit_tf446/lib/python3.10/site-packages/flash_attn/flash_blocksparse_attn_interface.py ADDED
@@ -0,0 +1,200 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Adapted from https://github.com/mlcommons/training_results_v1.1/blob/main/NVIDIA/benchmarks/bert/implementations/pytorch/fmha.py
2
+ import flash_attn_cuda
3
+ import torch
4
+ import torch.nn as nn
5
+
6
+
7
+ def convert_blockmask(blockmask, causal):
8
+ """Convert from the 0-1 format to the format used by the CUDA code.
9
+ 0 means the block is skipped.
10
+ nonzero means the block is not skipped.
11
+ Argument:
12
+ blockmask: (row, col): a 0-1 tensor
13
+ Return:
14
+ blockmask_converted: (col, row), dtype torch.int32: for each column, it contains the row
15
+ indices of the nonzero blocks, padded with -1 to reach length @row.
16
+ The indices are multiplied by 4, with the smallest bit used to encode whether
17
+ it is the first nonzero in its row, and the 2nd smallest bit to encode whether it is
18
+ the last nonzero in its row..
19
+ """
20
+ assert not causal
21
+ # TD [2022-05-13]: The indexing and sorting is very tricky
22
+ nrow, ncol = blockmask.shape
23
+ # Sort does not support bool on CUDA
24
+ blockmask = blockmask.to(dtype=torch.uint8)
25
+ nonzero_val, nonzero_sorted_rowidx = blockmask.sort(dim=0, stable=True, descending=True)
26
+ nonzero_unsorted_rowidx = nonzero_sorted_rowidx.argsort(dim=0)
27
+ last_nonzero_col_per_row = blockmask.sort(dim=-1, stable=True).indices[:, -1]
28
+ last_nonzero_col_per_row_after_sort = nonzero_unsorted_rowidx[
29
+ torch.arange(nrow, device=blockmask.device), last_nonzero_col_per_row
30
+ ]
31
+ first_nonzero_col_per_row = blockmask.sort(dim=-1, stable=True, descending=True).indices[:, 0]
32
+ first_nonzero_col_per_row_after_sort = nonzero_unsorted_rowidx[
33
+ torch.arange(nrow, device=blockmask.device), first_nonzero_col_per_row
34
+ ]
35
+ nonzero_idx = nonzero_sorted_rowidx * 4
36
+ nonzero_idx[last_nonzero_col_per_row_after_sort, last_nonzero_col_per_row] += 2
37
+ nonzero_idx[first_nonzero_col_per_row_after_sort, first_nonzero_col_per_row] += 1
38
+ nonzero_idx[nonzero_val == 0] = -1
39
+ return nonzero_idx.T.contiguous().to(dtype=torch.int32)
40
+
41
+
42
+ def _flash_blocksparse_attn_forward(
43
+ qkv, cu_seqlens, blockmask, dropout_p, max_s, softmax_scale, causal, return_softmax
44
+ ):
45
+ context, softmax_lse, *rest = flash_attn_cuda.fwd_block(
46
+ qkv, cu_seqlens, blockmask, dropout_p, max_s, softmax_scale, causal, return_softmax, None
47
+ )
48
+ # if context.isnan().any() or softmax_lse.isnan().any():
49
+ # breakpoint()
50
+ S_dmask = rest[0] if return_softmax else None
51
+ return context, softmax_lse, S_dmask
52
+
53
+
54
+ def _flash_blocksparse_attn_backward(
55
+ dout,
56
+ qkv,
57
+ out,
58
+ S_dmask,
59
+ softmax_lse,
60
+ cu_seqlens,
61
+ blockmask,
62
+ dropout_p,
63
+ max_s,
64
+ softmax_scale,
65
+ causal,
66
+ ):
67
+ dqkv, dp, softmax_d = flash_attn_cuda.bwd_block(
68
+ dout,
69
+ qkv,
70
+ out,
71
+ S_dmask,
72
+ softmax_lse,
73
+ cu_seqlens,
74
+ blockmask,
75
+ dropout_p,
76
+ softmax_scale,
77
+ max_s,
78
+ causal,
79
+ None,
80
+ )
81
+ # if dqkv.isnan().any() or softmax_d.isnan().any():
82
+ # breakpoint()
83
+ return dqkv
84
+
85
+
86
+ class FlashBlocksparseAttnFun(torch.autograd.Function):
87
+ @staticmethod
88
+ def forward(ctx, qkv, cu_seqlens, blockmask, dropout_p, max_s, softmax_scale, causal):
89
+ # Save rng_state because the backward pass will regenerate the dropout mask
90
+ rng_state = torch.cuda.get_rng_state() if dropout_p > 0 else None
91
+ if softmax_scale is None:
92
+ softmax_scale = qkv.shape[-1] ** (-0.5)
93
+ context, softmax_lse, S_dmask = _flash_blocksparse_attn_forward(
94
+ qkv,
95
+ cu_seqlens,
96
+ blockmask,
97
+ dropout_p,
98
+ max_s,
99
+ softmax_scale,
100
+ causal=causal,
101
+ return_softmax=False,
102
+ )
103
+ ctx.save_for_backward(qkv, context, S_dmask, softmax_lse, cu_seqlens, blockmask, rng_state)
104
+ ctx.dropout_p = dropout_p
105
+ ctx.max_s = max_s
106
+ ctx.softmax_scale = softmax_scale
107
+ ctx.causal = causal
108
+ return context
109
+
110
+ @staticmethod
111
+ def backward(ctx, dout):
112
+ qkv, context, S_dmask, softmax_lse, cu_seqlens, blockmask, rng_state = ctx.saved_tensors
113
+ if rng_state is not None:
114
+ cur_rng_state = torch.cuda.get_rng_state()
115
+ torch.cuda.set_rng_state(rng_state)
116
+ # S_dmask is None, temporarily use another tensor just to get it running
117
+ dqkv = _flash_blocksparse_attn_backward(
118
+ dout,
119
+ qkv,
120
+ context,
121
+ context,
122
+ softmax_lse,
123
+ cu_seqlens,
124
+ blockmask,
125
+ ctx.dropout_p,
126
+ ctx.max_s,
127
+ ctx.softmax_scale,
128
+ ctx.causal,
129
+ )
130
+ if rng_state is not None:
131
+ torch.cuda.set_rng_state(cur_rng_state)
132
+ return dqkv, None, None, None, None, None, None, None
133
+
134
+
135
+ # We duplicate code to return both the output and the softmax for testing
136
+ # Returning both makes backward a bit slower, so we want to keep using the other version for speed.
137
+ class FlashBlocksparseAttnFunWithS(torch.autograd.Function):
138
+ @staticmethod
139
+ def forward(ctx, qkv, cu_seqlens, blockmask, dropout_p, max_s, softmax_scale, causal):
140
+ # Save rng_state because the backward pass is gonna regenerate the dropout mask
141
+ rng_state = torch.cuda.get_rng_state() if dropout_p > 0 else None
142
+ if softmax_scale is None:
143
+ softmax_scale = qkv.shape[-1] ** (-0.5)
144
+ context, softmax_lse, S_dmask = _flash_blocksparse_attn_forward(
145
+ qkv,
146
+ cu_seqlens,
147
+ blockmask,
148
+ dropout_p,
149
+ max_s,
150
+ softmax_scale,
151
+ causal=causal,
152
+ return_softmax=True,
153
+ )
154
+ ctx.save_for_backward(qkv, context, S_dmask, softmax_lse, cu_seqlens, blockmask, rng_state)
155
+ ctx.dropout_p = dropout_p
156
+ ctx.max_s = max_s
157
+ ctx.softmax_scale = softmax_scale
158
+ ctx.causal = causal
159
+ return context, S_dmask, softmax_lse
160
+
161
+ @staticmethod
162
+ def backward(ctx, dout, _dS_dmask_ignored, _dsoftmax_sum_ignored):
163
+ qkv, context, S_dmask, softmax_lse, cu_seqlens, blockmask, rng_state = ctx.saved_tensors
164
+ if rng_state is not None:
165
+ cur_rng_state = torch.cuda.get_rng_state()
166
+ torch.cuda.set_rng_state(rng_state)
167
+ dqkv = _flash_blocksparse_attn_backward(
168
+ dout,
169
+ qkv,
170
+ context,
171
+ S_dmask,
172
+ softmax_lse,
173
+ cu_seqlens,
174
+ blockmask,
175
+ ctx.dropout_p,
176
+ ctx.max_s,
177
+ ctx.softmax_scale,
178
+ ctx.causal,
179
+ )
180
+ if rng_state is not None:
181
+ torch.cuda.set_rng_state(cur_rng_state)
182
+ return dqkv, None, None, None, None, None, None
183
+
184
+
185
+ def flash_blocksparse_attn_func(
186
+ qkv,
187
+ cu_seqlens,
188
+ blockmask,
189
+ dropout_p,
190
+ max_s,
191
+ softmax_scale=None,
192
+ causal=False,
193
+ return_attn_probs=False,
194
+ convert_mask=True,
195
+ ):
196
+ """dropout_p should be set to 0.0 during evaluation"""
197
+ func = FlashBlocksparseAttnFun if not return_attn_probs else FlashBlocksparseAttnFunWithS
198
+ if convert_mask:
199
+ blockmask = convert_blockmask(blockmask, causal=causal)
200
+ return func.apply(qkv, cu_seqlens, blockmask, dropout_p, max_s, softmax_scale, causal)
evalkit_tf446/lib/python3.10/site-packages/flash_attn/fused_softmax.py ADDED
@@ -0,0 +1,201 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # [2022-10-23] Copied from https://github.com/NVIDIA/apex/blob/master/apex/transformer/functional/fused_softmax.py
2
+ # for benchmarking.
3
+ # We added support for seqlen=2k and seqlen=4k
4
+
5
+ # coding=utf-8
6
+ # Copyright (c) 2021, NVIDIA CORPORATION. All rights reserved.
7
+ #
8
+ # Licensed under the Apache License, Version 2.0 (the "License");
9
+ # you may not use this file except in compliance with the License.
10
+ # You may obtain a copy of the License at
11
+ #
12
+ # http://www.apache.org/licenses/LICENSE-2.0
13
+ #
14
+ # Unless required by applicable law or agreed to in writing, software
15
+ # distributed under the License is distributed on an "AS IS" BASIS,
16
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
+ # See the License for the specific language governing permissions and
18
+ # limitations under the License.
19
+ import torch
20
+ from apex._autocast_utils import _cast_if_autocast_enabled
21
+ from apex.transformer.enums import AttnMaskType
22
+ from fused_softmax_lib import (
23
+ scaled_masked_softmax_backward,
24
+ scaled_masked_softmax_forward,
25
+ scaled_masked_softmax_get_batch_per_block,
26
+ scaled_upper_triang_masked_softmax_backward,
27
+ scaled_upper_triang_masked_softmax_forward,
28
+ )
29
+
30
+
31
+ class ScaledUpperTriangMaskedSoftmax(torch.autograd.Function):
32
+ """
33
+ Fused operation which performs following three operations in sequence
34
+ 1. Scale the tensor.
35
+ 2. Apply upper triangular mask (typically used in gpt models).
36
+ 3. Perform softmax.
37
+ """
38
+
39
+ @staticmethod
40
+ def forward(ctx, inputs, scale):
41
+ scale_t = torch.tensor([scale])
42
+ softmax_results = scaled_upper_triang_masked_softmax_forward(inputs, scale_t[0])
43
+ ctx.save_for_backward(softmax_results, scale_t)
44
+ return softmax_results
45
+
46
+ @staticmethod
47
+ def backward(ctx, output_grads):
48
+ softmax_results, scale_t = ctx.saved_tensors
49
+ input_grads = scaled_upper_triang_masked_softmax_backward(
50
+ output_grads, softmax_results, scale_t[0]
51
+ )
52
+ return input_grads, None
53
+
54
+
55
+ def scaled_upper_triang_masked_softmax(inputs, _, scale):
56
+ b, np, sq, sk = inputs.size()
57
+ assert sq == sk, "causal mask is only for self attention"
58
+ # Reshaping input to 3D tensor (attn_batches, sq, sk)
59
+ inputs = inputs.view(-1, sq, sk)
60
+ args = _cast_if_autocast_enabled(inputs, scale)
61
+ with torch.cuda.amp.autocast(enabled=False):
62
+ probs = ScaledUpperTriangMaskedSoftmax.apply(*args)
63
+ return probs.view(b, np, sq, sk)
64
+
65
+
66
+ # NOTE (mkozuki): `ScaledMaskedSoftmax` somehow doesn't work well with `torch.cuda.amp.custom_fwd`.
67
+ # Without `cast_inputs` kwarg, somehow inputs are not cast to dtype used in the autocast context.
68
+ # So I needed to manually write two `torch.autograd.Function` inheritances.
69
+ # Fused operation which performs following three operations in sequence
70
+ # 1. Scale the tensor.
71
+ # 2. Apply the mask.
72
+ # 3. Perform softmax.
73
+ class ScaledMaskedSoftmax(torch.autograd.Function):
74
+ @staticmethod
75
+ def forward(ctx, inputs, mask, scale):
76
+ scale_t = torch.tensor([scale])
77
+ softmax_results = scaled_masked_softmax_forward(inputs, mask, scale_t[0])
78
+ ctx.save_for_backward(softmax_results, scale_t)
79
+ return softmax_results
80
+
81
+ @staticmethod
82
+ def backward(ctx, output_grads):
83
+ softmax_results, scale_t = ctx.saved_tensors
84
+ input_grads = scaled_masked_softmax_backward(output_grads, softmax_results, scale_t[0])
85
+ return input_grads, None, None
86
+
87
+
88
+ def scaled_masked_softmax(inputs, mask, scale):
89
+ # input is 4D tensor (b, np, sq, sk)
90
+ args = _cast_if_autocast_enabled(inputs, mask, scale)
91
+ with torch.cuda.amp.autocast(enabled=False):
92
+ return ScaledMaskedSoftmax.apply(*args)
93
+
94
+
95
+ class FusedScaleMaskSoftmax(torch.nn.Module):
96
+ """
97
+ fused operation: scaling + mask + softmax
98
+
99
+ Arguments:
100
+ input_in_fp16: flag to indicate if input in fp16 data format.
101
+ input_in_bf16: flag to indicate if input in bf16 data format.
102
+ attn_mask_type: attention mask type (pad or causal)
103
+ scaled_masked_softmax_fusion: flag to indicate user want to use softmax fusion
104
+ mask_func: mask function to be applied.
105
+ softmax_in_fp32: if true, softmax in performed at fp32 precision.
106
+ scale: scaling factor used in input tensor scaling.
107
+ """
108
+
109
+ def __init__(
110
+ self,
111
+ input_in_fp16,
112
+ input_in_bf16,
113
+ attn_mask_type,
114
+ scaled_masked_softmax_fusion,
115
+ mask_func,
116
+ softmax_in_fp32,
117
+ scale,
118
+ ):
119
+ super().__init__()
120
+ self.input_in_fp16 = input_in_fp16
121
+ self.input_in_bf16 = input_in_bf16
122
+ if self.input_in_fp16 and self.input_in_bf16:
123
+ raise RuntimeError("both fp16 and bf16 flags cannot be active at the same time.")
124
+ self.input_in_float16 = self.input_in_fp16 or self.input_in_bf16
125
+ self.attn_mask_type = attn_mask_type
126
+ self.scaled_masked_softmax_fusion = scaled_masked_softmax_fusion
127
+ self.mask_func = mask_func
128
+ self.softmax_in_fp32 = softmax_in_fp32
129
+ self.scale = scale
130
+
131
+ if not (self.scale is None or softmax_in_fp32):
132
+ raise RuntimeError("softmax should be in fp32 when scaled")
133
+
134
+ if self.scaled_masked_softmax_fusion:
135
+ if self.attn_mask_type == AttnMaskType.causal:
136
+ self.fused_softmax_func = scaled_upper_triang_masked_softmax
137
+ elif self.attn_mask_type == AttnMaskType.padding:
138
+ self.fused_softmax_func = scaled_masked_softmax
139
+ else:
140
+ raise ValueError("Invalid attn_mask_type.")
141
+
142
+ def forward(self, input, mask):
143
+ # [b, np, sq, sk]
144
+ assert input.dim() == 4
145
+
146
+ if self.is_kernel_available(mask, *input.size()):
147
+ return self.forward_fused_softmax(input, mask)
148
+ else:
149
+ return self.forward_torch_softmax(input, mask)
150
+
151
+ def is_kernel_available(self, mask, b, np, sq, sk):
152
+ attn_batches = b * np
153
+
154
+ if (
155
+ self.scaled_masked_softmax_fusion # user want to fuse
156
+ and self.input_in_float16 # input must be fp16
157
+ and (
158
+ self.attn_mask_type == AttnMaskType.causal
159
+ or (self.attn_mask_type == AttnMaskType.padding and mask is not None)
160
+ )
161
+ and 16 < sk <= 8192 # sk must be 16 ~ 8192
162
+ and sq % 4 == 0 # sq must be divisor of 4
163
+ and sk % 4 == 0 # sk must be divisor of 4
164
+ and attn_batches % 4 == 0 # np * b must be divisor of 4
165
+ ):
166
+ if 0 <= sk <= 8192:
167
+ batch_per_block = self.get_batch_per_block(sq, sk, b, np)
168
+
169
+ if self.attn_mask_type == AttnMaskType.causal:
170
+ if attn_batches % batch_per_block == 0:
171
+ return True
172
+ else:
173
+ if sq % batch_per_block == 0:
174
+ return True
175
+ return False
176
+
177
+ def forward_fused_softmax(self, input, mask):
178
+ # input.shape = [b, np, sq, sk]
179
+ scale = self.scale if self.scale is not None else 1.0
180
+ return self.fused_softmax_func(input, mask, scale)
181
+
182
+ def forward_torch_softmax(self, input, mask):
183
+ if self.input_in_float16 and self.softmax_in_fp32:
184
+ input = input.float()
185
+
186
+ if self.scale is not None:
187
+ input = input * self.scale
188
+ mask_output = self.mask_func(input, mask) if mask is not None else input
189
+ probs = torch.nn.Softmax(dim=-1)(mask_output)
190
+
191
+ if self.input_in_float16 and self.softmax_in_fp32:
192
+ if self.input_in_fp16:
193
+ probs = probs.half()
194
+ else:
195
+ probs = probs.bfloat16()
196
+
197
+ return probs
198
+
199
+ @staticmethod
200
+ def get_batch_per_block(sq, sk, b, np):
201
+ return scaled_masked_softmax_get_batch_per_block(sq, sk, b, np)
evalkit_tf446/lib/python3.10/site-packages/flash_attn/layers/__init__.py ADDED
File without changes
evalkit_tf446/lib/python3.10/site-packages/flash_attn/layers/__pycache__/patch_embed.cpython-310.pyc ADDED
Binary file (1.92 kB). View file
 
evalkit_tf446/lib/python3.10/site-packages/flash_attn/layers/patch_embed.py ADDED
@@ -0,0 +1,67 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # We use the same API as https://github.com/rwightman/pytorch-image-models/blob/v0.6.11/timm/models/layers/patch_embed.py
2
+ # But we use nn.Linear instead of Conv2d and it's about 8x faster.
3
+
4
+ from functools import partial
5
+
6
+ import torch.nn as nn
7
+ from einops import rearrange
8
+ from torch import _assert
9
+ from torch.nn.modules.utils import _pair
10
+
11
+ try:
12
+ from flash_attn.ops.fused_dense import FusedDense
13
+ except ImportError:
14
+ FusedDense = None
15
+
16
+
17
+ class PatchEmbed(nn.Module):
18
+ """2D Image to Patch Embedding"""
19
+
20
+ def __init__(
21
+ self,
22
+ img_size=224,
23
+ patch_size=16,
24
+ in_chans=3,
25
+ embed_dim=768,
26
+ norm_layer=None,
27
+ flatten=True,
28
+ bias=True,
29
+ fused_bias_fc=False,
30
+ ):
31
+ super().__init__()
32
+ img_size = _pair(img_size)
33
+ patch_size = _pair(patch_size)
34
+ self.img_size = img_size
35
+ self.patch_size = patch_size
36
+ self.grid_size = (img_size[0] // patch_size[0], img_size[1] // patch_size[1])
37
+ self.num_patches = self.grid_size[0] * self.grid_size[1]
38
+ self.flatten = flatten
39
+ if fused_bias_fc and FusedDense is None:
40
+ raise ImportError("fused_dense is not installed")
41
+
42
+ linear_cls = nn.Linear if not fused_bias_fc or not bias else FusedDense
43
+ self.proj = linear_cls(in_chans * patch_size[0] * patch_size[1], embed_dim, bias=bias)
44
+ self.norm = norm_layer(embed_dim) if norm_layer else nn.Identity()
45
+
46
+ def forward(self, x):
47
+ _, _, H, W = x.shape
48
+ _assert(
49
+ H == self.img_size[0],
50
+ f"Input image height ({H}) doesn't match model ({self.img_size[0]}).",
51
+ )
52
+ _assert(
53
+ W == self.img_size[1],
54
+ f"Input image width ({W}) doesn't match model ({self.img_size[1]}).",
55
+ )
56
+ x = self.proj(
57
+ rearrange(
58
+ x,
59
+ "b c (h p1) (w p2) -> b h w (c p1 p2)",
60
+ p1=self.patch_size[0],
61
+ p2=self.patch_size[1],
62
+ )
63
+ )
64
+ if self.flatten:
65
+ x = rearrange(x, "b h w c -> b (h w) c")
66
+ x = self.norm(x)
67
+ return x
evalkit_tf446/lib/python3.10/site-packages/flash_attn/layers/rotary.py ADDED
@@ -0,0 +1,528 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Copyright (c) 2023, Tri Dao.
2
+
3
+ import math
4
+ from typing import Optional, Tuple, Union
5
+
6
+ import torch
7
+ from einops import rearrange, repeat
8
+ from flash_attn.ops.triton.rotary import apply_rotary
9
+
10
+
11
+ def rotate_half(x, interleaved=False):
12
+ if not interleaved:
13
+ x1, x2 = x.chunk(2, dim=-1)
14
+ return torch.cat((-x2, x1), dim=-1)
15
+ else:
16
+ x1, x2 = x[..., ::2], x[..., 1::2]
17
+ return rearrange(torch.stack((-x2, x1), dim=-1), "... d two -> ... (d two)", two=2)
18
+
19
+
20
+ def apply_rotary_emb_torch(x, cos, sin, interleaved=False):
21
+ """
22
+ x: (batch_size, seqlen, nheads, headdim)
23
+ cos, sin: (seqlen, rotary_dim / 2) or (batch_size, seqlen, rotary_dim / 2)
24
+ """
25
+ ro_dim = cos.shape[-1] * 2
26
+ assert ro_dim <= x.shape[-1]
27
+ cos = repeat(cos, "... d -> ... 1 (2 d)" if not interleaved else "... d -> ... 1 (d 2)")
28
+ sin = repeat(sin, "... d -> ... 1 (2 d)" if not interleaved else "... d -> ... 1 (d 2)")
29
+ return torch.cat(
30
+ [x[..., :ro_dim] * cos + rotate_half(x[..., :ro_dim], interleaved) * sin, x[..., ro_dim:]],
31
+ dim=-1,
32
+ )
33
+
34
+
35
+ class ApplyRotaryEmb(torch.autograd.Function):
36
+ @staticmethod
37
+ def forward(
38
+ ctx,
39
+ x,
40
+ cos,
41
+ sin,
42
+ interleaved=False,
43
+ inplace=False,
44
+ seqlen_offsets: Union[int, torch.Tensor] = 0,
45
+ cu_seqlens: Optional[torch.Tensor] = None,
46
+ max_seqlen: Optional[int] = None,
47
+ ):
48
+ out = apply_rotary(
49
+ x,
50
+ cos,
51
+ sin,
52
+ seqlen_offsets=seqlen_offsets,
53
+ cu_seqlens=cu_seqlens,
54
+ max_seqlen=max_seqlen,
55
+ interleaved=interleaved,
56
+ inplace=inplace,
57
+ )
58
+ if isinstance(seqlen_offsets, int):
59
+ ctx.save_for_backward(cos, sin, cu_seqlens) # Can't save int with save_for_backward
60
+ ctx.seqlen_offsets = seqlen_offsets
61
+ else:
62
+ ctx.save_for_backward(cos, sin, cu_seqlens, seqlen_offsets)
63
+ ctx.seqlen_offsets = None
64
+ ctx.interleaved = interleaved
65
+ ctx.inplace = inplace
66
+ ctx.max_seqlen = max_seqlen
67
+ return out if not inplace else x
68
+
69
+ @staticmethod
70
+ def backward(ctx, do):
71
+ seqlen_offsets = ctx.seqlen_offsets
72
+ if seqlen_offsets is None:
73
+ cos, sin, cu_seqlens, seqlen_offsets = ctx.saved_tensors
74
+ else:
75
+ cos, sin, cu_seqlens = ctx.saved_tensors
76
+ # TD [2023-09-02]: For some reason Triton (2.0.0.post1) errors with
77
+ # "[CUDA]: invalid device context", and cloning makes it work. Idk why. Triton 2.1.0 works.
78
+ if not ctx.interleaved and not ctx.inplace:
79
+ do = do.clone()
80
+ dx = apply_rotary(
81
+ do,
82
+ cos,
83
+ sin,
84
+ seqlen_offsets=seqlen_offsets,
85
+ cu_seqlens=cu_seqlens,
86
+ max_seqlen=ctx.max_seqlen,
87
+ interleaved=ctx.interleaved,
88
+ inplace=ctx.inplace,
89
+ conjugate=True,
90
+ )
91
+ return dx, None, None, None, None, None, None, None
92
+
93
+
94
+ def apply_rotary_emb(
95
+ x,
96
+ cos,
97
+ sin,
98
+ interleaved=False,
99
+ inplace=False,
100
+ seqlen_offsets: Union[int, torch.Tensor] = 0,
101
+ cu_seqlens: Optional[torch.Tensor] = None,
102
+ max_seqlen: Optional[int] = None,
103
+ ):
104
+ """
105
+ Arguments:
106
+ x: (batch_size, seqlen, nheads, headdim) if cu_seqlens is None
107
+ else (total_seqlen, nheads, headdim)
108
+ cos, sin: (seqlen_rotary, rotary_dim / 2)
109
+ interleaved: if True, rotate pairs of even and odd dimensions (GPT-J style) instead
110
+ of 1st half and 2nd half (GPT-NeoX style).
111
+ inplace: if True, apply rotary embedding in-place.
112
+ seqlen_offsets: (batch_size,) or int. Each sequence in x is shifted by this amount.
113
+ Most commonly used in inference when we have KV cache.
114
+ cu_seqlens: (batch + 1,) or None
115
+ max_seqlen: int
116
+ Return:
117
+ out: (batch_size, seqlen, nheads, headdim) if cu_seqlens is None
118
+ else (total_seqlen, nheads, headdim)
119
+ rotary_dim must be <= headdim
120
+ Apply rotary embedding to the first rotary_dim of x.
121
+ """
122
+ return ApplyRotaryEmb.apply(
123
+ x, cos, sin, interleaved, inplace, seqlen_offsets, cu_seqlens, max_seqlen
124
+ )
125
+
126
+
127
+ # For backward compatibility
128
+ apply_rotary_emb_func = apply_rotary_emb
129
+
130
+
131
+ class ApplyRotaryEmbQKV_(torch.autograd.Function):
132
+ @staticmethod
133
+ def forward(
134
+ ctx,
135
+ qkv,
136
+ cos,
137
+ sin,
138
+ cos_k=None,
139
+ sin_k=None,
140
+ interleaved=False,
141
+ seqlen_offsets: Union[int, torch.Tensor] = 0,
142
+ num_heads_q: Union[int] = None,
143
+ ):
144
+ if cos_k is None and sin_k is None and qkv.is_contiguous():
145
+ # Call 1 kernel instead of 2 kernels
146
+ # We need qkv to be contiguous so that when we reshape to combine (3, nheads)
147
+ # dimensions, we get the same tensor
148
+ if qkv.dim() == 5:
149
+ batch, seqlen, three, nheads, headdim = qkv.shape
150
+ assert three == 3
151
+ # qk = rearrange(qkv[:, :, :2], "b s t h d -> b s (t h) d")
152
+ qk = qkv[:, :, :2].reshape(batch, seqlen, -1, headdim)
153
+ else:
154
+ assert qkv.dim() == 4
155
+ assert num_heads_q is not None
156
+ num_heads_k = (qkv.shape[2] - num_heads_q) // 2
157
+ assert qkv.shape[2] == num_heads_q + 2 * num_heads_k
158
+ qk = qkv[:, :, :num_heads_q + num_heads_k]
159
+ apply_rotary(
160
+ qk, cos, sin, seqlen_offsets=seqlen_offsets, interleaved=interleaved, inplace=True
161
+ )
162
+ else:
163
+ cos_k = cos if cos_k is None else cos_k
164
+ sin_k = sin if sin_k is None else sin_k
165
+ if qkv.dim() == 5:
166
+ q, k = qkv[:, :, 0], qkv[:, :, 1]
167
+ else:
168
+ assert qkv.dim() == 4
169
+ assert num_heads_q is not None
170
+ num_heads_k = (qkv.shape[2] - num_heads_q) // 2
171
+ assert qkv.shape[2] == num_heads_q + 2 * num_heads_k
172
+ q, k = qkv[:, :, :num_heads_q], qkv[:, :, num_heads_q : num_heads_q + num_heads_k]
173
+ apply_rotary(q, cos, sin, seqlen_offsets, interleaved=interleaved, inplace=True)
174
+ apply_rotary(k, cos_k, sin_k, seqlen_offsets, interleaved=interleaved, inplace=True)
175
+ ctx.save_for_backward(cos, sin, cos_k, sin_k)
176
+ if isinstance(seqlen_offsets, int):
177
+ ctx.save_for_backward(cos, sin, cos_k, sin_k)
178
+ ctx.seqlen_offsets = seqlen_offsets
179
+ else:
180
+ ctx.save_for_backward(cos, sin, cos_k, sin_k, seqlen_offsets)
181
+ ctx.seqlen_offsets = None
182
+ ctx.interleaved = interleaved
183
+ ctx.num_heads_q = num_heads_q
184
+ return qkv
185
+
186
+ @staticmethod
187
+ def backward(ctx, dqkv):
188
+ seqlen_offsets = ctx.seqlen_offsets
189
+ if seqlen_offsets is None:
190
+ cos, sin, cos_k, sin_k, seqlen_offsets = ctx.saved_tensors
191
+ else:
192
+ cos, sin, cos_k, sin_k = ctx.saved_tensors
193
+ if cos_k is None and sin_k is None and dqkv.is_contiguous():
194
+ # Call 1 kernel instead of 2 kernels
195
+ # We need dqkv to be contiguous so that when we reshape to combine (3, nheads)
196
+ # dimensions, we get the same tensor
197
+ if dqkv.dim() == 5:
198
+ dqk = rearrange(dqkv[:, :, :2], "b s t h d -> b s (t h) d")
199
+ else:
200
+ assert dqkv.dim() == 4
201
+ assert ctx.num_heads_q is not None
202
+ num_heads_k = (dqkv.shape[2] - ctx.num_heads_q) // 2
203
+ assert dqkv.shape[2] == ctx.num_heads_q + 2 * num_heads_k
204
+ dqk = dqkv[:, :, : ctx.num_heads_q + num_heads_k]
205
+ apply_rotary(
206
+ dqk,
207
+ cos,
208
+ sin,
209
+ seqlen_offsets=seqlen_offsets,
210
+ interleaved=ctx.interleaved,
211
+ inplace=True,
212
+ conjugate=True,
213
+ )
214
+ else:
215
+ cos_k = cos if cos_k is None else cos_k
216
+ sin_k = sin if sin_k is None else sin_k
217
+ if dqkv.dim() == 5:
218
+ dq, dk = dqkv[:, :, 0], dqkv[:, :, 1]
219
+ else:
220
+ assert dqkv.dim() == 4
221
+ assert ctx.num_heads_q is not None
222
+ num_heads_k = (dqkv.shape[2] - ctx.num_heads_q) // 2
223
+ assert dqkv.shape[2] == ctx.num_heads_q + 2 * num_heads_k
224
+ dq = dqkv[:, :, : ctx.num_heads_q]
225
+ dk = dqkv[:, :, ctx.num_heads_q : ctx.num_heads_q + num_heads_k]
226
+ apply_rotary(
227
+ dq,
228
+ cos,
229
+ sin,
230
+ seqlen_offsets,
231
+ interleaved=ctx.interleaved,
232
+ inplace=True,
233
+ conjugate=True,
234
+ )
235
+ apply_rotary(
236
+ dk,
237
+ cos_k,
238
+ sin_k,
239
+ seqlen_offsets,
240
+ interleaved=ctx.interleaved,
241
+ inplace=True,
242
+ conjugate=True,
243
+ )
244
+ return dqkv, None, None, None, None, None, None, None
245
+
246
+
247
+ def apply_rotary_emb_qkv_(
248
+ qkv,
249
+ cos,
250
+ sin,
251
+ cos_k=None,
252
+ sin_k=None,
253
+ interleaved=False,
254
+ seqlen_offsets: Union[int, torch.Tensor] = 0,
255
+ num_heads_q: Optional[int] = None,
256
+ ):
257
+ """
258
+ Arguments:
259
+ qkv: (batch_size, seqlen, 3, nheads, headdim) or (batch_size, seqlen, num_heads_q + 2 * num_heads_k, headdim).
260
+ If qkv has shape (batch_size, seqlen, num_heads_q + 2 * num_heads_k, headdim) (e.g. MQA / GQA),
261
+ then num_heads_q must be provided.
262
+ cos, sin: (seqlen, rotary_dim / 2)
263
+ cos_k, sin_k: (seqlen, rotary_dim / 2), optional
264
+ interleaved: if True, rotate pairs of even and odd dimensions (GPT-J style) instead of
265
+ 1st half and 2nd half (GPT-NeoX style).
266
+ seqlen_offsets: (batch_size,) or int. Each sequence in Q and K is shifted by this amount.
267
+ Most commonly used in inference when we have KV cache.
268
+ Return:
269
+ qkv: (batch_size, seqlen, 3, nheads, headdim) or (batch_size, seqlen, num_heads_q + 2 * num_heads_k, headdim)
270
+ rotary_dim must be <= headdim
271
+ Apply rotary embedding *inplace* to the first rotary_dim of Q and K.
272
+ """
273
+ return ApplyRotaryEmbQKV_.apply(
274
+ qkv, cos, sin, cos_k, sin_k, interleaved, seqlen_offsets, num_heads_q
275
+ )
276
+
277
+
278
+ class ApplyRotaryEmbKV_(torch.autograd.Function):
279
+ @staticmethod
280
+ def forward(ctx, kv, cos, sin, interleaved=False, seqlen_offsets: Union[int, torch.Tensor] = 0):
281
+ batch, seqlen, two, nheads, headdim = kv.shape
282
+ assert two == 2
283
+ k = kv[:, :, 0]
284
+ apply_rotary(
285
+ k, cos, sin, seqlen_offsets=seqlen_offsets, interleaved=interleaved, inplace=True
286
+ )
287
+ if isinstance(seqlen_offsets, int):
288
+ ctx.save_for_backward(cos, sin) # Can't save int with save_for_backward
289
+ ctx.seqlen_offsets = seqlen_offsets
290
+ else:
291
+ ctx.save_for_backward(cos, sin, seqlen_offsets)
292
+ ctx.seqlen_offsets = None
293
+ ctx.interleaved = interleaved
294
+ return kv
295
+
296
+ @staticmethod
297
+ def backward(ctx, dkv):
298
+ seqlen_offsets = ctx.seqlen_offsets
299
+ if seqlen_offsets is None:
300
+ cos, sin, seqlen_offsets = ctx.saved_tensors
301
+ else:
302
+ cos, sin = ctx.saved_tensors
303
+ apply_rotary(
304
+ dkv[:, :, 0],
305
+ cos,
306
+ sin,
307
+ seqlen_offsets=seqlen_offsets,
308
+ interleaved=ctx.interleaved,
309
+ inplace=True,
310
+ conjugate=True,
311
+ )
312
+ return dkv, None, None, None, None
313
+
314
+
315
+ apply_rotary_emb_kv_ = ApplyRotaryEmbKV_.apply
316
+
317
+
318
+ def apply_rotary_emb_kv_(
319
+ kv,
320
+ cos,
321
+ sin,
322
+ interleaved=False,
323
+ seqlen_offsets: Union[int, torch.Tensor] = 0,
324
+ ):
325
+ """
326
+ Arguments:
327
+ kv: (batch_size, seqlen, 2, nheads, headdim)
328
+ cos, sin: (seqlen, rotary_dim / 2)
329
+ interleaved: if True, rotate pairs of even and odd dimensions (GPT-J style) instead of
330
+ 1st half and 2nd half (GPT-NeoX style).
331
+ seqlen_offsets: (batch_size,) or int. Each sequence in Q and K is shifted by this amount.
332
+ Most commonly used in inference when we have KV cache.
333
+ Return:
334
+ kv: (batch_size, seqlen, 2, nheads, headdim)
335
+ rotary_dim must be <= headdim
336
+ Apply rotary embedding *inplace* to the first rotary_dim of K.
337
+ """
338
+ return ApplyRotaryEmbKV_.apply(kv, cos, sin, interleaved, seqlen_offsets)
339
+
340
+
341
+ class RotaryEmbedding(torch.nn.Module):
342
+ """
343
+ The rotary position embeddings from RoFormer_ (Su et. al).
344
+ A crucial insight from the method is that the query and keys are
345
+ transformed by rotation matrices which depend on the relative positions.
346
+
347
+ Other implementations are available in the Rotary Transformer repo_ and in
348
+ GPT-NeoX_, GPT-NeoX was an inspiration
349
+
350
+ .. _RoFormer: https://arxiv.org/abs/2104.09864
351
+ .. _repo: https://github.com/ZhuiyiTechnology/roformer
352
+ .. _GPT-NeoX: https://github.com/EleutherAI/gpt-neox
353
+
354
+ If scale_base is not None, this implements XPos (Sun et al., https://arxiv.org/abs/2212.10554).
355
+ A recommended value for scale_base is 512: https://github.com/HazyResearch/flash-attention/issues/96
356
+ Reference: https://github.com/sunyt32/torchscale/blob/main/torchscale/component/xpos_relative_position.py
357
+ """
358
+
359
+ def __init__(
360
+ self,
361
+ dim: int,
362
+ base=10000.0,
363
+ interleaved=False,
364
+ scale_base=None,
365
+ pos_idx_in_fp32=True,
366
+ device=None,
367
+ ):
368
+ """
369
+ interleaved: if True, rotate pairs of even and odd dimensions (GPT-J style) instead
370
+ of 1st half and 2nd half (GPT-NeoX style).
371
+ pos_idx_in_fp32: if True, the position indices [0.0, ..., seqlen - 1] are in fp32,
372
+ otherwise they might be in lower precision.
373
+ This option was added because previously (before 2023-07-02), when we construct
374
+ the position indices, we use the dtype of self.inv_freq. In most cases this would
375
+ be fp32, but if the model is trained in pure bf16 (not mixed precision), then
376
+ self.inv_freq would be bf16, and the position indices are also in bf16.
377
+ Because of the limited precision of bf16 (e.g. 1995.0 is rounded to 2000.0), the
378
+ embeddings for some positions will coincide.
379
+ To maintain compatibility with models previously trained in pure bf16,
380
+ we add this option.
381
+ """
382
+ super().__init__()
383
+ self.dim = dim
384
+ self.base = float(base)
385
+ self.pos_idx_in_fp32 = pos_idx_in_fp32
386
+ # Generate and save the inverse frequency buffer (non trainable)
387
+ inv_freq = self._compute_inv_freq(device)
388
+ self.register_buffer("inv_freq", inv_freq, persistent=False)
389
+ self.interleaved = interleaved
390
+ self.scale_base = scale_base
391
+ scale = (
392
+ (torch.arange(0, dim, 2, device=device, dtype=torch.float32) + 0.4 * dim) / (1.4 * dim)
393
+ if scale_base is not None
394
+ else None
395
+ )
396
+ self.register_buffer("scale", scale, persistent=False)
397
+
398
+ self._seq_len_cached = 0
399
+ self._cos_cached = None
400
+ self._sin_cached = None
401
+ self._cos_k_cached = None
402
+ self._sin_k_cached = None
403
+
404
+ def _compute_inv_freq(self, device=None):
405
+ return 1.0 / (
406
+ self.base
407
+ ** (torch.arange(0, self.dim, 2, device=device, dtype=torch.float32) / self.dim)
408
+ )
409
+
410
+ def _update_cos_sin_cache(self, seqlen, device=None, dtype=None):
411
+ # Reset the tables if the sequence length has changed,
412
+ # if we're on a new device (possibly due to tracing for instance),
413
+ # or if we're switching from inference mode to training
414
+ if (
415
+ seqlen > self._seq_len_cached
416
+ or self._cos_cached is None
417
+ or self._cos_cached.device != device
418
+ or self._cos_cached.dtype != dtype
419
+ or (self.training and self._cos_cached.is_inference())
420
+ ):
421
+ self._seq_len_cached = seqlen
422
+ # We want fp32 here, not self.inv_freq.dtype, since the model could be loaded in bf16
423
+ # And the output of arange can be quite large, so bf16 would lose a lot of precision.
424
+ # However, for compatibility reason, we add an option to use the dtype of self.inv_freq.
425
+ if self.pos_idx_in_fp32:
426
+ t = torch.arange(seqlen, device=device, dtype=torch.float32)
427
+ # We want fp32 here as well since inv_freq will be multiplied with t, and the output
428
+ # will be large. Having it in bf16 will lose a lot of precision and cause the
429
+ # cos & sin output to change significantly.
430
+ # We want to recompute self.inv_freq if it was not loaded in fp32
431
+ if self.inv_freq.dtype != torch.float32:
432
+ inv_freq = self._compute_inv_freq(device=device)
433
+ else:
434
+ inv_freq = self.inv_freq
435
+ else:
436
+ t = torch.arange(seqlen, device=device, dtype=self.inv_freq.dtype)
437
+ inv_freq = self.inv_freq
438
+ # Don't do einsum, it converts fp32 to fp16 under AMP
439
+ # freqs = torch.einsum("i,j->ij", t, self.inv_freq)
440
+ freqs = torch.outer(t, inv_freq)
441
+ if self.scale is None:
442
+ self._cos_cached = torch.cos(freqs).to(dtype)
443
+ self._sin_cached = torch.sin(freqs).to(dtype)
444
+ else:
445
+ power = (
446
+ torch.arange(seqlen, dtype=self.scale.dtype, device=self.scale.device)
447
+ - seqlen // 2
448
+ ) / self.scale_base
449
+ scale = self.scale.to(device=power.device) ** rearrange(power, "s -> s 1")
450
+ # We want the multiplication by scale to happen in fp32
451
+ self._cos_cached = (torch.cos(freqs) * scale).to(dtype)
452
+ self._sin_cached = (torch.sin(freqs) * scale).to(dtype)
453
+ self._cos_k_cached = (torch.cos(freqs) / scale).to(dtype)
454
+ self._sin_k_cached = (torch.sin(freqs) / scale).to(dtype)
455
+
456
+ def forward(
457
+ self,
458
+ qkv: torch.Tensor,
459
+ kv: Optional[torch.Tensor] = None,
460
+ seqlen_offset: Union[int, torch.Tensor] = 0,
461
+ max_seqlen: Optional[int] = None,
462
+ num_heads_q: Optional[int] = None,
463
+ ) -> Union[torch.Tensor, Tuple[torch.Tensor, torch.Tensor]]:
464
+ """
465
+ qkv: (batch, seqlen, 3, nheads, headdim) or (batch, seqlen, num_heads_q + 2 * num_heads_k, headdim)
466
+ if kv is none, else it's just q of shape (batch, seqlen, nheads, headdim).
467
+ If qkv has shape (batch, seqlen, num_heads_q + 2 * num_heads_k, headdim) (e.g. MQA / GQA),
468
+ then num_heads_q must be provided.
469
+ kv: (batch, seqlen, 2, nheads, headdim)
470
+ seqlen_offset: (batch_size,) or int. Each sequence in x is shifted by this amount.
471
+ Most commonly used in inference when we have KV cache.
472
+ If it's a tensor of shape (batch_size,), then to update the cos / sin cache, one
473
+ should pass in max_seqlen, which will update the cos / sin cache up to that length.
474
+ Apply rotary embedding *inplace* to qkv and / or kv.
475
+ """
476
+ seqlen = qkv.shape[1]
477
+ if max_seqlen is not None:
478
+ self._update_cos_sin_cache(max_seqlen, device=qkv.device, dtype=qkv.dtype)
479
+ elif isinstance(seqlen_offset, int):
480
+ self._update_cos_sin_cache(seqlen + seqlen_offset, device=qkv.device, dtype=qkv.dtype)
481
+ if kv is None:
482
+ if self.scale is None:
483
+ return apply_rotary_emb_qkv_(
484
+ qkv,
485
+ self._cos_cached,
486
+ self._sin_cached,
487
+ interleaved=self.interleaved,
488
+ seqlen_offsets=seqlen_offset,
489
+ num_heads_q=num_heads_q,
490
+ )
491
+ else:
492
+ return apply_rotary_emb_qkv_(
493
+ qkv,
494
+ self._cos_cached,
495
+ self._sin_cached,
496
+ self._cos_k_cached,
497
+ self._sin_k_cached,
498
+ interleaved=self.interleaved,
499
+ seqlen_offsets=seqlen_offset,
500
+ num_heads_q=num_heads_q,
501
+ )
502
+ else:
503
+ q = qkv
504
+ q = apply_rotary_emb_func(
505
+ q,
506
+ self._cos_cached,
507
+ self._sin_cached,
508
+ interleaved=self.interleaved,
509
+ inplace=True,
510
+ seqlen_offsets=seqlen_offset,
511
+ )
512
+ if self.scale is None:
513
+ kv = apply_rotary_emb_kv_(
514
+ kv,
515
+ self._cos_cached,
516
+ self._sin_cached,
517
+ interleaved=self.interleaved,
518
+ seqlen_offsets=seqlen_offset,
519
+ )
520
+ else:
521
+ kv = apply_rotary_emb_kv_(
522
+ kv,
523
+ self._cos_k_cached,
524
+ self._sin_k_cached,
525
+ interleaved=self.interleaved,
526
+ seqlen_offsets=seqlen_offset,
527
+ )
528
+ return q, kv
evalkit_tf446/lib/python3.10/site-packages/flash_attn/modules/__init__.py ADDED
File without changes
evalkit_tf446/lib/python3.10/site-packages/flash_attn/modules/__pycache__/__init__.cpython-310.pyc ADDED
Binary file (177 Bytes). View file
 
evalkit_tf446/lib/python3.10/site-packages/flash_attn/modules/__pycache__/block.cpython-310.pyc ADDED
Binary file (8.61 kB). View file
 
evalkit_tf446/lib/python3.10/site-packages/flash_attn/modules/__pycache__/embedding.cpython-310.pyc ADDED
Binary file (6 kB). View file
 
evalkit_tf446/lib/python3.10/site-packages/flash_attn/modules/__pycache__/mha.cpython-310.pyc ADDED
Binary file (23.3 kB). View file
 
evalkit_tf446/lib/python3.10/site-packages/flash_attn/modules/__pycache__/mlp.cpython-310.pyc ADDED
Binary file (4.4 kB). View file
 
evalkit_tf446/lib/python3.10/site-packages/flash_attn/modules/block.py ADDED
@@ -0,0 +1,397 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Copyright (c) 2024, Tri Dao.
2
+
3
+ from functools import partial
4
+ from typing import Optional
5
+
6
+ import torch
7
+ import torch.nn as nn
8
+ import torch.nn.functional as F
9
+ from torch import Tensor
10
+ from torchvision.ops import StochasticDepth
11
+
12
+ from flash_attn.modules.mha import MHA
13
+ from flash_attn.modules.mlp import Mlp
14
+
15
+ try:
16
+ from flash_attn.ops.triton.layer_norm import layer_norm_fn, RMSNorm
17
+ except ImportError:
18
+ layer_norm_fn, RMSNorm = None, None
19
+
20
+
21
+ class Block(nn.Module):
22
+ def __init__(
23
+ self,
24
+ dim,
25
+ mixer_cls=None,
26
+ mlp_cls=None,
27
+ norm_cls=nn.LayerNorm,
28
+ dropout_cls=nn.Dropout,
29
+ prenorm=True,
30
+ resid_dropout1=0.0,
31
+ resid_dropout2=0.0,
32
+ drop_path1=0.0,
33
+ drop_path2=0.0,
34
+ fused_dropout_add_ln=False,
35
+ return_residual=False,
36
+ residual_in_fp32=False,
37
+ sequence_parallel=False,
38
+ mark_shared_params=False,
39
+ ):
40
+ """
41
+ For prenorm=True, this Block has a slightly different structure compared to a regular
42
+ prenorm Transformer block.
43
+ The standard block is: LN -> MHA -> Dropout -> Add -> LN -> MLP -> Dropout -> Add.
44
+ [Ref: https://arxiv.org/abs/2002.04745]
45
+ Here we have: Dropout -> Add -> LN -> MHA -> Dropout -> Add -> LN -> MLP, returning both
46
+ the hidden_states (output of the MLP) and the residual.
47
+ This is for performance reasons, as we can fuse the dropout, add and LayerNorm.
48
+ The residual needs to be provided (except for the very first block).
49
+
50
+ For prenorm=False, this Block has the same structure as a regular postnorm Transformer
51
+ block: MHA -> Dropout -> Add -> LN -> MLP -> Dropout -> Add -> LN.
52
+
53
+ return_residual: whether each of the sub-layers (mixer and mlp) will return the residual.
54
+ This is for performance reason: for post-norm architecture, returning the input allows us
55
+ to fuse the backward of nn.Linear with the residual connection.
56
+ """
57
+ super().__init__()
58
+ self.prenorm = prenorm
59
+ self.fused_dropout_add_ln = fused_dropout_add_ln
60
+ self.return_residual = return_residual
61
+ self.residual_in_fp32 = residual_in_fp32
62
+ if self.residual_in_fp32:
63
+ assert self.prenorm, "residual_in_fp32 is only compatible with prenorm=True"
64
+ if mixer_cls is None:
65
+ mixer_cls = partial(MHA, num_heads=dim // 64)
66
+ if mlp_cls is None:
67
+ mlp_cls = partial(Mlp, hidden_features=4 * dim)
68
+ self.mixer = mixer_cls(dim)
69
+ self.dropout1 = dropout_cls(resid_dropout1)
70
+ self.drop_path1 = StochasticDepth(drop_path1, mode="row")
71
+ self.norm1 = norm_cls(dim)
72
+ self.mlp = mlp_cls(dim)
73
+ if not isinstance(self.mlp, nn.Identity):
74
+ self.dropout2 = dropout_cls(resid_dropout2)
75
+ self.drop_path2 = StochasticDepth(drop_path2, mode="row")
76
+ self.norm2 = norm_cls(dim)
77
+
78
+ if self.fused_dropout_add_ln:
79
+ assert layer_norm_fn is not None, "Triton is not installed"
80
+ assert isinstance(self.norm1, (nn.LayerNorm, RMSNorm)) and isinstance(
81
+ self.dropout1, nn.Dropout
82
+ )
83
+
84
+ # TD [2023-01-07]: TODO: During training, if sequence_parallel is False and dropout != 0.0,
85
+ # then the input to each worker in the tensor parallel group will be different.
86
+ # This would produce wrong outputs? Somehow we'd need to sync the RNG state across workers.
87
+ # For now this is not an issue because we always use sequence_parallel=True during training
88
+ # and only use sequence_parallel=False during inference.
89
+
90
+ # Mark the norm parameters as "sequence_parallel" so that we run all-reduce on their grads.
91
+ if sequence_parallel:
92
+ for p in self.norm1.parameters():
93
+ p._sequence_parallel = True
94
+ if hasattr(self, "norm2"):
95
+ for p in self.norm2.parameters():
96
+ p._sequence_parallel = True
97
+ # Mark the norm parameters as "shared_params" so that we sync their values at init.
98
+ if mark_shared_params:
99
+ for p in self.norm1.parameters():
100
+ p._shared_params = True
101
+ if hasattr(self, "norm2"):
102
+ for p in self.norm2.parameters():
103
+ p._shared_params = True
104
+
105
+ def allocate_inference_cache(self, batch_size, max_seqlen, dtype=None, **kwargs):
106
+ return self.mixer.allocate_inference_cache(batch_size, max_seqlen, dtype=dtype, **kwargs)
107
+
108
+ def forward(
109
+ self,
110
+ hidden_states: Tensor,
111
+ residual: Optional[Tensor] = None,
112
+ mixer_subset=None,
113
+ mixer_kwargs=None,
114
+ ):
115
+ r"""Pass the input through the encoder layer.
116
+
117
+ Args:
118
+ hidden_states: the sequence to the encoder layer (required).
119
+ residual: if postnorm, residual=None, If prenorm, hidden_states = Attn/MLP(LN(residual))
120
+ mixer_subset: for cross-attention only. If not None, will take a subset of x
121
+ before applying the query projection. Useful for e.g., ViT where we only care
122
+ about the CLS token in the last layer.
123
+ """
124
+ if self.prenorm:
125
+ if not self.fused_dropout_add_ln:
126
+ dropped = self.drop_path1(self.dropout1(hidden_states))
127
+ residual = (dropped + residual) if residual is not None else dropped
128
+ hidden_states = self.norm1(residual.to(dtype=self.norm1.weight.dtype))
129
+ if self.residual_in_fp32:
130
+ residual = residual.to(torch.float32)
131
+ else:
132
+ if self.drop_path1.p == 0 or not self.training:
133
+ rowscale1 = None
134
+ else:
135
+ rowscale1 = self.drop_path1(
136
+ torch.ones(
137
+ hidden_states.shape[:-1],
138
+ device=hidden_states.device,
139
+ dtype=hidden_states.dtype,
140
+ )
141
+ )
142
+ hidden_states, residual = layer_norm_fn(
143
+ hidden_states,
144
+ self.norm1.weight,
145
+ self.norm1.bias,
146
+ residual=residual,
147
+ eps=self.norm1.eps,
148
+ dropout_p=self.dropout1.p if self.training else 0.0,
149
+ rowscale=rowscale1,
150
+ prenorm=True,
151
+ residual_in_fp32=self.residual_in_fp32,
152
+ is_rms_norm=isinstance(self.norm1, RMSNorm)
153
+ )
154
+ if mixer_kwargs is None:
155
+ mixer_kwargs = {}
156
+ if mixer_subset is not None:
157
+ mixer_kwargs["mixer_subset"] = mixer_subset
158
+ hidden_states = self.mixer(hidden_states, **mixer_kwargs)
159
+ if mixer_subset is not None:
160
+ residual = residual[:, mixer_subset]
161
+ if not isinstance(self.mlp, nn.Identity):
162
+ if not self.fused_dropout_add_ln:
163
+ dropped = self.drop_path2(self.dropout2(hidden_states))
164
+ residual = (dropped + residual) if residual is not None else dropped
165
+ hidden_states = self.norm2(residual.to(dtype=self.norm2.weight.dtype))
166
+ if self.residual_in_fp32:
167
+ residual = residual.to(torch.float32)
168
+ else:
169
+ if self.drop_path2.p == 0 or not self.training:
170
+ rowscale2 = None
171
+ else:
172
+ rowscale2 = self.drop_path2(
173
+ torch.ones(
174
+ hidden_states.shape[:-1],
175
+ device=hidden_states.device,
176
+ dtype=hidden_states.dtype,
177
+ )
178
+ )
179
+ hidden_states, residual = layer_norm_fn(
180
+ hidden_states,
181
+ self.norm2.weight,
182
+ self.norm2.bias,
183
+ residual=residual,
184
+ eps=self.norm2.eps,
185
+ dropout_p=self.dropout2.p if self.training else 0.0,
186
+ rowscale=rowscale2,
187
+ prenorm=True,
188
+ residual_in_fp32=self.residual_in_fp32,
189
+ is_rms_norm=isinstance(self.norm2, RMSNorm)
190
+ )
191
+ hidden_states = self.mlp(hidden_states)
192
+ return hidden_states, residual
193
+ else:
194
+ assert residual is None
195
+ mixer_out = self.mixer(
196
+ hidden_states, **(mixer_kwargs if mixer_kwargs is not None else {})
197
+ )
198
+ if self.return_residual: # mixer out is actually a pair here
199
+ mixer_out, hidden_states = mixer_out
200
+ if not self.fused_dropout_add_ln:
201
+ hidden_states = self.norm1(
202
+ (self.drop_path1(self.dropout1(mixer_out)) + hidden_states).to(
203
+ dtype=self.norm1.weight.dtype
204
+ )
205
+ )
206
+ else:
207
+ if self.drop_path1.p == 0 or not self.training:
208
+ rowscale1 = None
209
+ else:
210
+ rowscale1 = self.drop_path1(
211
+ torch.ones(
212
+ mixer_out.shape[:-1], device=mixer_out.device, dtype=mixer_out.dtype
213
+ )
214
+ )
215
+ hidden_states = layer_norm_fn(
216
+ mixer_out,
217
+ self.norm1.weight,
218
+ self.norm1.bias,
219
+ residual=hidden_states,
220
+ eps=self.norm1.eps,
221
+ dropout_p=self.dropout1.p if self.training else 0.0,
222
+ rowscale=rowscale1,
223
+ prenorm=False,
224
+ is_rms_norm=isinstance(self.norm1, RMSNorm)
225
+ )
226
+ if not isinstance(self.mlp, nn.Identity):
227
+ mlp_out = self.mlp(hidden_states)
228
+ if self.return_residual: # mlp out is actually a pair here
229
+ mlp_out, hidden_states = mlp_out
230
+ if not self.fused_dropout_add_ln:
231
+ hidden_states = self.norm2(
232
+ (self.drop_path2(self.dropout2(mlp_out)) + hidden_states).to(
233
+ dtype=self.norm2.weight.dtype
234
+ )
235
+ )
236
+ else:
237
+ if self.drop_path2.p == 0 or not self.training:
238
+ rowscale2 = None
239
+ else:
240
+ rowscale2 = self.drop_path2(
241
+ torch.ones(
242
+ mlp_out.shape[:-1], device=mlp_out.device, dtype=mlp_out.dtype
243
+ )
244
+ )
245
+ hidden_states = layer_norm_fn(
246
+ mlp_out,
247
+ self.norm2.weight,
248
+ self.norm2.bias,
249
+ residual=hidden_states,
250
+ eps=self.norm2.eps,
251
+ dropout_p=self.dropout2.p if self.training else 0.0,
252
+ rowscale=rowscale2,
253
+ prenorm=False,
254
+ is_rms_norm=isinstance(self.norm2, RMSNorm)
255
+ )
256
+ return hidden_states
257
+
258
+
259
+ class ParallelBlock(nn.Module):
260
+ """The attention (mixer) and MLP blocks are done in parallel, similar to GPT-J, GPT-NeoX,
261
+ and PaLM.
262
+ """
263
+
264
+ def __init__(
265
+ self,
266
+ dim,
267
+ mixer_cls=None,
268
+ mlp_cls=None,
269
+ norm_cls=nn.LayerNorm,
270
+ dropout_cls=nn.Dropout,
271
+ resid_dropout1=0.0,
272
+ resid_dropout2=0.0,
273
+ tied_norm=False,
274
+ fused_dropout_add_ln=False,
275
+ residual_in_fp32=False,
276
+ sequence_parallel=False,
277
+ mark_shared_params=False,
278
+ ):
279
+ """
280
+ This Block has a slightly different structure compared to a regular
281
+ prenorm Transformer block.
282
+ The standard block is: LN -> MHA / MLP -> Dropout -> Add.
283
+ [Ref: https://arxiv.org/abs/2002.04745]
284
+ Here we have: Dropout -> Add -> LN -> MHA / MLP, returning both
285
+ the hidden_states (output1 of the MHA / MLP) and the residual.
286
+ This is for performance reasons, as we can fuse the dropout, add and LayerNorm.
287
+ The residual needs to be provided (except for the very first block).
288
+ """
289
+ super().__init__()
290
+ self.tied_norm = tied_norm
291
+ self.fused_dropout_add_ln = fused_dropout_add_ln
292
+ self.residual_in_fp32 = residual_in_fp32
293
+ if mixer_cls is None:
294
+ mixer_cls = partial(MHA, num_heads=dim // 64)
295
+ if mlp_cls is None:
296
+ mlp_cls = partial(Mlp, hidden_features=4 * dim)
297
+ self.mixer = mixer_cls(dim)
298
+ self.dropout1 = dropout_cls(resid_dropout1)
299
+ self.norm1 = norm_cls(dim)
300
+ self.mlp = mlp_cls(dim)
301
+ self.dropout2 = dropout_cls(resid_dropout2)
302
+ if not self.tied_norm:
303
+ self.norm2 = norm_cls(dim)
304
+
305
+ if self.fused_dropout_add_ln:
306
+ assert layer_norm_fn is not None, "Triton is not installed"
307
+ assert isinstance(self.norm1, (nn.LayerNorm, RMSNorm)) and isinstance(
308
+ self.dropout1, nn.Dropout
309
+ )
310
+
311
+ # TD [2023-01-07]: TODO: During training, if sequence_parallel is False and dropout != 0.0,
312
+ # then the input to each worker in the tensor parallel group will be different.
313
+ # This would produce wrong outputs? Somehow we'd need to sync the RNG state across workers.
314
+ # For now this is not an issue because we always use sequence_parallel=True during training
315
+ # and only use sequence_parallel=False during inference.
316
+
317
+ # Mark the norm parameters as "sequence_parallel" so that we run all-reduce on their grads.
318
+ if sequence_parallel:
319
+ for p in self.norm1.parameters():
320
+ p._sequence_parallel = True
321
+ if hasattr(self, "norm2"):
322
+ for p in self.norm2.parameters():
323
+ p._sequence_parallel = True
324
+ # Mark the norm parameters as "shared_params" so that we sync their values at init.
325
+ if mark_shared_params:
326
+ for p in self.norm1.parameters():
327
+ p._shared_params = True
328
+ if hasattr(self, "norm2"):
329
+ for p in self.norm2.parameters():
330
+ p._shared_params = True
331
+
332
+ def allocate_inference_cache(self, batch_size, max_seqlen, dtype=None, **kwargs):
333
+ return self.mixer.allocate_inference_cache(batch_size, max_seqlen, dtype=dtype, **kwargs)
334
+
335
+ def forward(
336
+ self,
337
+ hidden_states1: Tensor,
338
+ hidden_states2: Optional[Tensor] = None,
339
+ residual: Optional[Tensor] = None,
340
+ mixer_kwargs=None,
341
+ ):
342
+ r"""Pass the input through the encoder layer.
343
+
344
+ Args:
345
+ hidden_states1: the output of the previous attention (mixer) or embedding layer.
346
+ hidden_states2: the output of the previous MLP layer (if None, will use hidden_states1).
347
+ residual.
348
+ """
349
+ # TODO: Ideally we should only do the allgather / allreduce once for
350
+ # the Linear to MLP & Attention
351
+ if not self.fused_dropout_add_ln:
352
+ dropped1 = self.dropout1(hidden_states1)
353
+ # For the very 1st block, we only want 1 dropout, not two different dropouts
354
+ if hidden_states2 is not None:
355
+ dropped2 = self.dropout2(hidden_states2)
356
+ residual = (
357
+ (residual + dropped1 + dropped2)
358
+ if residual is not None
359
+ else dropped1 + dropped2
360
+ )
361
+ else:
362
+ residual = (residual + dropped1) if residual is not None else dropped1
363
+ hidden_states1 = self.norm1(residual.to(dtype=self.norm1.weight.dtype))
364
+ hidden_states2 = (
365
+ self.norm2(residual.to(dtype=self.norm2.weight.dtype))
366
+ if not self.tied_norm
367
+ else hidden_states1
368
+ )
369
+ if self.residual_in_fp32:
370
+ residual = residual.to(torch.float32)
371
+ else:
372
+ weight2, bias2 = (
373
+ (self.norm2.weight, self.norm2.bias) if not self.tied_norm else (None, None)
374
+ )
375
+ hidden_states1, *rest, residual = layer_norm_fn(
376
+ hidden_states1,
377
+ self.norm1.weight,
378
+ self.norm1.bias,
379
+ residual=residual,
380
+ x1=hidden_states2,
381
+ weight1=weight2,
382
+ bias1=bias2,
383
+ eps=self.norm1.eps,
384
+ dropout_p=self.dropout1.p if self.training else 0.0,
385
+ prenorm=True,
386
+ residual_in_fp32=self.residual_in_fp32,
387
+ is_rms_norm=isinstance(self.norm1, RMSNorm)
388
+ )
389
+ if self.tied_norm:
390
+ hidden_states2 = hidden_states1
391
+ else:
392
+ hidden_states2, = rest
393
+ if mixer_kwargs is None:
394
+ mixer_kwargs = {}
395
+ hidden_states1 = self.mixer(hidden_states1, **mixer_kwargs)
396
+ hidden_states2 = self.mlp(hidden_states2)
397
+ return hidden_states1, hidden_states2, residual
evalkit_tf446/lib/python3.10/site-packages/flash_attn/modules/embedding.py ADDED
@@ -0,0 +1,216 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Copyright (c) 2022, Tri Dao.
2
+
3
+ import torch
4
+ import torch.nn as nn
5
+ from einops import rearrange
6
+ from torch import Tensor
7
+
8
+ from flash_attn.utils.distributed import all_reduce, reduce_scatter
9
+
10
+
11
+ class GPT2Embeddings(nn.Module):
12
+ def __init__(
13
+ self,
14
+ embed_dim,
15
+ vocab_size,
16
+ max_position_embeddings,
17
+ padding_idx=None,
18
+ word_embed_proj_dim=None,
19
+ device=None,
20
+ dtype=None,
21
+ ):
22
+ """
23
+ If max_position_embeddings <= 0, there's no position embeddings
24
+ If word_embe_proj_dim is not None (e.g., OPT-350m), we embed to that dimension
25
+ the project up to embed_dim
26
+ """
27
+ factory_kwargs = {"device": device, "dtype": dtype}
28
+ super().__init__()
29
+ if word_embed_proj_dim is None:
30
+ self.word_embeddings = nn.Embedding(
31
+ vocab_size, embed_dim, padding_idx=padding_idx, **factory_kwargs
32
+ )
33
+ self.project_in = None
34
+ else:
35
+ self.word_embeddings = nn.Embedding(
36
+ vocab_size, word_embed_proj_dim, padding_idx=padding_idx, **factory_kwargs
37
+ )
38
+ self.project_in = nn.Linear(
39
+ word_embed_proj_dim, embed_dim, bias=False, **factory_kwargs
40
+ )
41
+ self.max_position_embeddings = max_position_embeddings
42
+ if self.max_position_embeddings > 0:
43
+ self.position_embeddings = nn.Embedding(
44
+ max_position_embeddings, embed_dim, **factory_kwargs
45
+ )
46
+
47
+ def forward(self, input_ids, position_ids=None):
48
+ """
49
+ input_ids: (batch, seqlen)
50
+ position_ids: (batch, seqlen)
51
+ """
52
+ batch_size, seqlen = input_ids.shape
53
+ embeddings = self.word_embeddings(input_ids)
54
+ if self.project_in is not None:
55
+ embeddings = self.project_in(embeddings)
56
+ if self.max_position_embeddings > 0:
57
+ if position_ids is None:
58
+ position_ids = torch.arange(seqlen, dtype=torch.long, device=input_ids.device)
59
+ position_embeddings = self.position_embeddings(position_ids)
60
+ embeddings = embeddings + position_embeddings
61
+ return embeddings
62
+
63
+
64
+ class BertEmbeddings(nn.Module):
65
+ def __init__(
66
+ self,
67
+ embed_dim,
68
+ vocab_size,
69
+ max_position_embeddings,
70
+ type_vocab_size,
71
+ padding_idx=None,
72
+ device=None,
73
+ dtype=None,
74
+ ):
75
+ """
76
+ If max_position_embeddings <= 0, there's no position embeddings
77
+ If type_vocab_size <= 0, there's no token type embeddings
78
+ """
79
+ factory_kwargs = {"device": device, "dtype": dtype}
80
+ super().__init__()
81
+ self.word_embeddings = nn.Embedding(
82
+ vocab_size, embed_dim, padding_idx=padding_idx, **factory_kwargs
83
+ )
84
+ self.max_position_embeddings = max_position_embeddings
85
+ self.type_vocab_size = type_vocab_size
86
+ if self.max_position_embeddings > 0:
87
+ self.position_embeddings = nn.Embedding(
88
+ max_position_embeddings, embed_dim, **factory_kwargs
89
+ )
90
+ if self.type_vocab_size > 0:
91
+ self.token_type_embeddings = nn.Embedding(type_vocab_size, embed_dim, **factory_kwargs)
92
+
93
+ def forward(self, input_ids, position_ids=None, token_type_ids=None):
94
+ """
95
+ input_ids: (batch, seqlen)
96
+ position_ids: (batch, seqlen)
97
+ token_type_ids: (batch, seqlen)
98
+ """
99
+ batch_size, seqlen = input_ids.shape
100
+ embeddings = self.word_embeddings(input_ids)
101
+ if self.max_position_embeddings > 0:
102
+ if position_ids is None:
103
+ position_ids = torch.arange(seqlen, dtype=torch.long, device=input_ids.device)
104
+ position_embeddings = self.position_embeddings(position_ids)
105
+ embeddings = embeddings + position_embeddings
106
+ if self.type_vocab_size > 0:
107
+ if token_type_ids is None:
108
+ token_type_ids = torch.zeros(seqlen, dtype=torch.long, device=input_ids.device)
109
+ token_type_embeddings = self.token_type_embeddings(token_type_ids)
110
+ embeddings = embeddings + token_type_embeddings
111
+ return embeddings
112
+
113
+
114
+ class VocabParallelEmbedding(nn.Embedding):
115
+ def __init__(self, num_embeddings, *args, process_group=None, padding_idx=None, **kwargs):
116
+ self.process_group = process_group
117
+ if process_group is not None:
118
+ world_size = torch.distributed.get_world_size(process_group)
119
+ if num_embeddings % world_size != 0:
120
+ raise ValueError(
121
+ f"num_embeddings ({num_embeddings}) must be divisible by "
122
+ f"world_size ({world_size})"
123
+ )
124
+ if world_size > 1 and padding_idx is not None:
125
+ raise RuntimeError("ParallelEmbedding does not support padding_idx")
126
+ else:
127
+ world_size = 1
128
+ super().__init__(num_embeddings // world_size, *args, padding_idx=padding_idx, **kwargs)
129
+
130
+ def forward(self, input: Tensor) -> Tensor:
131
+ if self.process_group is None:
132
+ return super().forward(input)
133
+ else:
134
+ rank = torch.distributed.get_rank(self.process_group)
135
+ vocab_size = self.num_embeddings
136
+ vocab_start_index, vocab_end_index = rank * vocab_size, (rank + 1) * vocab_size
137
+ # Create a mask of valid vocab ids (1 means it needs to be masked).
138
+ input_ids_mask = (input < vocab_start_index) | (input >= vocab_end_index)
139
+ input = input - vocab_start_index
140
+ input[input_ids_mask] = 0
141
+ embeddings = super().forward(input)
142
+ embeddings[input_ids_mask] = 0.0
143
+ return embeddings
144
+
145
+
146
+ class ColumnParallelEmbedding(nn.Embedding):
147
+ def __init__(self, num_embeddings, embedding_dim, *args, process_group=None, **kwargs):
148
+ self.process_group = process_group
149
+ if process_group is not None:
150
+ world_size = torch.distributed.get_world_size(process_group)
151
+ if embedding_dim % world_size != 0:
152
+ raise ValueError(
153
+ f"embedding_dim ({embedding_dim}) must be divisible by "
154
+ f"world_size ({world_size})"
155
+ )
156
+ else:
157
+ world_size = 1
158
+ super().__init__(num_embeddings, embedding_dim // world_size, *args, **kwargs)
159
+
160
+
161
+ class ParallelGPT2Embeddings(nn.Module):
162
+ def __init__(
163
+ self,
164
+ embed_dim,
165
+ vocab_size,
166
+ max_position_embeddings,
167
+ process_group,
168
+ padding_idx=None,
169
+ sequence_parallel=True,
170
+ device=None,
171
+ dtype=None,
172
+ ):
173
+ """
174
+ If max_position_embeddings <= 0, there's no position embeddings
175
+ """
176
+ factory_kwargs = {"device": device, "dtype": dtype}
177
+ super().__init__()
178
+ self.process_group = process_group
179
+ self.sequence_parallel = sequence_parallel
180
+ self.word_embeddings = VocabParallelEmbedding(
181
+ vocab_size,
182
+ embed_dim,
183
+ padding_idx=padding_idx,
184
+ process_group=process_group,
185
+ **factory_kwargs,
186
+ )
187
+ self.max_position_embeddings = max_position_embeddings
188
+ if self.max_position_embeddings > 0:
189
+ self.position_embeddings = ColumnParallelEmbedding(
190
+ max_position_embeddings, embed_dim, process_group=process_group, **factory_kwargs
191
+ )
192
+
193
+ def forward(self, input_ids, position_ids=None, combine_batch_seqlen_dim=False):
194
+ """
195
+ input_ids: (batch, seqlen)
196
+ position_ids: (batch, seqlen)
197
+ """
198
+ batch_size, seqlen = input_ids.shape
199
+ world_size = torch.distributed.get_world_size(self.process_group)
200
+ embeddings = self.word_embeddings(input_ids)
201
+ if self.max_position_embeddings > 0:
202
+ if position_ids is None:
203
+ position_ids = torch.arange(seqlen, dtype=torch.long, device=input_ids.device)
204
+ position_embeddings = self.position_embeddings(position_ids)
205
+ if world_size <= 1:
206
+ embeddings = embeddings + position_embeddings
207
+ else:
208
+ partition_dim = self.position_embeddings.embedding_dim
209
+ rank = torch.distributed.get_rank(self.process_group)
210
+ embeddings[
211
+ ..., rank * partition_dim : (rank + 1) * partition_dim
212
+ ] += position_embeddings
213
+ if combine_batch_seqlen_dim:
214
+ embeddings = rearrange(embeddings, "b s d -> (b s) d")
215
+ reduce_fn = reduce_scatter if self.sequence_parallel else all_reduce
216
+ return embeddings if world_size <= 1 else reduce_fn(embeddings, self.process_group)
evalkit_tf446/lib/python3.10/site-packages/flash_attn/modules/mha.py ADDED
@@ -0,0 +1,1020 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Copyright (c) 2023, Tri Dao.
2
+
3
+ import math
4
+ from functools import partial
5
+
6
+ import torch
7
+ import torch.nn as nn
8
+ from einops import rearrange, repeat
9
+
10
+ from flash_attn.utils.distributed import get_dim_for_local_rank
11
+
12
+ try:
13
+ from flash_attn import (
14
+ flash_attn_kvpacked_func,
15
+ flash_attn_qkvpacked_func,
16
+ flash_attn_varlen_kvpacked_func,
17
+ flash_attn_varlen_qkvpacked_func,
18
+ flash_attn_with_kvcache,
19
+ )
20
+ except ImportError:
21
+ flash_attn_varlen_qkvpacked_func, flash_attn_varlen_kvpacked_func = None, None
22
+ flash_attn_qkvpacked_func, flash_attn_kvpacked_func = None, None
23
+ flash_attn_with_kvcache = None
24
+
25
+ try:
26
+ from flash_attn.ops.fused_dense import ColumnParallelLinear, FusedDense, RowParallelLinear
27
+ except ImportError:
28
+ FusedDense, ColumnParallelLinear, RowParallelLinear = None, None, None
29
+
30
+ try:
31
+ from flash_attn.layers.rotary import RotaryEmbedding
32
+ except ImportError:
33
+ RotaryEmbedding = None
34
+
35
+
36
+ # From https://github.com/ofirpress/attention_with_linear_biases/blob/4b92f28a005ead2567abe2359f633e73e08f3833/fairseq/models/transformer.py#L742
37
+ def get_alibi_slopes(nheads):
38
+ def get_slopes_power_of_2(nheads):
39
+ start = 2 ** (-(2 ** -(math.log2(nheads) - 3)))
40
+ ratio = start
41
+ return [start * ratio**i for i in range(nheads)]
42
+
43
+ if math.log2(nheads).is_integer():
44
+ return get_slopes_power_of_2(nheads)
45
+ else:
46
+ closest_power_of_2 = 2 ** math.floor(math.log2(nheads))
47
+ return (
48
+ get_slopes_power_of_2(closest_power_of_2)
49
+ + get_alibi_slopes(2 * closest_power_of_2)[0::2][: nheads - closest_power_of_2]
50
+ )
51
+
52
+
53
+ class FlashSelfAttention(nn.Module):
54
+ """Implement the scaled dot product attention with softmax.
55
+ Arguments
56
+ ---------
57
+ softmax_scale: The temperature to use for the softmax attention.
58
+ (default: 1/sqrt(d_keys) where d_keys is computed at
59
+ runtime)
60
+ attention_dropout: The dropout rate to apply to the attention
61
+ (default: 0.0)
62
+ """
63
+
64
+ def __init__(
65
+ self,
66
+ causal=False,
67
+ softmax_scale=None,
68
+ attention_dropout=0.0,
69
+ window_size=(-1, -1),
70
+ alibi_slopes=None,
71
+ deterministic=False,
72
+ ):
73
+ super().__init__()
74
+ assert flash_attn_varlen_qkvpacked_func is not None, "FlashAttention is not installed"
75
+ assert flash_attn_qkvpacked_func is not None, "FlashAttention is not installed"
76
+ self.causal = causal
77
+ self.softmax_scale = softmax_scale
78
+ self.drop = nn.Dropout(attention_dropout)
79
+ self.register_buffer("alibi_slopes", alibi_slopes, persistent=False)
80
+ self.window_size = window_size
81
+ self.deterministic = deterministic
82
+
83
+ def forward(self, qkv, causal=None, cu_seqlens=None, max_seqlen=None):
84
+ """Implements the multihead softmax attention.
85
+ Arguments
86
+ ---------
87
+ qkv: The tensor containing the query, key, and value.
88
+ If cu_seqlens is None and max_seqlen is None, then qkv has shape (B, S, 3, H, D).
89
+ If cu_seqlens is not None and max_seqlen is not None, then qkv has shape
90
+ (total, 3, H, D), where total is the sum of the sequence lengths in the batch.
91
+ causal: if passed, will override self.causal
92
+ cu_seqlens: (batch_size + 1,), dtype torch.int32. The cumulative sequence lengths
93
+ of the sequences in the batch, used to index into qkv.
94
+ max_seqlen: int. Maximum sequence length in the batch.
95
+ Returns:
96
+ --------
97
+ out: (total, H, D) if cu_seqlens is not None and max_seqlen is not None,
98
+ else (B, S, H, D).
99
+ """
100
+ assert qkv.dtype in [torch.float16, torch.bfloat16]
101
+ assert qkv.is_cuda
102
+ causal = self.causal if causal is None else causal
103
+ unpadded = cu_seqlens is not None
104
+ if self.alibi_slopes is not None:
105
+ self.alibi_slopes = self.alibi_slopes.to(torch.float32)
106
+ if unpadded:
107
+ assert cu_seqlens.dtype == torch.int32
108
+ assert max_seqlen is not None
109
+ assert isinstance(max_seqlen, int)
110
+ return flash_attn_varlen_qkvpacked_func(
111
+ qkv,
112
+ cu_seqlens,
113
+ max_seqlen,
114
+ self.drop.p if self.training else 0.0,
115
+ softmax_scale=self.softmax_scale,
116
+ causal=causal,
117
+ alibi_slopes=self.alibi_slopes,
118
+ window_size=self.window_size,
119
+ deterministic=self.deterministic,
120
+ )
121
+ else:
122
+ return flash_attn_qkvpacked_func(
123
+ qkv,
124
+ self.drop.p if self.training else 0.0,
125
+ softmax_scale=self.softmax_scale,
126
+ causal=causal,
127
+ alibi_slopes=self.alibi_slopes,
128
+ window_size=self.window_size,
129
+ deterministic=self.deterministic,
130
+ )
131
+
132
+
133
+ class FlashCrossAttention(nn.Module):
134
+ """Implement the scaled dot product attention with softmax.
135
+ Arguments
136
+ ---------
137
+ softmax_scale: The temperature to use for the softmax attention.
138
+ (default: 1/sqrt(d_keys) where d_keys is computed at
139
+ runtime)
140
+ attention_dropout: The dropout rate to apply to the attention
141
+ (default: 0.0)
142
+ """
143
+
144
+ def __init__(
145
+ self,
146
+ causal=False,
147
+ softmax_scale=None,
148
+ attention_dropout=0.0,
149
+ alibi_slopes=None,
150
+ window_size=(-1, -1),
151
+ deterministic=False,
152
+ ):
153
+ super().__init__()
154
+ assert flash_attn_varlen_kvpacked_func is not None, "FlashAttention is not installed"
155
+ assert flash_attn_kvpacked_func is not None, "FlashAttention is not installed"
156
+ self.causal = causal
157
+ self.softmax_scale = softmax_scale
158
+ self.drop = nn.Dropout(attention_dropout)
159
+ self.register_buffer("alibi_slopes", alibi_slopes, persistent=False)
160
+ self.window_size = window_size
161
+ self.deterministic = deterministic
162
+
163
+ def forward(
164
+ self,
165
+ q,
166
+ kv,
167
+ causal=None,
168
+ cu_seqlens=None,
169
+ max_seqlen=None,
170
+ cu_seqlens_k=None,
171
+ max_seqlen_k=None,
172
+ ):
173
+ """Implements the multihead softmax attention.
174
+ Arguments
175
+ ---------
176
+ q: The tensor containing the query. (B, Sq, H, D)
177
+ kv: The tensor containing the key and value. (B, Sk, 2, H_k, D)
178
+ causal: if passed, will override self.causal
179
+ cu_seqlens: (batch_size + 1,), dtype torch.int32. The cumulative sequence lengths
180
+ of the sequences in the batch, used to index into q.
181
+ max_seqlen: int. Maximum sequence length in the batch of q.
182
+ cu_seqlens_k: (batch_size + 1,), dtype torch.int32. The cumulative sequence lengths
183
+ of the sequences in the batch, used to index into kv.
184
+ max_seqlen_k: int. Maximum sequence length in the batch of k and v.
185
+ """
186
+ assert q.dtype in [torch.float16, torch.bfloat16]
187
+ assert q.is_cuda and kv.is_cuda
188
+ causal = self.causal if causal is None else causal
189
+ unpadded = cu_seqlens is not None
190
+ if self.alibi_slopes is not None:
191
+ self.alibi_slopes = self.alibi_slopes.to(torch.float32)
192
+ if unpadded:
193
+ assert cu_seqlens.dtype == torch.int32
194
+ assert max_seqlen is not None
195
+ assert isinstance(max_seqlen, int)
196
+ assert cu_seqlens_k is not None
197
+ assert cu_seqlens_k.dtype == torch.int32
198
+ assert max_seqlen_k is not None
199
+ assert isinstance(max_seqlen_k, int)
200
+ return flash_attn_varlen_kvpacked_func(
201
+ q,
202
+ kv,
203
+ cu_seqlens,
204
+ cu_seqlens_k,
205
+ max_seqlen,
206
+ max_seqlen_k,
207
+ self.drop.p if self.training else 0.0,
208
+ softmax_scale=self.softmax_scale,
209
+ causal=causal,
210
+ alibi_slopes=self.alibi_slopes,
211
+ window_size=self.window_size,
212
+ deterministic=self.deterministic,
213
+ )
214
+ else:
215
+ batch_size, seqlen_q = q.shape[0], q.shape[1]
216
+ seqlen_k = kv.shape[1]
217
+ assert kv.shape[0] == batch_size and kv.shape[4] == q.shape[3]
218
+ return flash_attn_kvpacked_func(
219
+ q,
220
+ kv,
221
+ self.drop.p if self.training else 0.0,
222
+ causal=causal,
223
+ softmax_scale=self.softmax_scale,
224
+ alibi_slopes=self.alibi_slopes,
225
+ window_size=self.window_size,
226
+ deterministic=self.deterministic,
227
+ )
228
+
229
+
230
+ class SelfAttention(nn.Module):
231
+ """Implement the scaled dot product attention with softmax.
232
+ Arguments
233
+ ---------
234
+ softmax_scale: The temperature to use for the softmax attention.
235
+ (default: 1/sqrt(d_keys) where d_keys is computed at
236
+ runtime)
237
+ attention_dropout: The dropout rate to apply to the attention
238
+ (default: 0.0)
239
+ """
240
+
241
+ def __init__(self, causal=False, softmax_scale=None, attention_dropout=0.0):
242
+ super().__init__()
243
+ self.causal = causal
244
+ self.softmax_scale = softmax_scale
245
+ self.drop = nn.Dropout(attention_dropout)
246
+
247
+ def forward(self, qkv, causal=None, key_padding_mask=None):
248
+ """Implements the multihead softmax attention.
249
+ Arguments
250
+ ---------
251
+ qkv: The tensor containing the query, key, and value. (B, S, 3, H, D)
252
+ causal: if passed, will override self.causal
253
+ key_padding_mask: boolean mask to apply to the attention weights. True means to keep,
254
+ False means to mask out. (B, S)
255
+ """
256
+ batch_size, seqlen = qkv.shape[0], qkv.shape[1]
257
+ causal = self.causal if causal is None else causal
258
+ q, k, v = qkv.unbind(dim=2)
259
+ softmax_scale = self.softmax_scale or 1.0 / math.sqrt(q.shape[-1])
260
+ scores = torch.einsum("bthd,bshd->bhts", q, k * softmax_scale)
261
+ if key_padding_mask is not None:
262
+ padding_mask = torch.full(
263
+ (batch_size, seqlen), -10000.0, dtype=scores.dtype, device=scores.device
264
+ )
265
+ padding_mask.masked_fill_(key_padding_mask, 0.0)
266
+ # TD [2022-09-30]: Adding is faster than masked_fill_ (idk why, just better kernel I guess)
267
+ scores = scores + rearrange(padding_mask, "b s -> b 1 1 s")
268
+ if causal:
269
+ # "triu_tril_cuda_template" not implemented for 'BFloat16'
270
+ # So we have to construct the mask in float
271
+ causal_mask = torch.triu(
272
+ torch.full((seqlen, seqlen), -10000.0, device=scores.device), 1
273
+ )
274
+ # TD [2022-09-30]: Adding is faster than masked_fill_ (idk why, just better kernel I guess)
275
+ scores = scores + causal_mask.to(dtype=scores.dtype)
276
+ attention = torch.softmax(scores, dim=-1, dtype=v.dtype)
277
+ attention_drop = self.drop(attention)
278
+ output = torch.einsum("bhts,bshd->bthd", attention_drop, v)
279
+ return output
280
+
281
+
282
+ class CrossAttention(nn.Module):
283
+ """Implement the scaled dot product attention with softmax.
284
+ Arguments
285
+ ---------
286
+ softmax_scale: The temperature to use for the softmax attention.
287
+ (default: 1/sqrt(d_keys) where d_keys is computed at
288
+ runtime)
289
+ attention_dropout: The dropout rate to apply to the attention
290
+ (default: 0.0)
291
+ """
292
+
293
+ def __init__(self, causal=False, softmax_scale=None, attention_dropout=0.0):
294
+ super().__init__()
295
+ self.causal = causal
296
+ self.softmax_scale = softmax_scale
297
+ self.drop = nn.Dropout(attention_dropout)
298
+
299
+ def forward(self, q, kv, causal=None, key_padding_mask=None):
300
+ """Implements the multihead softmax attention.
301
+ Arguments
302
+ ---------
303
+ q: The tensor containing the query. (B, Sq, H, D)
304
+ kv: The tensor containing the key and value. (B, Sk, 2, H_k, D)
305
+ causal: if passed, will override self.causal
306
+ key_padding_mask: boolean mask to apply to the attention weights. True means to keep,
307
+ False means to mask out. (B, Sk)
308
+ """
309
+ batch_size, seqlen_q = q.shape[0], q.shape[1]
310
+ causal = self.causal if causal is None else causal
311
+ seqlen_k = kv.shape[1]
312
+ assert kv.shape[0] == batch_size and kv.shape[4] == q.shape[3]
313
+ if kv.shape[3] != q.shape[2]: # MQA/GQA
314
+ kv = repeat(kv, "... hkv d -> ... (hkv g) d", g=q.shape[2] // kv.shape[3])
315
+ k, v = kv.unbind(dim=2)
316
+ softmax_scale = self.softmax_scale or 1.0 / math.sqrt(q.shape[-1])
317
+ scores = torch.einsum("bthd,bshd->bhts", q, k * softmax_scale)
318
+ if key_padding_mask is not None:
319
+ padding_mask = torch.full(
320
+ (batch_size, seqlen_k), -10000.0, dtype=scores.dtype, device=scores.device
321
+ )
322
+ padding_mask.masked_fill_(key_padding_mask, 0.0)
323
+ # TD [2022-09-30]: Adding is faster than masked_fill_ (idk why, just better kernel I guess)
324
+ scores = scores + rearrange(padding_mask, "b s -> b 1 1 s")
325
+ if causal:
326
+ # causal mask needs to take into account the difference between seqlen_q and seqlen_k
327
+ row_idx = rearrange(
328
+ torch.arange(seqlen_q, device=q.device, dtype=torch.long), "s -> s 1"
329
+ )
330
+ col_idx = torch.arange(seqlen_k, device=kv.device, dtype=torch.long)
331
+ sk = (
332
+ seqlen_k
333
+ if key_padding_mask is None
334
+ else rearrange(key_padding_mask.sum(-1), "b -> b 1 1 1")
335
+ )
336
+ causal_mask = col_idx > row_idx + sk - seqlen_q
337
+ scores = scores.masked_fill(causal_mask, -10000.0)
338
+ attention = torch.softmax(scores, dim=-1, dtype=v.dtype)
339
+ attention_drop = self.drop(attention)
340
+ output = torch.einsum("bhts,bshd->bthd", attention_drop, v)
341
+ return output
342
+
343
+
344
+ class LinearResidual(nn.Linear):
345
+ """Wrap nn.Linear to return the residual as well. For compatibility with FusedDense."""
346
+
347
+ def forward(self, input: torch.Tensor) -> torch.Tensor:
348
+ return super().forward(input), input
349
+
350
+
351
+ def _update_kv_cache(kv, inference_params, layer_idx):
352
+ """kv: (batch_size, seqlen, 2, nheads, head_dim) or (batch_size, 1, 2, nheads, head_dim)"""
353
+ # Pre-allocate memory for key-values for inference.
354
+ num_heads, head_dim = kv.shape[-2:]
355
+ if layer_idx not in inference_params.key_value_memory_dict:
356
+ kv_cache = torch.empty(
357
+ inference_params.max_batch_size,
358
+ inference_params.max_seqlen,
359
+ 2,
360
+ num_heads,
361
+ head_dim,
362
+ dtype=kv.dtype,
363
+ device=kv.device,
364
+ )
365
+ inference_params.key_value_memory_dict[layer_idx] = kv_cache
366
+ else:
367
+ kv_cache = inference_params.key_value_memory_dict[layer_idx]
368
+ # Adjust key and value for inference
369
+ batch_start = inference_params.batch_size_offset
370
+ batch_end = batch_start + kv.shape[0]
371
+ sequence_start = inference_params.seqlen_offset
372
+ sequence_end = sequence_start + kv.shape[1]
373
+ assert batch_end <= kv_cache.shape[0]
374
+ assert sequence_end <= kv_cache.shape[1]
375
+ assert kv_cache is not None
376
+ kv_cache[batch_start:batch_end, sequence_start:sequence_end, ...] = kv
377
+ return kv_cache[batch_start:batch_end, :sequence_end, ...]
378
+
379
+
380
+ class MHA(nn.Module):
381
+ """Multi-head self-attention and cross-attention"""
382
+
383
+ def __init__(
384
+ self,
385
+ embed_dim,
386
+ num_heads,
387
+ num_heads_kv=None,
388
+ cross_attn=False,
389
+ qkv_proj_bias=True,
390
+ out_proj_bias=True,
391
+ dropout=0.0,
392
+ softmax_scale=None,
393
+ causal=False,
394
+ layer_idx=None,
395
+ dwconv=False,
396
+ rotary_emb_dim=0,
397
+ rotary_emb_base=10000.0,
398
+ rotary_emb_scale_base=None,
399
+ rotary_emb_interleaved=False,
400
+ use_alibi=False,
401
+ window_size=(-1, -1),
402
+ fused_bias_fc=False,
403
+ use_flash_attn=False,
404
+ return_residual=False,
405
+ checkpointing=False,
406
+ device=None,
407
+ dtype=None,
408
+ ) -> None:
409
+ """
410
+ num_heads_kv: can be used to toggle MQA / GQA. If None, use num_heads.
411
+ return_residual: whether to return the input x along with the output. This is for
412
+ performance reason: for post-norm architecture, returning the input allows us
413
+ to fuse the backward of nn.Linear with the residual connection.
414
+ """
415
+ factory_kwargs = {"device": device, "dtype": dtype}
416
+ super().__init__()
417
+ self.embed_dim = embed_dim
418
+ self.cross_attn = cross_attn
419
+ self.causal = causal
420
+ self.layer_idx = layer_idx
421
+ self.dwconv = dwconv
422
+ self.rotary_emb_dim = rotary_emb_dim
423
+ self.use_flash_attn = use_flash_attn
424
+ self.return_residual = return_residual
425
+ self.checkpointing = checkpointing
426
+ if use_alibi:
427
+ assert use_flash_attn, "ALiBi code path requires flash_attn"
428
+ alibi_slopes = torch.tensor(get_alibi_slopes(num_heads), device=device)
429
+ else:
430
+ alibi_slopes = None
431
+ if window_size != (-1, -1):
432
+ assert use_flash_attn, "Local (sliding window) attention code path requires flash_attn"
433
+
434
+ self.num_heads = num_heads
435
+ self.num_heads_kv = num_heads_kv if num_heads_kv is not None else num_heads
436
+ assert (
437
+ self.num_heads % self.num_heads_kv == 0
438
+ ), "num_heads must be divisible by num_heads_kv"
439
+ assert self.embed_dim % num_heads == 0, "embed_dim must be divisible by num_heads"
440
+ self.head_dim = self.embed_dim // num_heads
441
+ qkv_dim = self.head_dim * (self.num_heads + 2 * self.num_heads_kv)
442
+ kv_dim = 2 * self.head_dim * self.num_heads_kv
443
+
444
+ if self.rotary_emb_dim > 0:
445
+ assert not cross_attn, "MHA with rotary embedding does not support cross-attention yet"
446
+ assert RotaryEmbedding is not None, "rotary_emb is not installed"
447
+ self.rotary_emb = RotaryEmbedding(
448
+ self.rotary_emb_dim,
449
+ base=rotary_emb_base,
450
+ scale_base=rotary_emb_scale_base,
451
+ interleaved=rotary_emb_interleaved,
452
+ device=device,
453
+ )
454
+
455
+ if fused_bias_fc and FusedDense is None:
456
+ raise ImportError("fused_dense is not installed")
457
+ linear_cls = nn.Linear if not fused_bias_fc else FusedDense
458
+ linear_resid_cls = (
459
+ LinearResidual if not fused_bias_fc else partial(FusedDense, return_residual=True)
460
+ )
461
+ wqkv_cls = linear_cls if not self.return_residual else linear_resid_cls
462
+ inner_attn_cls = (
463
+ partial(FlashSelfAttention, alibi_slopes=alibi_slopes, window_size=window_size)
464
+ if use_flash_attn
465
+ else SelfAttention
466
+ )
467
+ inner_cross_attn_cls = (
468
+ partial(FlashCrossAttention, alibi_slopes=alibi_slopes, window_size=window_size)
469
+ if use_flash_attn
470
+ else CrossAttention
471
+ )
472
+ if not self.cross_attn:
473
+ self.Wqkv = wqkv_cls(embed_dim, qkv_dim, bias=qkv_proj_bias, **factory_kwargs)
474
+ else:
475
+ self.Wq = linear_cls(embed_dim, embed_dim, bias=qkv_proj_bias, **factory_kwargs)
476
+ self.Wkv = wqkv_cls(embed_dim, kv_dim, bias=qkv_proj_bias, **factory_kwargs)
477
+ if self.dwconv:
478
+ if self.num_heads_kv == self.num_heads:
479
+ self.dwconv_qkv = nn.Conv1d(
480
+ qkv_dim, qkv_dim, kernel_size=3, padding=2, groups=qkv_dim
481
+ )
482
+ else:
483
+ self.dwconv_q = nn.Conv1d(
484
+ embed_dim, embed_dim, kernel_size=3, padding=2, groups=embed_dim
485
+ )
486
+ self.dwconv_kv = nn.Conv1d(kv_dim, kv_dim, kernel_size=3, padding=2, groups=kv_dim)
487
+ self.inner_attn = inner_attn_cls(
488
+ causal=causal,
489
+ softmax_scale=softmax_scale,
490
+ attention_dropout=dropout,
491
+ )
492
+ self.inner_cross_attn = inner_cross_attn_cls(
493
+ causal=causal, softmax_scale=softmax_scale, attention_dropout=dropout
494
+ )
495
+ self.out_proj = linear_cls(embed_dim, embed_dim, bias=out_proj_bias, **factory_kwargs)
496
+
497
+ def allocate_inference_cache(self, batch_size, max_seqlen, dtype=None):
498
+ dtype = self.out_proj.weight.dtype if dtype is None else dtype
499
+ device = self.out_proj.weight.device
500
+ return torch.empty(
501
+ batch_size,
502
+ max_seqlen,
503
+ 2,
504
+ self.num_heads_kv,
505
+ self.head_dim,
506
+ dtype=dtype,
507
+ device=device,
508
+ )
509
+
510
+ def _update_kv_cache(self, kv, inference_params):
511
+ """kv: (batch_size, seqlen, 2, nheads, head_dim) or (batch_size, 1, 2, nheads, head_dim)"""
512
+ assert not self.dwconv, "Generation does not support dwconv yet"
513
+ assert self.layer_idx is not None, "Generation requires layer_idx in the constructor"
514
+ return _update_kv_cache(kv, inference_params, self.layer_idx)
515
+
516
+ def _apply_rotary_update_kvcache_attention(self, q, kv, inference_params):
517
+ """
518
+ Fast path that combine 3 steps: apply rotary to Q and K, update kv cache, and apply attention.
519
+ q: (batch_size, seqlen_q, nheads, head_dim)
520
+ kv: (batch_size, seqlen_k, 2, nheads_kv, head_dim)
521
+ """
522
+ assert inference_params is not None and inference_params.seqlen_offset > 0
523
+ assert self.use_flash_attn
524
+ if self.rotary_emb_dim > 0:
525
+ assert self.rotary_emb.scale is None, "This code path does not support xPos"
526
+ self.rotary_emb._update_cos_sin_cache(
527
+ inference_params.max_seqlen, device=q.device, dtype=q.dtype
528
+ )
529
+ rotary_cos, rotary_sin = self.rotary_emb._cos_cached, self.rotary_emb._sin_cached
530
+ else:
531
+ rotary_cos, rotary_sin = None, None
532
+ batch = q.shape[0]
533
+ kv_cache = inference_params.key_value_memory_dict[self.layer_idx][:batch]
534
+ cache_seqlens = (
535
+ inference_params.lengths_per_sample[:batch]
536
+ if inference_params.lengths_per_sample is not None
537
+ else inference_params.seqlen_offset
538
+ )
539
+ alibi_slopes = getattr(self.inner_cross_attn, "alibi_slopes", None)
540
+ context = flash_attn_with_kvcache(
541
+ q,
542
+ kv_cache[:, :, 0],
543
+ kv_cache[:, :, 1],
544
+ kv[:, :, 0],
545
+ kv[:, :, 1],
546
+ rotary_cos=rotary_cos,
547
+ rotary_sin=rotary_sin,
548
+ cache_seqlens=cache_seqlens,
549
+ softmax_scale=self.inner_cross_attn.softmax_scale,
550
+ causal=self.inner_cross_attn.causal,
551
+ rotary_interleaved=self.rotary_emb.interleaved if self.rotary_emb_dim > 0 else False,
552
+ alibi_slopes=alibi_slopes,
553
+ )
554
+ return context
555
+
556
+ def _update_kvcache_attention(self, q, kv, inference_params):
557
+ """Write kv to inference_params, then do attention"""
558
+ if (
559
+ inference_params.seqlen_offset == 0
560
+ or flash_attn_with_kvcache is None
561
+ or not self.use_flash_attn
562
+ ):
563
+ # TODO: this only uses seqlen_offset and not lengths_per_sample.
564
+ kv = self._update_kv_cache(kv, inference_params)
565
+ return self.inner_cross_attn(q, kv)
566
+ else:
567
+ batch = q.shape[0]
568
+ kv_cache = inference_params.key_value_memory_dict[self.layer_idx][:batch]
569
+ cache_seqlens = (
570
+ inference_params.lengths_per_sample[:batch]
571
+ if inference_params.lengths_per_sample is not None
572
+ else inference_params.seqlen_offset
573
+ )
574
+ alibi_slopes = getattr(self.inner_cross_attn, "alibi_slopes", None)
575
+ return flash_attn_with_kvcache(
576
+ q,
577
+ kv_cache[:, :, 0],
578
+ kv_cache[:, :, 1],
579
+ kv[:, :, 0],
580
+ kv[:, :, 1],
581
+ cache_seqlens=cache_seqlens,
582
+ softmax_scale=self.inner_cross_attn.softmax_scale,
583
+ causal=self.inner_cross_attn.causal,
584
+ alibi_slopes=alibi_slopes,
585
+ )
586
+
587
+ def forward(
588
+ self,
589
+ x,
590
+ x_kv=None,
591
+ key_padding_mask=None,
592
+ cu_seqlens=None,
593
+ max_seqlen=None,
594
+ mixer_subset=None,
595
+ inference_params=None,
596
+ **kwargs,
597
+ ):
598
+ """
599
+ Arguments:
600
+ x: (batch, seqlen, hidden_dim) (where hidden_dim = num heads * head dim) if
601
+ cu_seqlens is None and max_seqlen is None, else (total, hidden_dim) where total
602
+ is the is the sum of the sequence lengths in the batch.
603
+ x_kv: (batch, seqlen, hidden_dim), only applicable for cross-attention. If None, use x.
604
+ cu_seqlens: (batch_size + 1,), dtype torch.int32. The cumulative sequence lengths
605
+ of the sequences in the batch, used to index into x. Only applicable when using
606
+ FlashAttention.
607
+ max_seqlen: int. Maximum sequence length in the batch.
608
+ key_padding_mask: boolean mask, True means to keep, False means to mask out.
609
+ (batch, seqlen). Only applicable when not using FlashAttention.
610
+ mixer_subset: for cross-attention only. If not None, will take a subset of x
611
+ before applying the query projection. Useful for e.g., ViT where we only care
612
+ about the CLS token in the last layer.
613
+ inference_params: for generation. Adapted from Megatron-LM (and Apex)
614
+ https://github.com/NVIDIA/apex/blob/3ff1a10f72ec07067c4e44759442329804ac5162/apex/transformer/testing/standalone_transformer_lm.py#L470
615
+ """
616
+ if cu_seqlens is not None:
617
+ assert max_seqlen is not None
618
+ assert key_padding_mask is None
619
+ assert self.use_flash_attn
620
+ assert not self.dwconv
621
+ assert self.rotary_emb_dim == 0
622
+ if key_padding_mask is not None:
623
+ assert cu_seqlens is None
624
+ assert max_seqlen is None
625
+ assert not self.use_flash_attn
626
+ if inference_params is not None:
627
+ assert key_padding_mask is None
628
+ assert cu_seqlens is None and max_seqlen is None
629
+ assert not self.dwconv
630
+
631
+ kwargs = (
632
+ {"cu_seqlens": cu_seqlens, "max_seqlen": max_seqlen, **kwargs}
633
+ if self.use_flash_attn
634
+ else {"key_padding_mask": key_padding_mask, **kwargs}
635
+ )
636
+ seqlen_offset = (
637
+ 0
638
+ if inference_params is None
639
+ else (
640
+ inference_params.lengths_per_sample
641
+ if inference_params.lengths_per_sample is not None
642
+ else inference_params.seqlen_offset
643
+ )
644
+ )
645
+ rotary_max_seqlen = inference_params.max_seqlen if inference_params is not None else None
646
+ batch, seqlen = x.shape[:2]
647
+ if not self.cross_attn and self.num_heads_kv == self.num_heads:
648
+ assert x_kv is None and mixer_subset is None
649
+ if not self.return_residual:
650
+ qkv = self.Wqkv(x)
651
+ else:
652
+ qkv, x = self.Wqkv(x)
653
+ if self.dwconv:
654
+ qkv = rearrange(
655
+ self.dwconv_qkv(rearrange(qkv, "b s d -> b d s"))[..., :-2], "b d s -> b s d"
656
+ ).contiguous()
657
+ qkv = rearrange(qkv, "... (three h d) -> ... three h d", three=3, d=self.head_dim)
658
+ if (
659
+ inference_params is None
660
+ or inference_params.seqlen_offset == 0
661
+ or (self.rotary_emb_dim == 0 or self.rotary_emb_dim % 16 != 0)
662
+ or not self.use_flash_attn
663
+ ):
664
+ if self.rotary_emb_dim > 0:
665
+ qkv = self.rotary_emb(
666
+ qkv, seqlen_offset=seqlen_offset, max_seqlen=rotary_max_seqlen
667
+ )
668
+ if inference_params is None:
669
+ if not self.checkpointing:
670
+ context = self.inner_attn(qkv, **kwargs)
671
+ else:
672
+ context = torch.utils.checkpoint.checkpoint(self.inner_attn, qkv, **kwargs)
673
+ else:
674
+ context = self._update_kvcache_attention(
675
+ qkv[:, :, 0], qkv[:, :, 1:], inference_params
676
+ )
677
+ else:
678
+ context = self._apply_rotary_update_kvcache_attention(
679
+ qkv[:, :, 0], qkv[:, :, 1:], inference_params
680
+ )
681
+ else:
682
+ if self.cross_attn:
683
+ if not self.return_residual:
684
+ q = self.Wq(x if mixer_subset is None else x[:, mixer_subset])
685
+ kv = self.Wkv(x_kv if x_kv is not None else x)
686
+ else:
687
+ if x_kv is not None:
688
+ kv, x_kv = self.Wkv(x_kv)
689
+ else:
690
+ kv, x = self.Wkv(x)
691
+ q = self.Wq(x if mixer_subset is None else x[:, mixer_subset])
692
+ else:
693
+ assert self.num_heads_kv != self.num_heads
694
+ if not self.return_residual:
695
+ qkv = self.Wqkv(x)
696
+ else:
697
+ qkv, x = self.Wqkv(x)
698
+ q = qkv[..., : self.num_heads * self.head_dim]
699
+ kv = qkv[..., self.num_heads * self.head_dim :]
700
+ q = rearrange(q, "... (h d) -> ... h d", d=self.head_dim)
701
+ kv = rearrange(kv, "... (two hkv d) -> ... two hkv d", two=2, d=self.head_dim)
702
+ if self.dwconv:
703
+ q = rearrange(
704
+ self.dwconv_q(rearrange(q, "b s d -> b d s"))[..., :-2], "b d s -> b s d"
705
+ ).contiguous()
706
+ kv = rearrange(
707
+ self.dwconv_kv(rearrange(kv, "b s d -> b d s"))[..., :-2], "b d s -> b s d"
708
+ ).contiguous()
709
+ if (
710
+ inference_params is None
711
+ or inference_params.seqlen_offset == 0
712
+ or (self.rotary_emb_dim == 0 or self.rotary_emb_dim % 16 != 0)
713
+ or not self.use_flash_attn
714
+ ):
715
+ if self.rotary_emb_dim > 0:
716
+ q, kv = self.rotary_emb(
717
+ q, kv, seqlen_offset=seqlen_offset, max_seqlen=rotary_max_seqlen
718
+ )
719
+ if inference_params is None:
720
+ if not self.checkpointing:
721
+ context = self.inner_cross_attn(q, kv, **kwargs)
722
+ else:
723
+ context = torch.utils.checkpoint.checkpoint(
724
+ self.inner_cross_attn, q, kv, **kwargs
725
+ )
726
+ else:
727
+ context = self._update_kvcache_attention(q, kv, inference_params)
728
+ else:
729
+ context = self._apply_rotary_update_kvcache_attention(q, kv, inference_params)
730
+ out = self.out_proj(rearrange(context, "... h d -> ... (h d)"))
731
+ return out if not self.return_residual else (out, x)
732
+
733
+
734
+ class ParallelMHA(nn.Module):
735
+ """Multi-head self-attention and cross-attention"""
736
+
737
+ def __init__(
738
+ self,
739
+ embed_dim,
740
+ num_heads,
741
+ process_group,
742
+ num_heads_kv=None,
743
+ qkv_proj_bias=True,
744
+ out_proj_bias=True,
745
+ dropout=0.0,
746
+ softmax_scale=None,
747
+ causal=False,
748
+ layer_idx=None,
749
+ rotary_emb_dim=0,
750
+ rotary_emb_base=10000.0,
751
+ rotary_emb_scale_base=None,
752
+ rotary_emb_interleaved=False,
753
+ use_alibi=False,
754
+ window_size=(-1, -1),
755
+ use_flash_attn=False,
756
+ checkpointing=False,
757
+ sequence_parallel=True,
758
+ device=None,
759
+ dtype=None,
760
+ ) -> None:
761
+ factory_kwargs = {"device": device, "dtype": dtype}
762
+ super().__init__()
763
+ self.embed_dim = embed_dim
764
+ self.causal = causal
765
+ self.layer_idx = layer_idx
766
+ self.rotary_emb_dim = rotary_emb_dim
767
+ self.use_flash_attn = use_flash_attn
768
+ self.checkpointing = checkpointing
769
+ self.process_group = process_group
770
+ self.world_size = process_group.size()
771
+ self.local_rank = torch.distributed.get_rank(process_group)
772
+
773
+ self.num_heads = num_heads
774
+ assert self.embed_dim % self.num_heads == 0, "embed_dim must be divisible by num_heads"
775
+
776
+ self.num_heads_kv = num_heads_kv if num_heads_kv is not None else num_heads
777
+ assert (
778
+ self.num_heads % self.num_heads_kv == 0
779
+ ), "num_heads must be divisible by num_heads_kv"
780
+
781
+ self.num_heads_per_rank = get_dim_for_local_rank(
782
+ self.num_heads, self.world_size, self.local_rank
783
+ )
784
+ self.num_heads_kv_per_rank = get_dim_for_local_rank(
785
+ self.num_heads_kv, self.world_size, self.local_rank
786
+ )
787
+ self.head_dim = self.embed_dim // num_heads
788
+ qkv_dim = self.head_dim * (self.num_heads + 2 * self.num_heads_kv)
789
+
790
+ if use_alibi:
791
+ assert use_flash_attn, "ALiBi code path requires flash_attn"
792
+ num_heads_local = math.ceil(self.num_heads / self.world_size)
793
+ alibi_slopes = torch.tensor(
794
+ get_alibi_slopes(num_heads)[
795
+ self.local_rank * num_heads_local : (self.local_rank + 1) * num_heads_local
796
+ ],
797
+ device=device,
798
+ )
799
+ else:
800
+ alibi_slopes = None
801
+ if window_size != (-1, -1):
802
+ assert use_flash_attn, "Local (sliding window) attention code path requires flash_attn"
803
+
804
+ if self.rotary_emb_dim > 0:
805
+ assert RotaryEmbedding is not None, "rotary_emb is not installed"
806
+ self.rotary_emb = RotaryEmbedding(
807
+ self.rotary_emb_dim,
808
+ base=rotary_emb_base,
809
+ scale_base=rotary_emb_scale_base,
810
+ interleaved=rotary_emb_interleaved,
811
+ device=device,
812
+ )
813
+
814
+ if ColumnParallelLinear is None or RowParallelLinear is None:
815
+ raise ImportError("fused_dense is not installed")
816
+ self.Wqkv = ColumnParallelLinear(
817
+ embed_dim,
818
+ qkv_dim,
819
+ process_group,
820
+ bias=qkv_proj_bias,
821
+ sequence_parallel=sequence_parallel,
822
+ multiple_of=self.head_dim * (self.num_heads // self.num_heads_kv + 2),
823
+ **factory_kwargs,
824
+ )
825
+ inner_attn_cls = (
826
+ partial(FlashSelfAttention, alibi_slopes=alibi_slopes, window_size=window_size)
827
+ if use_flash_attn
828
+ else SelfAttention
829
+ )
830
+ inner_cross_attn_cls = (
831
+ partial(FlashCrossAttention, alibi_slopes=alibi_slopes, window_size=window_size)
832
+ if use_flash_attn
833
+ else CrossAttention
834
+ )
835
+ self.inner_attn = inner_attn_cls(
836
+ causal=causal, softmax_scale=softmax_scale, attention_dropout=dropout
837
+ )
838
+ self.inner_cross_attn = inner_cross_attn_cls(
839
+ causal=causal, softmax_scale=softmax_scale, attention_dropout=dropout
840
+ )
841
+ self.out_proj = RowParallelLinear(
842
+ embed_dim,
843
+ embed_dim,
844
+ process_group,
845
+ bias=out_proj_bias,
846
+ sequence_parallel=sequence_parallel,
847
+ multiple_of=self.head_dim,
848
+ **factory_kwargs,
849
+ )
850
+
851
+ def allocate_inference_cache(self, batch_size, max_seqlen, dtype=None):
852
+ dtype = self.out_proj.weight.dtype if dtype is None else dtype
853
+ device = self.out_proj.weight.device
854
+ return torch.empty(
855
+ batch_size,
856
+ max_seqlen,
857
+ 2,
858
+ self.num_heads_kv_per_rank,
859
+ self.head_dim,
860
+ dtype=dtype,
861
+ device=device,
862
+ )
863
+
864
+ def _update_kv_cache(self, kv, inference_params):
865
+ """kv: (batch_size, seqlen, 2, nheads, head_dim) or (batch_size, 1, 2, nheads, head_dim)"""
866
+ assert self.layer_idx is not None, "Generation requires layer_idx in the constructor"
867
+ return _update_kv_cache(kv, inference_params, self.layer_idx)
868
+
869
+ def _apply_rotary_update_kvcache_attention(self, q, kv, inference_params):
870
+ """
871
+ Fast path that combine 3 steps: apply rotary to Q and K, update kv cache, and apply attention.
872
+ q: (batch_size, seqlen_q, nheads, head_dim)
873
+ kv: (batch_size, seqlen_k, 2, nheads_kv, head_dim)
874
+ """
875
+ assert inference_params is not None and inference_params.seqlen_offset > 0
876
+ assert self.use_flash_attn
877
+ if self.rotary_emb_dim > 0:
878
+ assert self.rotary_emb.scale is None, "This code path does not support xPos"
879
+ self.rotary_emb._update_cos_sin_cache(
880
+ inference_params.max_seqlen, device=q.device, dtype=q.dtype
881
+ )
882
+ rotary_cos, rotary_sin = self.rotary_emb._cos_cached, self.rotary_emb._sin_cached
883
+ else:
884
+ rotary_cos, rotary_sin = None, None
885
+ batch = q.shape[0]
886
+ kv_cache = inference_params.key_value_memory_dict[self.layer_idx][:batch]
887
+ cache_seqlens = (
888
+ inference_params.lengths_per_sample[:batch]
889
+ if inference_params.lengths_per_sample is not None
890
+ else inference_params.seqlen_offset
891
+ )
892
+ alibi_slopes = getattr(self.inner_cross_attn, "alibi_slopes", None)
893
+ context = flash_attn_with_kvcache(
894
+ q,
895
+ kv_cache[:, :, 0],
896
+ kv_cache[:, :, 1],
897
+ kv[:, :, 0],
898
+ kv[:, :, 1],
899
+ rotary_cos=rotary_cos,
900
+ rotary_sin=rotary_sin,
901
+ cache_seqlens=cache_seqlens,
902
+ softmax_scale=self.inner_cross_attn.softmax_scale,
903
+ causal=self.inner_cross_attn.causal,
904
+ rotary_interleaved=self.rotary_emb.interleaved if self.rotary_emb_dim > 0 else False,
905
+ alibi_slopes=alibi_slopes,
906
+ )
907
+ return context
908
+
909
+ def _update_kvcache_attention(self, q, kv, inference_params):
910
+ """Write kv to inference_params, then do attention"""
911
+ if inference_params.seqlen_offset == 0 or not self.use_flash_attn:
912
+ # TODO: this only uses seqlen_offset and not lengths_per_sample.
913
+ kv = self._update_kv_cache(kv, inference_params)
914
+ return self.inner_cross_attn(q, kv)
915
+ else:
916
+ batch = q.shape[0]
917
+ kv_cache = inference_params.key_value_memory_dict[self.layer_idx][:batch]
918
+ cache_seqlens = (
919
+ inference_params.lengths_per_sample[:batch]
920
+ if inference_params.lengths_per_sample is not None
921
+ else inference_params.seqlen_offset
922
+ )
923
+ alibi_slopes = getattr(self.inner_cross_attn, "alibi_slopes", None)
924
+ context = flash_attn_with_kvcache(
925
+ q,
926
+ kv_cache[:, :, 0],
927
+ kv_cache[:, :, 1],
928
+ kv[:, :, 0],
929
+ kv[:, :, 1],
930
+ cache_seqlens=cache_seqlens,
931
+ softmax_scale=self.inner_cross_attn.softmax_scale,
932
+ causal=self.inner_cross_attn.causal,
933
+ alibi_slopes=alibi_slopes,
934
+ )
935
+ return context
936
+
937
+ def forward(self, x, seqlen=None, inference_params=None, **kwargs):
938
+ """
939
+ Arguments:
940
+ x: (batch, seqlen, hidden_dim) (where hidden_dim = num heads * head dim) if seqlen=None.
941
+ If seqlen is not None, x is (batch * seqlen, hidden_dim). This is so that when we
942
+ split x during sequence parallel, we split the batch * seqlen dimension
943
+ (in case batch is small).
944
+ """
945
+ qkv = self.Wqkv(x)
946
+ if seqlen is not None:
947
+ qkv = rearrange(qkv, "(b s) ... -> b s ...", s=seqlen)
948
+ seqlen_offset = (
949
+ 0
950
+ if inference_params is None
951
+ else (
952
+ inference_params.lengths_per_sample
953
+ if inference_params.lengths_per_sample is not None
954
+ else inference_params.seqlen_offset
955
+ )
956
+ )
957
+ rotary_max_seqlen = inference_params.max_seqlen if inference_params is not None else None
958
+ if self.num_heads_kv == self.num_heads:
959
+ qkv = rearrange(qkv, "b s (three h d) -> b s three h d", three=3, d=self.head_dim)
960
+ if (
961
+ inference_params is None
962
+ or inference_params.seqlen_offset == 0
963
+ or (self.rotary_emb_dim == 0 or self.rotary_emb_dim % 16 != 0)
964
+ or not self.use_flash_attn
965
+ ):
966
+ if self.rotary_emb_dim > 0:
967
+ qkv = self.rotary_emb(
968
+ qkv, seqlen_offset=seqlen_offset, max_seqlen=rotary_max_seqlen
969
+ )
970
+ if inference_params is None:
971
+ if not self.checkpointing:
972
+ context = self.inner_attn(qkv, **kwargs)
973
+ else:
974
+ context = torch.utils.checkpoint.checkpoint(self.inner_attn, qkv, **kwargs)
975
+ else:
976
+ context = self._update_kvcache_attention(
977
+ qkv[:, :, 0], qkv[:, :, 1:], inference_params
978
+ )
979
+ else:
980
+ context = self._apply_rotary_update_kvcache_attention(
981
+ qkv[:, :, 0], qkv[:, :, 1:], inference_params
982
+ )
983
+ else:
984
+ q = rearrange(
985
+ qkv[..., : self.num_heads_per_rank * self.head_dim],
986
+ "... (h d) -> ... h d",
987
+ d=self.head_dim,
988
+ )
989
+ kv = rearrange(
990
+ qkv[..., self.num_heads_per_rank * self.head_dim :],
991
+ "... (two hkv d) -> ... two hkv d",
992
+ two=2,
993
+ d=self.head_dim,
994
+ )
995
+ if (
996
+ inference_params is None
997
+ or inference_params.seqlen_offset == 0
998
+ or (self.rotary_emb_dim == 0 or self.rotary_emb_dim % 16 != 0)
999
+ or not self.use_flash_attn
1000
+ ):
1001
+ if self.rotary_emb_dim > 0:
1002
+ q, kv = self.rotary_emb(
1003
+ q, kv, seqlen_offset=seqlen_offset, max_seqlen=rotary_max_seqlen
1004
+ )
1005
+ if inference_params is None:
1006
+ if not self.checkpointing:
1007
+ context = self.inner_cross_attn(q, kv, **kwargs)
1008
+ else:
1009
+ context = torch.utils.checkpoint.checkpoint(
1010
+ self.inner_cross_attn, q, kv, **kwargs
1011
+ )
1012
+ else:
1013
+ context = self._update_kvcache_attention(q, kv, inference_params)
1014
+ else:
1015
+ context = self._apply_rotary_update_kvcache_attention(q, kv, inference_params)
1016
+ context = rearrange(context, "b s h d -> b s (h d)")
1017
+ if seqlen is not None:
1018
+ context = rearrange(context, "b s d -> (b s) d")
1019
+ out = self.out_proj(context)
1020
+ return out
evalkit_tf446/lib/python3.10/site-packages/flash_attn/modules/mlp.py ADDED
@@ -0,0 +1,191 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Copyright (c) 2023, Tri Dao.
2
+
3
+ import torch
4
+ import torch.nn as nn
5
+ import torch.nn.functional as F
6
+ from torch.distributed import ProcessGroup
7
+
8
+
9
+ try:
10
+ from flash_attn.ops.activations import swiglu
11
+ except ImportError:
12
+ swiglu = None
13
+
14
+ try:
15
+ from flash_attn.ops.fused_dense import ColumnParallelLinear, RowParallelLinear
16
+ except ImportError:
17
+ ColumnParallelLinear, RowParallelLinear = None, None
18
+
19
+ try:
20
+ from flash_attn.ops.fused_dense import FusedMLP, ParallelFusedMLP
21
+ except ImportError:
22
+ FusedMLP, ParallelFusedMLP = None, None
23
+
24
+
25
+ class Mlp(nn.Module):
26
+ def __init__(
27
+ self,
28
+ in_features,
29
+ hidden_features=None,
30
+ out_features=None,
31
+ activation=F.gelu,
32
+ bias1=True,
33
+ bias2=True,
34
+ return_residual=False,
35
+ device=None,
36
+ dtype=None,
37
+ ):
38
+ factory_kwargs = {"device": device, "dtype": dtype}
39
+ super().__init__()
40
+ out_features = out_features if out_features is not None else in_features
41
+ hidden_features = hidden_features if hidden_features is not None else in_features * 4
42
+ self.return_residual = return_residual
43
+ self.fc1 = nn.Linear(in_features, hidden_features, bias=bias1, **factory_kwargs)
44
+ self.activation = activation
45
+ self.fc2 = nn.Linear(hidden_features, out_features, bias=bias2, **factory_kwargs)
46
+
47
+ def forward(self, x):
48
+ y = self.fc1(x)
49
+ y = self.activation(y)
50
+ y = self.fc2(y)
51
+ return y if not self.return_residual else (y, x)
52
+
53
+
54
+ class ParallelMLP(nn.Module):
55
+ def __init__(
56
+ self,
57
+ in_features,
58
+ hidden_features=None,
59
+ out_features=None,
60
+ activation=F.gelu,
61
+ process_group: ProcessGroup = None,
62
+ sequence_parallel=True,
63
+ bias1=True,
64
+ bias2=True,
65
+ device=None,
66
+ dtype=None,
67
+ ):
68
+ factory_kwargs = {"device": device, "dtype": dtype}
69
+ super().__init__()
70
+ assert ColumnParallelLinear is not None, "Need to install fused_dense"
71
+ assert RowParallelLinear is not None, "Need to install fused_dense"
72
+ out_features = out_features if out_features is not None else in_features
73
+ hidden_features = hidden_features if hidden_features is not None else in_features * 4
74
+ self.fc1 = ColumnParallelLinear(
75
+ in_features,
76
+ hidden_features,
77
+ process_group,
78
+ bias=bias1,
79
+ sequence_parallel=sequence_parallel,
80
+ **factory_kwargs,
81
+ )
82
+ self.activation = activation
83
+ self.fc2 = RowParallelLinear(
84
+ hidden_features,
85
+ out_features,
86
+ process_group,
87
+ bias=bias2,
88
+ sequence_parallel=sequence_parallel,
89
+ **factory_kwargs,
90
+ )
91
+
92
+ def forward(self, x):
93
+ y = self.fc1(x)
94
+ y = self.activation(y)
95
+ y = self.fc2(y)
96
+ return y
97
+
98
+
99
+ class GatedMlp(nn.Module):
100
+ def __init__(
101
+ self,
102
+ in_features,
103
+ hidden_features=None,
104
+ out_features=None,
105
+ activation=F.sigmoid,
106
+ bias1=True,
107
+ bias2=True,
108
+ multiple_of=128,
109
+ return_residual=False,
110
+ device=None,
111
+ dtype=None,
112
+ ):
113
+ factory_kwargs = {"device": device, "dtype": dtype}
114
+ super().__init__()
115
+ out_features = out_features if out_features is not None else in_features
116
+ hidden_features = (
117
+ hidden_features if hidden_features is not None else int(8 * in_features / 3)
118
+ )
119
+ hidden_features = (hidden_features + multiple_of - 1) // multiple_of * multiple_of
120
+ self.return_residual = return_residual
121
+ self.fc1 = nn.Linear(in_features, 2 * hidden_features, bias=bias1, **factory_kwargs)
122
+ self.activation = activation
123
+ self.fc2 = nn.Linear(hidden_features, out_features, bias=bias2, **factory_kwargs)
124
+
125
+ def forward(self, x):
126
+ y = self.fc1(x)
127
+ if self.activation == F.sigmoid: # Special case for GLU
128
+ y = F.glu(y, dim=-1)
129
+ elif self.activation == F.silu and swiglu is not None: # Special case for SwiGLU
130
+ y, gate = y.chunk(2, dim=-1)
131
+ y = swiglu(gate, y)
132
+ else:
133
+ y, gate = y.chunk(2, dim=-1)
134
+ y = y * self.activation(gate)
135
+ y = self.fc2(y)
136
+ return y if not self.return_residual else (y, x)
137
+
138
+
139
+ class ParallelGatedMlp(nn.Module):
140
+ """Parallel GatedMlp"""
141
+
142
+ def __init__(
143
+ self,
144
+ in_features,
145
+ process_group,
146
+ hidden_features=None,
147
+ out_features=None,
148
+ activation=F.sigmoid,
149
+ bias1=True,
150
+ bias2=True,
151
+ multiple_of=128,
152
+ sequence_parallel=True,
153
+ device=None,
154
+ dtype=None,
155
+ ):
156
+ factory_kwargs = {"device": device, "dtype": dtype}
157
+ super().__init__()
158
+ out_features = out_features if out_features is not None else in_features
159
+ hidden_features = (
160
+ hidden_features if hidden_features is not None else int(8 * in_features / 3)
161
+ )
162
+ hidden_features = (hidden_features + multiple_of - 1) // multiple_of * multiple_of
163
+ if ColumnParallelLinear is None or RowParallelLinear is None:
164
+ raise ImportError("fused_dense is not installed")
165
+ self.fc1 = ColumnParallelLinear(
166
+ in_features,
167
+ 2 * hidden_features,
168
+ process_group,
169
+ bias=bias1,
170
+ sequence_parallel=sequence_parallel,
171
+ **factory_kwargs,
172
+ )
173
+ self.activation = activation
174
+ self.fc2 = RowParallelLinear(
175
+ hidden_features,
176
+ out_features,
177
+ process_group,
178
+ bias=bias2,
179
+ sequence_parallel=sequence_parallel,
180
+ **factory_kwargs,
181
+ )
182
+
183
+ def forward(self, x):
184
+ y = self.fc1(x)
185
+ if self.activation == F.sigmoid: # Special case for GLU
186
+ y = F.glu(y, dim=-1)
187
+ else:
188
+ y, gate = y.chunk(2, dim=-1)
189
+ y = y * self.activation(gate)
190
+ y = self.fc2(y)
191
+ return y