Skip to content

Commit ec5e631

Browse files
authored
Support running whisper large v3 with external data weight (k2-fsa#2807)
1 parent 7d1d227 commit ec5e631

1 file changed

Lines changed: 31 additions & 20 deletions

File tree

sherpa-onnx/csrc/offline-whisper-model.cc

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -37,31 +37,27 @@ class OfflineWhisperModel::Impl {
3737
env_(ORT_LOGGING_LEVEL_ERROR),
3838
sess_opts_(GetSessionOptions(config)),
3939
allocator_{} {
40-
{
41-
auto buf = ReadFile(config.whisper.encoder);
42-
InitEncoder(buf.data(), buf.size());
43-
}
40+
encoder_sess_ = std::make_unique<Ort::Session>(
41+
env_, SHERPA_ONNX_TO_ORT_PATH(config.whisper.encoder), sess_opts_);
42+
InitEncoder(nullptr, 0);
4443

45-
{
46-
auto buf = ReadFile(config.whisper.decoder);
47-
InitDecoder(buf.data(), buf.size());
48-
}
44+
decoder_sess_ = std::make_unique<Ort::Session>(
45+
env_, SHERPA_ONNX_TO_ORT_PATH(config.whisper.decoder), sess_opts_);
46+
InitDecoder(nullptr, 0);
4947
}
5048

5149
explicit Impl(const SpokenLanguageIdentificationConfig &config)
5250
: lid_config_(config),
5351
env_(ORT_LOGGING_LEVEL_ERROR),
5452
sess_opts_(GetSessionOptions(config)),
5553
allocator_{} {
56-
{
57-
auto buf = ReadFile(config.whisper.encoder);
58-
InitEncoder(buf.data(), buf.size());
59-
}
54+
encoder_sess_ = std::make_unique<Ort::Session>(
55+
env_, SHERPA_ONNX_TO_ORT_PATH(config.whisper.encoder), sess_opts_);
56+
InitEncoder(nullptr, 0);
6057

61-
{
62-
auto buf = ReadFile(config.whisper.decoder);
63-
InitDecoder(buf.data(), buf.size());
64-
}
58+
decoder_sess_ = std::make_unique<Ort::Session>(
59+
env_, SHERPA_ONNX_TO_ORT_PATH(config.whisper.decoder), sess_opts_);
60+
InitDecoder(nullptr, 0);
6561
}
6662

6763
template <typename Manager>
@@ -234,8 +230,16 @@ class OfflineWhisperModel::Impl {
234230

235231
private:
236232
void InitEncoder(void *model_data, size_t model_data_length) {
237-
encoder_sess_ = std::make_unique<Ort::Session>(
238-
env_, model_data, model_data_length, sess_opts_);
233+
if (model_data) {
234+
encoder_sess_ = std::make_unique<Ort::Session>(
235+
env_, model_data, model_data_length, sess_opts_);
236+
} else if (!encoder_sess_) {
237+
SHERPA_ONNX_LOGE(
238+
"Please pass buffer data or initialize encoder session outside of "
239+
"this "
240+
"function");
241+
SHERPA_ONNX_EXIT(-1);
242+
}
239243

240244
GetInputNames(encoder_sess_.get(), &encoder_input_names_,
241245
&encoder_input_names_ptr_);
@@ -293,8 +297,15 @@ class OfflineWhisperModel::Impl {
293297
}
294298

295299
void InitDecoder(void *model_data, size_t model_data_length) {
296-
decoder_sess_ = std::make_unique<Ort::Session>(
297-
env_, model_data, model_data_length, sess_opts_);
300+
if (model_data) {
301+
decoder_sess_ = std::make_unique<Ort::Session>(
302+
env_, model_data, model_data_length, sess_opts_);
303+
} else if (!decoder_sess_) {
304+
SHERPA_ONNX_LOGE(
305+
"Please pass buffer data or initialize decoder session outside of "
306+
"this function");
307+
SHERPA_ONNX_EXIT(-1);
308+
}
298309

299310
GetInputNames(decoder_sess_.get(), &decoder_input_names_,
300311
&decoder_input_names_ptr_);

0 commit comments

Comments
 (0)