Static Verification of Tensor Shapes in Deep Learning

30 concepts16 papers1 proposal

In modern deep learning, tensors are the fundamental currency of computation. As these multi-dimensional arrays flow through complex neural network architectures, they undergo continuous transformations—reshaping, transposing, slicing, and broadcasting.

However, traditional deep learning frameworks treat tensor shapes as dynamic, runtime properties. This design choice introduces a major pain point for developers: shape mismatches and silent logic errors. If an axis is accidentally swapped or an incorrect dimension is passed to a layer, the program may crash hours into a training run, wasting expensive compute time. Worse, due to automatic broadcasting rules, the code might execute successfully while producing mathematically invalid results, leading to silent model degradation.

To bridge this gap, a growing body of research focuses on static and type-level verification of tensor shapes. The core idea is simple yet powerful: move the validation of tensor dimensions from runtime execution to compile-time analysis, transforming cryptic runtime failures into immediate, actionable feedback.


The Anatomy of a Silent Tensor Error

To understand why static verification is necessary, consider how deep learning models are typically written in dynamically typed languages like Python or JavaScript. As observed by Lili Quan et al., the flexible nature of these environments makes it easy to pass invalid data shapes to deep learning APIs.

When working with high-dimensional simulations or point-free programming (where operations are applied to whole tensors without explicit index labels), tracking axes manually becomes incredibly difficult. As Philipp Holl et al. and Florian Fervers et al. point out, the absence of symbolic axis names forces developers to manually track the ordering of batch, spatial, and instance indices.

This lack of strict enforcement leads to two major failure modes:

  1. Silent Failures: As highlighted by Momoko Hattori et al. and Han Wang 0023 et al., a tensor operation can execute without throwing an error but yield semantically invalid internal math. For instance, an accidental dimension swap might cause a model to broadcast weights across the batch dimension instead of the feature dimension.
  2. Security Vulnerabilities: Beyond logic errors, Huaming Chen et al. demonstrated that the vast majority of software vulnerabilities in frameworks like TensorFlow and PyTorch stem from inadequate validation of tensor attributes like rank and shape, creating an expanded attack surface for memory-related exploits. Even optimization logic can fail; Juan Zhai et al. showed that browser-side deep learning frameworks sometimes corrupt data during cache reuse because they identify distinct tensors as identical based solely on shared dimensions.

Approach 1: Lifting Shapes into the Type System

The most robust way to eliminate shape errors is to make them mathematically impossible by encoding tensor dimensions directly into a language's type system.

In a conventional tensor library, a tensor's type is simply Tensor. In a typesafe abstraction framework, the type signature includes its structural metadata, such as Tensor<Batch, Channel, Height, Width>.

Conventional:  [32, 3, 224, 224] ---> Tensor
Typesafe:      [32, 3, 224, 224] ---> Tensor<Batch, Channel, Height, Width>

Tongfei Chen demonstrated this paradigm in Typesafe Abstractions for Tensor Operations by leveraging heterogeneous lists (HLists) in Scala. In this setup, tensor transformations like transposition or dimension expansion are not just runtime array manipulations; they are type-level functions that compute the resulting dimension list at compile-time. If a developer attempts to multiply two matrices with incompatible axes, the compiler detects the mismatch immediately because the types do not unify.

This approach prioritizes the semantic identity of a dimension over its integer size. For example, a batch size of 32 and a channel depth of 32 are distinct types, preventing accidental broadcasting between them.

Similarly, other research directions embed these constraints into functional and formal environments:

  • Staged Compilation: As proposed by Richard Wei et al. in the DLVM compiler infrastructure, a staged domain-specific language (DSL) uses a strongly typed host language to verify tensor shapes during compilation, catching dimensional inconsistencies before generating executable code.
  • Linear Types: Patrik Jansson et al. leverage linear types in Haskell-embedded DSLs to ensure that tensor indices are handled correctly and adhere strictly to their defined algebraic properties.
  • Formal Proof Assistants: In TorchLean: Formalizing Neural Networks in Lean, Robert Joseph George et al. and the authors treat neural networks as first-class mathematical objects. By integrating dimensions into Lean's dependent type system, they provide formal guarantees of architectural consistency.

Approach 2: Static Analysis and Symbolic Execution

