Skip to content

Commit 3375aff

Browse files
committed
Support Zipformer transducer ASR with whisper features.
1 parent 54bf373 commit 3375aff

5 files changed

Lines changed: 31 additions & 0 deletions

File tree

sherpa-onnx/csrc/features.cc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,10 +131,13 @@ class FeatureExtractor::Impl {
131131
std::lock_guard<std::mutex> lock(mutex_);
132132
if (fbank_) {
133133
fbank_->InputFinished();
134+
return;
134135
} else if (whisper_fbank_) {
135136
whisper_fbank_->InputFinished();
137+
return;
136138
} else if (mfcc_) {
137139
mfcc_->InputFinished();
140+
return;
138141
}
139142

140143
SHERPA_ONNX_LOGE("unreachable code");

sherpa-onnx/csrc/online-recognizer-transducer-impl.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
#include "sherpa-onnx/csrc/file-utils.h"
1818
#include "sherpa-onnx/csrc/macros.h"
19+
#include "sherpa-onnx/csrc/offline-whisper-model.h"
1920
#include "sherpa-onnx/csrc/online-lm.h"
2021
#include "sherpa-onnx/csrc/online-recognizer-impl.h"
2122
#include "sherpa-onnx/csrc/online-recognizer.h"
@@ -133,6 +134,10 @@ class OnlineRecognizerTransducerImpl : public OnlineRecognizerImpl {
133134
config.decoding_method.c_str());
134135
exit(-1);
135136
}
137+
138+
if (model_->UseWhisperFeature()) {
139+
config_.feat_config.is_whisper = true;
140+
}
136141
}
137142

138143
template <typename Manager>
@@ -182,6 +187,10 @@ class OnlineRecognizerTransducerImpl : public OnlineRecognizerImpl {
182187
config.decoding_method.c_str());
183188
exit(-1);
184189
}
190+
191+
if (model_->UseWhisperFeature()) {
192+
config_.feat_config.is_whisper = true;
193+
}
185194
}
186195

187196
std::unique_ptr<OnlineStream> CreateStream() const override {
@@ -292,6 +301,11 @@ class OnlineRecognizerTransducerImpl : public OnlineRecognizerImpl {
292301
std::vector<float> features =
293302
ss[i]->GetFrames(num_processed_frames, chunk_size);
294303

304+
if (config_.feat_config.is_whisper) {
305+
OfflineWhisperModel::NormalizeFeatures(features.data(), chunk_size,
306+
feature_dim);
307+
}
308+
295309
// Question: should num_processed_frames include chunk_shift?
296310
ss[i]->GetNumProcessedFrames() += chunk_shift;
297311

sherpa-onnx/csrc/online-transducer-model.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,8 @@ class OnlineTransducerModel {
132132

133133
virtual int32_t SubsamplingFactor() const { return 4; }
134134

135+
virtual bool UseWhisperFeature() const { return false; }
136+
135137
virtual OrtAllocator *Allocator() = 0;
136138

137139
Ort::Value BuildDecoderInput(

sherpa-onnx/csrc/online-zipformer2-transducer-model.cc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,12 @@ void OnlineZipformer2TransducerModel::InitEncoder(void *model_data,
120120
SHERPA_ONNX_READ_META_DATA(T_, "T");
121121
SHERPA_ONNX_READ_META_DATA(decode_chunk_len_, "decode_chunk_len");
122122

123+
std::string feature_type;
124+
SHERPA_ONNX_READ_META_DATA_STR_WITH_DEFAULT(feature_type, "feature", "");
125+
if (feature_type == "whisper") {
126+
use_whisper_feature_ = true;
127+
}
128+
123129
if (config_.debug) {
124130
auto print = [](const std::vector<int32_t> &v, const char *name) {
125131
std::ostringstream os;

sherpa-onnx/csrc/online-zipformer2-transducer-model.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ class OnlineZipformer2TransducerModel : public OnlineTransducerModel {
5252
int32_t VocabSize() const override { return vocab_size_; }
5353
OrtAllocator *Allocator() override { return allocator_; }
5454

55+
bool UseWhisperFeature() const override { return use_whisper_feature_; }
56+
5557
private:
5658
void InitEncoder(void *model_data, size_t model_data_length);
5759
void InitDecoder(void *model_data, size_t model_data_length);
@@ -103,6 +105,10 @@ class OnlineZipformer2TransducerModel : public OnlineTransducerModel {
103105
int32_t context_size_ = 0;
104106
int32_t vocab_size_ = 0;
105107
int32_t feature_dim_ = 80;
108+
109+
// for models from
110+
// https://github.com/k2-fsa/icefall/blob/master/egs/multi_zh-hans/ASR/RESULTS.md#streaming-with-ctc-head
111+
bool use_whisper_feature_ = false;
106112
};
107113

108114
} // namespace sherpa_onnx

0 commit comments

Comments
 (0)