I've been experimenting with specialized small language models for a while. I tried many different ways to design and train them. They ended up with some interesting characteristics:

  • Easy to train. Models can be trained independently and each covers one or more domains.
  • Composable. Models can be loaded and unloaded at any time and run side by side.
  • Accurate. Models can be pretty accurate within their domains.

Here's what it looks like in a short demo. It starts with nothing loaded:

$ demo
Type a message or /help for the commands.

> hi
No model is loaded. Use /list and /load MODEL

No model, no response. Let's load one:

> /load conversation
Loaded conversation - 40M - 157MiB - 73ms

The model has about 40 million parameters. It loaded in 73ms1 and takes 157MiB of GPU memory. That's with unquantized weights.

Obviously this is a very small model and can't hold a deep conversation. Think of it as an NPC (non-player character) in a game. It knows greetings, thanks and a bit of small talk.

> hi
Hello
conversation - 86ms - 487 tokens/s

> how are you doing today?
I'm doing well. How are you?
conversation - 127ms - 513 tokens/s

> pretty good
Glad to hear it.
conversation - 113ms - 495 tokens/s

This conversation model handles greetings, thanks and small talk. But it doesn't know anything else. For example:

> what is 6 * 7?
No response.

No response is expected because the conversation model doesn't know anything about arithmetic. That being said there is an arithmetic model. Let's load it:

> /load arithmetic
Loaded arithmetic - 40M - 156MiB - 97ms

It reminds me:

- I know kung fu.

- Show me.

> what is 6 * 7?
42
arithmetic - 125ms (128ms) - 441 tokens/s (709 tokens/s)

> what is 12 * 6 * (30 - 2) + 1?
2017
arithmetic - 186ms (188ms) - 501 tokens/s (682 tokens/s)

Now we get a response. It comes from the arithmetic model we just loaded. The conversation model still receives the prompt and still returns no response.

You may notice multiple response time and token stats under each answer. The values in brackets represent the total response time and tokens per second for all loaded models. Models run in parallel and each one spends some time and tokens even when it has no response.

Let's do the same with algebra:

> 14x + 15 = 71
No response.

> /load algebra
Loaded algebra - 40M - 156MiB - 140ms

> 14x + 15 = 71
x = 4
algebra - 632ms (636ms) - 563 tokens/s (669 tokens/s)

So far we loaded three models for conversation, arithmetic and algebra. Together they take about 469MiB of GPU memory.

> /list
Model         Status  Params  Memory Init  Memory Peak  Load time
conversation  loaded     40M       157MiB       217MiB       73ms
arithmetic    loaded     40M       156MiB       212MiB       97ms
algebra       loaded     40M       156MiB       212MiB      140ms

Models can be unloaded at any time too. Let's unload the arithmetic model:

> /unload arithmetic
Unloaded arithmetic

> what is 12 * 6 * (30 - 2) + 1?
No response.

> how are you?
I'm doing well. How are you?
conversation - 138ms (141ms) - 464 tokens/s (707 tokens/s)

The arithmetic model is unloaded so we get no response again. The rest stay loaded and keep working.

How is this different than model routing?

Model routing is a common way to work with more than one language model. A router sits in front of the models and decides where each prompt goes. In most of these setups the models are general and any of them can answer the prompt. The router picks one that is good enough, cheap enough or available.

There is no model router here so nothing reads the prompt first and decides where it goes. Every loaded model gets the prompt. The models are specialized and only the ones that know the domain answer the prompt. No model is picked in advance.

One may argue that sending prompts to all loaded models costs more than routing them. That is a fair point. But the models are so small and they load and unload almost instantly. And remember that a model uses only a small number of tokens when it doesn't know the domain. By the way a model router can still sit in front of these models. In fact I'm thinking about implementing a model router specifically for these small models.

What is this good for?

These models are good for specialized tasks. They are pretty accurate within their domains and they are reluctant to answer anything else. That matters when a wrong answer is worse than no answer. For example a support assistant for a product or a learning app for children shouldn't answer questions about anything else.

They are also good when different capabilities are needed at different times. Models can be loaded and unloaded as needed. For example an NPC in a game only needs the models that match its role and an app only needs the models for the feature in use.

Technical Details

The three models have the same size. Each one has 12 layers, about 40 million parameters and is trained on its own dataset.

ModelTraining examplesValidation examplesTraining time1Validation accuracy
conversation22,5002,5001h 13m99.4%
arithmetic64,8817,20922h 6m98.8%
algebra46,7185,19114h 6m84.8%

Validation accuracy is the share of the validation examples that the model got right. These examples are kept out of training.

I'm pretty sure that accuracy can go higher with larger datasets, longer training, different batch sizes and other training parameters. Nevertheless these are still great numbers for training on a single 16GB GPU1.


There is more to specialized small language models than a demo can show. If you have any questions or comments feel free to reach out to me on X.

1

RTX 5060 Ti 16GB