frontpage.
newsnewestaskshowjobs

Made with ♥ by @iamnishanth

Open Source @Github

Open in hackernews

I have reimplemented Stable Diffusion 3.5 from scratch in pure PyTorch

https://github.com/yousef-rafat/miniDiffusion
360•yousef_g•14h ago

Comments

squircle•14h ago
Although I'm leaning heavily away from being passionate about software development, this is a cool project, and its freaken awesome how anyone can now reinvent the wheel from first principles.
albert_e•13h ago
Sounds like a great resources for learners.

Just wondering aloud --

Is there a tutorial/explainer by any chance that a beginner could use to follow along and learn how this is done.

an0malous•13h ago
fast.ai has a course on building Stable Diffusion: https://course.fast.ai/Lessons/part2.html
BinaryMachine•11h ago
Great resource Jeremy Howard is awesome, I have been waiting to take this course and follow along because anything older than a year in Deep Learning is already outdated. I hope they release a new version.
whiplash451•8h ago
I don’t think this is true. The fast.ai class covers a lot of fundamentals that are still valid and useful today.
b0a04gl•13h ago
does the DiT here actually capture cross-token attention the same way as full SD 3.5 or is it simplified for clarity?
reedlaw•13h ago
I'm not sure what this means. If it means the Stable Diffusion 3.5 model, why is it fetching that here: https://github.com/yousef-rafat/miniDiffusion/blob/main/enco...

The training dataset is very small, only including fashion-related pictures: https://github.com/yousef-rafat/miniDiffusion/tree/main/data...

yousef_g•13h ago
The dataset is for trying out fine-tuning of the diffusion model. It's a reimplementation of SD3 by writing the code from scratch again, but the weights are taken from HuggingFace due to hardware constraints on my part.
reedlaw•12h ago
So this implements SD3 inference and fine-tuning?
jatins•1h ago
> It's a reimplementation of SD3 by writing the code from scratch again, but the weights are taken from HuggingFace due to hardware constraints on my part.

Could you clarify what you mean by this part -- if the weights are taken from HF then what's the implementation for?

CamperBob2•13h ago
Add a Hugging Face Token in get_checkpoints.py before running the script.

Can you be a bit more specific here? It's not clear what such a token is, what it takes to get one, or where it would be placed in get_checkpoints.py.

einsteinx2•12h ago
> what such a token is

An API token from Hugging Face

> what it takes to get one

You generate them in your Hugging Face account

> where it would be placed in get_checkpoints.py.

Line 59 in the empty quotes where it says token = “”

CamperBob2•11h ago
Ah, I see it now, thanks.

That's the kind of thing that, stylistically speaking, it's good to define at the very top of the module.

einsteinx2•6h ago
Agreed. I’m not part of the project I just saw your comment and figured I’d try and help.
Dwedit•7h ago
Leaving off the "API" part from "API Token" causes confusion, since AI models tokenize all text into "tokens" before running the model. It's using the same word to describe two very different things.
einsteinx2•6h ago
Yep totally. Fwiw I’m not part of the project I just saw the comment and figured I’d try and help.
theturtle•13h ago
Cool. Can it still make images of Anne Hathaway leading a herd of blue giraffes on the Moon?
IncreasePosts•10h ago
Seems difficult, as there are no known portraits of Anne Hathaway
liuliu•12h ago
If you are interested in this: Flux reference implementation is very minimalistic: https://github.com/black-forest-labs/flux/tree/main/src/flux

The minRF project is very easy to start with training small diffusion models with rectified flow: https://github.com/cloneofsimo/minRF

Also, the reference implementation of SD 3.5 is actually minimalistic too: https://github.com/Stability-AI/sd3-ref

doctorpangloss•10h ago
Reference implementations are unmaintained and buggy.

For example https://github.com/huggingface/transformers/issues/27961 OpenAI's tokenizer for CLIP is buggy, it's a reference implementation, it isn't the one they used for training, and the problems with it go unsolved and get copied endlessly by other projects.

