Skip to content

Train models with JAX and Flax.

Dew trains language models, diffusion models and JEPA encoders. You write a Flax model and an objective, and one trainer compiles the step, shards it over your devices, keeps a moving average and checkpoints the run.

uv pip install "dew-ml @ git+https://github.com/AshishKumar4/dew"
train.py
import itertools
import jax
import numpy as np
import optax
from dew import Dataset, Trainer, models
from dew.data import ByteTokenizer
from dew.objectives.lm import LMObjective
from dew.sampling import Sampling, generate
tokenizer = ByteTokenizer()
text = tokenizer.encode("dew trains jax models. " * 3)
batch = {"text": np.tile(np.asarray(text[:65], np.int32), (8, 1))}
data = Dataset(train=lambda partition: itertools.repeat(batch),
val=None, records=8, batch=8)
model = models.build(
"causal_transformer", vocab_size=tokenizer.vocab_size,
emb_features=64, num_layers=2, num_heads=4,
mlp_features=256, max_seq_len=128)
objective = LMObjective(model, seq_len=64)
trainer = Trainer(objective, optax.adamw(3e-3),
key=jax.random.key(0))
state = trainer.fit(data, steps=100, log_every=25)
prompt = [tokenizer.encode("dew")]
out = generate(model, state.params, prompt, max_new_tokens=40,
key=jax.random.key(1),
sampling=Sampling(temperature=0))
print(tokenizer.decode(out.tokens[0]))
python train.py
Training from step 0 to 100 on {'data': 1, 'expert': 1, 'fsdp': 1, 'tensor': 1, 'sequence': 1, 'stage': 1} (1 process(es))
step 25: loss 0.0336
step 50: loss 0.0107
step 75: loss 0.0070
step 100: loss 0.0053
Goodput: first step after 6.81 s, 23.4% of the wall time in steps
dew trains jax models. dew trains jax model

Recorded on the CPU of a Colab runtime with 2 vCPUs, using JAX 0.11.2 and Dew at 60d49d2, on 2026-09-24. The script took 19 s.

A model, an objective, data and a trainer

The model is a plain Flax module. The objective says how to initialize it and what the loss is. The trainer owns everything else, so a new loss is a new objective and the training loop stays as it is.

  1. ModelA flax.linen.Module. Built-in ones come from models.build(name, ...).
  2. Objectiveinit(key) returns the variables, loss(variables, batch, step) the loss and metrics.
  3. DatasetTwo iterator factories, for training batches and one validation pass.
  4. TrainerCompiles the step, places the state on the mesh, updates the EMA, writes checkpoints and logs.
regression.py
import itertools
import flax.linen as nn
import jax
import jax.numpy as jnp
import numpy as np
import optax
from dew import Aux, Dataset, Field, InputSpec, Objective, Trainer
class Regression(Objective):
model = nn.Dense(1)
inputs = InputSpec(Field("x", (1,)))
def init(self, key, variables=None):
return self.model.init(key, jnp.ones((1, 1)))
def loss(self, variables, batch, step):
prediction = self.model.apply(variables, batch["x"])
loss = jnp.mean((prediction - batch["y"]) ** 2)
return loss, Aux(metrics={"mse": loss})
x = np.linspace(-1, 1, 32, dtype=np.float32).reshape(32, 1)
batch = {"x": x, "y": 2 * x + 1}
data = Dataset(train=lambda partition: itertools.repeat(batch),
val=None, records=32, batch=32)
objective = Regression()
trainer = Trainer(objective, optax.sgd(0.1), key=jax.random.key(0))
state = trainer.fit(data, steps=100, log_every=50)
print(objective.model.apply(state.params, jnp.array([[0.0], [1.0]])))
Output
Training from step 0 to 100 on {'data': 1, 'expert': 1, 'fsdp': 1, 'tensor': 1, 'sequence': 1, 'stage': 1} (1 process(es))
step 50: loss 0.0002
step 100: loss 0.0000
Goodput: first step after 0.75 s, 20.5% of the wall time in steps
[[0.99999994]
 [2.999413  ]]

What you can train

Each of these is an objective in the library, trained by the same trainer on the same mesh.

Language models

Pretraining on token windows or packed documents, chat fine-tuning that counts only the assistant tokens, and cached generation.

Diffusion language models

Masked diffusion over tokens (MDLM), LLaDA and Dream continued from their released weights, and block diffusion with Diffusion Gemma.

Representation learning

I-JEPA and V-JEPA: a context encoder, a moving-average target encoder, a predictor and block masks, with linear and k-NN probes.

Post-training

SFT, DPO, GRPO and PPO on the same trainer, asynchronous RL whose rollouts come from Dew, vLLM or SGLang, and Flow-GRPO for images.

Mixture of experts

Routed and shared experts, bias balancing, routing replay, grouped-matmul kernels, and expert parallelism over an expert mesh axis.

