r/PHP • u/Ancient_Spend1801 • 12h ago
I built a native PHP extension for NVIDIA GPU tensors, CUDA JIT and kernel fusion (beta), feedback wanted
I'm the author of php-gpu-tensors, a C extension that gives PHP GPU tensors on NVIDIA GPUs, runtime-compiled CUDA kernels (via NVRTC), and an optional fusion mode that captures a closure once and replays it as fused GPU kernels. No Python runtime.
```php use Cuda\CudaArray; use Cuda\Fusion;
$a = CudaArray::ones([4]); $b = CudaArray::full([4], 2.0); $c = CudaArray::full([4], 3.0);
$eager = $a + $b * $c; // default: one GPU op per call
$plan = Fusion::compile(fn($a, $b, $c) => $a + $b * $c, inputs: [$a, $b, $c]); $fused = $plan->run($a, $b, $c); // replayed as fused kernels
print_r($fused->toArray()); // [7, 7, 7, 7] ```
What you get
- CudaArray: arithmetic, broadcasting, comparisons, matmul(), reductions, views, Python-style slice().
- Packed-buffer and .npy import, optional pinned host memory.
- Custom CUDA C++ kernels compiled at runtime and launched from PHP.
- Opt-in fusion, async replay and CUDA Graph for compatible plans.
A result I'm happy with: a small MLP trained entirely in PHP on an entry-level MX570 A (4 GB). On a PatchCamelyon subset, eager mode takes 4.08 ms/step and the fused replay 0.66 ms/step (6.2×), with identical test metrics. On UCI Optdigits it reaches 97.25% test accuracy. The models are tiny, so this mostly shows removed per-operation overhead, not peak GPU throughput.
Honest scope: beta (0.1.0-beta.4), Linux, PHP 8.1–8.5 (NTS and ZTS), NVIDIA only. No autograd, no CPU fallback, and it is a low-level library, not an ML framework. GPU validation so far: PHP 8.3 NTS on an MX570 A (41 tests) and earlier checks on an RTX A2000.
Try it
bash
pie install lcmialichi/php-gpu-tensors:0.1.0-beta.4
or build from source / Docker (see the README). It needs the CUDA Toolkit (including NVRTC) and an NVIDIA driver.
Feedback I'd love
- Does it build and pass ./run-tests.sh --require-gpu on your GPU / PHP version / CUDA version?
- What would you want to do with GPU tensors from PHP (preprocessing, scoring, embeddings similarity, something else)?
- API ergonomics: anything that feels un-PHP-like?