SBForge: an AI framework in C#, and why I built it
SBForge trains and runs transformer models entirely in C#, with its GPU work on Vulkan. This is why I built it, what it does, and how it works.
SBForge is an AI framework I have been building under SwartBerg Studio. It trains and runs transformer models, and it is written entirely in C#: the data pipeline, the training coordinator, the host code for the GPU kernels, the inference SDK and the tools around them. Its GPU work runs on Vulkan compute shaders rather than on one vendor's toolkit.
This article covers why SBForge exists, what it does, and how it works.
Why I built it
To show that C# belongs in AI. The AI industry is almost entirely Python. I prefer to keep my own stacks in C# as far as I can, and I did not accept that machine learning had to be the exception. SBForge is my way of showing that a C# framework can train and run real models and be competitive while doing it.
Because I believe in dedicated models. Much of the industry is chasing single models that can do everything. I think the more useful future is models that are trained for one job and do that job well. It is the same as hiring. If I went looking for one person with every skill, I would probably not find them, and if I did, I could not afford their salary. It is far easier to find people who are good at a specific kind of work. Models are no different: a small model trained for a specific task costs much less to run than a large one that tries to cover everything.
So that models can live inside the software that uses them. A model that is called through a cloud API costs money every time it is used, needs a connection, and sends the user's data away from their device. A small, dedicated model can instead ship inside the application and run on the user's own hardware. SBForge produces a single checkpoint file that an application loads in process through its SDK, with no server or container to run alongside it.
One of the first models I want to build with SBForge shows what I mean: a product chat model for online shops. It is trained only on what a store sells, so a shopper can ask about its products in plain language and get an answer about that store's range. It does not need to know anything else, and it is cheaper to run because it does not try to.
Why Vulkan and not CUDA
Almost every deep learning framework runs its GPU work on NVIDIA's CUDA. It is mature and fast, but it runs on one vendor's hardware.
I prefer to design my applications so they are not locked to a vendor, and SBForge follows the same principle. Vulkan is an open GPU standard with drivers from the major GPU vendors, so one set of compute shaders can serve any GPU with a capable Vulkan driver, through one code path. That matters directly for dedicated models: an application that ships a model cannot assume its users own a particular brand of graphics card, and a shop that runs its own product model should be free to choose the hardware it runs on.
The choice has a cost. There are no ready-made libraries for this kind of work on Vulkan, so every kernel is written in-house: matrix multiplication, attention, normalisation, the optimizer step and quantisation. Each one has to be written, checked against a reference, and then made fast. The kernels are GLSL compute shaders, compiled to SPIR-V and validated at build time, so a shader that is broken fails the build instead of a training run. The matrix work runs in BF16 on the GPU's matrix units, with FP32 where accuracy needs it.
What SBForge does
- Data preparation. Download or point at text corpora, train a tokenizer and write the training shards. Every checkpoint records which corpora went into it.
- Training on the GPU. The forward pass, backward pass, loss, gradient clipping and optimizer step all run on the GPU. A test fails the build if any part of a training step runs on the CPU.
- Training models larger than the GPU. Weights, gradients and optimizer state can be streamed from host memory, so only part of the model has to fit in VRAM at a time.
- Training across machines. Several ordinary machines on a home or office network can train one model together. The design does not assume matched GPUs or a fast interconnect.
- Inference. A trained model loads from a single file and runs on the GPU, and can be quantised to INT8 or INT4 to make it smaller.
- Importing existing models. A published model can be imported from Hugging Face, which is also how SBForge's numerics are checked against a known reference.
How it is put together
SBForge is a small number of programs on top of a set of libraries:
| Component | What it does |
|---|---|
SBForge.Data |
Downloads corpora and writes raw text plus a manifest. |
TrainServer |
The coordinator. Holds the authoritative model, splits it into segments, prepares data and hands out work. |
TrainNode |
The GPU worker. Pulls its share of the model, trains it on the GPU and sends the result back. |
SBForge.Chat |
Loads a checkpoint so a person can talk to it and judge a run. |
SBForge.Sdk |
The inference SDK an application embeds. Kept small and compatible with ahead-of-time compilation. |
Nothing above the GPU layer refers to Vulkan directly. The rest of the framework talks to a compute contract, a set of interfaces for a device, its buffers and its operations, and Vulkan is the implementation of that contract. A dedicated test checks that the boundary holds.
For training across machines, SBForge does not exchange gradients on every step, which works in a data centre but not over an ordinary network. Each node trains on its own for a number of steps and then sends back how far its weights moved, an approach known as DiLoCo. The server combines those updates. It refuses updates that are too stale, drops any update containing a NaN before it can damage the model, and lets machines join, leave and rejoin a run while it keeps going.
How I check that it works
Training code can be wrong without crashing. A gradient that is slightly off, an optimizer setting that is quietly ignored, or a data loader that stops early will still produce a model and a falling loss curve. So I do not take a passing test suite as proof on its own.
- The GPU is checked against a reference. A plain CPU implementation of the model exists only in the test suite, and the GPU kernels are graded against it.
- A fix needs a test that fails without it. Before a bug fix is accepted, its test is run against the old code to see it fail, so the test is known to catch the problem it is named after.
- The whole suite runs regularly, not only before a release, and most of it runs real GPU kernels.
- Real runs, written down. Many of the most important defects were found by running real training jobs for hours across real processes, not by unit tests. I record what each run shows and what it does not, including claims that turned out to be wrong.
More to come
I will write more about SBForge here as it gets closer to release.
Get in touch
If you work on similar problems, or want to talk about using a dedicated model in your own software, I would like to hear from you. You can also follow the blog's feed.
Related articles
Why we only build in C# and .NET
Choosing one platform is not a limitation for us. It is the most important engineering decision we have made, and here is the reasoning.