While type-level verification provides ironclad guarantees, rewriting massive deep learning codebases in languages like Scala, Haskell, or Lean is often impractical. Most data science relies on Python. To protect Python developers, researchers have built static analysis tools that inspect code without executing it.

Julian Dolby et al. introduced Ariadne, a framework that extends static dataflow analysis to Python code using TensorFlow. Ariadne tracks the structural and dimensional semantics of tensors through the computation graph to catch subtle reshaping errors.

For more complex dynamic code, tools like PyTea, developed by Ho Young Jhoo et al., utilize symbolic execution and Satisfiability Modulo Theories (SMT) solvers.

[PyTorch Source Code] 
         │
         ▼
[Symbolic Translation] ──> Tracks shapes as symbolic variables (e.g., x = Batch)
         │
         ▼
   [SMT Solver] ─────────> Evaluates: Does any execution path violate 
         │                         operation-specific shape requirements?
         ▼
[Immediate Error Report]

PyTea translates PyTorch code into a symbolic internal representation where tensor dimensions are treated as symbolic variables rather than concrete integers. The tool then feeds the execution paths into an SMT solver. The solver evaluates the logical consistency between the path conditions and the operation-specific shape requirements. If the solver finds a scenario where an execution path is valid but the tensor operations violate dimension constraints, it flags a definitive shape mismatch in seconds—saving hours of wasted GPU training time.


Approach 3: Declarative Notations and Graph Verification

A parallel strategy focuses on changing how developers express tensor manipulations, replacing error-prone imperative code with self-documenting, declarative patterns.

Alex Rogozhnikov introduced this paradigm with Einops, which replaces cryptic sequences of reshape, transpose, and squeeze with a single, Einstein-inspired string notation. For example, an operation is written explicitly as:

$$\text{b c h w } \rightarrow \text{ b (h w) c}$$

As argued in Einops: Clear and Reliable Tensor Manipulations with Einstein-like Notation, this notation enforces structural integrity by requiring that all input dimensions are explicitly accounted for in the output pattern. While the original Einops library relies on runtime checks, its declarative nature lays the groundwork for seamless static analysis.

Finally, verification must extend to the compilers that optimize these graphs. Production compilers constantly rewrite tensor execution graphs to maximize hardware efficiency. Jai Arora et al. developed TensorRight, an automated verification system capable of proving the correctness of tensor graph rewrite rules for tensors of arbitrary rank and size. By mathematically reducing the infinite space of possible tensor shapes to a small, finite set of test cases, it ensures that compiler optimizations never introduce shape bugs or alter model semantics.

Conclusion

As emphasized by Paolo Bientinesi et al., establishing rigorous methods to ensure tensor computations are well-formed is essential for the safety and adoption of modern software infrastructure. Whether through type-level encodings, symbolic static analysis, or declarative notations, moving shape verification to the earliest stages of development provides the formal guarantees needed to build reliable, secure, and correct deep learning systems.

Key Foundational Work

Typesafe Abstractions for Tensor Operations (2017) — contributed 4 concept elements to this direction.
DLVM: A modern compiler infrastructure for deep learning systems (2018) — contributed 3 concept elements to this direction.
Ariadne: analysis for machine learning programs (2018) — contributed 1 concept elements to this direction.
History & citationVersion 1 · 2026-10-04

Current · Permanent link

View frozen version 1

Cite this direction

Latest version (a living page):

@misc{lacuna_staged_domain_specific_language_0072ff68,
  title = {staged domain-specific language},
  author = {{Lacuna}},
  howpublished = {Lacuna research direction},
  year = {2026},
  url = {https://lacuna.tiptreesystems.com/direction/staged-domain-specific-language/rdr_0072ff6898ed4555a240662a374249b8},
  note = {Latest version of a living research direction; cite a version for a fixed text. Accessed 2026-11-19},
}

This exact version (version 1, frozen text):

@misc{lacuna_staged_domain_specific_language_0072ff68_v1,
  title = {staged domain-specific language},
  author = {{Lacuna}},
  howpublished = {Lacuna research direction},
  year = {2026},
  url = {https://lacuna.tiptreesystems.com/direction/staged-domain-specific-language/rdr_0072ff6898ed4555a240662a374249b8/v/1},
  note = {Version 1, frozen 2026-10-04. Accessed 2026-11-19},
}
About

These directions overlap with this one through shared papers.

See something wrong? Report a mistake