Skip to content

Commit 46d6e0a

Browse files
authored
ruby : fix test failures in test_whisper (#2955)
* 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.
1 parent 1279f0d commit 46d6e0a

File tree

5 files changed

+36
-33
lines changed

5 files changed

+36
-33
lines changed

bindings/ruby/tests/test_callback.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def test_new_segment_callback
2525
assert start_time >= 0
2626
assert_kind_of Integer, end_time
2727
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
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
2929
end
3030
}
3131

@@ -145,9 +145,9 @@ def test_abort_callback_user_data
145145

146146
def test_abort_on
147147
do_abort = false
148-
aborted_from_callback = false
148+
_aborted_from_callback = false
149149
@params.on_new_segment do |segment|
150-
do_abort = true if segment.text.match? /ask/
150+
do_abort = true if segment.text.match?(/ask/)
151151
end
152152
i = 0
153153
@params.abort_on do

bindings/ruby/tests/test_error.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ class TestError < TestBase
44
def test_error
55
error = Whisper::Error.new(-2)
66
assert_equal "failed to compute log mel spectrogram", error.message
7-
assert_equal -2, error.code
7+
assert_equal(-2, error.code)
88
end
99

1010
def test_unknown_error
@@ -14,7 +14,7 @@ def test_unknown_error
1414

1515
def test_non_int_code
1616
assert_raise TypeError do
17-
error = Whisper::Error.new("non int")
17+
_error = Whisper::Error.new("non int")
1818
end
1919
end
2020
end

bindings/ruby/tests/test_params.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ def test_max_initial_ts
162162
end
163163

164164
def test_length_penalty
165-
assert_equal -1.0, @params.length_penalty
165+
assert_equal(-1.0, @params.length_penalty)
166166
@params.length_penalty = 0.5
167167
assert_equal 0.5, @params.length_penalty
168168
end
@@ -180,9 +180,9 @@ def test_entropy_thold
180180
end
181181

182182
def test_logprob_thold
183-
assert_in_delta -1.0, @params.logprob_thold
183+
assert_in_delta(-1.0, @params.logprob_thold)
184184
@params.logprob_thold = -0.5
185-
assert_in_delta -0.5, @params.logprob_thold
185+
assert_in_delta(-0.5, @params.logprob_thold)
186186
end
187187

188188
def test_no_speech_thold

bindings/ruby/tests/test_segment.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,13 @@ def test_on_new_segment
4949
if index == 0
5050
seg = segment
5151
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
52+
assert_match(/ask not what your country can do for you, ask what you can do for your country/, segment.text)
5353
end
5454
index += 1
5555
end
5656
whisper.transcribe(AUDIO, params)
5757
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
58+
assert_match(/ask not what your country can do for you, ask what you can do for your country/, seg.text)
5959
end
6060

6161
def test_on_new_segment_twice

bindings/ruby/tests/test_whisper.rb

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ def test_whisper
1616
params.print_timestamps = false
1717

1818
@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
19+
assert_match(/ask not what your country can do for you, ask what you can do for your country/, text)
2020
}
2121
end
2222

@@ -32,7 +32,7 @@ def test_full_lang_id
3232
def test_full_get_segment
3333
segment = whisper.full_get_segment(0)
3434
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
35+
assert_match(/ask not what your country can do for you, ask what you can do for your country/, segment.text)
3636
end
3737

3838
def test_full_get_segment_t0
@@ -59,7 +59,7 @@ def test_full_get_segment_speaker_turn_next
5959
end
6060

6161
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)
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))
6363
end
6464

6565
def test_full_get_segment_no_speech_prob
@@ -134,22 +134,22 @@ def test_full
134134
@whisper.full(@params, @samples, @samples.length)
135135

136136
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
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)
138138
end
139139

140140
def test_full_without_length
141141
@whisper.full(@params, @samples)
142142

143143
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
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)
145145
end
146146

147147
def test_full_enumerator
148148
samples = @samples.each
149149
@whisper.full(@params, samples, @samples.length)
150150

151151
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
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)
153153
end
154154

155155
def test_full_enumerator_without_length
@@ -171,53 +171,56 @@ def test_full_with_memory_view
171171
@whisper.full(@params, samples)
172172

173173
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
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)
175175
end
176176

177177
def test_full_parallel
178-
@whisper.full_parallel(@params, @samples, @samples.length, Etc.nprocessors)
178+
nprocessors = 2
179+
@whisper.full_parallel(@params, @samples, @samples.length, nprocessors)
179180

180-
assert_equal Etc.nprocessors, @whisper.full_n_segments
181+
assert_equal nprocessors, @whisper.full_n_segments
181182
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
183+
assert_match(/ask what you can do/i, text)
184+
assert_match(/for your country/i, text)
184185
end
185186

186187
def test_full_parallel_with_memory_view
188+
nprocessors = 2
187189
samples = JFKReader.new(AUDIO)
188-
@whisper.full_parallel(@params, samples, nil, Etc.nprocessors)
190+
@whisper.full_parallel(@params, samples, nil, nprocessors)
189191

190-
assert_equal Etc.nprocessors, @whisper.full_n_segments
192+
assert_equal nprocessors, @whisper.full_n_segments
191193
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+
assert_match(/ask what you can do/i, text)
195+
assert_match(/for your country/i, text)
194196
end
195197

196198
def test_full_parallel_without_length_and_n_processors
197199
@whisper.full_parallel(@params, @samples)
198200

199201
assert_equal 1, @whisper.full_n_segments
200202
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+
assert_match(/ask what you can do/i, text)
204+
assert_match(/for your country/i, text)
203205
end
204206

205207
def test_full_parallel_without_length
206-
@whisper.full_parallel(@params, @samples, nil, Etc.nprocessors)
208+
nprocessors = 2
209+
@whisper.full_parallel(@params, @samples, nil, nprocessors)
207210

208-
assert_equal Etc.nprocessors, @whisper.full_n_segments
211+
assert_equal nprocessors, @whisper.full_n_segments
209212
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
213+
assert_match(/ask what you can do/i, text)
214+
assert_match(/for your country/i, text)
212215
end
213216

214217
def test_full_parallel_without_n_processors
215218
@whisper.full_parallel(@params, @samples, @samples.length)
216219

217220
assert_equal 1, @whisper.full_n_segments
218221
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
222+
assert_match(/ask what you can do/i, text)
223+
assert_match(/for your country/i, text)
221224
end
222225
end
223226
end

0 commit comments

Comments
 (0)