Concepts / Positional Embeddings in Transformer Models

Positional Embeddings in Transformer Models

Long-context finetuning extends a model's context length, meaning how many tokens it can handle at once.

  • Programming

Why a Longer Input Is Not Just More Text

Suppose a language model can only take in a certain number of tokens at once, and you want it to read much longer documents or code files. The obvious plan is to gather longer documents, feed them to the model during finetuning, and let training do the rest. This article explains why that plan is incomplete. Extending how much a model can read at once is a different kind of change from most finetuning, and it reaches into the model's architecture, specifically the positional embeddings that record where each token sits.

What do you think happens?

If you only feed longer documents during finetuning and leave the model's architecture untouched, does the model gain the ability to handle the longer context?

  • Yes, the model simply learns to read further

  • No, the component that represents token positions must also be adjusted

Reveal answer
Answer:

No, the component that represents token positions must also be adjusted

The source explains that a longer sequence means more possible token positions. The positional embeddings must be able to cover those additional positions, and if the component was built only for shorter positions, feeding it longer inputs is not enough.

The Common First Guess About Longer Inputs

The first guess goes like this: feed the model longer documents during finetuning, let it practice on them, and it learns to read further. Under this picture, only the amount of text in each training example changes, and the model's internal machinery stays the same. The belief is understandable, because most finetuning works that way. You change the data, run training, and the model's behavior shifts. Long-context finetuning is different, and the difference matters for anyone who wants to extend a model's reach.

What Context Length Means

Definition

Context length is the number of tokens a model can take into account at once. Long-context finetuning is a technique for extending a model's context length, so the model can handle more tokens in a single input.

The defining feature of long-context finetuning is that it typically requires modifying the model's architecture rather than only changing the training data. The source names a specific part that is usually adjusted: the positional embeddings.

Positional Embeddings as a Record of Positions

Every token in a sequence occupies a position, and the model needs a way to represent where each token sits. Positional embeddings are the component of the architecture that handles these token positions. You can think of them as the model's record of where each token sits in the sequence. Each position in a short input has a slot in that record. When the input grows, more slots are needed.

Slot 0first token positionSlot 1second token positionSlot Nlast trained positionSlot N+1added positionnext positionextended range
Which position slots exist in the record when the input is longer than the trained context, and which slots must be added to cover the new positions?

The diagram is a simplified picture of the record. The first slots cover the positions the model was built for, and the added slots represent the wider range that extended context requires. The source does not describe a specific method for how the added positions are represented, so the diagram shows only that the record must grow to cover them.

Why Context Length Forces an Architecture Change

The three pieces are tied together. The context window size sets how many token positions a model must handle. The positional embeddings represent those positions. Token positions are what each token in an input occupies. A longer sequence means more possible token positions, so the positional embeddings must be able to cover those additional positions. If the component was built only for shorter positions, feeding it longer inputs is not enough. The component itself has to be adjusted so it can represent the wider range.

Context window sizehow many tokens at oncePositional embeddingsrepresent token positionsToken positionswhere each token sitssets how many positions must be coveredrepresents each position
If the context window grows, what else must change so that every token position is represented?

Code Llama's Extension from 4,096 to 16,384 Tokens

The source gives a concrete case. Code Llama models were created from the base model Llama 2 using different finetuning techniques. Using long-context finetuning, the developers increased the model's maximum context length from 4,096 tokens to 16,384 tokens so it could accommodate longer code files. Code files are a natural motivation, since a single source file or a set of related files can run far longer than a short question or a paragraph.

Worked example

Reasoning through a context extension

A model's maximum context length is 4,096 tokens. The goal is to handle 16,384-token code files. Which parts of the model must be considered, and why is more training data alone insufficient?

  1. 1
    Identify the old and new limits

    The maximum context length grows from 4,096 tokens to 16,384 tokens, so the number of token positions the model must represent grows as well.

  2. 2
    Locate the component that represents positions

    The positional embeddings handle token positions. They were built for the original range, so they must be able to cover the additional positions.

  3. 3
    Check whether data changes reach that component

    Longer training examples add text, but they do not by themselves change the component that represents positions. The architecture must be adjusted as well.

Result

The extension requires adjusting the positional embeddings so they cover positions beyond the original 4,096, and longer finetuning data is only one part of the work.

Distinguishing Long-Context from Other Finetuning

