Autograd and the tape
AI assistant
Build this with Claude Code, Cursor or Copilot
Copy a Talario-tuned prompt for Autograd and the tape, grounded in 8 real API signatures , into your IDE's AI. No chatbot, just exact context.
Talario is define-by-run. You build a forward computation in plain PHP and Talario records it onto a tape: a bump-allocated arena of POD nodes in C, where each tensor is an integer id rather than a PHP object. There is no per-op Zval and no operator-object churn on the hot path.
The shape of a step
use Talario\Device; use Talario\Tape; $dev = new Device('gpu'); // 'cpu', 'gpu', or 'auto' $t = new Tape($dev); $x = $t->tensor($features, [$n, $in]); // host -> device tensor id $w = $t->tensor($weights, [$in, $out], true); // requiresGrad = true $y = $t->matmul($x, $w); $y = $t->relu($y); $loss = $t->mseLoss($y, $t->tensor($targets, [$n, $out])); $t->backward($loss); // reverse-mode pass $grad = $t->grad($w); // read back the gradient
backward() walks the recorded tape in reverse, accumulating gradients on every
tensor that was created with requiresGrad = true (and on any bound
Parameter). Forward is lazy and the backward pass is batched, so a training
step resolves to a handful of backend submissions rather than one per op.
Reading values back
value($id) and grad($id) copy a tensor or its gradient back to a PHP array.
These are device-to-host transfers, so call them when you need the numbers (for
logging or a metric), not inside the inner loop.
Device memory
Device buffers are owned by the Zend object handlers of the Tape and
Parameter, so they are released deterministically the moment PHP drops the last
reference. The tape arena itself is freed in one shot when the tape is dropped.