hunterFormsBS 0.2.0__tar.gz → 0.3.1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {hunterformsbs-0.2.0 → hunterformsbs-0.3.1}/PKG-INFO +23 -13
- {hunterformsbs-0.2.0 → hunterformsbs-0.3.1}/README.md +19 -8
- {hunterformsbs-0.2.0 → hunterformsbs-0.3.1}/pyproject.toml +17 -10
- {hunterformsbs-0.2.0 → hunterformsbs-0.3.1}/src/hunterFormsBS/__init__.py +9 -3
- {hunterformsbs-0.2.0 → hunterformsbs-0.3.1}/src/hunterFormsBS/attend.py +23 -301
- {hunterformsbs-0.2.0 → hunterformsbs-0.3.1}/src/hunterFormsBS/bandSplitRotator.py +112 -139
- hunterformsbs-0.3.1/src/hunterFormsBS/bands.py +118 -0
- {hunterformsbs-0.2.0 → hunterformsbs-0.3.1}/src/hunterFormsBS/hyperACE.py +4 -4
- hunterformsbs-0.3.1/src/hunterFormsBS/loss.py +157 -0
- hunterformsbs-0.2.0/src/hunterFormsBS/bandSplit.py → hunterformsbs-0.3.1/src/hunterFormsBS/mask.py +9 -231
- {hunterformsbs-0.2.0 → hunterformsbs-0.3.1}/src/hunterFormsBS/theTypes.py +11 -6
- hunterformsbs-0.3.1/src/hunterFormsBS/transform.py +305 -0
- {hunterformsbs-0.2.0 → hunterformsbs-0.3.1}/LICENSE +0 -0
- {hunterformsbs-0.2.0 → hunterformsbs-0.3.1}/LICENSE-Solovyev +0 -0
- {hunterformsbs-0.2.0 → hunterformsbs-0.3.1}/LICENSE-Wang +0 -0
- {hunterformsbs-0.2.0 → hunterformsbs-0.3.1}/src/hunterFormsBS/py.typed +0 -0
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: hunterFormsBS
|
|
3
|
-
Version: 0.
|
|
4
|
-
Summary:
|
|
3
|
+
Version: 0.3.1
|
|
4
|
+
Summary: Universal frequency-band splitter for music source separation. The BandSplitRoformer module supports BSRoformer, MelBandRoformer, and custom overlapping or non-overlapping band configurations. Fully typed, modular, and documented, including migration help, usage, and paper references. PyTorch; CUDA-accelerated.
|
|
5
5
|
Keywords: artificial intelligence,audio source separation,machine learning,music source separation
|
|
6
6
|
Author: Hunter Hogan
|
|
7
7
|
Author-email: Hunter Hogan <HunterHogan@pm.me>
|
|
@@ -16,11 +16,10 @@ Classifier: Intended Audience :: Developers
|
|
|
16
16
|
Classifier: Intended Audience :: Science/Research
|
|
17
17
|
Classifier: Natural Language :: English
|
|
18
18
|
Classifier: Operating System :: OS Independent
|
|
19
|
-
Classifier: Programming Language :: Python :: 3.
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
20
20
|
Classifier: Programming Language :: Python :: 3.14
|
|
21
21
|
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
|
|
22
22
|
Classifier: Typing :: Typed
|
|
23
|
-
Requires-Dist: z0z-tools
|
|
24
23
|
Requires-Dist: einops>=0.8.2
|
|
25
24
|
Requires-Dist: huntermakespy
|
|
26
25
|
Requires-Dist: more-itertools
|
|
@@ -33,7 +32,7 @@ Requires-Dist: torch-einops-kit
|
|
|
33
32
|
Requires-Dist: torchaudio>=2.0.0
|
|
34
33
|
Maintainer: Hunter Hogan
|
|
35
34
|
Maintainer-email: Hunter Hogan <HunterHogan@pm.me>
|
|
36
|
-
Requires-Python: >=3.
|
|
35
|
+
Requires-Python: >=3.10
|
|
37
36
|
Project-URL: Context7, https://context7.com/hunterhogan/hunterformsbs
|
|
38
37
|
Project-URL: Donate, https://www.patreon.com/integrated
|
|
39
38
|
Project-URL: Download, https://pypi.org/project/hunterFormsBS
|
|
@@ -81,7 +80,7 @@ The key design idea is that the difference between the BS-style front end and th
|
|
|
81
80
|
is treated as a band-layout problem, not as a reason to maintain two unrelated model families.
|
|
82
81
|
|
|
83
82
|
- `hunterFormsBS.bandSplitRotator.BandSplitRotator` is the primary unified entry point and the only public separator class exported by this package.
|
|
84
|
-
- `hunterFormsBS.
|
|
83
|
+
- `hunterFormsBS.bands.BandSplit`, `hunterFormsBS.mask.MaskEstimator`, `hunterFormsBS.mask.MLP`, `hunterFormsBS.loss.lossComputation`, `hunterFormsBS.attend.Attention`, and `hunterFormsBS.transform.Transformer` hold the reusable typed building blocks shared across all band-layout modes.
|
|
85
84
|
- `hunterFormsBS.hyperACE.SegmModel` supplies the optional segmentation-style mask-estimation branch used when `use_hyperACE=True`.
|
|
86
85
|
- Attention and transformer options such as `attn_dropout`, `ff_dropout`, `flash_attn`, `sage_attention`, and `scale` keep the same identifiers as they move from model constructors into downstream blocks.
|
|
87
86
|
- All user-configurable separator settings live on `BandSplitRotator`, including optional HyperACE branch settings under the `segm_*` prefix.
|
|
@@ -168,14 +167,21 @@ prefix.
|
|
|
168
167
|
- Purpose: unified separator that can build BS-style, mel-style, or custom band layouts from one
|
|
169
168
|
model family, with downstream attention, transformer, STFT, mask-estimator, optional
|
|
170
169
|
segmentation-branch, and loss options on the constructor.
|
|
171
|
-
- `hunterFormsBS.
|
|
172
|
-
- Main symbols: `BandSplit`, `
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
170
|
+
- `hunterFormsBS.bands`
|
|
171
|
+
- Main symbols: `BandSplit`, `DEFAULT_FREQS_PER_BANDS`
|
|
172
|
+
- Purpose: band projection layer and the BS-style default frequency-bin partition.
|
|
173
|
+
- `hunterFormsBS.loss`
|
|
174
|
+
- Main symbols: `lossComputation`
|
|
175
|
+
- Purpose: multi-resolution STFT training-loss helper.
|
|
176
|
+
- `hunterFormsBS.mask`
|
|
177
|
+
- Main symbols: `MaskEstimator`, `MLP`
|
|
178
|
+
- Purpose: mask-estimation heads and band-local affine blocks.
|
|
176
179
|
- `hunterFormsBS.attend`
|
|
177
|
-
- Main symbols: `Attend`, `Attention
|
|
178
|
-
- Purpose: shared attention
|
|
180
|
+
- Main symbols: `Attend`, `Attention`
|
|
181
|
+
- Purpose: shared attention core with RoPE / PoPE, PyTorch SDPA, and optional SageAttention support.
|
|
182
|
+
- `hunterFormsBS.transform`
|
|
183
|
+
- Main symbols: `FeedForward`, `Transformer`
|
|
184
|
+
- Purpose: position-wise feedforward block and stacked attention-and-feedforward transformer.
|
|
179
185
|
- `hunterFormsBS.hyperACE`
|
|
180
186
|
- Main symbols: `SegmModel`, `HyperACE`, `Backbone`, `Decoder`, `ProgressiveUpsampleHead`
|
|
181
187
|
- Purpose: optional segmentation-style mask-estimation branch and its reusable building blocks.
|
|
@@ -243,7 +249,11 @@ comparisons with upstream repos stay easy to read.
|
|
|
243
249
|
- [BibTeX citation.](https://github.com/hunterhogan/hunterFormsBS/blob/main/citations/SU2024127063.bib) [TeX Source with precise formulas for AI agents.](https://arxiv.org/src/2104.09864)
|
|
244
250
|
- DOI: [10.1016/j.neucom.2023.127063](https://doi.org/10.1016/j.neucom.2023.127063)
|
|
245
251
|
- Free pre-print: [arXiv:2104.09864](https://arxiv.org/abs/2104.09864)
|
|
252
|
+
- Original blog about the idea: [https://kexue.fm/archives/8265](https://kexue.fm/archives/8265)
|
|
253
|
+
- An intuitive explanation: [https://blog.eleuther.ai/rotary-embeddings/](https://blog.eleuther.ai/rotary-embeddings/)
|
|
246
254
|
- Implementations:
|
|
255
|
+
- [ZhuiyiTechnology/roformer](https://github.com/ZhuiyiTechnology/roformer)
|
|
256
|
+
- [ZhuiyiTechnology/roformer-v2](https://github.com/ZhuiyiTechnology/roformer-v2)
|
|
247
257
|
- [lucidrains/rotary-embedding-torch](https://github.com/lucidrains/rotary-embedding-torch)
|
|
248
258
|
- [context7](https://context7.com/lucidrains/rotary-embedding-torch)
|
|
249
259
|
- [hunterhogan/rotary-embedding-torch](https://github.com/hunterhogan/rotary-embedding-torch)
|
|
@@ -37,7 +37,7 @@ The key design idea is that the difference between the BS-style front end and th
|
|
|
37
37
|
is treated as a band-layout problem, not as a reason to maintain two unrelated model families.
|
|
38
38
|
|
|
39
39
|
- `hunterFormsBS.bandSplitRotator.BandSplitRotator` is the primary unified entry point and the only public separator class exported by this package.
|
|
40
|
-
- `hunterFormsBS.
|
|
40
|
+
- `hunterFormsBS.bands.BandSplit`, `hunterFormsBS.mask.MaskEstimator`, `hunterFormsBS.mask.MLP`, `hunterFormsBS.loss.lossComputation`, `hunterFormsBS.attend.Attention`, and `hunterFormsBS.transform.Transformer` hold the reusable typed building blocks shared across all band-layout modes.
|
|
41
41
|
- `hunterFormsBS.hyperACE.SegmModel` supplies the optional segmentation-style mask-estimation branch used when `use_hyperACE=True`.
|
|
42
42
|
- Attention and transformer options such as `attn_dropout`, `ff_dropout`, `flash_attn`, `sage_attention`, and `scale` keep the same identifiers as they move from model constructors into downstream blocks.
|
|
43
43
|
- All user-configurable separator settings live on `BandSplitRotator`, including optional HyperACE branch settings under the `segm_*` prefix.
|
|
@@ -124,14 +124,21 @@ prefix.
|
|
|
124
124
|
- Purpose: unified separator that can build BS-style, mel-style, or custom band layouts from one
|
|
125
125
|
model family, with downstream attention, transformer, STFT, mask-estimator, optional
|
|
126
126
|
segmentation-branch, and loss options on the constructor.
|
|
127
|
-
- `hunterFormsBS.
|
|
128
|
-
- Main symbols: `BandSplit`, `
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
127
|
+
- `hunterFormsBS.bands`
|
|
128
|
+
- Main symbols: `BandSplit`, `DEFAULT_FREQS_PER_BANDS`
|
|
129
|
+
- Purpose: band projection layer and the BS-style default frequency-bin partition.
|
|
130
|
+
- `hunterFormsBS.loss`
|
|
131
|
+
- Main symbols: `lossComputation`
|
|
132
|
+
- Purpose: multi-resolution STFT training-loss helper.
|
|
133
|
+
- `hunterFormsBS.mask`
|
|
134
|
+
- Main symbols: `MaskEstimator`, `MLP`
|
|
135
|
+
- Purpose: mask-estimation heads and band-local affine blocks.
|
|
132
136
|
- `hunterFormsBS.attend`
|
|
133
|
-
- Main symbols: `Attend`, `Attention
|
|
134
|
-
- Purpose: shared attention
|
|
137
|
+
- Main symbols: `Attend`, `Attention`
|
|
138
|
+
- Purpose: shared attention core with RoPE / PoPE, PyTorch SDPA, and optional SageAttention support.
|
|
139
|
+
- `hunterFormsBS.transform`
|
|
140
|
+
- Main symbols: `FeedForward`, `Transformer`
|
|
141
|
+
- Purpose: position-wise feedforward block and stacked attention-and-feedforward transformer.
|
|
135
142
|
- `hunterFormsBS.hyperACE`
|
|
136
143
|
- Main symbols: `SegmModel`, `HyperACE`, `Backbone`, `Decoder`, `ProgressiveUpsampleHead`
|
|
137
144
|
- Purpose: optional segmentation-style mask-estimation branch and its reusable building blocks.
|
|
@@ -199,7 +206,11 @@ comparisons with upstream repos stay easy to read.
|
|
|
199
206
|
- [BibTeX citation.](https://github.com/hunterhogan/hunterFormsBS/blob/main/citations/SU2024127063.bib) [TeX Source with precise formulas for AI agents.](https://arxiv.org/src/2104.09864)
|
|
200
207
|
- DOI: [10.1016/j.neucom.2023.127063](https://doi.org/10.1016/j.neucom.2023.127063)
|
|
201
208
|
- Free pre-print: [arXiv:2104.09864](https://arxiv.org/abs/2104.09864)
|
|
209
|
+
- Original blog about the idea: [https://kexue.fm/archives/8265](https://kexue.fm/archives/8265)
|
|
210
|
+
- An intuitive explanation: [https://blog.eleuther.ai/rotary-embeddings/](https://blog.eleuther.ai/rotary-embeddings/)
|
|
202
211
|
- Implementations:
|
|
212
|
+
- [ZhuiyiTechnology/roformer](https://github.com/ZhuiyiTechnology/roformer)
|
|
213
|
+
- [ZhuiyiTechnology/roformer-v2](https://github.com/ZhuiyiTechnology/roformer-v2)
|
|
203
214
|
- [lucidrains/rotary-embedding-torch](https://github.com/lucidrains/rotary-embedding-torch)
|
|
204
215
|
- [context7](https://context7.com/lucidrains/rotary-embedding-torch)
|
|
205
216
|
- [hunterhogan/rotary-embedding-torch](https://github.com/hunterhogan/rotary-embedding-torch)
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
[project]
|
|
2
2
|
name = "hunterFormsBS"
|
|
3
|
-
version = "0.
|
|
4
|
-
description = "
|
|
3
|
+
version = "0.3.1"
|
|
4
|
+
description = "Universal frequency-band splitter for music source separation. The BandSplitRoformer module supports BSRoformer, MelBandRoformer, and custom overlapping or non-overlapping band configurations. Fully typed, modular, and documented, including migration help, usage, and paper references. PyTorch; CUDA-accelerated."
|
|
5
5
|
readme = "README.md"
|
|
6
|
-
requires-python = ">=3.
|
|
6
|
+
requires-python = ">=3.10"
|
|
7
7
|
license = "CC-BY-NC-4.0"
|
|
8
8
|
license-files = ["LICEN[CS]E*"]
|
|
9
9
|
authors = [{ name = "Hunter Hogan", email = "HunterHogan@pm.me" }]
|
|
@@ -22,13 +22,12 @@ classifiers = [
|
|
|
22
22
|
"Intended Audience :: Science/Research",
|
|
23
23
|
"Natural Language :: English",
|
|
24
24
|
"Operating System :: OS Independent",
|
|
25
|
-
"Programming Language :: Python :: 3.
|
|
25
|
+
"Programming Language :: Python :: 3.10",
|
|
26
26
|
"Programming Language :: Python :: 3.14",
|
|
27
27
|
"Topic :: Scientific/Engineering :: Artificial Intelligence",
|
|
28
28
|
"Typing :: Typed",
|
|
29
29
|
]
|
|
30
30
|
dependencies = [
|
|
31
|
-
"Z0Z_tools",
|
|
32
31
|
"einops>=0.8.2",
|
|
33
32
|
"hunterMakesPy",
|
|
34
33
|
"more-itertools",
|
|
@@ -50,20 +49,30 @@ Issues = "https://github.com/hunterhogan/hunterFormsBS/issues"
|
|
|
50
49
|
Repository = "https://github.com/hunterhogan/hunterFormsBS.git"
|
|
51
50
|
|
|
52
51
|
[dependency-groups]
|
|
53
|
-
|
|
54
|
-
|
|
52
|
+
dev = ["pytest-cov"]
|
|
53
|
+
pytorch = [
|
|
54
|
+
"torch==2.10.0;sys_platform == 'win32'",
|
|
55
|
+
"torchaudio==2.10.0;sys_platform == 'win32'"
|
|
56
|
+
]
|
|
57
|
+
testing = ["hunterHearsPy>=1.0.3", "pytest"]
|
|
55
58
|
|
|
56
59
|
[build-system]
|
|
57
60
|
requires = ["uv_build>=0.11.8,<0.12.0"]
|
|
58
61
|
build-backend = "uv_build"
|
|
59
62
|
|
|
63
|
+
[tool.coverage]
|
|
64
|
+
report = { exclude_lines = ["if TYPE_CHECKING:", "if __name__ == \"__main__\":"], ignore_errors = false }
|
|
65
|
+
run = { branch = true, concurrency = ["multiprocessing"], data_file = "tests/coverage/.coverage", omit = [
|
|
66
|
+
"tests/*"
|
|
67
|
+
], parallel = true, source = ["."] }
|
|
68
|
+
|
|
60
69
|
[tool.updateCitation]
|
|
61
70
|
filenameCitationDOTcff = "CITATION.cff"
|
|
62
71
|
pathFilenameCitationSSOT = "citations/CITATION.cff"
|
|
63
72
|
|
|
64
73
|
[tool.uv]
|
|
65
74
|
build-backend = { module-name = "hunterFormsBS", module-root = "src" }
|
|
66
|
-
default-groups = ["testing"]
|
|
75
|
+
default-groups = ["dev", "testing"]
|
|
67
76
|
|
|
68
77
|
[[tool.uv.index]]
|
|
69
78
|
explicit = true
|
|
@@ -71,7 +80,5 @@ name = "indexPytorch"
|
|
|
71
80
|
url = "https://download.pytorch.org/whl/cu128"
|
|
72
81
|
|
|
73
82
|
[tool.uv.sources]
|
|
74
|
-
pope-pytorch = { git = "https://github.com/hunterhogan/PoPE-pytorch.git" }
|
|
75
|
-
rotary-embedding-torch = { git = "https://github.com/hunterhogan/rotary-embedding-torch.git" }
|
|
76
83
|
torch = { index = "indexPytorch", group = "pytorch" }
|
|
77
84
|
torchaudio = { index = "indexPytorch", group = "pytorch" }
|
|
@@ -8,15 +8,21 @@ modules through `hunterFormsBS.*` submodule imports.
|
|
|
8
8
|
Modules
|
|
9
9
|
-------
|
|
10
10
|
attend
|
|
11
|
-
Attention layers
|
|
12
|
-
|
|
13
|
-
Band
|
|
11
|
+
Attention layers for separator models.
|
|
12
|
+
bands
|
|
13
|
+
Band projection layer and the BS-style default frequency-bin partition.
|
|
14
14
|
bandSplitRotator
|
|
15
15
|
Primary band-split separator model exposed by the package.
|
|
16
16
|
hyperACE
|
|
17
17
|
Optional segmentation-style mask-estimation branch and supporting building blocks.
|
|
18
|
+
loss
|
|
19
|
+
Multi-resolution STFT training-loss computation utilities.
|
|
20
|
+
mask
|
|
21
|
+
Mask estimation heads and band-local affine blocks.
|
|
18
22
|
theTypes
|
|
19
23
|
Typed configuration records for attention, mask estimation, STFT, transformer, and loss setup.
|
|
24
|
+
transform
|
|
25
|
+
Position-wise feedforward block and stacked attention-and-feedforward transformer.
|
|
20
26
|
"""
|
|
21
27
|
|
|
22
28
|
from __future__ import annotations
|
|
@@ -1,15 +1,14 @@
|
|
|
1
1
|
# ruff: noqa: PLC0415
|
|
2
|
-
"""
|
|
2
|
+
"""Evaluate and project attention for music source separation.
|
|
3
3
|
|
|
4
|
-
You can use this module to assemble the
|
|
4
|
+
You can use this module to assemble the attention core used by
|
|
5
5
|
`hunterFormsBS.bandSplitRotator.BandSplitRotator` [1]. The constructor parameter names stay aligned
|
|
6
6
|
across the stack so outer model classes can pass settings such as `attn_dropout`, `flash_attn`,
|
|
7
7
|
`sage_attention`, and `scale` without renaming fields. `Attend` evaluates attention from
|
|
8
8
|
precomputed query, key, and value arrays and can dispatch to an explicit implementation, to
|
|
9
9
|
PyTorch SDPA [2], or to SageAttention [3][4]. `Attention` projects activations into query, key,
|
|
10
|
-
and value arrays, applies RoPE [5] or PoPE [6], and then calls `Attend`.
|
|
11
|
-
|
|
12
|
-
stacks repeated `Attention` and `FeedForward` pairs.
|
|
10
|
+
and value arrays, applies RoPE [5] or PoPE [6], and then calls `Attend`. The feedforward and
|
|
11
|
+
transformer residual blocks now live in `hunterFormsBS.transform` [7].
|
|
13
12
|
|
|
14
13
|
Contents
|
|
15
14
|
--------
|
|
@@ -18,10 +17,6 @@ Classes
|
|
|
18
17
|
Evaluate attention output from precomputed query, key, and value arrays.
|
|
19
18
|
Attention
|
|
20
19
|
Project activations into multi-head query, key, and value arrays and return gated output.
|
|
21
|
-
FeedForward
|
|
22
|
-
Apply a position-wise expansion-and-projection nonlinear block.
|
|
23
|
-
Transformer
|
|
24
|
-
Stack repeated `Attention` and `FeedForward` pairs into a residual sequence.
|
|
25
20
|
|
|
26
21
|
References
|
|
27
22
|
----------
|
|
@@ -39,17 +34,16 @@ References
|
|
|
39
34
|
[6] Gopalakrishnan, A., Csordás, R., Schmidhuber, J., and Mozer, M. C. (2025).
|
|
40
35
|
Decoupling the "What" and "Where" With Polar Coordinate Positional Embeddings.
|
|
41
36
|
https://arxiv.org/abs/2509.10534
|
|
37
|
+
[7] `hunterFormsBS.transform`
|
|
42
38
|
"""
|
|
43
39
|
from __future__ import annotations
|
|
44
40
|
|
|
45
41
|
from einops import rearrange
|
|
46
|
-
from hunterFormsBS.theTypes import FlashAttentionConfig
|
|
47
|
-
from more_itertools import loops
|
|
42
|
+
from hunterFormsBS.theTypes import FlashAttentionConfig
|
|
48
43
|
from operator import neg
|
|
49
44
|
from packaging import version
|
|
50
45
|
from PoPE_pytorch import flash_attn_with_pope, PoPE
|
|
51
46
|
from torch import einsum, nn, Tensor
|
|
52
|
-
from torch.nn import Module, ModuleList
|
|
53
47
|
from torch_einops_kit import exists, once
|
|
54
48
|
from torch_einops_kit.scaleValues import RMSNorm
|
|
55
49
|
from typing import Any, cast, TYPE_CHECKING
|
|
@@ -59,6 +53,7 @@ import torch.nn.functional as F
|
|
|
59
53
|
if TYPE_CHECKING:
|
|
60
54
|
from collections.abc import Callable
|
|
61
55
|
from rotary_embedding_torch import RotaryEmbedding
|
|
56
|
+
from torch._C import _CudaDeviceProperties
|
|
62
57
|
|
|
63
58
|
print_once: Callable[..., Any] = once(print)
|
|
64
59
|
|
|
@@ -169,9 +164,10 @@ class Attend(nn.Module):
|
|
|
169
164
|
if not torch.cuda.is_available() or not self.flash:
|
|
170
165
|
return
|
|
171
166
|
|
|
172
|
-
device_properties = torch.cuda.get_device_properties(torch.device('cuda')) # pyright: ignore[reportUnknownVariableType, reportUnknownMemberType]
|
|
167
|
+
device_properties: _CudaDeviceProperties = torch.cuda.get_device_properties(torch.device('cuda')) # pyright: ignore[reportUnknownVariableType, reportUnknownMemberType]
|
|
168
|
+
device_properties = cast('_CudaDeviceProperties', device_properties) # ty:ignore[redundant-cast]
|
|
173
169
|
|
|
174
|
-
if device_properties.major == 8 and device_properties.minor == 0:
|
|
170
|
+
if device_properties.major == 8 and device_properties.minor == 0:
|
|
175
171
|
print_once('A100 GPU detected, using flash attention if input tensor is on cuda')
|
|
176
172
|
self.cuda_config = FlashAttentionConfig(enable_flash=True, enable_math=False, enable_mem_efficient=False)
|
|
177
173
|
else:
|
|
@@ -255,7 +251,6 @@ class Attend(nn.Module):
|
|
|
255
251
|
if is_cuda and exists(self.cuda_config):
|
|
256
252
|
config = self.cuda_config
|
|
257
253
|
|
|
258
|
-
# pytorch 2.0 flash attn: q, k, v, mask, attn_dropout, softmax_scale
|
|
259
254
|
with torch.backends.cuda.sdp_kernel(**config._asdict()): # pyright: ignore[reportDeprecated]
|
|
260
255
|
out: Tensor = F.scaled_dot_product_attention(q, k, v, dropout_p=self.attn_dropout if self.training else 0.0)
|
|
261
256
|
|
|
@@ -335,7 +330,6 @@ class Attend(nn.Module):
|
|
|
335
330
|
attention_weights = self.nn_Dropout(attention_weights)
|
|
336
331
|
|
|
337
332
|
# aggregate values
|
|
338
|
-
|
|
339
333
|
return einsum('b h i j, b h j d -> b h i d', attention_weights, v)
|
|
340
334
|
|
|
341
335
|
class Attention(nn.Module):
|
|
@@ -392,23 +386,23 @@ class Attention(nn.Module):
|
|
|
392
386
|
Transformer with Rotary Position Embedding. https://arxiv.org/abs/2104.09864
|
|
393
387
|
[4] `Attend`
|
|
394
388
|
|
|
395
|
-
[5] `Transformer`
|
|
389
|
+
[5] `hunterFormsBS.transform.Transformer`
|
|
396
390
|
|
|
397
391
|
[6] PyTorch.
|
|
398
392
|
https://context7.com/pytorch/pytorch
|
|
399
393
|
"""
|
|
400
394
|
def __init__(
|
|
401
|
-
self
|
|
402
|
-
dim: int
|
|
403
|
-
dim_head: int = 64
|
|
404
|
-
attn_dropout: float = 0.0
|
|
405
|
-
heads: int = 8
|
|
406
|
-
pope_embed: PoPE | None = None
|
|
407
|
-
rotary_embed: RotaryEmbedding | None = None
|
|
408
|
-
scale: float | None = None
|
|
409
|
-
|
|
410
|
-
flash: bool = True
|
|
411
|
-
sage_attention: bool = False,
|
|
395
|
+
self
|
|
396
|
+
, dim: int
|
|
397
|
+
, dim_head: int = 64
|
|
398
|
+
, attn_dropout: float = 0.0
|
|
399
|
+
, heads: int = 8
|
|
400
|
+
, pope_embed: PoPE | None = None
|
|
401
|
+
, rotary_embed: RotaryEmbedding | None = None
|
|
402
|
+
, scale: float | None = None
|
|
403
|
+
, *
|
|
404
|
+
, flash: bool = True
|
|
405
|
+
, sage_attention: bool = False,
|
|
412
406
|
) -> None:
|
|
413
407
|
"""Set up an attention block for a chosen width and head layout.
|
|
414
408
|
|
|
@@ -550,7 +544,7 @@ class Attention(nn.Module):
|
|
|
550
544
|
https://arxiv.org/abs/2104.09864
|
|
551
545
|
[4] `Attend.forward`
|
|
552
546
|
|
|
553
|
-
[5] `Transformer`
|
|
547
|
+
[5] `hunterFormsBS.transform.Transformer`
|
|
554
548
|
|
|
555
549
|
[6] Vaswani, A., Shazeer, N., Parmar, N., Uszkoreit, J., Jones, L.,
|
|
556
550
|
Gomez, A. N., Kaiser, Ł., and Polosukhin, I. (2017). Attention Is All You Need.
|
|
@@ -569,280 +563,8 @@ class Attention(nn.Module):
|
|
|
569
563
|
else:
|
|
570
564
|
out = self.attend(q, k, v)
|
|
571
565
|
|
|
572
|
-
# after attend
|
|
573
566
|
gates: Tensor = self.to_gates(x)
|
|
574
567
|
out *= rearrange(gates, 'b n h -> b h n 1').sigmoid()
|
|
575
568
|
|
|
576
569
|
out = rearrange(out, 'b h n d -> b n (h d)')
|
|
577
570
|
return self.to_out(out)
|
|
578
|
-
|
|
579
|
-
class FeedForward(Module):
|
|
580
|
-
"""Transform activations with a position-wise expansion-and-projection block.
|
|
581
|
-
|
|
582
|
-
You can use `FeedForward` after `Attention` [1] inside `Transformer` [2] when activation `Tensor`
|
|
583
|
-
`x` at each sequence position should pass through the same nonlinear feature transform.
|
|
584
|
-
`FeedForward` preserves the batch axis, sequence axis, and feature axis of activation `Tensor` `x`
|
|
585
|
-
while expanding the feature width internally before projecting back to the original width. The
|
|
586
|
-
shared `BandSplitRotator` stack uses one `FeedForward` block after every attention block [3].
|
|
587
|
-
|
|
588
|
-
Attributes
|
|
589
|
-
----------
|
|
590
|
-
net : nn.Sequential
|
|
591
|
-
Position-wise submodule sequence containing root-mean-square normalization, width expansion,
|
|
592
|
-
`nn.GELU`, `ff_dropout`, width projection, and output `ff_dropout` [4].
|
|
593
|
-
|
|
594
|
-
References
|
|
595
|
-
----------
|
|
596
|
-
[1] `Attention`
|
|
597
|
-
|
|
598
|
-
[2] `Transformer`
|
|
599
|
-
|
|
600
|
-
[3] `hunterFormsBS.bandSplitRotator.BandSplitRotator`
|
|
601
|
-
|
|
602
|
-
[4] PyTorch.
|
|
603
|
-
https://context7.com/pytorch/pytorch
|
|
604
|
-
"""
|
|
605
|
-
def __init__(self, dim: int, ff_mult: float | None = 4.0, ff_dropout: float = 0.0) -> None:
|
|
606
|
-
"""Set up a position-wise feedforward block for feature width `dim`.
|
|
607
|
-
|
|
608
|
-
You can use `__init__` to choose the input and output feature width `dim`, the internal width
|
|
609
|
-
expansion factor `ff_mult`, and the `ff_dropout` probability for the shared feedforward
|
|
610
|
-
sublayer inside `Transformer` [1]. `__init__` stores the resulting normalization, linear,
|
|
611
|
-
activation, and `ff_dropout` layers in `net` for reuse by later calls to `forward`.
|
|
612
|
-
|
|
613
|
-
Parameters
|
|
614
|
-
----------
|
|
615
|
-
dim : int
|
|
616
|
-
Input feature width before expansion and output feature width after projection.
|
|
617
|
-
ff_mult : float | None = 4.0
|
|
618
|
-
Multiplicative factor used to compute the hidden feature width `int(dim * ff_mult)`. When
|
|
619
|
-
`ff_mult` is `None`, `__init__` uses `4.0`.
|
|
620
|
-
ff_dropout : float = 0.0
|
|
621
|
-
Dropout probability applied after the hidden activation and after the output projection.
|
|
622
|
-
|
|
623
|
-
PyTorch
|
|
624
|
-
-------
|
|
625
|
-
submodule construction : implementation detail
|
|
626
|
-
`__init__` computes `dim_inner = int(dim * ff_mult)` and stores `net` as a six-stage
|
|
627
|
-
`nn.Sequential` with root-mean-square normalization, `nn.Linear(dim, dim_inner)`,
|
|
628
|
-
`nn.GELU`, `nn.Dropout(ff_dropout)`, `nn.Linear(dim_inner, dim)`, and a second
|
|
629
|
-
`nn.Dropout(ff_dropout)` [2].
|
|
630
|
-
|
|
631
|
-
References
|
|
632
|
-
----------
|
|
633
|
-
[1] `Transformer`
|
|
634
|
-
|
|
635
|
-
[2] PyTorch.
|
|
636
|
-
https://context7.com/pytorch/pytorch
|
|
637
|
-
"""
|
|
638
|
-
super().__init__()
|
|
639
|
-
if ff_mult is None:
|
|
640
|
-
ff_mult = 4.0
|
|
641
|
-
dim_inner: int = int(dim * ff_mult)
|
|
642
|
-
self.net: nn.Sequential = nn.Sequential(
|
|
643
|
-
RMSNorm(dim), nn.Linear(dim, dim_inner), nn.GELU(), nn.Dropout(ff_dropout), nn.Linear(dim_inner, dim), nn.Dropout(ff_dropout)
|
|
644
|
-
)
|
|
645
|
-
|
|
646
|
-
def forward(self, x: Tensor) -> Tensor:
|
|
647
|
-
"""Transform activations `x` with the position-wise feedforward block.
|
|
648
|
-
|
|
649
|
-
You can use `forward` when `x` already has batch axis, sequence axis, and feature axis.
|
|
650
|
-
`forward` applies the same nonlinear feature transform to every position independently, reuses
|
|
651
|
-
the submodule sequence configured by `__init__`, and returns an activation `Tensor` with the
|
|
652
|
-
same outer shape as `x`. `forward` supplies the position-wise branch used by `Transformer`
|
|
653
|
-
[1].
|
|
654
|
-
|
|
655
|
-
Parameters
|
|
656
|
-
----------
|
|
657
|
-
x : Tensor
|
|
658
|
-
Input activation `Tensor` with shape `batch × sequence position × feature`.
|
|
659
|
-
|
|
660
|
-
Returns
|
|
661
|
-
-------
|
|
662
|
-
out : Tensor
|
|
663
|
-
Output activation `Tensor` with shape `batch × sequence position × feature`.
|
|
664
|
-
|
|
665
|
-
Shape Transformation
|
|
666
|
-
--------------------
|
|
667
|
-
position-wise mapping : implementation detail
|
|
668
|
-
`forward` preserves the outer shape `B×N×D`. `forward` applies the same feedforward map to
|
|
669
|
-
each position vector `x[b, n, :]` independently of every other position.
|
|
670
|
-
|
|
671
|
-
References
|
|
672
|
-
----------
|
|
673
|
-
[1] `Transformer`
|
|
674
|
-
"""
|
|
675
|
-
return self.net(x)
|
|
676
|
-
|
|
677
|
-
class Transformer(Module):
|
|
678
|
-
"""Refine activations with repeated attention-and-feedforward blocks.
|
|
679
|
-
|
|
680
|
-
You can use `Transformer` as the shared sequence stack inside `BandSplitRotator` [1].
|
|
681
|
-
`Transformer` takes activation `Tensor` `x`, repeats `depth` pairs of `Attention` [2] and
|
|
682
|
-
`FeedForward` [3], and optionally normalizes the result. `Transformer`
|
|
683
|
-
preserves the batch axis, sequence axis, and feature axis of activation `Tensor` `x`. The
|
|
684
|
-
constructor still accepts several compatibility parameters, but the stack itself no longer
|
|
685
|
-
contains `LinearAttention`.
|
|
686
|
-
|
|
687
|
-
Attributes
|
|
688
|
-
----------
|
|
689
|
-
layers : ModuleList
|
|
690
|
-
Sequence of per-depth `[attention, feedforward]` pairs, where the attention entry is
|
|
691
|
-
`Attention` [2] and the feedforward entry is `FeedForward` [3].
|
|
692
|
-
norm : RMSNorm | nn.Identity
|
|
693
|
-
Final output normalization module applied after the last residual block when `norm_output` is
|
|
694
|
-
`True`, or `nn.Identity` otherwise.
|
|
695
|
-
|
|
696
|
-
References
|
|
697
|
-
----------
|
|
698
|
-
[1] `hunterFormsBS.bandSplitRotator.BandSplitRotator`
|
|
699
|
-
|
|
700
|
-
[2] `Attention`
|
|
701
|
-
|
|
702
|
-
[3] `FeedForward`
|
|
703
|
-
"""
|
|
704
|
-
def __init__(
|
|
705
|
-
self,
|
|
706
|
-
*,
|
|
707
|
-
depth: int,
|
|
708
|
-
attn_dropout: float = 0.0,
|
|
709
|
-
dim_head: int = 64,
|
|
710
|
-
dim: int,
|
|
711
|
-
ff_dropout: float = 0.0,
|
|
712
|
-
ff_mult: float | None = 4,
|
|
713
|
-
flash_attn: bool = True,
|
|
714
|
-
heads: int = 8,
|
|
715
|
-
linear_attn: bool = False, # noqa: ARG002
|
|
716
|
-
norm_output: bool = True,
|
|
717
|
-
pope_embed: PoPE | None = None,
|
|
718
|
-
rotary_embed: RotaryEmbedding | None = None,
|
|
719
|
-
sage_attention: bool = False,
|
|
720
|
-
scale: float | None = None,
|
|
721
|
-
) -> None:
|
|
722
|
-
"""Set up a transformer stack for feature width `dim` and layer count `depth`.
|
|
723
|
-
|
|
724
|
-
You can use `__init__` to choose the stack depth, model width, attention-head layout, dropout
|
|
725
|
-
probabilities, attention backend, and position-encoding path for a
|
|
726
|
-
shared transformer block. `__init__` stores `depth` pairs of `Attention` [1] and `FeedForward`
|
|
727
|
-
[2] in `layers`, forwards the downstream attention parameters without renaming through
|
|
728
|
-
`ParametersAttention` [3], and configures optional output normalization in `norm`. `__init__`
|
|
729
|
-
retains `linear_attn` only for configuration compatibility with earlier versions.
|
|
730
|
-
|
|
731
|
-
Parameters
|
|
732
|
-
----------
|
|
733
|
-
attn_dropout : float = 0.0
|
|
734
|
-
Dropout probability passed to each attention sublayer.
|
|
735
|
-
depth : int
|
|
736
|
-
Number of repeated attention-plus-feedforward residual blocks.
|
|
737
|
-
dim_head : int = 64
|
|
738
|
-
Feature width of each attention head.
|
|
739
|
-
dim : int
|
|
740
|
-
Input and output feature width of the stack.
|
|
741
|
-
ff_dropout : float = 0.0
|
|
742
|
-
Dropout probability passed to each `FeedForward` block [2].
|
|
743
|
-
ff_mult : float | None = 4
|
|
744
|
-
Hidden-width expansion factor passed to each `FeedForward` block [2].
|
|
745
|
-
flash_attn : bool = True
|
|
746
|
-
Whether each attention sublayer may use PyTorch SDPA (scaled dot-product attention)
|
|
747
|
-
backends [4].
|
|
748
|
-
heads : int = 8
|
|
749
|
-
Number of attention heads in each attention sublayer.
|
|
750
|
-
linear_attn : bool = False
|
|
751
|
-
Vestigial flag retained for configuration compatibility passthrough. This unified implementation
|
|
752
|
-
ignores `linear_attn` because `LinearAttention` was removed.
|
|
753
|
-
norm_output : bool = True
|
|
754
|
-
Whether to apply final output normalization after the last residual block.
|
|
755
|
-
pope_embed : PoPE | None = None
|
|
756
|
-
Optional polar-coordinate position encoder passed to each `Attention` [1] [5][6]. When
|
|
757
|
-
both positional encoders are present, `Attention.forward` prefers `pope_embed`.
|
|
758
|
-
rotary_embed : RotaryEmbedding | None = None
|
|
759
|
-
Optional rotary positional encoder passed to each `Attention` [1] [7].
|
|
760
|
-
sage_attention : bool = False
|
|
761
|
-
Whether each `Attention` should prefer SageAttention [8][9]. `hunterFormsBS` does not
|
|
762
|
-
install `sageattention`.
|
|
763
|
-
scale : float | None = None
|
|
764
|
-
Optional attention-score multiplier override passed to each `Attention` [1].
|
|
765
|
-
|
|
766
|
-
PyTorch
|
|
767
|
-
-------
|
|
768
|
-
layer construction : implementation detail
|
|
769
|
-
`__init__` first builds `ParametersAttention` [3] with the downstream attention arguments.
|
|
770
|
-
`__init__` then creates `depth` `ModuleList` pairs, each containing one
|
|
771
|
-
`Attention(**parametersAttention)` [1] and one `FeedForward(dim=dim, ff_mult=ff_mult,
|
|
772
|
-
ff_dropout=ff_dropout)` [2]. `norm` becomes `RMSNorm(dim)` when
|
|
773
|
-
`norm_output` is `True` and `nn.Identity()` otherwise [4].
|
|
774
|
-
|
|
775
|
-
References
|
|
776
|
-
----------
|
|
777
|
-
[1] `Attention`
|
|
778
|
-
|
|
779
|
-
[2] `FeedForward`
|
|
780
|
-
|
|
781
|
-
[3] `hunterFormsBS.theTypes.ParametersAttention`
|
|
782
|
-
|
|
783
|
-
[4] PyTorch.
|
|
784
|
-
https://context7.com/pytorch/pytorch
|
|
785
|
-
[5] Gopalakrishnan, A., Csordás, R., Schmidhuber, J., and Mozer, M. C. (2025).
|
|
786
|
-
Decoupling the "What" and "Where" With Polar Coordinate Positional Embeddings.
|
|
787
|
-
https://arxiv.org/abs/2509.10534
|
|
788
|
-
[6] lucidrains/PoPE-pytorch.
|
|
789
|
-
https://github.com/lucidrains/PoPE-pytorch
|
|
790
|
-
[7] Su, J., Lu, Y., Pan, S., Murtadha, A., Wen, B., and Liu, Y. (2021).
|
|
791
|
-
RoFormer: Enhanced Transformer with Rotary Position Embedding.
|
|
792
|
-
https://arxiv.org/abs/2104.09864
|
|
793
|
-
[8] Zhang, J., Wei, J., Huang, H., Zhang, P., Zhu, J., and Chen, J. (2025).
|
|
794
|
-
SageAttention: Accurate 8-Bit Attention for Plug-and-play Inference Acceleration.
|
|
795
|
-
https://arxiv.org/abs/2410.02367
|
|
796
|
-
[9] thu-ml/SageAttention
|
|
797
|
-
https://github.com/thu-ml/SageAttention
|
|
798
|
-
"""
|
|
799
|
-
super().__init__()
|
|
800
|
-
|
|
801
|
-
parametersAttention = ParametersAttention(
|
|
802
|
-
attn_dropout=attn_dropout,
|
|
803
|
-
dim_head=dim_head,
|
|
804
|
-
dim=dim,
|
|
805
|
-
flash=flash_attn,
|
|
806
|
-
heads=heads,
|
|
807
|
-
pope_embed=pope_embed,
|
|
808
|
-
rotary_embed=rotary_embed,
|
|
809
|
-
sage_attention=sage_attention,
|
|
810
|
-
scale=scale,
|
|
811
|
-
)
|
|
812
|
-
|
|
813
|
-
self.layers = ModuleList(
|
|
814
|
-
ModuleList([Attention(**parametersAttention)
|
|
815
|
-
, FeedForward(dim=dim, ff_mult=ff_mult, ff_dropout=ff_dropout)])
|
|
816
|
-
for _layer in loops(depth)
|
|
817
|
-
)
|
|
818
|
-
|
|
819
|
-
self.norm: RMSNorm | nn.Identity = RMSNorm(dim) if norm_output else nn.Identity()
|
|
820
|
-
|
|
821
|
-
def forward(self, x: Tensor) -> Tensor:
|
|
822
|
-
"""Transform activations `x` through the residual stack.
|
|
823
|
-
|
|
824
|
-
You can use `forward` when `x` already has batch axis, sequence axis, and feature axis.
|
|
825
|
-
|
|
826
|
-
Parameters
|
|
827
|
-
----------
|
|
828
|
-
x : Tensor
|
|
829
|
-
Input activation `Tensor` with shape `batch × sequence position × feature`.
|
|
830
|
-
|
|
831
|
-
Returns
|
|
832
|
-
-------
|
|
833
|
-
result : Tensor
|
|
834
|
-
Output activation `Tensor` with shape `batch × sequence position × feature`.
|
|
835
|
-
|
|
836
|
-
References
|
|
837
|
-
----------
|
|
838
|
-
[1] `Attention.forward`
|
|
839
|
-
|
|
840
|
-
[2] `FeedForward.forward`
|
|
841
|
-
"""
|
|
842
|
-
for sherpa in self.layers:
|
|
843
|
-
attn: Attention = cast('Attention', cast('ModuleList', sherpa)[0])
|
|
844
|
-
ff: FeedForward = cast('FeedForward', cast('ModuleList', sherpa)[1])
|
|
845
|
-
x = attn(x) + x
|
|
846
|
-
x = ff(x) + x
|
|
847
|
-
|
|
848
|
-
return self.norm(x)
|