Fix two transformers-v5 AttributeErrors in Florence-2 remote code

#122
configuration_florence2.py CHANGED
@@ -261,6 +261,7 @@ class Florence2LanguageConfig(PretrainedConfig):
261
  **kwargs,
262
  )
263
 
 
264
  # ensure backward compatibility for BART CNN models
265
  if self.forced_bos_token_id is None and kwargs.get("force_bos_token_to_be_generated", False):
266
  self.forced_bos_token_id = self.bos_token_id
 
261
  **kwargs,
262
  )
263
 
264
+ self.forced_bos_token_id = kwargs.get("forced_bos_token_id", None)
265
  # ensure backward compatibility for BART CNN models
266
  if self.forced_bos_token_id is None and kwargs.get("force_bos_token_to_be_generated", False):
267
  self.forced_bos_token_id = self.bos_token_id
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:
@@ -2335,17 +2341,26 @@ class Florence2PreTrainedModel(PreTrainedModel):
2335
  def _supports_flash_attn_2(self):
2336
  """
2337
  Retrieve language_model's attribute to check whether the model supports
2338
- Flash Attention 2 or not.
 
 
 
2339
  """
2340
- return self.language_model._supports_flash_attn_2
 
 
 
2341
 
2342
  @property
2343
  def _supports_sdpa(self):
2344
  """
2345
  Retrieve language_model's attribute to check whether the model supports
2346
- SDPA or not.
2347
  """
2348
- return self.language_model._supports_sdpa
 
 
 
2349
 
2350
 
2351
  FLORENCE2_INPUTS_DOCSTRING = r"""
@@ -2814,7 +2829,13 @@ class Florence2ForConditionalGeneration(Florence2PreTrainedModel):
2814
  ):
2815
  # cut decoder_input_ids if past_key_values is used
2816
  if past_key_values is not None:
2817
- past_length = past_key_values[0][0].shape[2]
 
 
 
 
 
 
2818
 
2819
  # Some generation methods already pass only the last input ID
2820
  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:
 
2341
  def _supports_flash_attn_2(self):
2342
  """
2343
  Retrieve language_model's attribute to check whether the model supports
2344
+ Flash Attention 2 or not. PreTrainedModel.__init__ reads this before
2345
+ self.language_model is assigned (it's set partway through this class's
2346
+ own __init__, after super().__init__() runs), so fall back to the
2347
+ constant Florence2LanguageForConditionalGeneration always declares.
2348
  """
2349
+ language_model = getattr(self, "language_model", None)
2350
+ if language_model is None:
2351
+ return True
2352
+ return language_model._supports_flash_attn_2
2353
 
2354
  @property
2355
  def _supports_sdpa(self):
2356
  """
2357
  Retrieve language_model's attribute to check whether the model supports
2358
+ SDPA or not. Same early-access issue as _supports_flash_attn_2 above.
2359
  """
2360
+ language_model = getattr(self, "language_model", None)
2361
+ if language_model is None:
2362
+ return True
2363
+ return language_model._supports_sdpa
2364
 
2365
 
2366
  FLORENCE2_INPUTS_DOCSTRING = r"""
 
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:
processing_florence2.py CHANGED
@@ -86,7 +86,9 @@ class Florence2Processor(ProcessorMixin):
86
 
87
  tokens_to_add = {
88
  'additional_special_tokens': \
89
- tokenizer.additional_special_tokens + \
 
 
90
  ['<od>', '</od>', '<ocr>', '</ocr>'] + \
91
  [f'<loc_{x}>' for x in range(1000)] + \
92
  ['<cap>', '</cap>', '<ncap>', '</ncap>','<dcap>', '</dcap>', '<grounding>', '</grounding>', '<seg>', '</seg>', '<sep>', '<region_cap>', '</region_cap>', '<region_to_desciption>', '</region_to_desciption>', '<proposal>', '</proposal>', '<poly>', '</poly>', '<and>']
@@ -249,17 +251,29 @@ class Florence2Processor(ProcessorMixin):
249
  elif isinstance(text, list) and _is_str_or_image(text[0]):
250
  pass
251
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
252
  pixel_values = self.image_processor(
253
  images,
254
- do_resize=do_resize,
255
- do_normalize=do_normalize,
256
  return_tensors=return_tensors,
257
- image_mean=image_mean,
258
- image_std=image_std,
259
- input_data_format=input_data_format,
260
  data_format=data_format,
261
- resample=resample,
262
- do_convert_rgb=do_convert_rgb,
263
  )["pixel_values"]
264
 
265
  if max_length is not None:
 
86
 
87
  tokens_to_add = {
88
  'additional_special_tokens': \
89
+ (getattr(tokenizer, "extra_special_tokens", None)
90
+ if getattr(tokenizer, "extra_special_tokens", None) is not None
91
+ else getattr(tokenizer, "additional_special_tokens", [])) + \
92
  ['<od>', '</od>', '<ocr>', '</ocr>'] + \
93
  [f'<loc_{x}>' for x in range(1000)] + \
94
  ['<cap>', '</cap>', '<ncap>', '</ncap>','<dcap>', '</dcap>', '<grounding>', '</grounding>', '<seg>', '</seg>', '<sep>', '<region_cap>', '</region_cap>', '<region_to_desciption>', '</region_to_desciption>', '<proposal>', '</proposal>', '<poly>', '</poly>', '<and>']
 
251
  elif isinstance(text, list) and _is_str_or_image(text[0]):
252
  pass
253
 
254
+ # None-valued kwargs must not be forwarded: this image processor treats an
255
+ # explicit None as "disable this feature" rather than "use the configured
256
+ # default", so passing do_resize=None (the default here when the caller
257
+ # doesn't override it) silently skips resizing -- and do_normalize=None
258
+ # silently skips normalization, with no error either way.
259
+ image_processor_overrides = {
260
+ k: v
261
+ for k, v in {
262
+ "do_resize": do_resize,
263
+ "do_normalize": do_normalize,
264
+ "image_mean": image_mean,
265
+ "image_std": image_std,
266
+ "input_data_format": input_data_format,
267
+ "resample": resample,
268
+ "do_convert_rgb": do_convert_rgb,
269
+ }.items()
270
+ if v is not None
271
+ }
272
  pixel_values = self.image_processor(
273
  images,
 
 
274
  return_tensors=return_tensors,
 
 
 
275
  data_format=data_format,
276
+ **image_processor_overrides,
 
277
  )["pixel_values"]
278
 
279
  if max_length is not None: