# Mechanizm self-attention (zapytania, klucze, wartości)

EN: **Self-attention (queries, keys, values)** · id `selfattention` · aliases: self-attention, attention mechanism, mechanizm uwagi, scaled dot-product attention, QKV, multi-head attention, uwaga wielogłowicowa, causal attention

Path: [AI Atlas](https://aiatlasgraph.com/llms.txt) › [Sieci i modele generatywne](https://aiatlasgraph.com/t/neural.md) › [Transformery i attention](https://aiatlasgraph.com/t/transformers.md) › Mechanizm self-attention (zapytania, klucze, wartości)

## Description

- PL: Serce transformera: każdy token tworzy zapytanie (Q), klucz (K) i wartość (V), a wynik to średnia wartości ważona podobieństwem zapytania do kluczy wszystkich tokenów. Dzięki temu model łączy informacje z całej sekwencji naraz; koszt rośnie kwadratowo z długością tekstu. W modelach generujących tekst stosuje się maskę przyczynową (token widzi tylko poprzednie).
- EN: The heart of the transformer: each token produces a query (Q), key (K) and value (V), and the output is a weighted average of values, weighted by query–key similarity across all tokens. This lets the model mix information from the whole sequence at once; cost grows quadratically with length. Text-generating models use a causal mask (a token sees only earlier ones).

## In one sentence

- PL: To sposób, w jaki każdy fragment danych dobiera informacje z innych fragmentów tego samego wejścia.
- EN: This lets each data element draw information from other elements of the same input.

## Problem it solves

- PL: Samo słowo nie wystarcza, żeby ustalić, do czego odnosi się w całym zdaniu.
- EN: A word alone is not enough to determine what it refers to in a sentence.

## What it does

- PL: Fragmenty tworzą zapytania i opisy, porównują je, a potem mieszają informacje według dopasowania i dozwolonego zakresu widzenia.
- EN: Elements create queries and descriptors, compare them, then mix information according to their match and the permitted viewing range.

## Everyday example

- PL: W zdaniu o oddaniu klucza sąsiadowi model zestawia zaimek z wcześniejszą wzmianką, żeby przygotować odpowiedź o odbiorcy.
- EN: In a sentence about giving a neighbour a key, a model connects a pronoun with an earlier reference to answer about the recipient.

## This concerns you if you…

- PL: chcesz zrozumieć, jak model wykorzystuje kontekst
- PL: analizujesz zależności między fragmentami tekstu
- EN: want to understand how a model uses context
- EN: analyse relationships between text fragments

## Keywords

scaled dot-product attention · multi-head attention · causal masks · cross-attention · query-key-value projections · softmax scaling

## How it works

- PL: Projekcje wejścia tworzą zapytania, klucze i wartości; skalowane iloczyny zapytań z kluczami przechodzą przez maskę i softmax. Powstałe wagi mieszają wartości, a równoległe głowy uczą różnych relacji przed połączeniem wyjść.
- EN: Input projections form queries, keys and values; scaled query–key dot products pass through masking and softmax. The resulting weights mix values, while parallel heads learn different relationships before their outputs are combined.

## Limits

- PL: Pełny zbiór relacji rośnie kwadratowo z długością. Wagi uwagi nie są same w sobie wyjaśnieniem przyczynowym; brak właściwej maski może ujawniać informacje niedostępne w zadaniu.
- EN: The full relationship set grows quadratically with length. Attention weights alone are not causal explanations; incorrect masking can expose information unavailable in the task.

## Key terms

- PL: **softmax**: Przekształcenie wyników porównania w nieujemne wagi sumujące się do jedności.
- PL: **maska przyczynowa**: Ograniczenie dostępu pozycji do niej samej i wcześniejszych pozycji.
- EN: **softmax**: Transforming comparison scores into nonnegative weights summing to one.
- EN: **causal mask**: Restricting a position's access to itself and earlier positions.

## Know first

- [Algebra liniowa: wektory i macierze (Linear algebra: vectors and matrices)](https://aiatlasgraph.com/t/linearalgebra.md)
- [Statystyka i niepewność (Statistics and uncertainty)](https://aiatlasgraph.com/t/probability.md)

## Examples

- PL: scaled dot-product attention · wiele głów (multi-head) · maska przyczynowa · cross-attention
- EN: scaled dot-product attention · multi-head attention · causal mask · cross-attention

## Related topics

Untyped, symmetric "see also" links.

- [Tłumaczenie maszynowe (Machine translation)](https://aiatlasgraph.com/t/machinetranslation.md)

## Sources

- [Dive into Deep Learning — table of contents](<https://d2l.ai/>) `d2l-toc` · by Aston Zhang, Zachary C. Lipton, Mu Li, Alexander J. Smola
- [Attention Is All You Need](<https://arxiv.org/abs/1706.03762>) `vaswani-attention-2017` · by Ashish Vaswani, Noam Shazeer, Niki Parmar, Jakob Uszkoreit, Llion Jones, Aidan N. Gomez, Łukasz Kaiser, Illia Polosukhin
- [Hugging Face LLM Course, chapter 1: 'How do Transformers work?' and 'Transformer Architectures'](<https://huggingface.co/learn/llm-course/chapter1/6>) `hf-llm-course-transformer-arch` · by Hugging Face · sponsored

---

AI Atlas revision 122 (2026-10-30) · HTML: https://aiatlasgraph.com/t/selfattention · index: https://aiatlasgraph.com/llms.txt · everything: https://aiatlasgraph.com/llms-full.txt · updated 2026-10-30
