Skip to content

Commit 1fe48eb

Browse files
fix: Remove parallel processing for now. It causes FFI issues in log callback for PHP 8.3 and 8.4
1 parent afc5be9 commit 1fe48eb

2 files changed

Lines changed: 5 additions & 72 deletions

File tree

src/WhisperContext.php

Lines changed: 5 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -80,52 +80,6 @@ public function tokenize(string $text, int $maxTokens): array
8080
return $output;
8181
}
8282

83-
/**
84-
* Split the input audio in chunks and process each chunk separately using whisper_full_with_state()
85-
*
86-
* Result is stored in the default state of the context
87-
* Not thread safe if executed in parallel on the same context.
88-
* It seems this approach can offer some speedup in some cases.
89-
* However, the transcription accuracy can be worse at the beginning and end of each chunk.
90-
*
91-
* @param float[] $pcm Raw PCM audio data, 32 bit floating point at a sample rate of 16 kHz, 1 channel.
92-
* @param WhisperFullParams $params The parameters to use.
93-
* @param int $nProcessors The number of processors to use.
94-
*/
95-
public function fullParallel(array $pcm, WhisperFullParams $params, int $nProcessors): void
96-
{
97-
if (empty($pcm)) {
98-
// can randomly trigger segmentation faults if we don't check this
99-
throw WhisperException::noSamples();
100-
}
101-
102-
$pcmSize = count($pcm);
103-
$samples = $this->ffi->new("float[$pcmSize]");
104-
105-
foreach ($pcm as $i => $sample) {
106-
$samples[$i] = $sample;
107-
}
108-
109-
$cParams = $params->toCStruct($this->ffi);
110-
$result = $this->ffi->whisper_full_parallel(
111-
$this->ctx,
112-
$cParams,
113-
$this->ffi->cast('float *', $samples),
114-
$pcmSize,
115-
$nProcessors
116-
);
117-
118-
match ($result) {
119-
-1, -2 => throw WhisperException::failedToCalculateSpectrogram(),
120-
-3 => throw WhisperException::failedToAutoDetectLanguage(),
121-
-5 => throw WhisperException::audioCtxLongerThanMax($params->audioCtx, $this->modelNAudioCtx()),
122-
-6 => WhisperException::failedToEncode(),
123-
-7, -8 => WhisperException::failedToDecode(),
124-
0 => null,
125-
default => throw WhisperException::genericError($result)
126-
};
127-
}
128-
12983
/**
13084
* Return the number of tokens in the vocabulary
13185
*/
@@ -490,7 +444,11 @@ private function setupLoggerCallback(): void
490444
} else {
491445
$logCallback = function (int $level, string $message, ?CData $user_data) use ($logger) {
492446
$psrLevel = (LogLevel::tryFrom($level) ?? LogLevel::INFO)->toPsrLogLevel();
493-
$logger->log($psrLevel, $message);
447+
try {
448+
$logger->log($psrLevel, $message);
449+
} catch (\Throwable $e) {
450+
fwrite(STDERR, $e->getMessage());
451+
}
494452
};
495453
}
496454

tests/Feature/ProcessingTest.php

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -83,31 +83,6 @@
8383
expect($numSegments)->toBeGreaterThan(0);
8484
});
8585

86-
it('can handle parallel processing', function () {
87-
ini_set('memory_limit', -1);
88-
$ctx = new WhisperContext($this->modelPath);
89-
$pcm = loadTestAudio('jfk');
90-
91-
$fullParams = WhisperFullParams::default();
92-
93-
$ctx->fullParallel($pcm, $fullParams, 2);
94-
95-
$numSegments = $ctx->nSegments();
96-
expect($numSegments)->toBeGreaterThan(0);
97-
98-
for ($i = 0; $i < $numSegments; $i++) {
99-
$segment = $ctx->getSegmentText($i);
100-
$startTimestamp = $ctx->getSegmentStartTime($i);
101-
$endTimestamp = $ctx->getSegmentEndTime($i);
102-
103-
// Verify segment data
104-
expect($segment)->toBeString()
105-
->and($startTimestamp)->toBeInt()
106-
->and($endTimestamp)->toBeInt()
107-
->and($startTimestamp)->toBeLessThanOrEqual($endTimestamp);
108-
}
109-
});
110-
11186
it('can convert raw PCM audio to log mel spectrogram', function () {
11287
$ctx = new WhisperContext($this->modelPath);
11388
$state = $ctx->createState();

0 commit comments

Comments
 (0)