An Efficient Matrix Multiplication Algorithm for Accelerating Inference in Binary and Ternary Neural Networks

Mohsen Dehghankar, Mahdi Erfanian, Abolfazl Asudeh

Introduction

Large Language Models (LLMs) and other Deep Neural Networks are notoriously resource-hungry, typically requiring high-end GPUs with massive VRAM to function. To make these models accessible on consumer hardware, researchers have increasingly turned to quantization—specifically binary (1-bit) and ternary (1.58-bit) weights, where every weight is restricted to values like $\{-1, 0, 1\}$.

However, even with these simplified weights, the underlying math of vector-matrix multiplication remains a computational bottleneck. Standard algorithms treat these matrices as dynamic, resulting in a time complexity of $O(n^2)$. This paper tackles a simple but powerful observation: once a model is trained, its weights never change. By exploiting this static nature, the authors introduce Redundant Segment Reduction (RSR), an algorithm that pre-calculates redundant patterns to break the $O(n^2)$ barrier, achieving up to a 29x speedup in matrix multiplication and a 6x reduction in memory usage.

Methodology: The RSR Algorithm

The core idea behind RSR is to eliminate redundant additions by grouping similar rows together. In a 1-bit or 1.58-bit world, there are very few unique ways a row can look. For instance, in a block of 10 columns, there are only 1,024 possible row patterns. In a matrix with 4,096 rows, many rows must be identical. RSR ensures we only perform the math for each unique pattern once.

The algorithm is split into two phases: Preprocessing (done once) and Inference (done at runtime).

  1. Column Blocking: The static weight matrix is split into smaller vertical blocks of width $k$.
  2. Row Permutation & Segmentation: Within each block, rows are sorted lexicographically based on their binary patterns. The algorithm identifies these "segments" of identical rows.
  3. Inference (Segmented Sums): At runtime, instead of multiplying the input vector by every single row, the algorithm first sums the elements of the input vector that correspond to the same weight pattern.
Overview of the RSR algorithm, detailing the Preprocessing and Inference stages.

By storing only the permutations and the unique segment indices rather than the full matrix, the memory footprint is reduced by a factor of $\log n$.

The RSR++ Optimization

While the base RSR algorithm significantly reduces redundant operations, the authors introduce an optimized version called RSR++ to further streamline the final computation step.

In standard RSR, the final step uses a standard product to compute the output. RSR++ replaces this with a recursive summation strategy. Instead of computing the product directly, it builds a summation tree that computes the final output in $O(2^k)$ time rather than $O(k \cdot 2^k)$.

Comparison of the final block product step in standard RSR versus the recursive summation in RSR++.

This optimization shifts the overall theoretical time complexity from $O(n^2)$ to $O(\frac{n^2}{\log n})$, providing a guaranteed logarithmic speedup over standard vector-matrix multiplication.

Experimental Setup and Key Results

The researchers evaluated their approach using synthetic factual queries (generated by GPT-4) and standard benchmarks like SimpleQuestions and TREC QA. They applied their algorithm to state-of-the-art 1.58-bit quantized models, including Llama3-8B, Falcon3-3B, and Falcon3-10B.

The practical gains were substantial across different environments:

  • Raw Performance: In native C++ implementations, RSR++ achieved up to a 29x speedup over standard matrix multiplication for large matrices ($n=2^{16}$).
  • Memory Efficiency: The algorithm reduced memory usage by up to 6x, requiring less than 17% of the original storage for the weight indices.
  • LLM Inference (CPU): When integrated into 1.58-bit LLMs, the authors observed up to a 5.24x speedup in token generation time on a standard CPU.
  • GPU Acceleration: Even compared to highly optimized PyTorch kernels, a proof-of-concept GPU implementation of RSR achieved a 2.5x speedup.

Limitations and Future Work

While the theoretical and practical results are highly promising, the authors note a few current limitations:

  • Implementation Level: Currently, the speedups are achieved at the application level (Python/C++). To reach the full potential of this math, it needs to be implemented as low-level CUDA or Triton kernels that can bypass the overhead of high-level libraries.
  • Matrix Size Threshold: The overhead of the algorithm means it is most effective for matrices larger than $2^{10}$. For very small layers, standard methods might still be faster.
  • Hardware Utilization: The current version doesn't yet utilize hardware-specific features like SIMD or specialized bit-manipulation instructions, which could further amplify the speedup.

Future work will focus on deeper hardware integration and extending the segmentation logic to higher-bit quantization (e.g., 4-bit or 8-bit), though managing the exponential growth of unique patterns ($2^q$) remains a significant challenge.

Summary updated

BibTeX
@inproceedings{dehghankar2025efficient,
  author = {Mohsen Dehghankar and Mahdi Erfanian and Abolfazl Asudeh},
  title = {{An Efficient Matrix Multiplication Algorithm for Accelerating Inference in Binary and Ternary Neural Networks}},
  booktitle = {International Conference on Machine Learning},
  year = {2025},
  url = {https://openreview.net/forum?id=Nvf4jFsbv9},
}
Figure