Skip to content

Commit ef8d6f2

Browse files
authored
Set missing seq_length variable when using inputs_embeds with ALBERT & Remove code duplication (huggingface#13152)
* Set seq_length variable when using inputs_embeds * remove code duplication
1 parent 180c6de commit ef8d6f2

File tree

14 files changed

+14
-27
lines changed

14 files changed

+14
-27
lines changed

src/transformers/models/albert/modeling_albert.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -693,12 +693,12 @@ def forward(
693693
raise ValueError("You cannot specify both input_ids and inputs_embeds at the same time")
694694
elif input_ids is not None:
695695
input_shape = input_ids.size()
696-
batch_size, seq_length = input_shape
697696
elif inputs_embeds is not None:
698697
input_shape = inputs_embeds.size()[:-1]
699698
else:
700699
raise ValueError("You have to specify either input_ids or inputs_embeds")
701700

701+
batch_size, seq_length = input_shape
702702
device = input_ids.device if input_ids is not None else inputs_embeds.device
703703

704704
if attention_mask is None:

src/transformers/models/bert/modeling_bert.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -936,13 +936,12 @@ def forward(
936936
raise ValueError("You cannot specify both input_ids and inputs_embeds at the same time")
937937
elif input_ids is not None:
938938
input_shape = input_ids.size()
939-
batch_size, seq_length = input_shape
940939
elif inputs_embeds is not None:
941940
input_shape = inputs_embeds.size()[:-1]
942-
batch_size, seq_length = input_shape
943941
else:
944942
raise ValueError("You have to specify either input_ids or inputs_embeds")
945943

944+
batch_size, seq_length = input_shape
946945
device = input_ids.device if input_ids is not None else inputs_embeds.device
947946

948947
# past_key_values_length

src/transformers/models/bert_generation/modeling_bert_generation.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -353,13 +353,12 @@ def forward(
353353
raise ValueError("You cannot specify both input_ids and inputs_embeds at the same time")
354354
elif input_ids is not None:
355355
input_shape = input_ids.size()
356-
batch_size, seq_length = input_shape
357356
elif inputs_embeds is not None:
358357
input_shape = inputs_embeds.size()[:-1]
359-
batch_size, seq_length = input_shape
360358
else:
361359
raise ValueError("You have to specify either input_ids or inputs_embeds")
362360

361+
batch_size, seq_length = input_shape
363362
device = input_ids.device if input_ids is not None else inputs_embeds.device
364363

365364
# past_key_values_length

src/transformers/models/big_bird/modeling_big_bird.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2024,13 +2024,12 @@ def forward(
20242024
raise ValueError("You cannot specify both input_ids and inputs_embeds at the same time")
20252025
elif input_ids is not None:
20262026
input_shape = input_ids.size()
2027-
batch_size, seq_length = input_shape
20282027
elif inputs_embeds is not None:
20292028
input_shape = inputs_embeds.size()[:-1]
2030-
batch_size, seq_length = input_shape
20312029
else:
20322030
raise ValueError("You have to specify either input_ids or inputs_embeds")
20332031

2032+
batch_size, seq_length = input_shape
20342033
device = input_ids.device if input_ids is not None else inputs_embeds.device
20352034

20362035
# past_key_values_length

src/transformers/models/canine/modeling_canine.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1119,13 +1119,12 @@ def forward(
11191119
raise ValueError("You cannot specify both input_ids and inputs_embeds at the same time")
11201120
elif input_ids is not None:
11211121
input_shape = input_ids.size()
1122-
batch_size, seq_length = input_shape
11231122
elif inputs_embeds is not None:
11241123
input_shape = inputs_embeds.size()[:-1]
1125-
batch_size, seq_length = input_shape
11261124
else:
11271125
raise ValueError("You have to specify either input_ids or inputs_embeds")
11281126

1127+
batch_size, seq_length = input_shape
11291128
device = input_ids.device if input_ids is not None else inputs_embeds.device
11301129

11311130
if attention_mask is None:

src/transformers/models/ibert/modeling_ibert.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -803,13 +803,12 @@ def forward(
803803
raise ValueError("You cannot specify both input_ids and inputs_embeds at the same time")
804804
elif input_ids is not None:
805805
input_shape = input_ids.size()
806-
batch_size, seq_length = input_shape
807806
elif inputs_embeds is not None:
808807
input_shape = inputs_embeds.size()[:-1]
809-
batch_size, seq_length = input_shape
810808
else:
811809
raise ValueError("You have to specify either input_ids or inputs_embeds")
812810

811+
batch_size, seq_length = input_shape
813812
device = input_ids.device if input_ids is not None else inputs_embeds.device
814813

815814
if attention_mask is None:

src/transformers/models/luke/modeling_luke.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -887,13 +887,12 @@ def forward(
887887
raise ValueError("You cannot specify both input_ids and inputs_embeds at the same time")
888888
elif input_ids is not None:
889889
input_shape = input_ids.size()
890-
batch_size, seq_length = input_shape
891890
elif inputs_embeds is not None:
892891
input_shape = inputs_embeds.size()[:-1]
893-
batch_size, seq_length = input_shape
894892
else:
895893
raise ValueError("You have to specify either input_ids or inputs_embeds")
896894

895+
batch_size, seq_length = input_shape
897896
device = input_ids.device if input_ids is not None else inputs_embeds.device
898897

899898
if attention_mask is None:

src/transformers/models/megatron_bert/modeling_megatron_bert.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -924,13 +924,12 @@ def forward(
924924
raise ValueError("You cannot specify both input_ids and inputs_embeds at the same time")
925925
elif input_ids is not None:
926926
input_shape = input_ids.size()
927-
batch_size, seq_length = input_shape
928927
elif inputs_embeds is not None:
929928
input_shape = inputs_embeds.size()[:-1]
930-
batch_size, seq_length = input_shape
931929
else:
932930
raise ValueError("You have to specify either input_ids or inputs_embeds")
933931

932+
batch_size, seq_length = input_shape
934933
device = input_ids.device if input_ids is not None else inputs_embeds.device
935934

936935
# past_key_values_length

src/transformers/models/rembert/modeling_rembert.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -829,13 +829,12 @@ def forward(
829829
raise ValueError("You cannot specify both input_ids and inputs_embeds at the same time")
830830
elif input_ids is not None:
831831
input_shape = input_ids.size()
832-
batch_size, seq_length = input_shape
833832
elif inputs_embeds is not None:
834833
input_shape = inputs_embeds.size()[:-1]
835-
batch_size, seq_length = input_shape
836834
else:
837835
raise ValueError("You have to specify either input_ids or inputs_embeds")
838836

837+
batch_size, seq_length = input_shape
839838
device = input_ids.device if input_ids is not None else inputs_embeds.device
840839

841840
# past_key_values_length

src/transformers/models/roberta/modeling_roberta.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -789,13 +789,12 @@ def forward(
789789
raise ValueError("You cannot specify both input_ids and inputs_embeds at the same time")
790790
elif input_ids is not None:
791791
input_shape = input_ids.size()
792-
batch_size, seq_length = input_shape
793792
elif inputs_embeds is not None:
794793
input_shape = inputs_embeds.size()[:-1]
795-
batch_size, seq_length = input_shape
796794
else:
797795
raise ValueError("You have to specify either input_ids or inputs_embeds")
798796

797+
batch_size, seq_length = input_shape
799798
device = input_ids.device if input_ids is not None else inputs_embeds.device
800799

801800
# past_key_values_length

src/transformers/models/roformer/modeling_roformer.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -888,13 +888,12 @@ def forward(
888888
raise ValueError("You cannot specify both input_ids and inputs_embeds at the same time")
889889
elif input_ids is not None:
890890
input_shape = input_ids.size()
891-
batch_size, seq_length = input_shape
892891
elif inputs_embeds is not None:
893892
input_shape = inputs_embeds.size()[:-1]
894-
batch_size, seq_length = input_shape
895893
else:
896894
raise ValueError("You have to specify either input_ids or inputs_embeds")
897895

896+
batch_size, seq_length = input_shape
898897
device = input_ids.device if input_ids is not None else inputs_embeds.device
899898

900899
# past_key_values_length

src/transformers/models/splinter/modeling_splinter.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -683,13 +683,12 @@ def forward(
683683
raise ValueError("You cannot specify both input_ids and inputs_embeds at the same time")
684684
elif input_ids is not None:
685685
input_shape = input_ids.size()
686-
batch_size, seq_length = input_shape
687686
elif inputs_embeds is not None:
688687
input_shape = inputs_embeds.size()[:-1]
689-
batch_size, seq_length = input_shape
690688
else:
691689
raise ValueError("You have to specify either input_ids or inputs_embeds")
692690

691+
batch_size, seq_length = input_shape
693692
device = input_ids.device if input_ids is not None else inputs_embeds.device
694693

695694
# past_key_values_length

src/transformers/models/visual_bert/modeling_visual_bert.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -767,10 +767,8 @@ def forward(
767767
raise ValueError("You cannot specify both input_ids and inputs_embeds at the same time")
768768
elif input_ids is not None:
769769
input_shape = input_ids.size()
770-
batch_size, seq_length = input_shape
771770
elif inputs_embeds is not None:
772771
input_shape = inputs_embeds.size()[:-1]
773-
batch_size, seq_length = input_shape
774772
else:
775773
raise ValueError("You have to specify either input_ids or inputs_embeds")
776774

@@ -779,6 +777,7 @@ def forward(
779777
f"`visual_embeds` can not be of type {type(visual_embeds)} when using a VisualBert Model."
780778
)
781779

780+
batch_size, seq_length = input_shape
782781
device = input_ids.device if input_ids is not None else inputs_embeds.device
783782

784783
visual_input_shape = visual_embeds.size()[:-1]

templates/adding_a_new_model/cookiecutter-template-{{cookiecutter.modelname}}/modeling_{{cookiecutter.lowercase_modelname}}.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -846,13 +846,12 @@ def forward(
846846
raise ValueError("You cannot specify both input_ids and inputs_embeds at the same time")
847847
elif input_ids is not None:
848848
input_shape = input_ids.size()
849-
batch_size, seq_length = input_shape
850849
elif inputs_embeds is not None:
851850
input_shape = inputs_embeds.size()[:-1]
852-
batch_size, seq_length = input_shape
853851
else:
854852
raise ValueError("You have to specify either input_ids or inputs_embeds")
855853

854+
batch_size, seq_length = input_shape
856855
device = input_ids.device if input_ids is not None else inputs_embeds.device
857856

858857
# past_key_values_length

0 commit comments

Comments
 (0)