Skip to content

Commit 86dd8f6

Browse files
Refactor libraries resolution to an enum
1 parent 2a0db6d commit 86dd8f6

5 files changed

Lines changed: 342 additions & 82 deletions

File tree

src/Libraries.php

Lines changed: 298 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,298 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
6+
namespace Codewithkyrian\Transformers;
7+
8+
use function Codewithkyrian\Transformers\Utils\joinPaths;
9+
10+
enum Libraries
11+
{
12+
case OpenBlas_Serial;
13+
case OpenBlas_OpenMP;
14+
case RindowMatlib_Serial;
15+
case RindowMatlib_OpenMP;
16+
case Lapacke_Serial;
17+
case Lapacke_OpenMP;
18+
case OnnxRuntime;
19+
20+
public const LIBS_DIR = __DIR__ . '/../libs/';
21+
public const VERSIONS_FILE = __DIR__ . '/../libs/VERSIONS';
22+
23+
protected const LIBRARIES = [
24+
'x86_64-darwin' => [
25+
'archive' => 'libraries-osx-x86_64-{{version}}.tar.gz',
26+
'checksum' => 'f72a2bcca40e2650756c6b96c69ef031236aaab1b98673e744da4eef0c4bddbd',
27+
'rindowmatlib.serial' => [
28+
'folder' => 'rindow-matlib-Darwin-{{version}}',
29+
'lib' => 'librindowmatlib_serial.dylib',
30+
'header' => 'rindow-matlib.h'
31+
],
32+
'rindowmatlib.openmp' => [
33+
'folder' => 'rindow-matlib-Darwin-{{version}}',
34+
'lib' => 'librindowmatlib_openmp.dylib',
35+
'header' => 'rindow-matlib.h'
36+
],
37+
'openblas.serial' => [
38+
'folder' => 'openblas-osx-x86_64-{{version}}',
39+
'lib' => 'libopenblas_serial.dylib',
40+
'header' => 'openblas.h'
41+
],
42+
'openblas.openmp' => [
43+
'folder' => 'openblas-osx-x86_64-{{version}}',
44+
'lib' => 'libopenblas_openmp.dylib',
45+
'header' => 'openblas.h'
46+
],
47+
'lapacke.serial' => [
48+
'folder' => 'openblas-osx-x86_64-{{version}}',
49+
'lib' => 'libopenblas_serial.dylib',
50+
'header' => 'lapacke.h'
51+
],
52+
'lapacke.openmp' => [
53+
'folder' => 'openblas-osx-x86_64-{{version}}',
54+
'lib' => 'libopenblas_openmp.dylib',
55+
'header' => 'lapacke.h'
56+
],
57+
'onnxruntime' => [
58+
'folder' => 'onnxruntime-osx-x86_64-{{version}}',
59+
'lib' => 'libonnxruntime.dylib',
60+
'header' => 'onnxruntime.h'
61+
],
62+
],
63+
64+
'arm64-darwin' => [
65+
'archive' => 'libraries-osx-arm64-{{version}}.tar.gz',
66+
'checksum' => 'f72a2bcca40e2650756c6b96c69ef031236aaab1b98673e744da4eef0c4bddbd',
67+
'rindowmatlib.serial' => [
68+
'folder' => 'rindow-matlib-Darwin-{{version}}',
69+
'lib' => 'librindowmatlib_serial.dylib',
70+
'header' => 'rindow-matlib.h'
71+
],
72+
'rindowmatlib.openmp' => [
73+
'folder' => 'rindow-matlib-Darwin-{{version}}',
74+
'lib' => 'librindowmatlib_openmp.dylib',
75+
'header' => 'rindow-matlib.h'
76+
],
77+
'openblas.serial' => [
78+
'folder' => 'openblas-osx-arm64-{{version}}',
79+
'lib' => 'libopenblas_serial.dylib',
80+
'header' => 'openblas.h'
81+
],
82+
'openblas.openmp' => [
83+
'folder' => 'openblas-osx-arm64-{{version}}',
84+
'lib' => 'libopenblas_openmp.dylib',
85+
'header' => 'openblas.h'
86+
],
87+
'lapacke.serial' => [
88+
'folder' => 'openblas-osx-arm64-{{version}}',
89+
'lib' => 'libopenblas_serial.dylib',
90+
'header' => 'lapacke.h'
91+
],
92+
'lapacke.openmp' => [
93+
'folder' => 'openblas-osx-arm64-{{version}}',
94+
'lib' => 'libopenblas_openmp.dylib',
95+
'header' => 'lapacke.h'
96+
],
97+
'onnxruntime' => [
98+
'folder' => 'onnxruntime-osx-arm64-{{version}}',
99+
'lib' => 'libonnxruntime.dylib',
100+
'header' => 'onnxruntime.h'
101+
],
102+
],
103+
104+
'x86_64-linux' => [
105+
'archive' => 'libraries-linux-x86_64-{{version}}.tar.gz',
106+
'checksum' => 'f72a2bcca40e2650756c6b96c69ef031236aaab1b98673e744da4eef0c4bddbd',
107+
'rindowmatlib.serial' => [
108+
'folder' => 'rindow-matlib-Linux-{{version}}',
109+
'lib' => 'librindowmatlib_serial.so',
110+
'header' => 'rindow-matlib.h'
111+
],
112+
'rindowmatlib.openmp' => [
113+
'folder' => 'rindow-matlib-Linux-{{version}}',
114+
'lib' => 'librindowmatlib_openmp.so',
115+
'header' => 'rindow-matlib.h'
116+
],
117+
'openblas.serial' => [
118+
'folder' => 'openblas-linux-x86_64-{{version}}',
119+
'lib' => 'libopenblas_serial.so',
120+
'header' => 'openblas.h'
121+
],
122+
'openblas.openmp' => [
123+
'folder' => 'openblas-linux-x86_64-{{version}}',
124+
'lib' => 'libopenblas_openmp.so',
125+
'header' => 'openblas.h'
126+
],
127+
'lapacke.serial' => [
128+
'folder' => 'openblas-linux-x86_64-{{version}}',
129+
'lib' => 'liblapacke_serial.so',
130+
'header' => 'lapacke.h'
131+
],
132+
'lapacke.openmp' => [
133+
'folder' => 'openblas-linux-x86_64-{{version}}',
134+
'lib' => 'liblapacke_openmp.so',
135+
'header' => 'lapacke.h'
136+
],
137+
'onnxruntime' => [
138+
'folder' => 'onnxruntime-linux-x86_64-{{version}}',
139+
'lib' => 'libonnxruntime.so',
140+
'header' => 'onnxruntime.h'
141+
],
142+
],
143+
144+
'aarch64-linux' => [
145+
'archive' => 'libraries-linux-arm64-{{version}}.tar.gz',
146+
'checksum' => 'f72a2bcca40e2650756c6b96c69ef031236aaab1b98673e744da4eef0c4bddbd',
147+
'rindowmatlib.serial' => [
148+
'folder' => 'rindow-matlib-Linux-{{version}}',
149+
'lib' => 'librindowmatlib_serial.so',
150+
'header' => 'rindow-matlib.h'
151+
],
152+
'rindowmatlib.openmp' => [
153+
'folder' => 'rindow-matlib-Linux-{{version}}',
154+
'lib' => 'librindowmatlib_openmp.so',
155+
'header' => 'rindow-matlib.h'
156+
],
157+
'openblas.serial' => [
158+
'folder' => 'openblas-linux-arm64-{{version}}',
159+
'lib' => 'libopenblas_serial.so',
160+
'header' => 'openblas.h'
161+
],
162+
'openblas.openmp' => [
163+
'folder' => 'openblas-linux-arm64-{{version}}',
164+
'lib' => 'libopenblas_openmp.so',
165+
'header' => 'openblas.h'
166+
],
167+
'lapacke.serial' => [
168+
'folder' => 'openblas-linux-arm64-{{version}}',
169+
'lib' => 'liblapacke_serial.so',
170+
'header' => 'lapacke.h'
171+
],
172+
'lapacke.openmp' => [
173+
'folder' => 'openblas-linux-arm64-{{version}}',
174+
'lib' => 'liblapacke_openmp.so',
175+
'header' => 'lapacke.h'
176+
],
177+
'onnxruntime' => [
178+
'folder' => 'onnxruntime-linux-arm64-{{version}}',
179+
'lib' => 'libonnxruntime.so',
180+
'header' => 'onnxruntime.h'
181+
],
182+
],
183+
184+
'x64-windows' => [
185+
'archive' => 'libraries-windows-x64-{{version}}.zip',
186+
'checksum' => 'f72a2bcca40e2650756c6b96c69ef031236aaab1b98673e744da4eef0c4bddbd',
187+
'rindowmatlib.serial' => [
188+
'folder' => 'rindow-matlib-Windows-{{version}}',
189+
'lib' => 'rindowmatlib_serial.dll',
190+
'header' => 'rindow-matlib.h'
191+
],
192+
'rindowmatlib.openmp' => [
193+
'folder' => 'rindow-matlib-Windows-{{version}}',
194+
'lib' => 'rindowmatlib_openmp.dll',
195+
'header' => 'rindow-matlib.h'
196+
],
197+
'openblas.serial' => [
198+
'folder' => 'openblas-windows-x64-{{version}}',
199+
'lib' => 'openblas_serial.dll',
200+
'header' => 'openblas.h'
201+
],
202+
'openblas.openmp' => [
203+
'folder' => 'openblas-windows-x64-{{version}}',
204+
'lib' => 'openblas_openmp.dll',
205+
'header' => 'openblas.h'
206+
],
207+
'lapacke.serial' => [
208+
'folder' => 'openblas-windows-x64-{{version}}',
209+
'lib' => 'lapacke_serial.dll',
210+
'header' => 'lapacke.h'
211+
],
212+
'lapacke.openmp' => [
213+
'folder' => 'openblas-windows-x64-{{version}}',
214+
'lib' => 'lapacke_openmp.dll',
215+
'header' => 'lapacke.h'
216+
],
217+
'onnxruntime' => [
218+
'folder' => 'onnxruntime-windows-x64-{{version}}',
219+
'lib' => 'onnxruntime.dll',
220+
'header' => 'onnxruntime.h'
221+
],
222+
],
223+
];
224+
225+
public static function platformKey(): string
226+
{
227+
return match (PHP_OS_FAMILY) {
228+
'Windows' => 'x64-windows',
229+
'Darwin' => php_uname('m') == 'x86_64' ? 'x86_64-darwin' : 'arm64-darwin',
230+
default => php_uname('m') == 'x86_64' ? 'x86_64-linux' : 'aarch64-linux',
231+
};
232+
}
233+
234+
public function version(): string
235+
{
236+
$versions = parse_ini_file(self::VERSIONS_FILE);
237+
238+
return match ($this) {
239+
self::OpenBlas_Serial,
240+
self::OpenBlas_OpenMP,
241+
self::Lapacke_Serial,
242+
self::Lapacke_OpenMP => $versions['OPENBLAS'],
243+
self::RindowMatlib_Serial,
244+
self::RindowMatlib_OpenMP => $versions['RINDOW_MATLIB'],
245+
self::OnnxRuntime => $versions['ONNXRUNTIME'],
246+
};
247+
}
248+
249+
public function folder(): string
250+
{
251+
return match ($this) {
252+
self::OpenBlas_Serial => self::LIBRARIES[self::platformKey()]['openblas.serial']['folder'],
253+
self::OpenBlas_OpenMP => self::LIBRARIES[self::platformKey()]['openblas.openmp']['folder'],
254+
self::RindowMatlib_Serial => self::LIBRARIES[self::platformKey()]['rindowmatlib.serial']['folder'],
255+
self::RindowMatlib_OpenMP => self::LIBRARIES[self::platformKey()]['rindowmatlib.openmp']['folder'],
256+
self::Lapacke_Serial => self::LIBRARIES[self::platformKey()]['lapacke.serial']['folder'],
257+
self::Lapacke_OpenMP => self::LIBRARIES[self::platformKey()]['lapacke.openmp']['folder'],
258+
self::OnnxRuntime => self::LIBRARIES[self::platformKey()]['onnxruntime']['folder'],
259+
};
260+
}
261+
262+
263+
public function libFile(): string
264+
{
265+
$file = match ($this) {
266+
self::OpenBlas_Serial => self::LIBRARIES[self::platformKey()]['openblas.serial']['lib'],
267+
self::OpenBlas_OpenMP => self::LIBRARIES[self::platformKey()]['openblas.openmp']['lib'],
268+
self::RindowMatlib_Serial => self::LIBRARIES[self::platformKey()]['rindowmatlib.serial']['lib'],
269+
self::RindowMatlib_OpenMP => self::LIBRARIES[self::platformKey()]['rindowmatlib.openmp']['lib'],
270+
self::Lapacke_Serial => self::LIBRARIES[self::platformKey()]['lapacke.serial']['lib'],
271+
self::Lapacke_OpenMP => self::LIBRARIES[self::platformKey()]['lapacke.openmp']['lib'],
272+
self::OnnxRuntime => self::LIBRARIES[self::platformKey()]['onnxruntime']['lib'],
273+
};
274+
275+
$folder = str_replace('{{version}}', $this->version(), $this->folder());
276+
277+
return joinPaths(self::LIBS_DIR, $folder, 'lib', $file);
278+
}
279+
280+
public function headerFile(): string
281+
{
282+
$file = match ($this) {
283+
self::OpenBlas_Serial => self::LIBRARIES[self::platformKey()]['openblas.serial']['header'],
284+
self::OpenBlas_OpenMP => self::LIBRARIES[self::platformKey()]['openblas.openmp']['header'],
285+
self::RindowMatlib_Serial => self::LIBRARIES[self::platformKey()]['rindowmatlib.serial']['header'],
286+
self::RindowMatlib_OpenMP => self::LIBRARIES[self::platformKey()]['rindowmatlib.openmp']['header'],
287+
self::Lapacke_Serial => self::LIBRARIES[self::platformKey()]['lapacke.serial']['header'],
288+
self::Lapacke_OpenMP => self::LIBRARIES[self::platformKey()]['lapacke.openmp']['header'],
289+
self::OnnxRuntime => self::LIBRARIES[self::platformKey()]['onnxruntime']['header'],
290+
};
291+
292+
$folder = str_replace('{{version}}', $this->version(), $this->folder());
293+
294+
return joinPaths(self::LIBS_DIR, $folder, 'include', $file);
295+
}
296+
297+
298+
}

src/Transformers.php

Lines changed: 0 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -12,44 +12,6 @@
1212

1313
class Transformers
1414
{
15-
public const VERSION = '0.4.0';
16-
public const LIBS_DIR = __DIR__ . '/../libs/';
17-
18-
protected const LIBRARIES = [
19-
'x86_64-darwin' => [
20-
'archive' => 'libraries-osx-x86_64-{{version}}.tar.gz',
21-
'checksum' => 'f72a2bcca40e2650756c6b96c69ef031236aaab1b98673e744da4eef0c4bddbd',
22-
'rindowmatlib.serial' => 'rindow-matlib-Darwin-1.0.0/lib/librindowmatlib_serial.dylib',
23-
'rindowmatlib.openmp' => 'rindow-matlib-Darwin-1.0.0/lib/librindowmatlib_openmp.dylib',
24-
'openblas.serial' => 'openblas-osx-x86_64-0.3.27/lib/libopenblas_serial.dylib',
25-
'openblas.openmp' => 'openblas-osx-x86_64-0.3.27/lib/libopenblas_openmp.dylib',
26-
'openblas.include' => 'openblas-osx-x86_64-0.3.27/include/openblas.h',
27-
'lapacke.serial' => 'openblas-osx-x86_64-0.3.27/lib/libopenblas_serial.dylib',
28-
'lapacke.openmp' => 'openblas-osx-x86_64-0.3.27/lib/libopenblas_openmp.dylib'
29-
],
30-
'arm64-darwin' => [
31-
'archive' => 'libraries-osx-arm64-{{version}}.tar.gz',
32-
'checksum' => 'f72a2bcca40e2650756c6b96c69ef031236aaab1b98673e744da4eef0c4bddbd',
33-
'rindowmatlib.serial' => 'rindow-matlib-Darwin-1.0.0/lib/librindowmatlib_serial.dylib',
34-
'rindowmatlib.openmp' => 'rindow-matlib-Darwin-1.0.0/lib/librindowmatlib_openmp.dylib',
35-
'openblas.serial' => 'openblas-osx-arm64-0.3.27/lib/libopenblas_serial.dylib',
36-
'openblas.openmp' => 'openblas-osx-arm64-0.3.27/lib/libopenblas_openmp.dylib',
37-
'openblas.include' => 'openblas-osx-arm64-0.3.27/include/openblas.h',
38-
'lapacke.serial' => 'openblas-osx-arm64-0.3.27/lib/libopenblas_serial.dylib',
39-
'lapacke.openmp' => 'openblas-osx-arm64-0.3.27/lib/libopenblas_openmp.dylib'
40-
],
41-
'x86_64-linux' => [
42-
43-
],
44-
'aarch64-linux' => [
45-
46-
],
47-
'x64-windows' => [
48-
49-
]
50-
];
51-
52-
5315
public static string $cacheDir = '.transformers-cache';
5416

5517
public static string $remoteHost = 'https://huggingface.co';
@@ -142,17 +104,4 @@ public function setImageDriver(ImageDriver $imageDriver): static
142104

143105
return $this;
144106
}
145-
146-
public static function getLib(string $key): string
147-
{
148-
$platformKey = match (PHP_OS_FAMILY) {
149-
'Windows' => 'x64-windows',
150-
'Darwin' => php_uname('m') == 'x86_64' ? 'x86_64-darwin' : 'arm64-darwin',
151-
default => php_uname('m') == 'x86_64' ? 'x86_64-linux' : 'aarch64-linux',
152-
};
153-
154-
$filename = self::LIBRARIES[$platformKey][$key];
155-
156-
return joinPaths(Transformers::LIBS_DIR, $filename);
157-
}
158107
}

src/Utils/Image.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ public static function setDriver(ImageDriver $imageDriver) : void
8686
{
8787
self::$imagine = match ($imageDriver) {
8888
ImageDriver::IMAGICK => new \Imagine\Imagick\Imagine(),
89-
ImageDriver::GD => new \Imagine\GD\Imagine(),
89+
ImageDriver::GD => new \Imagine\Gd\Imagine(),
9090
ImageDriver::VIPS => new \Imagine\Vips\Imagine(),
9191
};
9292
}

src/Utils/InferenceSession.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,7 @@ private function convertInputTensorToOnnxTensor($inputFeed, &$refs)
385385

386386
if ($inp['type'] == 'tensor(string)') {
387387
$inputTensorValues = $this->ffi->new("char*[$size]");
388-
$this->fillStringTensorValues($input, $inputTensorValues, $shape, $refs);
388+
$this->fillStringTensorValues($input, $inputTensorValues, $refs);
389389

390390
$typeEnum = $this->ffi->ONNX_TENSOR_ELEMENT_DATA_TYPE_STRING;
391391
$this->checkStatus(($this->api->CreateTensorAsOrtValue)($this->allocator, $inputNodeShape, $ndim, $typeEnum, \FFI::addr($inputTensor[$idx])));
@@ -404,15 +404,13 @@ private function convertInputTensorToOnnxTensor($inputFeed, &$refs)
404404
$this->unsupportedType('input', $inp['type']);
405405
}
406406

407-
408407
if ($size === 0) {
409408
$inputTensorValues = $this->ffi->new("void *");
410409
} else {
411410
$inputTensorValues = $this->ffi->new("{$castType}[$size]");
412411
}
413412

414413
$inputDump = $input->buffer()->dump();
415-
416414
\FFI::memcpy($inputTensorValues, $inputDump, strlen($inputDump));
417415

418416
$this->checkStatus(($this->api->CreateTensorWithDataAsOrtValue)($allocatorInfo, $inputTensorValues, \FFI::sizeof($inputTensorValues), $inputNodeShape, $ndim, $typeEnum, \FFI::addr($inputTensor[$idx])));

0 commit comments

Comments
 (0)