Skip to main content

torch-population

Colocated population and ensemble wrapper for PyTorch. Parameters, buffers, tracked EMAs, and any torch.optim.Optimizer state live together in one flat PopulationParameters container — scaling cleanly from a zero-overhead single-model wrapper to vectorized populations, gradient surgery, and differentiable meta-learning.

Install

$ pip install torch-population

Usage

1. Simple: single-model noop wrapper → vectorized population

import torch
from torch import nn
from torch_population import Population

net = nn.Sequential(nn.Linear(16, 32), nn.SiLU(), nn.Linear(32, 4))
x = torch.randn(8, 16)

# zero-overhead noop wrapper (exact parameter aliasing)

pop = Population(net)
assert pop.unwrap() is net

# scale to a vectorized population (or call pop.populate(4) later)

pop = Population(net, pop_size = 4)

out = pop(x)                                            # (4, 8, 4) shared input
out = pop(torch.randn(4, 8, 16), per_individual = True) # (4, 8, 4) per-individual input

# index out any individual as a normal nn.Module and write it back

ind = pop[0]
assert ind(x).shape == (8, 4)
pop[0] = ind

2. Colocated optimizers, state sharing & individual round-trip

# wrap any torch.optim.Optimizer class (built-in or custom)

opt = pop.optimizer(torch.optim.AdamW, lr = 1e-3, shared = False)
# opt = pop.optimizer(AnyOptimizer, lr = 1e-3, shared = True)              # 1/P shared opt state
# opt = pop.optimizer(AnyOptimizer, lr = [1e-4, 3e-4, 1e-3, 3e-3])         # per-individual HPs

opt.zero_grad()
pop(x).pow(2).mean().backward()
pop.clip_grad_norm_(1.0)                                # per-individual grad clipping (optional members=, names=)
opt.step()

# toggle optimizer state sharing at runtime ('mean', 'max', 'min', 'median', 'sum', 'first', 'zero', callable, or per-key dict)

opt.share(reduce = {'exp_avg': 'mean', 'exp_avg_sq': 'max', 'step': 'max'})
opt.unshare()

# insert an existing standalone module + optimizer into a population, run population ops, then extract both back out

solo = nn.Sequential(nn.Linear(16, 32), nn.SiLU(), nn.Linear(32, 4))
solo_opt = torch.optim.AdamW(solo.parameters(), lr = 1e-3)
solo(x).pow(2).mean().backward()
solo_opt.step()

pop[2] = (solo, solo_opt)                               # or pop.insert(2, solo, solo_opt)

opt.zero_grad()
pop(x).pow(2).mean().backward()
opt.step()

solo, solo_opt = pop.extract(2)                         # normal nn.Module + native torch.optim.Optimizer with updated state
solo_opt.zero_grad()
solo(x).pow(2).mean().backward()
solo_opt.step()

# or use the checkout context manager to extract, train solo, and write both back on exit

with pop.checkout(2) as (ind, ind_opt):
    ind_opt.zero_grad()
    ind(x).pow(2).mean().backward()
    ind_opt.step()

3. Shared layers, arbitrary methods, lifecycle & model soups

# share a backbone (params + buffers) while keeping independent heads

pop = Population(net, pop_size = 4, shared = ('0.*',))
opt = pop.optimizer(torch.optim.AdamW, lr = 1e-3)

sub_out = pop(x, members = (0, 2))                      # (2, 8, 4) subset forward
hidden  = pop.call('encode', x)                         # vmap any method or callable(module, *args)

# parameters, buffers, tracked EMAs, and optimizer state move in lockstep

pop.reset_(torch.tensor([True, False, False, False]))   # reinit masked members
pop.replace(3, 0, reset_opt = True)                     # clone winner 0 into slot 3
pop.delete(1)                                           # P: 4 -> 3
pop.append(0)                                           # P: 3 -> 4

avg_net = pop.mean()                                    # standalone nn.Module
med_net = pop.reduce('median')                          # or 'max', 'min', callable

4. Parameter & gradient EMAs (noop & populated)

# runnable target / Polyak EMA (scalar or per-member multi-timescale rates)

ema = pop.clone(include_opt = False)
ema.lerp_(pop, torch.tensor([0.001, 0.005, 0.02, 0.1]))
target_out = ema(x)

# colocated, lifecycle-synced EMAs for gradients, online Fisher, or flat parameters
# initializes without zero-bias, follows state_dict(), device moves, reset_(mask), delete/append

g = opt.autograd(pop(x).pow(2).mean(), flat = True)     # (D,) in noop, (P, D) in populated
g_ema  = opt.ema_('grad', g, decay = 0.98)              # Grokfast / smoothed gradient
fisher = opt.ema_('fisher', g.square(), decay = 0.99)   # online diagonal Fisher
p_ema  = pop.ema_('param', pop.flat_params, decay = 0.995)
ages   = pop.track_('age', torch.zeros(4, dtype = torch.long))  # dtype-agnostic lifecycle-synced metadata

out_ema = pop(x, params = p_ema)                        # run forward directly with flat EMA weights

5. Multi-loss gradient surgery, swapping & gradient penalties

# multi-loss flat autograd & gradient projection — identical code for 1 model (D,) or P models (P, D)

g1, g2 = opt.autograd(loss_a, loss_b, flat = True)
proj = g2 - ((g1 * g2).sum(dim = -1, keepdim = True).clamp(max = 0.) / g1.square().sum(dim = -1, keepdim = True).clamp(min = 1e-8)) * g1
opt.step(grads = g1 + proj)

# optimizer-agnostic reconciliation (returns a normal scalar loss for .backward(), clip_grad_norm_, any optimizer)
# spans multiple modules with zero state_dict() pollution and keeps unused branches at grad = None

