Skip to content

Commit 5c1de50

Browse files
Revamp model internals, unify generation flow, and extend platform support (#89)
This PR delivers a major refactor focused on simplifying the generation pipeline, improving cross-platform native support, extending model capabilities, and enhancing overall maintainability. ### Highlights: - Stopping Criteria: Added `MaxLength`, `MaxTime`, `EosToken` and `Interruptable` criteria for better generation control. - PretrainedConfig: Introduced base config to reduce boilerplate across model files. - Platform Package: Converted to platform-package with dynamic loading support for Linux, macOS, and Windows (x86_64 & ARM64). - Optimized FFI: Cleaned up FFI bindings for better reliability and developer ergonomics. - AutoModel Refactor: Unified resolution logic to prioritize generic models and avoid accidental task-specific selection. - New Model Support: Added Gemma, Qwen3, Phi, and other language models. - Introduced token-per-second (TPS) benchmarking - Improved logits processors and model session handling - Enhanced Audio/Image Utilities: Upgraded constructors and added better error handling. - Refactored `BPEModel` to support new merge format - PSR-3 Logging: Logging support throughout the codebase, now defaults to NullLogger - Added full inference and utility test coverage - Bumped shared library versions -onnxruntime and rindowmatlib - Updated documentation and examples Resolves #88 Resolves #29 No known breaking changes.
2 parents 6609377 + 3b64b88 commit 5c1de50

File tree

257 files changed

+14612
-4272
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

257 files changed

+14612
-4272
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
name: Build Rindow Matlib
2+
3+
on:
4+
workflow_dispatch:
5+
6+
jobs:
7+
build:
8+
name: Build rindow-matlib (${{ matrix.arch }})
9+
strategy:
10+
fail-fast: false
11+
matrix:
12+
include:
13+
- arch: x86_64
14+
runs-on: ubuntu-24.04
15+
artifact_name: linux-x86_64
16+
- arch: arm64
17+
runs-on: ubuntu-24.04-arm
18+
artifact_name: linux-arm64
19+
20+
runs-on: ${{ matrix.runs-on }}
21+
22+
steps:
23+
- name: Checkout code
24+
uses: actions/checkout@v4
25+
with:
26+
repository: rindow/rindow-matlib
27+
28+
- name: Install dependencies
29+
run: |
30+
sudo apt-get update
31+
sudo apt-get install -y build-essential cmake
32+
33+
- name: Build
34+
run: |
35+
cmake -S . -B build
36+
cmake --build build --config Release
37+
38+
- name: Run tests
39+
run: |
40+
cd build
41+
ctest -C Release
42+
43+
- name: Package
44+
run: |
45+
cd build
46+
cpack -G TGZ -C Release
47+
48+
- name: Upload package artifact
49+
uses: actions/upload-artifact@v4
50+
with:
51+
name: ${{ matrix.artifact_name }}
52+
path: packages/*

.github/workflows/release.yml

Lines changed: 0 additions & 41 deletions
This file was deleted.

.github/workflows/tests.yml

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
name: Tests
2+
3+
on: ['push', 'pull_request']
4+
5+
jobs:
6+
ci:
7+
runs-on: ${{ matrix.os }}
8+
9+
strategy:
10+
fail-fast: false
11+
matrix:
12+
os: [ubuntu-latest, macos-latest, windows-latest]
13+
php: [8.1, 8.2, 8.3, 8.4]
14+
max-parallel: 4
15+
16+
name: Tests PHP${{ matrix.php }} - ${{ matrix.os }}
17+
18+
steps:
19+
20+
- name: Checkout
21+
uses: actions/checkout@v4
22+
23+
- name: Cache dependencies
24+
uses: actions/cache@v4
25+
with:
26+
path: ~/.composer/cache/files
27+
key: dependencies-php-${{ matrix.php }}-composer-${{ hashFiles('composer.json') }}
28+
29+
- name: Setup PHP
30+
uses: shivammathur/setup-php@v2
31+
with:
32+
php-version: ${{ matrix.php }}
33+
extensions: dom, mbstring, zip, ffi
34+
coverage: none
35+
36+
- name: Install Composer dependencies
37+
run: composer update --no-interaction --prefer-dist
38+
39+
- name: Run Tests
40+
run: composer test

README.md

Lines changed: 19 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -11,26 +11,18 @@
1111
<a href="https://packagist.org/packages/codewithkyrian/transformers"><img src="https://img.shields.io/packagist/v/codewithkyrian/transformers" alt="Latest Stable Version"></a>
1212
<a href="https://github.com/CodeWithKyrian/transformers-php/blob/main/LICENSE"><img src="https://img.shields.io/github/license/codewithkyrian/transformers-php" alt="License"></a>
1313
<a href="https://github.com/codewithkyrian/transformers-php"><img src="https://img.shields.io/github/repo-size/codewithkyrian/transformers-php" alt="Documentation"></a>
14+
<a href="https://github.com/CodeWithKyrian/transformers-php/actions/workflows/tests.yml"><img src="https://github.com/CodeWithKyrian/transformers-php/actions/workflows/tests.yml/badge.svg" alt="Tests"></a>
1415
</p>
1516

16-
TransformersPHP is designed to be functionally equivalent to the Python library, while still maintaining the same level
17-
of performance and ease of use. This library is built on top of the Hugging Face's Transformers library, which provides
18-
thousands of pre-trained models in 100+ languages. It is designed to be a simple and easy-to-use library for PHP
19-
developers using a similar API to the Python library. These models can be used for a variety of tasks, including text
20-
generation, summarization, translation, and more.
17+
TransformersPHP is designed to be functionally equivalent to the Python library, while still maintaining the same level of performance and ease of use. This library is built on top of the Hugging Face's Transformers library, which provides thousands of pre-trained models in 100+ languages. It is designed to be a simple and easy-to-use library for PHP developers using a similar API to the Python library. These models can be used for a variety of tasks, including text generation, summarization, translation, and more.
2118

22-
TransformersPHP uses [ONNX Runtime](https://onnxruntime.ai/) to run the models, which is a high-performance scoring
23-
engine for Open Neural Network Exchange (ONNX) models. You can easily convert any PyTorch or TensorFlow model to ONNX
24-
and use it with TransformersPHP using [🤗 Optimum](https://github.com/huggingface/optimum#onnx--onnx-runtime).
19+
TransformersPHP uses [ONNX Runtime](https://onnxruntime.ai/) to run the models, which is a high-performance scoring engine for Open Neural Network Exchange (ONNX) models. You can easily convert any PyTorch or TensorFlow model to ONNX and use it with TransformersPHP using [🤗 Optimum](https://github.com/huggingface/optimum#onnx--onnx-runtime).
2520

26-
TO learn more about the library and how it works, head over to
27-
our [extensive documentation](https://codewithkyrian.github.io/transformers-php/introduction).
21+
TO learn more about the library and how it works, head over to our [extensive documentation](https://codewithkyrian.github.io/transformers-php/introduction).
2822

2923
## Quick tour
3024

31-
Because TransformersPHP is designed to be functionally equivalent to the Python library, it's super easy to learn from
32-
existing Python or Javascript code. We provide the `pipeline` API, which is a high-level, easy-to-use API that groups
33-
together a model with its necessary preprocessing and postprocessing steps.
25+
Because TransformersPHP is designed to be functionally equivalent to the Python library, it's super easy to learn from existing Python or Javascript code. We provide the `pipeline` API, which is a high-level, easy-to-use API that groups together a model with its necessary preprocessing and postprocessing steps.
3426

3527
<table>
3628
<tr>
@@ -104,15 +96,11 @@ composer require codewithkyrian/transformers
10496
```
10597

10698
> [!CAUTION]
107-
> The ONNX library is platform-specific, so it's important to run the composer require command on the target platform
108-
> where the code will be executed. In most cases, this will be your development machine or a server where you deploy
109-
> your application, but if you're using a Docker container, run the `composer require` command inside that container.
99+
> The ONNX library is platform-specific, so it's important to run the composer require command on the target platform where the code will be executed. In most cases, this will be your development machine or a server where you deploy your application, but if you're using a Docker container, run the `composer require` command inside that container.
110100
111101
## PHP FFI Extension
112102

113-
TransformersPHP uses the PHP FFI extension to interact with the ONNX runtime. The FFI extension is included by default
114-
in PHP 7.4 and later, but it may not be enabled by default. If the FFI extension is not enabled, you can enable it by
115-
uncommenting(remove the `;` from the beginning of the line) the
103+
TransformersPHP uses the PHP FFI extension to interact with the ONNX runtime. The FFI extension is included by default in PHP 7.4 and later, but it may not be enabled by default. If the FFI extension is not enabled, you can enable it by uncommenting(remove the `;` from the beginning of the line) the
116104
following line in your `php.ini` file:
117105

118106
```ini
@@ -129,14 +117,11 @@ After making these changes, restart your web server or PHP-FPM service, and you
129117

130118
## Documentation
131119

132-
For more detailed information on how to use the library, check out the
133-
documentation : [https://codewithkyrian.github.io/transformers-php](https://codewithkyrian.github.io/transformers-php)
120+
For more detailed information on how to use the library, check out the documentation : [https://codewithkyrian.github.io/transformers-php](https://codewithkyrian.github.io/transformers-php)
134121

135122
## Usage
136123

137-
By default, TransformersPHP uses hosted pretrained ONNX models. For supported tasks, models that have been converted to
138-
work with [Xenova's Transformers.js](https://huggingface.co/models?library=transformers.js) on HuggingFace should work
139-
out of the box with TransformersPHP.
124+
By default, TransformersPHP uses hosted pretrained ONNX models. For supported tasks, models that have been converted to work with [Xenova's Transformers.js](https://huggingface.co/models?library=transformers.js) on HuggingFace should work out of the box with TransformersPHP.
140125

141126
## Configuration
142127

@@ -151,27 +136,25 @@ Transformers::setup()
151136
->setRemotePathTemplate('...') // Set the remote path template for downloading models. Defaults to `{model}/resolve/{revision}/{file}`
152137
->setAuthToken('...') // Set the auth token for downloading models. Defaults to `null`
153138
->setUserAgent('...') // Set the user agent for downloading models. Defaults to `transformers-php/{version}`
154-
->setImageDriver('...') // Set the image driver for processing images. Defaults to `IMAGICK'
155-
->setLogger('...'); // Set the logger for TransformersPHP. Defaults to `null`
139+
->setImageDriver('...') // Set the image driver for processing images. Defaults to `VIPS`
140+
->setLogger($logger) // Set a PSR-3 compatible logger. Defaults to `NullLogger` if not set
141+
->apply(); // Apply the configuration
156142
```
157143

158-
You can call the `set` methods in any order, or leave any out entirely, in which case, it uses the default values. For
159-
more information on the configuration options and what they mean, checkout
144+
You can call the `set` methods in any order, or leave any out entirely, in which case, it uses the default values. For more information on the configuration options and what they mean, checkout
160145
the [documentation](https://codewithkyrian.github.io/transformers-php/configuration).
161146

162147
## Convert your models to ONNX
163148

164-
TransformersPHP only works with ONNX models, therefore, you must convert your PyTorch, TensorFlow or JAX models to
165-
ONNX. It is recommended to use [🤗 Optimum](https://huggingface.co/docs/optimum) to perform the conversion and
166-
quantization of your model.
149+
TransformersPHP only works with ONNX models, therefore, you must convert your PyTorch, TensorFlow or JAX models to ONNX. We recommend using the [conversion script](https://github.com/huggingface/transformers.js/blob/main/scripts/convert.py) from Transformers.js, which uses the [🤗 Optimum](https://huggingface.co/docs/optimum) behind the scenes to perform the conversion and quantization of your model.
150+
151+
```
152+
python -m convert --quantize --model_id <model_name_or_path>
153+
```
167154

168155
## Pre-Download Models
169156

170-
By default, TransformersPHP automatically retrieves model weights (ONNX format) from the Hugging Face model hub when
171-
you first use a pipeline or pretrained model. This can lead to a slight delay during the initial use. To improve the
172-
user experience, it's recommended to pre-download the models you intend to use before running them in your PHP
173-
application, especially for larger models. One way to do that is run the request once manually, but TransformersPHP
174-
also comes with a command line tool to help you do just that:
157+
By default, TransformersPHP automatically retrieves model weights (ONNX format) from the Hugging Face model hub when you first use a pipeline or pretrained model. This can lead to a slight delay during the initial use. To improve the user experience, it's recommended to pre-download the models you intend to use before running them in your PHP application, especially for larger models. One way to do that is run the request once manually, but TransformersPHP also comes with a command line tool to help you do just that:
175158

176159
```bash
177160
./vendor/bin/transformers download <model_identifier> [<task>] [options]

VERSION

Lines changed: 0 additions & 1 deletion
This file was deleted.

bin/transformers

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,21 @@
11
#!/usr/bin/env php
2-
<?php declare(strict_types=1);
2+
<?php
3+
4+
declare(strict_types=1);
35

46
use Symfony\Component\Console\Application;
57

68
require_once $_composer_autoload_path ?? __DIR__ . '/../vendor/autoload.php';
79

810
$application = new Application();
911

10-
// ... register commands
11-
1212
try {
1313
$application->setName('Transformers PHP CLI');
1414

15-
$application->add(new Codewithkyrian\Transformers\Commands\InstallCommand());
16-
$application->add(new Codewithkyrian\Transformers\Commands\DownloadModelCommand());
15+
$application->addCommand(new Codewithkyrian\Transformers\Commands\DownloadModelCommand());
1716

1817
$application->run();
1918
} catch (Exception $e) {
2019
echo $e->getMessage();
2120
exit(1);
22-
}
21+
}

composer.json

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,28 +11,28 @@
1111
"php",
1212
"codewithkyrian"
1313
],
14-
"type": "library",
14+
"type": "platform-package",
1515
"require": {
1616
"php": "^8.1",
1717
"ext-ffi": "*",
18-
"psr/log": "^1.1.3|^2.0|^3.0",
19-
"codewithkyrian/jinja-php": "^1.0",
20-
"codewithkyrian/transformers-libsloader": "^2.0",
18+
"codewithkyrian/jinja-php": "^2.0",
19+
"codewithkyrian/platform-package-installer": "^1.0",
2120
"imagine/imagine": "^1.3",
22-
"rindow/rindow-math-matrix": "^2.0",
23-
"rindow/rindow-matlib-ffi": "^1.0",
21+
"psr/log": "^1.1 || ^2.0 || ^3.0",
22+
"rindow/rindow-math-matrix": "^2.1",
23+
"rindow/rindow-matlib-ffi": "^1.1",
2424
"rindow/rindow-openblas-ffi": "^1.0",
25-
"symfony/console": "^6.4|^7.0",
26-
"rokka/imagine-vips": "^0.31.0"
25+
"rokka/imagine-vips": "^0.40",
26+
"symfony/console": "^6.4|^7.0"
2727
},
2828
"require-dev": {
29-
"pestphp/pest": "^2.31",
30-
"symfony/var-dumper": "^7.0"
29+
"ffi/var-dumper": "^1.0",
30+
"pestphp/pest": "^2.36.0|^3.5.0",
31+
"symfony/var-dumper": "^6.4.11|^7.1.5"
3132
},
3233
"suggest": {
3334
"ext-imagick": "Required to use the Imagick Driver for image processing",
34-
"ext-gd": "Required to use the GD Driver for image processing",
35-
"rokka/imagine-vips": "Required to use the VIPS Driver for image processing"
35+
"ext-gd": "Required to use the GD Driver for image processing"
3636
},
3737
"license": "Apache-2.0",
3838
"autoload": {
@@ -51,23 +51,31 @@
5151
}
5252
],
5353
"config": {
54+
"sort-packages": true,
5455
"allow-plugins": {
5556
"pestphp/pest-plugin": true,
56-
"codewithkyrian/transformers-libsloader": true
57+
"codewithkyrian/platform-package-installer": true
5758
}
5859
},
5960
"bin": [
6061
"bin/transformers"
6162
],
6263
"scripts": {
6364
"test": "vendor/bin/pest",
64-
"test:coverage": "XDEBUG_MODE=coverage ./vendor/bin/pest --coverage",
65-
"post-install-cmd": "Codewithkyrian\\Transformers\\Utils\\LibsChecker::check",
66-
"post-update-cmd": "Codewithkyrian\\Transformers\\Utils\\LibsChecker::check"
65+
"test:coverage": "XDEBUG_MODE=coverage ./vendor/bin/pest --coverage"
6766
},
6867
"archive": {
6968
"exclude": [
7069
"/docs"
7170
]
71+
},
72+
"extra": {
73+
"platform-urls": {
74+
"linux-x86_64": "https://github.com/codewithkyrian/transformers-php/releases/download/{version}/dist-linux-x86_64.tar.gz",
75+
"linux-arm64": "https://github.com/codewithkyrian/transformers-php/releases/download/{version}/dist-linux-arm64.tar.gz",
76+
"darwin-x86_64": "https://github.com/codewithkyrian/transformers-php/releases/download/{version}/dist-darwin-x86_64.tar.gz",
77+
"darwin-arm64": "https://github.com/codewithkyrian/transformers-php/releases/download/{version}/dist-darwin-arm64.tar.gz",
78+
"windows-x86_64": "https://github.com/codewithkyrian/transformers-php/releases/download/{version}/dist-windows-x86_64.zip"
79+
}
7280
}
73-
}
81+
}

0 commit comments

Comments
 (0)