-
Notifications
You must be signed in to change notification settings - Fork 132
Expand file tree
/
Copy pathtest_feature_extraction.cc
More file actions
449 lines (366 loc) · 18.2 KB
/
test_feature_extraction.cc
File metadata and controls
449 lines (366 loc) · 18.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
#include <vector>
#include <tuple>
#include <fstream>
#include <filesystem>
#include "gtest/gtest.h"
#include "operators/math/energy_stft_segmentation.hpp"
#include "ortx_cpp_helper.h"
#include "shared/api/speech_extractor.h"
using namespace ort_extensions;
TEST(ExtractorTest, TestWhisperFeatureExtraction) {
const char* audio_path[] = {"data/jfk.flac", "data/1272-141231-0002.wav", "data/1272-141231-0002.mp3"};
OrtxObjectPtr<OrtxRawAudios> raw_audios;
extError_t err = OrtxLoadAudios(raw_audios.ToBeAssigned(), audio_path, 3);
ASSERT_EQ(err, kOrtxOK);
OrtxObjectPtr<OrtxFeatureExtractor> feature_extractor(OrtxCreateSpeechFeatureExtractor,
"data/whisper/feature_extraction.json");
OrtxObjectPtr<OrtxTensorResult> result;
err = OrtxSpeechLogMel(feature_extractor.get(), raw_audios.get(), result.ToBeAssigned());
ASSERT_EQ(err, kOrtxOK);
OrtxObjectPtr<OrtxTensor> tensor;
err = OrtxTensorResultGetAt(result.get(), 0, tensor.ToBeAssigned());
ASSERT_EQ(err, kOrtxOK);
const float* data{};
const int64_t* shape{};
size_t num_dims;
err = OrtxGetTensorData(tensor.get(), reinterpret_cast<const void**>(&data), &shape, &num_dims);
ASSERT_EQ(err, kOrtxOK);
ASSERT_EQ(num_dims, 3);
ASSERT_EQ(shape[0], 3);
ASSERT_EQ(shape[1], 80);
ASSERT_EQ(shape[2], 3000);
}
TEST(ExtractorTest, TestPhi4AudioFeatureExtraction) {
const char* audio_path[] = {"data/jfk.flac", "data/1272-141231-0002.wav", "data/1272-141231-0002.mp3"};
OrtxObjectPtr<OrtxRawAudios> raw_audios;
extError_t err = OrtxLoadAudios(raw_audios.ToBeAssigned(), audio_path, 3);
ASSERT_EQ(err, kOrtxOK);
OrtxObjectPtr<OrtxFeatureExtractor> feature_extractor(OrtxCreateSpeechFeatureExtractor,
"data/models/phi-4/audio_feature_extraction.json");
OrtxObjectPtr<OrtxTensorResult> result;
err = OrtxFeatureExtraction(feature_extractor.get(), raw_audios.get(), result.ToBeAssigned());
ASSERT_EQ(err, kOrtxOK);
OrtxObjectPtr<OrtxTensor> tensor;
err = OrtxTensorResultGetAt(result.get(), 0, tensor.ToBeAssigned());
ASSERT_EQ(err, kOrtxOK);
const float* data{};
const int64_t* shape{};
size_t num_dims;
err = OrtxGetTensorData(tensor.get(), reinterpret_cast<const void**>(&data), &shape, &num_dims);
ASSERT_EQ(err, kOrtxOK);
ASSERT_EQ(std::vector<int64_t>(shape, shape + num_dims), std::vector<int64_t>({3, 1344, 80}));
tensor.reset();
const bool* audio_attention_mask{};
const int64_t* audio_mask_shape{};
size_t audio_mask_dims;
err = OrtxTensorResultGetAt(result.get(), 1, tensor.ToBeAssigned());
ASSERT_EQ(err, kOrtxOK);
err = OrtxGetTensorData(tensor.get(), reinterpret_cast<const void**>(&audio_attention_mask), &audio_mask_shape,
&audio_mask_dims);
ASSERT_EQ(err, kOrtxOK);
ASSERT_EQ(std::vector<int64_t>(audio_mask_shape, audio_mask_shape + audio_mask_dims),
std::vector<int64_t>({3, 1344}));
ASSERT_EQ(std::count(audio_attention_mask + 0 * 1344, audio_attention_mask + 1 * 1344, true), 1098);
ASSERT_EQ(std::count(audio_attention_mask + 1 * 1344, audio_attention_mask + 2 * 1344, true), 1332);
ASSERT_EQ(std::count(audio_attention_mask + 2 * 1344, audio_attention_mask + 3 * 1344, true), 1344);
tensor.reset();
err = OrtxTensorResultGetAt(result.get(), 2, tensor.ToBeAssigned());
ASSERT_EQ(err, kOrtxOK);
err = OrtxGetTensorData(tensor.get(), reinterpret_cast<const void**>(&data), &shape, &num_dims);
ASSERT_EQ(num_dims, 1);
ASSERT_EQ(std::vector<int64_t>(shape, shape + num_dims), std::vector<int64_t>({3}));
const float* actual_output = reinterpret_cast<const float*>(data);
ASSERT_FLOAT_EQ(actual_output[0], 138.0f);
ASSERT_FLOAT_EQ(actual_output[1], 167.0f);
ASSERT_FLOAT_EQ(actual_output[2], 168.0f);
}
TEST(ExtractorTest, TestPhi4AudioFeatureExtraction8k) {
const char* audio_path[] = {"data/models/phi-4/1272-128104-0004-8k.wav"};
OrtxObjectPtr<OrtxRawAudios> raw_audios;
extError_t err = OrtxLoadAudios(raw_audios.ToBeAssigned(), audio_path, 1);
ASSERT_EQ(err, kOrtxOK);
OrtxObjectPtr<OrtxFeatureExtractor> feature_extractor(OrtxCreateSpeechFeatureExtractor,
"data/models/phi-4/audio_feature_extraction.json");
OrtxObjectPtr<OrtxTensorResult> result;
err = OrtxFeatureExtraction(feature_extractor.get(), raw_audios.get(), result.ToBeAssigned());
ASSERT_EQ(err, kOrtxOK);
OrtxObjectPtr<OrtxTensor> tensor;
err = OrtxTensorResultGetAt(result.get(), 0, tensor.ToBeAssigned());
ASSERT_EQ(err, kOrtxOK);
const float* data{};
const int64_t* shape{};
size_t num_dims;
err = OrtxGetTensorData(tensor.get(), reinterpret_cast<const void**>(&data), &shape, &num_dims);
ASSERT_EQ(err, kOrtxOK);
ASSERT_EQ(std::vector<int64_t>(shape, shape + num_dims), std::vector<int64_t>({1, 2938, 80}));
tensor.reset();
const bool* audio_attention_mask{};
const int64_t* audio_mask_shape{};
size_t audio_mask_dims{};
err = OrtxTensorResultGetAt(result.get(), 1, tensor.ToBeAssigned());
ASSERT_EQ(err, kOrtxOK);
err = OrtxGetTensorData(tensor.get(), reinterpret_cast<const void**>(&audio_attention_mask), &audio_mask_shape,
&audio_mask_dims);
ASSERT_EQ(err, kOrtxOK);
ASSERT_EQ(std::vector<int64_t>(audio_mask_shape, audio_mask_shape + audio_mask_dims),
std::vector<int64_t>({1, 2938}));
const size_t num_elements = std::count(audio_attention_mask, audio_attention_mask + 2938, true);
ASSERT_EQ(num_elements, 2938);
tensor.reset();
err = OrtxTensorResultGetAt(result.get(), 2, tensor.ToBeAssigned());
ASSERT_EQ(err, kOrtxOK);
err = OrtxGetTensorData(tensor.get(), reinterpret_cast<const void**>(&data), &shape, &num_dims);
ASSERT_EQ(num_dims, 1);
ASSERT_EQ(std::vector<int64_t>(shape, shape + num_dims), std::vector<int64_t>({1}));
}
TEST(ExtractorTest, TestPhi4AudioOutput) {
const char* audio_path[] = {"data/1272-141231-0002.wav"};
OrtxObjectPtr<OrtxRawAudios> raw_audios;
extError_t err = OrtxLoadAudios(raw_audios.ToBeAssigned(), audio_path, 1);
ASSERT_EQ(err, kOrtxOK);
OrtxObjectPtr<OrtxFeatureExtractor> feature_extractor(OrtxCreateSpeechFeatureExtractor,
"data/models/phi-4/audio_feature_extraction.json");
OrtxObjectPtr<OrtxTensorResult> result;
err = OrtxFeatureExtraction(feature_extractor.get(), raw_audios.get(), result.ToBeAssigned());
ASSERT_EQ(err, kOrtxOK);
OrtxObjectPtr<OrtxTensor> tensor;
err = OrtxTensorResultGetAt(result.get(), 0, tensor.ToBeAssigned());
ASSERT_EQ(err, kOrtxOK);
const float* data{};
const int64_t* shape{};
size_t num_dims;
err = OrtxGetTensorData(tensor.get(), reinterpret_cast<const void**>(&data), &shape, &num_dims);
ASSERT_EQ(err, kOrtxOK);
ASSERT_EQ(std::vector<int64_t>(shape, shape + num_dims), std::vector<int64_t>({1, 1332, 80}));
// Dimensions
const size_t num_rows = shape[1];
const size_t num_columns = shape[2];
// Read the expected output from the file
std::filesystem::path expected_audio_embed_output_path = "data/models/phi-4/expected_output.txt";
std::ifstream expected_audio_embed_output(expected_audio_embed_output_path);
ASSERT_TRUE(expected_audio_embed_output.is_open());
// Define lambda for comparison
auto are_close = [](float a, float b, float rtol = 1e-03, float atol = 1e-02) -> bool {
return std::abs(a - b) <= atol || std::abs(a - b) <= rtol * std::abs(b);
};
size_t num_mismatched = 0;
size_t total_elements = num_rows * 10; // We only compare the first 10 columns
std::string line;
size_t row_idx = 0;
while (std::getline(expected_audio_embed_output, line) && row_idx < num_rows) {
std::stringstream ss(line); // Stringstream to parse each line
std::string value_str;
size_t col_idx = 0;
while (std::getline(ss, value_str, ',') && col_idx < 10) { // Only read the first 10 columns
float expected_value = std::stof(value_str); // Convert string to float
// Compare values
const float* row_start = data + (row_idx * num_columns);
if (!are_close(row_start[col_idx], expected_value)) {
num_mismatched++; // Count mismatches
std::cout << "Mismatch at (" << row_idx << "," << col_idx << "): "
<< "Expected: " << expected_value << ", Got: " << row_start[col_idx] << std::endl;
}
col_idx++;
}
row_idx++;
}
expected_audio_embed_output.close();
// Calculate the mismatch percentage
float mismatch_percentage = static_cast<float>(num_mismatched) / total_elements;
std::cout << "Mismatch percentage: " << mismatch_percentage * 100 << "%" << std::endl;
// We use a 2% mismatch threshold, same as that for Whisper
ASSERT_LT(mismatch_percentage, 0.02) << "Mismatch percentage exceeds 2% threshold!";
}
TEST(ExtractorTest, TestWhisperAudioOutput) {
const char* audio_path[] = {"data/1272-141231-0002.flac"};
OrtxObjectPtr<OrtxRawAudios> raw_audios;
extError_t err = OrtxLoadAudios(raw_audios.ToBeAssigned(), audio_path, 1);
ASSERT_EQ(err, kOrtxOK);
OrtxObjectPtr<OrtxFeatureExtractor> feature_extractor(OrtxCreateSpeechFeatureExtractor,
"data/whisper/feature_extraction.json");
OrtxObjectPtr<OrtxTensorResult> result;
err = OrtxFeatureExtraction(feature_extractor.get(), raw_audios.get(), result.ToBeAssigned());
ASSERT_EQ(err, kOrtxOK);
OrtxObjectPtr<OrtxTensor> tensor;
err = OrtxTensorResultGetAt(result.get(), 0, tensor.ToBeAssigned());
ASSERT_EQ(err, kOrtxOK);
const float* data{};
const int64_t* shape{};
size_t num_dims;
err = OrtxGetTensorData(tensor.get(), reinterpret_cast<const void**>(&data), &shape, &num_dims);
ASSERT_EQ(err, kOrtxOK);
ASSERT_EQ(std::vector<int64_t>(shape, shape + num_dims), std::vector<int64_t>({1, 80, 3000}));
// Dimensions
const size_t num_rows = shape[1];
const size_t num_columns = shape[2];
// Read the expected output from the file
std::filesystem::path expected_audio_embed_output_path = "data/whisper/expected_output.txt";
std::ifstream expected_audio_embed_output(expected_audio_embed_output_path);
ASSERT_TRUE(expected_audio_embed_output.is_open());
// Define lambda for comparison
auto are_close = [](float a, float b, float rtol = 1e-03, float atol = 1e-02) -> bool {
return std::abs(a - b) <= atol || std::abs(a - b) <= rtol * std::abs(b);
};
size_t num_mismatched = 0;
size_t total_elements = num_rows * 10; // We only compare the first 10 columns
std::string line;
size_t row_idx = 0;
while (std::getline(expected_audio_embed_output, line) && row_idx < num_rows) {
std::stringstream ss(line); // Stringstream to parse each line
std::string value_str;
size_t col_idx = 0;
while (std::getline(ss, value_str, ',') && col_idx < 10) { // Only read the first 10 columns
float expected_value = std::stof(value_str); // Convert string to float
// Compare values
const float* row_start = data + (row_idx * num_columns);
if (!are_close(row_start[col_idx], expected_value)) {
num_mismatched++; // Count mismatches
std::cout << "Mismatch at (" << row_idx << "," << col_idx << "): "
<< "Expected: " << expected_value << ", Got: " << row_start[col_idx] << std::endl;
}
col_idx++;
}
row_idx++;
}
expected_audio_embed_output.close();
// Calculate the mismatch percentage
float mismatch_percentage = static_cast<float>(num_mismatched) / total_elements;
std::cout << "Mismatch percentage: " << mismatch_percentage * 100 << "%" << std::endl;
// We use a 4% mismatch threshold currently, and aim to improve this further in the future
ASSERT_LT(mismatch_percentage, 0.04) << "Mismatch percentage exceeds 4% threshold!";
}
TEST(ExtractorTest, TestSplitSignalSegments) {
const int64_t sample_rate = 16000;
const int64_t num_samples = sample_rate * 2;
std::vector<float> pcm(num_samples);
const float freq = 440.0f;
for (int64_t i = 0; i < num_samples; ++i) {
pcm[i] = std::sin(2.0f * static_cast<float>(3.14159) * freq * (float)i / (float)sample_rate);
}
auto* alloc = &CppAllocator::Instance();
ortc::Tensor<float> input(alloc);
float* in_data = input.Allocate({1, num_samples});
std::memcpy(in_data, pcm.data(), num_samples * sizeof(float));
ortc::Tensor<int64_t> sr(alloc);
sr.Allocate({1})[0] = sample_rate;
ortc::Tensor<int64_t> frame_ms(alloc);
frame_ms.Allocate({1})[0] = 25;
ortc::Tensor<int64_t> hop_ms(alloc);
hop_ms.Allocate({1})[0] = 10;
ortc::Tensor<float> energy_threshold_db(alloc);
// Difference of 40 decibels can be a reasonable diff between voice and silence (or background noise)
energy_threshold_db.Allocate({1})[0] = -40.0f;
ortc::Tensor<int64_t> output(alloc);
extError_t err = OrtxSplitSignalSegments(
reinterpret_cast<OrtxTensor*>(&input), reinterpret_cast<OrtxTensor*>(&sr),
reinterpret_cast<OrtxTensor*>(&frame_ms), reinterpret_cast<OrtxTensor*>(&hop_ms),
reinterpret_cast<OrtxTensor*>(&energy_threshold_db), reinterpret_cast<OrtxTensor*>(&output));
ASSERT_EQ(err, kOrtxOK);
const auto& out_shape = output.Shape();
ASSERT_EQ(out_shape.size(), 2u);
ASSERT_EQ(out_shape[1], 2);
ASSERT_EQ(out_shape[0], 53);
ortc::Tensor<int64_t> merge_gap(alloc);
merge_gap.Allocate({1})[0] = 50;
ortc::Tensor<int64_t> merged_segments(alloc);
err = OrtxMergeSignalSegments(reinterpret_cast<OrtxTensor*>(&output), reinterpret_cast<OrtxTensor*>(&merge_gap),
reinterpret_cast<OrtxTensor*>(&merged_segments));
ASSERT_EQ(err, kOrtxOK);
const auto& merged_shape = merged_segments.Shape();
ASSERT_EQ(merged_shape.size(), 2u);
ASSERT_EQ(merged_shape[1], 2);
ASSERT_EQ(merged_shape[0], 4);
}
TEST(ExtractorTest, TestGemma4AudioFeatureExtraction) {
// Use existing test audio files to verify the Gemma 4 USM-style log-mel pipeline:
// AudioDecoder -> Gemma4LogMel
const char* audio_path[] = {"data/jfk.flac"};
OrtxObjectPtr<OrtxRawAudios> raw_audios;
extError_t err = OrtxLoadAudios(raw_audios.ToBeAssigned(), audio_path, 1);
ASSERT_EQ(err, kOrtxOK);
OrtxObjectPtr<OrtxFeatureExtractor> feature_extractor(OrtxCreateSpeechFeatureExtractor,
"data/models/gemma-4/audio_feature_extraction.json");
OrtxObjectPtr<OrtxTensorResult> result;
err = OrtxFeatureExtraction(feature_extractor.get(), raw_audios.get(), result.ToBeAssigned());
ASSERT_EQ(err, kOrtxOK);
// Output 0: log-mel spectrogram — float (batch, num_frames, 128)
OrtxObjectPtr<OrtxTensor> tensor;
err = OrtxTensorResultGetAt(result.get(), 0, tensor.ToBeAssigned());
ASSERT_EQ(err, kOrtxOK);
const float* data{};
const int64_t* shape{};
size_t num_dims;
err = OrtxGetTensorData(tensor.get(), reinterpret_cast<const void**>(&data), &shape, &num_dims);
ASSERT_EQ(err, kOrtxOK);
ASSERT_EQ(num_dims, 3ULL); // (batch, num_frames, feature_size)
ASSERT_EQ(shape[0], 1); // single audio
ASSERT_EQ(shape[2], 128); // 128 mel bins
EXPECT_GT(shape[1], 0); // should have some frames
// Verify values are finite (not NaN/Inf).
for (int64_t i = 0; i < std::min<int64_t>(shape[1] * 128, 5000); ++i) {
ASSERT_TRUE(std::isfinite(data[i])) << "log-mel value at index " << i << " is not finite";
}
// Verify log-mel values are in a reasonable range.
// With mel_floor=0.001, log(0.001) ~ -6.9078. Values should be >= ~-7
// and typically < ~5 for speech audio.
const float* frame0 = data;
for (int i = 0; i < 10; ++i) {
EXPECT_GE(frame0[i], -7.5f) << "Frame 0 bin " << i << " too low";
EXPECT_LE(frame0[i], 5.0f) << "Frame 0 bin " << i << " too high";
}
// The first mel bin of silent/padding frames should be close to log(mel_floor)
// = log(0.001) ~ -6.9078.
EXPECT_NEAR(frame0[0], -6.9078f, 0.05f)
<< "Frame 0 bin 0 should be near log(0.001) for semicausal pad region";
// Output 1: attention mask — bool (batch, num_frames)
err = OrtxTensorResultGetAt(result.get(), 1, tensor.ToBeAssigned());
ASSERT_EQ(err, kOrtxOK);
const bool* mask_data{};
const int64_t* mask_shape{};
size_t mask_dims;
err = OrtxGetTensorData(tensor.get(), reinterpret_cast<const void**>(&mask_data), &mask_shape, &mask_dims);
ASSERT_EQ(err, kOrtxOK);
ASSERT_EQ(mask_dims, 2ULL); // (batch, num_frames)
ASSERT_EQ(mask_shape[0], 1);
ASSERT_EQ(mask_shape[1], shape[1]); // same frame count
// For JFK audio (not truncated), all frames except those from the semicausal pad
// should be valid. At least some frames should be true.
int true_count = std::count(mask_data, mask_data + mask_shape[1], true);
EXPECT_GT(true_count, 0) << "Expected at least some valid frames";
}
TEST(ExtractorTest, TestGemma4AudioFeatureExtractionMultiFile) {
// Verify batched processing with multiple audio files.
const char* audio_path[] = {"data/jfk.flac", "data/1272-141231-0002.wav"};
OrtxObjectPtr<OrtxRawAudios> raw_audios;
extError_t err = OrtxLoadAudios(raw_audios.ToBeAssigned(), audio_path, 2);
ASSERT_EQ(err, kOrtxOK);
OrtxObjectPtr<OrtxFeatureExtractor> feature_extractor(OrtxCreateSpeechFeatureExtractor,
"data/models/gemma-4/audio_feature_extraction.json");
OrtxObjectPtr<OrtxTensorResult> result;
err = OrtxFeatureExtraction(feature_extractor.get(), raw_audios.get(), result.ToBeAssigned());
ASSERT_EQ(err, kOrtxOK);
// log-mel: batch dim should be 2
OrtxObjectPtr<OrtxTensor> tensor;
err = OrtxTensorResultGetAt(result.get(), 0, tensor.ToBeAssigned());
ASSERT_EQ(err, kOrtxOK);
const float* data{};
const int64_t* shape{};
size_t num_dims;
err = OrtxGetTensorData(tensor.get(), reinterpret_cast<const void**>(&data), &shape, &num_dims);
ASSERT_EQ(err, kOrtxOK);
ASSERT_EQ(num_dims, 3ULL);
ASSERT_EQ(shape[0], 2); // batch of 2
ASSERT_EQ(shape[2], 128);
// mask: batch dim should be 2
err = OrtxTensorResultGetAt(result.get(), 1, tensor.ToBeAssigned());
ASSERT_EQ(err, kOrtxOK);
const bool* mask_data{};
const int64_t* mask_shape{};
size_t mask_dims;
err = OrtxGetTensorData(tensor.get(), reinterpret_cast<const void**>(&mask_data), &mask_shape, &mask_dims);
ASSERT_EQ(err, kOrtxOK);
ASSERT_EQ(mask_dims, 2ULL);
ASSERT_EQ(mask_shape[0], 2);
}