Model Implementations¶
spectrans.models ¶
Spectral transformer model implementations.
This module provides transformer model implementations that replace traditional attention mechanisms with various spectral mixing approaches. Each model maintains the core transformer architecture with residual connections, layer normalization, and feedforward networks.
The models implement spectral transformers including FNet, Global Filter Networks, AFNO, spectral attention variants, and hybrid architectures that combine spectral and spatial processing.
Modules:
| Name | Description |
|---|---|
afno |
Adaptive Fourier Neural Operator models. |
base |
Base classes for models and components. |
fnet |
FNet models using Fourier mixing. |
fno_transformer |
Fourier Neural Operator transformer models. |
gfnet |
Global Filter Network models. |
hybrid |
Hybrid models combining spectral and attention. |
lst |
Linear Spectral Transform models. |
spectral_attention |
Models using spectral attention mechanisms. |
wavenet_transformer |
Wavelet-based transformer models. |
Classes:
| Name | Description |
|---|---|
BaseModel |
Abstract base class for all spectral transformer models. |
PositionalEncoding |
Sinusoidal positional encoding for sequence models. |
LearnedPositionalEncoding |
Learnable positional embedding layer. |
RotaryPositionalEncoding |
Rotary Position Embedding (RoPE) for improved length generalization. |
ALiBiPositionalBias |
Attention with Linear Biases (ALiBi) positional encoding. |
ClassificationHead |
Classification head for sequence classification tasks. |
RegressionHead |
Regression head for continuous prediction tasks. |
SequenceHead |
Generic sequence-to-sequence head for various tasks. |
FNet |
FNet model with Fourier mixing layers. |
FNetEncoder |
FNet encoder stack for encoder-only architectures. |
GFNet |
Global Filter Network model with learnable spectral filters. |
GFNetEncoder |
GFNet encoder stack implementation. |
AFNOEncoder |
Adaptive Fourier Neural Operator encoder. |
AFNOModel |
AFNO model for various tasks. |
SpectralAttentionEncoder |
Encoder using spectral attention with random Fourier features. |
SpectralAttentionTransformer |
Spectral attention transformer model. |
PerformerTransformer |
Performer-style transformer with linear attention approximation. |
LSTEncoder |
Linear Spectral Transform encoder using DCT/DST. |
LSTDecoder |
Linear Spectral Transform decoder implementation. |
LSTTransformer |
LST transformer with encoder-decoder architecture. |
FNOEncoder |
Fourier Neural Operator encoder for function space learning. |
FNODecoder |
FNO decoder for continuous function approximation. |
FNOTransformer |
FNO transformer for operator learning. |
WaveletEncoder |
Wavelet transform encoder with multiresolution analysis. |
WaveletDecoder |
Wavelet decoder for signal reconstruction. |
WaveletTransformer |
Wavelet transformer model. |
HybridEncoder |
Encoder combining spectral and spatial attention layers. |
HybridTransformer |
Hybrid model alternating between spectral and attention mechanisms. |
AlternatingTransformer |
Transformer with alternating spectral and attention layers. |
StandardAttention |
Standard multi-head self-attention wrapper for hybrid models. |
Examples:
Basic FNet usage:
>>> import torch
>>> from spectrans.models import FNet
>>>
>>> # Create FNet model
>>> model = FNet(
... hidden_dim=512,
... num_layers=12,
... vocab_size=32000,
... max_seq_len=512
... )
>>>
>>> # Forward pass
>>> input_ids = torch.randint(0, 32000, (2, 128))
>>> outputs = model(input_ids)
>>> print(outputs.shape) # torch.Size([2, 128, 512])
Global Filter Network example:
>>> from spectrans.models import GFNet
>>>
>>> # Create GFNet for sequence classification
>>> model = GFNet(
... hidden_dim=768,
... num_layers=12,
... num_classes=10,
... sequence_length=256
... )
>>>
>>> # Classification forward pass
>>> x = torch.randn(4, 256, 768)
>>> logits = model(x)
>>> print(logits.shape) # torch.Size([4, 10])
AFNO for continuous functions:
>>> from spectrans.models import AFNOModel
>>>
>>> # Create AFNO model
>>> model = AFNOModel(
... hidden_dim=512,
... num_layers=8,
... n_modes=32,
... input_dim=2,
... output_dim=1
... )
>>>
>>> # Function approximation
>>> x = torch.randn(8, 64, 64, 2) # Batch of 2D functions
>>> output = model(x)
>>> print(output.shape) # torch.Size([8, 64, 64, 1])
Hybrid spectral-attention model:
>>> from spectrans.models import HybridTransformer
>>>
>>> # Create hybrid model alternating spectral and attention
>>> model = HybridTransformer(
... hidden_dim=512,
... num_layers=12,
... num_heads=8,
... spectral_type="fourier",
... vocab_size=50000
... )
>>>
>>> input_ids = torch.randint(0, 50000, (2, 256))
>>> outputs = model(input_ids)
>>> print(outputs.shape) # torch.Size([2, 256, 512])
Wavelet transformer for multiresolution analysis:
>>> from spectrans.models import WaveletTransformer
>>>
>>> # Create wavelet transformer
>>> model = WaveletTransformer(
... hidden_dim=512,
... num_layers=8,
... wavelet="db4",
... levels=3,
... vocab_size=32000
... )
>>>
>>> input_ids = torch.randint(0, 32000, (2, 512))
>>> outputs = model(input_ids)
>>> print(outputs.shape) # torch.Size([2, 512, 512])
Positional encodings with RoPE and ALiBi:
>>> from spectrans.models import FNet, RotaryPositionalEncoding, ALiBiPositionalBias
>>>
>>> # FNet with RoPE
>>> model = FNet(
... hidden_dim=512,
... num_layers=12,
... vocab_size=50000,
... pos_encoding=RotaryPositionalEncoding(dim=512)
... )
>>>
>>> # Or with ALiBi (no positional embeddings needed)
>>> model_alibi = FNet(
... hidden_dim=512,
... num_layers=12,
... vocab_size=50000,
... pos_encoding=ALiBiPositionalBias(num_heads=8)
... )
Notes
All models in this module follow the same architectural principles. Spectral processing replaces quadratic attention with spectral transforms that scale as \(O(n \log n)\) or \(O(n)\) in time complexity. Residual connections maintain gradient flow around each spectral layer and feedforward network. Layer normalization is applied before spectral mixing and feedforward operations for training stability.
The models support multiple positional encoding methods including sinusoidal, learned embeddings, RoPE, and ALiBi for various sequence modeling needs. Specialized output heads are provided for classification, regression, and sequence-to-sequence tasks.
The mathematical foundation for spectral mixing is based on the convolution theorem, which states that convolution in the spatial domain is equivalent to element-wise multiplication in the frequency domain:
This enables efficient global mixing of sequence elements through spectral transforms like FFT, DCT, or DWT, avoiding the quadratic complexity of traditional attention mechanisms.
Model Complexity Comparison:
- Standard Transformer: \(O(n^2 d + nd^2)\) time, \(O(n^2 + nd)\) space
- FNet: \(O(nd \log n + nd^2)\) time, \(O(nd)\) space
- GFNet: \(O(nd \log n + nd^2)\) time, \(O(nd)\) space
- AFNO: \(O(k_n k_d d + nd \log n)\) time, \(O(k_n k_d d)\) space
- LST: \(O(nd \log n + nd^2)\) time, \(O(nd)\) space
- Wavelet: \(O(nd + nd^2)\) time, \(O(nd)\) space
Where \(n\) is sequence length, \(d\) is hidden dimension, and \(k_n, k_d\) are retained spectral modes.
References
James Lee-Thorp, Joshua Ainslie, Ilya Eckstein, and Santiago Ontanon. 2022. FNet: Mixing tokens with Fourier transforms. In Proceedings of the 2022 Conference of the North American Chapter of the Association for Computational Linguistics: Human Language Technologies (NAACL-HLT), pages 4296-4313, Seattle.
Yongming Rao, Wenliang Zhao, Zheng Zhu, Jiwen Lu, and Jie Zhou. 2021. Global filter networks for image classification. In Advances in Neural Information Processing Systems 34 (NeurIPS 2021), pages 980-993.
John Guibas, Morteza Mardani, Zongyi Li, Andrew Tao, Anima Anandkumar, and Bryan Catanzaro. 2022. Adaptive Fourier neural operators: Efficient token mixers for transformers. In Proceedings of the International Conference on Learning Representations (ICLR).
Zongyi Li, Nikola Kovachki, Kamyar Azizzadenesheli, Burigede Liu, Kaushik Bhattacharya, Andrew Stuart, and Anima Anandkumar. 2021. Fourier neural operator for parametric partial differential equations. In Proceedings of the International Conference on Learning Representations (ICLR).
Krzysztof Choromanski, Valerii Likhosherstov, David Dohan, Xingyou Song, Andreea Gane, Tamas Sarlos, Peter Hawkins, Jared Davis, Afroz Mohiuddin, Lukasz Kaiser, David Belanger, Lucy Colwell, and Adrian Weller. 2021. Rethinking attention with performers. In Proceedings of the International Conference on Learning Representations (ICLR).
See Also
spectrans.layers.mixing : Spectral mixing layer implementations.
spectrans.layers.attention : Spectral attention mechanisms.
spectrans.layers.operators : Neural operator layers.
spectrans.blocks : Transformer block implementations.
Classes¶
AFNOEncoder ¶
AFNOEncoder(hidden_dim: int = 768, num_layers: int = 12, max_sequence_length: int = 1024, modes_seq: int | None = None, modes_hidden: int | None = None, mlp_ratio: float = 2.0, use_positional_encoding: bool = True, positional_encoding_type: PositionalEncodingType = 'sinusoidal', dropout: float = 0.1, ffn_hidden_dim: int | None = None, norm_eps: float = 1e-12, gradient_checkpointing: bool = False)
Bases: AFNOModel
Encoder-only AFNO model for representation learning.
This variant of AFNO is designed for tasks that require extracting representations rather than making predictions. It's particularly efficient for processing very long sequences due to the mode truncation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
hidden_dim
|
int
|
Hidden dimension size. Default is 768. |
768
|
num_layers
|
int
|
Number of AFNO layers. Default is 12. |
12
|
max_sequence_length
|
int
|
Maximum sequence length. Default is 1024. |
1024
|
modes_seq
|
int | None
|
Number of Fourier modes in sequence dimension. |
None
|
modes_hidden
|
int | None
|
Number of Fourier modes in hidden dimension. |
None
|
mlp_ratio
|
float
|
MLP expansion ratio. Default is 2.0. |
2.0
|
use_positional_encoding
|
bool
|
Whether to use positional encoding. Default is True. |
True
|
positional_encoding_type
|
str
|
Type of positional encoding. Default is 'sinusoidal'. |
'sinusoidal'
|
dropout
|
float
|
Dropout probability. Default is 0.1. |
0.1
|
ffn_hidden_dim
|
int | None
|
Hidden dimension for FFN. If None, defaults to 4 * hidden_dim. |
None
|
norm_eps
|
float
|
Epsilon for layer normalization. Default is 1e-12. |
1e-12
|
gradient_checkpointing
|
bool
|
Whether to use gradient checkpointing. Default is False. |
False
|
Source code in spectrans/models/afno.py
AFNOModel ¶
AFNOModel(vocab_size: int | None = None, hidden_dim: int = 768, num_layers: int = 12, max_sequence_length: int = 1024, modes_seq: int | None = None, modes_hidden: int | None = None, mlp_ratio: float = 2.0, num_classes: int | None = None, use_positional_encoding: bool = True, positional_encoding_type: PositionalEncodingType = 'sinusoidal', dropout: float = 0.1, ffn_hidden_dim: int | None = None, norm_eps: float = 1e-12, output_type: OutputHeadType = 'classification', gradient_checkpointing: bool = False)
Bases: BaseModel
Adaptive Fourier Neural Operator transformer model.
AFNO performs token mixing using truncated Fourier modes and learnable MLPs in the frequency domain, processing long sequences with \(O(n \log n)\) time complexity.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vocab_size
|
int | None
|
Size of the vocabulary for token embeddings. If None, expects pre-embedded inputs. |
None
|
hidden_dim
|
int
|
Hidden dimension size. Default is 768. |
768
|
num_layers
|
int
|
Number of AFNO layers. Default is 12. |
12
|
max_sequence_length
|
int
|
Maximum sequence length. Default is 1024. |
1024
|
modes_seq
|
int | None
|
Number of Fourier modes to keep in sequence dimension. If None, defaults to max_sequence_length // 2. |
None
|
modes_hidden
|
int | None
|
Number of Fourier modes to keep in hidden dimension. If None, defaults to hidden_dim // 2. |
None
|
mlp_ratio
|
float
|
Expansion ratio for MLP in Fourier domain. Default is 2.0. |
2.0
|
num_classes
|
int | None
|
Number of output classes for classification. Default is None. |
None
|
use_positional_encoding
|
bool
|
Whether to use positional encoding. Default is True. |
True
|
positional_encoding_type
|
str
|
Type of positional encoding: 'sinusoidal' or 'learned'. Default is 'sinusoidal'. |
'sinusoidal'
|
dropout
|
float
|
Dropout probability. Default is 0.1. |
0.1
|
ffn_hidden_dim
|
int | None
|
Hidden dimension for FFN. If None, defaults to 4 * hidden_dim. |
None
|
norm_eps
|
float
|
Epsilon for layer normalization. Default is 1e-12. |
1e-12
|
output_type
|
str
|
Type of output head: 'classification', 'regression', 'sequence', or 'none'. Default is 'classification'. |
'classification'
|
gradient_checkpointing
|
bool
|
Whether to use gradient checkpointing. Default is False. |
False
|
Attributes:
| Name | Type | Description |
|---|---|---|
modes_seq |
int
|
Number of Fourier modes in sequence dimension. |
modes_hidden |
int
|
Number of Fourier modes in hidden dimension. |
mlp_ratio |
float
|
MLP expansion ratio in frequency domain. |
blocks |
ModuleList
|
List of AFNO transformer blocks. |
Methods:
| Name | Description |
|---|---|
build_blocks |
Build AFNO transformer blocks with adaptive Fourier mixing. |
from_config |
Create AFNO model from configuration. |
Source code in spectrans/models/afno.py
Functions¶
build_blocks ¶
Build AFNO transformer blocks with adaptive Fourier mixing.
Returns:
| Type | Description |
|---|---|
ModuleList
|
List of AFNO transformer blocks. |
Source code in spectrans/models/afno.py
from_config
classmethod
¶
from_config(config: AFNOModelConfig) -> AFNOModel
Create AFNO model from configuration.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
AFNOModelConfig
|
Configuration object with model parameters. |
required |
Returns:
| Type | Description |
|---|---|
AFNOModel
|
Configured AFNO model. |
Source code in spectrans/models/afno.py
ALiBiPositionalBias ¶
Bases: Module
Attention with Linear Biases (ALiBi) positional encoding.
This module implements ALiBi, which adds a linear bias to attention scores based on the relative distance between tokens. Unlike traditional position embeddings, ALiBi enables extrapolation to longer sequences.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
num_heads
|
int
|
Number of attention heads. |
required |
max_sequence_length
|
int
|
Maximum sequence length to encode. |
5000
|
Attributes:
| Name | Type | Description |
|---|---|---|
num_heads |
int
|
Number of attention heads. |
slopes |
Tensor
|
Head-specific slope parameters. |
alibi |
Tensor | None
|
Cached linear bias matrix. |
References
Ofir Press, Noah A. Smith, and Mike Lewis. 2022. Train short, test long: Attention with linear biases enables input length extrapolation. In Proceedings of the International Conference on Learning Representations (ICLR).
Methods:
| Name | Description |
|---|---|
forward |
Add ALiBi bias to attention scores. |
get_bias |
Get ALiBi bias matrix for a given sequence length. |
Source code in spectrans/models/base.py
Functions¶
forward ¶
Add ALiBi bias to attention scores.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
attention_scores
|
Tensor
|
Attention scores of shape (batch_size, num_heads, seq_len, seq_len). |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
Attention scores with ALiBi bias added. |
Source code in spectrans/models/base.py
get_bias ¶
Get ALiBi bias matrix for a given sequence length.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
seq_len
|
int
|
Sequence length. |
required |
device
|
device | None
|
Device to place the bias tensor. |
None
|
Returns:
| Type | Description |
|---|---|
Tensor
|
ALiBi bias of shape (1, num_heads, seq_len, seq_len). |
Source code in spectrans/models/base.py
BaseModel ¶
BaseModel(vocab_size: int | None = None, hidden_dim: int = 768, num_layers: int = 12, max_sequence_length: int = 512, num_classes: int | None = None, use_positional_encoding: bool = True, positional_encoding_type: PositionalEncodingType = 'sinusoidal', dropout: float = 0.1, ffn_hidden_dim: int | None = None, norm_eps: float = 1e-12, output_type: OutputHeadType = 'classification', gradient_checkpointing: bool = False)
Bases: SpectralComponent, ABC
Abstract base class for spectral transformer models.
This class provides the common functionality shared by all spectral transformer models, including embeddings, positional encoding, and output heads. Subclasses must implement the build_blocks method to define their specific architecture.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vocab_size
|
int | None
|
Size of the vocabulary for token embeddings. If None, no input embedding layer is created (assumes pre-embedded inputs). |
None
|
hidden_dim
|
int
|
Hidden dimension size for the model. |
768
|
num_layers
|
int
|
Number of transformer blocks in the model. |
12
|
max_sequence_length
|
int
|
Maximum sequence length the model can process. |
512
|
num_classes
|
int | None
|
Number of output classes for classification. If None, no classification head is added. |
None
|
use_positional_encoding
|
bool
|
Whether to use positional encoding. Default is True. |
True
|
positional_encoding_type
|
PositionalEncodingType
|
Type of positional encoding: 'sinusoidal', 'learned', 'rotary', 'alibi', or 'none'. Default is 'sinusoidal'. |
'sinusoidal'
|
dropout
|
float
|
Dropout probability. Default is 0.1. |
0.1
|
ffn_hidden_dim
|
int | None
|
Hidden dimension for feedforward networks. If None, defaults to 4 * hidden_dim. |
None
|
norm_eps
|
float
|
Epsilon for layer normalization. Default is 1e-12. |
1e-12
|
output_type
|
OutputHeadType
|
Type of output head: 'classification', 'regression', 'sequence', 'lm', or 'none'. Default is 'classification'. |
'classification'
|
gradient_checkpointing
|
bool
|
Whether to use gradient checkpointing for memory efficiency. Default is False. |
False
|
Attributes:
| Name | Type | Description |
|---|---|---|
hidden_dim |
int
|
Hidden dimension size. |
num_layers |
int
|
Number of transformer blocks. |
max_sequence_length |
int
|
Maximum sequence length. |
embedding |
Embedding | None
|
Token embedding layer (if vocab_size is provided). |
positional_encoding |
PositionalEncoding | LearnedPositionalEncoding | None
|
Positional encoding module. |
blocks |
ModuleList
|
List of transformer blocks. |
output_head |
Module | None
|
Task-specific output head. |
dropout |
Dropout
|
Dropout layer. |
Methods:
| Name | Description |
|---|---|
build_blocks |
Build the transformer blocks for the model. |
forward |
Forward pass through the model. |
from_config |
Create model instance from configuration object. |
Source code in spectrans/models/base.py
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 | |
Functions¶
build_blocks
abstractmethod
¶
Build the transformer blocks for the model.
This method must be implemented by subclasses to define the specific architecture using appropriate mixing layers.
Returns:
| Type | Description |
|---|---|
ModuleList
|
List of transformer blocks. |
Source code in spectrans/models/base.py
forward ¶
forward(input_ids: Tensor | None = None, inputs_embeds: Tensor | None = None, attention_mask: Tensor | None = None) -> Tensor
Forward pass through the model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input_ids
|
Tensor | None
|
Input token IDs of shape (batch_size, sequence_length). Required if embedding layer exists. |
None
|
inputs_embeds
|
Tensor | None
|
Pre-embedded inputs of shape (batch_size, sequence_length, hidden_dim). Used if no embedding layer or to bypass embedding. |
None
|
attention_mask
|
Tensor | None
|
Attention mask of shape (batch_size, sequence_length). Values should be 0 or 1 (1 for tokens to attend to). |
None
|
Returns:
| Type | Description |
|---|---|
Tensor
|
Output tensor. Shape depends on the output head: - Classification: (batch_size, num_classes) - Regression: (batch_size, 1) - Sequence: (batch_size, sequence_length, vocab_size) - None: (batch_size, sequence_length, hidden_dim) |
Raises:
| Type | Description |
|---|---|
ValueError
|
If neither input_ids nor inputs_embeds is provided. |
Source code in spectrans/models/base.py
from_config
classmethod
¶
from_config(config: ModelConfig) -> BaseModel
Create model instance from configuration object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
ModelConfig
|
Configuration object with model parameters. |
required |
Returns:
| Type | Description |
|---|---|
BaseModel
|
Configured model instance. |
Source code in spectrans/models/base.py
ClassificationHead ¶
ClassificationHead(hidden_dim: int, num_classes: int, dropout: float = 0.1, pooling: PoolingType = 'cls')
Bases: Module
Classification output head.
This module pools sequence outputs and projects to class logits.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
hidden_dim
|
int
|
Input hidden dimension. |
required |
num_classes
|
int
|
Number of output classes. |
required |
dropout
|
float
|
Dropout probability. Default is 0.1. |
0.1
|
pooling
|
PoolingType
|
Pooling strategy: 'cls', 'mean', or 'max'. Default is 'cls'. |
'cls'
|
Attributes:
| Name | Type | Description |
|---|---|---|
pooling |
PoolingType
|
Pooling strategy. |
dropout |
Dropout
|
Dropout layer. |
classifier |
Linear
|
Output projection layer. |
Methods:
| Name | Description |
|---|---|
forward |
Forward pass through classification head. |
Source code in spectrans/models/base.py
Functions¶
forward ¶
Forward pass through classification head.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
hidden_states
|
Tensor
|
Input tensor of shape (batch_size, sequence_length, hidden_dim). |
required |
attention_mask
|
Tensor | None
|
Attention mask for pooling operations. |
None
|
Returns:
| Type | Description |
|---|---|
Tensor
|
Classification logits of shape (batch_size, num_classes). |
Source code in spectrans/models/base.py
LearnedPositionalEncoding ¶
Bases: Module
Learned positional embeddings.
This module uses learnable positional embeddings instead of fixed sinusoidal encodings.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
hidden_dim
|
int
|
Dimension of the embeddings. |
required |
max_sequence_length
|
int
|
Maximum sequence length to encode. |
5000
|
dropout
|
float
|
Dropout probability. Default is 0.1. |
0.1
|
Attributes:
| Name | Type | Description |
|---|---|---|
position_embeddings |
Embedding
|
Learnable position embeddings. |
dropout |
Dropout
|
Dropout layer. |
Methods:
| Name | Description |
|---|---|
forward |
Add learned positional embeddings to input tensor. |
Source code in spectrans/models/base.py
Functions¶
forward ¶
Add learned positional embeddings to input tensor.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Tensor
|
Input tensor of shape (batch_size, sequence_length, hidden_dim). |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
Tensor with positional embeddings added. |
Source code in spectrans/models/base.py
PositionalEncoding ¶
Bases: Module
Sinusoidal positional encoding.
This module adds sinusoidal positional encodings to embeddings, following the approach in "Attention is All You Need".
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
hidden_dim
|
int
|
Dimension of the embeddings. |
required |
max_sequence_length
|
int
|
Maximum sequence length to encode. |
5000
|
dropout
|
float
|
Dropout probability. Default is 0.1. |
0.1
|
Attributes:
| Name | Type | Description |
|---|---|---|
dropout |
Dropout
|
Dropout layer. |
pe |
Tensor
|
Precomputed positional encodings. |
Methods:
| Name | Description |
|---|---|
forward |
Add positional encoding to input tensor. |
Source code in spectrans/models/base.py
Functions¶
forward ¶
Add positional encoding to input tensor.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Tensor
|
Input tensor of shape (batch_size, sequence_length, hidden_dim). |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
Tensor with positional encoding added. |
Source code in spectrans/models/base.py
RegressionHead ¶
Bases: Module
Regression output head.
This module pools sequence outputs and projects to a scalar value.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
hidden_dim
|
int
|
Input hidden dimension. |
required |
dropout
|
float
|
Dropout probability. Default is 0.1. |
0.1
|
pooling
|
PoolingType
|
Pooling strategy: 'cls', 'mean', or 'max'. Default is 'mean'. |
'mean'
|
Attributes:
| Name | Type | Description |
|---|---|---|
pooling |
PoolingType
|
Pooling strategy. |
dropout |
Dropout
|
Dropout layer. |
regressor |
Linear
|
Output projection layer. |
Methods:
| Name | Description |
|---|---|
forward |
Forward pass through regression head. |
Source code in spectrans/models/base.py
Functions¶
forward ¶
Forward pass through regression head.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
hidden_states
|
Tensor
|
Input tensor of shape (batch_size, sequence_length, hidden_dim). |
required |
attention_mask
|
Tensor | None
|
Attention mask for pooling operations. |
None
|
Returns:
| Type | Description |
|---|---|
Tensor
|
Regression output of shape (batch_size, 1). |
Source code in spectrans/models/base.py
RotaryPositionalEncoding ¶
Bases: Module
Rotary Position Embedding (RoPE).
This module implements Rotary Position Embeddings as described in the RoFormer paper. RoPE encodes absolute position with rotation matrix and naturally incorporates relative position dependency in self-attention formulation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
hidden_dim
|
int
|
Dimension of the embeddings. Must be even. |
required |
max_sequence_length
|
int
|
Maximum sequence length to encode. |
5000
|
base
|
float
|
Base for the frequency calculation. Default is 10000. |
10000.0
|
Attributes:
| Name | Type | Description |
|---|---|---|
inv_freq |
Tensor
|
Inverse frequencies for computing rotary embeddings. |
cos_cached |
Tensor | None
|
Cached cosine values for positions. |
sin_cached |
Tensor | None
|
Cached sine values for positions. |
References
Jianlin Su, Yu Lu, Shengfeng Pan, Ahmed Murtadha, Bo Wen, and Yunfeng Liu. 2024. RoFormer: Enhanced transformer with rotary position embedding. Neurocomputing, 568:127063.
Methods:
| Name | Description |
|---|---|
forward |
Apply rotary position embedding to input tensor. |
Source code in spectrans/models/base.py
Functions¶
forward ¶
Apply rotary position embedding to input tensor.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Tensor
|
Input tensor of shape (batch_size, num_heads, sequence_length, head_dim) or (batch_size, sequence_length, hidden_dim). |
required |
offset
|
int
|
Position offset for incremental decoding. Default is 0. |
0
|
Returns:
| Type | Description |
|---|---|
Tensor
|
Tensor with rotary position embeddings applied. |
Source code in spectrans/models/base.py
SequenceHead ¶
Bases: Module
Sequence-to-sequence output head.
This module projects hidden states to vocabulary logits for sequence generation tasks.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
hidden_dim
|
int
|
Input hidden dimension. |
required |
vocab_size
|
int
|
Output vocabulary size. |
required |
dropout
|
float
|
Dropout probability. Default is 0.1. |
0.1
|
Attributes:
| Name | Type | Description |
|---|---|---|
dropout |
Dropout
|
Dropout layer. |
lm_head |
Linear
|
Language modeling head. |
Methods:
| Name | Description |
|---|---|
forward |
Forward pass through sequence head. |
Source code in spectrans/models/base.py
Functions¶
forward ¶
Forward pass through sequence head.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
hidden_states
|
Tensor
|
Input tensor of shape (batch_size, sequence_length, hidden_dim). |
required |
attention_mask
|
Tensor | None
|
Not used, kept for interface consistency. |
None
|
Returns:
| Type | Description |
|---|---|
Tensor
|
Vocabulary logits of shape (batch_size, sequence_length, vocab_size). |
Source code in spectrans/models/base.py
FNet ¶
FNet(vocab_size: int | None = None, hidden_dim: int = 768, num_layers: int = 12, max_sequence_length: int = 512, num_classes: int | None = None, use_positional_encoding: bool = True, positional_encoding_type: PositionalEncodingType = 'sinusoidal', dropout: float = 0.1, ffn_hidden_dim: int | None = None, norm_eps: float = 1e-12, output_type: OutputHeadType = 'classification', use_real_fft: bool = True, gradient_checkpointing: bool = False)
Bases: BaseModel
FNet model with Fourier transform-based token mixing.
FNet replaces the self-attention mechanism with Fourier transforms, achieving \(O(n \log n)\) complexity.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vocab_size
|
int | None
|
Size of the vocabulary for token embeddings. If None, expects pre-embedded inputs. |
None
|
hidden_dim
|
int
|
Hidden dimension size. Default is 768. |
768
|
num_layers
|
int
|
Number of FNet layers. Default is 12. |
12
|
max_sequence_length
|
int
|
Maximum sequence length. Default is 512. |
512
|
num_classes
|
int | None
|
Number of output classes for classification. Default is None. |
None
|
use_positional_encoding
|
bool
|
Whether to use positional encoding. Default is True. |
True
|
positional_encoding_type
|
str
|
Type of positional encoding: 'sinusoidal' or 'learned'. Default is 'sinusoidal'. |
'sinusoidal'
|
dropout
|
float
|
Dropout probability. Default is 0.1. |
0.1
|
ffn_hidden_dim
|
int | None
|
Hidden dimension for FFN. If None, defaults to 4 * hidden_dim. |
None
|
norm_eps
|
float
|
Epsilon for layer normalization. Default is 1e-12. |
1e-12
|
output_type
|
str
|
Type of output head: 'classification', 'regression', 'sequence', or 'none'. Default is 'classification'. |
'classification'
|
use_real_fft
|
bool
|
Whether to use real FFT for efficiency. Default is True. |
True
|
gradient_checkpointing
|
bool
|
Whether to use gradient checkpointing. Default is False. |
False
|
Attributes:
| Name | Type | Description |
|---|---|---|
use_real_fft |
bool
|
Whether real FFT is used for efficiency. |
blocks |
ModuleList
|
List of FNet transformer blocks. |
Methods:
| Name | Description |
|---|---|
build_blocks |
Build FNet transformer blocks with Fourier mixing. |
from_config |
Create FNet model from configuration. |
Source code in spectrans/models/fnet.py
Functions¶
build_blocks ¶
Build FNet transformer blocks with Fourier mixing.
Returns:
| Type | Description |
|---|---|
ModuleList
|
List of FNet transformer blocks. |
Source code in spectrans/models/fnet.py
from_config
classmethod
¶
from_config(config: FNetModelConfig) -> FNet
Create FNet model from configuration.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
FNetModelConfig
|
Configuration object with model parameters. |
required |
Returns:
| Type | Description |
|---|---|
FNet
|
Configured FNet model. |
Source code in spectrans/models/fnet.py
FNetEncoder ¶
FNetEncoder(hidden_dim: int = 768, num_layers: int = 12, max_sequence_length: int = 512, use_positional_encoding: bool = True, positional_encoding_type: PositionalEncodingType = 'sinusoidal', dropout: float = 0.1, ffn_hidden_dim: int | None = None, norm_eps: float = 1e-12, use_real_fft: bool = True, gradient_checkpointing: bool = False)
Bases: FNet
Encoder-only FNet model for representation learning.
This variant of FNet is designed for tasks that require extracting representations rather than making predictions. It returns the hidden states from the final layer without any task-specific head.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
hidden_dim
|
int
|
Hidden dimension size. Default is 768. |
768
|
num_layers
|
int
|
Number of FNet layers. Default is 12. |
12
|
max_sequence_length
|
int
|
Maximum sequence length. Default is 512. |
512
|
use_positional_encoding
|
bool
|
Whether to use positional encoding. Default is True. |
True
|
positional_encoding_type
|
str
|
Type of positional encoding. Default is 'sinusoidal'. |
'sinusoidal'
|
dropout
|
float
|
Dropout probability. Default is 0.1. |
0.1
|
ffn_hidden_dim
|
int | None
|
Hidden dimension for FFN. If None, defaults to 4 * hidden_dim. |
None
|
norm_eps
|
float
|
Epsilon for layer normalization. Default is 1e-12. |
1e-12
|
use_real_fft
|
bool
|
Whether to use real FFT. Default is True. |
True
|
gradient_checkpointing
|
bool
|
Whether to use gradient checkpointing. Default is False. |
False
|
Source code in spectrans/models/fnet.py
FNODecoder ¶
FNODecoder(vocab_size: int, hidden_dim: int = 512, num_layers: int = 12, max_sequence_length: int = 2048, modes: int = 32, mlp_ratio: float = 2.0, causal: bool = True, ffn_hidden_dim: int | None = None, dropout: float = 0.0, use_positional_encoding: bool = True, positional_encoding_type: PositionalEncodingType = 'sinusoidal', gradient_checkpointing: bool = False)
Bases: BaseModel
Decoder FNO model for generation tasks.
This model uses causal FNO blocks suitable for autoregressive generation tasks. The spectral operations are modified to respect causality.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vocab_size
|
int
|
Size of the vocabulary for generation. |
required |
hidden_dim
|
int
|
Hidden dimension size. |
512
|
num_layers
|
int
|
Number of decoder blocks. |
12
|
max_sequence_length
|
int
|
Maximum sequence length. |
2048
|
modes
|
int
|
Number of Fourier modes (adjusted for causality). |
32
|
mlp_ratio
|
float
|
MLP expansion ratio. |
2.0
|
causal
|
bool
|
Whether to use causal masking. |
True
|
ffn_hidden_dim
|
int | None
|
Hidden dimension of the feedforward network. |
None
|
dropout
|
float
|
Dropout probability. |
0.0
|
use_positional_encoding
|
bool
|
Whether to use positional encoding. |
True
|
positional_encoding_type
|
str
|
Type of positional encoding. |
"sinusoidal"
|
gradient_checkpointing
|
bool
|
Whether to use gradient checkpointing. |
False
|
Examples:
>>> decoder = FNODecoder(
... vocab_size=10000,
... hidden_dim=512,
... num_layers=12,
... modes=32,
... causal=True,
... max_sequence_length=2048
... )
>>> input_ids = torch.randint(0, 10000, (32, 100))
>>> logits = decoder(input_ids)
>>> assert logits.shape == (32, 100, 10000)
Methods:
| Name | Description |
|---|---|
build_blocks |
Build decoder blocks with causal FNO layers. |
forward |
Forward pass through the decoder. |
Source code in spectrans/models/fno_transformer.py
Functions¶
build_blocks ¶
Build decoder blocks with causal FNO layers.
Returns:
| Type | Description |
|---|---|
ModuleList
|
List of causal FNO decoder blocks. |
Source code in spectrans/models/fno_transformer.py
forward ¶
forward(input_ids: Tensor | None = None, inputs_embeds: Tensor | None = None, attention_mask: Tensor | None = None) -> Tensor
Forward pass through the decoder.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input_ids
|
Tensor | None
|
Input token IDs of shape (batch_size, sequence_length). |
None
|
inputs_embeds
|
Tensor | None
|
Pre-embedded inputs of shape (batch_size, sequence_length, hidden_dim). |
None
|
attention_mask
|
Tensor | None
|
Attention mask for padding. |
None
|
Returns:
| Type | Description |
|---|---|
Tensor
|
Logits of shape (batch_size, sequence_length, vocab_size). |
Source code in spectrans/models/fno_transformer.py
FNOEncoder ¶
FNOEncoder(hidden_dim: int = 512, num_layers: int = 6, max_sequence_length: int = 1024, modes: int = 32, mlp_ratio: float = 2.0, ffn_hidden_dim: int | None = None, dropout: float = 0.0, use_positional_encoding: bool = True, positional_encoding_type: PositionalEncodingType = 'sinusoidal', gradient_checkpointing: bool = False)
Bases: BaseModel
Encoder-only FNO model for representation learning.
This model uses stacked FNO blocks without causal masking, suitable for bidirectional encoding tasks like feature extraction and representation learning.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
hidden_dim
|
int
|
Hidden dimension size for the model. |
512
|
num_layers
|
int
|
Number of encoder blocks. |
6
|
max_sequence_length
|
int
|
Maximum sequence length. |
1024
|
modes
|
int
|
Number of Fourier modes to retain. |
32
|
mlp_ratio
|
float
|
MLP expansion ratio in FNO blocks. |
2.0
|
ffn_hidden_dim
|
int | None
|
Hidden dimension of the feedforward network. |
None
|
dropout
|
float
|
Dropout probability. |
0.0
|
use_positional_encoding
|
bool
|
Whether to use positional encoding. |
True
|
positional_encoding_type
|
str
|
Type of positional encoding. |
"sinusoidal"
|
gradient_checkpointing
|
bool
|
Whether to use gradient checkpointing. |
False
|
Examples:
>>> encoder = FNOEncoder(
... hidden_dim=512,
... num_layers=6,
... modes=32,
... max_sequence_length=1024
... )
>>> x = torch.randn(32, 100, 512)
>>> encoded = encoder(inputs_embeds=x)
>>> assert encoded.shape == x.shape
Methods:
| Name | Description |
|---|---|
build_blocks |
Build encoder blocks with FNO layers. |
Source code in spectrans/models/fno_transformer.py
Functions¶
build_blocks ¶
Build encoder blocks with FNO layers.
Returns:
| Type | Description |
|---|---|
ModuleList
|
List of FNO encoder blocks. |
Source code in spectrans/models/fno_transformer.py
FNOTransformer ¶
FNOTransformer(vocab_size: int | None = None, hidden_dim: int = 512, num_layers: int = 6, max_sequence_length: int = 1024, modes: int = 32, mlp_ratio: float = 2.0, use_2d: bool = False, spatial_dim: int | None = None, num_classes: int | None = None, ffn_hidden_dim: int | None = None, dropout: float = 0.0, use_positional_encoding: bool = True, positional_encoding_type: PositionalEncodingType = 'sinusoidal', gradient_checkpointing: bool = False)
Bases: BaseModel
Fourier Neural Operator transformer model.
This model uses Fourier Neural Operators for sequence mixing, achieving O(n log n) complexity through FFT operations. The model learns mappings between function spaces by parameterizing kernels in the Fourier domain.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vocab_size
|
int | None
|
Size of the vocabulary for token embeddings. If None, expects pre-embedded inputs. |
None
|
hidden_dim
|
int
|
Hidden dimension size for the model. |
512
|
num_layers
|
int
|
Number of transformer blocks. |
6
|
max_sequence_length
|
int
|
Maximum sequence length the model can process. |
1024
|
modes
|
int
|
Number of Fourier modes to retain (frequency truncation). |
32
|
mlp_ratio
|
float
|
Expansion ratio for the MLP in FNO blocks. |
2.0
|
use_2d
|
bool
|
Whether to use 2D spectral convolutions for spatial data. |
False
|
spatial_dim
|
int | None
|
Spatial dimension when using 2D convolutions (sequence = spatial_dim²). |
None
|
num_classes
|
int | None
|
Number of output classes for classification. |
None
|
ffn_hidden_dim
|
int | None
|
Hidden dimension of the feedforward network. Default is 4 * hidden_dim. |
None
|
dropout
|
float
|
Dropout probability. |
0.0
|
use_positional_encoding
|
bool
|
Whether to use positional encoding. |
True
|
positional_encoding_type
|
str
|
Type of positional encoding ("sinusoidal" or "learned"). |
"sinusoidal"
|
gradient_checkpointing
|
bool
|
Whether to use gradient checkpointing to save memory. |
False
|
Attributes:
| Name | Type | Description |
|---|---|---|
blocks |
ModuleList
|
Stack of FNO transformer blocks. |
Examples:
>>> model = FNOTransformer(
... hidden_dim=512,
... num_layers=6,
... modes=32,
... max_sequence_length=1024
... )
>>> x = torch.randn(32, 100, 512)
>>> output = model(inputs_embeds=x)
>>> assert output.shape == x.shape
Methods:
| Name | Description |
|---|---|
build_blocks |
Build transformer blocks with FNO layers. |
from_config |
Create model from configuration. |
Source code in spectrans/models/fno_transformer.py
Functions¶
build_blocks ¶
Build transformer blocks with FNO layers.
Returns:
| Type | Description |
|---|---|
ModuleList
|
List of FNO transformer blocks. |
Source code in spectrans/models/fno_transformer.py
from_config
classmethod
¶
from_config(config: FNOTransformerConfig) -> FNOTransformer
Create model from configuration.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
FNOTransformerConfig
|
Model configuration object. |
required |
Returns:
| Type | Description |
|---|---|
FNOTransformer
|
Instantiated model. |
Source code in spectrans/models/fno_transformer.py
GFNet ¶
GFNet(vocab_size: int | None = None, hidden_dim: int = 768, num_layers: int = 12, max_sequence_length: int = 512, num_classes: int | None = None, use_positional_encoding: bool = True, positional_encoding_type: PositionalEncodingType = 'sinusoidal', dropout: float = 0.1, ffn_hidden_dim: int | None = None, norm_eps: float = 1e-12, output_type: OutputHeadType = 'classification', filter_activation: FilterActivationType = 'sigmoid', gradient_checkpointing: bool = False)
Bases: BaseModel
Global Filter Network model with learnable frequency domain filters.
GFNet uses learnable complex filters in the Fourier domain for token mixing, maintaining \(O(n \log n)\) complexity.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vocab_size
|
int | None
|
Size of the vocabulary for token embeddings. If None, expects pre-embedded inputs. |
None
|
hidden_dim
|
int
|
Hidden dimension size. Default is 768. |
768
|
num_layers
|
int
|
Number of GFNet layers. Default is 12. |
12
|
max_sequence_length
|
int
|
Maximum sequence length. Default is 512. |
512
|
num_classes
|
int | None
|
Number of output classes for classification. Default is None. |
None
|
use_positional_encoding
|
bool
|
Whether to use positional encoding. Default is True. |
True
|
positional_encoding_type
|
str
|
Type of positional encoding: 'sinusoidal' or 'learned'. Default is 'sinusoidal'. |
'sinusoidal'
|
dropout
|
float
|
Dropout probability. Default is 0.1. |
0.1
|
ffn_hidden_dim
|
int | None
|
Hidden dimension for FFN. If None, defaults to 4 * hidden_dim. |
None
|
norm_eps
|
float
|
Epsilon for layer normalization. Default is 1e-12. |
1e-12
|
output_type
|
str
|
Type of output head: 'classification', 'regression', 'sequence', or 'none'. Default is 'classification'. |
'classification'
|
filter_activation
|
str
|
Activation function for filters: 'sigmoid' or 'tanh'. Default is 'sigmoid'. |
'sigmoid'
|
gradient_checkpointing
|
bool
|
Whether to use gradient checkpointing. Default is False. |
False
|
Attributes:
| Name | Type | Description |
|---|---|---|
filter_activation |
str
|
Activation function used for filters. |
blocks |
ModuleList
|
List of GFNet transformer blocks. |
Methods:
| Name | Description |
|---|---|
build_blocks |
Build GFNet transformer blocks with global filter mixing. |
from_config |
Create GFNet model from configuration. |
Source code in spectrans/models/gfnet.py
Functions¶
build_blocks ¶
Build GFNet transformer blocks with global filter mixing.
Returns:
| Type | Description |
|---|---|
ModuleList
|
List of GFNet transformer blocks. |
Source code in spectrans/models/gfnet.py
from_config
classmethod
¶
from_config(config: GFNetModelConfig) -> GFNet
Create GFNet model from configuration.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
GFNetModelConfig
|
Configuration object with model parameters. |
required |
Returns:
| Type | Description |
|---|---|
GFNet
|
Configured GFNet model. |
Source code in spectrans/models/gfnet.py
GFNetEncoder ¶
GFNetEncoder(hidden_dim: int = 768, num_layers: int = 12, max_sequence_length: int = 512, use_positional_encoding: bool = True, positional_encoding_type: PositionalEncodingType = 'sinusoidal', dropout: float = 0.1, ffn_hidden_dim: int | None = None, norm_eps: float = 1e-12, filter_activation: FilterActivationType = 'sigmoid', gradient_checkpointing: bool = False)
Bases: GFNet
Encoder-only GFNet model for representation learning.
This variant of GFNet is designed for tasks that require extracting representations rather than making predictions. It returns the hidden states from the final layer without any task-specific head.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
hidden_dim
|
int
|
Hidden dimension size. Default is 768. |
768
|
num_layers
|
int
|
Number of GFNet layers. Default is 12. |
12
|
max_sequence_length
|
int
|
Maximum sequence length. Default is 512. |
512
|
use_positional_encoding
|
bool
|
Whether to use positional encoding. Default is True. |
True
|
positional_encoding_type
|
str
|
Type of positional encoding. Default is 'sinusoidal'. |
'sinusoidal'
|
dropout
|
float
|
Dropout probability. Default is 0.1. |
0.1
|
ffn_hidden_dim
|
int | None
|
Hidden dimension for FFN. If None, defaults to 4 * hidden_dim. |
None
|
norm_eps
|
float
|
Epsilon for layer normalization. Default is 1e-12. |
1e-12
|
filter_activation
|
str
|
Activation function for filters. Default is 'sigmoid'. |
'sigmoid'
|
gradient_checkpointing
|
bool
|
Whether to use gradient checkpointing. Default is False. |
False
|
Source code in spectrans/models/gfnet.py
AlternatingTransformer ¶
AlternatingTransformer(vocab_size: int | None = None, hidden_dim: int = 768, num_layers: int = 12, max_sequence_length: int = 512, layer1_type: str = 'fourier', layer2_type: str = 'attention', layer1_config: dict | None = None, layer2_config: dict | None = None, num_classes: int | None = None, use_positional_encoding: bool = True, positional_encoding_type: PositionalEncodingType = 'sinusoidal', dropout: float = 0.1, ffn_hidden_dim: int | None = None, norm_eps: float = 1e-12, output_type: OutputHeadType = 'classification', gradient_checkpointing: bool = False)
Bases: BaseModel
Transformer that strictly alternates between two mixing strategies.
A simplified hybrid model that alternates between exactly two types of mixing layers following a strict pattern: layer1_type for even-indexed layers, layer2_type for odd-indexed layers. This design enables controlled comparisons between different mixing strategies.
For \(L\) layers, the alternation follows:
Each layer applies the mixing operation with residual connection:
followed by the standard feedforward block with another residual connection.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vocab_size
|
int | None
|
Size of the vocabulary for token embeddings. |
None
|
hidden_dim
|
int
|
Hidden dimension size. |
768
|
num_layers
|
int
|
Number of transformer blocks. |
12
|
max_sequence_length
|
int
|
Maximum sequence length. |
512
|
layer1_type
|
str
|
Type of first mixing layer. |
'fourier'
|
layer2_type
|
str
|
Type of second mixing layer. |
'attention'
|
layer1_config
|
dict | None
|
Configuration for first layer type. |
None
|
layer2_config
|
dict | None
|
Configuration for second layer type. |
None
|
num_classes
|
int | None
|
Number of output classes. |
None
|
use_positional_encoding
|
bool
|
Whether to use positional encoding. |
True
|
positional_encoding_type
|
PositionalEncodingType
|
Type of positional encoding. |
'sinusoidal'
|
dropout
|
float
|
Dropout probability. |
0.1
|
ffn_hidden_dim
|
int | None
|
Hidden dimension for FFN. |
None
|
norm_eps
|
float
|
Layer normalization epsilon. |
1e-12
|
output_type
|
OutputHeadType
|
Type of output head. |
'classification'
|
gradient_checkpointing
|
bool
|
Whether to use gradient checkpointing. |
False
|
Methods:
| Name | Description |
|---|---|
build_blocks |
Build alternating transformer blocks. |
Source code in spectrans/models/hybrid.py
Functions¶
build_blocks ¶
Build alternating transformer blocks.
Returns:
| Type | Description |
|---|---|
ModuleList
|
List of alternating transformer blocks. |
Source code in spectrans/models/hybrid.py
HybridEncoder ¶
HybridEncoder(hidden_dim: int = 768, num_layers: int = 12, max_sequence_length: int = 512, spectral_type: str = 'fourier', spatial_type: str = 'attention', alternation_pattern: str = 'even_spectral', num_heads: int = 8, spectral_config: dict | None = None, spatial_config: dict | None = None, use_positional_encoding: bool = True, positional_encoding_type: PositionalEncodingType = 'sinusoidal', dropout: float = 0.1, ffn_hidden_dim: int | None = None, norm_eps: float = 1e-12, gradient_checkpointing: bool = False)
Bases: HybridTransformer
Encoder-only hybrid transformer for representation learning.
This variant returns hidden states without any task-specific head, suitable for feature extraction and representation learning.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
hidden_dim
|
int
|
Hidden dimension size. |
768
|
num_layers
|
int
|
Number of transformer blocks. |
12
|
max_sequence_length
|
int
|
Maximum sequence length. |
512
|
spectral_type
|
str
|
Type of spectral mixing. |
'fourier'
|
spatial_type
|
str
|
Type of spatial mixing. |
'attention'
|
alternation_pattern
|
str
|
Layer alternation pattern. |
'even_spectral'
|
num_heads
|
int
|
Number of attention heads. |
8
|
spectral_config
|
dict | None
|
Spectral layer configuration. |
None
|
spatial_config
|
dict | None
|
Spatial layer configuration. |
None
|
use_positional_encoding
|
bool
|
Whether to use positional encoding. |
True
|
positional_encoding_type
|
PositionalEncodingType
|
Type of positional encoding. |
'sinusoidal'
|
dropout
|
float
|
Dropout probability. |
0.1
|
ffn_hidden_dim
|
int | None
|
Hidden dimension for FFN. |
None
|
norm_eps
|
float
|
Layer normalization epsilon. |
1e-12
|
gradient_checkpointing
|
bool
|
Whether to use gradient checkpointing. |
False
|
Source code in spectrans/models/hybrid.py
HybridTransformer ¶
HybridTransformer(vocab_size: int | None = None, hidden_dim: int = 768, num_layers: int = 12, max_sequence_length: int = 512, spectral_type: str = 'fourier', spatial_type: str = 'attention', alternation_pattern: str = 'even_spectral', num_heads: int = 8, spectral_config: dict | None = None, spatial_config: dict | None = None, num_classes: int | None = None, use_positional_encoding: bool = True, positional_encoding_type: PositionalEncodingType = 'sinusoidal', dropout: float = 0.1, ffn_hidden_dim: int | None = None, norm_eps: float = 1e-12, output_type: OutputHeadType = 'classification', gradient_checkpointing: bool = False)
Bases: BaseModel
Hybrid Spectral-Spatial Transformer model.
Combines spectral and spatial mixing strategies across layers to balance computational efficiency with modeling expressiveness. The model alternates between spectral layers (efficient global mixing) and spatial layers (expressive local modeling) according to configurable patterns.
For a sequence \(\mathbf{X}_0 \in \mathbb{R}^{n \times d}\), the hybrid transformer applies alternating transformations:
Spectral layers (\(\ell\) even for "even_spectral" pattern):
Spatial layers (\(\ell\) odd for "even_spectral" pattern):
where \(\text{LN}(\cdot)\) denotes LayerNorm and each block concludes with:
The spectral mixing operations provide different complexity-accuracy tradeoffs: - Fourier: \(O(n \log n)\) via FFT/IFFT - Wavelet: \(O(n)\) via fast DWT algorithms - AFNO: \(O(k_n k_d d)\) with mode truncation parameters \(k_n, k_d\) - GFNet: \(O(n \log n)\) with learnable spectral filters
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vocab_size
|
int | None
|
Size of the vocabulary for token embeddings. |
None
|
hidden_dim
|
int
|
Hidden dimension size. |
768
|
num_layers
|
int
|
Number of transformer blocks. |
12
|
max_sequence_length
|
int
|
Maximum sequence length. |
512
|
spectral_type
|
str
|
Type of spectral mixing: 'fourier', 'wavelet', 'afno', 'gfnet'. |
'fourier'
|
spatial_type
|
str
|
Type of spatial mixing: 'attention', 'spectral_attention', 'lst'. |
'attention'
|
alternation_pattern
|
str
|
How to alternate: 'even_spectral', 'alternate', 'custom'. |
'even_spectral'
|
num_heads
|
int
|
Number of attention heads for spatial layers. |
8
|
spectral_config
|
dict | None
|
Additional configuration for spectral layers. |
None
|
spatial_config
|
dict | None
|
Additional configuration for spatial layers. |
None
|
num_classes
|
int | None
|
Number of output classes for classification. |
None
|
use_positional_encoding
|
bool
|
Whether to use positional encoding. |
True
|
positional_encoding_type
|
PositionalEncodingType
|
Type of positional encoding. |
'sinusoidal'
|
dropout
|
float
|
Dropout probability. |
0.1
|
ffn_hidden_dim
|
int | None
|
Hidden dimension for FFN. |
None
|
norm_eps
|
float
|
Layer normalization epsilon. |
1e-12
|
output_type
|
OutputHeadType
|
Type of output head. |
'classification'
|
gradient_checkpointing
|
bool
|
Whether to use gradient checkpointing. |
False
|
Attributes:
| Name | Type | Description |
|---|---|---|
spectral_type |
str
|
Type of spectral mixing being used. |
spatial_type |
str
|
Type of spatial mixing being used. |
alternation_pattern |
str
|
The alternation pattern. |
blocks |
ModuleList
|
List of hybrid transformer blocks. |
Methods:
| Name | Description |
|---|---|
build_blocks |
Build hybrid transformer blocks. |
from_config |
Create hybrid transformer from configuration. |
Source code in spectrans/models/hybrid.py
Functions¶
build_blocks ¶
Build hybrid transformer blocks.
Returns:
| Type | Description |
|---|---|
ModuleList
|
List of transformer blocks with alternating mixing strategies. |
Source code in spectrans/models/hybrid.py
from_config
classmethod
¶
from_config(config: HybridModelConfig) -> HybridTransformer
Create hybrid transformer from configuration.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
HybridModelConfig
|
Configuration object with model parameters. |
required |
Returns:
| Type | Description |
|---|---|
HybridTransformer
|
Configured hybrid transformer model. |
Source code in spectrans/models/hybrid.py
StandardAttention ¶
Bases: Module
Standard multi-head self-attention wrapper.
Wraps PyTorch's MultiheadAttention for use as a mixing layer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
hidden_dim
|
int
|
Hidden dimension size. |
required |
num_heads
|
int
|
Number of attention heads. |
8
|
dropout
|
float
|
Dropout probability. |
0.0
|
Methods:
| Name | Description |
|---|---|
forward |
Apply self-attention. |
Source code in spectrans/models/hybrid.py
Functions¶
forward ¶
Apply self-attention.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Tensor
|
Input tensor of shape (batch_size, seq_len, hidden_dim). |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
Output tensor of same shape. |
Source code in spectrans/models/hybrid.py
LSTDecoder ¶
LSTDecoder(vocab_size: int, hidden_dim: int = 512, num_layers: int = 12, max_sequence_length: int = 2048, transform_type: TransformLSTType = 'dst', causal: bool = True, use_conv_bias: bool = True, ffn_hidden_dim: int | None = None, dropout: float = 0.0, use_positional_encoding: bool = True, positional_encoding_type: PositionalEncodingType = 'sinusoidal', gradient_checkpointing: bool = False)
Bases: BaseModel
Decoder LST model with optional causal masking.
This model uses linear spectral transforms with support for causal masking, suitable for autoregressive generation tasks.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vocab_size
|
int
|
Size of the vocabulary. |
required |
hidden_dim
|
int
|
Hidden dimension size. |
512
|
num_layers
|
int
|
Number of transformer blocks. |
12
|
max_sequence_length
|
int
|
Maximum sequence length. |
2048
|
transform_type
|
TransformLSTType
|
Type of spectral transform (DST is preferred for causal). |
"dst"
|
causal
|
bool
|
Whether to use causal masking. |
True
|
use_conv_bias
|
bool
|
Use bias in spectral convolution. |
True
|
ffn_hidden_dim
|
int | None
|
FFN hidden dimension. |
None
|
dropout
|
float
|
Dropout probability. |
0.0
|
use_positional_encoding
|
bool
|
Use positional encoding. |
True
|
positional_encoding_type
|
str
|
Positional encoding type. |
"sinusoidal"
|
gradient_checkpointing
|
bool
|
Use gradient checkpointing. |
False
|
Examples:
>>> decoder = LSTDecoder(
... vocab_size=10000,
... hidden_dim=512,
... num_layers=12,
... transform_type="dst",
... causal=True,
... max_sequence_length=2048
... )
>>> input_ids = torch.randint(0, 10000, (32, 100))
>>> logits = decoder(input_ids)
>>> assert logits.shape == (32, 100, 10000)
Methods:
| Name | Description |
|---|---|
build_blocks |
Build decoder blocks with causal LST layers. |
forward |
Forward pass through the decoder. |
Source code in spectrans/models/lst.py
Functions¶
build_blocks ¶
Build decoder blocks with causal LST layers.
Returns:
| Type | Description |
|---|---|
ModuleList
|
List of causal LST decoder blocks. |
Source code in spectrans/models/lst.py
forward ¶
forward(input_ids: Tensor | None = None, inputs_embeds: Tensor | None = None, attention_mask: Tensor | None = None) -> Tensor
Forward pass through the decoder.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input_ids
|
Tensor | None
|
Input token IDs of shape (batch_size, sequence_length). |
None
|
inputs_embeds
|
Tensor | None
|
Pre-embedded inputs of shape (batch_size, sequence_length, hidden_dim). |
None
|
Returns:
| Type | Description |
|---|---|
Tensor
|
Language modeling logits of shape (batch_size, sequence_length, vocab_size). |
Source code in spectrans/models/lst.py
LSTEncoder ¶
LSTEncoder(vocab_size: int | None = None, hidden_dim: int = 512, num_layers: int = 6, max_sequence_length: int = 1024, transform_type: TransformLSTType = 'dct', use_conv_bias: bool = True, ffn_hidden_dim: int | None = None, dropout: float = 0.0, use_positional_encoding: bool = True, positional_encoding_type: PositionalEncodingType = 'sinusoidal')
Bases: BaseModel
Encoder-only LST model for representation learning.
This model uses linear spectral transforms without a classification head, suitable for generating embeddings or as a component in larger architectures.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vocab_size
|
int | None
|
Size of the vocabulary for token embeddings. |
None
|
hidden_dim
|
int
|
Hidden dimension size. |
512
|
num_layers
|
int
|
Number of transformer blocks. |
6
|
max_sequence_length
|
int
|
Maximum sequence length. |
1024
|
transform_type
|
TransformLSTType
|
Type of spectral transform. |
"dct"
|
use_conv_bias
|
bool
|
Use bias in spectral convolution. |
True
|
ffn_hidden_dim
|
int | None
|
FFN hidden dimension. |
None
|
dropout
|
float
|
Dropout probability. |
0.0
|
use_positional_encoding
|
bool
|
Use positional encoding. |
True
|
positional_encoding_type
|
str
|
Positional encoding type. |
"sinusoidal"
|
Methods:
| Name | Description |
|---|---|
build_blocks |
Build encoder blocks with LST layers. |
Source code in spectrans/models/lst.py
Functions¶
build_blocks ¶
Build encoder blocks with LST layers.
Returns:
| Type | Description |
|---|---|
ModuleList
|
List of LST encoder blocks. |
Source code in spectrans/models/lst.py
LSTTransformer ¶
LSTTransformer(vocab_size: int | None = None, hidden_dim: int = 512, num_layers: int = 6, max_sequence_length: int = 1024, transform_type: TransformLSTType = 'dct', use_conv_bias: bool = True, num_classes: int | None = None, ffn_hidden_dim: int | None = None, dropout: float = 0.0, use_positional_encoding: bool = True, positional_encoding_type: PositionalEncodingType = 'sinusoidal', gradient_checkpointing: bool = False)
Bases: BaseModel
Linear Spectral Transform transformer model.
This model uses linear spectral transforms (DCT/DST/Hadamard) for sequence mixing, achieving O(n log n) complexity through fast transform algorithms. The model applies learned transformations in the spectral domain for efficient global token interactions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vocab_size
|
int | None
|
Size of the vocabulary for token embeddings. If None, expects pre-embedded inputs. |
None
|
hidden_dim
|
int
|
Hidden dimension size for the model. |
512
|
num_layers
|
int
|
Number of transformer blocks. |
6
|
max_sequence_length
|
int
|
Maximum sequence length the model can process. |
1024
|
transform_type
|
TransformLSTType
|
Type of spectral transform to use. |
"dct"
|
use_conv_bias
|
bool
|
Whether to use bias in spectral convolution. |
True
|
num_classes
|
int | None
|
Number of output classes for classification. |
None
|
ffn_hidden_dim
|
int | None
|
Hidden dimension of the feedforward network. Default is 4 * hidden_dim. |
None
|
dropout
|
float
|
Dropout probability. |
0.0
|
use_positional_encoding
|
bool
|
Whether to use positional encoding. |
True
|
positional_encoding_type
|
PositionalEncodingType
|
Type of positional encoding ("sinusoidal", "learned", "rotary", "alibi", or "none"). |
"sinusoidal"
|
gradient_checkpointing
|
bool
|
Whether to use gradient checkpointing to save memory. |
False
|
Attributes:
| Name | Type | Description |
|---|---|---|
blocks |
ModuleList
|
Stack of LST transformer blocks. |
Examples:
>>> model = LSTTransformer(
... hidden_dim=512,
... num_layers=6,
... transform_type="dct",
... max_sequence_length=1024
... )
>>> x = torch.randn(32, 100, 512)
>>> output = model(inputs_embeds=x)
>>> assert output.shape == x.shape
Methods:
| Name | Description |
|---|---|
build_blocks |
Build transformer blocks with LST layers. |
from_config |
Create model from configuration. |
Source code in spectrans/models/lst.py
Functions¶
build_blocks ¶
Build transformer blocks with LST layers.
Returns:
| Type | Description |
|---|---|
ModuleList
|
List of LST transformer blocks. |
Source code in spectrans/models/lst.py
from_config
classmethod
¶
from_config(config: LSTModelConfig) -> LSTTransformer
Create model from configuration.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
LSTModelConfig
|
Model configuration object. |
required |
Returns:
| Type | Description |
|---|---|
LSTTransformer
|
Configured model instance. |
Source code in spectrans/models/lst.py
PerformerTransformer ¶
PerformerTransformer(vocab_size: int | None = None, hidden_dim: int = 512, num_layers: int = 6, max_sequence_length: int = 1024, num_heads: int = 8, num_features: int | None = None, num_classes: int | None = None, ffn_hidden_dim: int | None = None, dropout: float = 0.0, use_positional_encoding: bool = True, positional_encoding_type: PositionalEncodingType = 'sinusoidal', gradient_checkpointing: bool = False)
Bases: BaseModel
Performer transformer with positive orthogonal random features.
This model implements the Performer architecture which uses positive orthogonal random features (PORF) to approximate the softmax kernel with improved variance reduction compared to standard RFF.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vocab_size
|
int | None
|
Vocabulary size. |
None
|
hidden_dim
|
int
|
Hidden dimension. |
512
|
num_layers
|
int
|
Number of layers. |
6
|
max_sequence_length
|
int
|
Maximum sequence length. |
1024
|
num_heads
|
int
|
Number of heads. |
8
|
num_features
|
int | None
|
Number of random features. |
None
|
num_classes
|
int | None
|
Number of classes. |
None
|
ffn_hidden_dim
|
int | None
|
FFN dimension. |
None
|
dropout
|
float
|
Dropout rate. |
0.0
|
use_positional_encoding
|
bool
|
Use positional encoding. |
True
|
positional_encoding_type
|
str
|
Positional encoding type. |
"sinusoidal"
|
gradient_checkpointing
|
bool
|
Use gradient checkpointing. |
False
|
Examples:
>>> performer = PerformerTransformer(
... hidden_dim=512,
... num_layers=6,
... num_heads=8,
... num_features=256,
... max_sequence_length=1024
... )
>>> x = torch.randn(32, 100, 512)
>>> output = performer(inputs_embeds=x)
Methods:
| Name | Description |
|---|---|
build_blocks |
Build Performer blocks with orthogonal features. |
Source code in spectrans/models/spectral_attention.py
Functions¶
build_blocks ¶
Build Performer blocks with orthogonal features.
Returns:
| Type | Description |
|---|---|
ModuleList
|
List of Performer blocks. |
Source code in spectrans/models/spectral_attention.py
SpectralAttentionEncoder ¶
SpectralAttentionEncoder(vocab_size: int | None = None, hidden_dim: int = 512, num_layers: int = 6, max_sequence_length: int = 1024, num_heads: int = 8, num_features: int | None = None, kernel_type: KernelType = 'softmax', use_orthogonal: bool = False, ffn_hidden_dim: int | None = None, dropout: float = 0.0, use_positional_encoding: bool = True, positional_encoding_type: PositionalEncodingType = 'sinusoidal')
Bases: BaseModel
Encoder-only spectral attention model for representation learning.
This model uses spectral attention layers without a classification head, suitable for generating embeddings or as a component in larger architectures.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vocab_size
|
int | None
|
Size of the vocabulary for token embeddings. |
None
|
hidden_dim
|
int
|
Hidden dimension size. |
512
|
num_layers
|
int
|
Number of transformer blocks. |
6
|
max_sequence_length
|
int
|
Maximum sequence length. |
1024
|
num_heads
|
int
|
Number of attention heads. |
8
|
num_features
|
int | None
|
Number of random features. |
None
|
kernel_type
|
KernelType
|
Kernel type. |
"softmax"
|
use_orthogonal
|
bool
|
Use orthogonal features. |
False
|
ffn_hidden_dim
|
int | None
|
FFN hidden dimension. |
None
|
dropout
|
float
|
Dropout probability. |
0.0
|
use_positional_encoding
|
bool
|
Use positional encoding. |
True
|
positional_encoding_type
|
str
|
Positional encoding type. |
"sinusoidal"
|
Methods:
| Name | Description |
|---|---|
build_blocks |
Build encoder blocks with spectral attention. |
Source code in spectrans/models/spectral_attention.py
Functions¶
build_blocks ¶
Build encoder blocks with spectral attention.
Returns:
| Type | Description |
|---|---|
ModuleList
|
List of spectral attention blocks. |
Source code in spectrans/models/spectral_attention.py
SpectralAttentionTransformer ¶
SpectralAttentionTransformer(vocab_size: int | None = None, hidden_dim: int = 512, num_layers: int = 6, max_sequence_length: int = 1024, num_heads: int = 8, num_features: int | None = None, kernel_type: KernelType = 'softmax', use_orthogonal: bool = False, num_classes: int | None = None, ffn_hidden_dim: int | None = None, dropout: float = 0.0, use_positional_encoding: bool = True, positional_encoding_type: PositionalEncodingType = 'sinusoidal', gradient_checkpointing: bool = False)
Bases: BaseModel
Spectral Attention transformer using Random Fourier Features.
This model uses spectral attention layers with RFF approximation to achieve linear complexity attention computation. The model maintains the expressive power of standard transformers while being efficient for long sequences.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vocab_size
|
int | None
|
Size of the vocabulary for token embeddings. If None, expects pre-embedded inputs. |
None
|
hidden_dim
|
int
|
Hidden dimension size for the model. |
512
|
num_layers
|
int
|
Number of transformer blocks. |
6
|
max_sequence_length
|
int
|
Maximum sequence length the model can process. |
1024
|
num_heads
|
int
|
Number of attention heads. |
8
|
num_features
|
int | None
|
Number of random features for RFF approximation. If None, uses hidden_dim. |
None
|
kernel_type
|
KernelType
|
Type of kernel to approximate. |
"softmax"
|
use_orthogonal
|
bool
|
Whether to use orthogonal random features. |
False
|
num_classes
|
int | None
|
Number of output classes for classification. |
None
|
ffn_hidden_dim
|
int | None
|
Hidden dimension of the feedforward network. Default is 4 * hidden_dim. |
None
|
dropout
|
float
|
Dropout probability. |
0.0
|
use_positional_encoding
|
bool
|
Whether to use positional encoding. |
True
|
positional_encoding_type
|
str
|
Type of positional encoding ("sinusoidal" or "learned"). |
"sinusoidal"
|
gradient_checkpointing
|
bool
|
Whether to use gradient checkpointing to save memory. |
False
|
Attributes:
| Name | Type | Description |
|---|---|---|
blocks |
ModuleList
|
Stack of spectral attention transformer blocks. |
Examples:
>>> model = SpectralAttentionTransformer(
... hidden_dim=512,
... num_layers=6,
... num_heads=8,
... num_features=256,
... max_sequence_length=1024
... )
>>> x = torch.randn(32, 100, 512)
>>> output = model(inputs_embeds=x)
>>> assert output.shape == x.shape
Methods:
| Name | Description |
|---|---|
build_blocks |
Build transformer blocks with spectral attention layers. |
from_config |
Create model from configuration. |
Source code in spectrans/models/spectral_attention.py
Functions¶
build_blocks ¶
Build transformer blocks with spectral attention layers.
Returns:
| Type | Description |
|---|---|
ModuleList
|
List of spectral attention transformer blocks. |
Source code in spectrans/models/spectral_attention.py
from_config
classmethod
¶
from_config(config: SpectralAttentionModelConfig) -> SpectralAttentionTransformer
Create model from configuration.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
SpectralAttentionModelConfig
|
Model configuration object. |
required |
Returns:
| Type | Description |
|---|---|
SpectralAttentionTransformer
|
Configured model instance. |
Source code in spectrans/models/spectral_attention.py
WaveletDecoder ¶
WaveletDecoder(vocab_size: int, hidden_dim: int = 768, num_layers: int = 12, max_sequence_length: int = 512, wavelet: WaveletType = 'db4', levels: int = 2, mixing_mode: str = 'pointwise', use_positional_encoding: bool = True, positional_encoding_type: PositionalEncodingType = 'sinusoidal', dropout: float = 0.1, ffn_hidden_dim: int | None = None, norm_eps: float = 1e-12, gradient_checkpointing: bool = False)
Bases: WaveletTransformer
Decoder wavelet transformer for sequence generation.
This variant uses causal wavelet processing suitable for autoregressive generation tasks. The wavelet decomposition is modified to respect causality constraints.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vocab_size
|
int
|
Size of the vocabulary for token generation. |
required |
hidden_dim
|
int
|
Hidden dimension size. |
768
|
num_layers
|
int
|
Number of wavelet transformer blocks. |
12
|
max_sequence_length
|
int
|
Maximum sequence length. |
512
|
wavelet
|
WaveletType
|
Type of wavelet to use. |
'db4'
|
levels
|
int
|
Number of decomposition levels (typically lower for causality). |
2
|
mixing_mode
|
str
|
Coefficient mixing strategy. |
'pointwise'
|
use_positional_encoding
|
bool
|
Whether to use positional encoding. |
True
|
positional_encoding_type
|
PositionalEncodingType
|
Type of positional encoding. |
'sinusoidal'
|
dropout
|
float
|
Dropout probability. |
0.1
|
ffn_hidden_dim
|
int | None
|
Hidden dimension for FFN. |
None
|
norm_eps
|
float
|
Layer normalization epsilon. |
1e-12
|
gradient_checkpointing
|
bool
|
Whether to use gradient checkpointing. |
False
|
Source code in spectrans/models/wavenet_transformer.py
WaveletEncoder ¶
WaveletEncoder(hidden_dim: int = 768, num_layers: int = 12, max_sequence_length: int = 512, wavelet: WaveletType = 'db4', levels: int = 3, mixing_mode: str = 'pointwise', use_positional_encoding: bool = True, positional_encoding_type: PositionalEncodingType = 'sinusoidal', dropout: float = 0.1, ffn_hidden_dim: int | None = None, norm_eps: float = 1e-12, gradient_checkpointing: bool = False)
Bases: WaveletTransformer
Encoder-only wavelet transformer for representation learning.
This variant is designed for extracting representations from sequences using wavelet-based mixing, without any task-specific output head.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
hidden_dim
|
int
|
Hidden dimension size. |
768
|
num_layers
|
int
|
Number of wavelet transformer blocks. |
12
|
max_sequence_length
|
int
|
Maximum sequence length. |
512
|
wavelet
|
WaveletType
|
Type of wavelet to use. |
'db4'
|
levels
|
int
|
Number of decomposition levels. |
3
|
mixing_mode
|
str
|
Coefficient mixing strategy. |
'pointwise'
|
use_positional_encoding
|
bool
|
Whether to use positional encoding. |
True
|
positional_encoding_type
|
PositionalEncodingType
|
Type of positional encoding. |
'sinusoidal'
|
dropout
|
float
|
Dropout probability. |
0.1
|
ffn_hidden_dim
|
int | None
|
Hidden dimension for FFN. |
None
|
norm_eps
|
float
|
Layer normalization epsilon. |
1e-12
|
gradient_checkpointing
|
bool
|
Whether to use gradient checkpointing. |
False
|
Source code in spectrans/models/wavenet_transformer.py
WaveletTransformer ¶
WaveletTransformer(vocab_size: int | None = None, hidden_dim: int = 768, num_layers: int = 12, max_sequence_length: int = 512, wavelet: WaveletType = 'db4', levels: int = 3, mixing_mode: str = 'pointwise', num_classes: int | None = None, use_positional_encoding: bool = True, positional_encoding_type: PositionalEncodingType = 'sinusoidal', dropout: float = 0.1, ffn_hidden_dim: int | None = None, norm_eps: float = 1e-12, output_type: OutputHeadType = 'classification', gradient_checkpointing: bool = False)
Bases: BaseModel
Wavelet transformer with DWT-based sequence mixing.
This model replaces attention mechanisms with discrete wavelet transforms, providing multi-resolution analysis of sequences with \(O(n)\) complexity per channel. The DWT decomposes input sequences into approximation and detail coefficients at multiple scales, representing both local transients and global structure.
The wavelet mixing operation applies the DWT along the sequence dimension for each channel independently, processes the coefficients through learnable transformations, and reconstructs the sequence via the inverse DWT (IDWT). Perfect reconstruction is maintained when no coefficient modification occurs.
For input :math:\mathbf{X} \in \mathbb{R}^{n \times d}, each channel undergoes:
.. math:: \mathbf{c} = \text{DWT}J(\mathbf{X} i \in [1,d]}) \quad \text{for
.. math:: \tilde{\mathbf{c}} = f_{\theta}(\mathbf{c})
.. math:: \mathbf{Y}_{:,i} = \text{IDWT}_J(\tilde{\mathbf{c}})
where :math:f_{\theta} represents learnable coefficient transformations and :math:J
is the number of decomposition levels.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vocab_size
|
int | None
|
Size of the vocabulary for token embeddings. If None, expects pre-embedded inputs. |
None
|
hidden_dim
|
int
|
Hidden dimension size. |
768
|
num_layers
|
int
|
Number of wavelet transformer blocks. |
12
|
max_sequence_length
|
int
|
Maximum sequence length the model can process. |
512
|
wavelet
|
WaveletType
|
Type of wavelet to use (e.g., 'db4', 'sym6', 'coif3'). |
'db4'
|
levels
|
int
|
Number of wavelet decomposition levels. |
3
|
mixing_mode
|
str
|
How to mix wavelet coefficients: 'pointwise', 'channel', or 'level'. |
'pointwise'
|
num_classes
|
int | None
|
Number of output classes for classification. |
None
|
use_positional_encoding
|
bool
|
Whether to use positional encoding. |
True
|
positional_encoding_type
|
PositionalEncodingType
|
Type of positional encoding. |
'sinusoidal'
|
dropout
|
float
|
Dropout probability. |
0.1
|
ffn_hidden_dim
|
int | None
|
Hidden dimension for FFN. If None, defaults to 4 * hidden_dim. |
None
|
norm_eps
|
float
|
Epsilon for layer normalization. |
1e-12
|
output_type
|
OutputHeadType
|
Type of output head. |
'classification'
|
gradient_checkpointing
|
bool
|
Whether to use gradient checkpointing for memory efficiency. |
False
|
Attributes:
| Name | Type | Description |
|---|---|---|
wavelet |
WaveletType
|
The wavelet family being used. |
levels |
int
|
Number of decomposition levels. |
mixing_mode |
str
|
Coefficient mixing strategy. |
blocks |
ModuleList
|
List of wavelet transformer blocks. |
Methods:
| Name | Description |
|---|---|
build_blocks |
Build wavelet transformer blocks. |
from_config |
Create wavelet transformer from configuration. |
Source code in spectrans/models/wavenet_transformer.py
Functions¶
build_blocks ¶
Build wavelet transformer blocks.
Returns:
| Type | Description |
|---|---|
ModuleList
|
List of wavelet transformer blocks with DWT mixing layers. |
Source code in spectrans/models/wavenet_transformer.py
from_config
classmethod
¶
from_config(config: WaveletTransformerConfig) -> WaveletTransformer
Create wavelet transformer from configuration.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
WaveletTransformerConfig
|
Configuration object with model parameters. |
required |
Returns:
| Type | Description |
|---|---|
WaveletTransformer
|
Configured wavelet transformer model. |