Skip to content

Replace transformer blocks config with building blocks - #470

Merged
jonatanklosko merged 1 commit into
mainfrom
jk-transformer-building-blocks
Sep 30, 2026
Merged

jonatanklosko merged 1 commit into
mainfrom
jk-transformer-building-blocks

Conversation

@jonatanklosko

Copy link
Copy Markdown
Member

Given the similarity between LLMs, we have a single transformer function that each model called with a bunch of options (and the options were passed a few levels down where they were actually relevant, so adding a new option involved some boilerplate). However, as each model introduces some changes or new approach, it grows the option list. In a few cases, we had to add callbacks to let the caller further override the behaviour. This makes it progressively hard to tell the overall shape of a given model.

On the other hand, we don't want to duplicate all code per model. Being self-contained has some advantages, but there are fundamental layer/math parts that we don't want to diverge and potentially re-review across models.

This PR changes it to a better middle ground, which is, we replace the one massive function with a few building blocks. This means each model has a bit more code, but it also has flexibility to arrange layers and their order as necessary, and that flow stays visible within the model. Models that change something more fundamentally, can break further break apart into using smaller building blocks.

We still have Layers.Transformer.blocks/3 function that covers the repeated block structure of most LLMs, but it now accepts a function to build each block in entirety, and that gets composed out of smaller pieces.

Models now write the block body as plain code, with Layers.Transformer.blocks/3
handling the cache and outputs, and shared helpers for attention and FFN.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@jonatanklosko
jonatanklosko merged commit 30b7d95 into main Sep 30, 2026
2 checks passed
@jonatanklosko
jonatanklosko deleted the jk-transformer-building-blocks branch September 30, 2026 14:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant