> ## 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.

# Train with record-level differential privacy

> Add differential privacy to a Dromeus run: write a private draft, give each node a durable privacy ledger, and register custom optimizers safely.

Keeping records on each machine doesn't by itself make model updates private: an update can still reveal information about the records it was trained on. A private run adds record-level differential privacy to every node's local training. Dromeus owns Poisson sampling, per-record clipping, Gaussian noise and privacy accounting through Opacus; the group's optimizer only sees the sanitized gradients.

Install the `privacy` extra first:

```bash theme={null}
pip install --pre "dromeus[privacy]"
```

## Write a private draft

Pass `privacy` when writing the draft:

```python theme={null}
dromeus.make_draft(
    task(),
    "draft.yaml",
    run_id="private-regression-2026-11",
    participants=4,
    rounds=20,
    local_steps=10,
    privacy=dromeus.Privacy(max_grad_norm=1.0, noise_multiplier=1.1, delta=1e-5),
)
```

| `Privacy` field | Meaning |
| - | - |
| `max_grad_norm` | Per-record gradient clipping norm |
| `noise_multiplier` | Gaussian noise relative to the clipping norm |
| `delta` | The delta of the (epsilon, delta) guarantee |
| `epsilon_limit` | Optional cap on privacy spending |
| `randomness` | `"secure"` (default) or `"reproducible_public_benchmark"` |

The privacy horizon is the run's total local steps, `rounds x local_steps`, and sampling switches to Poisson. Real private runs use `randomness="secure"`, which needs the pinned secure random-number dependency. Use `"reproducible_public_benchmark"` only for seeded research on public data.

## Give each node a ledger

Each private dataset needs a durable ledger that records every privacy reservation, including those of failed runs. Create it once, then reopen it for every later run:

```python theme={null}
ledger = dromeus.PrivacyLedger.create("ledger.json", lineage="site-a-records")
# later runs: dromeus.PrivacyLedger("ledger.json", lineage="site-a-records")

local = dromeus.LocalData(
    dataset=my_records,
    data_identity=my_file_hash,
    ledger=ledger,
    physical_batch_size=16,  # records processed at once; doesn't change accounting
)
```

> [!CAUTION]
>
> Never delete or recreate a ledger to restart a run. The ledger is the record of privacy already spent on that dataset.

Secure runs refuse a `research_seed`; reproducible public benchmarks require one. Secure private runs can't resume after a crash: a failed step leaves the whole horizon's reservation spent.

## Register an optimizer

`Optimizer.sgd`, `Optimizer.adam` and `Optimizer.adamw` cover most runs, private or not. For any other PyTorch optimizer, pass an `OptimizerRegistration` as the task's `optimizer`. Every node supplies the same trusted implementation. The [RMSprop reference](https://github.com/devesh1011/dromeus/blob/main/tests/support/private_optimizer.py) is a complete registration to copy and adapt.

A registration supplies:

* `optimizer_type`: the exact PyTorch optimizer class, including custom subclasses.
* `implementation_hash`: SHA-256 identifying the optimizer implementation, factory and state-validator contract, including relevant dependencies.
* `create(model)`: builds fresh optimizer state on the actual model parameters.
* `validate_state(state, parameters, logical_steps)`: rejects wrong state fields, tensor shapes, dtypes, counters and initial values.

```python theme={null}
from my_optimizer import registration

task = dromeus.TrainingTask(
    ...,  # model, batches, loss and definitions as usual
    optimizer=registration(),
    batch_size=64,
    schedule="linear_decay",
)
```

Supported optimizers are ordinary dense float32 `torch.optim.Optimizer` implementations that update from the sanitized `.grad`, the model weights, public options and their own state, with a `step()` that takes no closure. Optimizers that rely on closures, raw per-sample gradients, secondary backward passes, sparse gradients or custom zeroing need a different, audited contract.

> [!WARNING]
>
> Dromeus compares the implementation hash with the group's declaration; it doesn't audit code from the hash. Install and inspect the optimizer package on every node, and never import code because a remote draft names it.

## What stays private

* Group formation shares model weights, never optimizer state, sampler state or private data.
* Raw private losses aren't reported. Public evaluation must use a declared, fixed public holdout.
* Local checkpoints contain optimizer, sampler and accountant state. Don't publish them; publish model-only exports.
* A per-sequence guarantee on token data doesn't automatically protect a whole document or person. Repeated or overlapping chunks need composition.


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