Skip to content

In development. Expect rough edges and changes. The source code is not public yet.

Hugging Face 6 min read Deep dive hardMaster

Nyströmformer: Approximating self-attention in linear time and memory via the Nyström method

Nyströmformer replaces quadratic softmax self-attention with an O(n) approximation built on the Nyström method, which reconstructs a matrix from a sampled subset of its rows and columns via a Moore-Penrose pseudoinverse. Sampling columns of the softmax matrix directly is impossible because each entry needs its full row for normalization. The model therefore builds landmarks from segment means of queries and keys and composes three small softmax kernels (n×m, m×m pseudoinverse, m×n), so QKᵀ is never computed. The paper reports 32–64 landmarks staying competitive at sequence lengths of 4096–8192. A walkthrough of the Hugging Face PyTorch implementation follows, along with masked-language-modeling usage of the released checkpoints.

Assumes: Transformer self-attentionlinear algebra and matrix pseudoinversePyTorch basics

Related posts

  • Perceiver IO: a scalable, fully-attentional model that works on any modality

    Hugging Face hardMaster

    Perceiver IO avoids the quadratic cost of self-attention by running attention over a small fixed set of latent variables (e.g. 256×1280). Inputs of any modality enter only through cross-attention, where the latents supply the queries, and outputs are produced by cross-attention with trainable decoder queries. Walking through the Hugging Face Transformers implementation, the post traces tensor shapes through preprocessors, the latent encoder, decoders and postprocessors. Examples cover byte-level text classification and masked language modeling, which reaches 81.8 on GLUE without a tokenizer, and image classification with learned, Fourier or convolutional preprocessing.

  • Hugging Face Reads, Feb. 2021 - Long-range Transformers

    Hugging Face hardMaster

    Long-range attention methods attack the quadratic memory and time cost of self-attention in sequence length, and four papers illustrate the main families. Longformer combines dilated windowed and global attention to reach linear cost and serves as a drop-in replacement, so pretrained checkpoints can be adapted to long inputs. Compressive Transformer extends Transformer-XL with a compressed memory of past activations, using pooling or convolution, to model longer dependencies. Linformer exploits the low rank of attention matrices to project the sequence dimension and get linear complexity. Each paper comes with complexity analysis, main findings and open questions, such as TPU indexing costs, hyperparameter brittleness and long-target seq2seq tasks.

  • Fine-tuning LLMs to 1.58bit: extreme quantization made easy

    Hugging Face hardMaster

    Existing LLMs can be fine-tuned into BitNet's 1.58-bit ternary format (-1, 0, 1) instead of pre-training from scratch, and a Llama3 8B fine-tuned this way on 100B tokens outperforms Llama 1 7B on MMLU. Training uses BitLinear layers with absmean per-tensor weight quantization, absmax per-token 8-bit activation quantization, layer normalization before activation quantization, and a straight-through estimator implemented via detach(). Inference quantizes weights without rescaling and runs matrix multiplication through efficient kernels. The resulting models load in Transformers through a new 'bitnet' quantization method with no API changes, and pre-training experiments show layer normalization lowering validation perplexity from 6.3 to 5.9.

  • A Gentle Introduction to 8-bit Matrix Multiplication for transformers at scale using transformers, accelerate and bitsandbytes

    Hugging Face mediumAdept

    LLM.int8() makes 8-bit inference of 175B-class transformers possible with no measurable quality loss. It handles the systematic outlier features that emerge beyond about 6B parameters through mixed-precision decomposition: hidden-state columns with values of magnitude 6 or more are multiplied in FP16, and everything else goes through vector-wise int8 quantization before being dequantized and summed. The method is built on FP32/FP16/BF16 trade-offs and absmax and zero-point quantization, and it halves memory relative to half precision. On OPT-175B and BLOOM-176B, lm-eval-harness benchmarks stay within standard error. It is integrated into Hugging Face transformers via bitsandbytes and Accelerate.

  • Quanto: a PyTorch quantization backend for Optimum

    Hugging Face mediumAdept

    Quanto is a PyTorch quantization backend for Optimum that works in eager mode on any device (CPU, CUDA, MPS), supports int2/int4/int8/float8 weights and int8/float8 activations, and accelerates int8-int8, fp16-int4, bf16-int8 and bf16-int4 matmuls on CUDA. The workflow runs from dynamic quantization through optional activation calibration and quantization-aware training to freezing integer weights, then serializing with safetensors plus a quantization map for reloading via requantize. It integrates with transformers through QuantoConfig, works across modalities such as Whisper, and is torch.compile-friendly when activations are left unquantized.

The quest for software engineering knowledge.

Look:
Layout: