Skip to content

Commit cc929a4

Browse files
fix: statically calling FFI::cast deprecated in PHP 8.3
1 parent 7fa5e57 commit cc929a4

File tree

8 files changed

+83
-25
lines changed

8 files changed

+83
-25
lines changed

examples/pipelines/asr.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@
1919
$audioUrl = __DIR__ . '/../sounds/kyrian-dev.wav';
2020
$audioUrl = __DIR__ . '/../sounds/jfk.wav';
2121
$audioUrl = __DIR__ . '/../sounds/preamble.wav';
22-
$audioUrl = __DIR__ . '/../sounds/taunt.wav';
23-
$audioUrl = __DIR__ . '/../sounds/gettysburg.wav';
24-
$audioUrl = __DIR__ . '/../sounds/kyrian-speaking.wav';
25-
$audioUrl = __DIR__ . '/../sounds/ted_60.wav';
22+
//$audioUrl = __DIR__ . '/../sounds/taunt.wav';
23+
//$audioUrl = __DIR__ . '/../sounds/gettysburg.wav';
24+
//$audioUrl = __DIR__ . '/../sounds/kyrian-speaking.wav';
25+
//$audioUrl = __DIR__ . '/../sounds/ted_60.wav';
2626
//$audioUrl = __DIR__ . '/../sounds/sample-1.mp3';
2727

2828

src/FFI/Libs/FastTransformersUtils.php

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Exception;
99
use FFI;
1010
use FFI\CData;
11+
use FFI\CType;
1112

