theodore

theodore

Let's build GPT from scratch w/ Nx and Axon

Hello,

Like many people on Earth, I’ve been trying to understand the transformer architecture. I translated the Python code in Let's build GPT: from scratch, in code, spelled out. - YouTube to Elixir and packaged it in this livebook.

Some first impressions of Elixir’s ML libraries

  • Livebook is very nice
  • Using a functional style to implement models made more sense to my brain

Performance could be better (like using cache), but it should all work.

I hope this is helpful for anyone who’s trying to understand the transformer architecture.

Thanks!

Most Liked

NickGnd

NickGnd

Hey @theodore :wave:
Last week i started watching the video and implementing the GPT step-by-step following Karpathy’s video. Your livebook has been of great help so far, without which I’d have give up at the multinational distribution implementation.

Here my WIP livebook, I’m still at the training of the Bigram Model.

There are some small differences compared to your, in particular in the forward and loss function implementation, the training with my implementation seems a bit faster for what it matters :man_shrugging: but the generated text is basically the same (love the reproducibility :muscle:).

Also, how did you come up with this part of the code?

# Cap the input sequence length from [t, block size]
context_length = min(t, block_size)
context_range = -context_length..-1
context_slice = acc[[.., context_range]]

I (think) makes total sense since the prediction focus on the last block_size char at max, but I don’t recall Karpathy mentioning that when implementing the generate function, in fact is not present in his version and the generated text is the same when passing the whole acc - feels magic :sparkles: - but it’s slower.

Let’s see if I manage to proceed and finish it, but looking at your livebook, I still have a long way to the end :railway_track:

Again, thank you for putting together this great piece of work :man_bowing:
Best.

theodore

theodore

Haha that’s awesome to hear! I appreciate it.

That’s a good question. The short answer is that I added the “fully finished” generate_fn in the beginning so I could reuse it for the singlehead / multihead / fully-finished models :smile:

But, you’re right, the bigram model doesn’t need to truncate the input because the predicted next char doesn’t rely on context. Every char already has a mapping to its likely next char in the [65][65] embedding kernel

  • t is likely to produce h
  • h is likely to produce e

But, if you try out the non-truncating generate_fn on the single head model, you’ll see this gibberish:


S:
Babthat tebrsb udinmipfsm enryarlrent cweetetpenycro oorctsstsnll miiesor in vutt? n

Crcewtoemi beoercstosl;dercta 'incasesptb wnf n f ntr',,kntn,iaessdisuyt asbitsfdcolultoitem i'osamner-mesiidr krestkuof'osdst. soutrjo la citeeemiktinavbeimtidelgkdtom ir, et, n rksyet.eyr? wstnserrrssrdewpnititoe'sirbrst i  ' m lam Tseht. mt diemrt qsspiswtatroira; rfwtocycct psoubefemt mtne' w a sfrrn. setetea acaoslt selfta.




aytdestinteet,eehiyploeyiticosa snun' g




 Itit nisvin eap rohuwprcoa rtto pn rhteteaepevit sho ta wdkilsrtsito ha quvb; lumorldwxetoteri'


 nwsfetenaseaxesllelyeeo! lo; retiameirlrsitto rsnrcihueaaeddrdda , Tt-epinmemeb gdusifkto: tlrremnert J nr ltsb rssaorisscoa .,!enedsasdmwa R I ha rrrf nwt. duiheissyrthtprereeuid. tl yt ea vunikiicsisshmjarrpreoretrei t ira,yeyyera enoaastewrdelmocylidtrnmsU


 verpseratseii? Lit atoedswottea srm' asfoyyila? pviyurnyeyigcyemt wepimirt tfwmaymo stsfolsdulfa I wsunak' vfepaa spktsytteekhopmanb' Lepswa O aegcino;elrmogymua had lei

vs the original


S:
Babther te st bud hean! fad ry sis lr,
an withanu sr mou toous miche erer fo wous in
Lou byoun, I mine sthon omf th Cor Il, oum win m.

Fro hepith sanll hitro Il ous ecist wirs hal st os nchat har hinlost, art st fpivis, be Pans che-Cre, yan!
Th, headise foacer ch orsu thive ivisen prourilay, ksese.
QPUESTV:
'

[...]

Goodluck with learning the transformer architecture. I think implementing it in elixir helped cement the math behind it more, so even though it took a long time, I found it worthwhile. Anyways I hope that helped!

theodore

theodore

Thanks Nick :smiley: Yea it was quite a grind to figure it out. I actually used some of your notebooks to learn Nx + Axon, so thank you too!

johnknott

johnknott

This is incredible. Thank you so much.

NickGnd

NickGnd

Hey @theodore
I just checked out the livebook, that’s a lot of work, impressive! Thanks for sharing :heart:

I’ll definitely watch the video together with your livebook. Maybe this time I’ll finally grasp the transformer architecture and not stop at the intricate diagram.

Thanks again, cheers!

Where Next?

Popular in Other Resources Top

PJUllrich
I’m happy to announce RunElixir.com, a free online guide to Elixir. It aims to answer the questions: How can I get started with Elixir?, ...
New
D4no0
This is a little bit offtopic. I would like to make a relative UI renderer (similar to the ones present on maket: android, IOS, WPF) for...
New
zenw0lf
Hello all! I just finished a full step-by-step tutorial on how to build a JSON API with Phoenix: https://lobotuerto.com/blog/building-a...
New
New
maz
https://www.reddit.com/r/elixir/comments/f8by3w/im_connecting_a_godot_game_client_to_an_elixir
New
aifrak
tl;td: My solutions are here and here. Elixir is awesome. Exercism is awesome. Thank you Elixir community. Recently, I have solved all t...
New
ambareesha7
there are lot of learning resources and blogs are available but they scattered across, so i’m trying to pooling them in this repo. pleas...
New
landonwilkins
Hi! My apartment’s walls are blank. I want to fill at least one with Elixir diagrams. Links to Diagrams https://github.com/benjamintan...
New
crockwave
I updated my fork of the live_view_hooks demo repo to include fullCalendar integration, including JSON API, event creation and storage in...
New
kpanic
Hi everyone, At the company where I work (Taxfix), we prepared an internal Elixir course for our folks. We decided to share it with eve...
New

Other popular topics Top

TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
273 38985 115
New
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
977 41022 311
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
sergio
I couldn’t find any guides that worked well with Phoenix 1.6.0 and esbuild. I hope this helps people test the waters and eases you into t...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New

We're in Beta

About us Mission Statement