Fix StorageView conversion for CPU/GPU compatibility

This commit is contained in:
Quentin Fuxa
2025-09-09 15:44:16 +02:00
parent 1f7798c7c1
commit 3358877054

View File

@@ -409,9 +409,12 @@ class PaddedAlignAttWhisper:
mel_padded_2 = self.fw_feature_extractor(waveform=input_segments.numpy(), padding=N_SAMPLES)[None, :]
mel = fw_pad_or_trim(mel_padded_2, N_FRAMES, axis=-1)
encoder_feature_ctranslate = self.fw_encoder.encode(mel)
if type(encoder_feature_ctranslate).__module__ == 'ctranslate2._ext':
if self.device == 'cpu': #it seems that on gpu, passing StorageView to torch.as_tensor fails and wrapping in the array works
encoder_feature_ctranslate = np.array(encoder_feature_ctranslate)
encoder_feature = torch.as_tensor(encoder_feature_ctranslate, device=self.device)
try:
encoder_feature = torch.as_tensor(encoder_feature_ctranslate, device=self.device)
except TypeError: # Normally the cpu condition should prevent having exceptions, but just in case:
encoder_feature = torch.as_tensor(np.array(encoder_feature_ctranslate), device=self.device)
else:
# mel + padding to 30s
mel_padded = log_mel_spectrogram(input_segments, n_mels=self.model.dims.n_mels, padding=N_SAMPLES,