SwarmTorch

Get started

Run hello_swarm from pinned source.

SwarmTorch is not yet published as a release, so the quickstart builds from an exact commit. The example is self-contained: no network, no dataset, no files written.

Source revision: 7a75c55 Repository metadata checked: Claim reviewed:

1. Prerequisites

  • Rust 1.81 or newer

    Install with rustup. Optional adapters need newer compilers; see compatibility.

  • Git

    To clone and pin the reviewed revision.

  • No hardware needed

    The example runs on any desktop target with the default features.

2. Run the example

The checkout is detached at the reviewed commit and --locked refuses to change the lockfile, so every build is reproducible.

terminal
git clone https://github.com/swarmic/SwarmTorch.git
cd SwarmTorch
git checkout --detach 7a75c553c6356f35473d7dbaf6b8e256a86023a7
cargo run -p swarm-torch --example hello_swarm --locked

3. Read the output

Exact numbers depend on the revision. These lines confirm each stage completed.

  1. Cluster initialized: peer=…

    01The SwarmCluster configuration passed validation and was built for a local peer.

  2. Local SGD step complete: loss…

    02One analytical LinearModel (4 to 2) training step ran; loss is printed before and after.

  3. SafeTensors v1 checkpoint verified…

    03The model was exported to SafeTensors bytes in memory, re-imported, and the parameters matched.

4. What the code does

hello_swarm.rs
// swarm-torch/examples/hello_swarm.rs (excerpt, revision 7a75c55)
let config = SwarmCluster::builder()
    .topology(Topology::gossip(2))
    .consensus(RobustAggregation::CenteredClipping { tau: 1.0, max_iterations: 3 })
    .max_rounds(50)
    .convergence_threshold(0.005)
    .try_build()?;
let cluster = SwarmCluster::new(config, PeerId::new([1u8; 32]));

let mut model = LinearModel::new(4, 2)?.with_random_init(42);
let mut optimizer: SgdOptimizer<LinearModel> =
    SgdOptimizer::with_hyperparams(0.05, 0.9, 1e-4)?;

let input = [1.0f32, -0.5, 0.25, 0.75];
let target = [0.5f32, -0.25];
let preds = model.forward(&input)?;
let mut d_preds = [0.0f32; 2];
let initial_loss = MseLoss::compute_and_grad(preds.as_slice(), &target, &mut d_preds)?;

let mut param_grads = [0.0f32; 10];
model.backward_into(&input, &d_preds, &mut param_grads)?;
optimizer.step(&mut model, &param_grads)?;

let layout = ModelFlatTensorLayoutV1::for_linear(4, 2)?;
let limits = SafeTensorsLimitsV1::default();
let checkpoint_bytes = export_safetensors_checkpoint_with_layout_v1(&model, &layout, &limits)?;
let mut restored = LinearModel::new(4, 2)?;
import_safetensors_checkpoint_with_layout_v1(&mut restored, &layout, &checkpoint_bytes, &limits)?;
assert_eq!(restored.parameters(), model.parameters());
  1. 1

    Validated configuration

    try_build() rejects invalid topology, consensus and round limits before any peer starts. Centered clipping bounds the influence of any single update.

  2. 2

    Deterministic model

    A 4-input, 2-output LinearModel seeded with 42, so every run starts from the same ten parameters.

  3. 3

    One analytical step

    Forward pass, mean squared error and its gradient, backward into a flat buffer, then SGD with momentum 0.9 and weight decay 1e-4.

  4. 4

    In-memory checkpoint

    The model is exported to SafeTensors bytes with explicit size limits, imported into a fresh model and compared parameter by parameter. Nothing touches the filesystem.

Full source at 7a75c55

5. Next steps