Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
813bc4d
feat: refactor model generation for simplicity and efficiency
CodeWithKyrian Nov 20, 2024
03df97a
feat: new PretrainedConfig reducing code repetition across model files
CodeWithKyrian Nov 23, 2024
cbfd6e1
feat: Update exampledx
CodeWithKyrian Mar 9, 2025
3f3db2b
feat: Revamp library architecture and platform support
CodeWithKyrian Mar 11, 2025
a7dbad6
refactor: Simplify library components and improve error handling
CodeWithKyrian Mar 11, 2025
e01a1c3
refactor(models, ffi): Improve PretrainedModel structure, generation …
CodeWithKyrian Apr 29, 2025
8ce4c9c
chore: Update README and getting-started documentation for clarity an…
CodeWithKyrian Apr 29, 2025
e7a91cf
feat: Bump shared libraries versions, onnxruntime -> 1.21.0, rindowma…
CodeWithKyrian Apr 29, 2025
d39a23f
fix: depedency version compatibility for PHP 8.1
CodeWithKyrian Apr 29, 2025
4a01a49
chore: Update tests for improved clarity and functionality
CodeWithKyrian Apr 29, 2025
fb57fca
fix(tests): Correct path joining in HubTest to use DIRECTORY_SEPARATO…
CodeWithKyrian Apr 29, 2025
d63ad09
feat: Enhance image processing methods and improve Vips integration
CodeWithKyrian Jul 17, 2025
5e479da
feat: streamline generation config merging
CodeWithKyrian Jul 17, 2025
38b8c5f
feat: Remove redundant install command
CodeWithKyrian Jul 17, 2025
e89b340
feat: code style improvements across board
CodeWithKyrian Jul 17, 2025
9e2ee6b
test: add comprehensive tests Inference session
CodeWithKyrian Jul 17, 2025
875ba04
tests: add comprehensive tests for Image utility
CodeWithKyrian Jul 17, 2025
57d019e
refactor: Move AutoConfig and PretrainedConfig to appropriate directo…
CodeWithKyrian Jul 17, 2025
024ed11
refactor: Improve tokenizer type detection logic when not specified.
CodeWithKyrian Jul 17, 2025
bf080f7
feat: Add support for `eos` and `last_token` pooling in FeatureExtrac…
CodeWithKyrian Jul 17, 2025
0ec177a
feat: add PSR-3 logging support
CodeWithKyrian Jul 18, 2025
688ef6c
refactor: rename PretrainedMixin to AutoModelBase
CodeWithKyrian Jul 18, 2025
e5e4a43
refactor: unify model session handling and clean up feature extractors
CodeWithKyrian Jul 18, 2025
a1da846
feat: extend PretrainedConfig with additional model support
CodeWithKyrian Jul 18, 2025
9882719
refactor: rename model class mappings for consistency and clarity
CodeWithKyrian Jul 18, 2025
894bc90
feat: add support for new models and update text generation pipeline
CodeWithKyrian Jul 19, 2025
7f48433
feat: enhance BPEModel to support new merge format and improve ranks …
CodeWithKyrian Jul 21, 2025
f523672
refactor: reorder model resolution in AutoModel to prioritize generic…
CodeWithKyrian Jul 21, 2025
5c90090
refactor: improve auto-model resolution for task
CodeWithKyrian Jul 21, 2025
648e1b4
chore: update rindow matlib binary version from 1.1.0 to 1.1.1
CodeWithKyrian Jul 21, 2025
da188aa
fix: explicitly check for null when adding BPE node to queue
CodeWithKyrian Jul 21, 2025
c295dca
chore: update jinja-php version from 1.0 to 2.0
CodeWithKyrian Jul 21, 2025
3b64b88
Merge branch 'main' into model-revamp
CodeWithKyrian Jul 21, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions .github/workflows/build-rindow-matlib.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
name: Build Rindow Matlib

on:
workflow_dispatch:

jobs:
build:
name: Build rindow-matlib (${{ matrix.arch }})
strategy:
fail-fast: false
matrix:
include:
- arch: x86_64
runs-on: ubuntu-24.04
artifact_name: linux-x86_64
- arch: arm64
runs-on: ubuntu-24.04-arm
artifact_name: linux-arm64

runs-on: ${{ matrix.runs-on }}

steps:
- name: Checkout code
uses: actions/checkout@v4
with:
repository: rindow/rindow-matlib

- name: Install dependencies
run: |
sudo apt-get update
sudo apt-get install -y build-essential cmake

- name: Build
run: |
cmake -S . -B build
cmake --build build --config Release

- name: Run tests
run: |
cd build
ctest -C Release

- name: Package
run: |
cd build
cpack -G TGZ -C Release

- name: Upload package artifact
uses: actions/upload-artifact@v4
with:
name: ${{ matrix.artifact_name }}
path: packages/*
41 changes: 0 additions & 41 deletions .github/workflows/release.yml

This file was deleted.

40 changes: 40 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
name: Tests

on: ['push', 'pull_request']

jobs:
ci:
runs-on: ${{ matrix.os }}

strategy:
fail-fast: false
matrix:
os: [ubuntu-latest, macos-latest, windows-latest]
php: [8.1, 8.2, 8.3, 8.4]
max-parallel: 4

name: Tests PHP${{ matrix.php }} - ${{ matrix.os }}

steps:

- name: Checkout
uses: actions/checkout@v4

- name: Cache dependencies
uses: actions/cache@v4
with:
path: ~/.composer/cache/files
key: dependencies-php-${{ matrix.php }}-composer-${{ hashFiles('composer.json') }}

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
extensions: dom, mbstring, zip, ffi
coverage: none

- name: Install Composer dependencies
run: composer update --no-interaction --prefer-dist

- name: Run Tests
run: composer test
55 changes: 19 additions & 36 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,18 @@
<a href="https://packagist.org/packages/codewithkyrian/transformers"><img src="https://img.shields.io/packagist/v/codewithkyrian/transformers" alt="Latest Stable Version"></a>
<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>
<a href="https://github.com/codewithkyrian/transformers-php"><img src="https://img.shields.io/github/repo-size/codewithkyrian/transformers-php" alt="Documentation"></a>
<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>
</p>

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.
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.

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).
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).

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

## Quick tour

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.
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.

<table>
<tr>
Expand Down Expand Up @@ -104,15 +96,11 @@ composer require codewithkyrian/transformers
```

> [!CAUTION]
> 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.
> 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.

## PHP FFI Extension

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
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
following line in your `php.ini` file:

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

## Documentation

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)
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)

## Usage

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.
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.

## Configuration

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

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
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
the [documentation](https://codewithkyrian.github.io/transformers-php/configuration).

## Convert your models to ONNX

TransformersPHP only works with ONNX models, therefore, you must convert your PyTorch, TensorFlow or JAX models to
ONNX. It is recommended to use [🤗 Optimum](https://huggingface.co/docs/optimum) to perform the conversion and
quantization of your model.
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.

```
python -m convert --quantize --model_id <model_name_or_path>
```

## Pre-Download Models

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

```bash
./vendor/bin/transformers download <model_identifier> [<task>] [options]
Expand Down
1 change: 0 additions & 1 deletion VERSION

This file was deleted.

11 changes: 5 additions & 6 deletions bin/transformers
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
#!/usr/bin/env php
<?php declare(strict_types=1);
<?php

declare(strict_types=1);

use Symfony\Component\Console\Application;

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

$application = new Application();

// ... register commands

try {
$application->setName('Transformers PHP CLI');

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

$application->run();
} catch (Exception $e) {
echo $e->getMessage();
exit(1);
}
}
42 changes: 25 additions & 17 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,28 @@
"php",
"codewithkyrian"
],
"type": "library",
"type": "platform-package",
"require": {
"php": "^8.1",
"ext-ffi": "*",
"psr/log": "^1.1.3|^2.0|^3.0",
"codewithkyrian/jinja-php": "^1.0",
"codewithkyrian/transformers-libsloader": "^2.0",
"codewithkyrian/jinja-php": "^2.0",
"codewithkyrian/platform-package-installer": "^1.0",
"imagine/imagine": "^1.3",
"rindow/rindow-math-matrix": "^2.0",
"rindow/rindow-matlib-ffi": "^1.0",
"psr/log": "^1.1 || ^2.0 || ^3.0",
"rindow/rindow-math-matrix": "^2.1",
"rindow/rindow-matlib-ffi": "^1.1",
"rindow/rindow-openblas-ffi": "^1.0",
"symfony/console": "^6.4|^7.0",
"rokka/imagine-vips": "^0.31.0"
"rokka/imagine-vips": "^0.40",
"symfony/console": "^6.4|^7.0"
},
"require-dev": {
"pestphp/pest": "^2.31",
"symfony/var-dumper": "^7.0"
"ffi/var-dumper": "^1.0",
"pestphp/pest": "^2.36.0|^3.5.0",
"symfony/var-dumper": "^6.4.11|^7.1.5"
},
"suggest": {
"ext-imagick": "Required to use the Imagick Driver for image processing",
"ext-gd": "Required to use the GD Driver for image processing",
"rokka/imagine-vips": "Required to use the VIPS Driver for image processing"
"ext-gd": "Required to use the GD Driver for image processing"
},
"license": "Apache-2.0",
"autoload": {
Expand All @@ -51,23 +51,31 @@
}
],
"config": {
"sort-packages": true,
"allow-plugins": {
"pestphp/pest-plugin": true,
"codewithkyrian/transformers-libsloader": true
"codewithkyrian/platform-package-installer": true
}
},
"bin": [
"bin/transformers"
],
"scripts": {
"test": "vendor/bin/pest",
"test:coverage": "XDEBUG_MODE=coverage ./vendor/bin/pest --coverage",
"post-install-cmd": "Codewithkyrian\\Transformers\\Utils\\LibsChecker::check",
"post-update-cmd": "Codewithkyrian\\Transformers\\Utils\\LibsChecker::check"
"test:coverage": "XDEBUG_MODE=coverage ./vendor/bin/pest --coverage"
},
"archive": {
"exclude": [
"/docs"
]
},
"extra": {
"platform-urls": {
"linux-x86_64": "https://github.com/codewithkyrian/transformers-php/releases/download/{version}/dist-linux-x86_64.tar.gz",
"linux-arm64": "https://github.com/codewithkyrian/transformers-php/releases/download/{version}/dist-linux-arm64.tar.gz",
"darwin-x86_64": "https://github.com/codewithkyrian/transformers-php/releases/download/{version}/dist-darwin-x86_64.tar.gz",
"darwin-arm64": "https://github.com/codewithkyrian/transformers-php/releases/download/{version}/dist-darwin-arm64.tar.gz",
"windows-x86_64": "https://github.com/codewithkyrian/transformers-php/releases/download/{version}/dist-windows-x86_64.zip"
}
}
}
}
Loading
Loading