hunterFormsBS 0.3.2__py3-none-any.whl

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/bands.py ADDED
@@ -0,0 +1,118 @@
1
+ """Build the frequency-domain front end for band-split source separation.
2
+
3
+ You can use this module to partition STFT frequency bins into bands and project each band into a
4
+ common feature width. `BandSplit` is the shared front-end projector consumed by
5
+ `hunterFormsBS.bandSplitRotator.BandSplitRotator` [1][2][3]. `DEFAULT_FREQS_PER_BANDS` provides
6
+ the standard non-overlapping frequency-bin partition used by the BS-RoFormer front end.
7
+
8
+ Contents
9
+ --------
10
+ Classes
11
+ BandSplit
12
+ Project concatenated band slices to a shared feature width.
13
+
14
+ Variables
15
+ DEFAULT_FREQS_PER_BANDS
16
+ Standard non-overlapping frequency-bin partition for the BS-RoFormer front end.
17
+
18
+ References
19
+ ----------
20
+ [1] Lu, W.-T., Wang, J.-C., Kong, Q., & Hung, Y.-N. (2023). Music Source Separation with
21
+ Band-Split RoPE Transformer. https://arxiv.org/abs/2309.02612
22
+ [2] Wang, J.-C., Lu, W.-T., and Chen, J. (2024) Mel-RoFormer for Vocal Separation and Vocal Melody
23
+ Transcription https://arxiv.org/abs/2409.04702
24
+ [3] `hunterFormsBS.bandSplitRotator.BandSplitRotator`
25
+ """
26
+ from __future__ import annotations
27
+
28
+ from torch import nn, Tensor
29
+ from torch.nn import Module, ModuleList
30
+ from torch_einops_kit.scaleValues import RMSNorm
31
+ from typing import TYPE_CHECKING
32
+ import torch
33
+
34
+ if TYPE_CHECKING:
35
+ from collections.abc import Sequence
36
+
37
+ DEFAULT_FREQS_PER_BANDS: tuple[int, ...] = (2,) * 24 + (4,) * 12 + (12,) * 8 + (24,) * 8 + (48,) * 8 + (128, 129)
38
+
39
+ class BandSplit(Module):
40
+ """Project band slices to a shared feature width.
41
+
42
+ You can use this class to convert one concatenated band representation into one stack of band
43
+ tokens with a common feature width. `BandSplit` treats the last axis of the input `Tensor` as
44
+ consecutive band slices, applies one learned projection module to each slice, and returns one
45
+ `Tensor` with an explicit band axis. `BandSplit` is the shared front-end projector used by
46
+ `hunterFormsBS.bandSplitRotator.BandSplitRotator` [1].
47
+
48
+ Attributes
49
+ ----------
50
+ dim_inputs : list[int]
51
+ Width of each contiguous slice consumed from the last axis of the input `Tensor`.
52
+ to_features : ModuleList
53
+ Per-band projection module collection. Each entry transforms one slice width from `dim_inputs`
54
+ to the shared feature width configured in `__init__`.
55
+
56
+ References
57
+ ----------
58
+ [1] `hunterFormsBS.bandSplitRotator.BandSplitRotator`
59
+ """
60
+ def __init__(self, dim: int, dim_inputs: Sequence[int]) -> None:
61
+ """Configure one projection module per input band.
62
+
63
+ You can use `__init__` to declare how `BandSplit` should partition the last axis of each input
64
+ `Tensor` and how wide each projected band token should become. `__init__` stores the partition
65
+ widths in `self.dim_inputs` and builds one projection module for each partition.
66
+
67
+ Parameters
68
+ ----------
69
+ dim : int
70
+ Shared feature width produced for every band slice.
71
+ dim_inputs : Sequence[int]
72
+ Width sequence that partitions the last axis of the input `Tensor` into contiguous band
73
+ slices.
74
+ """
75
+ super().__init__()
76
+ self.dim_inputs: list[int] = list(dim_inputs)
77
+ self.to_features: ModuleList = ModuleList([])
78
+
79
+ for dim_in in self.dim_inputs:
80
+ net: nn.Sequential = nn.Sequential(RMSNorm(dim_in), nn.Linear(dim_in, dim))
81
+ self.to_features.append(net)
82
+
83
+ def forward(self, x: Tensor) -> Tensor:
84
+ """Project each band slice in `x` to one shared feature width.
85
+
86
+ You can use `forward` when `x` already stores one concatenated slice per band along the last
87
+ axis. `forward` splits `x` according to `self.dim_inputs`, applies the learned projection
88
+ module associated with each slice, and returns one `Tensor` with one explicit band axis.
89
+
90
+ Parameters
91
+ ----------
92
+ x : Tensor
93
+ Input band representation. `x.shape[-1]` must equal `sum(self.dim_inputs)`.
94
+
95
+ Returns
96
+ -------
97
+ band_features : Tensor
98
+ Projected band representation. `band_features.shape[-2]` equals `len(self.dim_inputs)`.
99
+ `band_features.shape[-1]` equals the shared projection width configured in `__init__`.
100
+
101
+ Shape Transformation
102
+ --------------------
103
+ input partition : relation
104
+ `forward` partitions `x` into contiguous last-axis slices whose widths come from
105
+ `self.dim_inputs`.
106
+ output layout : relation
107
+ If the leading shape of `x` is `leadingShape`, the return value has shape `(*leadingShape,
108
+ len(self.dim_inputs), projectedWidth)`, where `projectedWidth` is the shared projection
109
+ width configured in `__init__`.
110
+ """
111
+ tuple_inputs: tuple[Tensor, ...] = torch.split(x, self.dim_inputs, dim=-1)
112
+
113
+ outs: list[Tensor] = []
114
+ for split_input, to_feature in zip(tuple_inputs, self.to_features, strict=True):
115
+ split_output: Tensor = to_feature(split_input)
116
+ outs.append(split_output)
117
+
118
+ return torch.stack(outs, dim=-2)