Boost PHP Performance with High‑Speed Tensor Computing Using PHP‑ORT
PHP‑ORT is a high‑performance PHP extension that brings SIMD‑accelerated tensor operations and optional ONNX Runtime integration to PHP, offering multi‑core parallelism, extensive type support, and memory‑efficient processing for machine‑learning, scientific, and data‑intensive applications.
Introduction
PHP‑ORT is a PHP extension that provides high‑performance tensor computation. The source code is hosted at https://github.com/krakjoe/ort. It can optionally use Microsoft ONNX Runtime for model inference.
What is PHP‑ORT?
The extension implements multi‑dimensional array (tensor) operations with SIMD (AVX2, SSE4.1, SSE2) and multi‑core parallelism. It supports element‑wise math, linear‑algebra functions, reduction operations, and nine data types with automatic type promotion.
Key Features
SIMD acceleration : AVX2, SSE4.1, SSE2 can yield up to 8× speedup.
Multi‑core parallelism : workloads are automatically distributed across CPU cores.
Comprehensive type support : nine numeric types with automatic promotion.
Memory efficiency : reference‑counted tensors and zero‑copy slicing minimise overhead.
ONNX integration : optional support for Microsoft ONNX Runtime enables fast model loading and inference.
Installation
Requirements: PHP 7.4+ or 8.0+, GCC 4.8+ or Clang 3.8+, and a CPU supporting at least SSE2 (AVX2 recommended).
Basic build:
phpize
./configure --enable-ort
make
sudo make installEnable the extension by adding to php.ini: extension=ort.so Optional ONNX Runtime integration (example with v1.22.0):
wget https://github.com/microsoft/onnxruntime/releases/download/v1.22.0/onnxruntime-linux-x64-1.22.0.tgz
tar -C /usr/local -xvf onnxruntime-linux-x64-1.22.0.tgz --strip-components=1
phpize
./configure --with-ort-onnx=/usr/local --enable-ort
make
sudo make installNote: Use the official Microsoft ONNX Runtime release to avoid compatibility issues.
Basic Usage
Example creating two 1000×1000 float tensors and performing common operations:
use ORT\Tensor\Transient;
use ORT\Math;
// $matrix_data should be a flat float array of length 1,000,000
$a = new Transient([1000, 1000], $matrix_data, ORT\Tensor::FLOAT);
$b = new Transient([1000, 1000], $matrix_data, ORT\Tensor::FLOAT);
// Matrix multiplication
$result = Math\matmul($a, $b);
// Element‑wise addition and scalar multiplication
$sum = Math\add($a, $b);
$scaled = Math\multiply($a, 2.5);
// Reductions
$total = Math\reduce\tensor\sum($a); // sum of all elements
$rowSums = Math\reduce\axis\sum($a, 1); // sum along axis 1Performance Diagnostics
Check the active backend and detected core count:
echo "Backend: " . (ORT\Math\backend() ?: "scalar") . "
";
echo "Cores: " . ORT\Math\cores() . "
";Configuration
Environment variable ORT_SCALE_CORES controls the thread‑pool size (default equals the number of CPU cores). Build options: --enable-ort-backend (default) – enable SIMD backend. --disable-ort-avx2 or --disable-ort-sse41 – fall back to lower‑level SIMD. --enable-ort-neon – enable NEON support on ARMv8. --with-ort-onnx=PATH – link against ONNX Runtime located at PATH.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Open Source Tech Hub
Sharing cutting-edge internet technologies and practical AI resources.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
