This repository was archived by the owner on Jan 24, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathTextDecoder.cpp
More file actions
319 lines (254 loc) · 12.1 KB
/
TextDecoder.cpp
File metadata and controls
319 lines (254 loc) · 12.1 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
#include "TextDecoder.hpp"
#include <fstream>
#include <iostream>
#include <string>
#include <unordered_set>
#include <vector>
#include "backend_class.hpp"
#include "tensorflow/lite/schema/schema_generated.h"
using namespace WhisperKit;
// 'Monolithic KV Cache' ~ corresponds to the QUIC exported Whisper models
// TODO: make a metadata class for the model signature and tendor indices,
// pass to the subclasses so we don't have to reopen the file outside of
// loading the actual tflite model. TFLIte APIs require signature runner
// to get this information from interpreter, which is not available if the
// model doesn't have a signature.
bool is_exact_match_for_monolithic_kv_cache(const tflite::Model* model) {
const std::unordered_set<std::string> expected_input_names = {
"x", "index", "k_cache_cross", "v_cache_cross", "k_cache_self", "v_cache_self"};
const std::unordered_set<std::string> expected_output_names = {"logits", "k_cache", "v_cache"};
const auto* subgraph = model->subgraphs()->Get(0);
const auto* inputs = subgraph->inputs();
const auto* outputs = subgraph->outputs();
if (inputs->size() != expected_input_names.size() || outputs->size() != expected_output_names.size()) {
return false;
}
const auto* tensors = subgraph->tensors();
std::unordered_set<std::string> input_names;
for (int i = 0; i < inputs->size(); ++i) {
int tensor_index = inputs->Get(i);
input_names.insert(tensors->Get(tensor_index)->name()->str());
}
std::unordered_set<std::string> output_names;
for (int i = 0; i < outputs->size(); ++i) {
int tensor_index = outputs->Get(i);
output_names.insert(tensors->Get(tensor_index)->name()->str());
}
if (input_names != expected_input_names || output_names != expected_output_names) {
return false;
}
return true;
}
namespace WhisperKit {
constexpr const int kKvFactor = 2;
constexpr const int kLayersWhisperTiny = 4;
constexpr const int kLayersWhisperBase = 6;
constexpr const int kLayersWhisperSmall = 12;
constexpr const int kLayersWhisperMedium = 24;
constexpr const int kLayersWhisperLarge = 32;
constexpr const char* kVariantWhisperTiny = "tiny";
constexpr const char* kVariantWhisperBase = "base";
constexpr const char* kVariantWhisperSmall = "small";
constexpr const char* kVariantWhisperMedium = "medium";
constexpr const char* kVariantWhisperLarge = "large";
constexpr const char* kVariantNone = "none";
} // namespace WhisperKit
const int layers_for_variant(const std::string& variant) {
if (variant == kVariantWhisperTiny) {
return kLayersWhisperTiny;
} else if (variant == kVariantWhisperBase) {
return kLayersWhisperBase;
} else if (variant == kVariantWhisperSmall) {
return kLayersWhisperSmall;
} else if (variant == kVariantWhisperMedium) {
return kLayersWhisperMedium;
} else if (variant == kVariantWhisperLarge) {
return kLayersWhisperLarge;
}
return 0;
}
bool is_exact_match_for_separate_kv_cache_no_alignment_heads(const tflite::Model* model) {
const auto* subgraph = model->subgraphs()->Get(0);
const auto* inputs = subgraph->inputs();
const auto* outputs = subgraph->outputs();
const auto& num_inputs = inputs->size();
const auto& num_outputs = outputs->size();
constexpr const int num_shared_inputs = 4; // x, index, k_cache_cross, v_cache_cross
constexpr const int num_layers_whisper_tiny = 4;
constexpr const int kv_factor = 2;
constexpr const int minimum_num_inputs = num_shared_inputs + num_layers_whisper_tiny * kv_factor;
constexpr const int minimum_num_outputs = 2 * num_layers_whisper_tiny + 1; // logits
if (num_inputs < minimum_num_inputs || num_outputs < minimum_num_outputs) {
return false;
}
/* helper functions to calculate the number of inputs and outputs for a given number of layers */
auto calculate_num_inputs_for_variant_with_layers = [=](const int num_layers) -> auto{
return num_shared_inputs + num_layers * kv_factor;
};
auto calculate_num_outputs_for_variant_with_layers = [=](const int num_layers) -> auto{
return kv_factor * num_layers + 1;
};
auto output_names_for_variant_with_layers = [=](const int num_layers) -> auto{
std::unordered_set<std::string> output_names;
output_names.insert(std::string("logits"));
for (int i = 0; i < num_layers; ++i) {
output_names.insert(std::string("k_cache_" + std::to_string(i)));
output_names.insert(std::string("v_cache_" + std::to_string(i)));
}
return output_names;
};
auto input_names_for_variant_with_layers = [](const int num_layers) -> auto{
std::unordered_set<std::string> input_names;
input_names.insert(std::string("x"));
input_names.insert(std::string("index"));
input_names.insert(std::string("k_cache_cross"));
input_names.insert(std::string("v_cache_cross"));
for (int i = 0; i < num_layers; ++i) {
input_names.insert(std::string("k_cache_self_" + std::to_string(i)));
input_names.insert(std::string("v_cache_self_" + std::to_string(i)));
}
return input_names;
};
char* variant = const_cast<char*>(kVariantNone);
if (num_inputs == calculate_num_inputs_for_variant_with_layers(kLayersWhisperTiny)) {
variant = const_cast<char*>(kVariantWhisperTiny);
} else if (num_inputs == calculate_num_inputs_for_variant_with_layers(kLayersWhisperBase)) {
variant = const_cast<char*>(kVariantWhisperBase);
} else if (num_inputs == calculate_num_inputs_for_variant_with_layers(kLayersWhisperSmall)) {
variant = const_cast<char*>(kVariantWhisperSmall);
} else if (num_inputs == calculate_num_inputs_for_variant_with_layers(kLayersWhisperMedium)) {
variant = const_cast<char*>(kVariantWhisperMedium);
} else if (num_inputs == calculate_num_inputs_for_variant_with_layers(kLayersWhisperLarge)) {
variant = const_cast<char*>(kVariantWhisperLarge);
}
// no matches found for inputs
if (variant == kVariantNone) {
return false;
}
if (variant == kVariantWhisperTiny) {
if (num_outputs != calculate_num_outputs_for_variant_with_layers(kLayersWhisperTiny)) {
return false;
}
} else if (variant == kVariantWhisperBase) {
if (num_outputs != calculate_num_outputs_for_variant_with_layers(kLayersWhisperBase)) {
return false;
}
} else if (variant == kVariantWhisperSmall) {
if (num_outputs != calculate_num_outputs_for_variant_with_layers(kLayersWhisperSmall)) {
return false;
}
} else if (variant == kVariantWhisperMedium) {
if (num_outputs != calculate_num_outputs_for_variant_with_layers(kLayersWhisperMedium)) {
return false;
}
} else if (variant == kVariantWhisperLarge) {
if (num_outputs != calculate_num_outputs_for_variant_with_layers(kLayersWhisperLarge)) {
return false;
}
}
auto expected_input_names = input_names_for_variant_with_layers(layers_for_variant(variant));
auto expected_output_names = output_names_for_variant_with_layers(layers_for_variant(variant));
std::unordered_set<std::string> input_names;
std::unordered_set<std::string> output_names;
const auto* tensors = subgraph->tensors();
auto normalize_name = [](const std::string& name) -> std::string {
// Names were padded with null characters to ensure alignment with original exported names
// Remove extra null characters, or naive string matching will fail.
auto name_copy = name.c_str();
return std::string(name_copy);
};
for (int i = 0; i < num_inputs; ++i) {
auto name = normalize_name(tensors->Get(inputs->Get(i))->name()->str());
input_names.insert(name);
}
for (int i = 0; i < num_outputs; ++i) {
auto name = normalize_name(tensors->Get(outputs->Get(i))->name()->str());
output_names.insert(name);
}
if (input_names != expected_input_names) {
return false;
}
if (output_names != expected_output_names) {
return false;
}
return true;
}
std::unique_ptr<TextDecoder> TextDecoderFactory::CreateFromFile(const std::string& tflite_model_path) {
std::ifstream file(tflite_model_path, std::ios::binary | std::ios::ate);
if (!file) throw std::runtime_error("Failed to open file");
std::streamsize size = file.tellg();
file.seekg(0, std::ios::beg);
std::vector<char> buffer(size);
if (!file.read(buffer.data(), size)) throw std::runtime_error("Failed to read file");
const tflite::Model* model = tflite::GetModel(buffer.data());
if (!model) throw std::runtime_error("Failed to load model");
auto is_monolithic_kv_cache = is_exact_match_for_monolithic_kv_cache(model);
if (is_monolithic_kv_cache) {
return std::make_unique<MonolithicKVDecoder>(tflite_model_path);
}
auto is_separate_kv_cache_no_alignment_heads = is_exact_match_for_separate_kv_cache_no_alignment_heads(model);
throw std::runtime_error("Decoder model signature not recognized");
}
std::pair<char*, int> MonolithicKVDecoder::get_logits_tensor() { return decoder_outputs[0]; }
MonolithicKVDecoder::MonolithicKVDecoder(const std::string& tflite_model_path) {
_model_path = tflite_model_path;
// Note that the decoder model is not initialized here, it is initialized in the initialize method
_decoder_model = std::make_unique<MODEL_SUPER_CLASS>("TextDecoder");
if (!_decoder_model) {
throw std::runtime_error("Decoder model not initialized");
}
}
bool MonolithicKVDecoder::initialize(std::string model_path, std::string lib_dir, std::string cache_dir, int backend,
bool debug) {
return _decoder_model->initialize(model_path, lib_dir, cache_dir, backend, debug);
}
void MonolithicKVDecoder::uninitialize() { _decoder_model->uninitialize(); }
void MonolithicKVDecoder::read_input_data(char* input_data, int idx) {
_decoder_model->read_input_data(input_data, idx);
}
void MonolithicKVDecoder::bind_input_tensor(char* input_data, const std::string& tensor_name) {
if (tensor_name == "x") {
_decoder_model->read_input_data(input_data, 0);
return;
}
if (tensor_name == "index") {
_decoder_model->read_input_data(input_data, 1);
return;
}
if (tensor_name == "k_cache_cross") {
_decoder_model->read_input_data(input_data, 2);
return;
}
if (tensor_name == "v_cache_cross") {
_decoder_model->read_input_data(input_data, 3);
return;
}
// self attention kv cache handled separately
throw std::runtime_error("Invalid tensor name");
}
void MonolithicKVDecoder::invoke(bool measure_time) { _decoder_model->invoke(measure_time); }
void MonolithicKVDecoder::update_kv_cache() {
if (decoder_outputs.empty()) {
decoder_outputs = _decoder_model->get_output_ptrs();
}
// k_cache_self
_decoder_model->read_input_data(decoder_outputs[1].first, 4);
// v_cache_self
_decoder_model->read_input_data(decoder_outputs[2].first, 5);
}
void MonolithicKVDecoder::initialize_kv_cache() {
if (decoder_outputs.empty()) {
decoder_outputs = _decoder_model->get_output_ptrs();
}
// first k_cache_self is all zeros
memset(decoder_outputs[1].first, 0, decoder_outputs[1].second);
// first v_cache_self is all zeros
memset(decoder_outputs[2].first, 0, decoder_outputs[2].second);
}
float MonolithicKVDecoder::get_latency_median() { return _decoder_model->get_latency_median(); }
float MonolithicKVDecoder::get_latency_avg() { return _decoder_model->get_latency_avg(); }
std::unique_ptr<json> MonolithicKVDecoder::get_latency_json() { return _decoder_model->get_latency_json(); }
std::vector<std::pair<char*, int>> MonolithicKVDecoder::get_input_ptrs() { return _decoder_model->get_input_ptrs(); }
std::vector<std::pair<char*, int>> MonolithicKVDecoder::get_output_ptrs() { return _decoder_model->get_output_ptrs(); }
int MonolithicKVDecoder::get_inference_num() { return _decoder_model->get_inference_num(); }
float MonolithicKVDecoder::get_latency_sum() { return _decoder_model->get_latency_sum(); }