DEV Community

Cover image for Introducing ZMatrix: High-Performance Tensor Operations for PHP
Wesley Teixeira
Wesley Teixeira

Posted on

Introducing ZMatrix: High-Performance Tensor Operations for PHP

Exploring ZMatrix:

High-Performance Tensor Operations in PHP
Tensor operations—working with multidimensional arrays—are a cornerstone of modern computing, powering everything from machine learning to scientific simulations. While languages like Python and C++ dominate this space with libraries like NumPy and Eigen, PHP has historically lagged behind. That’s where ZMatrix, a C++-powered PHP extension, comes in. ZMatrix brings blazing-fast tensor operations directly to PHP, blending the performance of C++ with the simplicity of PHP’s ecosystem.

Inspired by the need for efficient numerical computing in PHP, I created ZMatrix to bridge this gap. Whether you’re building data-driven APIs, prototyping algorithms, or processing large datasets, ZMatrix empowers you to do it all without leaving PHP.

The Foundation of ZMatrix

At its core, ZMatrix is a PHP extension that leverages C++ for high-performance tensor math. It uses zero-copy arrays to map PHP data structures directly to C++ memory, avoiding unnecessary overhead.

Here’s a simple example to get a feel for it:

use ZMatrix\ZTensor;

$t = ZTensor::arr([
    [1, 2, 3],
    [4, 5, 6],
]);
echo "Shape: ", json_encode($t->shape()), "\n"; // [2,3]
Enter fullscreen mode Exit fullscreen mode

This creates a 2x3 tensor from a PHP array, ready for optimized operations—all powered by a C++ backend.
Installing ZMatrix
Getting ZMatrix up and running is straightforward. Here’s how to install it on a typical Linux system with PHP 8.4:

🚀 Installation

git clone https://github.com/webtec3/zmatrix.git
cd zmatrix
phpize
./configure
make -j$(nproc)
sudo make install
echo "extension=zmatrix.so" | sudo tee /etc/php/8.4/cli/conf.d/20-zmatrix.ini
Enter fullscreen mode Exit fullscreen mode

Once installed, you’re ready to start using tensor operations in your PHP scripts.
Working with Tensors in ZMatrix
ZMatrix offers a rich set of features to manipulate tensors efficiently. Here are some examples of what you can do:

Creating a Tensor

use ZMatrix\ZTensor;
$t = ZTensor::arr([
    [1, 2, 3],
    [4, 5, 6],
]);
echo "Shape: ", json_encode($t->shape()), "\n"; // [2,3]
Enter fullscreen mode Exit fullscreen mode

Element-wise Operations

$a = ZTensor::arr([1, 2, 3]);
$b = ZTensor::ones([3])->multiply_scalar(5);
$c = $a->add($b);
echo $c, "\n"; // [6, 7, 8]
Enter fullscreen mode Exit fullscreen mode

Element-wise Arithmetic

$a = ZTensor::arr([1,2,3]);
$b = ZTensor::ones([3])->multiply_scalar(5);
$c = $a->add($b);
echo $c, "\n"; // [[6,7,8]]
Enter fullscreen mode Exit fullscreen mode

Matrix Multiplication

$A = ZTensor::arr([[1, 2, 3], [4, 5, 6]]);
$B = ZTensor::arr([[7, 8], [9, 10], [11, 12]]);
$C = $A->matmul($B);
echo $C, "\n"; // [[58, 64], [139, 154]]
Enter fullscreen mode Exit fullscreen mode

Statistical Computations

$data = ZTensor::random([1000]);
echo "Mean: ", $data->mean(), ", Std: ", $data->std(), "\n";
Enter fullscreen mode Exit fullscreen mode

Activation Functions

$x = ZTensor::arr([-1, 0, 1]);
echo "Sigmoid: ", $x->sigmoid(), "\n"; // [0.2689, 0.5, 0.7311]
echo "ReLU: ", $x->relu(), "\n";     // [0, 0, 1]
Enter fullscreen mode Exit fullscreen mode

Statistical Ops

$data = ZTensor::random([1000]);
echo "Mean: ", $data->mean(), ", Std: ", $data->std(), "\n";
Enter fullscreen mode Exit fullscreen mode

Neural-style Activations

$x = ZTensor::arr([-1,0,1]);
echo "Sigmoid: ", $x->sigmoid(), "\n";
echo "ReLU:     ", $x->relu(),    "\n";
Enter fullscreen mode Exit fullscreen mode

🔍 Under the Hood

These operations are intuitive and mirror the simplicity of PHP, while under the hood, they leverage C++ optimizations like BLAS, OpenMP, and SIMD for maximum performance.
Performance Under the Hood
ZMatrix’s speed comes from its C++ foundation:

BLAS: Accelerates matrix multiplication with cblas_sgemm.
OpenMP + SIMD: Parallelizes operations across CPU cores.
Zero-copy Views: Functions like reshape() and transpose() avoid data duplication.
Float32 Storage: Optimized for numerical efficiency.

Benchmarks show ZMatrix performing hundreds of millions of operations per second, making it a viable choice for real-time and large-scale applications.
Who Can Benefit from ZMatrix?
ZMatrix is versatile and caters to various use cases:

Backend Developers: Add lightweight machine learning or data processing to PHP APIs.
Data Engineers: Build efficient pipelines within PHP microservices.
Educators: Teach computational concepts using PHP’s accessible syntax.

Get Involved

ZMatrix is open source (MIT license) and welcomes contributions. Check it out on GitHub: https://github.com/webtec3/zmatrix. Star the repo, file issues, or submit pull requests to help shape its future!
Conclusion
ZMatrix brings high-performance tensor operations to PHP, combining the ease of PHP with the power of C++. It’s lightweight, efficient, and opens up new possibilities for numerical computing in PHP applications. Give it a try in your next project and see how it can elevate your work!

Top comments (0)