@@ -39,6 +39,18 @@ Tensors have the following properties:
3939 each dimension. Strides are used to efficiently access elements in a tensor without the need for reshaping the
4040 underlying data.
4141
42+ ## Tensors in TransformersPHP
43+
44+ The ` Tensor ` class in TransformersPHP provides a flexible and efficient way to work with tensors in PHP. By default, it
45+ uses a C-based buffer to store the tensor's data, which allows for fast element-wise operations and mathematical
46+ operations using OpenBLAS. The operations can further be accelerated if you installed OpenMP - allowing for parallel
47+ computation across multiple cores. TransformersPHP selects the best available backend for your system, depending on the
48+ installed libraries. OpenBLAS is already included in the package, so you don't need to install it separately. However,
49+ you can install OpenMP to enable parallel computation. Checkout the OpenMP installation guide for your operating system.
50+
51+ There are few edge cases where OpenBLAS might not be installed properly. In such cases, TransformersPHP will fall back
52+ to using the PHP-based buffer, which is slower but still functional.
53+
4254## Creating a Tensor
4355
4456You can create a tensor using the Tensor class constructor or by converting from a multidimensional array using the
@@ -47,11 +59,11 @@ fromArray method. Below are examples of how to create tensors:
4759### Using the Constructor
4860
4961``` php
50- use Codewithkyrian\Transformers\Tensor\Tensor;use Interop\Polite\Math\Matrix\NDArray;
62+ use Codewithkyrian\Transformers\Tensor\Tensor;
5163
5264$data = [1, 2, 3, 4, 5, 6];
5365$shape = [2, 3];
54- $dtype = NDArray ::int16;
66+ $dtype = Tensor ::int16;
5567$tensor = new Tensor($data, $dtype, $shape); // If dtype is not provided, it defaults to NDArray::float32
5668```
5769
@@ -64,6 +76,16 @@ $data = [[1, 2, 3], [4, 5, 6]];
6476$tensor = Tensor::fromArray($data);
6577```
6678
79+ ### Using fill Method
80+
81+ ``` php
82+ use Codewithkyrian\Transformers\Tensor\Tensor;
83+
84+ $shape = [2, 3];
85+ $value = 5;
86+ $tensor = Tensor::fill($shape, $value); // Creates a tensor filled with the specified value
87+ ```
88+
6789### Using zeros and ones methods
6890
6991``` php
@@ -280,6 +302,27 @@ matrix multiplication, reshaping, transposing, etc. Below are some common tensor
280302
281303 $softmax->toArray(); // [[0.09003057317038, 0.2447284710548, 0.66524095577482], [0.09003057317038, 0.2447284710548, 0.66524095577482]]
282304 ```
305+
306+ - ### ` topk(int $k = null, bool $sorted = true) `
307+ Returns the top k elements and their indices along the specified axis.
308+
309+ Parameters:
310+ - ` $k ` : The number of top elements to return. If not provided, all elements are returned.
311+ - ` $sorted ` : Whether to return the elements in sorted order.
312+
313+ Returns:
314+ - A tuple containing two tensors: the top k elements and their indices.
315+
316+ Example:
317+ ``` php
318+ $data = [[1, 2, 3], [4, 5, 6]];
319+ $tensor = Tensor::fromArray($data);
320+
321+ [$values, $indices] = $tensor->topk(2);
322+
323+ $values->toArray(); // [[3, 2], [6, 5]]
324+ $indices->toArray(); // [[2, 1], [2, 1]]
325+ ```
283326
284327- ### ` max(int $axis = null) `
285328 Returns the maximum values along the specified axis.
@@ -437,7 +480,7 @@ matrix multiplication, reshaping, transposing, etc. Below are some common tensor
437480 $result = $tensor->add($tensor2); // [[6, 8], [10, 12]]
438481 ```
439482
440- - ### `multiply(float|int $value)`
483+ - ### `multiply(Tensor| float|int $value)`
441484 Multiplies the tensor by the specified scalar value.
442485
443486 Parameters:
0 commit comments