Purpose

The Tensor.php file in the PHP PyTorch/NumPy equivalent API serves as a comprehensive layer that abstracts tensor operations from the underlying C-based TensorEngine. It provides functionalities for creating, manipulating, and performing mathematical operations on tensors, facilitating the development of machine learning models using PHP.

Key Components

Classes, Functions, Methods with Signatures

  • Tensor
    • Constructors:
      public function __construct(array $shape = [], int $dtype = self::DTYPE_FLOAT32, ?\FFI\CData $arena = null)
      

      Creates a new tensor or wraps an existing C-engine tensor pointer.

    • Methods:
      • view(): Returns a view of the current tensor without copying data.
      • slice(int $axis, int $start, int $length): Slices the tensor along the specified axis.
      • sliceStep(int $axis, int $start, int $end, int $step): Slices the tensor with a step parameter.
      • row(int $row): Returns a row view of the tensor.
      • col(int $col): Returns a column view of the tensor.
      • fill(float $val): Fills the tensor with a specified value.
      • copy(): Creates a copy of the current tensor.
      • isContiguous(): Checks if the tensor is contiguous in memory.
      • contiguous(): Returns a contiguous copy of the tensor.
      • emptyLike(self $t): Creates an uninitialized tensor with the same shape and dtype as another tensor.
      • zeros(int ...$shape), ones(int ...$shape): Creates tensors filled with zeros or ones respectively.
      • zerosArena(\FFI\CData $arena, int ...$shape), onesArena(\FFI\CData $arena, int ...$shape): Creates zero-filled or one-filled tensors using a specified arena for memory management.
      • range(float $start, float $end, float $step = 1.0), linspace(float $start, float $end, int $steps): Generates linearly spaced values within a range.
      • randomNormal(array $shape, float $mean = 0.0, float $stddev = 1.0, ?\FFI\CData $arena = null), randomUniform(array $shape, float $minVal = 0.0, float $maxVal = 1.0, ?\FFI\CData $arena = null): Generates tensors with random values from normal or uniform distributions.
      • randomChoice(int $n, bool $replace = true), randomPermutation(): Generates random choices and permutations of tensor elements.
      • linear(self $W, ?self $bias = null): Applies a linear transformation to the tensor.
      • addRelu(self $b): Adds another tensor and applies ReLU activation.
      • mulAdd(self $B, self $C): Performs a fused multiply-add operation.
    • Static Methods:
      • configureThreading(int $ompThreads, int $blasThreads = 1): Configures threading for OpenMP and BLAS operations.
      • fusedBceLossAndGrad(self $preds, self $targets, ?self $grads = null): float: Computes binary cross-entropy loss and its gradient.

Properties

  • DTYPE_FLOAT32: Constant representing the floating-point data type for tensors.

Usage Example

// Create a tensor with shape [2, 3] filled with zeros
$zerosTensor = Tensor::zeros(2, 3);

// Create a tensor with shape [2, 3] filled with ones
$onesTensor = Tensor::ones(2, 3);

// Perform matrix multiplication using the linear method
$resultTensor = $zerosTensor->linear($onesTensor);

// Print the result tensor
print_r($resultTensor);

Dependencies

  • FFI: The Foreign Function Interface for interfacing with C libraries.
  • TensorEngine: A C library providing core tensor operations.

Error Handling

The checkError method is used to propagate errors from the underlying C-engine, ensuring that any exceptions or runtime errors are caught and handled appropriately in PHP.