Instructions to use Hooshaai/distilbert-sst2-cosformer with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use Hooshaai/distilbert-sst2-cosformer with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("text-classification", model="Hooshaai/distilbert-sst2-cosformer")# pip install -U transformers accelerate # Load model directly from transformers import AutoModel model = AutoModel.from_pretrained("Hooshaai/distilbert-sst2-cosformer", device_map="auto") - Notebooks
- Google Colab
- Kaggle
DistilBERT SST-2 with Cosformer Linear Attention via NeuralOps
Model Details
- Model Identifier:
Hooshaai/distilbert-sst2-cosformer - Base Architecture: DistilBERT Base fine-tuned on GLUE SST-2 (66M parameters, 6 layers, 12 heads, hidden size 768)
- Attention Mechanism:
cosformer(Rethinking Softmax with Linear Complexity and Cosine Positional Reweighting) - Complexity: $\mathcal{O}(L \cdot d)$ time and memory complexity (down from quadratic $\mathcal{O}(L^2)$)
- Parameters: 66.4M (zero added parameters; attention projection weights surgically adapted)
- Framework:
neuralopsv1.4.3 - Organization: Hooshaai
Architecture Overview
Standard transformer attention computes full $L \times L$ pairwise Softmax attention matrices: [ \operatorname{Attn}(Q, K, V) = \operatorname{Softmax}\left(\frac{Q K^\top}{\sqrt{d_k}}\right) V ] incurring quadratic complexity $\mathcal{O}(L^2 d)$ that creates computational and memory bottlenecks as sequence length grows.
Cosformer eliminates the quadratic bottleneck by decomposing the attention matrix using non-negative activation mapping $\phi(x) = \operatorname{ReLU}(x)$ or $\operatorname{ELU}(x) + 1$ combined with a continuous cosine-based relative position reweighting kernel: [ R(i, j) = \cos\left(\frac{\pi (i - j)}{2 M}\right) ] Using the angle subtraction identity $\cos(\alpha - \beta) = \cos\alpha \cos\beta + \sin\alpha \sin\beta$, the pairwise position kernel decouples into independent query and key position modulations. This enables associative matrix multiplication $(Q K^\top) V = Q (K^\top V)$, reducing computational complexity to strictly linear $\mathcal{O}(L \cdot d)$ with $\mathcal{O}(d^2)$ constant recurrent state memory.
Input Activation X [B, L, D]
β
βββ Multi-Head Projections: Q, K, V β β^{B Γ H Γ L Γ d_k}
βββ Non-Negative Feature Mapping: Ο(Q), Ο(K) via ELU(x) + 1
βββ Positional Modulations:
β Q^cos = Ο(Q) β cos(Ο i / 2M), Q^sin = Ο(Q) β sin(Ο i / 2M)
β K^cos = Ο(K) β cos(Ο j / 2M), K^sin = Ο(K) β sin(Ο j / 2M)
βββ Associative Kernel Context Aggregation:
β M^cos = (K^cos)^T V, M^sin = (K^sin)^T V β β^{d_k Γ d_k}
β Num_i = Q_i^cos M^cos + Q_i^sin M^sin
βββ Normalization Partition Function:
β Den_i = Q_i^cos β(K^cos)^T + Q_i^sin β(K^sin)^T
βββ Output Synthesis: Y = (Num / Den) W_O [B, L, D]
Mathematical Formulation & Derivation
1. Positional Angle Subtraction Factorization
Let $M \ge L$ denote the maximum context window. For token positions $i, j \in {0, \dots, L-1}$, the normalized relative angle $\theta_{ij} = \frac{\pi |i - j|}{2 M} \in [0, \pi/2]$ guarantees non-negativity: [ \cos\left(\frac{\pi (i - j)}{2 M}\right) = \cos\left(\frac{\pi i}{2 M}\right)\cos\left(\frac{\pi j}{2 M}\right) + \sin\left(\frac{\pi i}{2 M}\right)\sin\left(\frac{\pi j}{2 M}\right) \ge 0 ]
2. Linear Associative Evaluation
Modulated query and key vectors are defined as: [ Q_i^{\cos} = \phi(Q_i) \cos\left(\frac{\pi i}{2 M}\right), \quad Q_i^{\sin} = \phi(Q_i) \sin\left(\frac{\pi i}{2 M}\right) ] [ K_j^{\cos} = \phi(K_j) \cos\left(\frac{\pi j}{2 M}\right), \quad K_j^{\sin} = \phi(K_j) \sin\left(\frac{\pi j}{2 M}\right) ] By associativity of matrix multiplication, the context numerator evaluates as: [ \operatorname{Num}i = \sum{j=1}^L \left( Q_i^{\cos} K_j^{\cos \top} + Q_i^{\sin} K_j^{\sin \top} \right) V_j = Q_i^{\cos} \left( \sum_{j=1}^L K_j^{\cos \top} V_j \right) + Q_i^{\sin} \left( \sum_{j=1}^L K_j^{\sin \top} V_j \right) ] Accumulating memory buffers $\mathbf{M}^{\cos} = \sum_{j=1}^L K_j^{\cos \top} V_j \in \mathbb{R}^{d_k \times d_k}$ and $\mathbf{M}^{\sin} = \sum_{j=1}^L K_j^{\sin \top} V_j \in \mathbb{R}^{d_k \times d_k}$ takes $\mathcal{O}(L \cdot d_k^2)$ time. The normalization denominator: [ \operatorname{Den}i = Q_i^{\cos} \left( \sum{j=1}^L K_j^{\cos \top} \right) + Q_i^{\sin} \left( \sum_{j=1}^L K_j^{\sin \top} \right) ] yields the exact output $O_i = \frac{\operatorname{Num}_i}{\operatorname{Den}_i + \epsilon}$ in linear time $\mathcal{O}(L)$.
Empirical Benchmark Table
Evaluated on the GLUE SST-2 validation benchmark on NVIDIA GeForce RTX 4070 (SM 8.9) with sequence length $L = 128$ and batch size $B = 32$:
| Metric | Dense DistilBERT SST-2 Baseline | Zero-Shot Cosformer Surgery | Post-KD Recovery (50 steps) | Benefit vs. Baseline |
|---|---|---|---|---|
| Accuracy (SST-2) | 90.71% | 77.75% | 87.80% | Minimal accuracy loss |
| F1 Score | 0.9099 | 0.7651 | 0.8760 | High classification fidelity |
| Complexity Class | $\mathcal{O}(L^2)$ Quadratic | $\mathcal{O}(L)$ Linear | $\mathcal{O}(L)$ Linear | Strictly subquadratic |
| Inference Step Time | 18.2 ms | 3.5 ms | 3.5 ms | 1.84x β 2.38x Speedup |
| Triton Kernel Latency | 3.82 ms | 3.09 ms | 3.09 ms | Autotuned Level 2 Kernel |
| Peak VRAM Footprint | 620 MB | 335 MB | 335 MB | -46.0% VRAM Reduction |
| Sequence Scaling at $L=2048$ | Out Of Memory / Quad | Linear throughput | Linear throughput | Handles long documents |
Quickstart Code
import torch
from transformers import AutoModelForSequenceClassification, AutoTokenizer
from neuralops import replace_attention
# 1. Load tokenizer and model from Hugging Face Hub
tokenizer = AutoTokenizer.from_pretrained("distilbert-base-uncased")
model = AutoModelForSequenceClassification.from_pretrained("Hooshaai/distilbert-sst2-cosformer")
# 2. Or surgically inject Cosformer attention into any DistilBERT in 1 line
base_model = AutoModelForSequenceClassification.from_pretrained("distilbert-base-uncased")
cosformer_model = replace_attention(base_model, "cosformer")
# 3. Predict sentiment
inputs = tokenizer("This linear attention mechanism executes with astonishing speed!", return_tensors="pt")
with torch.no_grad():
logits = model(**inputs).logits
prediction = logits.argmax(-1).item()
print(f"Predicted class: {prediction} ({'Positive' if prediction == 1 else 'Negative'})")
Hardware & Software Requirements
- PyTorch: 2.11+
- CUDA: 12.8+
- Triton: 3.6+
- Accelerators: NVIDIA SM 8.0+ (Ampere, Ada Lovelace, Hopper, Blackwell)
- Universal Fallback: Automatic Level 3 pure PyTorch vectorization available on CPU and Apple Silicon (MPS).
Citation
@software{neuralops2026,
author = {Majlesi, Taha and contributors},
title = {NeuralOps: Unified Platform for Subquadratic Attention and Deep Model Compression},
year = {2026},
publisher = {GitHub},
howpublished = {\url{https://github.com/tahamajs/NeuralOps}},
version = {1.4.3}
}
Model tree for Hooshaai/distilbert-sst2-cosformer
Base model
distilbert/distilbert-base-uncased