Language models
Pretraining on token windows or packed documents, chat fine-tuning that counts only the assistant tokens, and cached generation.
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"import itertools
import jaximport numpy as npimport optax
from dew import Dataset, Trainer, modelsfrom dew.data import ByteTokenizerfrom dew.objectives.lm import LMObjectivefrom 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]))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
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.
flax.linen.Module. Built-in ones come from models.build(name, ...).init(key) returns the variables, loss(variables, batch, step) the loss and metrics.import itertools
import flax.linen as nnimport jaximport jax.numpy as jnpimport numpy as npimport 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]])))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 ]]Each of these is an objective in the library, trained by the same trainer on the same mesh.
Pretraining on token windows or packed documents, chat fine-tuning that counts only the assistant tokens, and cached generation.
Diffusion, flow matching and latent diffusion on DiT, U-Net and MMDiT backbones, with 17 solvers and classifier-free guidance.
Masked diffusion over tokens (MDLM), LLaDA and Dream continued from their released weights, and block diffusion with Diffusion Gemma.
I-JEPA and V-JEPA: a context encoder, a moving-average target encoder, a predictor and block masks, with linear and k-NN probes.
SFT, DPO, GRPO and PPO on the same trainer, asynchronous RL whose rollouts come from Dew, vLLM or SGLang, and Flow-GRPO for images.
Routed and shared experts, bias balancing, routing replay, grouped-matmul kernels, and expert parallelism over an expert mesh axis.

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

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.
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.
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.pyMeshSpec(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.dew launch: every layout of the dense, mixture-of-experts, Mamba-2 hybrid and DiT models, each checked against one device.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.
Notebooks that run top to bottom on Colab, shown here with the outputs of their last complete run.
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.
MIT license. Dew is research software before 1.0: the API and checkpoint formats can change.