A grid of 32 flower images generated from noise by a small diffusion transformer
Flowers sampled from noise by the 9M-parameter diffusion transformer that tutorial 02 trains in 6,000 steps.
ROMEO:
Tut Tybalt, Gracious fazous to deparal,
Your bring shall be find that is in the sign,
Why both his hardy nundrest to an all ment.

RICHMOND:
Let must be, my lord, if you that day:
But yea, sir, then time and with himself;
Her oft protection to his death to the supper your
mother married to men.

HERMIONE:
And not that his by the duke a heaveness.

Third I change it another canst thou beast;
When
Text from the 14M-parameter byte-level model of tutorial 05, after 1,500 steps on Tiny Shakespeare.
The same four starting noises sampled with seven different solvers at 40 steps
Seven solvers on one checkpoint and one set of starting noises, from tutorial 04.

Pretrained checkpoints, in and out

load_pretrained reads Hugging Face checkpoints of 33 decoder families into Dew's own modules, and Pretrained.save writes trained weights back in the source's layout. This list comes from the code's registries when the site is built.

Dense decoders

  • Llama
  • Mistral
  • Ministral
  • Qwen 2
  • Qwen 3
  • Qwen 3.5, text
  • Gemma
  • Gemma 2
  • Gemma 3, text
  • Gemma 3n, text
  • Gemma 4, text
  • OLMo 3

Mixture of experts

  • Mixtral
  • Qwen3-MoE
  • Qwen 3.5 MoE, text
  • gpt-oss
  • Llama 4, text
  • GLM 4 MoE
  • GLM MoE with sparse attention
  • DeepSeek V2
  • DeepSeek V3
  • DeepSeek V3.2
  • DeepSeek V4
  • Kimi K2
  • Kimi K2.5

Hybrid and linear attention

  • Qwen3-Next
  • GLM 5 Next, text
  • Kimi Linear
  • Kimi K3, text
  • Mamba-2

Diffusion language models

  • LLaDA
  • Dream
  • Diffusion Gemma

Multimodal

  • Gemma 3
  • Gemma 4
  • Gemma 4, unified
  • Gemma 3n
  • Qwen 3.5
  • Llama 4

Diffusion pipelines

  • Stable Diffusion
  • Stable Diffusion XL
  • Stable Diffusion 3
  • Flux
  • Qwen-Image 2.1
  • Stable Diffusion, Flax weights
  • Stable Diffusion XL, Flax weights

Each port is checked against its reference implementation in float32, most on fixtures with the release's own configuration and tensor shapes. Qwen3-0.6B, SmolLM2-135M and Mamba-2 130M were also compared at full size. Supported models lists every model_type and how it was checked.

One trainer, from one device to a mesh

MeshSpec names the axes: data, fsdp, expert,tensor, sequence and stage. The model names the logical axes of its parameters, and Layout maps them onto the mesh, so the model code does not change when the mesh does. Attention picks cuDNN on supported GPUs, Pallas splash attention on TPUs and XLA elsewhere.

trainer = Trainer(objective, optax.adamw(3e-4), key=jax.random.key(0),
mesh=MeshSpec(fsdp=4, tensor=2))
dew launch --hosts 10.0.0.1 10.0.0.2 -- /opt/dew/.venv/bin/python train.py
MeshSpec(fsdp=4, tensor=2)Eight devices in a grid of four columns along the fsdp axis and two rows along the tensor axis. Each device holds one of the eight blocks of an MLP weight matrix.fsdp = 4tensor = 2device 0W[0, 0]device 1W[1, 0]device 2W[2, 0]device 3W[3, 0]device 4W[0, 1]device 5W[1, 1]device 6W[2, 1]device 7W[3, 1]
MeshSpec(fsdp=4, tensor=2) on eight devices. An MLP weight splits its model width over fsdp and its hidden width over tensor, so each device holds one of eight blocks.

What has been run

  • The tutorials on one GPU or a CPU, and the test suite on a CPU and on pools of local processes.
  • On one host with four RTX 3090s, a process pool launched by dew launch: every layout of the dense, mixture-of-experts, Mamba-2 hybrid and DiT models, each checked against one device.
  • On one TPU v6e chip, the sequence-parallel exchange around the splash attention kernel, forward and backward.

Dew has not been run on two physical nodes yet, and hybrid sharding and the sequence exchanges have no throughput numbers. Train on several nodes keeps the full list.

Tutorials

Notebooks that run top to bottom on Colab, shown here with the outputs of their last complete run.

Built on the JAX stack

Models are Flax modules and their variables are JAX pytrees. Optimizers are Optax transformations, checkpoints are written by Orbax, and data loads through Grain. Dew grew out of FlaxDiff, and Google's TPU Research Cloud supported the larger experiments.

Install DewSource on GitHub

MIT license. Dew is research software before 1.0: the API and checkpoint formats can change.