danbev commited on
Commit
2ccaffe
·
unverified ·
1 Parent(s): 3f6a806

ruby : fix test failures in test_whisper (#2955)

Browse files

* bindings.ruby : fix test failures in test_whisper

This commit updates the parallel tests to use 2 processors instead of
the number of processors on the system. It also comments out the setting
of the log callback to an empty lambda as this causes a segfault when
enabled.

The motivation for the change to the number of processors is that if one
has a large number of processors, for example I have 16 on the machine I
used to test this, this would cause the following warning to be printed:
```console
whisper_full_with_state: input is too short - 680 ms < 1000 ms. consider padding the input audio with silence
```

This is logged from:
```c++
int whisper_full_with_state(
struct whisper_context * ctx,
struct whisper_state * state,
struct whisper_full_params params,
const float * samples,
int n_samples) {
...
if (seek_end < seek_start + 100) {
WHISPER_LOG_WARN("%s: input is too short - %d ms < 1000 ms. consider padding the input audio with silence\n", __func__, (seek_end - seek_start)*10);
return 0;
}
```
This will return early and there will be segment callbacks to be invoked
which in turn will cause the tests to fail.

* bindings.ruby : fix warnings in tests

This commit fixes the following warnings in the Ruby tests:
```console
/whisper/bindings/ruby/tests/test_segment.rb:52:
warning: ambiguity between regexp and two divisions:
wrap regexp in parentheses or add a space after `/' operator
```
And also adds a '_' prefix to some unused variables to avoid warnings.

* bindings.ruby : enable Wisper.log_set in tests

The commit reverts the commenting out of the Whisper.log_set call in
the test_whisper.rb tests.

I'm no longer getting segfaults when running the tests with this
which was the case earlier. One theory could be that I rebased this to
include the latest ggml sync to master to make sure things still worked.
With the latest changes in ggml, I can't reproduce the segfaults.

bindings/ruby/tests/test_callback.rb CHANGED
@@ -25,7 +25,7 @@ class TestCallback < TestBase
25
  assert start_time >= 0
26
  assert_kind_of Integer, end_time
27
  assert end_time > 0
28
- assert_match /ask not what your country can do for you, ask what you can do for your country/, text if i_segment == 0
29
  end
30
  }
31
 
@@ -145,9 +145,9 @@ class TestCallback < TestBase
145
 
146
  def test_abort_on
147
  do_abort = false
148
- aborted_from_callback = false
149
  @params.on_new_segment do |segment|
150
- do_abort = true if segment.text.match? /ask/
151
  end
152
  i = 0
153
  @params.abort_on do
 
25
  assert start_time >= 0
26
  assert_kind_of Integer, end_time
27
  assert end_time > 0
28
+ assert_match(/ask not what your country can do for you, ask what you can do for your country/, text) if i_segment == 0
29
  end
30
  }
31
 
 
145
 
146
  def test_abort_on
147
  do_abort = false
148
+ _aborted_from_callback = false
149
  @params.on_new_segment do |segment|
150
+ do_abort = true if segment.text.match?(/ask/)
151
  end
152
  i = 0
153
  @params.abort_on do
bindings/ruby/tests/test_error.rb CHANGED
@@ -4,7 +4,7 @@ class TestError < TestBase
4
  def test_error
5
  error = Whisper::Error.new(-2)
6
  assert_equal "failed to compute log mel spectrogram", error.message
7
- assert_equal -2, error.code
8
  end
9
 
10
  def test_unknown_error
@@ -14,7 +14,7 @@ class TestError < TestBase
14
 
15
  def test_non_int_code
16
  assert_raise TypeError do
17
- error = Whisper::Error.new("non int")
18
  end
19
  end
20
  end
 
4
  def test_error
5
  error = Whisper::Error.new(-2)
6
  assert_equal "failed to compute log mel spectrogram", error.message
7
+ assert_equal(-2, error.code)
8
  end
9
 
10
  def test_unknown_error
 
14
 
15
  def test_non_int_code
16
  assert_raise TypeError do
17
+ _error = Whisper::Error.new("non int")
18
  end
19
  end
20
  end
bindings/ruby/tests/test_params.rb CHANGED
@@ -162,7 +162,7 @@ class TestParams < TestBase
162
  end
163
 
164
  def test_length_penalty
165
- assert_equal -1.0, @params.length_penalty
166
  @params.length_penalty = 0.5
167
  assert_equal 0.5, @params.length_penalty
168
  end
@@ -180,9 +180,9 @@ class TestParams < TestBase
180
  end
181
 
182
  def test_logprob_thold
183
- assert_in_delta -1.0, @params.logprob_thold
184
  @params.logprob_thold = -0.5
185
- assert_in_delta -0.5, @params.logprob_thold
186
  end
187
 
188
  def test_no_speech_thold
 
162
  end
163
 
164
  def test_length_penalty
165
+ assert_equal(-1.0, @params.length_penalty)
166
  @params.length_penalty = 0.5
167
  assert_equal 0.5, @params.length_penalty
168
  end
 
180
  end
181
 
182
  def test_logprob_thold
183
+ assert_in_delta(-1.0, @params.logprob_thold)
184
  @params.logprob_thold = -0.5
185
+ assert_in_delta(-0.5, @params.logprob_thold)
186
  end
187
 
188
  def test_no_speech_thold
bindings/ruby/tests/test_segment.rb CHANGED
@@ -49,13 +49,13 @@ class TestSegment < TestBase
49
  if index == 0
50
  seg = segment
51
  assert_equal 0, segment.start_time
52
- assert_match /ask not what your country can do for you, ask what you can do for your country/, segment.text
53
  end
54
  index += 1
55
  end
56
  whisper.transcribe(AUDIO, params)
57
  assert_equal 0, seg.start_time
58
- assert_match /ask not what your country can do for you, ask what you can do for your country/, seg.text
59
  end
60
 
61
  def test_on_new_segment_twice
 
49
  if index == 0
50
  seg = segment
51
  assert_equal 0, segment.start_time
52
+ assert_match(/ask not what your country can do for you, ask what you can do for your country/, segment.text)
53
  end
54
  index += 1
55
  end
56
  whisper.transcribe(AUDIO, params)
57
  assert_equal 0, seg.start_time
58
+ assert_match(/ask not what your country can do for you, ask what you can do for your country/, seg.text)
59
  end
60
 
61
  def test_on_new_segment_twice
bindings/ruby/tests/test_whisper.rb CHANGED
@@ -16,7 +16,7 @@ class TestWhisper < TestBase
16
  params.print_timestamps = false
17
 
18
  @whisper.transcribe(AUDIO, params) {|text|
19
- assert_match /ask not what your country can do for you, ask what you can do for your country/, text
20
  }
21
  end
22
 
@@ -32,7 +32,7 @@ class TestWhisper < TestBase
32
  def test_full_get_segment
33
  segment = whisper.full_get_segment(0)
34
  assert_equal 0, segment.start_time
35
- assert_match /ask not what your country can do for you, ask what you can do for your country/, segment.text
36
  end
37
 
38
  def test_full_get_segment_t0
@@ -59,7 +59,7 @@ class TestWhisper < TestBase
59
  end
60
 
61
  def test_full_get_segment_text
62
- assert_match /ask not what your country can do for you, ask what you can do for your country/, whisper.full_get_segment_text(0)
63
  end
64
 
65
  def test_full_get_segment_no_speech_prob
@@ -134,14 +134,14 @@ class TestWhisper < TestBase
134
  @whisper.full(@params, @samples, @samples.length)
135
 
136
  assert_equal 1, @whisper.full_n_segments
137
- assert_match /ask not what your country can do for you, ask what you can do for your country/, @whisper.each_segment.first.text
138
  end
139
 
140
  def test_full_without_length
141
  @whisper.full(@params, @samples)
142
 
143
  assert_equal 1, @whisper.full_n_segments
144
- assert_match /ask not what your country can do for you, ask what you can do for your country/, @whisper.each_segment.first.text
145
  end
146
 
147
  def test_full_enumerator
@@ -149,7 +149,7 @@ class TestWhisper < TestBase
149
  @whisper.full(@params, samples, @samples.length)
150
 
151
  assert_equal 1, @whisper.full_n_segments
152
- assert_match /ask not what your country can do for you, ask what you can do for your country/, @whisper.each_segment.first.text
153
  end
154
 
155
  def test_full_enumerator_without_length
@@ -171,26 +171,28 @@ class TestWhisper < TestBase
171
  @whisper.full(@params, samples)
172
 
173
  assert_equal 1, @whisper.full_n_segments
174
- assert_match /ask not what your country can do for you, ask what you can do for your country/, @whisper.each_segment.first.text
175
  end
176
 
177
  def test_full_parallel
178
- @whisper.full_parallel(@params, @samples, @samples.length, Etc.nprocessors)
 
179
 
180
- assert_equal Etc.nprocessors, @whisper.full_n_segments
181
  text = @whisper.each_segment.collect(&:text).join
182
- assert_match /ask what you can do/i, text
183
- assert_match /for your country/i, text
184
  end
185
 
186
  def test_full_parallel_with_memory_view
 
187
  samples = JFKReader.new(AUDIO)
188
- @whisper.full_parallel(@params, samples, nil, Etc.nprocessors)
189
 
190
- assert_equal Etc.nprocessors, @whisper.full_n_segments
191
  text = @whisper.each_segment.collect(&:text).join
192
- assert_match /ask what you can do/i, text
193
- assert_match /for your country/i, text
194
  end
195
 
196
  def test_full_parallel_without_length_and_n_processors
@@ -198,17 +200,18 @@ class TestWhisper < TestBase
198
 
199
  assert_equal 1, @whisper.full_n_segments
200
  text = @whisper.each_segment.collect(&:text).join
201
- assert_match /ask what you can do/i, text
202
- assert_match /for your country/i, text
203
  end
204
 
205
  def test_full_parallel_without_length
206
- @whisper.full_parallel(@params, @samples, nil, Etc.nprocessors)
 
207
 
208
- assert_equal Etc.nprocessors, @whisper.full_n_segments
209
  text = @whisper.each_segment.collect(&:text).join
210
- assert_match /ask what you can do/i, text
211
- assert_match /for your country/i, text
212
  end
213
 
214
  def test_full_parallel_without_n_processors
@@ -216,8 +219,8 @@ class TestWhisper < TestBase
216
 
217
  assert_equal 1, @whisper.full_n_segments
218
  text = @whisper.each_segment.collect(&:text).join
219
- assert_match /ask what you can do/i, text
220
- assert_match /for your country/i, text
221
  end
222
  end
223
  end
 
16
  params.print_timestamps = false
17
 
18
  @whisper.transcribe(AUDIO, params) {|text|
19
+ assert_match(/ask not what your country can do for you, ask what you can do for your country/, text)
20
  }
21
  end
22
 
 
32
  def test_full_get_segment
33
  segment = whisper.full_get_segment(0)
34
  assert_equal 0, segment.start_time
35
+ assert_match(/ask not what your country can do for you, ask what you can do for your country/, segment.text)
36
  end
37
 
38
  def test_full_get_segment_t0
 
59
  end
60
 
61
  def test_full_get_segment_text
62
+ assert_match(/ask not what your country can do for you, ask what you can do for your country/, whisper.full_get_segment_text(0))
63
  end
64
 
65
  def test_full_get_segment_no_speech_prob
 
134
  @whisper.full(@params, @samples, @samples.length)
135
 
136
  assert_equal 1, @whisper.full_n_segments
137
+ assert_match(/ask not what your country can do for you, ask what you can do for your country/, @whisper.each_segment.first.text)
138
  end
139
 
140
  def test_full_without_length
141
  @whisper.full(@params, @samples)
142
 
143
  assert_equal 1, @whisper.full_n_segments
144
+ assert_match(/ask not what your country can do for you, ask what you can do for your country/, @whisper.each_segment.first.text)
145
  end
146
 
147
  def test_full_enumerator
 
149
  @whisper.full(@params, samples, @samples.length)
150
 
151
  assert_equal 1, @whisper.full_n_segments
152
+ assert_match(/ask not what your country can do for you, ask what you can do for your country/, @whisper.each_segment.first.text)
153
  end
154
 
155
  def test_full_enumerator_without_length
 
171
  @whisper.full(@params, samples)
172
 
173
  assert_equal 1, @whisper.full_n_segments
174
+ assert_match(/ask not what your country can do for you, ask what you can do for your country/, @whisper.each_segment.first.text)
175
  end
176
 
177
  def test_full_parallel
178
+ nprocessors = 2
179
+ @whisper.full_parallel(@params, @samples, @samples.length, nprocessors)
180
 
181
+ assert_equal nprocessors, @whisper.full_n_segments
182
  text = @whisper.each_segment.collect(&:text).join
183
+ assert_match(/ask what you can do/i, text)
184
+ assert_match(/for your country/i, text)
185
  end
186
 
187
  def test_full_parallel_with_memory_view
188
+ nprocessors = 2
189
  samples = JFKReader.new(AUDIO)
190
+ @whisper.full_parallel(@params, samples, nil, nprocessors)
191
 
192
+ assert_equal nprocessors, @whisper.full_n_segments
193
  text = @whisper.each_segment.collect(&:text).join
194
+ assert_match(/ask what you can do/i, text)
195
+ assert_match(/for your country/i, text)
196
  end
197
 
198
  def test_full_parallel_without_length_and_n_processors
 
200
 
201
  assert_equal 1, @whisper.full_n_segments
202
  text = @whisper.each_segment.collect(&:text).join
203
+ assert_match(/ask what you can do/i, text)
204
+ assert_match(/for your country/i, text)
205
  end
206
 
207
  def test_full_parallel_without_length
208
+ nprocessors = 2
209
+ @whisper.full_parallel(@params, @samples, nil, nprocessors)
210
 
211
+ assert_equal nprocessors, @whisper.full_n_segments
212
  text = @whisper.each_segment.collect(&:text).join
213
+ assert_match(/ask what you can do/i, text)
214
+ assert_match(/for your country/i, text)
215
  end
216
 
217
  def test_full_parallel_without_n_processors
 
219
 
220
  assert_equal 1, @whisper.full_n_segments
221
  text = @whisper.each_segment.collect(&:text).join
222
+ assert_match(/ask what you can do/i, text)
223
+ assert_match(/for your country/i, text)
224
  end
225
  end
226
  end