Skip to content

Commit 627c6c3

Browse files
Update docs
1 parent 0037528 commit 627c6c3

5 files changed

Lines changed: 96 additions & 14 deletions

File tree

docs/configuration.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ The cache directory is where TransformersPHP stores the downloaded ONNX models.
3737
the `.transformers-cache/models` directory from the root of your project. Please ensure this directory is writable by
3838
your application.
3939

40+
### `setLibsDir(?string $libsDir)`
41+
42+
The libs directory is where TransformersPHP stores the shared libraries required for running the models. You typically
43+
don't need to change this setting unless you have a specific requirement to store the libraries in a different location.
44+
4045
### `setRemoteHost(string $remoteHost)`
4146

4247
The remote host defines where TransformersPHP looks to download model files. The default host

docs/getting-started.md

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,18 @@ You can install the library via Composer. This is the recommended way to install
2222
composer require codewithkyrian/transformers
2323
```
2424

25-
ONNX runtime will be installed automatically as well. For Windows users, it may take more time to install the ONNX
26-
library compared to Linux or macOS users (no shades 😅).
25+
All the shared libraries required by TransformersPHP will be installed automatically. If it fails for some reason, you
26+
can install them manually using the following command:
27+
28+
```bash
29+
./vendor/bin/transformers install
30+
```
2731

2832
> [!CAUTION]
29-
> The ONNX library is platform-specific, so it's important to run the composer require command on the target platform
30-
> where the code will be executed. In most cases, this will be your development machine or a server where you deploy
31-
> your
32-
> application, but if you're using a Docker container, run the `composer require` command inside that container.
33+
> The ONNX library is platform-specific, so it's important to run the `composer require`, or `transformers install`
34+
> command on the target platform where the code will be executed. In most cases, this will be your development machine
35+
> or a server where you deploy your application, but if you're using a Docker container, run the `composer require`
36+
> command inside that container.
3337
3438
This command sets up everything you need to start using pre-trained ONNX models with TransformersPHP.
3539

docs/utils/image.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,14 @@ Transformers::setup()
3636
->apply();
3737
```
3838

39+
If you're using the `Image` class directly, you can set the image driver using the `setImageDriver()` method.
40+
41+
```php
42+
use Codewithkyrian\Transformers\Utils\Image;
43+
44+
Image::setDriver(ImageDriver::GD);
45+
```
46+
3947
## Image Processing Operations
4048

4149
The `Image` utility class provides a range of image processing operations that can be performed on images. These
@@ -101,6 +109,23 @@ operations include:
101109
```php
102110
$resizedImage = $image->resize(300, 200);
103111
```
112+
113+
- ### `thumbnail(int $width, int $height, int|Resample $resample = 2)`
114+
Creates a thumbnail of the image with the specified width and height, using the specified resampling method. This
115+
operation affects the instance it's called on, but still returns that instance for method chaining.
116+
117+
Parameters:
118+
- `$width` (int) The target width of the thumbnail.
119+
- `$height` (int) The target height of the thumbnail.
120+
- `$resample` (int|Resample) The resampling method to use. Default is `Resample::BICUBIC`.
121+
122+
Returns:
123+
- An image object representing the thumbnail.
124+
125+
Example:
126+
```php
127+
$thumbnailImage = $image->thumbnail(100, 100);
128+
```
104129

105130
- ### `crop(int $xMin, int $yMin, int $xMax, int $yMax)`
106131
Crops the image to the specified bounding box defined by the top-left and bottom-right coordinates. This operation

docs/utils/tensor.md

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -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

4456
You 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:

src/Tensor/Tensor.php

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,11 @@ public function toArray()
404404
return $this->unflattenArray($this->buffer, $idx, $this->shape);
405405
}
406406

407+
public function toString(): string
408+
{
409+
return $this->buffer->dump();
410+
}
411+
407412
/**
408413
* Convert the tensor into a flat array of the buffer contents.
409414
*/
@@ -629,18 +634,18 @@ public function sigmoid(): self
629634
/**
630635
* Return a new Tensor with every element multiplied by a constant.
631636
*
632-
* @param float|int $scalar The constant to multiply by.
637+
* @param float|int $value The constant to multiply by.
633638
*
634639
* @return self
635640
*/
636-
public function multiply(Tensor|float|int $scalar): self
641+
public function multiply(Tensor|float|int $value): self
637642
{
638643
$mo = self::mo();
639644

640-
if ($scalar instanceof Tensor) {
641-
$ndArray = $mo->la()->multiply($this, $scalar);
645+
if ($value instanceof Tensor) {
646+
$ndArray = $mo->la()->multiply($this, $value);
642647
} else {
643-
$ndArray = $mo->la()->scal($scalar, $this);
648+
$ndArray = $mo->la()->scal($value, $this);
644649
}
645650

646651
return new static($ndArray->buffer(), $ndArray->dtype(), $ndArray->shape(), $ndArray->offset());

0 commit comments

Comments
 (0)