> ## Documentation Index
> Fetch the complete documentation index at: https://dromeus.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Define a training task for your own model

> Write a Dromeus TrainingTask for your PyTorch model: the model factory, batch structure, per-record loss, optimizer, shared definitions and local data.

A `TrainingTask` tells Dromeus what to train: how to build the model, what a batch looks like, how to compute the loss for each record, and which optimizer to use. Every node in a group builds the identical task from the same code. This guide builds the task from the [example](https://github.com/devesh1011/dromeus/blob/main/examples/custom_training.py), a complete regression task in about 120 lines that imports only from `dromeus`.

## Write the task

```python my_training.py theme={null}
from pathlib import Path

import torch
from torch import nn

import dromeus


def build_model() -> nn.Module:
    return nn.Sequential(nn.Linear(3, 7), nn.Tanh(), nn.Linear(7, 1))


def huber_per_record(model, batch):
    inputs, targets = batch
    losses = nn.functional.huber_loss(model(inputs), targets, reduction="none")
    return losses.mean(dim=1)  # one loss per record, shape [B]


def task() -> dromeus.TrainingTask:
    return dromeus.TrainingTask(
        model_factory=build_model,
        batch_adapter=dromeus.TensorBatchAdapter(
            example=(torch.zeros(2, 3), torch.zeros(2, 1)),  # public, at least 2 records
            definition="inputs [B,3] float32, targets [B,1] float32",
        ),
        loss_per_record=huber_per_record,
        model_definition="regression: Linear(3,7), Tanh, Linear(7,1); float32",
        task_definition="regression: three float32 features; scalar float32 target",
        loss_definition="Huber loss, delta 1, one value per record",
        implementation_hash=dromeus.code_hash(Path(__file__)),
        optimizer=dromeus.Optimizer.adam(learning_rate=0.005),
        batch_size=8,
    )
```

### Model

`model_factory` builds a fresh model. It must create only public initial state, because it runs when the draft is written and on every node. Parameters must be dense float32; represent a scalar parameter as shape `[1]`.

### Batches

`TensorBatchAdapter` describes one batch. `example` is a public or synthetic batch of at least two records: Dromeus uses it to check the model and loss without reading anyone's records. Batches can be tuples, lists or string-keyed dicts of tensors, with records on axis 0 and fixed shapes after it. Pad variable-length sequences to a declared length.

### Loss

`loss_per_record(model, batch)` runs the forward pass and returns a differentiable float tensor of shape `[B]`, one loss per record. Don't call `backward()` or step an optimizer: Dromeus does both. Don't mix information across records.

### Optimizer

Use `dromeus.Optimizer.sgd`, `dromeus.Optimizer.adam` or `dromeus.Optimizer.adamw`. For any other PyTorch optimizer, pass an `OptimizerRegistration`. See [Register an optimizer](https://dromeus.mintlify.site/guides/private-training.md#register-an-optimizer).

### Shared definitions

The three definitions are short descriptions of what the code means. They become hashes in the draft, so every node must use the exact same text. Change the text whenever you change the meaning.

`implementation_hash` identifies the code itself. `dromeus.code_hash(...)` hashes the files you pass, so pass every module your task depends on.

> [!WARNING]
>
> Hashes show that peers agree; they don't verify or sandbox code. Install and inspect the task code and its dependencies on every node. Dromeus never imports code named by a peer's draft.

## Report public metrics

Add `public_evaluate` to see model quality each round. It receives the model and returns a dict of named floats. It must use public data only, never a node's own records:

```python theme={null}
def evaluate(model):
    inputs, targets = public_holdout()  # fixed, public data
    with torch.no_grad():
        error = model(inputs) - targets
    return {"rmse": float(error.square().mean().sqrt())}


dromeus.TrainingTask(..., public_evaluate=evaluate)
```

## Provide each node's local data

Each node wraps its own records in `LocalData`:

```python theme={null}
local = dromeus.LocalData(
    dataset=my_records,          # any indexable torch Dataset of record tensors
    data_identity=my_file_hash,  # names this exact copy of the records
)
```

Checkpoints are bound to `data_identity`, so use a stable identifier such as a hash of the data file.

To run a node from the command line, expose a function that returns both the task and the node's data. The example reads the data path from an environment variable:

```python theme={null}
def setup() -> tuple[dromeus.TrainingTask, dromeus.LocalData]:
    return task(), local_data(Path(os.environ["DROMEUS_LOCAL_DATA"]))
```

Then `dromeus run --task my_training:setup` uses it. Dromeus imports `my_training` from the current directory.

## What Dromeus checks

Before any record is read, Dromeus checks the model on the public `example` batch. It supports custom containers of compatible operations, tied parameters, declared frozen parameters and immutable public buffers. It rejects BatchNorm, cross-record losses, unsafe trainable-layer buffers, uncontrolled model randomness, unbounded streams and changing record populations, naming the module path and the missing capability. There is no silent layer replacement.

See the [TrainingTask reference](https://dromeus.mintlify.site/reference/training-task.md) for every field, token data and frozen weights.

## Next

> [card] [Run a group](https://dromeus.mintlify.site/guides/run-a-group.md)
>
> Write the shared draft and start every node.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.