-
-
Notifications
You must be signed in to change notification settings - Fork 580
/
models.js
1379 lines (1134 loc) 路 45.5 KB
/
models.js
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
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
const {
Callable,
getModelFile,
fetchJSON,
dispatchCallback,
isIntegralNumber,
exists,
} = require("./utils.js");
const {
Sampler,
} = require("./samplers.js");
const {
LogitsProcessorList,
GenerationConfig,
ForceTokensLogitsProcessor,
ForcedBOSTokenLogitsProcessor,
ForcedEOSTokenLogitsProcessor,
WhisperTimeStampLogitsProcessor
} = require("./generation.js");
const { Tensor } = require('./tensor_utils.js')
const ONNX = require('onnxruntime-web');
const InferenceSession = ONNX.InferenceSession
const ONNXTensor = ONNX.Tensor
//////////////////////////////////////////////////
// Helper functions
async function constructSession(modelPath, fileName, progressCallback = null) {
let buffer = await getModelFile(modelPath, fileName, progressCallback);
let session = await InferenceSession.create(buffer, {
// executionProviders: ["webgl"]
executionProviders: ["wasm"]
});
return session
}
async function sessionRun(session, inputs) {
let output = await session.run(inputs);
output = replaceTensors(output);
return output;
}
function replaceTensors(obj) {
// Convert ONNX Tensors with our custom Tensor class
// to support additional functions
for (let prop in obj) {
if (obj[prop] instanceof ONNXTensor) {
obj[prop] = new Tensor(obj[prop]);
}
}
return obj;
}
function _prepare_attention_mask(self, tokens) {
// Prepare attention mask
let pad_token_id = self.config.pad_token_id ?? null;
let eos_token_id = self.config.eos_token_id ?? null;
if (isIntegralNumber(eos_token_id)) {
eos_token_id = [eos_token_id];
}
let is_pad_token_in_inputs = tokens.indexOf(pad_token_id) !== -1;
let is_pad_token_not_equal_to_eos_token_id = (eos_token_id === null) || !eos_token_id.includes(pad_token_id)
if (is_pad_token_in_inputs && is_pad_token_not_equal_to_eos_token_id) {
let data = BigInt64Array.from(
// Note: != so that int matches bigint
tokens.data.map(x => x != pad_token_id)
)
return new Tensor('int64', data, tokens.dims)
} else {
return new Tensor(
'int64',
new BigInt64Array(tokens.data.length).fill(1n),
tokens.dims
)
}
}
function boolTensor(value) {
// Create boolean tensor
return new Tensor('bool', [value], [1]);
}
// JS doesn't support mixings, so we define some reused functions here, and allow "this" to be passed in
async function seq2seqLoadModel(modelPath, progressCallback) {
let info = await Promise.all([
fetchJSON(modelPath, 'config.json', progressCallback),
constructSession(modelPath, 'encoder_model.onnx', progressCallback),
constructSession(modelPath, 'decoder_model_merged.onnx', progressCallback),
fetchJSON(modelPath, 'generation_config.json', progressCallback, false),
])
// Called when all parts are loaded
dispatchCallback(progressCallback, {
status: 'loaded',
name: modelPath
});
return info;
}
async function seq2seq_forward(self, model_inputs, {
encoder_input_name = 'input_ids',
add_decoder_pkv = true
} = {}) {
let encoderOutputs = model_inputs.encoder_outputs;
let pastKeyValues = model_inputs.past_key_values;
if (encoderOutputs === null) {
const encoderFeeds = {
[encoder_input_name]: model_inputs[encoder_input_name],
}
if (self.session.inputNames.includes('attention_mask')) {
encoderFeeds.attention_mask = model_inputs.attention_mask
}
const encoderResults = await sessionRun(self.session, encoderFeeds);
encoderOutputs = encoderResults.last_hidden_state;
}
let decoderFeeds = {
input_ids: model_inputs.decoder_input_ids,
encoder_hidden_states: encoderOutputs,
use_cache_branch: boolTensor(pastKeyValues !== null)
};
if (self.decoder_merged_session.inputNames.includes('encoder_attention_mask')) {
decoderFeeds.encoder_attention_mask = model_inputs.attention_mask
}
self.addPastKeyValues(decoderFeeds, pastKeyValues, add_decoder_pkv);
const decoderResults = await sessionRun(self.decoder_merged_session, decoderFeeds);
let logits = decoderResults.logits;
pastKeyValues = self.getPastKeyValues(decoderResults);
return new Seq2SeqLMOutput(logits, pastKeyValues, encoderOutputs);
}
function seq2seqStartBeams(self, inputTokenIds, numOutputTokens, requires_attention_mask = true) {
let beams = [];
let beamId = 0;
for (let tokens of inputTokenIds) {
// TODO: Improve
// Currently, just add back batch dimension.
// In future, allow for true parallel execution
tokens.dims = [1, ...tokens.dims]
// Create beam
let start = {
inputs: tokens,
encoder_outputs: null,
past_key_values: null,
// decoder_input_ids == output_token_ids
output_token_ids: [self.config.decoder_start_token_id],
done: false,
score: 0,
id: beamId++ // assign unique id to beams
}
if (requires_attention_mask) {
start.attention_mask = _prepare_attention_mask(self, tokens);
}
beams.push(start);
}
return beams;
}
async function seq2seqRunBeam(self, beam, {
input_name = 'input_ids',
} = {}
) {
// 1. Prepare
let model_inputs = {
[input_name]: beam.inputs,
decoder_input_ids: self.toI64Tensor(beam.output_token_ids.slice(-1)),
encoder_outputs: beam.encoder_outputs,
past_key_values: beam.past_key_values,
}
if (beam.attention_mask) {
model_inputs.attention_mask = beam.attention_mask
}
// 2. Run
let output = await self.forward(model_inputs);
// 3. Update
beam.past_key_values = output.past_key_values;
beam.encoder_outputs = output.encoder_outputs;
return output;
}
async function textgen_forward(self, model_inputs) {
let past_key_values = model_inputs.past_key_values;
let decoderFeeds = {
input_ids: model_inputs.input_ids,
attention_mask: model_inputs.attention_mask,
use_cache_branch: boolTensor(past_key_values !== null)
}
self.addPastKeyValues(decoderFeeds, past_key_values)
let decoderResults = await sessionRun(self.session, decoderFeeds);
let logits = decoderResults.logits;
past_key_values = self.getPastKeyValues(decoderResults);
return { logits, past_key_values };
}
function textgenStartBeams(self, inputTokenIds, numOutputTokens, inputs_attention_mask) {
let beams = [];
let beamId = 0;
for (let tokens of inputTokenIds) {
// TODO: Improve
// Currently, just add back batch dimension.
// In future, allow for true parallel execution
tokens.dims = [1, ...tokens.dims]
let attn_mask;
if (inputs_attention_mask) {
attn_mask = inputs_attention_mask.get(beamId)
attn_mask.dims = [1, ...attn_mask.dims]
} else {
attn_mask = _prepare_attention_mask(self, tokens)
}
let start = {
input: tokens,
model_input_ids: tokens,
attention_mask: attn_mask,
past_key_values: null,
output_token_ids: [],
num_output_tokens: numOutputTokens,
done: false,
score: 0,
id: beamId++ // assign unique id to beams
}
beams.push(start);
}
return beams;
}
async function textgenRunBeam(self, beam) {
let attnMaskData = new BigInt64Array(beam.input.data.length + beam.output_token_ids.length).fill(1n)
// 1. Prepare
let model_inputs = {
input_ids: beam.model_input_ids,
attention_mask: new Tensor(
'int64',
attnMaskData,
[1, attnMaskData.length]
),
past_key_values: beam.past_key_values,
}
// 2. Run
let output = await self.forward(model_inputs);
// 3. Update
beam.past_key_values = output.past_key_values;
return output;
}
function textgenUpdatebeam(beam, newTokenId) {
beam.output_token_ids = [...beam.output_token_ids, newTokenId];
beam.model_input_ids = new Tensor('int64', [BigInt(newTokenId)], [1, 1]);
}
//////////////////////////////////////////////////
//////////////////////////////////////////////////
// Base class
class PreTrainedModel extends Callable {
constructor(config, session) {
super();
this.config = config;
this.session = session;
}
async dispose() {
// Dispose of all ONNX sessions sessions
// TODO use: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/FinalizationRegistry
let promises = [];
for (let key of Object.keys(this)) {
let item = this[key];
if (item instanceof InferenceSession) {
promises.push(item.handler.dispose())
}
}
return await Promise.all(promises);
}
static async from_pretrained(modelPath, progressCallback = null) {
let config = await fetchJSON(modelPath, 'config.json', progressCallback);
let modelName = config.is_encoder_decoder ? 'encoder_model.onnx' : 'model.onnx';
// Load model
let session = await constructSession(modelPath, modelName, progressCallback);
// Called when all parts are loaded
dispatchCallback(progressCallback, {
status: 'loaded',
name: modelPath
});
return new this(config, session);
}
toI64Tensor(items) {
if (items instanceof Tensor) {
return items;
}
// items is an array
if (items.length === 0) {
throw Error("items must be non-empty");
}
if (Array.isArray(items[0])) {
// batched
if (items.some(x => x.length !== items[0].length)) {
throw Error("Unable to create tensor, you should probably activate truncation and/or padding with 'padding=True' and/or 'truncation=True' to have batched tensors with the same length.")
}
return new Tensor('int64',
BigInt64Array.from(items.flat().map(x => BigInt(x))),
[items.length, items[0].length]
);
} else {
//flat
return new Tensor('int64',
BigInt64Array.from(items.map(x => BigInt(x))),
[1, items.length]
);
}
}
async _call(model_inputs) {
return await sessionRun(this.session, model_inputs);
}
async forward(model_inputs) {
throw Error("forward should be implemented in subclasses.")
}
/**
* @param {GenerationConfig} generation_config
* @param {number} input_ids_seq_length
* @returns {LogitsProcessorList}
*/
_get_logits_processor(
generation_config,
input_ids_seq_length,
// encoder_input_ids, TODO
// prefix_allowed_tokens_fn, TODO
logits_processor = null
) {
const processors = new LogitsProcessorList();
// if (generation_config.diversity_penalty !== null && generation_config.diversity_penalty > 0.0) {
// processors.push(new HammingDiversityLogitsProcessor(
// generation_config.diversity_penalty,
// generation_config.num_beams,
// generation_config.num_beam_groups
// ));
// }
// if (generation_config.encoder_repetition_penalty !== null && generation_config.encoder_repetition_penalty !== 1.0) {
// processors.push(new EncoderRepetitionPenaltyLogitsProcessor(
// generation_config.encoder_repetition_penalty,
// encoder_input_ids
// ));
// }
// if (generation_config.repetition_penalty !== null && generation_config.repetition_penalty !== 1.0) {
// processors.push(new RepetitionPenaltyLogitsProcessor(generation_config.repetition_penalty));
// }
// if (generation_config.no_repeat_ngram_size !== null && generation_config.no_repeat_ngram_size > 0) {
// processors.push(new NoRepeatNGramLogitsProcessor(generation_config.no_repeat_ngram_size));
// }
// if (generation_config.encoder_no_repeat_ngram_size !== null && generation_config.encoder_no_repeat_ngram_size > 0) {
// if (this.config.is_encoder_decoder) {
// processors.push(new EncoderNoRepeatNGramLogitsProcessor(
// generation_config.encoder_no_repeat_ngram_size,
// encoder_input_ids
// ));
// } else {
// throw new Error("It's impossible to use `encoder_no_repeat_ngram_size` with decoder-only architecture");
// }
// }
// if (generation_config.bad_words_ids !== null) {
// processors.push(new NoBadWordsLogitsProcessor(generation_config.bad_words_ids, generation_config.eos_token_id));
// }
// if (generation_config.min_length !== null && generation_config.eos_token_id !== null && generation_config.min_length > 0) {
// processors.push(new MinLengthLogitsProcessor(generation_config.min_length, generation_config.eos_token_id));
// }
// if (generation_config.min_new_tokens !== null && generation_config.eos_token_id !== null && generation_config.min_new_tokens > 0) {
// processors.push(new MinNewTokensLengthLogitsProcessor(
// input_ids_seq_length,
// generation_config.min_new_tokens,
// generation_config.eos_token_id
// ));
// }
// if (prefix_allowed_tokens_fn !== null) {
// processors.push(new PrefixConstrainedLogitsProcessor(
// prefix_allowed_tokens_fn,
// generation_config.num_beams / generation_config.num_beam_groups
// ));
// }
if (generation_config.forced_bos_token_id !== null) {
processors.push(new ForcedBOSTokenLogitsProcessor(generation_config.forced_bos_token_id));
}
if (generation_config.forced_eos_token_id !== null) {
processors.push(new ForcedEOSTokenLogitsProcessor(
generation_config.max_length,
generation_config.forced_eos_token_id
));
}
// if (generation_config.remove_invalid_values === true) {
// processors.push(new InfNanRemoveLogitsProcessor());
// }
// if (generation_config.exponential_decay_length_penalty !== null) {
// processors.push(new ExponentialDecayLengthPenalty(
// generation_config.exponential_decay_length_penalty,
// generation_config.eos_token_id,
// input_ids_seq_length
// ));
// }
// if (generation_config.suppress_tokens !== null) {
// processors.push(new SuppressTokensLogitsProcessor(generation_config.suppress_tokens));
// }
// if (generation_config.begin_suppress_tokens !== null) {
// let begin_index = input_ids_seq_length;
// begin_index = (input_ids_seq_length > 1 || generation_config.forced_bos_token_id === null) ? begin_index : begin_index + 1;
// if (generation_config.forced_decoder_ids !== null) {
// begin_index += generation_config.forced_decoder_ids[generation_config.forced_decoder_ids.length - 1][0];
// }
// processors.push(new SuppressTokensAtBeginLogitsProcessor(generation_config.begin_suppress_tokens, begin_index));
// }
if (generation_config.forced_decoder_ids !== null) {
processors.push(new ForceTokensLogitsProcessor(generation_config.forced_decoder_ids));
}
if (logits_processor !== null) {
processors.extend(logits_processor)
}
// `LogitNormalization` should always be the last logit processor, when present
// if (generation_config.renormalize_logits === true) {
// processors.push(new LogitNormalization());
// }
return processors;
}
_get_generation_config(generation_config) {
// Create empty generation config (contains defaults)
let gen_config = new GenerationConfig();
// Apply model's generation config
Object.assign(gen_config, this.generation_config);
// Finally, use any generation config specified by the user
// when calling `generate`
if (generation_config !== null) {
Object.assign(gen_config, generation_config);
}
return gen_config;
}
async generate(
inputs,
generation_config = null,
logits_processor = null,
{
inputs_attention_mask = null
} = {},
) {
if (inputs.length === 0) {
throw Error("Must supply a non-empty array of input token ids.")
}
// Update generation config with defaults
generation_config = this._get_generation_config(generation_config);
logits_processor = logits_processor ?? new LogitsProcessorList()
// TODO Update generation config
// this.generation_config
// Update logits processor
logits_processor = this._get_logits_processor(
generation_config,
inputs.length,
logits_processor
)
// TODO implement early_stopping
// https://huggingface.co/blog/how-to-generate
let numOutputTokens = 1;
const maxOutputTokens = numOutputTokens + (generation_config.max_new_tokens ?? Infinity);
let sampler = Sampler.getSampler(generation_config);
let beams = this.getStartBeams(inputs, numOutputTokens, inputs_attention_mask);
while (beams.some(x => !x.done) && numOutputTokens < maxOutputTokens) {
let newest_beams = [];
for (let beam of beams) {
if (beam.done) {
// TODO add length penalty (for ending early)
// Add this beam back into the pool
newest_beams.push(beam);
continue
}
let output = await this.runBeam(beam);
logits_processor(beam.output_token_ids, output.logits)
let sampledTokens = sampler(output.logits);
for (let [newTokenId, logProb] of sampledTokens) {
// use previous beam as a starting point
let newBeam = { ...beam };
// update new beam
this.updateBeam(newBeam, newTokenId);
newBeam.score += logProb;
if (newTokenId === this.config.eos_token_id) {
newBeam.done = true;
}
newest_beams.push(newBeam);
}
}
++numOutputTokens;
// Next, we get the best beams, per ID
newest_beams = this.groupBeams(newest_beams).map(
group => group
.sort((a, b) => b.score - a.score) // sort based on score
.slice(0, generation_config.num_beams) // remove outside beam width
);
// Flatten beams
beams = newest_beams.flat();
// Run callback
if (generation_config.callback_function) {
generation_config.callback_function(beams);
}
}
return this.groupBeams(beams).map(
batch => {
if (generation_config.num_return_sequences > 1) {
return batch.slice(0, generation_config.num_return_sequences).map(x => x.output_token_ids);
} else {
return [batch[0].output_token_ids];
}
}
)
}
groupBeams(beams) {
// Group beams by their ids
const groups = {};
for (const obj of beams) {
if (groups[obj.id] === undefined) {
groups[obj.id] = [obj];
} else {
groups[obj.id].push(obj);
}
}
return Object.values(groups);
}
getPastKeyValues(decoderResults) {
const pkvs = {};
for (const name in decoderResults) {
if (name.startsWith('present')) {
pkvs[name.replace('present', 'past_key_values')] = decoderResults[name]
}
}
return pkvs;
}
addPastKeyValues(decoderFeeds, pastKeyValues, hasDecoder = false) {
if (pastKeyValues === null) {
// TODO support batches (i.e., batch_size > 1)
if (hasDecoder) {
let encoder_dims = [1, this.num_encoder_heads, 0, this.encoder_dim_kv];
for (let i = 0; i < this.num_encoder_layers; ++i) {
decoderFeeds[`past_key_values.${i}.encoder.key`] = new Tensor('float32', [], encoder_dims)
decoderFeeds[`past_key_values.${i}.encoder.value`] = new Tensor('float32', [], encoder_dims)
}
let decoder_dims = [1, this.num_decoder_heads, 0, this.decoder_dim_kv];
for (let i = 0; i < this.num_decoder_layers; ++i) {
decoderFeeds[`past_key_values.${i}.decoder.key`] = new Tensor('float32', [], decoder_dims)
decoderFeeds[`past_key_values.${i}.decoder.value`] = new Tensor('float32', [], decoder_dims)
}
} else {
let dims = [1, this.num_heads, 0, this.dim_kv]
for (let i = 0; i < this.num_layers; ++i) {
decoderFeeds[`past_key_values.${i}.key`] = new Tensor('float32', [], dims)
decoderFeeds[`past_key_values.${i}.value`] = new Tensor('float32', [], dims)
}
}
} else {
Object.assign(decoderFeeds, pastKeyValues)
}
}
}
//////////////////////////////////////////////////
// Bert models
class BertPreTrainedModel extends PreTrainedModel { }
class BertModel extends BertPreTrainedModel { }
class BertForMaskedLM extends BertPreTrainedModel {
async _call(model_inputs) {
let logits = (await super._call(model_inputs)).logits;
return new MaskedLMOutput(logits)
}
}
class BertForSequenceClassification extends BertPreTrainedModel {
async _call(model_inputs) {
let logits = (await super._call(model_inputs)).logits;
return new SequenceClassifierOutput(logits)
}
}
class BertForQuestionAnswering extends BertPreTrainedModel {
async _call(model_inputs) {
let outputs = await super._call(model_inputs);
return new QuestionAnsweringModelOutput(outputs.start_logits, outputs.end_logits);
}
}
//////////////////////////////////////////////////
//////////////////////////////////////////////////
// DistilBert models
class DistilBertPreTrainedModel extends PreTrainedModel { }
class DistilBertModel extends DistilBertPreTrainedModel { }
class DistilBertForSequenceClassification extends DistilBertPreTrainedModel {
async _call(model_inputs) {
let logits = (await super._call(model_inputs)).logits;
return new SequenceClassifierOutput(logits)
}
}
class DistilBertForQuestionAnswering extends DistilBertPreTrainedModel {
async _call(model_inputs) {
let outputs = await super._call(model_inputs);
return new QuestionAnsweringModelOutput(outputs.start_logits, outputs.end_logits);
}
}
class DistilBertForMaskedLM extends DistilBertPreTrainedModel {
async _call(model_inputs) {
let logits = (await super._call(model_inputs)).logits;
return new MaskedLMOutput(logits)
}
}
//////////////////////////////////////////////////
//////////////////////////////////////////////////
// DistilBert models
class AlbertPreTrainedModel extends PreTrainedModel { }
class AlbertModel extends AlbertPreTrainedModel { }
class AlbertForSequenceClassification extends AlbertPreTrainedModel {
async _call(model_inputs) {
let logits = (await super._call(model_inputs)).logits;
return new SequenceClassifierOutput(logits)
}
}
class AlbertForQuestionAnswering extends AlbertPreTrainedModel {
async _call(model_inputs) {
let outputs = await super._call(model_inputs);
return new QuestionAnsweringModelOutput(outputs.start_logits, outputs.end_logits);
}
}
class AlbertForMaskedLM extends AlbertPreTrainedModel {
async _call(model_inputs) {
let logits = (await super._call(model_inputs)).logits;
return new MaskedLMOutput(logits)
}
}
//////////////////////////////////////////////////
//////////////////////////////////////////////////
// T5 models
class T5PreTrainedModel extends PreTrainedModel { };
class T5Model extends T5PreTrainedModel {
async generate(...args) {
throw Error(
"The current model class (T5Model) is not compatible with `.generate()`, as it doesn't have a language model head. Please use one of the following classes instead: {'T5ForConditionalGeneration'}"
)
}
}
class T5ForConditionalGeneration extends T5PreTrainedModel {
constructor(config, session, decoder_merged_session, generation_config) {
super(config, session);
this.decoder_merged_session = decoder_merged_session;
this.generation_config = generation_config;
this.num_decoder_layers = this.config.num_decoder_layers;
this.num_decoder_heads = this.config.num_heads;
this.decoder_dim_kv = this.config.d_kv;
this.num_encoder_layers = this.config.num_layers;
this.num_encoder_heads = this.config.num_heads;
this.encoder_dim_kv = this.config.d_kv;
}
static async from_pretrained(modelPath, progressCallback = null) {
let info = await seq2seqLoadModel(modelPath, progressCallback);
return new this(...info);
}
getStartBeams(inputs, numOutputTokens, ...args) {
return seq2seqStartBeams(this, inputs, numOutputTokens);
}
async runBeam(beam) {
return await seq2seqRunBeam(this, beam);
}
updateBeam(beam, newTokenId) {
beam.output_token_ids = [...beam.output_token_ids, newTokenId];
}
async forward(model_inputs) {
return await seq2seq_forward(this, model_inputs);
}
}
//////////////////////////////////////////////////
//////////////////////////////////////////////////
// Bart models
class BartPretrainedModel extends PreTrainedModel { };
class BartModel extends BartPretrainedModel {
async generate(...args) {
throw Error(
"The current model class (BartModel) is not compatible with `.generate()`, as it doesn't have a language model head. Please use one of the following classes instead: {'BartForConditionalGeneration'}"
)
}
}
class BartForConditionalGeneration extends BartPretrainedModel {
constructor(config, session, decoder_merged_session, generation_config) {
super(config, session);
this.decoder_merged_session = decoder_merged_session;
this.generation_config = generation_config;
this.num_decoder_layers = this.config.decoder_layers;
this.num_decoder_heads = this.config.decoder_attention_heads;
this.decoder_dim_kv = this.config.d_model / this.num_decoder_heads;
this.num_encoder_layers = this.config.encoder_layers;
this.num_encoder_heads = this.config.encoder_attention_heads;
this.encoder_dim_kv = this.config.d_model / this.num_encoder_heads;
}
static async from_pretrained(modelPath, progressCallback = null) {
let info = await seq2seqLoadModel(modelPath, progressCallback);
return new this(...info);
}
getStartBeams(inputs, numOutputTokens, ...args) {
return seq2seqStartBeams(this, inputs, numOutputTokens);
}
async runBeam(beam) {
return await seq2seqRunBeam(this, beam);
}
updateBeam(beam, newTokenId) {
beam.output_token_ids = [...beam.output_token_ids, newTokenId];
}
async forward(model_inputs) {
return await seq2seq_forward(this, model_inputs);
}
}
//////////////////////////////////////////////////
//////////////////////////////////////////////////
// Roberta models
class RobertaPreTrainedModel extends PreTrainedModel { }
class RobertaModel extends RobertaPreTrainedModel { }
class RobertaForMaskedLM extends RobertaPreTrainedModel {
async _call(model_inputs) {
let logits = (await super._call(model_inputs)).logits;
return new MaskedLMOutput(logits)
}
}
class RobertaForSequenceClassification extends RobertaPreTrainedModel {
async _call(model_inputs) {
let logits = (await super._call(model_inputs)).logits;
return new SequenceClassifierOutput(logits)
}
}
class RobertaForQuestionAnswering extends RobertaPreTrainedModel {
async _call(model_inputs) {
let outputs = await super._call(model_inputs);
return new QuestionAnsweringModelOutput(outputs.start_logits, outputs.end_logits);
}
}
//////////////////////////////////////////////////
//////////////////////////////////////////////////
// T5 models
class WhisperPreTrainedModel extends PreTrainedModel { };
class WhisperModel extends WhisperPreTrainedModel {
async generate(...args) {
throw Error(
"The current model class (WhisperModel) is not compatible with `.generate()`, as it doesn't have a language model head. Please use one of the following classes instead: {'WhisperForConditionalGeneration'}"
)
}
}
class WhisperForConditionalGeneration extends WhisperPreTrainedModel {
constructor(config, session, decoder_merged_session, generation_config) {
super(config, session);
this.decoder_merged_session = decoder_merged_session;
this.generation_config = generation_config;
this.num_decoder_layers = this.config.decoder_layers;
this.num_decoder_heads = this.config.decoder_attention_heads;
this.decoder_dim_kv = this.config.d_model / this.num_decoder_heads;
this.num_encoder_layers = this.config.encoder_layers;
this.num_encoder_heads = this.config.encoder_attention_heads;
this.encoder_dim_kv = this.config.d_model / this.num_encoder_heads;
}
async generate(
inputs,
generation_config = null,
logits_processor = null,
) {
// Create generation config object
generation_config = this._get_generation_config(generation_config);
// Whisper has additional options for returning timestamps
generation_config.return_timestamps ??= false;
// TODO add language and task
if (generation_config.return_timestamps) {
logits_processor = [new WhisperTimeStampLogitsProcessor(generation_config)]
}
// Modify forced_decoder_ids_mapping. This is the way HF also does it,
// but it would probably be best to not modify the class' mapping, and
// rather create a copy?
return super.generate(inputs, generation_config, logits_processor)
}
static async from_pretrained(modelPath, progressCallback = null) {
let info = await seq2seqLoadModel(modelPath, progressCallback);
return new this(...info);
}
getStartBeams(inputTokenIds, numOutputTokens, ...args) {
// arguments ignored in this case
return seq2seqStartBeams(this, inputTokenIds, numOutputTokens, false);
}
async runBeam(beam) {
return await seq2seqRunBeam(this, beam, {
input_name: 'input_features',
});
}
updateBeam(beam, newTokenId) {
beam.output_token_ids = [...beam.output_token_ids, newTokenId];
}
async forward(model_inputs) {
return await seq2seq_forward(this, model_inputs, {
encoder_input_name: 'input_features',
});
}
}
//////////////////////////////////////////////////
//////////////////////////////////////////////////
class VisionEncoderDecoderModel extends PreTrainedModel {
constructor(config, session, decoder_merged_session) {
super(config, session);
this.decoder_merged_session = decoder_merged_session;
this.num_layers = this.config.decoder.n_layer;
this.num_heads = this.config.decoder.n_head;
this.dim_kv = this.config.decoder.n_embd / this.num_heads;
}
static async from_pretrained(modelPath, progressCallback = null) {
let [config, session, decoder_merged_session] = await Promise.all([
fetchJSON(modelPath, 'config.json', progressCallback),
constructSession(modelPath, 'encoder_model.onnx', progressCallback),
constructSession(modelPath, 'decoder_merged_session.onnx', progressCallback),
])
// Called when all parts are loaded
dispatchCallback(progressCallback, {
status: 'loaded',
name: modelPath
});
return new this(config, session, decoder_merged_session);
}
getStartBeams(inputs, numOutputTokens, ...args) {
return seq2seqStartBeams(this, inputs, numOutputTokens);
}
async runBeam(beam) {
return seq2seqRunBeam(this, beam, {
input_name: 'pixel_values',
});
}
updateBeam(beam, newTokenId) {
beam.output_token_ids = [...beam.output_token_ids, newTokenId];
}
async forward(model_inputs) {
return await seq2seq_forward(this, model_inputs, {
encoder_input_name: 'pixel_values',
add_decoder_pkv: false
})
}
}
//////////////////////////////////////////////////
//////////////////////////////////////////////////
// CLIP models
class CLIPPreTrainedModel extends PreTrainedModel { }
class CLIPModel extends CLIPPreTrainedModel {
}
//////////////////////////////////////////////////
//////////////////////////////////////////////////
// GPT2 models
class GPT2PreTrainedModel extends PreTrainedModel { }
class GPT2Model extends GPT2PreTrainedModel {
async generate(...args) {
throw Error(
"The current model class (GPT2Model) is not compatible with `.generate()`, as it doesn't have a language model head. Please use one of the following classes instead: {'GPT2LMHeadModel'}"
)
}