What about Flux? They don't say it was used for training, it wasn't, there are bugs with it that break cudagraphs or similar that aren't that impactful. On the other hand, it uses CLIP reference, and CLIP reference is buggy, so this is buggy...

42lux•10h ago
You can disable clip l on flux without a loss in quality. You are also making an elephant out of a fly. CLIP is used everywhere.
doctorpangloss•5h ago
Consider another interpretation: CLIP L in Flux can be disabled without a loss in quality because the way it is used is buggy!
electroglyph•7h ago
It shouldn't take a lot of effort to fix a tokenizer...
doctorpangloss•2h ago
People are a little too blinded by the insight porn of matching buggy behavior to just read and comprehend the issue. They can’t engage with the simpler and more pornographic insight porn that the reference implementations are buggy and do not match the trained artifacts.
liuliu•4h ago
Congrats on finding a bug!

However, the keyword here is training / inference divergence. Unfortunately, nobody is going to spend multi-million to retrain a model, so our reimplementation needs to be bug-to-bug correct to use the trained weights properly. That's why the reference implementations are essential because it is from the original model trainers so you have the best "bet" on matching the training code properly.

To give you some concrete example of bugs we needs to maintain:

1. In SDXL, they use OpenClipG for text encoding, but wrongfully uses 0 as padding tokens (corresponding to symbol "!") whereas even for OpenClipG its own training, the endoftext token was used as padding token. However, if you switching SDXL to use endoftext token as padding token, due to training / inference divergence, you get subpar generated images.

2. In FLUX, we mainly use T5 as text encoder. However, T5 usually used as encoder with mask to exactly the same input length, to avoid extended impact of padding tokens. In FLUX, we don't apply mask for T5 text encoding, hence intuitively causing padding token to take more effect than it should. Again, "fixing" this bug without retraining you will get subpar generated images.

There are many examples like this, some are easier to fix some are not (HiDream uses a different ODE solver that is different than what we usually do for rectified flow, hence you need to negate its prediction to be compatible with existing samplers, but this is "easier to fix").

TL;DR: Yes, there are bugs in software, but we better to maintain bug-to-bug compatibility than trying to "fix" it, hence highlight the importance of a "done" reference implementation, rather than a usual "active" implementations in software industry otherwise.

