MSc AI thesis · Project Paper (CT096) · Asia Pacific University · 2024

Mistral 7B, in Shakespeare's voice

For my master's thesis I fine-tuned a 7-billion-parameter language model on Shakespeare, on a laptop. The base model stayed frozen in 4-bit; I trained a LoRA adapter on top. Four attempts in one day, one that worked, and a few things I'd now describe differently.

0training steps
0on one laptop GPU
2.37 → 1.15training loss
0adapter, on a 14.5 GB base model

The setup

Everything here is read from the files the training tool saved, not from memory.

What was trained

Base model
Mistral-7B-v0.1, loaded in 4-bit (QLoRA)
Adapter
LoRA rank 128, alpha 256, dropout 0.05, on the attention q and v projections: about 54.5 million trainable parameters (a 218 MB adapter file at 32-bit)
Data
Shakespeare as raw text, 375 KB, cut into 320-token blocks. The corpus came from my NLP group project in Oct 2023, a spell checker built on a Shakespeare trigram model.
Optimiser
AdamW, learning rate 3×10-5, linear decay after 100 warm-up steps
Batch
7 per step × 5 accumulated = 35
Stop rule
stop once loss falls below 1.2
Tooling
text-generation-webui with the Training PRO extension
GPU
RTX 3080 Ti Laptop, 16 GB (about 10.5 GB used)

Why this way

Full fine-tuning of a 7B model needs far more memory than a laptop has. QLoRA gets around that twice: the frozen base model is stored in 4-bit, and only two small matrices per attention layer are trained. The whole result is one adapter file you load on top of the original model.

text-generation-webui extensions panel with Training_PRO enabled
The Training PRO extension switched on in text-generation-webui.

One day, six runs

11 March 2024. Four runs left a folder of logs and settings behind; the offline Weights & Biases logs show two more that crashed at step 2. Times are local (Malaysia).

StartRunRankSettingsWhat happened
12:49lorad8lr 3×10-4, batch 4, 256-token blocksStopped. 781 steps in 1.45 h, only 0.2 of an epoch; loss hovered around 2.2.
16:40glutenberg128lr 3×10-5, batch 7×5, 256-token blocksStopped after 39 steps (8.6 min). Its 39 steps were carried into the final run.
16:52glutenberg1128same, 448-token blocksStopped after 4 steps; the next run used 320-token blocks instead.
18:13no folder——Crashed at step 2, before any loss was logged. No adapter saved.
18:25no folder——Crashed at step 2 again. Five minutes later the next attempt held.
18:30glutenberg2128same, 320-token blocksThe thesis run. Resumed at step 39, ran to 859 steps over 7.02 epochs in 3.78 h; loss 1.15. Checkpoints saved at loss 1.76, 1.55 and 1.40.

Rank 128: the run that worked

Loss per logged step (each averages 5 accumulated batches). It dips below the 1.2 stop threshold at the very end.

Rank 8: the first attempt

A 40-step rolling mean of the per-step loss, because the raw values jump between 1.3 and 2.9. It drifted down only slightly before I stopped it.

My thesis reads this pair as an ablation showing rank 8 can't learn. Looking at the logs again, that's too strong: the rank-8 run used a different learning rate, batch and block size, and saw only 0.2 of an epoch before it was stopped. It shows rank 8 at those settings wasn't working, not that rank 8 never could.

A training console on the laptop screen
An earlier training attempt on 11 Mar 2024, photographed on the laptop screen.
The training UI's loss graph on the laptop screen
Another earlier attempt the same day, with the training UI's loss graph. The numbers on this page come from the saved logs, not these photos.

Before and after

The same request to both models: "Write a sonnet on Spring in the style of William Shakespeare."

Base Mistral 7B

Fair Verona, thou art lovely as the day; Thy verdant fields are bathed in golden rays, And thy green hills are crowned with daisies white; The lambs frolic on the meadows bright,

Opening lines. Pleasant, rhymed, and modern-sounding: Shakespeare's names, not his voice.

With the LoRA

What thou art I cannot tell; for thy disguise Hath changed my sight so that I see not thee now. Thy form is fair, but pays not what it promises; And yet I know a gentle spirit lies

Opening lines. Early-modern grammar, inverted syntax, and an argument rather than a description.

Fluent is not the same as right

I asked both models to explain a passage. The fine-tuned one sounds far more knowledgeable, and names the wrong play.

Base model explaining the passage in general terms
Base model: vague, and it treats the passage as a love poem.
Fine-tuned model attributing the passage to The Taming of the Shrew
Fine-tuned: confident, specific, and wrong. It says Petruchio to Katherine in The Taming of the Shrew; the lines are Bertram to Diana in All's Well That Ends Well, Act 4 Scene 2.

What the chat logs kept

The chat UI saved every conversation. They don't record which adapter was loaded, so these are dated, not labelled.

19 Feb 2024 · before any fine-tuning
> help me with creating loras using the alpaca format
Of course! To create LORAs using the Alpaca format, follow these steps:
1. ... pip install alpaca-tradeapi
2. Create an Alpaca API client ... your API key, secret, and base URL
   (e.g., production or paper trading).
I asked about the Alpaca instruction format for training data. It explained a stock-trading API instead. This is the day I started reading about LoRAs.
29 Mar 2024 · asked for new sonnets
> write a sonnet about india in the style of shakespeare
Sonnet 18 by William Shakespeare
Shall I compare thee to a summer's day? ...
> write a sonnet about birds ... and explain it
Sonnet 29 by William Shakespeare
When in disgrace with fortune and men's eyes, ...
> write a sonnet on kids in the style of shakepeare
Sonnet 30 by William Shakespeare
Three requests for new poems, three of Shakespeare's own sonnets, word for word, ignoring the subject. Memorising the training text, not writing in its style.
24 Mar 2024 · the template
> write a sonnet on cars in the style of shakespeare
O, cars! Sweet conveyances! Thou art my undoing, And mine love's delight. I burn for thee With such a fire That even heaven itself doth quake In fear Of thy swiftness and power. ...
The highlighted words are copied from its answer to the previous prompt in the same chat, with only the subject swapped. Unrhymed and fourteen lines in name only. It also wrote Shrek as a Shakespeare play, which was the best thing it did all month.

Thesis: Finetuning Open-Source LLMs for Teaching Purpose, Satyam Saxena, Asia Pacific University of Technology & Innovation, 2024. doi:10.5281/zenodo.17305216 (CC BY 4.0).