The source places long-context finetuning alongside two other techniques, and the comparison clarifies what makes it distinct. Instruction finetuning is supervised finetuning on instruction data. Its responses can be open-ended, such as a book summary, or close-ended, such as a classification answer. The source notes that high-quality instruction data can be challenging and expensive to create, especially when instructions demand factual consistency, domain expertise, or political correctness.

Preference finetuning uses reinforcement learning to push the model toward responses that people prefer. It depends on comparative data, typically arranged as an instruction, a winning response, and a losing response. Long-context finetuning, by contrast, is aimed at a property of the model: how many tokens it can handle. It is not defined by the kind of data or the reward signal used during training. Neither instruction finetuning nor preference finetuning is defined by extending context length.

Context lengthmore tokens at onceSupervised finetuningon instruction dataReinforcementlearningtoward preferred responsesArchitecture adjustedpositional embeddingsInstruction dataopen or close-endedresponsesComparative datawinning and losingresponses
What does each method change, and how do its inputs differ from the others?
MethodWhat it targetsTypical inputDefined by context length?
Long-context finetuningNumber of tokens the model can handle at onceLonger sequences, often with architecture changesYes
Instruction finetuningSupervised behavior on instruction tasksInstruction data with open or close-ended responsesNo
Preference finetuningResponses people preferComparative data: instruction, winning response, losing responseNo

The three finetuning methods differ in what they aim to change

The Risk on Shorter Sequences

Extending context length is harder than many other finetuning techniques, and the resulting model might degrade on shorter sequences. Before extension, the model handles short inputs with the positional record it was built for. After extension, the same model is asked to represent a wider range of positions, and its behavior on short inputs is no longer guaranteed to stay the same. The source states the risk as a possibility, so the practical lesson is to check short-input behavior after a context extension rather than assume it is preserved.

Mistakes Beginners Make with Context Extension

  • Common mistake

    Treating long-context finetuning as only a data change

    What it looks like

    Feeding 16,384-token code files during finetuning while leaving the positional embeddings unchanged

    Why it fails

    The positional embeddings must be able to cover the additional positions, and longer data does not change that component on its own.

    Do this instead

    Plan an architecture adjustment, such as changing the positional embeddings, alongside the longer training data.

  • Common mistake

    Assuming shorter inputs are unaffected after extension

    What it looks like

    Releasing an extended model without rechecking how it handles short questions

    Why it fails

    The source warns that the resulting model might degrade on shorter sequences.

    Do this instead

    Evaluate short-input behavior after the extension, not only long-input behavior.

  • Common mistake

    Confusing context length with instruction or preference training

    What it looks like

    Calling a model with a larger context window preference-finetuned because it was trained on comparative data

    Why it fails

    Long-context finetuning is aimed at how many tokens the model can handle. Instruction and preference finetuning are defined by data format and training goals, not by context length.

    Do this instead

    Name the method by what it changes: context length for long-context finetuning, instruction data for instruction finetuning, and preferred responses for preference finetuning.

Checking Your Understanding of Context Extension

MEDIUM

A team has a model that accepts up to 4,096 tokens. They want it to read 16,384-token code files. Their plan is to collect longer code files, finetune on them, and leave the model's architecture unchanged. Explain what part of the model is likely to limit this plan, and what extra step the team should take before and after training.

Hints
  • Ask what a longer sequence means for the number of token positions.
  • Recall which component the source says handles token positions and what must happen to it.
  • Think about whether the model's short-input behavior should be rechecked after the change.
EASY

Name the finetuning method that would be used to make a model accept longer inputs, and name the two other methods it differs from. For each, state the one thing it is defined by.

Hints
  • Long-context finetuning is defined by context length.
  • Instruction finetuning uses supervised training on instruction data.
  • Preference finetuning uses reinforcement learning with comparative data.

Key Takeaways

  • Long-context finetuning extends a model's context length, meaning how many tokens it can take into account at once.
  • Extending context length typically requires modifying the architecture, especially the positional embeddings, because a longer sequence means more possible token positions that must be covered.
  • Data changes alone are not enough, and a context-extended model might degrade on shorter sequences, so short-input behavior should be checked.
  • Long-context finetuning is defined by context length, while instruction finetuning uses supervised training on instruction data and preference finetuning uses reinforcement learning on comparative data.

Related concepts

More concepts starting with P