(I maintain the most complete reimplementation of SoTA media generation models in Swift: https://github.com/drawthingsai/draw-things-community/tree/m.... So I tend to think that I know one or two about "reimplementation from scratch".)

doctorpangloss•2h ago
I think if you read the issue carefully you would understand that the CLIP implementation in transformers and as published by OpenAI is wrong and does not match their trained model code; and that doing the fix I suggest, empirically for me and in theory, improves results.
vergessenmir•12h ago
Is there any notable properties of this implementation, are some parts slower, faster etc
NoelJacob•12h ago
So, that's Stable Diffusion without license constraints, is it?
Sharlin•10h ago
No, the inference/training algorithms, being math, are not copyrightable. OP just wrote another implementation. What's copyrighted are the models, which OP did not train from scratch (having neither the training material nor the compute to do that).
echelon•10h ago
We should be specific when we say "models".

The code outlining the network vs. the resultant weights. (Also vs. any training, inference, fine tuning, misc support code, etc.)

The theoretical diagram of how the code networks and modules are connected is math. But an implementation of that in code is copyrightable.

Afaik, the weights are still a grey area. Whereas code is code and is copyrightable.

Weights are not produced by humans. They are the result of an automated process and are not afforded copyright protection. But this hasn't been tested in court.

If OpenAI GPT 4o weights leak, I think the whole world could use it for free. You'd just have to write the code to run them yourself.

bravesoul2•4h ago
I use model architecture for the code/math and weights for the weights to avoid confusion!

Then there are hyperparameters which are also needed to be known to use the weights with the model architecture.

vrighter•8h ago
which means he is still in full violation of their license
Zambyte•6h ago
> What's copyrighted are the models

Has this actually been tested yet? Or are we still at the stage of AI companies trying to pretend this into reality?

dheera•5h ago
I mean, if you take a match to a blank CD-ROM, or shoot neutrinos at a USB drive, there is a very small chance that you get the SD weights stored on them
Zambyte•1h ago
You can say that about literally any digital information. This isn't really interesting in the context of the copyright status of AI models.
bravesoul2•4h ago
If the models are copyright protected then presumably they obeyed license on the upstream dependencies they included (i.e. the training data).
bravesoul2•4h ago
Oh :( wasn't what I thought it would be. Wondered why it wasn't more blown up on HN!
caycep•11h ago
How usable is the original academic source available from Ludwig Maximilian University CompViz group?
eapriv•11h ago
I find it hilarious that “from scratch” now somehow means “in PyTorch”.
monsieurbanana•11h ago
If any "from scratch" post doesn't start with linking to a Primitive Technology video, I'm closing the tab
mkoubaa•9h ago
Unless the author was raised by chimps I'm out
0cf8612b2e1e•8h ago
Not fusing heavier elements from hydrogen? I’m out.
chairmansteve•9h ago
Yeah. Should have done it in assembly.
mardifoufs•7h ago
Pytorch is a pretty basic building block when you get to some degree of model complexity. It wouldn't really be interesting to implement autograd or some other things pytorch provides imo when the goal is to show a reimplantation of something as "higher" level as SD. It's similar to how I don't mind it when someone doesn't reimplement an OS, or a JavaScript engine when writing a web app from scratch.

And there's been a recent surge in abstractions over pytorch, and even standalone packages for models that you are just expected to import and use as an API (which are very useful, don't get me wrong!). So it's nice to see an implementation that doesn't have 10 different dependencies that each abstract over something pytorch does.

refulgentis•9h ago
I'm embarrassed to ask: can someone elaborate on, say, what we have now that we didn't have before the repo existed?

I have studiously avoided making models, though I've been adjacent to their output for years now... I think the root of my confusion is I kinda assumed there was already PyTorch based scripts for inference / training. (I assumed _at least_ inference scripts were released with models, and kinda figured fine-tuning / training ones were too)

So then I'm not sure if I'm just looking at a clean room / dirty room rewrite of those. Or maybe everyone is using "PyTorch" but it's usually calling into CUDA/C/some proprietary thingy that is much harder to grok than a pure PyTorch impl?

Anyways, these arent great guesses, so I'll stop myself here. :)

_tqr3•8h ago
Stability AI, creators of Stable Diffusion models release their products under own Stability AI Community License which is not "free" like MIT license. You are not allowed to modify the weights in certain ways.

This package is basically running the model (inference) and maybe fine tuning it using existing AI weights. A great way to learn but still could run into same licensing issue.

refulgentis•7h ago
You can't finetune SD 3.5!?

I thought the community license stuff was about keeping people from using it in prod and charging for it without Stability getting at least a small taste.

This sucks.

I haven't been keeping up with gooner squad on Civit, but I did have some understanding SD was less popular, but I thought it was just because 3.5 came far too long after Flux with too little, if any, quality increase to be worth building new scaffolding for.

fc417fc802•49m ago
> You can't finetune SD 3.5!?

They don't want you finetuning it in specific ways that might make them look bad by association.

rockemsockem•6h ago
I believe this is the main piece

> with minimal dependencies

I haven't tried running SD 3.5 specifically, but it's built on hugging face libraries which I personally always find to be a mess of dependencies that make it really hard to setup without the exact configuration the original developers used (which is often not provided in enough detail to actually work). This makes it pretty hard to run certain models especially if it's a few months/years after the original release.

For example this appears to be the requirements for the stability AI reference implementation for SD3.5 and there are no versions specified and it includes "transformers" which is just an enormous library.

https://github.com/Stability-AI/sd3.5/blob/main/requirements...

refulgentis•6h ago
Ah, tyvm, that maps well onto my knowledge set, I have a ONNX inference wrapper written in Dart. However, I have never been able to leverage transformers.js ONNX demo code, i.e. have a reference to port to Dart.

IIRC it is written in an abstraction layer that supports a transformers-like API surface. This also makes it opaque to figure out what you're actually passing to the model, adding a Python dep mess on top of that...woo boy.

hkon•9h ago
now do it in minecraft
ineedasername•7h ago
When I think of SD 3.5 (or any version) I think of the portion that results from training, i.e., the weights. The code seems less important? I mean as far as output quality is concerned, or performance. But I'm honestly not sure, and not trying to judge these efforts on that basis.
Dwedit•6h ago
Does using pure PyTorch improve performance on non-NVIDIA cards in any way? Or is PyTorch so highly optimized for CUDA that no other GPU vendors have a chance?
VeejayRampay•6h ago
I believe pytorch works nicely with rocm, but I don't know if it's nicely to the point where it's "on par"
3abiton•6h ago
It seems to be the case, although pytorch rocm is coming around slowly. Very slowly, if you get it working that is.
chickenzzzzu•6h ago
It is possible to run ML workloads on for example AMD devices via Vulkan. With newer extensions like cooperative matrix, and maybe also in the future some scheduling magic exposed by the driver through a new extension, the remaining single digit percent gap CUDA has will evaporate.
jwitthuhn•6h ago
Pytorch also runs great on apple silicon, though it is hard to directly compare because Apple's high end GPUs can't compute anywhere near as much as nvidia's high end stuff.

e: I'll also add that pytorch does still have one oddity on apple silicon which is that it considers each tensor to be 'owned' by a particular device, either a cpu or gpu. Macs have unified memory but pytorch will still do a full copy when you 'move' data between the cpu and gpu because it just wasn't built for unified memory.

brcmthrowaway•5h ago
Does pytorch work on AS out of the box? Or do you need some apple specific package
thom•2h ago
`uv pip install torch` just works, set your default device to `mps:0`, enjoy the RAM. Depends what you're doing though - some stuff isn't implemented, so if you're trying to fit a Gamma/Beta/Student-T distribution you're out of luck.
SV_BubbleTime•5h ago
All twelve people using SD 3.5 may be interested in this.
godelski•4h ago

       self.q = nn.Linear(embed_size, embed_size, bias = False)
       self.k = nn.Linear(embed_size, embed_size, bias = False)
       self.v = nn.Linear(embed_size, embed_size, bias = False)
Try

       self.qkv = nn.Linear(embed_size, 3*embed_size, bias = False)

    def forward(...):
       ...
       qkv = self.qkv(x)
jszymborski•4h ago
This adds connections between the parameters of q, k, and v whereas the original doesn't, unless my very tired brain is missing something.
smus•2h ago
Nope, they all depend on x and the same is true in this scenario
godelski•26m ago
It is actually really common practice. It is a single linear layer so there's no connection intranodes. The reason to do this is because it is a bit less computationally intensive.

tldr: linear layers have an associative property

nothrowaways•2h ago
Pure pytorch?

Zero-Index, O(1) Data Engine with Native Natural-Language Querying

1•mymanagertech•14m ago•0 comments

Notes on – Anthropic: How we built our multi-agent research system

https://simonwillison.net/2025/Jun/14/multi-agent-research-system/
2•thunderbong•22m ago•0 comments

John Wells, 64, Who Fled New York for the Solitude of the Desert, Dies

https://www.nytimes.com/2025/06/12/style/john-wells-dead.html
2•mitchbob•24m ago•2 comments

Graduate student develops an A.I.-based approach to restore damaged paintings

https://www.smithsonianmag.com/smart-news/graduate-student-develops-an-ai-based-approach-to-restore-time-damaged-artwork-to-its-former-glory-180986799/
1•jmillikin•25m ago•0 comments

O3-Pro Speculative Reasoning

https://smw.ai/blog/comparison-of-o3-pro-gemini-2-5-pro-and-opus-4-max
1•aethelyon•32m ago•1 comments

I want to build a list of weird software developers like me. Are you weird too?

https://humanprogrammers.com
1•olinelson•36m ago•0 comments

'Gas station heroin' is technically illegal and widely available

https://apnews.com/article/tianeptine-gas-station-heroin-fda-poison-4c8f6b98c9c615c9236ac950ce34850c
4•rmason•37m ago•0 comments

BeOS

https://en.wikipedia.org/wiki/BeOS
3•RyanShook•44m ago•0 comments

IC3PEAK - Marching [video]

https://www.youtube.com/watch?v=xqohApD6Ng8
1•consumer451•52m ago•0 comments

Everybody in History, Everywhere, All at Once

https://fi-le.net/history/
1•jfil•54m ago•0 comments

Show HN: Freaky Font

https://freakyfont.app
1•hapJam•1h ago•0 comments

Novo Nordisk's patent on Ozempic expired in Canada over unpaid $450 late fee

https://bsky.app/profile/dataandpolitics.net/post/3lrlwurbnrc2j
4•donohoe•1h ago•1 comments

The Travel Writer's Dilemma: Share, or Gatekeep?

https://www.nytimes.com/2025/06/10/travel/travel-writing-secret-discoveries.html
2•Thevet•1h ago•0 comments

Dice Identification 101: Mold Characteristics (2019)

https://dicemaniacsclub.wordpress.com/2019/08/29/dice-identification-101-mold-characteristics/
1•zdw•1h ago•0 comments

The Cadence (2020)

https://medium.com/craft-ventures/the-cadence-how-to-operate-a-saas-startup-436aa8099e8
1•gone35•1h ago•1 comments

New York passes a bill to prevent AI-fueled disasters

https://techcrunch.com/2025/06/13/new-york-passes-a-bill-to-prevent-ai-fueled-disasters/
2•dsubburam•1h ago•0 comments

Show HN: I created a multiple JSON queries tool

https://app.postpilot.dev/
1•anlac96_it•1h ago•0 comments

Streamlit state management for React developers

https://outlore.dev/blog/streamlit-state-management/
1•outlore•1h ago•1 comments

How I monitor and control all my powered devices (Zigbee and HA)

https://www.jeffgeerling.com/blog/2025/how-i-monitor-and-control-all-my-powered-devices-zigbee-ha
1•mikece•1h ago•0 comments

Bear Essentials Vlog

https://old.reddit.com/r/midjourney/comments/1l7z2b6/things_are_getting_crazy_bear_essentials_vlog/
1•b0ner_t0ner•1h ago•0 comments

Reclaiming the future: Māori voices leading in the age of AI and quantum tech

https://thespinoff.co.nz/atea/09-06-2025/reclaiming-the-future-maori-voices-leading-in-the-age-of-ai-and-quantum-tech
1•gnabgib•1h ago•0 comments

I have discovered a new number (with Mathematical Proof)

1•resident_person•1h ago•2 comments

Show HN: I made an online Unicode Cuneiform digital clock

https://oisinmoran.com/sumertime
1•OisinMoran•1h ago•0 comments

Somalia's construction boom in Mogadishu gives women high ambitions

https://www.bbc.com/news/articles/cj427dy11j0o
2•1659447091•1h ago•0 comments

Making Software Like Lego: How Aspire Brings the Pieces Together

https://medium.com/@davidfowl/making-software-like-lego-how-aspire-brings-the-pieces-together-d6a99c2c4cde
1•vyrotek•1h ago•0 comments

Some sunscreen ingredients may disrupt sperm cell function (2016)

https://medicalxpress.com/news/2016-04-sunscreen-ingredients-disrupt-sperm-cell.html
1•OutOfHere•1h ago•1 comments

Show HN: Jiffly - AI agent that finds, calls, & books the best local services

https://getjiffly.com/
1•titusblair•1h ago•0 comments

Large language models often know when they are being evaluated

https://arxiv.org/abs/2505.23836
31•jonbaer•2h ago•35 comments

Show HN: An LLM Running on a PS Vita

https://github.com/callbacked/psvita-llm
4•callbacked•2h ago•0 comments

The State of Rideshare and Autonomous Vehicles

https://www.platformaeronaut.com/p/the-state-of-rideshare-and-autonomous
2•apesti•2h ago•1 comments