-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathmodeling_utils.js
More file actions
1726 lines (1540 loc) · 68.2 KB
/
Copy pathmodeling_utils.js
File metadata and controls
1726 lines (1540 loc) · 68.2 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
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
import { Callable } from '../utils/generic.js';
import { constructSessions, sessionRun } from './session.js';
import { AutoConfig, getCacheNames } from '../configs.js';
import { Tensor, full_like, cat, zeros_like, ones_like, ones } from '../utils/tensor.js';
import { DataTypeMap } from '../utils/dtypes.js';
// These will be populated by registry.js
export let MODEL_MAPPING_NAMES = null;
/**
* Register task mappings (called by registry.js after defining full mappings)
* @param {Object} mappings - Object with mapping names as keys
*/
export function registerTaskMappings(mappings) {
MODEL_MAPPING_NAMES = mappings;
}
import { GITHUB_ISSUE_URL } from '../utils/constants.js';
import { getModelJSON } from '../utils/hub.js';
import { Seq2SeqLMOutput } from './modeling_outputs.js';
import {
LogitsProcessorList,
ForcedBOSTokenLogitsProcessor,
ForcedEOSTokenLogitsProcessor,
SuppressTokensLogitsProcessor,
SuppressTokensAtBeginLogitsProcessor,
NoRepeatNGramLogitsProcessor,
RepetitionPenaltyLogitsProcessor,
NoBadWordsLogitsProcessor,
MinLengthLogitsProcessor,
MinNewTokensLengthLogitsProcessor,
TemperatureLogitsWarper,
ClassifierFreeGuidanceLogitsProcessor,
} from '../generation/logits_process.js';
import { GenerationConfig } from '../generation/configuration_utils.js';
import { EosTokenCriteria, MaxLengthCriteria, StoppingCriteriaList } from '../generation/stopping_criteria.js';
import { LogitsSampler } from '../generation/logits_sampler.js';
import { DefaultProgressCallback, pick } from '../utils/core.js';
import { ModelOutput } from './modeling_outputs.js';
import { logger } from '../utils/logger.js';
import { DynamicCache } from '../cache_utils.js';
import { get_model_files } from '../utils/model_registry/get_model_files.js';
import { get_file_metadata } from '../utils/model_registry/get_file_metadata.js';
import { MODEL_SESSION_CONFIG, MODEL_TYPES } from './session_config.js';
/**
* Converts an array or Tensor of integers to an int64 Tensor.
* @param {any[]|Tensor} items The input integers to be converted.
* @returns {Tensor} The int64 Tensor with the converted values.
* @throws {Error} If the input array is empty or the input is a batched Tensor and not all sequences have the same length.
* @private
*/
function 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]);
}
}
/**
* Creates a boolean tensor with a single value.
* @param {boolean} value The value of the tensor.
* @returns {Tensor} The boolean tensor.
* @private
*/
export function boolTensor(value) {
return new Tensor('bool', [value], [1]);
}
export { MODEL_TYPES } from './session_config.js';
/**
* Runtime-only model type configuration (forward functions, generation flags).
* Session/file configuration lives in `MODEL_SESSION_CONFIG` (session_config.js)
* and is merged in at lookup time by `resolveTypeConfig` to avoid duplication.
*/
const MODEL_RUNTIME_CONFIG = {
[MODEL_TYPES.DecoderOnly]: {
can_generate: true,
forward: decoder_forward,
prepare_inputs: decoder_prepare_inputs_for_generation,
},
[MODEL_TYPES.DecoderOnlyWithoutHead]: {
can_generate: false,
forward: decoder_forward,
prepare_inputs: decoder_prepare_inputs_for_generation,
},
[MODEL_TYPES.Seq2Seq]: {
can_generate: true,
forward: seq2seq_forward,
prepare_inputs: encoder_decoder_prepare_inputs_for_generation,
},
[MODEL_TYPES.Vision2Seq]: {
can_generate: true,
forward: seq2seq_forward,
prepare_inputs: encoder_decoder_prepare_inputs_for_generation,
},
[MODEL_TYPES.Musicgen]: {
can_generate: true,
forward: seq2seq_forward,
},
[MODEL_TYPES.EncoderDecoder]: {
can_generate: false,
forward: seq2seq_forward,
},
[MODEL_TYPES.ImageTextToText]: {
can_generate: true,
forward: image_text_to_text_forward,
prepare_inputs: multimodal_text_to_text_prepare_inputs_for_generation,
},
[MODEL_TYPES.AudioTextToText]: {
can_generate: true,
forward: audio_text_to_text_forward,
prepare_inputs: multimodal_text_to_text_prepare_inputs_for_generation,
},
[MODEL_TYPES.ImageAudioTextToText]: {
can_generate: true,
prepare_inputs: multimodal_text_to_text_prepare_inputs_for_generation,
},
[MODEL_TYPES.Phi3V]: {
can_generate: true,
prepare_inputs: multimodal_text_to_text_prepare_inputs_for_generation,
},
[MODEL_TYPES.MultiModality]: {
can_generate: true,
},
[MODEL_TYPES.AutoEncoder]: {
can_generate: false,
forward: auto_encoder_forward,
},
[MODEL_TYPES.Chatterbox]: {
can_generate: true,
forward: encoder_forward,
},
[MODEL_TYPES.VoxtralRealtime]: {
can_generate: true,
prepare_inputs: decoder_prepare_inputs_for_generation,
},
default: {
can_generate: false,
forward: encoder_forward,
},
};
/**
* Resolves the model type config for a given class name and config.
* @param {string} modelName The name of the class being used to load.
* @param {Object} config The model config.
* @returns {{ typeConfig: Object, textOnly: boolean, modelType: number|undefined }}
*/
function resolveTypeConfig(modelName, config) {
let modelType = MODEL_TYPE_MAPPING.get(modelName);
let textOnly = false;
// Detect cross-architecture loading: e.g., ForCausalLM class loading a ForConditionalGeneration model.
// In this case, use the native architecture's type config (for forward/sessions) in text-only mode.
const nativeArch = config?.architectures?.[0];
if (
nativeArch &&
nativeArch !== modelName &&
modelName?.endsWith('ForCausalLM') &&
nativeArch.endsWith('ForConditionalGeneration')
) {
const nativeType = MODEL_TYPE_MAPPING.get(nativeArch);
if (nativeType !== undefined) {
modelType = nativeType;
textOnly = true;
}
}
const runtimeConfig = MODEL_RUNTIME_CONFIG[modelType] ?? MODEL_RUNTIME_CONFIG.default;
const sessionConfig = MODEL_SESSION_CONFIG[modelType] ?? MODEL_SESSION_CONFIG.default;
return { typeConfig: { ...runtimeConfig, ...sessionConfig }, textOnly, modelType };
}
export const MODEL_TYPE_MAPPING = new Map();
export const MODEL_NAME_TO_CLASS_MAPPING = new Map();
export const MODEL_CLASS_TO_NAME_MAPPING = new Map();
/**
* A base class for pre-trained models that provides the model configuration and an ONNX session.
*/
export class PreTrainedModel extends Callable {
main_input_name = 'input_ids';
forward_params = ['input_ids', 'attention_mask'];
_return_dict_in_generate_keys = null;
/**
* Creates a new instance of the `PreTrainedModel` class.
* @param {import('../configs.js').PretrainedConfig} config The model configuration.
* @param {Record<string, any>} sessions The inference sessions for the model.
* @param {Record<string, Object>} configs Additional configuration files (e.g., generation_config.json).
*/
constructor(config, sessions, configs) {
super();
this.config = config;
this.sessions = sessions;
this.configs = configs;
const modelName = MODEL_CLASS_TO_NAME_MAPPING.get(this.constructor);
const { typeConfig } = resolveTypeConfig(modelName, config);
this.can_generate = typeConfig.can_generate;
this._forward = typeConfig.forward;
this._prepare_inputs_for_generation = typeConfig.prepare_inputs;
if (this.can_generate) {
this.forward_params.push('past_key_values');
}
/** @type {import('../configs.js').TransformersJSConfig} */
this.custom_config = this.config['transformers.js_config'] ?? {};
}
/**
* Disposes of all the ONNX sessions that were created during inference.
* @returns {Promise<unknown[]>} An array of promises, one for each ONNX session that is being disposed.
* @todo Use https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/FinalizationRegistry
*/
async dispose() {
const promises = [];
for (const session of Object.values(this.sessions)) {
promises.push(session.release?.());
}
return await Promise.all(promises);
}
/**
* Instantiate one of the model classes of the library from a pretrained model.
*
* The model class to instantiate is selected based on the `model_type` property of the config object
* (either passed as an argument or loaded from `pretrained_model_name_or_path` if possible)
*
* @param {string} pretrained_model_name_or_path The name or path of the pretrained model. Can be either:
* - A string, the *model id* of a pretrained model hosted inside a model repo on huggingface.co.
* Valid model ids can be located at the root-level, like `bert-base-uncased`, or namespaced under a
* user or organization name, like `dbmdz/bert-base-german-cased`.
* - A path to a *directory* containing model weights, e.g., `./my_model_directory/`.
* @param {import('../utils/hub.js').PretrainedModelOptions} options Additional options for loading the model.
*
* @returns {Promise<PreTrainedModel>} A new instance of the `PreTrainedModel` class.
*/
static async from_pretrained(
pretrained_model_name_or_path,
{
progress_callback = null,
config = null,
cache_dir = null,
local_files_only = false,
revision = 'main',
model_file_name = null,
subfolder = 'onnx',
device = null,
dtype = null,
use_external_data_format = null,
session_options = {},
} = {},
) {
const options = {
progress_callback,
config,
cache_dir,
local_files_only,
revision,
model_file_name,
subfolder,
device,
dtype,
use_external_data_format,
session_options,
};
const modelName = MODEL_CLASS_TO_NAME_MAPPING.get(this);
config = options.config = await AutoConfig.from_pretrained(pretrained_model_name_or_path, options);
const { typeConfig, textOnly, modelType } = resolveTypeConfig(modelName, config);
if (modelType === undefined) {
const type = modelName ?? config?.model_type;
if (type !== 'custom') {
logger.warn(
`Model type for '${type}' not found, assuming encoder-only architecture. Please report this at ${GITHUB_ISSUE_URL}.`,
);
}
}
// If a progress callback is provided AND it hasn't already been wrapped
// by pipeline() (which does its own aggregation), gather file metadata
// upfront so we can emit `progress_total` events. This lets consumers
// render a single overall progress bar when calling from_pretrained() directly.
if (progress_callback && !(progress_callback instanceof DefaultProgressCallback)) {
/** @type {import('../utils/core.js').FilesLoadingMap} */
const files_loading = {};
try {
const expected_files = await get_model_files(pretrained_model_name_or_path, {
config,
dtype,
device,
model_file_name,
});
const metadata = await Promise.all(
expected_files.map((file) => get_file_metadata(pretrained_model_name_or_path, file, options)),
);
metadata.forEach((m, i) => {
if (m.exists) {
// config.json is fetched by AutoConfig.from_pretrained() above
const isAlreadyLoaded = expected_files[i] === 'config.json';
files_loading[expected_files[i]] = {
loaded: isAlreadyLoaded ? (m.size ?? 0) : 0,
total: m.size ?? 0,
};
}
});
} catch (e) {
// If we fail to get metadata, we can still proceed without total progress.
// This may happen with local-only models or custom cache setups.
logger.warn(`Unable to fetch model file metadata for total progress tracking: ${e}`);
}
if (Object.keys(files_loading).length > 0) {
options.progress_callback = new DefaultProgressCallback(progress_callback, files_loading);
}
}
const sessions = typeConfig.sessions(config, options, textOnly);
const promises = [
constructSessions(pretrained_model_name_or_path, sessions, options, typeConfig.cache_sessions),
];
if (typeConfig.optional_configs) {
promises.push(get_optional_configs(pretrained_model_name_or_path, typeConfig.optional_configs, options));
}
const info = await Promise.all(promises);
// @ts-ignore
return new this(config, ...info);
}
/**
* Runs the model with the provided inputs
* @param {Object} model_inputs Object containing input tensors
* @returns {Promise<Object>} Object containing output tensors
*/
async _call(model_inputs) {
return await this.forward(model_inputs);
}
/**
* Forward method for a pretrained model. If not overridden by a subclass, the correct forward method
* will be chosen based on the model type.
* @param {Object} model_inputs The input data to the model in the format specified in the ONNX model.
* @returns {Promise<Object>} The output data from the model in the format specified in the ONNX model.
* @throws {Error} This method must be implemented in subclasses.
*/
async forward(model_inputs) {
return await this._forward(this, model_inputs);
}
/**
* Get the model's generation config, if it exists.
* @returns {GenerationConfig|null} The model's generation config if it exists, otherwise `null`.
*/
get generation_config() {
return this.configs?.generation_config ?? null;
}
/**
* @param {GenerationConfig} generation_config
* @param {number} input_ids_seq_length The starting sequence length for the input ids.
* @returns {LogitsProcessorList}
* @private
*/
_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) {
const begin_index =
input_ids_seq_length > 1 || generation_config.forced_bos_token_id === null
? input_ids_seq_length
: input_ids_seq_length + 1;
processors.push(
new SuppressTokensAtBeginLogitsProcessor(generation_config.begin_suppress_tokens, begin_index),
);
}
// DEPRECATED: https://github.com/huggingface/transformers/pull/29485
// if (generation_config.forced_decoder_ids !== null) {
// processors.push(new ForceTokensLogitsProcessor(generation_config.forced_decoder_ids));
// }
// 8. prepare batched CFG externally
if (generation_config.guidance_scale !== null && generation_config.guidance_scale > 1) {
processors.push(new ClassifierFreeGuidanceLogitsProcessor(generation_config.guidance_scale));
}
if (generation_config.temperature === 0 && generation_config.do_sample) {
logger.warn(
'`do_sample` changed to false because `temperature: 0` implies greedy sampling (always selecting the most likely token), which is incompatible with `do_sample: true`.',
);
generation_config.do_sample = false;
}
if (generation_config.do_sample) {
if (generation_config.temperature !== null && generation_config.temperature !== 1.0) {
processors.push(new TemperatureLogitsWarper(generation_config.temperature));
}
// TODO: Add TopPLogitsWarper and TopKLogitsWarper
// if (generation_config.top_k !== null && generation_config.top_k !== 0) {
// processors.push(new TopKLogitsWarper(generation_config.top_k));
// }
// if (generation_config.top_p !== null && generation_config.top_p < 1.0) {
// processors.push(new TopPLogitsWarper(generation_config.top_p));
// }
}
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;
}
/**
* This function merges multiple generation configs together to form a final generation config to be used by the model for text generation.
* It first creates an empty `GenerationConfig` object, then it applies the model's own `generation_config` property to it. Finally, if a `generation_config` object was passed in the arguments, it overwrites the corresponding properties in the final config with those of the passed config object.
* @param {GenerationConfig|null} generation_config A `GenerationConfig` object containing generation parameters.
* @param {Object} kwargs Additional generation parameters to be used in place of those in the `generation_config` object.
* @returns {GenerationConfig} The final generation config object to be used by the model for text generation.
*/
_prepare_generation_config(generation_config, kwargs, cls = GenerationConfig) {
// Create empty generation config (contains defaults)
// We pass `this.config` so that if `eos_token_id` or `bos_token_id` exist in the model's config, we will use them
const config = { ...this.config };
for (const key of ['decoder', 'generator', 'text_config']) {
// Special case: some models have generation attributes set in the decoder.
// Use them if still unset in the generation config.
if (key in config) {
Object.assign(config, config[key]);
}
}
const gen_config = new cls(config);
// Apply model's generation config, if it exists
Object.assign(gen_config, this.generation_config ?? {});
// Next, use any generation config specified by the user
// when calling `generate`
if (generation_config) {
Object.assign(gen_config, generation_config);
}
// Finally, if any kwargs were passed, use them to overwrite
if (kwargs) {
Object.assign(gen_config, pick(kwargs, Object.getOwnPropertyNames(gen_config)));
}
return gen_config;
}
/**
*
* @param {GenerationConfig} generation_config
* @param {import('../generation/stopping_criteria.js').StoppingCriteria|import('../generation/stopping_criteria.js').StoppingCriteria[]|StoppingCriteriaList} [stopping_criteria=null]
*/
_get_stopping_criteria(generation_config, stopping_criteria = null) {
const criteria = new StoppingCriteriaList();
if (generation_config.max_length !== null) {
criteria.push(
new MaxLengthCriteria(generation_config.max_length, this.config.max_position_embeddings ?? null),
);
}
// if (generation_config.max_time !== null) {
// criteria.push(new MaxTimeCriteria(generation_config.max_time));
// }
if (generation_config.eos_token_id !== null) {
criteria.push(new EosTokenCriteria(generation_config.eos_token_id));
}
if (stopping_criteria) {
criteria.extend(stopping_criteria);
}
return criteria;
}
/**
* Confirms that the model class is compatible with generation.
* If not, raises an exception that points to the right class to use.
*/
_validate_model_class() {
if (!this.can_generate) {
const generate_compatible_mappings = [
MODEL_MAPPING_NAMES.MODEL_FOR_CAUSAL_LM_MAPPING_NAMES,
// MODEL_MAPPING_NAMES.MODEL_FOR_CAUSAL_IMAGE_MODELING_MAPPING, // TODO
MODEL_MAPPING_NAMES.MODEL_FOR_VISION_2_SEQ_MAPPING_NAMES,
MODEL_MAPPING_NAMES.MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING_NAMES,
MODEL_MAPPING_NAMES.MODEL_FOR_SPEECH_SEQ_2_SEQ_MAPPING_NAMES,
].filter(Boolean); // Filter out null mappings (in case registry hasn't loaded yet)
const modelName = MODEL_CLASS_TO_NAME_MAPPING.get(this.constructor);
const generate_compatible_classes = new Set();
const modelType = this.config.model_type;
for (const model_mapping of generate_compatible_mappings) {
const supported_models = model_mapping?.get(modelType);
if (supported_models) {
generate_compatible_classes.add(supported_models);
}
}
let errorMessage = `The current model class (${modelName}) is not compatible with \`.generate()\`, as it doesn't have a language model head.`;
if (generate_compatible_classes.size > 0) {
errorMessage += ` Please use the following class instead: ${[...generate_compatible_classes].join(', ')}`;
}
throw Error(errorMessage);
}
}
prepare_inputs_for_generation(...args) {
if (!this._prepare_inputs_for_generation) {
throw new Error('prepare_inputs_for_generation is not implemented for this model.');
}
return this._prepare_inputs_for_generation(this, ...args);
}
/**
*
* @param {Object} inputs
* @param {bigint[][]} inputs.generated_input_ids
* @param {Object} inputs.outputs
* @param {Object} inputs.model_inputs
* @param {boolean} inputs.is_encoder_decoder
* @returns {Object} The updated model inputs for the next generation iteration.
*/
_update_model_kwargs_for_generation({ generated_input_ids, outputs, model_inputs, is_encoder_decoder }) {
// update past_key_values
model_inputs['past_key_values'] = getPastKeyValues(outputs, model_inputs.past_key_values);
// update inputs for next run
model_inputs['input_ids'] = new Tensor('int64', generated_input_ids.flat(), [generated_input_ids.length, 1]);
if (!is_encoder_decoder) {
// update attention mask
model_inputs.attention_mask = cat(
[model_inputs.attention_mask, ones([model_inputs.attention_mask.dims[0], 1])],
1,
);
} else if ('decoder_attention_mask' in model_inputs) {
model_inputs.decoder_attention_mask = cat(
[model_inputs.decoder_attention_mask, ones([model_inputs.decoder_attention_mask.dims[0], 1])],
1,
);
}
// force recreate position_ids in next iteration
model_inputs['position_ids'] = null;
return model_inputs;
}
/**
* This function extracts the model-specific `inputs` for generation.
* @param {Object} params
* @param {Tensor} [params.inputs=null]
* @param {number} [params.bos_token_id=null]
* @param {Record<string, Tensor|number[]>} [params.model_kwargs]
* @returns {{inputs_tensor: Tensor, model_inputs: Record<string, Tensor> & {past_key_values?: DynamicCache}, model_input_name: string}} The model-specific inputs for generation.
*/
_prepare_model_inputs({ inputs, bos_token_id, model_kwargs }) {
const model_inputs = pick(model_kwargs, this.forward_params);
const input_name = this.main_input_name;
if (input_name in model_inputs) {
if (inputs) {
throw new Error(
'`inputs`: {inputs}` were passed alongside {input_name} which is not allowed. ' +
'Make sure to either pass {inputs} or {input_name}=...',
);
}
} else {
model_inputs[input_name] = inputs;
}
const inputs_tensor = model_inputs[input_name];
return { inputs_tensor, model_inputs, model_input_name: input_name };
}
async _prepare_encoder_decoder_kwargs_for_generation({
inputs_tensor,
model_inputs,
model_input_name,
generation_config,
}) {
if (
this.sessions['model'].inputNames.includes('inputs_embeds') &&
!model_inputs.inputs_embeds &&
'_prepare_inputs_embeds' in this
) {
// Encoder expects `inputs_embeds` instead of `input_ids`
const { input_ids, pixel_values, attention_mask, ...kwargs } = model_inputs;
// @ts-ignore
const prepared_inputs = await this._prepare_inputs_embeds(model_inputs);
model_inputs = {
...kwargs,
...pick(prepared_inputs, ['inputs_embeds', 'attention_mask']),
};
}
let { last_hidden_state } = await encoder_forward(this, model_inputs);
// for classifier free guidance we need to add a 'null' input to our encoder hidden states
if (generation_config.guidance_scale !== null && generation_config.guidance_scale > 1) {
last_hidden_state = cat([last_hidden_state, full_like(last_hidden_state, 0.0)], 0);
if ('attention_mask' in model_inputs) {
model_inputs['attention_mask'] = cat(
[model_inputs['attention_mask'], zeros_like(model_inputs['attention_mask'])],
0,
);
}
} else if (model_inputs.decoder_input_ids) {
// Ensure that the encoder outputs have the same batch size as the decoder inputs,
// allowing for more efficient batched generation for single inputs
const decoder_input_ids_batch_size = toI64Tensor(model_inputs.decoder_input_ids).dims[0];
if (decoder_input_ids_batch_size !== last_hidden_state.dims[0]) {
if (last_hidden_state.dims[0] !== 1) {
throw new Error(
`The encoder outputs have a different batch size (${last_hidden_state.dims[0]}) than the decoder inputs (${decoder_input_ids_batch_size}).`,
);
}
last_hidden_state = cat(
Array.from({ length: decoder_input_ids_batch_size }, () => last_hidden_state),
0,
);
}
}
model_inputs['encoder_outputs'] = last_hidden_state;
return model_inputs;
}
/**
* Prepares `decoder_input_ids` for generation with encoder-decoder models
* @param {*} param0
*/
_prepare_decoder_input_ids_for_generation({
batch_size,
model_input_name,
model_kwargs,
decoder_start_token_id,
bos_token_id,
generation_config,
}) {
let { decoder_input_ids, ...model_inputs } = model_kwargs;
// Prepare input ids if the user has not defined `decoder_input_ids` manually.
if (!(decoder_input_ids instanceof Tensor)) {
if (!decoder_input_ids) {
decoder_start_token_id ??= bos_token_id;
if (this.config.model_type === 'musicgen') {
// Custom logic (TODO: move to Musicgen class)
decoder_input_ids = Array.from(
{
// @ts-expect-error TS2339
length: batch_size * this.config.decoder.num_codebooks,
},
() => [decoder_start_token_id],
);
} else if (Array.isArray(decoder_start_token_id)) {
if (decoder_start_token_id.length !== batch_size) {
throw new Error(
`\`decoder_start_token_id\` expcted to have length ${batch_size} but got ${decoder_start_token_id.length}`,
);
}
decoder_input_ids = decoder_start_token_id;
} else {
decoder_input_ids = Array.from(
{
length: batch_size,
},
() => [decoder_start_token_id],
);
}
} else if (!Array.isArray(decoder_input_ids[0])) {
// Correct batch size
decoder_input_ids = Array.from(
{
length: batch_size,
},
() => decoder_input_ids,
);
}
decoder_input_ids = toI64Tensor(decoder_input_ids);
}
model_inputs['decoder_attention_mask'] = ones_like(decoder_input_ids);
return { input_ids: decoder_input_ids, model_inputs };
}
/**
* Generates sequences of token ids for models with a language modeling head.
* @param {import('../generation/parameters.js').GenerationFunctionParameters} options
* @returns {Promise<ModelOutput|Tensor>} The output of the model, which can contain the generated token ids, attentions, and scores.
*/
async generate({
inputs = null,
generation_config = null,
logits_processor = null,
stopping_criteria = null,
streamer = null,
// inputs_attention_mask = null,
...kwargs
}) {
this._validate_model_class();
// Update generation config with defaults and kwargs
generation_config = this._prepare_generation_config(generation_config, kwargs);
// 3. Define model inputs
let { inputs_tensor, model_inputs, model_input_name } = this._prepare_model_inputs({
inputs,
model_kwargs: /** @type {Record<string, Tensor|number[]>} */ (kwargs),
});
const is_encoder_decoder = this.config.is_encoder_decoder;
// 4. Define other model kwargs
if (!is_encoder_decoder) {
// decoder-only models should use left-padding for generation
} else if (!('encoder_outputs' in model_inputs)) {
// if model is encoder decoder encoder_outputs are created
// and added to `model_kwargs`
model_inputs = await this._prepare_encoder_decoder_kwargs_for_generation({
inputs_tensor,
model_inputs,
model_input_name,
generation_config,
});
}
// 5. Prepare `input_ids` which will be used for auto-regressive generation
// TODO: Update to align with HF transformers' implementation
let input_ids;
if (is_encoder_decoder) {
// Generating from the encoder outputs
({ input_ids, model_inputs } = this._prepare_decoder_input_ids_for_generation({
batch_size: model_inputs[model_input_name].dims.at(0),
model_input_name,
model_kwargs: model_inputs,
decoder_start_token_id: generation_config.decoder_start_token_id,
bos_token_id: generation_config.bos_token_id,
generation_config,
}));
} else {
input_ids = model_inputs[model_input_name];
}
// 6. Prepare `max_length` depending on other stopping criteria.
let input_ids_length = input_ids.dims.at(-1);
if (generation_config.max_new_tokens !== null) {
generation_config.max_length = input_ids_length + generation_config.max_new_tokens;
}
// input_ids_length = model_inputs[model_input_name].dims.at(1);
// // inputs instanceof Tensor ? : inputs.length;
// // decoder-only
// if (input_ids_length === 0) {
// throw Error("Must supply a non-empty array of input token ids.")
// }
// let decoder_input_ids =
// generation_config.decoder_input_ids
// ?? generation_config.decoder_start_token_id
// ?? generation_config.bos_token_id
// ?? generation_config.eos_token_id;
// Update logits processor
// 8. prepare distribution pre_processing samplers
const prepared_logits_processor = this._get_logits_processor(
generation_config,
input_ids_length,
logits_processor,
);
// 9. prepare stopping criteria
const prepared_stopping_criteria = this._get_stopping_criteria(generation_config, stopping_criteria);
// /** @type {number[]} */
// let eos_token_ids = generation_config.eos_token_id;
// if (eos_token_ids !== null && !Array.isArray(eos_token_ids)) {
// eos_token_ids = [eos_token_ids];
// }
const numInputs = model_inputs[model_input_name].dims.at(0);
// TODO:
// done is a list of booleans to keep track of which inputs are done
// const done = new Array(numInputs).fill(false);
// For efficiency purposes, we remove completed rows from model_inputs
// when the beam is complete, and we keep track of the row index
// const rowIndexToBatchIndex = new Map();
const sampler = LogitsSampler.getSampler(generation_config);
// TODO make > numInputs
const scores = new Array(numInputs).fill(0);
/** @type {bigint[][]} */
const all_input_ids = input_ids.tolist();
if (streamer) {
streamer.put(all_input_ids);
}
// const all_generated_input_ids = Array.from({ length: numInputs }, () => []);
// NOTE: For now, we don't support spawning new beams
// TODO: when we do, we simply copy past key values and accumulate into single large tensor
////////////////////////////////////////////////////
// Generic search which handles 4 generation modes:
// - GenerationMode.GREEDY_SEARCH
// - GenerationMode.SAMPLE
// - GenerationMode.BEAM_SEARCH
// - GenerationMode.BEAM_SAMPLE
////////////////////////////////////////////////////
let outputs;
let attentions = {};
let return_dict_items = {};
while (true) {
// prepare model inputs
model_inputs = this.prepare_inputs_for_generation(all_input_ids, model_inputs, generation_config);
outputs = await this.forward(model_inputs);
if (generation_config.return_dict_in_generate) {
if (generation_config.output_attentions) {
// Get attentions if they are present
const token_attentions = getAttentions(outputs);
for (const key in token_attentions) {
if (!(key in attentions)) {
attentions[key] = [];
}
attentions[key].push(token_attentions[key]);
}
} else if (this._return_dict_in_generate_keys) {
Object.assign(return_dict_items, pick(outputs, this._return_dict_in_generate_keys));
}
}
// Logits are of the form [batch_size, out_seq_length, vocab_size]
// In most cases, this will be [batch_size, 1, vocab_size]
// So, we select the last token's logits:
// (equivalent to `logits = outputs.logits[:, -1, :]`)
// The `.to('float32')` is necessary for models with float16 logits,
// and is a no-op for float32 logits.
// TODO: Support float16 sampling in the sampler directly
const logits = outputs.logits.slice(null, -1, null).to('float32');
const next_tokens_scores = prepared_logits_processor(all_input_ids, logits);
/** @type {[bigint][]} */
const generated_input_ids = [];
// const new_kv_cache = [];// NOTE: Only used for beam search when concatenating new kv
// Loop over each batch
for (let batch_idx = 0; batch_idx < next_tokens_scores.dims.at(0); ++batch_idx) {
const logs = next_tokens_scores[batch_idx];
const sampledTokens = await sampler(logs);