Automatic Differentiation for Tensor Algebras

Sebastian Urban, Patrick van der SmagtarXiv 2017

Abstract

Automatic Differentiation (AD) is a cornerstone of modern machine learning, yet existing frameworks often struggle with tensors defined through complex element-wise index mappings. This paper introduces a novel algorithm that utilizes integer linear algebra—specifically Smith Normal Form and Fourier-Motzkin elimination—to automatically derive symbolic derivative expressions for such tensors. The algorithm automates "index gymnastics," correctly handling summations and index transformations to produce efficient, parallel-ready gradient code.

Introduction

The efficiency of optimization in machine learning depends heavily on the accuracy and speed of gradient computations. While standard AD frameworks like PyTorch or TensorFlow excel at high-level operations, they encounter difficulties with "element-wise" defined tensors where the mapping between function indices and argument indices is non-trivial. For instance, a function defined as $f_{ij}(x) = \exp(x_{i+j})$ requires specific index shifts and summation ranges in its derivative. Manually deriving these expressions is tedious and prone to error. This work addresses the lack of a general method for automatically generating symbolic expressions for the backward pass when indices undergo linear transformations.

Methodology

The proposed algorithm extends symbolic reverse-mode AD to tensor algebras by treating index mappings as systems of linear integer equations and inequalities.

  1. Symbolic Reverse Accumulation: The process begins with standard AD on a computational graph where nodes represent elementary functions and edges represent arguments.
Computational graph illustrating symbolic reverse accumulation for a generic function.
  1. Integer Linear Equations: To map function indices $\alpha$ to argument indices $\beta$, the algorithm solves $A\alpha = \beta$. Using the Smith Normal Form, it identifies a "pseudo-inverse" for these integer index mappings.
  2. Integer Linear Inequalities: When mappings are not one-to-one (e.g., an index disappears in the function definition), summations are required. The algorithm employs Fourier-Motzkin elimination to determine precise summation bounds, ensuring symbolic expressions iterate only over valid, non-zero index ranges.
  3. Unified Derivation: These steps culminate in a unified "Element-wise expression derivation" algorithm that transforms a tensor function definition into a symbolic gradient expression suitable for tensor compilers.

Key Insights

A primary insight of this work is that common tensor indexing operations—such as transposes, diagonals, and shifts—can be represented as linear transforms. This allows the use of integer programming tools to automate derivation. Furthermore, the algorithm "discovers" the necessary mathematical structure, automatically inserting sum operations or Kronecker deltas (implemented as efficient if-then-else blocks) as needed.

Crucially, the resulting symbolic expressions are "embarrassingly parallel." Because the algorithm produces expressions for each element of the derivative, the code can be executed in CUDA kernels without needing atomic operations or complex synchronization, as each thread computes its own output element independently.

Results and Evaluation

The algorithm's performance was evaluated on complex, nested tensor expressions, demonstrating both symbolic correctness and computational efficiency.

  • Numerical Verification: The derived symbolic expressions were verified by comparing their output against finite difference methods and standard AD, showing perfect agreement.
  • Efficiency and Node Reuse: By reusing intermediate values within the computational graph, the algorithm prevents the "expression blowup" typical of naive symbolic differentiation. This reuse is critical for maintaining performance in complex trigonometric or nested functions.
Computational graph for a complex trigonometric function showing node reuse.

Limitations and Future Work

The current methodology is restricted to linear index combinations; non-linear indexing (e.g., $x_{i^2}$) remains a challenge for the integer linear algebra approach. Future work includes integrating this algorithm into production tensor compilers like TACO and developing further optimization passes to simplify the generated symbolic expressions for maximum performance.

Conclusion

By formalizing index gymnastics through integer linear algebra, this research provides a robust framework for extending automatic differentiation to complex tensor algebras, facilitating the development of more flexible and efficient machine learning models.

Summary updated

BibTeX
@misc{urban2017automatic,
  author = {Sebastian Urban and Patrick van der Smagt},
  title = {{Automatic Differentiation for Tensor Algebras}},
  year = {2017},
  eprint = {1711.01348},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/1711.01348},
}
Figure