advanced24 min studyLesson 824 in Engineering

Compact minibatches while preserving global node identity

Share feature rows without accidentally merging messages, layers or time contexts.

On this page

What you’ll understand

  • Build and invert a global-to-local node map.
  • Preserve sampled edges and multiplicity after sharing feature rows.
  • Distinguish seed outputs from context nodes and versioned states.

The same global node can appear through several seeds, paths or replacement draws in one minibatch. Fetching its feature row once can save memory and network traffic, but computation still needs to remember where each occurrence sends a message. Compaction is an indexing transformation with semantic constraints. It should preserve the declared sampled computation rather than treating every repeated identifier as redundant work that can be deleted.

Local indices need an invertible identity map

Assign each required global node a local index in a batch table, then rewrite every sampled edge endpoint through that map. Keep the inverse mapping for feature fetches, labels and output interpretation. PyTorch Geometric 2.7.0 NeighborLoader exposes n_id for global node identities and distinguishes the seed-node batch size from the full sampled context. Code that assumes every local node has a training target can accidentally supervise context nodes or compare predictions with labels in the wrong order.

A global identifier includes its namespace and, for heterogeneous graphs, its node type. User 17 and item 17 need not identify the same entity. Graph partitioning can also relabel nodes, so a partition-local ID is not automatically a dataset-global ID. Test identity round trips and edge endpoints using deliberately nonconsecutive IDs. Sequential toy IDs can allow a mistaken array-position assumption to survive until production data introduces larger offsets or a different node ordering.

g:VB→{0,…,∣VB∣−1},g−1(g(v))=vg:V_B\to\{0,\ldots,|V_B|-1\},\qquad g^{-1}(g(v))=v
V_B is the batch’s set of global identities under a declared namespace and context. g is a bijective compaction map, not a hash with collisions; local indices and node counts are dimensionless.↔ Scroll wide equations horizontally.

Share storage without erasing the computation graph

A source shared by two destinations still contributes to both edges. A repeated replacement draw can require repeated contribution or an equivalent weight even if its feature row is stored once. During backpropagation, gradients from every use must accumulate into the appropriate source representation or embedding row. Collapsing edge records merely because one endpoint repeats changes the graph operator. If parallel edges have distinct relation attributes or times, preserve those distinctions as well as their endpoint identities.

Representations at different layers are different tensors even when their node IDs agree. Shared input storage does not imply the same hidden vector can replace both h at layer one and h at layer two. A minibatch can use bipartite computation blocks with separate source and destination index sets, so remapping must follow each block’s contract. Similarly, two uses of a node under different temporal cutoffs or feature versions cannot safely share a computed state unless the model and version rules prove that state is the same.

∂L∂hu=∑e∈U(u)∂L∂hu∣e\frac{\partial L}{\partial h_u}=\sum_{e\in\mathcal U(u)}\left.\frac{\partial L}{\partial h_u}\right|_e
The sum is over all uses of the same differentiable source representation h_u in this computation graph, including retained multiplicities. It describes accumulation of contributions with compatible feature units; it does not merge distinct layer or temporal states.↔ Scroll wide equations horizontally.

Check seed order and reproducibility

After compaction, verify that output positions map back to the intended seeds before computing loss or serving responses. Preserve edge sampling probabilities and any normalization data through reordering. Measure occurrences, unique feature rows, sampled edges and computation-block sizes separately; one reduced count does not establish a smaller peak allocation. Use a fixed sampled trace to compare compacted and unshared implementations numerically. Small floating-point differences from reduction order can be acceptable under a declared tolerance, while missing contributions are a semantic failure.

The cache lab demonstrates sharing only at an abstract address level. Reset and set both C=4 and s=4; the first sixteen requests contain four unique addresses, yielding four misses and twelve hits. Step adds a seventeenth request and predicts one further hit. The stream uses N=min(4096,16+floor(2t)) and begins empty. A hit avoids another abstract fetch but does not erase that request’s logical use. This LRU instrument has no graph edges, gradients, global-to-local maps or layer-specific tensors.

