/
input_generators.py
1397 lines (1231 loc) 路 52 KB
/
input_generators.py
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
# coding=utf-8
# Copyright 2022 The HuggingFace Team. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Dummy input generation classes."""
import functools
import random
from abc import ABC, abstractmethod
from typing import Any, List, Optional, Tuple, Union
import numpy as np
from transformers.utils import is_tf_available, is_torch_available
from .normalized_config import (
NormalizedConfig,
NormalizedEncoderDecoderConfig,
NormalizedSeq2SeqConfig,
NormalizedTextConfig,
NormalizedVisionConfig,
)
if is_torch_available():
import torch
if is_tf_available():
import tensorflow as tf
def check_framework_is_available(func):
@functools.wraps(func)
def wrapper(*args, **kwargs):
framework = kwargs.get("framework", "pt")
pt_asked_but_not_available = framework == "pt" and not is_torch_available()
tf_asked_but_not_available = framework == "tf" and not is_tf_available()
if (pt_asked_but_not_available or tf_asked_but_not_available) and framework != "np":
framework_name = "PyTorch" if framework == "pt" else "TensorFlow"
raise RuntimeError(f"Requested the {framework_name} framework, but it does not seem installed.")
return func(*args, **kwargs)
return wrapper
DEFAULT_DUMMY_SHAPES = {
"batch_size": 2,
"sequence_length": 16,
"num_choices": 4,
# image
"width": 64,
"height": 64,
"num_channels": 3,
"point_batch_size": 3,
"nb_points_per_image": 2,
# audio
"feature_size": 80,
"nb_max_frames": 3000,
"audio_sequence_length": 16000,
}
class DTYPE_MAPPER:
@classmethod
def np(cls, dtype):
mapping = {
"fp32": np.float32,
"fp16": np.float16,
"int64": np.int64,
"int32": np.int32,
"int8": np.int8,
"bool": bool,
}
return mapping[dtype]
@classmethod
def pt(cls, dtype):
mapping = {
"fp32": torch.float32,
"fp16": torch.float16,
"bf16": torch.bfloat16,
"int64": torch.int64,
"int32": torch.int32,
"int8": torch.int8,
"bool": torch.bool,
}
return mapping[dtype]
@classmethod
def tf(cls, dtype):
mapping = {
"fp32": tf.float32,
"fp16": tf.float16,
"bf16": tf.bfloat16,
"int64": tf.int64,
"int32": tf.int32,
"int8": tf.int8,
"bool": tf.bool,
}
return mapping[dtype]
class DummyInputGenerator(ABC):
"""
Generates dummy inputs for the supported input names, in the requested framework.
"""
SUPPORTED_INPUT_NAMES = ()
def supports_input(self, input_name: str) -> bool:
"""
Checks whether the `DummyInputGenerator` supports the generation of the requested input.
Args:
input_name (`str`):
The name of the input to generate.
Returns:
`bool`: A boolean specifying whether the input is supported.
"""
return any(input_name.startswith(supported_input_name) for supported_input_name in self.SUPPORTED_INPUT_NAMES)
@abstractmethod
def generate(self, input_name: str, framework: str = "pt", int_dtype: str = "int64", float_dtype: str = "fp32"):
"""
Generates the dummy input matching `input_name` for the requested framework.
Args:
input_name (`str`):
The name of the input to generate.
framework (`str`, defaults to `"pt"`):
The requested framework.
int_dtype (`str`, defaults to `"int64"`):
The dtypes of generated integer tensors.
float_dtype (`str`, defaults to `"fp32"`):
The dtypes of generated float tensors.
Returns:
A tensor in the requested framework of the input.
"""
raise NotImplementedError
@staticmethod
@check_framework_is_available
def random_int_tensor(
shape: List[int], max_value: int, min_value: int = 0, framework: str = "pt", dtype: str = "int64"
):
"""
Generates a tensor of random integers in the [min_value, max_value) range.
Args:
shape (`List[int]`):
The shape of the random tensor.
max_value (`int`):
The maximum value allowed.
min_value (`int`, defaults to 0):
The minimum value allowed.
framework (`str`, defaults to `"pt"`):
The requested framework.
dtype (`str`, defaults to `"int64"`):
The dtype of the generated integer tensor. Could be "int64", "int32", "int8".
Returns:
A random tensor in the requested framework.
"""
if framework == "pt":
return torch.randint(low=min_value, high=max_value, size=shape, dtype=DTYPE_MAPPER.pt(dtype))
elif framework == "tf":
return tf.random.uniform(shape, minval=min_value, maxval=max_value, dtype=DTYPE_MAPPER.tf(dtype))
else:
return np.random.randint(min_value, high=max_value, size=shape, dtype=DTYPE_MAPPER.np(dtype))
@staticmethod
@check_framework_is_available
def random_mask_tensor(shape: List[int], padding_side: str = "right", framework: str = "pt", dtype: str = "int64"):
"""
Generates a mask tensor either right or left padded.
Args:
shape (`List[int]`):
The shape of the random tensor.
padding_side (`str`, defaults to "right"):
The side on which the padding is applied.
framework (`str`, defaults to `"pt"`):
The requested framework.
dtype (`str`, defaults to `"int64"`):
The dtype of the generated integer tensor. Could be "int64", "int32", "int8".
Returns:
A random mask tensor either left padded or right padded in the requested framework.
"""
shape = tuple(shape)
mask_length = random.randint(1, shape[-1] - 1)
if framework == "pt":
mask_tensor = torch.cat(
[
torch.ones(*shape[:-1], shape[-1] - mask_length, dtype=DTYPE_MAPPER.pt(dtype)),
torch.zeros(*shape[:-1], mask_length, dtype=DTYPE_MAPPER.pt(dtype)),
],
dim=-1,
)
if padding_side == "left":
mask_tensor = torch.flip(mask_tensor, [-1])
elif framework == "tf":
mask_tensor = tf.concat(
[
tf.ones((*shape[:-1], shape[-1] - mask_length), dtype=DTYPE_MAPPER.tf(dtype)),
tf.zeros((*shape[:-1], mask_length), dtype=DTYPE_MAPPER.tf(dtype)),
],
axis=-1,
)
if padding_side == "left":
mask_tensor = tf.reverse(mask_tensor, [-1])
else:
mask_tensor = np.concatenate(
[
np.ones((*shape[:-1], shape[-1] - mask_length), dtype=DTYPE_MAPPER.np(dtype)),
np.zeros((*shape[:-1], mask_length), dtype=DTYPE_MAPPER.np(dtype)),
],
axis=-1,
)
if padding_side == "left":
mask_tensor = np.flip(mask_tensor, [-1])
return mask_tensor
@staticmethod
@check_framework_is_available
def random_float_tensor(
shape: List[int], min_value: float = 0, max_value: float = 1, framework: str = "pt", dtype: str = "fp32"
):
"""
Generates a tensor of random floats in the [min_value, max_value) range.
Args:
shape (`List[int]`):
The shape of the random tensor.
min_value (`float`, defaults to 0):
The minimum value allowed.
max_value (`float`, defaults to 1):
The maximum value allowed.
framework (`str`, defaults to `"pt"`):
The requested framework.
dtype (`str`, defaults to `"fp32"`):
The dtype of the generated float tensor. Could be "fp32", "fp16", "bf16".
Returns:
A random tensor in the requested framework.
"""
if framework == "pt":
tensor = torch.empty(shape, dtype=DTYPE_MAPPER.pt(dtype)).uniform_(min_value, max_value)
return tensor
elif framework == "tf":
return tf.random.uniform(shape, minval=min_value, maxval=max_value, dtype=DTYPE_MAPPER.tf(dtype))
else:
return np.random.uniform(low=min_value, high=max_value, size=shape).astype(DTYPE_MAPPER.np(dtype))
@staticmethod
@check_framework_is_available
def constant_tensor(
shape: List[int], value: Union[int, float] = 1, dtype: Optional[Any] = None, framework: str = "pt"
):
"""
Generates a constant tensor.
Args:
shape (`List[int]`):
The shape of the constant tensor.
value (`Union[int, float]`, defaults to 1):
The value to fill the constant tensor with.
dtype (`Optional[Any]`, defaults to `None`):
The dtype of the constant tensor.
framework (`str`, defaults to `"pt"`):
The requested framework.
Returns:
A constant tensor in the requested framework.
"""
if framework == "pt":
return torch.full(shape, value, dtype=dtype)
elif framework == "tf":
return tf.constant(value, dtype=dtype, shape=shape)
else:
return np.full(shape, value, dtype=dtype)
@staticmethod
def _infer_framework_from_input(input_) -> str:
framework = None
if is_torch_available() and isinstance(input_, torch.Tensor):
framework = "pt"
elif is_tf_available() and isinstance(input_, tf.Tensor):
framework = "tf"
elif isinstance(input_, np.ndarray):
framework = "np"
else:
raise RuntimeError(f"Could not infer the framework from {input_}")
return framework
@classmethod
def concat_inputs(cls, inputs, dim: int):
"""
Concatenates inputs together.
Args:
inputs:
The list of tensors in a given framework to concatenate.
dim (`int`):
The dimension along which to concatenate.
Returns:
The tensor of the concatenation.
"""
if not inputs:
raise ValueError("You did not provide any inputs to concat")
framework = cls._infer_framework_from_input(inputs[0])
if framework == "pt":
return torch.cat(inputs, dim=dim)
elif framework == "tf":
return tf.concat(inputs, axis=dim)
else:
return np.concatenate(inputs, axis=dim)
@classmethod
def pad_input_on_dim(
cls,
input_,
dim: int,
desired_length: Optional[int] = None,
padding_length: Optional[int] = None,
value: Union[int, float] = 1,
dtype: Optional[Any] = None,
):
"""
Pads an input either to the desired length, or by a padding length.
Args:
input_:
The tensor to pad.
dim (`int`):
The dimension along which to pad.
desired_length (`Optional[int]`, defaults to `None`):
The desired length along the dimension after padding.
padding_length (`Optional[int]`, defaults to `None`):
The length to pad along the dimension.
value (`Union[int, float]`, defaults to 1):
The value to use for padding.
dtype (`Optional[Any]`, defaults to `None`):
The dtype of the padding.
Returns:
The padded tensor.
"""
if (desired_length is None and padding_length is None) or (
desired_length is not None and padding_length is not None
):
raise ValueError("You need to provide either `desired_length` or `padding_length`")
framework = cls._infer_framework_from_input(input_)
shape = input_.shape
padding_shape = list(shape)
diff = desired_length - shape[dim] if desired_length else padding_length
if diff <= 0:
return input_
padding_shape[dim] = diff
return cls.concat_inputs(
[input_, cls.constant_tensor(padding_shape, value=value, dtype=dtype, framework=framework)], dim=dim
)
class DummyTextInputGenerator(DummyInputGenerator):
"""
Generates dummy encoder text inputs.
"""
SUPPORTED_INPUT_NAMES = (
"input_ids",
"attention_mask",
"encoder_attention_mask",
"token_type_ids",
"position_ids",
)
def __init__(
self,
task: str,
normalized_config: NormalizedTextConfig,
batch_size: int = DEFAULT_DUMMY_SHAPES["batch_size"],
sequence_length: int = DEFAULT_DUMMY_SHAPES["sequence_length"],
num_choices: int = DEFAULT_DUMMY_SHAPES["num_choices"],
random_batch_size_range: Optional[Tuple[int, int]] = None,
random_sequence_length_range: Optional[Tuple[int, int]] = None,
random_num_choices_range: Optional[Tuple[int, int]] = None,
padding_side: str = "right",
**kwargs,
):
self.task = task
if isinstance(normalized_config, NormalizedEncoderDecoderConfig):
self.vocab_size = normalized_config.vocab_size
else:
self.vocab_size = normalized_config.vocab_size
if random_batch_size_range:
low, high = random_batch_size_range
self.batch_size = random.randint(low, high)
else:
self.batch_size = batch_size
if random_sequence_length_range:
low, high = random_sequence_length_range
self.sequence_length = random.randint(low, high)
else:
self.sequence_length = sequence_length
if random_num_choices_range:
low, high = random_num_choices_range
self.num_choices = random.randint(low, high)
else:
self.num_choices = num_choices
self.padding_side = padding_side
self.normalized_config = normalized_config
def generate(
self,
input_name: str,
framework: str = "pt",
int_dtype: str = "int64",
float_dtype: str = "fp32",
):
min_value = 0
max_value = 2 if input_name != "input_ids" else self.vocab_size
shape = [self.batch_size, self.sequence_length]
if self.task == "multiple-choice":
shape = [self.batch_size, self.num_choices, self.sequence_length]
if "mask" in input_name:
return self.random_mask_tensor(shape, padding_side=self.padding_side, framework=framework, dtype=int_dtype)
else:
return self.random_int_tensor(shape, max_value, min_value=min_value, framework=framework, dtype=int_dtype)
class DummyXPathSeqInputGenerator(DummyTextInputGenerator):
"""
Generates dummy xpath sequences.
"""
SUPPORTED_INPUT_NAMES = (
"xpath_tags_seq",
"xpath_subs_seq",
)
def __init__(
self,
task: str,
normalized_config: NormalizedTextConfig,
batch_size: int = DEFAULT_DUMMY_SHAPES["batch_size"],
sequence_length: int = DEFAULT_DUMMY_SHAPES["sequence_length"],
num_choices: int = DEFAULT_DUMMY_SHAPES["num_choices"],
random_batch_size_range: Optional[Tuple[int, int]] = None,
random_sequence_length_range: Optional[Tuple[int, int]] = None,
random_num_choices_range: Optional[Tuple[int, int]] = None,
padding_side: str = "right",
**kwargs,
):
super().__init__(
task,
normalized_config,
batch_size=batch_size,
sequence_length=sequence_length,
num_choices=num_choices,
random_batch_size_range=random_batch_size_range,
random_sequence_length_range=random_sequence_length_range,
random_num_choices_range=random_num_choices_range,
padding_side=padding_side,
**kwargs,
)
self.max_depth = normalized_config.max_depth
self.tag_pad_id = normalized_config.tag_pad_id
self.subs_pad_id = normalized_config.subs_pad_id
def generate(
self,
input_name: str,
framework: str = "pt",
int_dtype: str = "int64",
float_dtype: str = "fp32",
):
min_value = 0
max_value = self.tag_pad_id if input_name == "xpath_tags_seq" else self.subs_pad_id
shape = [self.batch_size, self.sequence_length, self.max_depth]
return self.random_int_tensor(shape, max_value, min_value=min_value, framework=framework, dtype=int_dtype)
class DummyDecoderTextInputGenerator(DummyTextInputGenerator):
"""
Generates dummy decoder text inputs.
"""
SUPPORTED_INPUT_NAMES = (
"decoder_input_ids",
"decoder_attention_mask",
)
class DummySeq2SeqDecoderTextInputGenerator(DummyDecoderTextInputGenerator):
SUPPORTED_INPUT_NAMES = (
"decoder_input_ids",
"decoder_attention_mask",
"encoder_outputs",
"encoder_hidden_states",
)
def __init__(
self,
task: str,
normalized_config: NormalizedTextConfig,
batch_size: int = DEFAULT_DUMMY_SHAPES["batch_size"],
sequence_length: int = DEFAULT_DUMMY_SHAPES["sequence_length"],
num_choices: int = DEFAULT_DUMMY_SHAPES["num_choices"],
random_batch_size_range: Optional[Tuple[int, int]] = None,
random_sequence_length_range: Optional[Tuple[int, int]] = None,
random_num_choices_range: Optional[Tuple[int, int]] = None,
**kwargs,
):
super().__init__(
task,
normalized_config,
batch_size=batch_size,
sequence_length=sequence_length,
num_choices=num_choices,
random_batch_size_range=random_batch_size_range,
random_sequence_length_range=random_sequence_length_range,
random_num_choices_range=random_num_choices_range,
)
if isinstance(normalized_config, NormalizedEncoderDecoderConfig):
self.hidden_size = normalized_config.ENCODER_NORMALIZED_CONFIG_CLASS.hidden_size
else:
self.hidden_size = normalized_config.hidden_size
def generate(self, input_name: str, framework: str = "pt", int_dtype: str = "int64", float_dtype: str = "fp32"):
if input_name in ["encoder_outputs", "encoder_hidden_states"]:
return (
self.random_float_tensor(
shape=[self.batch_size, self.sequence_length, self.hidden_size],
min_value=0,
max_value=1,
framework=framework,
dtype=float_dtype,
),
None,
None,
)
return super().generate(input_name, framework=framework, int_dtype=int_dtype)
class DummyPastKeyValuesGenerator(DummyInputGenerator):
"""
Generates dummy past_key_values inputs.
"""
SUPPORTED_INPUT_NAMES = ("past_key_values",)
def __init__(
self,
task: str,
normalized_config: NormalizedTextConfig,
batch_size: int = DEFAULT_DUMMY_SHAPES["batch_size"],
sequence_length: int = DEFAULT_DUMMY_SHAPES["sequence_length"],
random_batch_size_range: Optional[Tuple[int, int]] = None,
random_sequence_length_range: Optional[Tuple[int, int]] = None,
**kwargs,
):
self.num_layers = normalized_config.num_layers
self.num_attention_heads = normalized_config.num_attention_heads
self.hidden_size = normalized_config.hidden_size
if random_batch_size_range:
low, high = random_batch_size_range
self.batch_size = random.randint(low, high)
else:
self.batch_size = batch_size
if random_sequence_length_range:
low, high = random_sequence_length_range
self.sequence_length = random.randint(low, high)
else:
self.sequence_length = sequence_length
def generate(self, input_name: str, framework: str = "pt", int_dtype: str = "int64", float_dtype: str = "fp32"):
shape = (
self.batch_size,
self.num_attention_heads,
self.sequence_length,
self.hidden_size // self.num_attention_heads,
)
return [
(
self.random_float_tensor(shape, framework=framework, dtype=float_dtype),
self.random_float_tensor(shape, framework=framework, dtype=float_dtype),
)
for _ in range(self.num_layers)
]
class DummySeq2SeqPastKeyValuesGenerator(DummyInputGenerator):
"""
Generates dummy past_key_values inputs for seq2seq architectures.
"""
SUPPORTED_INPUT_NAMES = ("past_key_values",)
def __init__(
self,
task: str,
normalized_config: Union[NormalizedSeq2SeqConfig, NormalizedEncoderDecoderConfig],
batch_size: int = DEFAULT_DUMMY_SHAPES["batch_size"],
sequence_length: int = DEFAULT_DUMMY_SHAPES["sequence_length"],
encoder_sequence_length: Optional[int] = None,
random_batch_size_range: Optional[Tuple[int, int]] = None,
random_sequence_length_range: Optional[Tuple[int, int]] = None,
**kwargs,
):
self.normalized_config = normalized_config
if random_batch_size_range:
low, high = random_batch_size_range
self.batch_size = random.randint(low, high)
else:
self.batch_size = batch_size
if random_sequence_length_range:
low, high = random_sequence_length_range
self.sequence_length = random.randint(low, high)
else:
self.sequence_length = sequence_length
self.encoder_sequence_length = (
self.sequence_length if encoder_sequence_length is None else encoder_sequence_length
)
if isinstance(normalized_config, NormalizedEncoderDecoderConfig):
# encoder_num_attention_heads / decoder_num_attention_heads are bad names, they rather refer to cross / self attention num heads.
self.encoder_num_attention_heads = (
self.normalized_config.DECODER_NORMALIZED_CONFIG_CLASS.encoder_num_attention_heads
)
self.decoder_num_attention_heads = (
self.normalized_config.DECODER_NORMALIZED_CONFIG_CLASS.decoder_num_attention_heads
)
# Same, `encoder_hidden_size` and `decoder_hidden_size` are bad names.
self.encoder_hidden_size = self.normalized_config.DECODER_NORMALIZED_CONFIG_CLASS.hidden_size
self.decoder_hidden_size = self.normalized_config.DECODER_NORMALIZED_CONFIG_CLASS.hidden_size
self.decoder_num_layers = self.normalized_config.DECODER_NORMALIZED_CONFIG_CLASS.num_layers
else:
self.encoder_num_attention_heads = self.normalized_config.encoder_num_attention_heads
self.decoder_num_attention_heads = self.normalized_config.decoder_num_attention_heads
self.encoder_hidden_size = self.normalized_config.hidden_size
self.decoder_hidden_size = self.normalized_config.hidden_size
self.decoder_num_layers = self.normalized_config.decoder_num_layers
def generate(self, input_name: str, framework: str = "pt", int_dtype: str = "int64", float_dtype: str = "fp32"):
encoder_shape = (
self.batch_size,
self.encoder_num_attention_heads,
self.encoder_sequence_length,
self.encoder_hidden_size // self.encoder_num_attention_heads,
)
decoder_shape = (
self.batch_size,
self.decoder_num_attention_heads,
self.sequence_length,
self.decoder_hidden_size // self.decoder_num_attention_heads,
)
return [
(
self.random_float_tensor(decoder_shape, framework=framework, dtype=float_dtype),
self.random_float_tensor(decoder_shape, framework=framework, dtype=float_dtype),
self.random_float_tensor(encoder_shape, framework=framework, dtype=float_dtype),
self.random_float_tensor(encoder_shape, framework=framework, dtype=float_dtype),
)
for _ in range(self.decoder_num_layers)
]
# TODO: should it just be merged to DummyTextInputGenerator?
class DummyBboxInputGenerator(DummyInputGenerator):
"""
Generates dummy bbox inputs.
"""
SUPPORTED_INPUT_NAMES = ("bbox",)
def __init__(
self,
task: str,
normalized_config: NormalizedConfig,
batch_size: int = DEFAULT_DUMMY_SHAPES["batch_size"],
sequence_length: int = DEFAULT_DUMMY_SHAPES["sequence_length"],
random_batch_size_range: Optional[Tuple[int, int]] = None,
random_sequence_length_range: Optional[Tuple[int, int]] = None,
**kwargs,
):
self.task = task
# self.max_2d_position_embeddings = normalized_config.max_2d_position_embeddings
if random_batch_size_range:
low, high = random_batch_size_range
self.batch_size = random.randint(low, high)
else:
self.batch_size = batch_size
if random_sequence_length_range:
low, high = random_sequence_length_range
self.sequence_length = random.randint(low, high)
else:
self.sequence_length = sequence_length
def generate(self, input_name: str, framework: str = "pt", int_dtype: str = "int64", float_dtype: str = "fp32"):
return self.random_int_tensor(
[self.batch_size, self.sequence_length, 4],
# TODO: find out why this fails with the commented code.
1, # self.max_2d_position_embeddings - 1,
framework=framework,
dtype=int_dtype,
)
class DummyVisionInputGenerator(DummyInputGenerator):
"""
Generates dummy vision inputs.
"""
SUPPORTED_INPUT_NAMES = (
"pixel_values",
"pixel_mask",
"sample",
"latent_sample",
)
def __init__(
self,
task: str,
normalized_config: NormalizedVisionConfig,
batch_size: int = DEFAULT_DUMMY_SHAPES["batch_size"],
num_channels: int = DEFAULT_DUMMY_SHAPES["num_channels"],
width: int = DEFAULT_DUMMY_SHAPES["width"],
height: int = DEFAULT_DUMMY_SHAPES["height"],
**kwargs,
):
self.task = task
# Some vision models can take any input sizes, in this case we use the values provided as parameters.
if normalized_config.has_attribute("num_channels"):
self.num_channels = normalized_config.num_channels
else:
self.num_channels = num_channels
if normalized_config.has_attribute("image_size"):
self.image_size = normalized_config.image_size
elif normalized_config.has_attribute("input_size"):
input_size = normalized_config.input_size
self.num_channels = input_size[0]
self.image_size = input_size[1:]
else:
self.image_size = (height, width)
if not isinstance(self.image_size, (tuple, list)):
self.image_size = (self.image_size, self.image_size)
self.batch_size = batch_size
self.height, self.width = self.image_size
def generate(self, input_name: str, framework: str = "pt", int_dtype: str = "int64", float_dtype: str = "fp32"):
if input_name == "pixel_mask":
return self.random_int_tensor(
shape=[self.batch_size, self.height, self.width],
max_value=1,
framework=framework,
dtype=int_dtype,
)
else:
return self.random_float_tensor(
shape=[self.batch_size, self.num_channels, self.height, self.width],
framework=framework,
dtype=float_dtype,
)
class DummyAudioInputGenerator(DummyInputGenerator):
SUPPORTED_INPUT_NAMES = ("input_features", "input_values")
def __init__(
self,
task: str,
normalized_config: NormalizedConfig,
batch_size: int = DEFAULT_DUMMY_SHAPES["batch_size"],
feature_size: int = DEFAULT_DUMMY_SHAPES["feature_size"],
nb_max_frames: int = DEFAULT_DUMMY_SHAPES["nb_max_frames"],
audio_sequence_length: int = DEFAULT_DUMMY_SHAPES["audio_sequence_length"],
**kwargs,
):
self.task = task
self.normalized_config = normalized_config
if hasattr(self.normalized_config, "feature_size"):
self.feature_size = self.normalized_config.feature_size
else:
self.feature_size = feature_size
self.nb_max_frames = nb_max_frames
self.batch_size = batch_size
self.sequence_length = audio_sequence_length
def generate(self, input_name: str, framework: str = "pt", int_dtype: str = "int64", float_dtype: str = "fp32"):
if input_name == "input_values": # raw waveform
return self.random_float_tensor(
shape=[self.batch_size, self.sequence_length],
min_value=-1,
max_value=1,
framework=framework,
dtype=float_dtype,
)
else:
return self.random_float_tensor(
shape=[self.batch_size, self.feature_size, self.nb_max_frames],
min_value=-1,
max_value=1,
framework=framework,
dtype=float_dtype,
)
class DummyTimestepInputGenerator(DummyInputGenerator):
"""
Generates dummy time step inputs.
"""
SUPPORTED_INPUT_NAMES = (
"timestep",
"text_embeds",
"time_ids",
"timestep_cond",
)
def __init__(
self,
task: str,
normalized_config: NormalizedConfig,
batch_size: int = DEFAULT_DUMMY_SHAPES["batch_size"],
random_batch_size_range: Optional[Tuple[int, int]] = None,
**kwargs,
):
self.task = task
self.vocab_size = normalized_config.vocab_size
self.text_encoder_projection_dim = normalized_config.text_encoder_projection_dim
self.time_ids = 5 if normalized_config.requires_aesthetics_score else 6
if random_batch_size_range:
low, high = random_batch_size_range
self.batch_size = random.randint(low, high)
else:
self.batch_size = batch_size
self.time_cond_proj_dim = normalized_config.config.time_cond_proj_dim
def generate(self, input_name: str, framework: str = "pt", int_dtype: str = "int64", float_dtype: str = "fp32"):
if input_name == "timestep":
shape = [self.batch_size]
return self.random_int_tensor(shape, max_value=self.vocab_size, framework=framework, dtype=int_dtype)
if input_name == "text_embeds":
dim = self.text_encoder_projection_dim
elif input_name == "timestep_cond":
dim = self.time_cond_proj_dim
else:
dim = self.time_ids
shape = [self.batch_size, dim]
return self.random_float_tensor(shape, max_value=self.vocab_size, framework=framework, dtype=float_dtype)
class DummyLabelsGenerator(DummyInputGenerator):
SUPPORTED_INPUT_NAMES = (
"labels",
"start_positions",
"end_positions",
)
def __init__(
self,
task: str,
normalized_config: NormalizedConfig,
batch_size: int = DEFAULT_DUMMY_SHAPES["batch_size"],
random_batch_size_range: Optional[Tuple[int, int]] = None,
**kwargs,
):
self.task = task
if random_batch_size_range:
low, high = random_batch_size_range
self.batch_size = random.randint(low, high)
else:
self.batch_size = batch_size
self.sequence_length = kwargs.get("sequence_length", None)
self.num_labels = kwargs.get("num_labels", None)
def generate(self, input_name: str, framework: str = "pt", int_dtype: str = "int64", float_dtype: str = "fp32"):
max_value = self.num_labels if self.num_labels is not None else 0
if self.sequence_length is None:
shape = [self.batch_size]
else:
shape = [self.batch_size, self.sequence_length]
return self.random_int_tensor(shape, max_value=max_value, framework=framework, dtype=int_dtype)
class DummyPointsGenerator(DummyInputGenerator):
"""
Generates dummy time step inputs.
"""
SUPPORTED_INPUT_NAMES = ("input_points", "input_labels")
def __init__(
self,
task: str,
normalized_config: NormalizedConfig,
batch_size: int = DEFAULT_DUMMY_SHAPES["batch_size"],
point_batch_size: int = DEFAULT_DUMMY_SHAPES["point_batch_size"],
nb_points_per_image: int = DEFAULT_DUMMY_SHAPES["nb_points_per_image"],
**kwargs,
):
self.task = task
self.batch_size = batch_size
self.point_batch_size = point_batch_size
self.nb_points_per_image = nb_points_per_image
def generate(self, input_name: str, framework: str = "pt", int_dtype: str = "int64", float_dtype: str = "fp32"):
if input_name == "input_points":
shape = [self.batch_size, self.point_batch_size, self.nb_points_per_image, 2]
return self.random_float_tensor(shape, framework=framework, dtype=float_dtype)
else: # input_labels
shape = [self.batch_size, self.point_batch_size, self.nb_points_per_image]
return self.random_int_tensor(shape, min_value=0, max_value=1, framework=framework, dtype=int_dtype)
class DummyVisionEmbeddingsGenerator(DummyInputGenerator):
SUPPORTED_INPUT_NAMES = ("image_positional_embeddings", "image_embeddings")
def __init__(
self,
task: str,
normalized_config: NormalizedConfig,
batch_size: int = DEFAULT_DUMMY_SHAPES["batch_size"],
image_embedding_size: Optional[int] = None,
output_channels: Optional[int] = None,
**kwargs,
):
self.task = task
self.batch_size = batch_size
self.image_embedding_size = (
image_embedding_size
if image_embedding_size is not None
else normalized_config.prompt_encoder_config.image_embedding_size
)
self.output_channels = (
output_channels if output_channels is not None else normalized_config.vision_config.output_channels
)
def generate(self, input_name: str, framework: str = "pt", int_dtype: str = "int64", float_dtype: str = "fp32"):
shape = [self.batch_size, self.output_channels, self.image_embedding_size, self.image_embedding_size]
return self.random_float_tensor(shape, framework=framework)
class DummyPix2StructInputGenerator(DummyInputGenerator):
"""
Generates dummy time step inputs.
"""
SUPPORTED_INPUT_NAMES = ("flattened_patches",)
def __init__(
self,
task: str,
normalized_config: NormalizedConfig,
preprocessors: List[Any],
batch_size: int = DEFAULT_DUMMY_SHAPES["batch_size"],
num_channels: int = DEFAULT_DUMMY_SHAPES["num_channels"],
**kwargs,
):
self.task = task
self.batch_size = batch_size
# looking for static shapes in Pix2StructProcessor
patch_height = preprocessors[1].image_processor.patch_size["height"]
patch_width = preprocessors[1].image_processor.patch_size["width"]
self.flattened_patch_size = 2 + patch_height * patch_width * num_channels
self.max_patches = preprocessors[1].image_processor.max_patches
def generate(self, input_name: str, framework: str = "pt", int_dtype: str = "int64", float_dtype: str = "fp32"):
shape = [self.batch_size, self.max_patches, self.flattened_patch_size]
return self.random_float_tensor(shape, framework=framework, dtype=float_dtype)