Skip to content

Commit 9b2fc65

Browse files
authored
Support dynamic decoder layers in canary model runtime (#3268)
1 parent 33554f7 commit 9b2fc65

2 files changed

Lines changed: 12 additions & 3 deletions

File tree

sherpa-onnx/csrc/offline-canary-model-meta-data.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ struct OfflineCanaryModelMetaData {
1414
int32_t vocab_size;
1515
int32_t subsampling_factor = 8;
1616
int32_t feat_dim = 120;
17+
int32_t num_decoder_layers = 6;
18+
int32_t decoder_hidden_size = 1024;
1719
std::string normalize_type;
1820
std::unordered_map<std::string, int32_t> lang2id;
1921
};

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

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,11 +117,13 @@ class OfflineCanaryModel::Impl {
117117
}
118118

119119
std::vector<Ort::Value> GetInitialDecoderStates() {
120-
std::array<int64_t, 3> shape{1, 0, 1024};
120+
int32_t num_layers = meta_.num_decoder_layers;
121+
int64_t hidden_size = meta_.decoder_hidden_size;
122+
std::array<int64_t, 3> shape{1, 0, hidden_size};
121123

122124
std::vector<Ort::Value> ans;
123-
ans.reserve(6);
124-
for (int32_t i = 0; i < 6; ++i) {
125+
ans.reserve(num_layers);
126+
for (int32_t i = 0; i < num_layers; ++i) {
125127
Ort::Value state = Ort::Value::CreateTensor<float>(
126128
Allocator(), shape.data(), shape.size());
127129

@@ -178,6 +180,11 @@ class OfflineCanaryModel::Impl {
178180
"normalize_type");
179181
SHERPA_ONNX_READ_META_DATA(meta_.subsampling_factor, "subsampling_factor");
180182
SHERPA_ONNX_READ_META_DATA(meta_.feat_dim, "feat_dim");
183+
184+
SHERPA_ONNX_READ_META_DATA_WITH_DEFAULT(meta_.num_decoder_layers,
185+
"num_decoder_layers", 6);
186+
SHERPA_ONNX_READ_META_DATA_WITH_DEFAULT(meta_.decoder_hidden_size,
187+
"decoder_hidden_size", 1024);
181188
}
182189

183190
void InitDecoder(void *model_data, size_t model_data_length) {

0 commit comments

Comments
 (0)