5 ms·
Diffusion models from scratch, from a new theoretical perspective
- swyx 3y agooh this has code! great stuff. diffusion papers are famous for a lot of equations (https://twitter.com/cto_junior/status/1766518604395155830 https://twitter.com/cto_junior/status/1766518604395155830) but code is much more legible (and precise?) for the rest of us. all theory papers should come with reference impl code. i'd love an extension of this for the diffusion transformer, which drives Sora and other videogen models. maybe combine this post with https://jaykmody.com/blog/gpt-from-scratch/ https://jaykmody.com/blog/gpt-from-scratch/ and make the "diffusion transformer from scratch" intro
- GaggiX 3y ago>i'd love an extension of this for the diffusion transformer All you need to do is replace the U-net with a transformer encoder (remove the embeddings, and project the image patches into vectors of size n_embd), and the diffusion process can remain the same.
- swyx 3y agoseems too simple. isn't there also a temporal dimension you need to encode?
- GaggiX 3y agoFor the conditioning and t there are different possibilities, for example the unpooled text embeddings (if the model is conditioned on text) usually go in the cross-attn while the pooled text embedding plus t is used in adaLN blocks like StyleGAN (the first one), but there are many other different strategies.
- godelski 3y agoThey're famous for lots of equations, but truth be told, most diffusion researchers I know have the exact same response. A lot of people repeat the same exact equations and they only are there for review purposes. On the other hand, if you want to really dig in, I'd suggest reading into works by Kingma, Gao, Ricky Tian Qi Chen, and honestly, any of Max Welling's students (Tomczak (was post doc), Hoogeboom, etc), and of course the unsung hero Aapo Hyvärinen. Here's a taste at a Kingma & Gao work that's on the lighter side but relevant to the SD3 paper. The unfortunate part is that there's a lot of reliance on knowing and understanding prior works which make these less approachable, but honestly this is a bit difficult to call a meaningful critique (it's research, not educational work aimed at public). https://arxiv.org/abs/2303.00848 https://arxiv.org/abs/2303.00848
- swyx 3y agoyeah fair enough :) i am unfortunately not driven enough on diffusion models yet to need to dive into those but i hope a future seeker finds your references here.
- godelski 3y agoThat's fine! Not everyone needs to be. I think it is just a shame we pretend. Makes research convoluted and messy. As I said the other day, we should just allow people to pursue whatever they want and lay a framework where the incentives drive the clearest explanations possible. https://news.ycombinator.com/item?id=39665427 https://news.ycombinator.com/item?id=39665427
- api 3y agoHow long before more legible code or code-like constructs (or markup?) start to displace arcane mathematical notation? It would take quite a long time but I could see this slowly happening for analogous reasons to how the printing press favored alphabetic languages over pictorial ones. Computers teach us to read, write, and express linguistically and are now thanks to LLMs capable of working much more fluently with language. Squiggles and lots of Greek letters seem like an anachronism.
- godelski 3y ago> How long before more legible code or code-like constructs (or markup?) start to displace arcane mathematical notation? My best guess is never. The problem is that math is a language itself. In fact, I'd more accurately call it a family of languages. People often confuse it for an inherent thing because most of the time we use it in applied ways for subjects like physics, and the sciences. But clearly those people have never taken abstract algebra or developed their own toy algebra (or group, ring (or rung), or field). You might say they have an _ideal_istic notion of mathematics. That said, I am absolutely sympathetic to the complexity of mathematics and there is so much that is difficult to grasp. What's worse, is that when this abstract gobbledy goop finally clicks it's so abundantly obvious that it deceives you into thinking you've known it all along. I think this contributes to the difficulty of teaching the subject because those qualified have forgotten the struggles they themselves have had (to be fair, human psychology plays a big role here too because if we only remember the struggles we'd have difficulty finding the passion and beauty). But I think our best hope is to find a unified mathematics. The obsession people have with things like set theory and category theory is that you can see this interconnectedness. You get this deep feeling that you're looking at the same things manifesting in different ways. Remember that a lot of mathematics is converting your system into another system (we might call this an isomorphic mapping) such that your problem is easier to solve in the other system (think about how we might want to go from Cartesian coordinates to polar, but now abstract that concept out more than your friend that took a few tabs of acid). You zoom out move around, and zoom back in, and maybe a few more times (but you have to keep track of your path). > Squiggles and lots of Greek letters seem like an anachronism. Do you have another suggestion? Because I don't see how code, English, or literally any other thing that can be written down is anything more meaningful than "a bunch of squiggles." There's only so many simple, and importantly, distinguishable squiggles that we can write down. Luckily we have a tool to exactly determine this ;) The shitty part of mathematics is also the best part, in that you have to embrace the abstractness of it all. But of course human brains weren't designed for this. (Yes, there are some of us trying to figure out how to get machines to think with that extreme level of abstractedness at its core. No, this is not how computers already "think.") So yeah, I empathize with you. Quite a lot. There's so much more that I want to know about this thing too. But it is often seemingly impenetrable. All I can tell you is that it just looks that way and isn't. Everyone has the capacity but finding the right way up this mountain (that I'm nowhere near Terry, and he's nowhere near the summit) can be just as hard as the climb itself. The best way is persistence though. Just remember that you don't play video games because they are easy. Sometimes you gotta just trick yourself into pushing through. And some of the best advice I can give is revisiting old topics. So much of what you think you've known actually has a tremendous amount of depth to it. Coming back with fresh eyes and a better understanding of the larger picture can help you see all the beauty in it, even if this is sometimes fleeting. I'm sorry to have vomited so much, but you know, Stockholm Syndrome is a bitch.
- ycy 3y agoAuthor here, when I tried to understand diffusion models I realized that the code and math can be greatly simplified, which led to me writing this blog post and diffusion library. Happy to answer any questions.
- xchip 3y agoYour post is awesome and is explaining something nobody else did, thanks!
- thomasahle 3y agoYour `get_sigma_embeds(batches, sigma)` seems to not use its first input? Did you mean to broadcast sigma to shape (batches, 1)?
- ycy 3y agoMy intention was to omit the details of batching in the blog post for clarity of exposition, and I'll update the post accordingly. Sorry for the confusion, but you can see the full implementation here: https://github.com/yuanchenyang/smalldiffusion/blob/main/src/smalldiffusion/model.py https://github.com/yuanchenyang/smalldiffusion/blob/main/src...
- godelski 3y agoAs a researcher, there's a lot of diffusion blogs that I do not like. But I actually really do like this one! It does a great job at getting to the meat of things, showing some of the complexities (often missing) but without getting lost or distracted. I especially like the discussion of trajectories as this is motivating to many things I think a lot of people struggle with (e.g. with schedulers). That can be hard to write. Albeit not as complete, I think this is far more approachable than the Song or Lilian's blogs (great resources too, but wrong audience). Great job! I'm actually going to recommend this to others. FWIW, a friend of mine (and not "a friend of mine") wrote this minimal diffusion awhile back that I've found useful that's a bit more "full" w.r.t DDPM. More dropping here since I saw others excited to see code and it could provide good synergy here: https://github.com/VSehwag/minimal-diffusion/ https://github.com/VSehwag/minimal-diffusion/
- skybrian 3y agoThis is a nice explanation of the theory. It seems to be dataset-independent. I'm wondering about the specifics of generating images. For example, what is it about image generators makes it hard for them to generate piano keyboards? It seems like some better representation of medium-distance constraints is needed to get alternating groups of two and three black notes.
- Vecr 3y agoIt's the finger problem, you've got to get the number, size, angle, position, etc. right every single time or people can tell very fast. It's not like tree branches where people won't notice if the splitting positions are "wrong".
- strangecasts 3y agoSuper interesting! Immediately reminded of Iterative alpha-(de)Blending [1] which also sets out to set up a conceptually simpler diffusion model, and also arrives at formulating it as an approximate iterative projection process - I think this approach allows for more interesting experiments like the denoiser error analysis, though. [1] https://arxiv.org/pdf/2305.03486.pdf https://arxiv.org/pdf/2305.03486.pdf
- adamnemecek 3y agoAll machine learning models are convolutions, mark my words.
- sva_ 3y agoI think you posted this a few times, maybe you want to elaborate on this point? I have trouble seeing reinforcement learning as convolution, for example.
- adamnemecek 3y agoIt's a Fredholm equation which in turn is a convolutional equation.
- carlthome 3y agoNext you're gonna say that polynomial multiplication is also just convolution of the arguments.
- adamnemecek 3y agoIt's not the standard convolution.
- hotdogscout 3y ago[flagged]
- acekingspade 3y agoGood article, but I feel that it misses an important property of diffusion models that they model the score function (derivative of log prob) [1] and that diffusion sampling is akin to Langevin dynamics [2]. IMO these explain why it's easier to train these models than GANs, because of an easier modeling objective. [1] https://yang-song.net/blog/2021/score/ https://yang-song.net/blog/2021/score/ [2] https://lilianweng.github.io/posts/2021-07-11-diffusion-models/ https://lilianweng.github.io/posts/2021-07-11-diffusion-mode...
- ycy 3y agoYes, these blog posts offer a different perspective on diffusion models from the "projection onto data" perspective described in this blog post. You can view them as different ways of interpreting the same training objective and sampling process. In our perspective, diffusion models are easier to train because instead of predicting the gradient of the _exact_ distance function, the training objective predicts the gradient of a _smoothed_ distance function. Sampling the diffusion model is akin to taking multiple approximate gradient steps. To gain a deeper understanding of diffusion models, I encourage everyone to read all of these blog posts and learn about the different interpretations :)
- dr_dshiv 3y agoIs part of the idea of diffusion that you get a huge amount of training data? Like, you get to contrast all of these randomly diffused images with the undiffused image?
- porphyra 3y agoAnother great post is also called Diffusion Models From Scratch: https://www.tonyduan.com/diffusion/index.html https://www.tonyduan.com/diffusion/index.html That goes into a lot more mathematical detail but is also accompanied by a minimal, very easy to understand <500 line implementation.