stanimirovb commited on
Commit
6282f63
·
unverified ·
1 Parent(s): 9373d6b

whisper : auto-grow working areas for mel_calc_cuda (#2227)

Browse files

* whisper : auto-grow working areas for mel_calc_cuda, fixes #2226

* whisper : only calculate mel spectrogram on GPU if audio is <= 5 min

Files changed (3) hide show
  1. whisper-mel-cuda.cu +50 -26
  2. whisper-mel.hpp +1 -1
  3. whisper.cpp +19 -3
whisper-mel-cuda.cu CHANGED
@@ -145,17 +145,6 @@ void calc_magnitudes(
145
 
146
  constexpr auto LOG_MEL_PREFIX_SIZE = 256;
147
 
148
- size_t get_log_mel_temp_storage_size() {
149
- constexpr auto maxPaddedSamples = 2 * WHISPER_N_SAMPLES + WHISPER_N_FFT;
150
- constexpr auto maxFrames = 1 + (maxPaddedSamples - WHISPER_N_FFT) / WHISPER_HOP_LENGTH;
151
- constexpr auto maxMels = 160;
152
-
153
- size_t nbytes = 0;
154
- float * temp = nullptr;
155
- cub::DeviceReduce::Max(nullptr, nbytes, temp, temp, maxFrames * maxMels);
156
- return nbytes + LOG_MEL_PREFIX_SIZE;
157
- }
158
-
159
  void calc_log_mel(
160
  const float * mel_data,
161
  int n_mel,
@@ -186,11 +175,14 @@ class mel_calc_cuda : public whisper_mel_calc {
186
 
187
  float * m_hann_window = nullptr;
188
 
 
 
 
 
 
189
  size_t m_cufft_workspace_size = 0;
190
  void * m_cufft_workspace = nullptr;
191
 
192
- float * m_filters = nullptr;
193
-
194
  size_t m_log_mel_temp_storage_size = 0;
195
  void * m_log_mel_temp_storage = nullptr;
196
  public:
@@ -215,14 +207,6 @@ public:
215
  CUDA_CHECK(cudaMemcpyAsync(m_hann_window, hw.data, hw.len * sizeof(float), cudaMemcpyHostToDevice, m_stream));
216
  }
217
 
218
- // create working area
219
- {
220
- constexpr auto maxPaddedSamples = 2 * WHISPER_N_SAMPLES + WHISPER_N_FFT;
221
- constexpr auto maxFrames = 1 + (maxPaddedSamples - WHISPER_N_FFT) / WHISPER_HOP_LENGTH;
222
- CUFFT_CHECK(cufftEstimate1d(WHISPER_N_FFT, CUFFT_R2C, maxFrames, &m_cufft_workspace_size));
223
- CUDA_CHECK(cudaMallocAsync(&m_cufft_workspace, m_cufft_workspace_size, m_stream));
224
- }
225
-
226
  // fill filters
227
  {
228
  auto& f = filters.data;
@@ -230,10 +214,8 @@ public:
230
  CUDA_CHECK(cudaMemcpyAsync(m_filters, f.data(), f.size() * sizeof(float), cudaMemcpyHostToDevice, m_stream));
231
  }
232
 
233
- {
234
- m_log_mel_temp_storage_size = get_log_mel_temp_storage_size();
235
- CUDA_CHECK(cudaMallocAsync(&m_log_mel_temp_storage, m_log_mel_temp_storage_size, m_stream));
236
- }
237
  }
238
 
239
  ~mel_calc_cuda() {
@@ -245,7 +227,49 @@ public:
245
  CUDA_CHECK(cudaFree(m_log_mel_temp_storage));
246
  }
247
 
248
- virtual whisper_mel calculate(whisper_span<const float> samples, int /*n_threads*/) const override {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
249
  const size_t mirror_pad = WHISPER_N_FFT / 2;
250
  const size_t padded_size = samples.len + WHISPER_N_SAMPLES + WHISPER_N_FFT;
251
 
 
145
 
146
  constexpr auto LOG_MEL_PREFIX_SIZE = 256;
147
 
 
 
 
 
 
 
 
 
 
 
 
148
  void calc_log_mel(
149
  const float * mel_data,
150
  int n_mel,
 
175
 
176
  float * m_hann_window = nullptr;
177
 
178
+ float * m_filters = nullptr;
179
+
180
+ // max samples for which we have allocated memory for the temp working areas below (cufft, log_mel)
181
+ int m_n_max_samples = 0;
182
+
183
  size_t m_cufft_workspace_size = 0;
184
  void * m_cufft_workspace = nullptr;
185
 
 
 
186
  size_t m_log_mel_temp_storage_size = 0;
187
  void * m_log_mel_temp_storage = nullptr;
188
  public:
 
207
  CUDA_CHECK(cudaMemcpyAsync(m_hann_window, hw.data, hw.len * sizeof(float), cudaMemcpyHostToDevice, m_stream));
208
  }
209
 
 
 
 
 
 
 
 
 
210
  // fill filters
211
  {
212
  auto& f = filters.data;
 
214
  CUDA_CHECK(cudaMemcpyAsync(m_filters, f.data(), f.size() * sizeof(float), cudaMemcpyHostToDevice, m_stream));
215
  }
216
 
217
+ // preallocate working areas enough for the most common cases (<= 30s)
218
+ ensure_working_areas(WHISPER_N_SAMPLES);
 
 
219
  }
220
 
221
  ~mel_calc_cuda() {
 
227
  CUDA_CHECK(cudaFree(m_log_mel_temp_storage));
228
  }
229
 
230
+ void ensure_working_areas(int n_samples) {
231
+ if (n_samples <= m_n_max_samples) {
232
+ return;
233
+ }
234
+
235
+ const auto max_padded_samples = n_samples + WHISPER_N_SAMPLES + WHISPER_N_FFT;
236
+ const auto max_frames = 1 + (max_padded_samples - WHISPER_N_FFT) / WHISPER_HOP_LENGTH;
237
+
238
+ // cufft workspace
239
+ {
240
+ if (m_cufft_workspace) {
241
+ CUDA_CHECK(cudaFree(m_cufft_workspace));
242
+ m_cufft_workspace_size = 0;
243
+ m_cufft_workspace = nullptr;
244
+ }
245
+ CUFFT_CHECK(cufftEstimate1d(WHISPER_N_FFT, CUFFT_R2C, max_frames, &m_cufft_workspace_size));
246
+ CUDA_CHECK(cudaMallocAsync(&m_cufft_workspace, m_cufft_workspace_size, m_stream));
247
+ }
248
+
249
+ // device reduce working area
250
+ {
251
+ if (m_log_mel_temp_storage) {
252
+ CUDA_CHECK(cudaFree(m_log_mel_temp_storage));
253
+ m_log_mel_temp_storage_size = 0;
254
+ m_log_mel_temp_storage = nullptr;
255
+ }
256
+
257
+ const auto max_mels = 160;
258
+
259
+ size_t nbytes = 0;
260
+ float* temp = nullptr;
261
+ cub::DeviceReduce::Max(nullptr, nbytes, temp, temp, max_frames * max_mels);
262
+ m_log_mel_temp_storage_size = nbytes + LOG_MEL_PREFIX_SIZE;
263
+
264
+ CUDA_CHECK(cudaMallocAsync(&m_log_mel_temp_storage, m_log_mel_temp_storage_size, m_stream));
265
+ }
266
+
267
+ m_n_max_samples = n_samples;
268
+ }
269
+
270
+ virtual whisper_mel calculate(whisper_span<const float> samples, int /*n_threads*/) override {
271
+ ensure_working_areas(samples.len);
272
+
273
  const size_t mirror_pad = WHISPER_N_FFT / 2;
274
  const size_t padded_size = samples.len + WHISPER_N_SAMPLES + WHISPER_N_FFT;
275
 
whisper-mel.hpp CHANGED
@@ -29,6 +29,6 @@ struct whisper_span {
29
 
30
  struct whisper_mel_calc {
31
  virtual ~whisper_mel_calc();
32
- virtual whisper_mel calculate(whisper_span<const float> samples, int n_threads) const = 0;
33
  static whisper_span<const float> hann_window();
34
  };
 
29
 
30
  struct whisper_mel_calc {
31
  virtual ~whisper_mel_calc();
32
+ virtual whisper_mel calculate(whisper_span<const float> samples, int n_threads) = 0;
33
  static whisper_span<const float> hann_window();
34
  };
whisper.cpp CHANGED
@@ -802,6 +802,7 @@ struct whisper_state {
802
 
803
  whisper_mel mel;
804
  whisper_mel_calc * mel_calc = nullptr;
 
805
 
806
  whisper_batch batch;
807
 
@@ -3079,7 +3080,7 @@ struct mel_calc_cpu : public whisper_mel_calc {
3079
  mel_calc_cpu(ggml_backend_t backend, const whisper_filters & filters) : m_backend(backend), m_filters(filters) {}
3080
 
3081
  // ref: https://github.com/openai/whisper/blob/main/whisper/audio.py#L110-L157
3082
- whisper_mel calculate(whisper_span<const float> ssamples, int n_threads) const override {
3083
  // Hann window
3084
  const float * hann = global_cache.hann_window;
3085
 
@@ -3721,6 +3722,8 @@ void whisper_free_state(struct whisper_state * state) {
3721
 
3722
  delete state->mel_calc;
3723
  state->mel_calc = nullptr;
 
 
3724
 
3725
  #ifdef WHISPER_USE_COREML
3726
  if (state->ctx_coreml != nullptr) {
@@ -3778,11 +3781,24 @@ void whisper_free_params(struct whisper_full_params * params) {
3778
  }
3779
  }
3780
 
3781
- int whisper_pcm_to_mel_with_state(struct whisper_context * /*ctx*/, struct whisper_state * state, const float * samples, int n_samples, int n_threads) {
3782
  const int64_t t_start_us = ggml_time_us();
3783
 
3784
  whisper_mel_free(state->mel);
3785
- state->mel = state->mel_calc->calculate({samples, n_samples}, n_threads);
 
 
 
 
 
 
 
 
 
 
 
 
 
3786
 
3787
  state->t_mel_us += ggml_time_us() - t_start_us;
3788
 
 
802
 
803
  whisper_mel mel;
804
  whisper_mel_calc * mel_calc = nullptr;
805
+ whisper_mel_calc * mel_calc_fallback = nullptr;
806
 
807
  whisper_batch batch;
808
 
 
3080
  mel_calc_cpu(ggml_backend_t backend, const whisper_filters & filters) : m_backend(backend), m_filters(filters) {}
3081
 
3082
  // ref: https://github.com/openai/whisper/blob/main/whisper/audio.py#L110-L157
3083
+ whisper_mel calculate(whisper_span<const float> ssamples, int n_threads) override {
3084
  // Hann window
3085
  const float * hann = global_cache.hann_window;
3086
 
 
3722
 
3723
  delete state->mel_calc;
3724
  state->mel_calc = nullptr;
3725
+ delete state->mel_calc_fallback;
3726
+ state->mel_calc_fallback = nullptr;
3727
 
3728
  #ifdef WHISPER_USE_COREML
3729
  if (state->ctx_coreml != nullptr) {
 
3781
  }
3782
  }
3783
 
3784
+ int whisper_pcm_to_mel_with_state(struct whisper_context * ctx, struct whisper_state * state, const float * samples, int n_samples, int n_threads) {
3785
  const int64_t t_start_us = ggml_time_us();
3786
 
3787
  whisper_mel_free(state->mel);
3788
+ if (n_samples <= 5 * 60 * WHISPER_SAMPLE_RATE) {
3789
+ // calculate mel spectrogram for lengths up to 5 minutes on the most optimal mel calculator
3790
+ state->mel = state->mel_calc->calculate({samples, n_samples}, n_threads);
3791
+ } else {
3792
+ // calcuate mel spectrogram for longer audios on the CPU
3793
+ // 1. gpu calculations may use hundreds of megabytes of memory for longer audios so we're being conservative
3794
+ // with our gpu demands
3795
+ // 2. the time to transcribe audios this long will be dominated by the decoding time, so the mel calculation
3796
+ // taking longer is not a major concern
3797
+ if (!state->mel_calc_fallback) {
3798
+ state->mel_calc_fallback = new mel_calc_cpu(state->backend, ctx->model.filters);
3799
+ }
3800
+ state->mel = state->mel_calc_fallback->calculate({samples, n_samples}, n_threads);
3801
+ }
3802
 
3803
  state->t_mel_us += ggml_time_us() - t_start_us;
3804