LongContext Finetuning RepetitionTask
Introduction
This cookbook is part of a technical deep dive blogpost on long context finetuning that you can read here.
If you ask an LLM to repeat a sequence back to you, surely this should be easy, right? The answer is not straight forward and might be surprising to many!
A lot of the capabilities that we know and trust our LLMs to have, fall short at longer contexts!
To solve this repetition task a LLM should be able to use a simple induction head - that just copies a specific part of the input back out.
However, for this task at longer contexts non-finetuned models fail quite miserably!
In this notebook we will:
- Use a previously created dataset of long input sequences (upto 128k tokens)
- We will setup the repitition task, where we ask the model to repeat the last
kwords of the sequences created in Step 1. - Demonstrate how even the best LLMs fail at this simple repitition task.
- We will fine-tune the model on ~1975 examples of this long-context task and show a radical improvement.

Install Libraries
Dataset Preperation
In order to create a long context dataset for our repetition task we extracted 2000 samples of varying length as shown below:
Repetition Task Definition
We used the code above to previously curate a dataset of long sequences by processing the FineWeb and RedPajama datasets and retrieving 2000 English documents of 32k and 128k context length each.
For each of these documents we want to setup a task prompt that we can pass into an LLM.
This was done as follows:
For the task prompts outlined above we need the LLM to, given an input sequence of arbitrary length, repeat the last k words of the sequence back to us. Where K is an random number beteween 1 and 100.
We also extract the correct last k words directly from the document and store this to use for comparision with ground truth later.
We have provided a JSON file from which you can load the task prompts.
2000
'Return last 67 words from this text: \n\n- freely available\nToxins 2010, 2(4), 461-493; doi:10.3390/to'
'S.P.A.; Marmejo, J.; Giusti, W.; Deetz, K. Oligonucleotides with fluorescent dyes at opposite ends provide a quenched probe system useful for detecting PCR products and nucleic acid hybridization. PCR Met. Appl. 1995, 4, 357–362. [Google Scholar] © 2010 by the authors; licensee Molecular Diversity Preservation International, Basel, Switzerland This article is an open-access article distributed under the terms and conditions of the Creative Commons Attribution license (http://creativecommons.org/licenses/by/3.0/).'
'Here\'s the last 67 words from this text in a more readable format:\n\n"Detection of Ochratoxin A (OTA) Producers in Contaminated Commodities using PCR-Based Techniques. Real-time PCR (RT-PCR) can detect and quantify fungus DNA, providing new tools for fungal detection and quantification. RT-PCR can be performed using different chemistries, such as SYBR® Green I dye and TaqMan®. Both systems have proven useful in monitoring and quantifying OTA fungal producers in many food commodities."'
As we can see from the single example above, our LLM is not great at this task.
Ideally the LLM should be able to use an induction head to repeat a previously seen sequence back out. An induction head is a key component in transformer models that specializes in pattern recognition and prediction. Like a pattern-matching expert, it identifies repeated sequences in text and uses previous occurrences to predict what comes next. For example, if a phrase appeared before and was followed by specific text, the induction head remembers this pattern and applies it to similar future situations. This capability is fundamental to how transformers process language, enabling them to learn from repetition and make informed predictions based on previously seen patterns. Think of it as the model's memory mechanism for recognizing and utilizing recurring patterns in text.
Use Levenshtein Distance to Evaluate
For this repetition task we need an exact comparision between the correct sequence of words to the LLM output sequence of words.
Since this is an exact matching task we will use Levenshtein Distance.
Levenshtein Distance measures how different two strings are by counting the minimum number of single-character changes (including inserting a character, deleting a character, or replacing a character) needed to turn one string into another.
For example the levenshtein distance between kitten and sitting is 3 since we need 3 operations to for from one to the other.
kitten → sitten (replace 'k' with 's')
sitten → sittin (replace 'e' with 'i')
sittin → sitting (insert 'g')
Total Levenshtein Distance = 3 operations
Think of it like measuring the "editing effort" needed to transform one word into another. The lower the number, the more similar the strings are. A distance of 0 means the strings are identical, while larger numbers indicate more differences.
For our purpose we will use ratio = 1 - (leven_distance / (len1 + len2)) to obtain a score between 0 and 1.
0implies that the two strings are very different1implies that that two strings are identical
For our repetition task higher is better!
0.3618290258449304
Next we will loop over the first 25 task items and see how well our Llama 3.1 70B model performs at this task!
0%| | 0/25 [00:00<?, ?it/s]
0.377094996064535 103.44
[0.3004739336492891, , 0.3609022556390977, , 0.4263494967978042, , 0.34236804564907275, , 0.41393034825870645, , 0.35359116022099446, , 0.8858057630736392, , 0.36530442035029187, , 0.2229924898902369, , 0.39370078740157477, , 0.36111111111111116, , 0.33757961783439494, , 0.3677758318739054, , 0.8792569659442724, , 0.5407725321888412, , 0.1308455926324602, , 0.318349299926308, , 0.21875, , 0.33444816053511706, , 0.2104413347685683, , 0.24250681198910085, , 0.6222222222222222, , 0.3307692307692308, , 0.3536842105263158, , 0.11344327836081958]
As we can see above, Llama3.1 70B performs suboptimally at this repetition task.
Fine-tune on Repetition Task
Below we will fine-tune a smaller Llama 3.1 8B model on 1975 examples of this repetition task and see if we can get it to outperform on this task.
38838055
Once the Model is finetuned we can assess how well it performs.
Deploy Model and Run Evals
Before we can run the evaluations we need to deploy our finetuned model as a Dedicated Endpoint(DE). After fine-tuning completes, access your model through the Together AI dashboard. Go to Models, select your fine-tuned model, and select Deploy. Choose from the available hardware options - we'll use a single A100-80GB GPU for this example.

0%| | 0/25 [00:00<?, ?it/s]
0.8149842379839035 15.08
We can see that even for the smaller 8B model the Score improves from 0.37 to 0.81 after finetuning when compared to the 70B untuned model.
We can also see that after finetuning the model more often outputs the correct number of words, with the length difference decreasing from 103.44 before finetuning to 15.08 afterwards.
To learn more about our fine-tuning API read the docs here!