Spenso implementation architecture
Tensor structures and payloads, direct contraction, network graph/store execution, Symbolica parsing, serialization and ownership boundaries, invariants, and tests.
Status: Current implementation architecture, audited against the Spenso source on 2026-10-01.
This note covers the
spensoRust crate.spenso-macros,spenso-hep-lib, andspynso3are separate packages: they provide derives, concrete physics tensors, and a Python adapter rather than a second tensor engine.
Shared display notation
symbolica-notation owns IndexDisplay, its validation, escaped rendering and existing spenso::index-display-v1 encoding. It depends only on Symbolica and thiserror and is shared with graph presentation without a Spenso dependency. Representation metadata, palettes, classes and row placement remain in Spenso. The extraction preserves rendered bytes and portable math-display attachments.
Boundaries
Spenso is an in-process, generic tensor and tensor-network library. Its implementation is split into these layers:
structure: representations, dimensions, abstract and concrete indices, slots, ordering, permutations, names, and structure merging;tensors: dense, sparse, symbolic, and—when Symbolica support is enabled—parametric payloads;algebra,iterators, andcontraction: scalar promotion, indexed traversal, traces, exterior products, and single- or multi-index contraction kernels;network: a Linnet-backed expression graph, separate value store, tensor and function libraries, contraction planning, and sequential or parallel execution;- feature-gated Symbolica integration in
shadowing,network::parsing,symbolic_parallelism, andsymbolica_init.
The crate does not own a global physics tensor catalogue. A caller supplies a Library for external tensor keys and a FunctionLibrary for opaque functions. spenso-hep-lib is one possible source of concrete tensors; Idenso is a separate identity-and-rewrite layer over Spenso-compatible Symbolica expressions.
Core representation
The structural kernel is Slot<R, Aind>, which pairs a Representation<R> with an abstract index. A representation combines a representation name and a dimension. Its RepName implementation owns duality, matching, ordering, and any negative-metric convention. Two slots contract only when their dimensions and representation orientations match and their abstract indices are equal after dualization.
TensorStructure exposes an ordered external slot sequence and the conversions derived from it: shape, size, flat and expanded indices, duality, permutations, and repeated-index traces. Concrete implementations choose different ownership policies: OrderedStructure owns an ordered slot vector, NamedStructure adds a global name and arguments, and SmartShadowStructure couples symbolic and ordered views. Canonicalized keeps a value in canonical order alongside its immutable CanonicalLayout; reindex_storage instead returns Reindexed, with a checked pending index permutation to apply to the payload. HasStructure keeps metadata separate from that payload.
IndexedStructure names Canonicalized<OrderedStructure<LibraryRep, AbstractIndex>>. Its logical_slots() applies the retained layout to recover the declared axis order. The alias alone does not exclude AbstractIndex::Open; the strict TryFrom conversion from PartialStructure rejects unresolved ports, including open indices nested in scopes. It allocates no indices and preserves canonical slot order, representation partitions and both layout permutations without sorting again. This matters when symbolic dimensions compare equal during ordering.
PartialStructure keeps the same layout with PartialIndex<AbstractIndex> for general symbolic and positional composition. Indexed-to-partial metadata projection wraps each index as explicit and retains the existing order. These structure operations do not rewrite tensor payloads; Idenso owns checked carrier conversion and proof preservation. Neither layout-bearing wrapper implements the canonical-storage TensorStructure contract of bare OrderedStructure.
The principal materialized payloads are:
DenseTensor<T, S>, an element vector plus structure. Its checked constructor requires the vector length to equal the structure size, apart from the one-element scalar case;SparseTensor<T, S>, a map fromFlatIndexto non-default entries, an explicit zero value, and the structure. Checked index-based construction converts validated concrete indices to flat positions;DataTensor<T, S>, a dense-or-sparse enum that preserves one structure and coefficient type across generic operations.
These fields are public, so the checked constructors are the invariant-preserving boundary; callers constructing structs literally are responsible for keeping payload extent and structure consistent.
Direct contraction flow
Contract first delegates topology to StructureContract::merge. The merge identifies slots that match the other structure’s dual slots, removes contracted pairs, and returns both operand subsets plus MergeInfo, which records clean operand order or an interleaved result.
left structure + right structure
-> merge dual slots and construct result structure
-> zero matches: exterior product
-> one match: single contraction
-> multiple matches: multi contraction
-> apply normal, reversed, or interleaved data kernel
-> tensor with the merged external structureCoefficient types participate through fallible multiplication, accumulation, subtraction, and promotion traits. Representation-specific negative entries can therefore change accumulation signs without changing the structural matching rule. Dense–dense, dense–sparse, and sparse–dense DataTensor contractions produce dense output; sparse–sparse preserves sparse output. Internal pairs on one tensor use Trace::internal_contract instead of the two-operand path.
Network representation and execution
Network<S, LibKey, FunKey, Aind> owns three independent pieces of state:
NetworkGraphowns expression topology in a Linnet half-edge graph. Nodes are either leaves orSum,Neg,Product,Power, andFunctionoperations. Head edges encode the expression tree; slot edges encode contractions between dual slots;- the generic store
Sowns local tensor and scalar values. The standardNetworkStore<T, Sc>uses stable vector positions during an execution and keeps optional scalar aliases alongside the source scalars; NetworkStatesummarizes whether the expression is a pure scalar, scalar, tensor, or self-dual tensor so network algebra can maintain compatibility.
A leaf refers to a local store position, a lazy tensor sum or scaled tensor, a scalar position, or a caller-owned library key with its canonical layout. Live indices belong to graph edges, not a duplicate leaf cache. The graph never embeds the external library value. Materialization occurs through the supplied Library, while opaque function nodes delegate to the supplied FunctionLibrary.
map_occurrences materializes each graph occurrence against its current port bindings while reusing untouched stored aliases with the same logical layout. It checks surviving storage slots and records any storage permutation separately from the logical-position witness. reindex_ports applies simultaneous external port assignments through this boundary; it does not relabel unrelated internal contractions with matching labels. Concrete execution rejects unmaterialized bound ports. Network arithmetic refreshes its state from the resulting graph, and component execution invalidates logical layout observations it cannot retain.
NetworkGraph::to_expression_at_with_operation exposes native expression emission through separate value and operation callbacks. The value callback can intercept a whole node before descent; otherwise children are emitted in tree order and the operation callback sees their completed expressions. Unary arity is checked before that callback. Returning None uses the native constructor, callback errors propagate, and generated syntax is not visited again. This output operation does not admit a typed tensor or publish its retained metadata.
Execution follows one semantic path with interchangeable policies:
Network::execute
-> merge adjacent expression operations
-> trace slot self-loops on leaves
-> find ready operation subgraphs
-> materialize library leaves when needed
-> execute scalar, sum, power, function, or product operation
-> choose product pairs through ContractionStrategy
-> replace each completed subgraph with a result leaf
-> refresh NetworkStateExecutionStrategy controls graph scheduling; ContractionStrategy controls pair selection inside products. Sequential, bounded Steps, and extraction-based variants share the same ExecuteOp boundary. Parallel evaluates independent ready operations against read-only base storage plus per-worker overlays, then rebases newly appended store indexes before graph replacement. It deliberately falls back to sequential execution when Symbolica’s Rayon support is disabled or the contraction strategy performs partial graph rewrites.
Symbolic parallelism and optimized sums
With symbolica, the process-wide SymbolicParallelism policy controls Rayon work involving Symbolica atoms. Auto keeps WebAssembly kernels serial; on native targets it checks the Symbolica license once and uses a workload heuristic where an operation provides one; Serial disables Rayon; Parallel forces Rayon and bypasses Auto’s license safety check. Configure the policy before tensor work: changing it concurrently with an active operation is unsupported.
The network Parallel execution strategy still falls back to Sequential when that policy resolves to serial or when the selected contraction strategy performs partial graph rewrites. Separately, FastTensorSum, FastTensorSumContractible, balanced scalar/tensor sums, contraction profiles, and pair estimates provide optional optimized paths inside the same network semantics. Unsupported shapes or unprofitable cases use the ordinary sum and contraction paths. Network counters and timers are opt-in diagnostic instrumentation; they do not change the execution result. SPENSO_NETWORK_PROFILE records cumulative counters and timers with periodic reports and slow-operation diagnostics. Per-operation starts, batch completion and merge-progress lines additionally require SPENSO_NETWORK_PROFILE_VERBOSE.
Symbolica parsing boundary
The symbolica feature enables network::parsing. NetworkParse and the more general Network::try_from_view lower a Symbolica AtomView into the same graph and store used by programmatic construction. Dispatch recognizes addition, multiplication, integer powers, and functions before falling back to scalar or opaque-tensor construction.
ParseSettings is semantic policy, not formatting: it controls scalar precontraction, first-term structure discovery, recursion depth, shorthand expansion versus opaque leaves, composite scalar handling, and how strictly a function must be tagged as a tensor. The tensor library resolves known keys; TensorFromExpression owns construction of an opaque tensor leaf; the function library owns supported opaque operations. A parse-local ParseState reserves written slot-index names before allocating dummies, and parser clones share that reservation set and allocator. Positive powers that lower shorthand with internal dummies reparse each copy from the original base, giving it fresh internal indices while retaining explicit boundary slots. Callers combining independently parsed expressions must still manage index namespaces deliberately.
Trusted shallow planning uses Network::try_from_admitted_view with opaque shorthand and depth one. It shares the ordinary parser’s construction and dispatch, while reusing established syntax and dummy-scope admission instead of validating and reserving the whole input again. LeafInterfaceCache stores exact boundary expressions’ tensor classification and logical slots. Unchanged leaves reuse these facts across graph replacements, including scalar interfaces with internal contractions. The cache requires unchanged classification policy and admission facts; callbacks and materialized rewrites leave the trusted path. No live occurrence binding is stored in this cache: graph ports still own it. The profiler records misses, hits, actual syntactic visits, and uncached inference time separately from general structure-attempt time.
The detailed dispatch and shorthand behavior are recorded in the Symbolica-to-network parsing flow. Syntax and rewrite ownership across Spenso and Idenso are recorded in the Symbolica syntax and rewrite boundary.
Features and serialization
The default native feature forwards GMP/MPFR support; wasm selects Symbolica’s Wasm backend with default features disabled. Native code generation is a separate native-code-generation feature. symbolica enables Symbolica atoms, parametric tensors, parsing, state-aware decoding, and Linnet’s Symbolica adapter. python only adds PyO3 extraction for Spenso’s complex scalar type; the full Python tensor surface lives in spynso3. python-stubs adds stub metadata and implies python.
Structures, dense and sparse tensors, DataTensor, NetworkGraph, NetworkStore, and Network derive Serde and bincode representations when their generic fields support them. Under symbolica, selected types also decode through Symbolica’s state map. These are type-level encodings, not a versioned checkpoint protocol: Spenso provides no state directory, database, schema migration, or cross-version compatibility promise. The optional portable-payload module carries representation and math-display declarations beside native Atom bytes through symbolica-typst-plugin; Spenso-aware consumers validate and register these before Atom import. The caller still owns byte storage, format versioning, Symbolica-state availability, and library-key reconstruction.
Ownership and error boundaries
Structure and direct-contraction failures are represented by SlotError, StructureError, and ContractionError. Network construction and execution widen these into TensorNetworkError, which also covers graph invariants, library lookup, function-library application, incompatible sums, unsupported powers, incomplete contraction, and I/O or opaque errors. Parsing returns that same network error type after converting coefficient and structure failures.
The store owns local results; graph leaves only own indexes into it. External libraries and function libraries are borrowed for an execution and remain caller-owned. Consequently a serialized graph containing LibraryKey leaves is not self-contained unless the same key semantics and values are restored. A completed network can expose zero, one, a scalar, or a tensor through its result accessors; callers should use those checked boundaries rather than assuming the final leaf kind.
Some low-level APIs retain programmer-invariant panics—for example literal structs with invalid shape, inconsistent ordered structures, or an integer power of a non-self-dual tensor. Public fallible constructors, parsing entry points, and execution methods are the intended boundaries for untrusted or dynamically inferred inputs.
Maintained invariants
- Slot contraction requires equal abstract indices and matching dual representations at equal dimensions; matching names alone is insufficient.
- Tensor payload order and flat-index interpretation come from the attached structure.
- A network’s head edges form expression dependencies, while slot edges carry tensor contraction topology; the two edge roles are not interchangeable.
- Local leaf indexes refer to the associated store. Joining stores or committing parallel overlays must rebase every affected tensor and scalar reference.
- A library leaf owns a key and canonical layout, not the library tensor. Materialization reads live indices from graph edges and applies the pending storage permutation at the library boundary.
- Ready-operation replacement preserves the boundary slot edges of the collapsed subgraph and finishes Linnet’s deferred node identifications before later scheduling.
- Parse depth and shorthand settings can change graph granularity without changing the external tensor structure that opaque leaves report.
Verification and related documentation
Unit and snapshot tests live beside structures, dense and sparse data, contraction kernels, network graph and store operations, libraries, execution strategies, symbolic parsing, parametric tensors, and serialization-sensitive types. The default typed boundary is exercised with cargo test -p spenso; parsing and symbolic execution require cargo test -p spenso --features symbolica. Rayon behavior also depends on the Symbolica parallelism setting, so performance checks should record both the Cargo feature set and runtime thread configuration.
For supported workflows, start with the first-contraction tutorial, continue with the network guide, and use the native Spenso Rustdoc for exact public signatures. Python construction and execution are documented in the Spynso3 workflow, whose ownership is separate from the core crate described here.
Factor preservation at materialization
Parsing retains scalar spectators and compatible sum boundaries. Fallible Concretize methods separate target construction from canonical layout: symbolic tensors may retain symbolic dimensions, while finite-component targets report errors. This uses the same CanonicalLayout as library leaves. TensorFromExpression returns that layout together with the resulting tensor. An eager opaque-word realization can contract repeated spectator ports before the outer network is assembled; its layout therefore describes the realized coordinate order. Symbolic targets retain the source layout because their contractions still belong to the outer network. The parser never reattaches a pre-contraction layout to a tensor with a smaller interface. The eager boundary also resolves completed lazy scaled tensors and tensor sums through result_tensor, preserving their coefficients and logical coordinates. Odd tensor powers preserve the remaining base after paired contractions. Completed execution waves release unused store payloads while retaining live aliases and tensor handles.
Process-local notation registry
The shared notation crate also owns Notation: an optional structured index label, an optional structured head, and bounded plain/LaTeX/Typst display text. Notation::register is idempotent for equal values and rejects conflicts; Notation::lookup returns a shared immutable handle. An empty lazy map makes no Symbolica calls during initialization. Locks cover only lookup and insertion, never print callbacks, and poisoned locks fail explicitly. Renderers offer the symbol callback first, then registered notation, then an escaped symbol name. Display-only tags and tensor-head user data are never read for presentation. The callback ownership flag distinguishes user callbacks from Spenso callbacks that delegate back into the renderer. No tags or user data change on registration. Python print mappings validate display text and parse allowlisted MathML into a private escaped presentation tree, bounded by bytes, depth and node count. Typst source exports populate document settings keyed by full symbol name: tensor-labels contains head visuals, index-labels contains records with label and visual, and callback-symbols lists callback-owned functions. print-heads and print-calls retain target-specific overrides. Endpoint labels s and t still suppress their ordinary slot one. Notation is never implicitly serialized by Symbolica Expression.save or State::export. Spenso-aware portable exports explicitly collect referenced registry entries in spenso.notation, version 1. Each fully qualified symbol identity keys a CBOR map with optional index-label, head, plain, latex, typst, and mathml. Callback code and callback ownership markers remain process-local; imports preserve identical existing local registrations. Structured labels reuse the bounded spenso.math-display tree encoding; display-identity index symbols retain that separate identity schema. Notation maps are limited to 64 KiB each and 128 declarations per set, within the common attachment limits of 256 records and 1 MiB total. Text overrides are limited to 4096 UTF-8 bytes; display trees share a 64-node budget and depth 16; MathML is parsed through the 4096-byte, 256-node, depth-16 allowlist. Import validates all declarations and conflicts before atom import, then registers names atomically without guessing symbol tags or attributes. Equal imports are idempotent; conflicting local notation produces a named typed error. Tydenso regenerates attachments only for output references, and its Typst layer turns validated structured labels into content and treats transported Typst overrides as text, never executable source. Imported DisplayText values retain a literal mode so subsequent Rust and Python Typst or LaTeX source exports also escape them. Trusted local source overrides retain their existing behavior.