One shared source still serves two destinations

Seeds are global nodes 10 and 20. Sampled edges are 30→10, 30→20 and 40→20. Compact all required nodes as [10,20,30,40]. Source scalar features are h₃₀=3 and h₄₀=4. Each stored input feature row occupies 32 bytes.

  1. The map is 10↦0, 20↦1, 30↦2 and 40↦3. There are four unique input rows, using 128 bytes. Storing two seed occurrences plus three source occurrences independently would use five rows, or 160 bytes, under this input-only accounting.

  2. Remap the edges as 2→0, 2→1 and 3→1. The two uses of source 30 remain two edges even though its feature row appears only once in the compact table.

  3. A plain incoming sum gives output 3 for seed 10 and 3+4=7 for seed 20. Removing either edge from node 30 because its ID repeats would corrupt one output. The inverse map sends local outputs 0 and 1 back to seeds 10 and 20.

The result

Compaction saves one input feature row here while preserving all three messages. Its correctness is demonstrated by the output map and the unchanged per-seed aggregates, not only by a smaller allocation.

Explore the model

Interactive lab

Interactive lab · cache

A shared fetch still has multiple uses

Reset → set C=4 and s=4 → predict four unique addresses, misses 4 and hits 12 in sixteen requests. Step adds one request and one hit, until the 4096-request cap.

A shared fetch still has multiple usesA fully associative LRU cache starts empty and requests address (i·s) mod 16. Hits move an address to the newest position; misses evict the oldest when full. Playback starts with 16 requests and adds two per second, capped at 4096 explicitly simulated requests. This toy stream is not a processor-cache benchmark. Current readings: Hits / requests: 0 / 16; Hit rate: 0%; Address-stream period: 16 addresses. All parameters are adjustable with the labeled controls below.Fully associative LRU cache after 16 deterministic requests: 0 hits and 16 misses.Most recent 16 requestsaddress = i × 3 mod 160M3M6M9M12M15M2M5M8M11M14M1M4M7M10M13MCurrent cache order · oldest → newest471013Latest address 13 · miss: inserted as newestToy address stream · period 16 · starts empty
Cached addressMiss / evictionMost recent request
Hits / requests
0 / 16
Hit rate
0%
Address-stream period
16 addresses
t = 0 s

A fully associative LRU cache starts empty and requests address (i·s) mod 16. Hits move an address to the newest position; misses evict the oldest when full. Playback starts with 16 requests and adds two per second, capped at 4096 explicitly simulated requests. This toy stream is not a processor-cache benchmark. Parameter changes pause playback and recompute the model at the current elapsed time. Reset restores both parameters and returns time to zero. Playback starts paused. Adjust either parameter, then choose Play or Step.

Look forSharing abstract address storage does not remove logical uses. The fully associative LRU instrument does not compact graph blocks or accumulate gradients.

Check your understanding

Why must edges 30→10 and 30→20 both remain after node 30’s feature row is compacted once?

Try it yourself

Seeds are 100 and 200, with edges 200→100, 300→100 and 300→200. Compact nodes as [100,200,300]. Let h₂₀₀=2 and h₃₀₀=5, and use 32-byte feature rows. Give remapped edges, seed sums and unique-row bytes.

Take a moment to make a prediction or work it out on paper. Open a hint when you need one.

Give me a hint

A seed can also be a source. Count identities across both roles once for input storage, while preserving all three directed edges.

Compare with the solution

The edges become 1→0, 2→0 and 2→1. Seed outputs are 2+5=7 for node 100 and 5 for node 200. Three unique input rows use 96 bytes, compared with five independent seed/source occurrences using 160 bytes. The sharing does not eliminate source 300’s second contribution or node 200’s separate role as a seed.

Take it with you

  • Keep explicit global/local maps and identify seed outputs.
  • Node storage deduplication does not remove edge contributions.
  • Layer, type, time and version context can prevent state sharing.

Read further

Original sources and deeper explanations to follow at your own pace.

Optional progress, saved in this browser when available. Revisit any lesson at any time.

Lesson not marked complete.