Skip to content

Size circuit simulations from their synapses, replay and spike files - #1241

Draft
eleftherioszisis wants to merge 24 commits into
mainfrom
number-synapses-from-edge-indices
Draft

eleftherioszisis wants to merge 24 commits into
mainfrom
number-synapses-from-edge-indices

Conversation

@eleftherioszisis

@eleftherioszisis eleftherioszisis commented Nov 21, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

The memory of a neurodamus simulation comes from its synapses, its replay and its spike files much more than from its neurons. An rCA1 cell has 8 times the synapses of an SSCX cell, so the per-cell rule of SSCX let the CA1 job 01a1cb34 run out of memory, and the temporary 0.09 GB per cell that replaced it triples every SSCX allocation. This PR records what drives memory at generation, and sizes the nodes with a model measured in the simulation image.

  • Generation records number_synapses (onto the simulated cells), number_synapses_in_replay (estimated, whose source spikes in a spike stimulus) and number_spikes_in_replay on the Simulation, each only if entitysdk's Simulation has the field.
  • The estimator gives the nodes that hold the measured peak: per node, the ranks and the replay spikes every rank reads; split over the nodes, the cells, synapses, replayed synapses and LFP weights. It keeps 0.03 GB per cell as a floor, so SSCX keeps its nodes, and 0.09 GB per cell for simulations without counts.
  • It never sizes a run that cannot run: spike inputs no node can hold, and simulations needing more nodes than exist (20 large nodes on cell_a), are refused with an error that says why.

Refs openbraininstitute/prod-circuit-simulation#265.

Model

Fitted on 76 runs of neurodamus 4.2.11 and 4.3.0 in the production image (NEURON and CoreNEURON, rCA1 and SSCX cells, 1 to 16 ranks, replay, large spike files, reports, LFP), launched as launch-system does. Every run is within 0.85 to 1.09 of it.

Term Cost Paid
MPI rank 34.9 MB on every node
Replay spike, per rank (every rank reads all the spike files) 41.4 B on every node
Rank, with LFP 6.5 MB on every node
Cell 5.55 MB split over the nodes
Synapse (neurodamus creates all of them, replayed or not) 3.07 KB split over the nodes
Replayed synapse (spike train and NetCon of its connection) 2.05 KB split over the nodes
LFP weights of the simulated cells 1.04 times split over the nodes

