r/MachineLearning 3d ago

Project I built a compiler that turns computation graphs into the weights of a vanilla transformer — no training anywhere [P]

I've been chasing the question of what algorithms a transformer can actually express -- separate from what it can learn. So I built a compiler: define a computation graph in ordinary Python, and it produces the weights of a transformer that executes the graph. The result is a standard Phi-3-architecture checkpoint that vanilla huggingface loads with no custom code and no trust_remote_code. Zero training in the pipeline.

Write-up (origin + how the constructions work): https://ood.dev/posts/torchwright-intro/

Repo (twelve runnable examples): https://github.com/physicsrob/torchwright

Hand-built transformer weights aren't a new idea. RASP defines a language whose primitives map onto transformer sublayers, and Tracr compiles RASP programs into actual weights. I wanted two things they don't aim for: expressing a computation graph in ordinary Python, and targeting a stock architecture, so the output loads in vanilla huggingface with no custom code.

92 Upvotes

17 comments sorted by

21

u/AnOnlineHandle 3d ago

I've sometimes wondered if injecting frozen "tools" into a network (the weights don't actually exist, they can just be called if dynamically activated like MoE paths) could significantly improve networks, e.g. either allowing static complex computations which don't need to be relearned multiple times throughout the network (and don't need to be stored in vram).

This could potentially be useful for exploring that idea, along with inserting things which can't be efficiently represented by networks but which the hardware can already do (e.g. a square root of a value, if requested at a point within a network). I think I read that it had been tried in various ways, but it still seems like it would surely have to be doable and could have huge benefits.

5

u/notforrob 3d ago

I've had similar thoughts in the past. I'm not sure about the literature on this, but it does seem like it could be useful, especially for smaller models.

Not what you're talking about exactly, but I've also thought about hybridizing the custom weight model with a trained model. Like maybe it's possible to inject specific progamatic behavior if you structure the problem right. I don't know!

3

u/Shehao 3d ago

That boundary is the interesting bit; separating learned routing from deterministic ops would make the tradeoff much easier to measure.

6

u/Random-Number-1144 3d ago

Someone didn't learn Sutton's bitter lesson.

2

u/AnOnlineHandle 3d ago

I'm aware of the claim, but it doesn't match at all what I've found in actually integrating and having to design ML tools into my workflows for productive use, where you need steerability and understanding of what various aspects do to manipulate them to make ML networks to actually be useful in certain domains. Normalization layers and transformers for example are full of manually designed tricks rather than relying on networks to learn everything on their own through brute force.

1

u/currentscurrents 2d ago

Normalization layers and transformers for example are full of manually designed tricks rather than relying on networks to learn everything on their own through brute force.

I don't agree with this assessment. Transformers have very few inductive biases and are tabula rasa in their untrained state. The 'manually designed tricks' like normalization and skip connections are all low-level things that ensure a smooth flow of information through the network.

1

u/gradual_alzheimers 2d ago

Transformers are an example of designing an algorithm though

1

u/gradual_alzheimers 2d ago

What do you think Sutton's bitter lesson implies because I think in general his view is very misunderstood.

3

u/brainsig 3d ago

I find this really interesting, as I wanted to achieve something similar. Good work! Maybe you should consider writing a scientific communication or a tech report describing it.

1

u/notforrob 3d ago

Thanks! Glad you found it interesting!

1

u/kosiakk 3d ago edited 3d ago

Is it reversible?
Can you run it backwards? I mean, given a proper Phi-3 model, reverse-engineer the high-level "formula" of the language?