Skip to content

Transformer architecture

Коротко:

Transformer — нейросеть-архитектура, введённая Google 2017 ("Attention is All You Need"). Основа всех modern LLM. Ключевой innovation — self-attention mechanism: каждый token "смотрит" на все остальные в sequence + вычисляет weights. Plus: multi-head attention, positional encoding, layer normalization, feed-forward network. Decoder-only (GPT) vs encoder-only (BERT) vs encoder-decoder (T5).

Ниже: подробности, пример, смежные термины, FAQ.

Попробовать бесплатно →

Подробности

  • Self-attention: Q × K^T → softmax × V. O(N²) complexity по context length
  • Multi-head: parallel attention heads (8-128), captured разные patterns
  • Positional encoding: RoPE, ALiBi — adding position info без absolute indices
  • Layers: 24-120 (GPT-4 suspected ~100 layers)
  • Long context: Flash Attention, sparse attention — reduce O(N²)

Пример

# PyTorch простая self-attention
import torch, torch.nn as nn
class SelfAttention(nn.Module):
    def __init__(self, dim):
        super().__init__()
        self.Q = nn.Linear(dim, dim)
        self.K = nn.Linear(dim, dim)
        self.V = nn.Linear(dim, dim)
    def forward(self, x):
        q, k, v = self.Q(x), self.K(x), self.V(x)
        scores = q @ k.transpose(-2, -1) / (k.size(-1) ** 0.5)
        weights = torch.softmax(scores, dim=-1)
        return weights @ v

Смежные термины

Больше по теме

Часто задаваемые вопросы

Почему transformer стал доминирующим?

Parallel compute (в отличие от RNN), scales хорошо с params + data, attention захватывает long-range dependencies. Works на любой sequence data.

Flash Attention — что?

Оптимизированная implementation self-attention. Использует SRAM efficiently, memory linear (not quadratic). 2-4× faster training. v3 — 2025.

Alternatives к transformer?

Mamba / State Space Models (SSM) — linear complexity. Пока uncompetitive с transformers на language, но promising для specific tasks.