Studying Transformers Code First: Half 1 — The Setup | by Lily Hughes-Robinson | Jul, 2023

I don’t learn about you, however someday taking a look at code is less complicated than studying papers. Once I was engaged on AdventureGPT, I began by studying the supply code to BabyAGI, an implementation of the ReAct paper in round 600 traces of python.

Just lately, I grew to become conscious of a current paper referred to as TinyStories via episode 33 of the wonderful Cognitive Revolution Podcast. TinyStories makes an attempt to indicate that fashions skilled on tens of millions (not billions) of parameters will be efficient with high-enough high quality knowledge. Within the case of the Microsoft researchers within the paper, they utilized artificial knowledge generated from GPT-3.5 and GPT-4 that may have value round $10k retail to generate. The dataset and fashions can be found from the creator’s HuggingFace repo.

I used to be captivated to listen to {that a} mannequin may very well be skilled on 30M and fewer parameters. For reference, I’m working all my mannequin coaching and inference on a Lenovo Legion 5 laptop computer with a GTX 1660 Ti. Even only for inference, most fashions with over 3B parameters are too giant to run on my machine. I do know there are cloud compute assets accessible for a value, however I’m studying all this in my spare time and may actually solely afford the modest OpenAI invoice I rack up through API calls. Subsequently, the concept that there have been fashions I may prepare on my modest {hardware} immediately lit me up.

I began studying the TinyStories paper and shortly realized that they utilized the now defunct GPT Neo mannequin in there mannequin coaching. I began digging into the code to see if I may perceive it and I spotted I wanted one thing even smaller to begin from. For context, I’m primarily a backend software program engineer with simply sufficient machine studying expertise to not get utterly misplaced when listening to individuals speak about neural nets. I’m nowhere close to a correct ML engineer and this led me to sort “gpt from scratch” into my most well-liked search engine to discover a gentler introduction. I discovered the video under and every little thing shifted.

This was what I used to be searching for. Along with the fundamental repo linked within the video, there’s a polished model referred to as nanoGPT which continues to be below energetic growth. What’s extra, the coaching code and mannequin code are round 300 traces of python every. To me, that was much more thrilling than the video. I closed the video and began pouring over the supply code. nanoGPT makes use of PyTorch, which I’ve by no means used earlier than. It additionally options simply sufficient math to make and machine studying jargon to make the neophyte in me anxious. This was going to be one thing of a much bigger enterprise than I anticipated.

Among the best methods to grasp one thing is to jot down about. Subsequently, I plan on choosing aside the code within the nanoGPT repo, studying the well-known “Attention is All You Need” paper, and studying transformers in a bottoms-up, fingers on manner. No matter I study alongside the best way I hope to jot down about on this collection. If you wish to comply with alongside, clone the nanoGPT repo to your machine (the mannequin may even be skilled on CPU, so no {hardware} excuses) and comply with alongside.

The very first thing I did after cloning the repo was comply with the README’s directions for coaching the only mannequin, the character-level era mannequin utilizing the tiny_shakespeare dataset. There’s a script to organize the dataset for coaching, a script to do the precise coaching, and a sampling script to output generated textual content. With a number of terminal instructions and an hour+ of coaching, I had easy mannequin to output Shakespearean-sounding textual content.

Following directions is all properly and good, however I don’t actually perceive one thing till I modify it to work for my very own use case. My aim right here was to coach an analogous character-level mannequin utilizing the TinyStories dataset. This required creating my very own knowledge preparation script to get the dataset prepared for coaching. Let’s dig into that deeper.

The nanoGPT has two sorts of knowledge preparation scripts: one for GPT-2 fashion fashions and one for character-level fashions. I grabbed among the code from the GPT-2 fashions for downloading from HuggingFace repositories and took every little thing else from the tiny_shakespeare character-level script. One vital level right here, tiny_shakespeare is simply over 1MB and incorporates solely 40k traces of Shakespeare. TinyStories is over 3GB compressed and incorporates 39.7M tales. The strategies for tokenizing and slicing tiny_shakespeare weren’t straight transferable, a minimum of not with the 32GB of RAM my laptop computer has. I crashed my machine a number of instances attempting pythonic, easy-to-read strategies of getting ready TinyStories. The ultimate script makes use of a number of methods I’ll element under.

First off, my most well-liked answer for processing lists of knowledge is list comprehension, a syntax for producing new lists from current lists with modifications. The problem with listing comprehension on this case is that that 3GB of compressed textual content turns into nearer to 10GB in RAM. Now, listing comprehension requires a number of copies of the listing in RAM. Not a problem for small knowledge, however unworkable for TinyStories.

The outputs of the information preparation scripts is a compressed NumPy array of character stage encoding for the prepare and validation knowledge plus a metadata pickle which incorporates the total listing of distinctive characters and the encoding/decoding maps to transform these characters to numbers. Utilizing this as reference, we don’t want something aside from the ultimate encoded array of numbers as soon as the distinctive characters are discovered and mapped to numbers. The easiest way to do that reminiscence effectively is to iterate via a the information with a easy for-loop whereas constructing these outputs piece-mils. To do that, you initialize an preliminary variable earlier than the loop which then will get up to date every interplay. This prevents a number of variations of the dataset from being held in RAM and solely outputs what we want. The ultimate vocab era code is under:

chars_dataset = set([])
len_dataset = 0

# get all of the distinctive characters that happen on this textual content in addition to whole size for coaching knowledge
desc = "Enumerate characters in coaching set"
for story in tqdm(dataset['train']['text'], desc):
chars = listing(set(story))

for char in chars:

len_dataset += len(story)

That mentioned, an array of 30.7M tales (over 4B characters) encoded as numbers nonetheless takes up a non-trivial quantity of RAM as a result of Python is storing the ints dynamically. Enter NumPy, which has a way more environment friendly array storage the place you may specify the precise measurement of the ints. Along with the environment friendly storage, NumPy additionally has a reminiscence environment friendly array concatenation which can be utilized to construct the ultimate encoded array iteratively slightly than abruptly.

My completion on the script was so as to add a progress bar utilizing tqdm for every step and I used to be lastly able to run the script. So, I ran it in a single day and got here again within the morning. Once I got here again, the script was nonetheless working, with over 100 estimated hours of compute time remaining.

That is when it actually hit me: 30.7M tales is small for a language mannequin, however could be very a lot not a toy dataset to be processed on a single thread. It was time to herald the large weapons: parallelization. Parallelism brings in a whole lot of complexity and overhead, however the efficiency features was well worth the commerce off. Fortunately, there are a variety of the way to parallelize Python code. Many of those options require main rewrites to a serially executed script or difficult abstractions. With a bit of digging, I discovered one thing that allowed me to maintain most of my script the identical however nonetheless run a number of processes to benefit from all of my threads.

Ray is a library for simply parallelizing strategies in Python and may simply be run domestically or as a cluster. It handles working duties in a queue and spinning up employee processes to eat away at that queue. There is a superb information to ray under if this has whet your urge for food.

When it got here to picking what to parallelize, the encode operate appeared like a very good candidate. It has clear inputs and outputs, no unintended effects on these inputs, and was simply one of many largest parts of the compute time. Adapting the present code to work with ray couldn’t have been simpler: the operate turns into accessible to ray through a decorator, the practical name adjustments barely so as to add a distant attribute, and there’s a operate to kick off executing all the information. Beneath is an instance of the way it appeared in my code base initially:

import ray


# given all of the distinctive characters inside a dataset,
# create a singular mapping of characters to ints
stoi = { ch:i for i,ch in enumerate(chars_dataset) }

def encode(s):
return [stoi[c] for c in s]

encoded_stories = []
for story in dataset[‘train’][‘text’]:


Armed with all my CPU’s energy, I cast forward solely to right away crash my laptop computer. With the domestically distributed name stack utilized by ray, your entire dataset was in reminiscence a number of instances over. Merely enqueuing your entire dataset precipitated an out-of-memory error. Aggravated, I used this as an excuse to purchase extra RAM (64GB right here we come!), however continued to tweak the code whereas the RAM shipped.

The subsequent logical place was to batch the requests being dealt with by ray into one thing that might match inside an inexpensive quantity of reminiscence. Including batching logic was moderately easy and is current within the closing codebase I’ll hyperlink to on the finish of the article. What truly grew to become attention-grabbing was experimenting with the batch measurement. Initially, I selected a random batch measurement (5000) and it began out properly, but it surely grew to become apparent to me {that a} truthful period of time was being spent on the single-threaded code throughout every batch.

Basically, watching my most well-liked system monitor, I noticed a single core pinned for minutes earlier than lastly all my laptop computer’s cores lit up for a number of seconds earlier than going again to a solely a single core being utilized. This lead my to play with the batch measurement a bit, hoping to feed the ravenous CPU cores quicker and hold them engaged longer. Decreasing the batch measurement didn’t assist as a result of there was a lot synchronous code in every batch used to slice and put together a batch from the total dataset. That code couldn’t be parallelized, so it meant that every batch had a big startup value time clever producing the chunk. This led me to attempt the alternative, rising the chunk measurement to maintain the cores extra engaged for longer. This labored, as chunk era took the identical period of time no matter chunk measurement, however every chunk processed extra knowledge. Combining this with transferring my encoding post-processing into ray capabilities, I used to be in a position to chew via 30% of the coaching dataset in just some hours, all on a single laptop computer.

Lastly, after a number of extra hours, I had a completely ready, customized dataset to feed to the character-level mannequin. I used to be happy that I didn’t must resort to using costly cloud compute to course of the coaching set, which was my subsequent transfer if the RAM improve didn’t work. What’s extra, I realized intimately what it meant to create/course of a dataset for a character-level mannequin.

Within the subsequent article on this collection, I shall be inspecting the precise mannequin code, explaining as finest I can and linking to copious exterior assets to offer extra info the place my information falls brief. As soon as the article is written, I’ll return and supply a hyperlink right here. Within the meantime, I’ve linked the ultimate model of my dataset preparation script under so you may comply with alongside and see what it takes to course of a considerably giant dataset on a restricted compute platform.

Introduction to Weight Quantization | In direction of Knowledge Science

Individuals Analytics Is The New Massive Factor & Right here’s Why You Ought to Know About It | by Rashi Desai | Jul, 2023