masterofaudio2077 commited on
Commit
9cb81b6
·
verified ·
1 Parent(s): c13af62

Fix TypeError: EncoderDecoderCache is not subscriptable in prepare_inputs_for_generation

Browse files

prepare_inputs_for_generation (both Florence2LanguageForConditionalGeneration and Florence2ForConditionalGeneration -- identical code in both) read past_length via past_key_values[0][0].shape[2], assuming the legacy tuple-of-tuples KV cache format. Current transformers passes a Cache object (EncoderDecoderCache for encoder-decoder models like this one), which isn't subscriptable. Every Cache subclass implements get_seq_length() for exactly this purpose -- use it when available, falling back to the legacy tuple indexing for older transformers/custom cache objects that predate the Cache class.

Files changed (1) hide show
  1. modeling_florence2.py +14 -2
modeling_florence2.py CHANGED
@@ -2194,7 +2194,13 @@ class Florence2LanguageForConditionalGeneration(Florence2LanguagePreTrainedModel
2194
  ):
2195
  # cut decoder_input_ids if past_key_values is used
2196
  if past_key_values is not None:
2197
- past_length = past_key_values[0][0].shape[2]
 
 
 
 
 
 
2198
 
2199
  # Some generation methods already pass only the last input ID
2200
  if decoder_input_ids.shape[1] > past_length:
@@ -2823,7 +2829,13 @@ class Florence2ForConditionalGeneration(Florence2PreTrainedModel):
2823
  ):
2824
  # cut decoder_input_ids if past_key_values is used
2825
  if past_key_values is not None:
2826
- past_length = past_key_values[0][0].shape[2]
 
 
 
 
 
 
2827
 
2828
  # Some generation methods already pass only the last input ID
2829
  if decoder_input_ids.shape[1] > past_length:
 
2194
  ):
2195
  # cut decoder_input_ids if past_key_values is used
2196
  if past_key_values is not None:
2197
+ # past_key_values is a Cache object (e.g. EncoderDecoderCache) in current
2198
+ # transformers, not the legacy tuple-of-tuples this code was written for --
2199
+ # it isn't subscriptable, but every Cache subclass implements get_seq_length()
2200
+ if hasattr(past_key_values, "get_seq_length"):
2201
+ past_length = past_key_values.get_seq_length()
2202
+ else:
2203
+ past_length = past_key_values[0][0].shape[2]
2204
 
2205
  # Some generation methods already pass only the last input ID
2206
  if decoder_input_ids.shape[1] > past_length:
 
2829
  ):
2830
  # cut decoder_input_ids if past_key_values is used
2831
  if past_key_values is not None:
2832
+ # past_key_values is a Cache object (e.g. EncoderDecoderCache) in current
2833
+ # transformers, not the legacy tuple-of-tuples this code was written for --
2834
+ # it isn't subscriptable, but every Cache subclass implements get_seq_length()
2835
+ if hasattr(past_key_values, "get_seq_length"):
2836
+ past_length = past_key_values.get_seq_length()
2837
+ else:
2838
+ past_length = past_key_values[0][0].shape[2]
2839
 
2840
  # Some generation methods already pass only the last input ID
2841
  if decoder_input_ids.shape[1] > past_length: