News for hackers. Stuff that matters.
Re:GPT in 500 Lines of SQL by chrsig on Friday February 23, 2024 @10:35PM (#39488921)
This is beautiful. I'd actually been going down this same rabbit hole with sqlite, I hadn't gotten far enough to bring a neural net into it.

I'd been inspired by the makemore lecture series[0]. At the 1hr mark or so, he switches from counting to using a nn, which is about as far as I've gotten. Breaking it down into a relational model is actually a really great exercise.

[0] https://www.youtube.com/watch?v=PaCmpygFfXo

Re:GPT in 500 Lines of SQL by pests on Saturday February 24, 2024 @04:08PM (#39495152)
If you keep watching you will see the NN derive the same exact table as the counting method and even gives the exact same results when generating.
Re:GPT in 500 Lines of SQL by ianand on Saturday February 24, 2024 @12:16AM (#39489307)
This is great. In a similar vein, I implemented GPT entirely in spreadsheet functions with accompanying video tutorials

https://spreadsheets-are-all-you-need.ai/

Re:GPT in 500 Lines of SQL by danielmarkbruce on Saturday February 24, 2024 @12:37AM (#39489400)
Nice job. Spreadsheets are a natural way to explain an LLM. I suspect that you can show training well too by calculating the derivatives for each parameter under each training example and showing it all explicitly mapped to the relevant parameter etc etc
Re:GPT in 500 Lines of SQL by ianand on Saturday February 24, 2024 @02:19AM (#39489796)
Thank you. Validating to hear others feel spreadsheets are a natural and accessible way to explain LLMs. Someone asks “what’s do they mean by a parameter in a model” or “what is the attention matrix” and you can just pull it up graphically laid out. Then they can fiddle with it and get a visceral feel for things. It also becomes easier for non coders do things like logit lens which is just a few extra simple spreadsheet functions.

I actually plan to do what you describe after I do the embeddings video (but only for a “toy” neural net as a proof-of-concept introduction to gradient descent).

Re:GPT in 500 Lines of SQL by airstrike on Saturday February 24, 2024 @12:41AM (#39489417)
Not only is that amazing, your video was so well done. Superb crowd work! Color me double impressed.
Re:GPT in 500 Lines of SQL by ianand on Saturday February 24, 2024 @02:23AM (#39489830)
Thanks! Each one takes a surprisingly long time to make. Even figuring out how make the explanation accessible and compelling yet still accurate takes awhile and then there’s still the actual video to do.
Re:GPT in 500 Lines of SQL by alisonatwork on Saturday February 24, 2024 @05:28AM (#39490559)
Your first video is fantastic. As someone who thinks LLMs are pretty nifty but never had a professional need to learn how they actually work, that 10 minute video taught me more than several years of reading esoteric HN comments and fluffy mainstream media on the topic. Seeing a bazillion floating point numbers all stacked up ready to be calculated across also makes it much more intuitive why this tech devours GPUs, which never really occurred to me before. Thanks for sharing.
Re:GPT in 500 Lines of SQL by int_19h on Sunday February 25, 2024 @05:52PM (#39505765)
Great approach and great delivery; looking forward to the next video in the series!
Re:GPT in 500 Lines of SQL by hsbauauvhabzb on Friday February 23, 2024 @11:56PM (#39489235)
I’ve completely avoided GPT and LLMs. This looks like it would generate some level of fluidity in text output, but not be able to parse and answer a question.

Is there any simplistic blog posts / training courses which go through how they work, or expose a toy engine in python or similar that? All the training I’ve seen so far seems oriented at how to use the platforms rather than how they actually work.

Re:GPT in 500 Lines of SQL by zaptrem on Saturday February 24, 2024 @12:03AM (#39489264)
Strap in, this is by far the best resource: https://www.youtube.com/watch?v=kCc8FmEb1nY
Re:GPT in 500 Lines of SQL by ford on Saturday February 24, 2024 @12:32AM (#39489379)
Jay Alammar has my favorite sequence of tutorials from basic neural network math to GPT2.

Particularly [0], [1], and [2]

[0] http://jalammar.github.io/illustrated-transformer/

[1] http://jalammar.github.io/illustrated-gpt2/

[2] https://jalammar.github.io/visualizing-neural-machine-transl...

Re:GPT in 500 Lines of SQL by jer0me on Friday February 23, 2024 @10:33PM (#39488913)
Related:

A GPT in 60 Lines of NumPy - https://news.ycombinator.com/item?id=34726115 - February 2023 (146 comments)

Re:GPT in 500 Lines of SQL by sunnybeetroot on Sunday February 25, 2024 @02:40PM (#39504041)
This is mentioned in the article near the top.
Re:GPT in 500 Lines of SQL by brainless on Friday February 23, 2024 @11:15PM (#39489061)
I think, I think, GPT creating GPT... creating GPT will be a thing soon. GPTception.
Re:GPT in 500 Lines of SQL by codetiger on Friday February 23, 2024 @11:42PM (#39489186)
GPT creating a better algo than itself is what’s even more interesting
Re:GPT in 500 Lines of SQL by mavamaarten on Saturday February 24, 2024 @06:03AM (#39490698)
It might. But also, all decisions and knowledge is pretty much based on a resampling of our own language, conversations and internet data. It might puzzle together some existing ideas that have never been combined. Or it might hallucinate a better solution by accident. But we're definitely not at the level yet where it will actively build something great.
Re:GPT in 500 Lines of SQL by erwincoumans on Saturday February 24, 2024 @06:30AM (#39490809)
Self-play GPT (by bots in a rich simulation) similar to Alpha Go Zero?
Re:GPT in 500 Lines of SQL by codetiger on Saturday February 24, 2024 @07:13AM (#39490950)
Games like Alpha Go have very limited(or known) end state so reinforcements learning or similar methods work great. However, I wonder how will AI train itself in learning human languages without being judged by humans. It’s just a matter of time before someone figures out
Re:GPT in 500 Lines of SQL by erwincoumans on Saturday February 24, 2024 @01:48PM (#39494079)
Right, a rich simulator with humans for feedback: an evolved version of online worlds with a mix of AI NPC's and real people, with the task: find the NPC's. The NPC's can train in rooms with exclusive NPC's or mixed with people, without knowing.
Re:GPT in 500 Lines of SQL by wizzwizz4 on Saturday February 24, 2024 @09:09AM (#39491592)
Self-play works for Go, because the "world" (for lack of a better term) can be fully simulated. Human language talks about the real world, which we cannot simulate, so self-play wouldn't be able to learn new things about the world.

We might end up with more regularised language, and a more consistent model of the world, but that would come at the expense of accuracy and faithfulness (two things which are already lacking).

Re:GPT in 500 Lines of SQL by incahoots on Friday February 23, 2024 @11:33PM (#39489141)
Personally I like the AI dogman angle. AI trained to beat other AI (resumes tailored to beat ATS algorithms)
Re:GPT in 500 Lines of SQL by seasonalnull on Friday February 23, 2024 @10:26PM (#39488874)
This should be illegal
Re:GPT in 500 Lines of SQL by fifilura on Saturday February 24, 2024 @05:59AM (#39490678)
Can you elaborate?
Re:GPT in 500 Lines of SQL by behnamoh on Friday February 23, 2024 @10:31PM (#39488907)
What is this sorcery?
Re:GPT in 500 Lines of SQL by slt2021 on Friday February 23, 2024 @11:41PM (#39489182)
I can feel the SQL-Force in this young jedi, Midichlorian level is on another level
Re:GPT in 500 Lines of SQL by swasheck on Friday February 23, 2024 @11:48PM (#39489204)
alex is a genius. he’s worth a follow.
Re:GPT in 500 Lines of SQL by B1FF_PSUVM on Saturday February 24, 2024 @10:35AM (#39492249)
"It's full of jewels", as someone almost said.

E.g. The Sultan’s Riddle in SQL https://explainextended.com/2016/12/31/happy-new-year-8/

Re:GPT in 500 Lines of SQL by wanderingmind on Saturday February 24, 2024 @01:14AM (#39489543)
These marvels need to be preserved. Just posting the archive link here in case the blog is not maintained in future.

https://archive.is/VAGzF

Re:GPT in 500 Lines of SQL by neonate on Saturday February 24, 2024 @04:06AM (#39490238)
Re:GPT in 500 Lines of SQL by zarkenfrood on Saturday February 24, 2024 @01:50AM (#39489689)
Thanks, this is a fantastic article and it would be a shame to be lost.
Re:GPT in 500 Lines of SQL by rawgabbit on Saturday February 24, 2024 @01:35AM (#39489630)
This is very cool
Re:GPT in 500 Lines of SQL by lagniappe on Saturday February 24, 2024 @01:54AM (#39489708)
This is a great read, I didn't expect to scroll right back to the top as soon as I finished it the first time.
Re:GPT in 500 Lines of SQL by namnhocq8 on Saturday February 24, 2024 @02:14AM (#39489777)
Tại xỉu
Re:GPT in 500 Lines of SQL by namnhocq8 on Wednesday February 28, 2024 @12:28AM (#39534355)
234
Re:GPT in 500 Lines of SQL by ksarw on Saturday February 24, 2024 @03:13AM (#39490024)
Great write up, I enjoyed the reading the explanations for each piece and found them to be clear and quite thorough.

I did make the mistake though of clicking "+ expand source", and after seeing the (remarkable) abomination I can sympathize with ChatGPT's "SQL is not suitable for implementing large language model..." :)

Re:GPT in 500 Lines of SQL by deskamess on Saturday February 24, 2024 @08:14AM (#39491253)
I did that too and could not find a way to collapse it.
Re:GPT in 500 Lines of SQL by sigmoid10 on Saturday February 24, 2024 @03:36AM (#39490123)
It's a nice demo. Unfortunately the article mixes up things in the explanation for causal masking, because it seems the author conflates aspects from training and inference. First, causal masking exists to prevent the model from "peeking" at future tokens during training, and second (at least for GPT-like architectures) for enforcing the autoregressive aspect during inference. During inference we only use the last token anyways, so it will attend the entire input sequence. So this token is definitely not decided only from the last token's embedding.
Re:GPT in 500 Lines of SQL by jakjak123 on Saturday February 24, 2024 @04:59AM (#39490441)
This is a very good article and introduction.
Re:GPT in 500 Lines of SQL by Alifatisk on Saturday February 24, 2024 @05:54AM (#39490658)
I love this, something that starts off as some kind of sorcery a year ago is now being explained so well and almost in a childish way.
Re:GPT in 500 Lines of SQL by Hendrikto on Saturday February 24, 2024 @07:44AM (#39491099)
Despite this impressive achievement, the author does not appear to have the most solid theoretical background.

Some explanations (e.g. regarding UTF encoding) are not entirely correct and use uncommon terminology (e.g. alphabet instead of vocabulary).

Re:GPT in 500 Lines of SQL by worewood on Saturday February 24, 2024 @10:29AM (#39492192)
Also, about the non-determinism issue, there was a post some time ago and that comes from the way the GPU does the calculations, something something floating point something.

So of course the algorithm is deterministic, but the real-life implementation isn't.

Re:GPT in 500 Lines of SQL by gugagore on Saturday February 24, 2024 @12:04PM (#39493099)
Floating point addition, for example, is not associative, so the order of taking a sum affects the result. If the summation were sequential and single threaded, it would be deterministic. But it happens in parallel, so timing variations affect the result.

But there is probabilistic sampling that happens (see "temperature").

Re:GPT in 500 Lines of SQL by marginalia_nu on Saturday February 24, 2024 @12:41PM (#39493444)
> Floating point addition, for example, is not associative, so the order of taking a sum affects the result. If the summation were sequential and single threaded, it would be deterministic. But it happens in parallel, so timing variations affect the result.

In this sense, I don't think it's fair to say floating point math is non-deterministic, as much as parallel computation is non-deterministic. FP behaves in unexpected ways, but the same order of operations always yields the same unexpected results (except on Pentium 1).

Re:GPT in 500 Lines of SQL by dzink on Saturday February 24, 2024 @10:49AM (#39492416)
Electricity, Cars, and Gas were once upon a time a luxury as well - reserved to those who could afford them or had unique access / credentials / skills. The people who were able to simplify and spread the advanced tech to the common person became Billionaires.
Re:GPT in 500 Lines of SQL by darkmx0z on Saturday February 24, 2024 @10:53AM (#39492451)
when talking about a set of characters, alphabet is a commonly used term https://cs.stackexchange.com/search?q=string++alphabet+size
Re:GPT in 500 Lines of SQL by ecnahc515 on Saturday February 24, 2024 @12:15PM (#39493208)
Sure, but if your doing work in machine learning that’s generally not the terminology used, hinting that this isn’t the area the author specializes in (which isn’t a bad thing, but take their explanations with a grain of salt).
Re:GPT in 500 Lines of SQL by maelito on Saturday February 24, 2024 @06:58AM (#39490895)
> and almost in a childish way.

No. You've got to have a solid background in computer science to even start to understand fully this article.

Even the title itself is not accessible to 99% of humans.

Re:GPT in 500 Lines of SQL by bookofjoe on Saturday February 24, 2024 @08:16AM (#39491267)
Count me in as one of the 99%
Re:GPT in 500 Lines of SQL by coldtea on Saturday February 24, 2024 @06:06PM (#39495961)
Parent obviously means childish as in "simple and easy manner" not as "something 10 year olds will understand".
Re:GPT in 500 Lines of SQL by pedrosorio on Saturday February 24, 2024 @01:46PM (#39494062)
This sorcery didn't start a year ago. The model being described in the article is GPT-2 which was released in early 2019.
Re:GPT in 500 Lines of SQL by huqedato on Saturday February 24, 2024 @06:49AM (#39490869)
Fantastic article. It kept my eyes on the screen for 2 hours, without interruption The author is a genius.
Re:GPT in 500 Lines of SQL by Hendrikto on Saturday February 24, 2024 @07:40AM (#39491083)
> Plain Unicode, however, doesn't really work well with neural networks.

That is not true. See ByT5, for example.

> As an illustration, let's take the word "PostgreSQL". If we were to encode it (convert to an array of numbers) using Unicode, we would get 10 numbers that could potentially be from 1 to 149186. It means that our neural network would need to store a matrix with 149186 rows in it and perform a number of calculations on 10 rows from this matrix.

What the author calls alphabet here, is typically called vocabulary. And you can just use UTF-8 bytes as your vocabulary, so you end up with 256 tokens, not 149186. That is what ByT5 does.

Re:GPT in 500 Lines of SQL by int_19h on Sunday February 25, 2024 @06:01PM (#39505836)
The point isn't that it doesn't work at all, but that it doesn't work as well as other approaches that we have. Which is evidenced by the fact that all the best-performing models on the market use tokenization. It's not a secret that tokenization is fundamentally a hack, and that ideally we'll get rid of it eventually one way or another (https://twitter.com/karpathy/status/1657949234535211009). And in principle, you can compensate for the deficiencies of byte-level tokenization with larger models and larger contexts. But what this means in practice is that a model that has the same level of intelligence (on most tasks; obviously, there are some specific tasks, like say counting characters in a word, where tokenization is detrimental to intelligence) takes a lot more resources to train, hence why we aren't seeing more of those.
Re:GPT in 500 Lines of SQL by deskamess on Saturday February 24, 2024 @08:19AM (#39491280)
In the tokenization example for '"Mississippilessly", why is 'si' not part of the combined column? It appears twice in the text. My speculation is that it got collapsed out with 'iss' (a longer token). Is that right?
Re:GPT in 500 Lines of SQL by remram on Saturday February 24, 2024 @12:43PM (#39493458)
Yes. At step 1 there are two 'i','s' and two 's','i', 'is' gets picked because it comes first. Then at step 7, because 'is' got formed, there are two instances of 'is','s' and only one instance of 's','i' so 'iss' gets formed.
Re:GPT in 500 Lines of SQL by deskamess on Saturday February 24, 2024 @03:02PM (#39494688)
Thanks.
Re:GPT in 500 Lines of SQL by cdelsolar on Saturday February 24, 2024 @08:59AM (#39491535)
Serious question, how do I get this smart?
Re:GPT in 500 Lines of SQL by anthomtb on Saturday February 24, 2024 @12:43PM (#39493459)
No doubt the author is a super genius ex-child prodigy whizzkid who emerged from the womb with the umbilical cord diagramming Euler's formula.

For real though, and knowing this is a leading question, the author has near-on 15 years of blog posts showing complex problems being solved in SQL. Is their brain bigger than yours and mine? Maybe a little bit. Do they have a ton of experience doing things like this? Most definitely.

Re:GPT in 500 Lines of SQL by fifilura on Sunday February 25, 2024 @02:01AM (#39498349)
Also remember he builds these things piece by piece from the inside out.

It is easy to get overwhelmed when looking at the end result.

To learn you should probably run one block at a time to understand what each piece does. (In "normal" languages those pieces would be an isolated function)

Re:GPT in 500 Lines of SQL by JonChesterfield on Saturday February 24, 2024 @10:29AM (#39492189)
Is this an accurate representation of the GPT driver loop?

    def generate(prompt: str) -> str:
      # Transforms a string into a list of tokens.
      tokens = tokenize(prompt) # tokenize(prompt: str) -> list[int]
    
      while True:
     
        # Runs the algorithm.
        # Returns tokens' probabilities: a list of 50257 floats, adding up to 1.
        candidates = gpt2(tokens) # gpt2(tokens: list[int]) -> list[float]
     
        # Selects the next token from the list of candidates
        next_token = select_next_token(candidates)
        # select_next_token(candidates: list[float]) -> int
     
        # Append it to the list of tokens
        tokens.append(next_token)
     
        # Decide if we want to stop generating.
        # It can be token counter, timeout, stopword or something else.
        if should_stop_generating():
          break
 
      # Transform the list of tokens into a string
      completion = detokenize(tokens) # detokenize(tokens: list[int]) -> str
      return completion

because that looks a lot like a state machine implementing Shlemiel the painter's algorithm which throws doubt on the intrinsic compute cost of the generative exercise.
Re:GPT in 500 Lines of SQL by NortySpock on Saturday February 24, 2024 @11:00AM (#39492532)
I think the "context window" that people refer to with large language models means there's a maximum number of tokens that are retained, with the oldest being discarded. The window is a sliding window.
Re:GPT in 500 Lines of SQL by canjobear on Saturday February 24, 2024 @04:24PM (#39495254)
Yes, that is the loop. All the magic is in the gpt2 function there.
Re:GPT in 500 Lines of SQL by creatonez on Saturday February 24, 2024 @05:41PM (#39495799)
This is a very small section of the algorithm. This is just how it collects the tokens it has generated into a sentence.
Re:GPT in 500 Lines of SQL by ein0p on Saturday February 24, 2024 @12:44PM (#39493469)
Unexpectedly insightful and answers some of the same questions I had early on: not just “how” questions, but “why” as well. You see the pattern with softmax quite often. I wish it was taught as “differentiable argmax” rather than by giving people a formula straight away. That’s not all it is, but that’s how it’s often used.
Re:GPT in 500 Lines of SQL by 101008 on Saturday February 24, 2024 @02:14PM (#39494304)
I keep reading that GPT is a "smarter" "complex" Markov, in the end, just a function spitting out next word with some probability.

But from my experience that cannot be true - it has to learn somehow. There is an easy example to make. Tell it something that happened today and contradicts the past (I used to test this with the Qatar World Cup), and then ask questions that are affected by that event, and it replied correctly. How is that possible? How a simple sentence (the information I provide) changes the probabilites for next token by that far?

Re:GPT in 500 Lines of SQL by pests on Saturday February 24, 2024 @04:23PM (#39495250)
Mostly #2 of what the other response says. The entire input to the NN is what it's learning off - the weights are static, you are changing the probabilities based on which context is provided and it's length.
Re:GPT in 500 Lines of SQL by Lerc on Saturday February 24, 2024 @02:20PM (#39494364)
There are two parts of the knowledge at play here.

1. The trained knowledge included in the parameters of the model

2. The context of the conversation

The 'learning' you are experiencing here is due to the conversation context retaining the new facts. Historically the context windows were very short and as the conversation continues it would quickly forget the new facts.

More recently context windows have grown to rather massive lengths.

Re:GPT in 500 Lines of SQL by drexlspivey on Saturday February 24, 2024 @04:54PM (#39495455)
Markov chains also depend on the initial state. In GPTs the initial state is your conversation history
Re:GPT in 500 Lines of SQL by Scene_Cast2 on Saturday February 24, 2024 @04:47PM (#39495401)
Interestingly, modern ML does not require Turing completeness. And yet, people are considering the possibility of AGI - I would find it pretty amusing if Turing completeness isn't necessary.
Re:GPT in 500 Lines of SQL by DennisP on Saturday February 24, 2024 @06:55PM (#39496234)
Seems to me that Turing completeness is necessary, for the simple reason that I can mentally trace through Turing-complete code.
Re:GPT in 500 Lines of SQL by r-w on Sunday February 25, 2024 @12:08AM (#39497831)
Without a piece of paper to take notes on? ;)
Re:GPT in 500 Lines of SQL by DennisP on Sunday February 25, 2024 @09:52AM (#39501230)
My own memory functions as the paper tape as long as the program is simple enough :)
Re:GPT in 500 Lines of SQL by int_19h on Sunday February 25, 2024 @04:42PM (#39505099)
Token inference by itself isn't Turing complete, but if its output can have side effects (e.g. editing the prompt for the next iteration), that's a whole different story.
Re:GPT in 500 Lines of SQL by mikeytown2 on Sunday February 25, 2024 @12:06AM (#39497818)
There a GitHub page for this?
Re:GPT in 500 Lines of SQL by pajop on Sunday February 25, 2024 @01:56PM (#39503606)
Re:GPT in 500 Lines of SQL by haolez on Sunday February 25, 2024 @12:23AM (#39497929)
Is there a world where this can be made performant? Just my geek curiosity kicking in, as I love Postgres and SQL.
Re:GPT in 500 Lines of SQL by firemelt on Sunday February 25, 2024 @01:18PM (#39503236)
laugh at people who overhyped it with singularity agi and skynet lmao
Re:GPT in 500 Lines of SQL by edpichler on Wednesday February 28, 2024 @05:51AM (#39536379)
What a beautiful blog post. I could understand more about LLM.