1213
class FastTransformersUtils
1314
{
@@ -36,18 +37,32 @@ protected static function ffi(): FFI
3637
/**
3738
* Creates a new instance of the specified type.
3839
*
39-
* @param string $type The type of the instance to create.
40+
* @param CType|string $type The type of the instance to create.
4041
* @param bool $owned Whether the instance should be owned. Default is true.
4142
* @param bool $persistent Whether the instance should be persistent. Default is false.
4243
*
4344
* @return CData|null The created instance, or null if the creation failed.
4445
* @throws Exception
4546
*/
46-
public static function new(string $type, bool $owned = true, bool $persistent = false): ?CData
47+
public static function new(CType|string $type, bool $owned = true, bool $persistent = false): ?CData
4748
{
4849
return self::ffi()->new($type, $owned, $persistent);
4950
}
5051

52+
/**
53+
* Casts a pointer to a different type.
54+
*
55+
* @param CType|string $type The type to cast to.
56+
* @param CData|int|float|bool|null $ptr The pointer to cast.
57+
*
58+
* @return ?CData The cast pointer, or null if the cast failed.
59+
* @throws Exception
60+
*/
61+
public static function cast(CType|string$type, CData|int|float|bool|null$ptr): ?CData
62+
{
63+
return self::ffi()->cast($type, $ptr);
64+
}
65+
5166
/**
5267
* Retrieves the value of the enum constant with the given name.
5368
*
@@ -74,7 +89,7 @@ public static function version(): string
7489

7590
public static function padReflect($input, int $length, int $paddedLength): CData
7691
{
77-
$padded = FFI::new("float[$paddedLength]");
92+
$padded = self::new("float[$paddedLength]");
7893
self::ffi()->pad_reflect($input, $length, $padded, $paddedLength);
7994

8095
return $padded;
@@ -87,7 +102,7 @@ public static function spectrogram(
87102
bool $doPad, bool $transpose
88103
): CData
89104
{
90-
$spectrogram = FFI::new("float[$spectrogramLength]");
105+
$spectrogram = self::new("float[$spectrogramLength]");
91106

92107
self::ffi()->spectrogram(
93108
$waveform, $waveformLength, $spectrogram, $spectrogramLength, $hopLength, $fftLength, $window,

src/FFI/Libs/OnnxRuntime.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Exception;
99
use FFI;
1010
use FFI\CData;
11+
use FFI\CType;
1112
use RuntimeException;
1213

1314
class OnnxRuntime
@@ -50,7 +51,16 @@ public static function new(string $type, bool $owned = true, bool $persistent =
5051
return self::ffi()->new($type, $owned, $persistent);
5152
}
5253

53-
public static function cast($type, $ptr): ?CData
54+
/**
55+
* Casts a pointer to a different type.
56+
*
57+
* @param CType|string $type The type to cast to.
58+
* @param CData|int|float|bool|null $ptr The pointer to cast.
59+
*
60+
* @return ?CData The cast pointer, or null if the cast failed.
61+
* @throws Exception
62+
*/
63+
public static function cast(CType|string$type, CData|int|float|bool|null$ptr): ?CData
5464
{
5565
return self::ffi()->cast($type, $ptr);
5666
}
@@ -306,8 +316,8 @@ public static function SessionGetOutputTypeInfo($session, int $index): CData
306316

307317
public static function GetAvailableProviders(): array
308318
{
309-
$outPtr = FFI::new('char**');
310-
$lengthPtr = FFI::new('int');
319+
$outPtr = self::new('char**');
320+
$lengthPtr = self::new('int');
311321

312322
self::checkStatus((self::api()->GetAvailableProviders)(FFI::addr($outPtr), FFI::addr($lengthPtr)));
313323

src/FFI/Libs/Samplerate.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Exception;
1010
use FFI;
1111
use FFI\CData;
12+
use FFI\CType;
1213
use RuntimeException;
1314
use function Codewithkyrian\Transformers\Utils\joinPaths;
1415

@@ -51,6 +52,20 @@ public static function new(string $type, bool $owned = true, bool $persistent =
5152
return self::ffi()->new($type, $owned, $persistent);
5253
}
5354

55+
/**
56+
* Casts a pointer to a different type.
57+
*
58+
* @param CType|string $type The type to cast to.
59+
* @param CData|int|float|bool|null $ptr The pointer to cast.
60+
*
61+
* @return ?CData The cast pointer, or null if the cast failed.
62+
* @throws Exception
63+
*/
64+
public static function cast(CType|string$type, CData|int|float|bool|null$ptr): ?CData
65+
{
66+
return self::ffi()->cast($type, $ptr);
67+
}
68+
5469
/**
5570
* Retrieves the value of the enum constant with the given name.
5671
*
@@ -85,7 +100,7 @@ public static function version(): string
85100
*/
86101
public static function srcNew(int $converterType, int $channels): CData
87102
{
88-
$error = FFI::new('int32_t');
103+
$error = self::new('int32_t');
89104

90105
$state = self::ffi()->src_new($converterType, $channels, FFI::addr($error));
91106

src/FFI/Libs/Sndfile.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Exception;
1010
use FFI;
1111
use FFI\CData;
12+
use FFI\CType;
1213
use RuntimeException;
1314
use function Codewithkyrian\Transformers\Utils\joinPaths;
1415

@@ -79,6 +80,20 @@ public static function new(string $type, bool $owned = true, bool $persistent =
7980
return self::ffi()->new($type, $owned, $persistent);
8081
}
8182

83+
/**
84+
* Casts a pointer to a different type.
85+
*
86+
* @param CType|string $type The type to cast to.
87+
* @param CData|int|float|bool|null $ptr The pointer to cast.
88+
*
89+
* @return ?CData The cast pointer, or null if the cast failed.
90+
* @throws Exception
91+
*/
92+
public static function cast(CType|string$type, CData|int|float|bool|null $ptr): ?CData
93+
{
94+
return self::ffi()->cast($type, $ptr);
95+
}
96+
8297
/**
8398
* Retrieves the value of the enum constant with the given name.
8499
*

src/Pipelines/AutomaticSpeechRecognitionPipeline.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
namespace Codewithkyrian\Transformers\Pipelines;
77

8-
use Codewithkyrian\Transformers\Generation\Streamers\WhisperTextStreamer;
98
use Codewithkyrian\Transformers\Tensor\Tensor;
109
use Codewithkyrian\Transformers\Utils\Audio;
1110
use Codewithkyrian\Transformers\Utils\GenerationConfig;

src/Utils/Audio.php

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,14 @@ public function toTensor(int $samplerate = 41000, int $chunkSize = 2048): Tensor
5656
$state = Samplerate::srcNew(Samplerate::enum('SRC_SINC_FASTEST'), $this->channels());
5757

5858
$inputSize = $chunkSize * $this->channels();
59-
$inputData = FFI::new("float[$inputSize]");
59+
$inputData = Samplerate::new("float[$inputSize]");
6060
$outputSize = $chunkSize * $this->channels();
61-
$outputData = FFI::new("float[$outputSize]");
61+
$outputData = Samplerate::new("float[$outputSize]");
6262

6363
$srcData = Samplerate::new('SRC_DATA');
64-
$srcData->data_in = FFI::cast('float *', $inputData);
64+
$srcData->data_in = Samplerate::cast('float *', $inputData);
6565
$srcData->output_frames = $chunkSize / $this->channels();
66-
$srcData->data_out = FFI::cast('float *', $outputData);
66+
$srcData->data_out = Samplerate::cast('float *', $outputData);
6767
$srcData->src_ratio = $samplerate / $this->samplerate();
6868

6969
while (true) {
@@ -105,15 +105,19 @@ public function toTensor(int $samplerate = 41000, int $chunkSize = 2048): Tensor
105105

106106
$audioTensor = Tensor::fromString($tensorData, Tensor::float32, [$totalOutputFrames, $this->channels()]);
107107

108-
return $audioTensor->mean(1);
108+
if ($this->channels() > 1) {
109+
$audioTensor = $audioTensor->mean(1);
110+
}
111+
112+
return $audioTensor->squeeze();
109113
}
110114

111115
public function fromTensor(Tensor $tensor): void
112116
{
113117
$size = $tensor->size();
114-
$buffer = FFI::new("float[$size]");
118+
$buffer = Sndfile::new("float[$size]");
115119
$bufferString = $tensor->toString();
116-
$buffer->cdata = FFI::cast('float *', $bufferString);
120+
$buffer->cdata = Sndfile::cast('float *', (int)$bufferString);
117121

118122
$write = Sndfile::writeFrames($this->sndfile, $buffer, $size);
119123

src/Utils/InferenceSession.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -325,13 +325,13 @@ private function convertInputTensorToOnnxTensor($inputFeed, &$refs): ?CData
325325
$ndim = $input->ndim();
326326
$size = $input->size();
327327

328-
$inputNodeShape = FFI::new("int64_t[$ndim]");
328+
$inputNodeShape = OnnxRuntime::new("int64_t[$ndim]");
329329
for ($i = 0; $i < $ndim; $i++) {
330330
$inputNodeShape[$i] = $shape[$i];
331331
}
332332

333333
if ($inp['type'] == 'tensor(string)') {
334-
$inputTensorValues = FFI::new("char*[$size]");
334+
$inputTensorValues = OnnxRuntime::new("char*[$size]");
335335
$this->fillStringTensorValues($input, $inputTensorValues, $refs);
336336

337337
$typeEnum = OnnxRuntime::enum('ONNX_TENSOR_ELEMENT_DATA_TYPE_STRING');
@@ -351,9 +351,9 @@ private function convertInputTensorToOnnxTensor($inputFeed, &$refs): ?CData
351351
}
352352

353353
if ($size === 0) {
354-
$inputTensorValues = FFI::new("void *");
354+
$inputTensorValues = OnnxRuntime::new("void *");
355355
} else {
356-
$inputTensorValues = FFI::new("{$castType}[$size]");
356+
$inputTensorValues = OnnxRuntime::new("{$castType}[$size]");
357357
}
358358

359359
$inputDump = $input->buffer()->dump();
@@ -386,7 +386,7 @@ private function fillStringTensorValues(Tensor $input, $ptr, &$refs): void
386386
private function createNodeNames($names, &$refs): CData
387387
{
388388
$namesSize = count($names);
389-
$ptr = FFI::new("char*[$namesSize]");
389+
$ptr = OnnxRuntime::new("char*[$namesSize]");
390390
foreach ($names as $i => $name) {
391391
$strPtr = Libc::cstring($name);
392392
$ptr[$i] = $strPtr;

0 commit comments

Comments
 (0)