diff --git a/benchmarks/sparse_current.py b/benchmarks/sparse_current.py new file mode 100644 index 0000000..0ce414d --- /dev/null +++ b/benchmarks/sparse_current.py @@ -0,0 +1,131 @@ +"""Benchmark actual FlyVis Network forward/backward on its visual connectome. + +Run after installing FlyVis: python benchmarks/sparse_current.py --out results.jsonl +Model compilation and data loading are excluded. No training dataset is needed. +""" + +import argparse +import gc +import json +import platform +import statistics +import subprocess +import time +from pathlib import Path + +import torch + +from flyvis import Network + + +def measure(backend, extent, batch, steps, repeats, warmup): + torch.manual_seed(20260926) + model = Network( + connectome=dict( + type="ConnectomeFromAvgFilters", + file="fib25-fib19_v2.2.json", + extent=extent, + n_syn_fill=1, + ), + backend=backend, + ) + torch.manual_seed(20260927) + stimulus = torch.randn(batch, steps, model.n_nodes) * 0.1 + + def step(): + model.zero_grad(set_to_none=True) + output = model(stimulus, dt=0.02) + loss = output.square().mean() + loss.backward() + return loss + + for _ in range(warmup): + step() + model.zero_grad(set_to_none=True) + gc.collect() + torch.cuda.empty_cache() + torch.cuda.synchronize() + baseline = torch.cuda.memory_allocated() + torch.cuda.reset_peak_memory_stats() + measurements = [] + for _ in range(repeats): + torch.cuda.synchronize() + start = time.perf_counter() + loss = step() + torch.cuda.synchronize() + measurements.append((time.perf_counter() - start) * 1000) + peak = torch.cuda.max_memory_allocated() + result = dict( + backend=backend, + extent=extent, + nodes=model.n_nodes, + edges=model.n_edges, + trainable_parameters=sum( + p.numel() for p in model.parameters() if p.requires_grad + ), + batch=batch, + steps=steps, + dtype=str(stimulus.dtype), + dt=0.02, + repeats=repeats, + warmup=warmup, + forward_backward_ms=statistics.median(measurements), + repetition_ms=measurements, + baseline_bytes=baseline, + peak_bytes=peak, + incremental_peak_bytes=peak - baseline, + loss=float(loss.detach()), + gradients_finite=all( + bool(torch.isfinite(p.grad).all()) + for p in model.parameters() + if p.requires_grad and p.grad is not None + ), + ) + model.zero_grad(set_to_none=True) + gc.collect() + torch.cuda.empty_cache() + return result + + +def main(): + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument("--extent", type=int, default=15) + parser.add_argument("--batches", type=int, nargs="+", default=[4, 16]) + parser.add_argument("--steps", type=int, nargs="+", default=[8, 32]) + parser.add_argument("--repeats", type=int, default=5) + parser.add_argument("--warmup", type=int, default=2) + parser.add_argument("--out", type=Path, required=True) + args = parser.parse_args() + if not torch.cuda.is_available(): + parser.error("This peak-memory benchmark requires CUDA") + torch.set_default_device("cuda") + torch.set_default_dtype(torch.float32) + torch.set_num_threads(4) + environment = dict( + torch=torch.__version__, + cuda=torch.version.cuda, + device=torch.cuda.get_device_name(), + platform=platform.platform(), + python=platform.python_version(), + threads=torch.get_num_threads(), + source_commit=subprocess.check_output( + ["git", "rev-parse", "HEAD"], text=True + ).strip(), + source_dirty=bool(subprocess.check_output(["git", "status", "--porcelain"])), + ) + args.out.parent.mkdir(parents=True, exist_ok=True) + with args.out.open("w", encoding="utf-8") as stream: + for batch in args.batches: + for steps in args.steps: + for backend in ("scatter", "csr"): + result = measure( + backend, args.extent, batch, steps, args.repeats, args.warmup + ) + result["environment"] = environment + stream.write(json.dumps(result) + "\n") + stream.flush() + print(json.dumps(result), flush=True) + + +if __name__ == "__main__": + main() diff --git a/docs/docs/reference/network.md b/docs/docs/reference/network.md index 97f08e1..01331ca 100644 --- a/docs/docs/reference/network.md +++ b/docs/docs/reference/network.md @@ -1,5 +1,48 @@ # Network +## Optional CSR aggregation + +`Network(backend="csr")` selects CSR aggregation for the default +`PPNeuronIGRSynapses` dynamics. The default remains `backend="scatter"`. +For Hydra-based training, set the solver override `backend=csr`. + +The default scatter path expands activity to `[batch, edges]` before activation +and weighted summation. The sparse backend instead activates `[batch, nodes]` +and computes the same summed current with sparse matrix multiplication. Its +custom backward samples the weight gradient only at existing connections; it +does not construct a dense `[nodes, nodes]` gradient. Forward and transposed CSR +layouts are cached, so backward does not sort the graph at each time step. + +Shared synaptic strengths, signs, counts, biases, time constants, Euler updates, +and node state hooks retain their existing meaning. Unsorted and parallel edges +are supported: parallel weights are summed once per forward and gradients flow +to every original edge. Per-edge current inspection still uses the original +connectome ordering and will allocate edge-sized tensors when requested. + +Backend activation storage scales with `batch * nodes * steps` instead of +`batch * edges * steps`. Graph layouts, parameters, output trajectories and +temporary workspace still consume memory. Use CSR at batch sizes of 16 or more on the +supplied visual-system graph, or when memory limits training. On the RTX 5090 +and H200, scatter was faster at batch 4 and CSR was faster at batch 16. + +Requirements and limitations: + +- CPU or CUDA, float32 or float64, with PyTorch CSR `sampled_addmm` support. +- First-order gradients only; higher-order derivatives are unsupported. +- The built-in `PPNeuronIGRSynapses` class and its out-of-place pointwise + activations. Other dynamics continue to use scatter. +- Fixed topology. Construct a new network for a different connectome. +- No AMP, `torch.compile`, MPS or arbitrary custom-activation support is claimed. + +CSR layouts are nonpersistent buffers. A sparse network and a scatter network +constructed from the same configuration have compatible parameter state dicts. +Reconstructing a network from `network.config` uses scatter by default, +including for models trained with CSR. + +Run `python benchmarks/sparse_current.py --out results.jsonl` for a bounded +forward/backward benchmark on the supplied visual-system connectome. It does +not require downloading the training dataset or running an optimization job. + ::: flyvis.network.network.Network ## Stimulus diff --git a/flyvis/config/solver.yaml b/flyvis/config/solver.yaml index 3cf8d50..353bd99 100644 --- a/flyvis/config/solver.yaml +++ b/flyvis/config/solver.yaml @@ -1,6 +1,7 @@ ensemble_and_network_id: ??? task_name: ??? train: true +backend: scatter resume: false checkpoint_only: false network_name: ${task_name}/${ensemble_and_network_id} diff --git a/flyvis/network/dynamics.py b/flyvis/network/dynamics.py index 7a41fbe..d1c4989 100644 --- a/flyvis/network/dynamics.py +++ b/flyvis/network/dynamics.py @@ -190,6 +190,7 @@ def write_state_velocity( target_sum: Callable, x_t: torch.Tensor, dt: float, + current_sum: Callable = None, **kwargs, ) -> None: """ @@ -202,19 +203,20 @@ def write_state_velocity( target_sum: Function to sum edge values for each target node. x_t: External input at time t. dt: Time step. + current_sum: Optional node-level weighted aggregation. The sparse + backend activates each neuron before propagation instead of + materializing an activated copy for every outgoing edge. **kwargs: Additional keyword arguments. """ + internal_current = ( + target_sum(params.edges.weight * self.activation(state.sources.activity)) + if current_sum is None + else current_sum(self.activation(state.nodes.activity)) + ) vel.nodes.activity = ( 1 / torch.max(params.nodes.time_const, torch.tensor(dt).float()) - * ( - -state.nodes.activity - + params.nodes.bias - + target_sum( - params.edges.weight * self.activation(state.sources.activity) - ) # internal chemical current - + x_t - ) + * (-state.nodes.activity + params.nodes.bias + internal_current + x_t) ) def currents( diff --git a/flyvis/network/network.py b/flyvis/network/network.py index db55efa..08e519e 100644 --- a/flyvis/network/network.py +++ b/flyvis/network/network.py @@ -8,6 +8,7 @@ import logging import warnings from contextlib import contextmanager +from functools import partial from typing import Any, Callable, Dict, Iterable, Literal, Optional, Union import numpy as np @@ -29,8 +30,9 @@ from flyvis.utils.nn_utils import n_params, simulation from flyvis.utils.tensor_utils import AutoDeref, RefTensor -from .dynamics import NetworkDynamics +from .dynamics import NetworkDynamics, PPNeuronIGRSynapses from .initialization import Parameter +from .sparse import SparseCurrent from .stimulus import init_stimulus logger = logging.getLogger(__name__) @@ -46,6 +48,9 @@ class Network(nn.Module): dynamics: Network dynamics configuration. node_config: Node parameter configuration. edge_config: Edge parameter configuration. + backend: ``"scatter"`` (default) or ``"csr"``. The latter + avoids edge-by-batch activations for the default PPNeuronIGRSynapses + dynamics, with first-order float32/float64 gradients on CPU/CUDA. Attributes: connectome (Connectome): Connectome directory. @@ -120,6 +125,7 @@ def __init__( ), ), stimulus_config: Dict[str, Any] = Namespace(type="Stimulus", init_buffer=False), + backend: str = "scatter", ): super().__init__() @@ -133,6 +139,17 @@ def __init__( # Store the connectome, dynamics, and parameters. self.connectome = init_connectome(**connectome) self.dynamics = forward_subclass(NetworkDynamics, dynamics) + if backend not in ("scatter", "csr"): + raise ValueError(f"Unknown propagation backend: {backend!r}") + if backend == "csr" and ( + type(self.dynamics) is not PPNeuronIGRSynapses + or getattr(self.dynamics.activation, "inplace", False) + ): + raise ValueError( + "csr requires PPNeuronIGRSynapses with a built-in, " + "out-of-place pointwise activation" + ) + self.backend = backend # Load constant indices into memory. # Store source/target indices. @@ -141,6 +158,11 @@ def __init__( self.n_nodes = len(self.connectome.nodes.index) self.n_edges = len(self.connectome.edges.source_index) + self._sparse_current = ( + SparseCurrent(self._source_indices, self._target_indices, self.n_nodes) + if backend == "csr" + else None + ) # Optional way of parameter sharing is averaging at every call across # precomputed masks. This can be useful for e.g. symmetric electrical @@ -293,6 +315,8 @@ def _param_api(self) -> AutoDeref[str, AutoDeref[str, RefTensor]]: params[route][param_name] = RefTensor(values, indices) # Add derived parameters. self.dynamics.write_derived_params(params) + if self._sparse_current is not None: + params.edges.csr_weight = self._sparse_current.prepare(params.edges.weight) for k, v in params.nodes.items(): if k not in params.sources: params.sources[k] = self._source_gather(v) @@ -397,8 +421,13 @@ def _next_state( """ vel = AutoDeref(nodes=AutoDeref(), edges=AutoDeref()) + kwargs = {} + if self._sparse_current is not None: + kwargs["current_sum"] = partial( + self._sparse_current, values=params.edges.csr_weight + ) self.dynamics.write_state_velocity( - vel, state, params, self.target_sum, x_t, dt=dt + vel, state, params, self.target_sum, x_t, dt=dt, **kwargs ) next_state = AutoDeref( diff --git a/flyvis/network/sparse.py b/flyvis/network/sparse.py new file mode 100644 index 0000000..89eac4c --- /dev/null +++ b/flyvis/network/sparse.py @@ -0,0 +1,150 @@ +"""Opt-in CSR aggregation for instantaneous, graded-release synapses. + +Only first-order differentiation is supported. The edge gradient is sampled at +existing coordinates, avoiding both dense adjacency gradients and edge-by-batch +messages. Layouts are derived from the connectome and are not checkpoint data. +""" + +import torch +from torch import Tensor, nn +from torch.autograd.function import once_differentiable + + +class _CSRMM(torch.autograd.Function): + @staticmethod + def forward(ctx, values, activity, crow, col, transpose_crow, transpose_col, order): + n = activity.shape[-1] + adjacency = torch.sparse_csr_tensor( + crow, + col, + values, + size=(n, n), + device=values.device, + dtype=values.dtype, + check_invariants=False, + ) + ctx.save_for_backward( + values, activity, crow, col, transpose_crow, transpose_col, order + ) + return torch.sparse.mm(adjacency, activity.T).T + + @staticmethod + @once_differentiable + def backward(ctx, grad_output): + values, activity, crow, col, transpose_crow, transpose_col, order = ( + ctx.saved_tensors + ) + n = activity.shape[-1] + adjacency = torch.sparse_csr_tensor( + crow, + col, + values, + size=(n, n), + device=values.device, + dtype=values.dtype, + check_invariants=False, + ) + grad_values = grad_activity = None + if ctx.needs_input_grad[0]: + grad_values = torch.sparse.sampled_addmm( + adjacency, grad_output.T, activity, beta=0 + ).values() + if ctx.needs_input_grad[1]: + # CSC @ dense would sort/convert to CSR at every backward step. + # Cache the transposed topology and only gather the current values. + transposed = torch.sparse_csr_tensor( + transpose_crow, + transpose_col, + values.index_select(0, order), + size=(n, n), + device=values.device, + dtype=values.dtype, + check_invariants=False, + ) + grad_activity = torch.sparse.mm(transposed, grad_output.T).T + return grad_values, grad_activity, None, None, None, None, None + + +class SparseCurrent(nn.Module): + """Fixed connectivity with dynamic weights and batch-major neuron activity. + + Unsorted and parallel edges are supported. Parallel weights are summed once per + network forward, with gradients routed back to every original edge. Preparing + weights once also avoids retaining a reordered edge vector per recurrent step. + """ + + def __init__(self, source: Tensor, target: Tensor, num_nodes: int): + super().__init__() + if num_nodes < 1 or source.ndim != 1 or source.shape != target.shape: + raise ValueError("csr requires 1D source/target arrays and nodes") + if source.dtype not in (torch.int32, torch.int64) or target.dtype not in ( + torch.int32, + torch.int64, + ): + raise ValueError("csr requires integer edge indices") + if source.device != target.device: + raise ValueError("csr source/target indices must share a device") + if source.numel() and bool( + ( + (source < 0) + | (source >= num_nodes) + | (target < 0) + | (target >= num_nodes) + ).any() + ): + raise ValueError("csr edge index is out of bounds") + self.num_nodes = num_nodes + self.num_edges = source.numel() + coordinates, inverse = torch.unique( + target.to(torch.int64) * num_nodes + source.to(torch.int64), + sorted=True, + return_inverse=True, + ) + rows = coordinates // num_nodes + counts = torch.bincount(rows, minlength=num_nodes) + crow = torch.cat((counts.new_zeros(1), counts.cumsum(0))) + self.register_buffer("crow", crow, persistent=False) + self.register_buffer("col", coordinates % num_nodes, persistent=False) + self.register_buffer("inverse", inverse, persistent=False) + order = torch.argsort((coordinates % num_nodes) * num_nodes + rows) + transpose_counts = torch.bincount(self.col, minlength=num_nodes) + self.register_buffer( + "transpose_crow", + torch.cat((transpose_counts.new_zeros(1), transpose_counts.cumsum(0))), + persistent=False, + ) + self.register_buffer("transpose_col", rows[order], persistent=False) + self.register_buffer("transpose_order", order, persistent=False) + + def prepare(self, edge_weight: Tensor) -> Tensor: + """Map current edge weights to unique CSR coordinates, preserving autograd.""" + if edge_weight.shape != (self.num_edges,): + raise ValueError("csr requires one weight per original edge") + if edge_weight.device != self.inverse.device: + raise ValueError("csr weights and topology must share a device") + return edge_weight.new_zeros(self.col.numel()).index_add( + 0, self.inverse, edge_weight + ) + + def forward(self, activity: Tensor, values: Tensor) -> Tensor: + """Sum weighted activated inputs, returning ``[batch, nodes]``.""" + if activity.ndim != 2 or activity.shape[1] != self.num_nodes: + raise ValueError("csr expects activity shaped [batch, nodes]") + if activity.device.type not in ("cpu", "cuda") or activity.dtype not in ( + torch.float32, + torch.float64, + ): + raise ValueError("csr supports float32/float64 on CPU and CUDA") + if activity.device != values.device or activity.dtype != values.dtype: + raise ValueError("csr activity and weights must share device/dtype") + if values.shape != self.col.shape or values.device != self.col.device: + raise ValueError("csr values must match the prepared CSR layout") + return _CSRMM.apply( + values, + activity, + self.crow, + self.col, + self.transpose_crow, + self.transpose_col, + self.transpose_order, + ) diff --git a/flyvis/solver.py b/flyvis/solver.py index cccc40d..68cfa61 100644 --- a/flyvis/solver.py +++ b/flyvis/solver.py @@ -159,7 +159,9 @@ def _init_solver( initialized = [] if init_network: - self.network = Network(**self.config.network) + self.network = Network( + **self.config.network, backend=self.config.get("backend", "scatter") + ) initialized.append("network") if init_task: diff --git a/flyvis_cli/training/train_single.py b/flyvis_cli/training/train_single.py index 8461e9b..d1ca929 100644 --- a/flyvis_cli/training/train_single.py +++ b/flyvis_cli/training/train_single.py @@ -114,6 +114,9 @@ def prepare_config(args): "description", ] } + # Preserve the saved solver configuration when resuming older scatter runs. + if args.get("backend", "scatter") != "scatter": + config["backend"] = args.backend return config diff --git a/tests/test_solver.py b/tests/test_solver.py index 7e0ad59..1aa21cf 100644 --- a/tests/test_solver.py +++ b/tests/test_solver.py @@ -1,8 +1,10 @@ import pytest from datamate import set_root_context +from omegaconf import OmegaConf from flyvis.solver import MultiTaskSolver from flyvis.utils.config_utils import get_default_config +from flyvis_cli.training.train_single import prepare_config @pytest.fixture(scope="module") @@ -34,10 +36,41 @@ def test_solver_config(): "ensemble_and_network_id=0", ], ) + assert config.backend == "scatter" assert config.task_name == "flow" assert config.ensemble_and_network_id == 0 +@pytest.mark.parametrize("backend", ["csr", None]) +def test_solver_network_backend(tmp_path, backend): + overrides = [ + "task_name=flow", + "ensemble_and_network_id=0", + "network.connectome.extent=1", + ] + if backend: + overrides.append(f"backend={backend}") + args = get_default_config( + path="../../flyvis/config/solver.yaml", overrides=overrides, as_namespace=False + ) + config = prepare_config(OmegaConf.create(args)) + if backend is None: + assert "backend" not in config # Compatible with saved scatter runs. + with set_root_context(str(tmp_path)): + solver = MultiTaskSolver( + "test", + config, + init_decoder=False, + init_task=False, + init_optim=False, + init_penalties=False, + init_scheduler=False, + ) + assert solver.network.backend == (backend or "scatter") + assert "backend" not in solver.config.network + assert "backend" not in solver.network.config + + @pytest.mark.slow def test_solver_init(solver): assert isinstance(solver, MultiTaskSolver) diff --git a/tests/test_sparse_current.py b/tests/test_sparse_current.py new file mode 100644 index 0000000..d022e80 --- /dev/null +++ b/tests/test_sparse_current.py @@ -0,0 +1,233 @@ +"""Sparse aggregation parity, including the real FlyVis recurrent runtime.""" + +import pytest +import torch + +from flyvis import Network +from flyvis.network.dynamics import activation_fns +from flyvis.network.sparse import SparseCurrent + +DEVICES = ["cpu"] + (["cuda"] if torch.cuda.is_available() else []) + + +@pytest.fixture(autouse=True) +def defaults(): + device, dtype = torch.get_default_device(), torch.get_default_dtype() + torch.set_default_dtype(torch.float64) + yield + torch.set_default_device(device) + torch.set_default_dtype(dtype) + + +def make_network(backend, activation="relu"): + return Network( + connectome=dict( + type="ConnectomeFromAvgFilters", + file="fib25-fib19_v2.2.json", + extent=1, + n_syn_fill=1, + ), + dynamics=dict(type="PPNeuronIGRSynapses", activation=dict(type=activation)), + node_config=dict( + bias=dict( + type="RestingPotential", + groupby=["type"], + initial_dist="Normal", + mode="mean", + requires_grad=True, + mean=0.5, + std=0.05, + ), + time_const=dict( + type="TimeConstant", + groupby=["type"], + initial_dist="Value", + value=0.05, + requires_grad=True, + ), + ), + backend=backend, + ).to(dtype=torch.get_default_dtype()) + + +@pytest.mark.parametrize("device", DEVICES) +@pytest.mark.parametrize( + "train_values,train_state", [(True, True), (True, False), (False, True)] +) +def test_unsorted_parallel_edges_and_gradients(device, train_values, train_state): + torch.set_default_device(device) + source = torch.tensor([2, 0, 2, 4, 1, 0]) + target = torch.tensor([1, 3, 1, 0, 4, 0]) + op = SparseCurrent(source, target, 6) # Also includes an isolated node. + w0 = torch.tensor([0.0, 1.0, -0.5, 0.3, -0.2, 0.1]) + h0 = torch.randn(3, 12)[:, ::2] # Noncontiguous input. + results = [] + for sparse in (False, True): + w = w0.clone().requires_grad_(train_values) + h = h0.detach().requires_grad_(train_state) + if sparse: + y = op(h, op.prepare(w)) + else: + y = torch.zeros(3, 6).scatter_add( + -1, target.expand(3, -1), w * h.index_select(-1, source) + ) + y.square().sum().backward() + results.append((y.detach(), w.grad, h.grad)) + for reference, actual in zip(*results): + if reference is not None: + torch.testing.assert_close(actual, reference, atol=1e-10, rtol=1e-9) + + +def test_finite_difference_gradcheck(): + torch.set_default_device("cpu") + op = SparseCurrent(torch.tensor([2, 0, 2]), torch.tensor([1, 2, 1]), 4) + w = torch.randn(3, requires_grad=True) + h = torch.randn(2, 4, requires_grad=True) + assert torch.autograd.gradcheck(lambda w, h: op(h, op.prepare(w)), (w, h)) + + +@pytest.mark.parametrize("device", DEVICES) +@pytest.mark.parametrize("activation", list(activation_fns)) +@pytest.mark.parametrize("dtype", [torch.float32, torch.float64]) +def test_real_network_outputs_and_all_gradients(device, activation, dtype): + torch.set_default_device(device) + torch.set_default_dtype(dtype) + reference = make_network("scatter", activation) + actual = make_network("csr", activation) + actual.load_state_dict(reference.state_dict(), strict=True) + x0 = torch.randn(2, 5, reference.n_nodes) * 0.2 + outputs, inputs, gradients = [], [], [] + for model in (reference, actual): + x = x0.clone().requires_grad_(True) + y = model(x, dt=0.01) + y.square().mean().backward() + outputs.append(y.detach()) + inputs.append(x.grad) + gradients.append({ + n: p.grad for n, p in model.named_parameters() if p.requires_grad + }) + tolerances = ( + dict(atol=1e-6, rtol=1e-4) + if dtype == torch.float32 + else dict(atol=1e-9, rtol=1e-8) + ) + torch.testing.assert_close(outputs[1], outputs[0], **tolerances) + torch.testing.assert_close(inputs[1], inputs[0], **tolerances) + assert gradients[0].keys() == gradients[1].keys() + for name in gradients[0]: + assert gradients[0][name] is not None, name + torch.testing.assert_close(gradients[1][name], gradients[0][name], **tolerances) + + +@pytest.mark.parametrize("device", DEVICES) +def test_hooks_initial_state_currents_and_optimizer_updates(device): + torch.set_default_device(device) + reference, actual = make_network("scatter"), make_network("csr") + actual.load_state_dict(reference.state_dict()) + optimizers = [torch.optim.Adam(m.parameters(), lr=1e-4) for m in (reference, actual)] + + def hook(state): + state.nodes.activity = state.nodes.activity * 0.9 + 0.02 + return state + + for model in (reference, actual): + model.register_state_hook(hook) + x = torch.randn(2, 4, reference.n_nodes) * 0.2 + for _ in range(3): + results = [] + for model, optimizer in zip((reference, actual), optimizers): + optimizer.zero_grad(set_to_none=True) + params = model._param_api() + state = model._initial_state(params, 2) + h = torch.full((2, model.n_nodes), 0.3, requires_grad=True) + state.nodes.activity = h + states = model(x, dt=0.01, state=model._state_api(state), as_states=True) + y = torch.stack([s.nodes.activity for s in states], dim=1) + # Per-edge inspection remains in original connectome order. + currents = model.dynamics.currents(states[-1], params) + loss = y.square().mean() + currents.square().mean() + loss.backward() + results.append((y.detach(), h.grad.clone(), currents.detach())) + optimizer.step() + for got, expected in zip(results[1], results[0]): + torch.testing.assert_close(got, expected, atol=1e-9, rtol=1e-7) + rebuilt = Network(**actual.config) + rebuilt.load_state_dict(actual.state_dict(), strict=True) + assert actual.config == reference.config + assert "backend" not in actual.config + assert rebuilt.backend == "scatter" + assert set(actual.state_dict()) == set(reference.state_dict()) + + +def test_no_edge_batch_activations_saved(): + torch.set_default_device("cpu") + model = make_network("csr") + saved_shapes = [] + + def pack(tensor): + saved_shapes.append(tuple(tensor.shape)) + return tensor + + with torch.autograd.graph.saved_tensors_hooks(pack, lambda t: t): + model(torch.randn(3, 5, model.n_nodes), dt=0.01).square().mean().backward() + assert (3, model.n_edges) not in saved_shapes + assert (model.n_edges, 3) not in saved_shapes + assert (model.n_nodes, model.n_nodes) not in saved_shapes + + +@pytest.mark.parametrize("backend", ["scatter", "csr"]) +def test_forward_inference_then_training(backend): + torch.set_default_device("cpu") + model = make_network(backend) + with torch.inference_mode(): + model(torch.randn(2, 3, model.n_nodes), dt=0.01) + model(torch.randn(2, 3, model.n_nodes), dt=0.01).sum().backward() + assert model.edges_syn_strength.grad is not None + + +def test_unsupported_dynamics_and_inplace_activation_rejected(): + torch.set_default_device("cpu") + for dynamics in ( + dict(type="NetworkDynamics", activation=dict(type="relu")), + dict(type="PPNeuronIGRSynapses", activation=dict(type="relu", inplace=True)), + ): + with pytest.raises(ValueError, match="requires PPNeuronIGRSynapses"): + Network( + connectome=dict(type="ConnectomeFromAvgFilters", extent=1), + dynamics=dynamics, + backend="csr", + ) + + +@pytest.mark.parametrize("device", DEVICES) +def test_empty_edges(device): + torch.set_default_device(device) + indices = torch.empty(0, dtype=torch.int64) + op = SparseCurrent(indices, indices, 4) + values = torch.empty(0, requires_grad=True) + h = torch.randn(2, 4, requires_grad=True) + y = op(h, op.prepare(values)) + y.sum().backward() + torch.testing.assert_close(y, torch.zeros_like(y)) + torch.testing.assert_close(h.grad, torch.zeros_like(h)) + assert values.grad is not None and values.grad.numel() == 0 + + +@pytest.mark.parametrize("source,target", [([4], [0]), ([-1], [0]), ([0], [4])]) +def test_invalid_edge_coordinates_rejected(source, target): + torch.set_default_device("cpu") + with pytest.raises(ValueError, match="out of bounds"): + SparseCurrent(torch.tensor(source), torch.tensor(target), 4) + + +def test_sparse_layout_moves_with_module_and_stays_out_of_checkpoint(): + torch.set_default_device("cpu") + op = SparseCurrent(torch.tensor([2, 0]), torch.tensor([1, 2]), 3) + assert not op.state_dict() + for device in DEVICES: + op.to(device) + h = torch.randn(2, 3, device=device, requires_grad=True) + w = torch.randn(2, device=device, requires_grad=True) + op(h, op.prepare(w)).sum().backward() + assert all(t.device.type == device for t in op.buffers()) + assert h.grad is not None and w.grad is not None