Times 1.15 (the largest measured peak is 1.09 times the fit), with 90% of a node usable. Instances now declare their ranks_per_instance (192 on hpc7a.96xlarge, 176 on HB176rs_v4, 4 on t3a.xlarge; 2 on cell_b's small pool, not checked) and, on cell_a, max_instances (20 large, 12 small, from aws-terraform-deployment launch_system/pcs.tf).

What it gives:

Case (cell_a) 0.03 GB/cell 0.09 GB/cell (main) This PR
CA1 job 01a1cb34 (9,587 cells, 219M synapses, 5.4% of CA3 replayed) 1 (ran out of memory) 2 2 (ran on 2)
Same, every CA3 source at 10 Hz for 5 s (13.4M spikes) 1 2 (would run out of memory) 3
SSCX nbS1-O1 (212k cells) 9 25 (more than cell_a has) 9
Whole rCA1 (456k cells, 9.9G synapses) 18 (would run out of memory) 54 refused: needs 54 of 20

On the 122 distinct circuits of staging and production simulated whole, the estimate gives the 0.03 rule's nodes to 112 (every SSCX, thalamus, MICrONS, H01, Allen and synaptome circuit) and more to the 10 rCA1 circuits whose synapses need them; fewer to none.

The measurements, the fit, every run against the model and these comparisons are in docs/circuit-simulation-memory-measurements.md; how the estimate works is in docs/circuit-simulation-resources.md.

Counting at generation

  • The synapses come from the target_to_source index of each edges file, read with Client.open_file (entitysdk 0.22), from the local asset store or with range reads. The layout is not trusted: a population is counted per cell only if its index is at most 32 MB and accounts for every edge; otherwise in total, from the shape of source_node_id, with a node set getting its share of the cells times 2.5. All 2,926 circuits of staging and production were checked: every counted population has the index, synaptome writers give it too few or too many rows, and nbS1 has 68 MB of index per population.
  • The replayed synapses are each source population's synapses times the share of its nodes that spike in the spike files generation writes, times 2.5, capped. The spikes are those of all the files.
  • Any error while counting records the neuron count only: it never fails generation.
  • Cost, measured on production circuits: the first simulation of a campaign reads its circuit's indices in 4 to 10 s (from outside AWS), adding at most 43 MB to the peak memory of the service (rCA1, 456k cells), and saves the synapses per cell next to the staged circuit; the next simulations of the campaign load that file (5.2 MB and 9 ms for the whole rCA1 circuit). Nothing is kept in the service between requests. Counting 13.4M replay spikes takes 0.09 s and 23 MB. At launch the estimator makes the get_entity it made before, plus two per LFP recording array.

Dependencies and rollout

  • entitysdk 0.22 (Client.open_file, released). uv.lock and the launch-script requirements pin it; make compile-deps, make check-deps and make check-launch-deps were run against the private index and leave them unchanged.
  • The three counts need fields of Simulation in entitycore and entitysdk: number_synapses is in entitycore (Add number_synapses to Simulation entitycore#775, merged), number_synapses_in_replay and number_spikes_in_replay in Add number_synapses_in_replay and number_spikes_in_replay to Simulation entitycore#780; entitysdk has none of them yet (the change is ready, not opened). entitysdk 0.22 reads responses with fields it does not know, so the order of the releases does not matter for clients. Until entitysdk has the fields, generation records the neuron count only and the estimate stays at 0.09 GB per cell.

Limits

  • Measured on one machine with up to 16 ranks: the per-rank terms are extrapolated to 176 and 192 ranks. The CA1 job is the only production check.
  • Two circuits measured; the 0.03 GB per cell floor covers circuits whose cells cost more. SSCX measured 0.013 to 0.016 GB per cell, about half the 0.025 of the production runs behind the 0.03 rule, unexplained.
  • Source sets chosen for their connections to the simulated cells could replay more synapses than their share times 2.5.
  • cell_b's node limit (the Azure subscription quota) is not known, so it is not checked.

Checks

  • Lint, format and ty clean on the changed code. 1,990 tests pass over the generation, library and app suites (the 3 collection errors are optional extras not installed locally).
  • The counting was run end to end against staging and production on 89 circuits with the layouts above: totals equal to the edges of every circuit, per-cell counts equal to the exact ones on the 81 counted exactly.

@codecov

codecov Bot commented Nov 21, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 98.85932% with 3 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
obi_one/scientific/library/circuit_synapses.py 98.21% 1 Missing and 1 partial ⚠️
obi_one/scientific/library/replay_inputs.py 97.05% 0 Missing and 1 partial ⚠️
@@            Coverage Diff             @@
##             main    #1241      +/-   ##
==========================================
+ Coverage   86.43%   86.56%   +0.12%     
==========================================
  Files         385      388       +3     
  Lines       24528    24786     +258     
  Branches     2820     2855      +35     
==========================================
+ Hits        21202    21457     +255     
- Misses       2776     2777       +1     
- Partials      550      552       +2     
Flag Coverage Δ
app 92.86% <100.00%> (+0.10%) ⬆️
lib 85.32% <98.51%> (+0.12%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
app/mappings.py 100.00% <ø> (ø)
app/schemas/cluster.py 100.00% <100.00%> (ø)
...services/resource_estimation/circuit_simulation.py 100.00% <100.00%> (ø)
obi_one/db_sdk/circuit.py 100.00% <100.00%> (ø)
obi_one/scientific/library/circuit_staging.py 97.50% <100.00%> (ø)
obi_one/scientific/library/simulation_synapses.py 100.00% <100.00%> (ø)
...scientific/tasks/generate_simulations/task/task.py 95.51% <100.00%> (+0.14%) ⬆️
obi_one/scientific/library/replay_inputs.py 97.05% <97.05%> (ø)
obi_one/scientific/library/circuit_synapses.py 98.21% <98.21%> (ø)
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@eleftherioszisis
eleftherioszisis force-pushed the number-synapses-from-edge-indices branch from f166c82 to db32908 Compare November 27, 2026 00:07
@eleftherioszisis eleftherioszisis changed the title Record the synapses of a simulation and use them to size its resources Size circuit simulations from their synapses, replay and spike files Nov 27, 2026
@eleftherioszisis eleftherioszisis self-assigned this Nov 27, 2026
The memory of a simulation depends on its synapses much more than on its neurons: the rCA1 hippocampus slice has about 8 times more synapses per neuron than SSCX, which an estimate from the number of neurons alone misses.
Generation runs in the API service with the nodes of the circuit only, so the edge files are not available.
The synapses onto the simulated cells are counted from the SONATA edge indices, which are read remotely with HTTP range requests and kept per circuit.
The estimate is the larger of the per-cell rule and a memory model fitted on 18 runs of rCA1 and SSCX.

Refs openbraininstitute/prod-circuit-simulation#265
The lib CI job runs ty with the released entitysdk, which has no Client.open_remote_file yet, and the staged circuit of a task is typed as optional.
The remote file is typed as io.IOBase, so the check no longer depends on entitysdk.http_file.

Refs openbraininstitute/prod-circuit-simulation#265
The first coefficients came from a NEURON build without CoreNEURON and took the 2.5x of the neurodamus docs for CoreNEURON, which gave 4 nodes to a job that runs on 2.
In the simulation image, neurodamus frees the NEURON model before CoreNEURON loads, so CoreNEURON raises the peak by 1% to 11%, and a synapse costs 3.5 KB.
With the synapses counted, the temporary 0.09 GB per cell is replaced by the original 0.03 as a floor.

Refs openbraininstitute/prod-circuit-simulation#265
- Read the edge indices with Client.open_file (entitysdk 0.22): from the local asset store when it
  has the file, otherwise with range reads.
- Do not trust the layout of an edges file. A population is counted per cell only if its
  target_to_source index is at most 32 MB and accounts for every edge; otherwise it is counted in
  total from the shape of source_node_id, and a node set gets its share of the cells times 2.5.
  All 2,926 circuits of staging and production were checked: synaptome writers give the index too
  few or too many rows, and nbS1 has 68 MB of index per population.
- Keep the synapses by source population, and count the spiking sources and the spikes of the
  replay files generation writes, to record number_replayed_synapses and number_replay_spikes
  next to number_synapses, when entitysdk's Simulation has them.
- Any error while counting records the neuron count only: it must not fail generation.

Refs openbraininstitute/prod-circuit-simulation#265
…mage

The model was fitted on 76 runs of neurodamus 4.2.11 and 4.3.0 in the production image (NEURON and
CoreNEURON, rCA1 and SSCX, 1 to 16 ranks, replay, spike files, reports, LFP), within 0.85 to 1.09
of every run:

- on every node: 34.9 MB per rank, 41.4 bytes per replay spike per rank (every rank reads all the
  spike files), 6.5 MB per rank with LFP;
- split over the nodes: 5.55 MB per cell, 3.07 KB per synapse, 2.05 KB per replayed synapse,
  1.04 times the LFP weights of the simulated cells;
- times 1.15, 90% of a node usable, at least the 0.03 GB per cell SSCX ran with.

The CA1 job 01a1cb34 gets the 2 nodes it ran on, and the model agrees it could not fit 1. SSCX
keeps its nodes. Simulations without counted synapses keep 0.09 GB per cell. Instances get their
ranks (cores) and, on cell_a, the nodes available: spike inputs no node can hold, and simulations
needing more nodes than exist, are refused with an error instead of failing or waiting forever.

docs/circuit-simulation-memory-measurements.md holds the measurements, the fit and its checks.

Refs openbraininstitute/prod-circuit-simulation#265
- Generation: one method counts, and the caller decides that the counts are optional and logs a
  failure. The circuit config is parsed for the population sizes only when there are spike files.
- circuit_synapses: one comprehension for the biophysical populations; one check per bound of
  the index; the cache drops the least recently used circuit.
- replayed_synapses takes the replayed sources, not the whole ReplayCounts.
- Estimator: the circuit is fetched once for all the recording arrays (generation refuses arrays
  of another circuit); LFP is checked once; one helper for the per-cell rules.

No change in behaviour.
azure-terraform-deployment defines cell_b's small pool as Standard_D4s_v5 (4 vCPU), and launch-system
starts one rank per core, so 4 ranks, not the 2 guessed. Its nodes are limited by the vCPU quota of
the subscription, which is not in the repository, so cell_b gets no max_instances.
It is cached and shared by every simulation of a circuit. The dataclass is frozen, its mappings
are MappingProxyType and its per-cell arrays are not writeable. read_synapses_per_cell builds the
counts in local dicts and the CellSynapses at the end, and the count of a node set is the function
synapses_by_source instead of methods.
Only what mutated it had to go: count_by_source and count read it, and stay methods of the frozen
dataclass.
pydantic's frozen config replaces the frozen dataclass and its __post_init__: the fields cannot be
replaced, the mappings and arrays are not wrapped.
docs/circuit-simulation-resources.md lists every constant of the estimate, the counting and the
instances: its value, what it represents and how it was measured or chosen. The code keeps short
docstrings and a pointer to it.
MemoryModel holds the terms as fields with the measured values as defaults, and computes the memory
every node holds (per_node_gb) and the memory split over the nodes (split_gb). simulation_nodes
uses the MEMORY_MODEL instance, or another model if given.
peak_memory_safety_factor says what the factor applies to. reserved_node_memory_fraction (0.1) is
the share of a node's memory kept for the system, Slurm and MPI, instead of the share left (0.9).
BYTES_PER_KIB, BYTES_PER_MIB and BYTES_PER_GIB replace the 1024, 2**20 and 2**30 of the memory model
and of the LFP weights: the estimate is in GiB, as memory_per_instance_gb.
MIB, KIB and BYTE are those units in GiB, the unit of the estimate, so each term reads as it was
measured: 5.55 * MIB per cell, 3.07 * KIB per synapse, 41.4 * BYTE per spike and rank.
simulation_synapse_counts and replay_spike_files (obi_one/scientific/library/simulation_synapses.py)
replace the task's _synapse_counts and _replay_counts; the task checks the circuit applies and
calls them.
…service cache

The simulations of a campaign share the staged circuit: the first reads the edge indices and saves
the synapses per cell next to its config (synapses_per_cell.npz), the others load it. The module
cache, its lock and its eviction go: nothing is kept in the service between requests, and each
campaign reads the indices once. The file of the whole rCA1 circuit is 5.2 MB, saved in 11 ms and
loaded in 9 ms.
@eleftherioszisis
eleftherioszisis force-pushed the number-synapses-from-edge-indices branch from 2e0590e to c42f6a4 Compare November 27, 2026 11:20
It is an entitysdk helper (a directory listing and Client.open_file), so it sits with the other
circuit helpers of db_sdk, with REMOTE_BLOCK_SIZE. circuit_staging's _asset_path_in becomes the
public asset_path_in, which both use.
…tences

Functions say what they return or do, classes and attributes what they hold, without getting
longer. A test docstring still spoke of a cache.
It is how many times denser than average a subset may be (synapses per cell of a node set, or per
source of the replayed sources), which is what the 2.5 was measured as.
share becomes worst_case_fraction_of_total (of a population counted only in total) and
worst_case_fraction_of_weights (of the weight matrices).

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant