Skip to content

Commit 6ab7e1d

Browse files
Refactor FFI libraries into distinct wrapper classes for more structure and clarity
1 parent 78bcd16 commit 6ab7e1d

29 files changed

Lines changed: 1739 additions & 984 deletions

examples/pipelines/asr.php

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,26 +11,25 @@
1111

1212
ini_set('memory_limit', '-1');
1313

14-
//$transcriber = pipeline('automatic-speech-recognition', 'Xenova/whisper-tiny.en');
15-
$transcriber = pipeline('automatic-speech-recognition', 'Xenova/whisper-tiny');
14+
$transcriber = pipeline('automatic-speech-recognition', 'Xenova/whisper-tiny.en');
15+
//$transcriber = pipeline('automatic-speech-recognition', 'Xenova/whisper-tiny');
1616
//$transcriber = pipeline('automatic-speech-recognition', 'Xenova/whisper-base');
1717
//$transcriber = pipeline('automatic-speech-recognition', 'Xenova/wav2vec2-large-xlsr-53-english');
1818

1919
$audioUrl = __DIR__ . '/../sounds/kyrian-dev.wav';
20-
//$audioUrl = __DIR__ . '/../sounds/jfk.wav';
20+
$audioUrl = __DIR__ . '/../sounds/jfk.wav';
2121
//$audioUrl = __DIR__ . '/../sounds/preamble.wav';
2222
//$audioUrl = __DIR__ . '/../sounds/taunt.wav';
23-
//$audioUrl = __DIR__ . '/../sounds/gettysburg.wav';
23+
$audioUrl = __DIR__ . '/../sounds/gettysburg.wav';
2424
//$audioUrl = __DIR__ . '/../sounds/kyrian-speaking.wav';
2525
//$audioUrl = __DIR__ . '/../sounds/ted_60.wav';
26-
$audioUrl = __DIR__ . '/../sounds/french-audio.wav';
26+
//$audioUrl = __DIR__ . '/../sounds/french-audio.wav';
2727

2828
$streamer = StdOutStreamer::make();
2929
$output = $transcriber($audioUrl,
3030
maxNewTokens: 256,
3131
chunkLengthSecs: 24,
32-
task: 'translate'
33-
// returnTimestamps: true,
32+
// returnTimestamps: 'word',
3433
// streamer: $streamer
3534
);
3635

libs/VERSIONS

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
DOWNLOADER=1.0.0
22
OPENBLAS=0.3.27
33
RINDOW_MATLIB=1.0.1
4-
ONNXRUNTIME=1.17.0
4+
ONNXRUNTIME=1.17.0
5+
SNDFILE=1.2.2
6+
SAMPLERATE=0.2.2
7+
FAST_TRANSFORMERS_UTILS=1.0.0

src/FFI/FastTransformersUtils.php

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
6+
namespace Codewithkyrian\Transformers\FFI;
7+
8+
use Codewithkyrian\Transformers\Transformers;
9+
use Codewithkyrian\TransformersLibrariesDownloader\Libraries;
10+
use FFI;
11+
use FFI\CData;
12+
13+
class FastTransformersUtils
14+
{
15+
private static FFI $ffi;
16+
17+
private static function ffi(): FFI
18+
{
19+
if (!isset(self::$ffi)) {
20+
$headerCode = file_get_contents(Libraries::FastTransformersUtils->headerFile(Transformers::$libsDir));
21+
self::$ffi = FFI::cdef($headerCode, Libraries::FastTransformersUtils->libFile(Transformers::$libsDir));
22+
}
23+
24+
return self::$ffi;
25+
}
26+
27+
public static function new($type, bool $owned = true, bool $persistent = false): ?CData
28+
{
29+
return self::ffi()->new($type, $owned, $persistent);
30+
}
31+
32+
public static function enum(string $name)
33+
{
34+
return self::ffi()->{$name};
35+
}
36+
37+
public static function padReflect($input, int $length, int $paddedLength): CData
38+
{
39+
$padded = FFI::new("float[$paddedLength]");
40+
self::ffi()->pad_reflect($input, $length, $padded, $paddedLength);
41+
42+
return $padded;
43+
}
44+
45+
public static function spectrogram(
46+
$waveform, int $waveformLength, int $spectrogramLength, int $hopLength, int $fftLength,
47+
$window, int $windowLength, int $d1, int $d1Max, float $power, bool $center, float $preemphasis,
48+
$melFilters, int $nMelFilters, $nFreqBins, float $melFloor, int $logMel, ?bool $removeDcOffset,
49+
bool $doPad, bool $transpose
50+
): CData
51+
{
52+
$spectrogram = FFI::new("float[$spectrogramLength]");
53+
54+
self::ffi()->spectrogram(
55+
$waveform, $waveformLength, $spectrogram, $spectrogramLength, $hopLength, $fftLength, $window,
56+
$windowLength, $d1, $d1Max, $power, $center, $preemphasis, $melFilters, $nMelFilters, $nFreqBins,
57+
$melFloor, $logMel, $removeDcOffset, $doPad, $transpose,
58+
);
59+
60+
return $spectrogram;
61+
}
62+
}

src/FFI/Libc.php

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
6+
namespace Codewithkyrian\Transformers\FFI;
7+
8+
use FFI;
9+
use FFI\CData;
10+
use http\Exception\RuntimeException;
11+
12+
class Libc
13+
{
14+
private static FFI $ffi;
15+
16+
public static function ffi(): FFI
17+
{
18+
if (!isset(self::$ffi)) {
19+
if (PHP_OS_FAMILY == 'Windows') {
20+
self::$ffi = FFI::cdef(
21+
'size_t mbstowcs(void *wcstr, const char *mbstr, size_t count);',
22+
'msvcrt.dll'
23+
);
24+
}
25+
else{
26+
self::$ffi = FFI::cdef();
27+
}
28+
}
29+
30+
return self::$ffi;
31+
}
32+
33+
public static function new($type, bool $owned = true, bool $persistent = false): ?CData
34+
{
35+
return self::ffi()->new($type, $owned, $persistent);
36+
}
37+
38+
public static function mbStringToWcString(CData $wcStr, string $mbStr, int $count): CData
39+
{
40+
$length = self::ffi()->mbstowcs($wcStr, $mbStr, $count);
41+
if ($length != strlen($mbStr)) {
42+
throw new RuntimeException('Expected mbstowcs to return ' . strlen($mbStr) . ", got $length");
43+
}
44+
45+
return $wcStr;
46+
}
47+
48+
public static function cstring($str): CData
49+
{
50+
$bytes = strlen($str) + 1;
51+
// TODO fix?
52+
$ptr = self::new("char[$bytes]", owned: false);
53+
FFI::memcpy($ptr, $str, $bytes - 1);
54+
$ptr[$bytes - 1] = "\0";
55+
56+
return $ptr;
57+
}
58+
}

0 commit comments

Comments
 (0)