@@ -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,28 +59,37 @@ fromArray method. Below are examples of how to create tensors:
4759### Using the Constructor
4860
4961``` php
50- use Codewithkyrian\Transformers\Utils\Tensor;
51- use Interop\Polite\Math\Matrix\NDArray;
62+ use Codewithkyrian\Transformers\Tensor\Tensor;
5263
5364$data = [1, 2, 3, 4, 5, 6];
5465$shape = [2, 3];
55- $dtype = NDArray ::int16;
66+ $dtype = Tensor ::int16;
5667$tensor = new Tensor($data, $dtype, $shape); // If dtype is not provided, it defaults to NDArray::float32
5768```
5869
5970### Using the fromArray Method
6071
6172``` php
62- use Codewithkyrian\Transformers\Utils \Tensor;
73+ use Codewithkyrian\Transformers\Tensor \Tensor;
6374
6475$data = [[1, 2, 3], [4, 5, 6]];
6576$tensor = Tensor::fromArray($data);
6677```
6778
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+
6889### Using zeros and ones methods
6990
7091``` php
71- use Codewithkyrian\Transformers\Utils \Tensor;
92+ use Codewithkyrian\Transformers\Tensor \Tensor;
7293
7394$shape = [2, 3];
7495$tensor = Tensor::zeros($shape); // Creates a tensor of zeros with the specified shape
@@ -78,7 +99,7 @@ $tensor = Tensor::ones($shape); // Creates a tensor of ones with the specified s
7899### Using zerosLike and onesLike methods
79100
80101``` php
81- use Codewithkyrian\Transformers\Utils \Tensor;
102+ use Codewithkyrian\Transformers\Tensor \Tensor;
82103
83104$data = [[1, 2, 3], [4, 5, 6]];
84105$tensor = Tensor::fromArray($data);
@@ -114,7 +135,7 @@ and buffer.
114135 Returns the tensor's flat buffer as a regular PHP array.
115136
116137``` php
117- use Codewithkyrian\Transformers\Utils \Tensor;
138+ use Codewithkyrian\Transformers\Tensor \Tensor;
118139
119140$data = [[1, 2, 3], [4, 5, 6]];
120141
@@ -281,6 +302,27 @@ matrix multiplication, reshaping, transposing, etc. Below are some common tensor
281302
282303 $softmax->toArray(); // [[0.09003057317038, 0.2447284710548, 0.66524095577482], [0.09003057317038, 0.2447284710548, 0.66524095577482]]
283304 ```
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+ ```
284326
285327- ### ` max(int $axis = null) `
286328 Returns the maximum values along the specified axis.
@@ -438,7 +480,7 @@ matrix multiplication, reshaping, transposing, etc. Below are some common tensor
438480 $result = $tensor->add($tensor2); // [[6, 8], [10, 12]]
439481 ```
440482
441- - ### `multiply(float|int $value)`
483+ - ### `multiply(Tensor| float|int $value)`
442484 Multiplies the tensor by the specified scalar value.
443485
444486 Parameters:
0 commit comments