---
title: sGraph Verification in Tensor Programs
url: https://www.emergentmind.com/topics/sgraph-verification
type: topic
---

# sGraph Verification in Tensor Programs

sGraph Verification

An sGraph is a symbolic, hierarchical representation of a family of tensor programs, used to compactly encode and reason about program equivalences and optimizations by symbolically representing certain execution parameters, program structure, and parallelization strategies. sGraph verification refers to the formal process of proving that all concrete instantiations of a given sGraph are functionally equivalent to a target tensor computation under a specified set of algebraic identities and hardware constraints. Conceptualized in the Prism/SIGMA superoptimization framework, sGraph verification leverages symbolic expression analysis, algebraic rewriting, and e-graph–based equivalence checking to guarantee the semantic correctness of tensor program transformations [2604.15272].

## 1. sGraph Definition and Structure

An sGraph \(S\) is defined as the tuple:
\[
S = (V,\,E,\,\mathcal{P},\,\mathbf{m},\,\mathit{expand})
\]
where:
- \(V\): Set of hierarchical nodes, each representing a kernel-level, block-graph, or thread-graph operator.
- \(E \subseteq V\times V\): Directed acyclic edge set capturing data-flow dependencies.
- \(\mathcal{P} = \mathcal{P}_g \cup \mathcal{P}_f\): Symbolic parallelization parameters (grid and loop dimensions).
- \(\mathbf{m} = \{ m_{T,d,p} \}\): Boolean mapping variables, specifying if data dimension \(d\) of tensor \(T\) is partitioned along parallel dimension \(p\).
- \(\mathit{expand}\): A mapping that assigns each hierarchical operator node to a finer-grained sGraph, preserving the GPU kernel/block/thread hierarchy.

Partitioning constraints enforce that for each \(T\), each \(p\) partitions at most one \(d\) and each \(d\) is partitioned at most once w.r.t. grid dimensions. An sGraph thus symbolically captures entire classes of program implementations sharing common structure and parallelization schemes [2604.15272].

## 2. Symbolic Semantics and Mapping Variables

sGraphs define families of tensor programs that depend symbolically on mapping variables (\(\mathbf{m}\)) and parallel dimension sizes (\(\mathbf{d}\)). For each data dimension \(d\) of tensor \(T\), the “local” size in a given subgraph equals:
\[
\frac{D_{T,d}}{\sigma(T,d;\mathbf{m},\mathbf{d})}, \qquad
\sigma(T,d;\mathbf{m},\mathbf{d}) = \prod_{p\in\mathcal{P}} \left( m_{T,d,p} d_p + (1-m_{T,d,p}) \right)
\]
where \(D_{T,d}\) is the global extent. These symbolic shape transformations mechanically propagate through the hierarchical structure via the expand field.

Algebraic constraints, such as
\[
\frac{D_X}{\sigma(X,c)} = \frac{D_W}{\sigma(W,r)} \implies m_{X,c,p} = m_{W,r,p}\quad\forall p
\]
are collected to ensure shape compatibility for operators like matmul, enforcing valid parallel decompositions.

## 3. Verification via E-Graph Equivalence

For a fixed assignment \(\hat{\mathbf{m}}\) (concrete mapping of partition variables), each sGraph induces a deterministic tensor expression \(E_{\text{cand}}\). The verification problem is to check whether:
\[
E_{\text{cand}} = E_{\text{in}}
\]
where \(E_{\text{in}}\) is the reference (e.g., user-specified) computation.

sGraph verification is performed using equivalence graphs (e-graphs), such as those implemented in the egg library. The expression language is augmented with special operators for partitioning (\(\mathsf{part}\)), reduction (\(\mathsf{red}\)), combination (\(\mathsf{comb}\)), and replication (\(\mathsf{repl}\)), and key tensor operations (matmul, add, etc.). A rich set of rewrite rules formalize algebraic identities, including:
- Associativity, distributivity of matmul
- Commutation and cancellation properties of parallel operators
- Parallel matmul reductions (e.g., \(\mathsf{red}(\mathsf{matmul}(\mathsf{part}(A,p)), \mathsf{part}(B,q)),p) \to \mathsf{matmul}(A,B)\))

If the e-graph unifies \(E_{\text{cand}}\) and \(E_{\text{in}}\) into one equivalence class after saturation, the candidate mapping is deemed correct [2604.15272].

## 4. Pruning: Algebraic and Hardware Constraints

sGraph verification is coupled with structured pruning:
- **Symbolic dimension matching**: Linear equalities over mapping variables are enforced to ensure that all partitionings and reductions are shape compatible.
- **Expression-guided pruning**: At \(\mathbf{d} = \mathbf{1}\), the sGraph degenerates to a scalar-dataflow DAG; checks are performed that every intermediate computation must be structurally encountered in the root expression, cheaply eliminating degenerate sGraphs.
- **Hardware constraints**: Resource limitations (e.g., shared memory) further restrict allowed values for \(\mathbf{d}\), ensuring generated sGraphs are hardware-feasible.

This logic allows the superoptimizer to consider only structurally valid and concrete-equivalent program representations.

## 5. Correctness Criteria and Soundness

*Correct mapping*: An assignment \(\hat{\mathbf{m}}\) is correct if, for all parallel dimension choices \(\mathbf{d}\), the corresponding sGraph program yields the same functional result as the reference computation:
\[
\forall\,\mathbf{d}: S_{\hat{\mathbf{m}},\mathbf{d}}\ \text{computes the same function as }G_{\text{in}}
\]
*Feasible sGraph*: An sGraph is feasible if some correct mapping exists.

Soundness of the verification procedure is conditional on the soundness of the employed algebraic axioms: if the e-graph deems the sGraph correct, all corresponding concrete programs are semantically equivalent for all allowed dimension assignments. Completeness (catching all possible correct mappings) is not guaranteed [2604.15272].

## 6. Example: Fused Softmax-MatMul Verification

Prism demonstrates verification with a fused softmax–matmul block. The candidate sGraph, under suitable mapping choices, can be algebraically reduced using successive e-graph rewrites (pushing partitions into \(\mathsf{exp}\), rewriting reductions, commuting combination operators) to the original computation:
\[
E_{\text{in}} = \mathsf{matmul}\left(\mathsf{div}\left(\mathsf{exp}(v_X), \mathsf{sum}(\mathsf{exp}(v_X),c)\right), v_W \right)
\]
Matching against the optimized sGraph expression after rewrites yields a verified equivalence.

## 7. Practical Impact and Scope

Evaluation on large LLM workloads shows that sGraph-based superoptimization achieves \(2.2\times\)–\(4.9\times\) speedup over state-of-the-art, with optimization times reduced by up to \(3.4\times\). sGraph verification thus enables exhaustive yet scalable exploration of high-performance tensor kernel implementations in real-world systems, bridging formal functional verification and practical superoptimization for production workloads [2604.15272].

Source: https://www.emergentmind.com/topics/sgraph-verification