from torch_population import PopulationParameters

params = PopulationParameters((actor, obs_history, action_history))
loss = params.reconcile(loss_a, loss_b, fn = lambda g1, g2: g1 + g2)
loss.backward()

# cross-member gradient swapping / gossip mixing across a population

g = opt.autograd(loss_pop, flat = True)
opt.step(grads = g.flip(0))                             # swap grads in a population of 2 (or g.roll(1, 0) / M @ g)

# parameter-gradient penalty (or input-gradient R1 / WGAN-GP with per_individual = True)

g = opt.autograd(loss_pop, flat = True, create_graph = True, retain_graph = True)
(loss_pop + 0.1 * g.square().sum()).backward()

6. Weight-space research: SAM, Evolution Strategies, Genetic Algorithms & model soups

# pass any 1D (D,) or 2D (K, D) flat parameter tensor directly into pop(x, params = ...)

# Sharpness-Aware Minimization (SAM) — works for 1 model or P models
g = opt.autograd(pop(x).pow(2).mean(), flat = True)
eps = 0.05 * g / g.norm(dim = -1, keepdim = True).clamp(min = 1e-8)
opt.step(grads = opt.autograd(pop(x, params = pop.flat_params + eps.detach()).pow(2).mean(), flat = True))

# Evolution Strategies — vmap 64 perturbations on a single noop model
single = Population(net)
noise = torch.randn(64, single.flat_params.numel())
outs = single(x, params = single.flat_params.detach() + 0.05 * noise)   # (64, 8, 4)

# Genetic Algorithm — tournament selection + weight-averaging crossover + mutation
fit = -pop(x).sub(y).pow(2).mean(dim = (-2, -1))                        # (P,) fitness
losers = fit.topk(P - elites, largest = False).indices
cand = torch.randint(P, (len(losers), 2, tourney_size))
p1, p2 = cand.gather(-1, fit[cand].argmax(-1, keepdim = True)).squeeze(-1).unbind(-1)
pop[losers] = pop[p1].lerp_(pop.view(p2), 0.5)
with torch.no_grad():
    pop.flat_params[losers] += 0.05 * torch.randn_like(pop.flat_params[losers])

# differentiable model soup & mode connectivity
soup_out = pop(x, params = pop.flat_params.mean(dim = 0))               # (8, 4), grads flow to all P members

7. Advanced: differentiable multi-optimizer meta-learning & long-term memory consolidation

pop = Population(net, pop_size = 4)
ltm = pop.clone(include_opt = False)                    # long-term memory copy of the population

# attach multiple different optimizers (each has its own colocated state)

opt_sgd   = pop.optimizer(torch.optim.SGD, lr = 5e-2, momentum = 0.9, name = 'sgd')
opt_rms   = pop.optimizer(torch.optim.RMSprop, lr = 1e-2, name = 'rms')
opt_adamw = pop.optimizer(torch.optim.AdamW, lr = 1e-2, name = 'adamw')

# backprop through unrolled updates across different optimizers (or per-member via members = ...)

fast = pop.population_parameters
for step_opt in (opt_sgd, opt_rms, opt_adamw):
    inner_loss = (pop(x_tr, params = fast) - meta_target).pow(2).mean()
    fast = step_opt.functional_step(inner_loss, fast, create_graph = True)

pop(x_val, params = fast).pow(2).mean().backward()      # meta-gradients flow through all 3 optimizers

# consolidate fast weights into a long-term memory version of an individual (or whole population)

ltm.view(0).lerp_(fast, 0.05)                           # Polyak EMA on individual 0
ltm.lerp_(fast, torch.tensor([0.01, 0.05, 0.1, 0.2]))   # multi-timescale LTM across all members
pop.view(0).ema_('ltm', fast.flat_params[0], decay = 0.95)  # or colocated LTM runnable via params=

Examples & Research Use-Case Suite

$ python train_pop_mnist.py
$ pytest                                                # core tests + tests/use_cases/ research cookbook

Citations

@inproceedings{anonymous2026continual,
    title   = {Continual Reinforcement Learning with Neuroevolution},
    author  = {Anonymous},
    booktitle = {Submitted to The Fifteenth International Conference on Learning Representations},
    year    = {2026},
    url     = {https://openreview.net/forum?id=UxsGk4on0e},
    note    = {under review}
}

Metadata

Release files for torch-population 0.0.4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for torch-population 0.0.4
File Size Uploaded Provenance Actions
torch_population-0.0.4.tar.gz 42.4 kB Without provenance Details for torch_population-0.0.4.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for torch-population 0.0.4
File Interpreter ABI Platform Actions
torch_population-0.0.4-py3-none-any.whl Python 3 none any Details for torch_population-0.0.4-py3-none-any.whl

Total release size: 88.5 kB

Release files / torch_population-0.0.4.tar.gz

Download URL torch_population-0.0.4.tar.gz
Size 42.4 kB
Tags Source
SHA-256 checksum
How to use checksums
24c894d305ad36d1e3d8abf2a36b0b87bab982e510e84400439140c9c8a8b992
BLAKE2b-256 checksum
How to use checksums
1aacfb00a1b5a3ccb9ade770579f1281dc13afd066c04e44d418452780723f2f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.8.17

Release files / torch_population-0.0.4-py3-none-any.whl

Download URL torch_population-0.0.4-py3-none-any.whl
Size 46.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
b5ed3c67bcffcc0e46acc6350227e2d62234e3a789727f8d097f98f620063176
BLAKE2b-256 checksum
How to use checksums
cde2d6e5f41a2aa816a389622171c8a5a6f2896c8fe2be9a0c590a2a2944a4ae
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.8.17

Security for torch-population 0.0.4

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page