masterofaudio2077 commited on
Commit
c13af62
·
verified ·
1 Parent(s): 5e92a22

Fix AttributeError from _supports_sdpa read before language_model exists

Browse files

Florence2PreTrainedModel._supports_sdpa/_supports_flash_attn_2 are properties that forward to self.language_model._supports_sdpa. But self.language_model is only assigned partway through Florence2ForConditionalGeneration.__init__, after super().__init__(config) runs -- and this transformers version's PreTrainedModel.__init__ now reads self._supports_sdpa during that super().__init__() call, before language_model exists.

The resulting AttributeError from the inner self.language_model access gets silently reinterpreted by Python's descriptor protocol (property getter raises AttributeError -> falls through to nn.Module.__getattr__ -> raises a new AttributeError naming the outer attribute) -- hence the confusing "no attribute '_supports_sdpa'" message instead of the real cause.

Fall back to True (the constant Florence2LanguageForConditionalGeneration always declares for both flags) when language_model isn't set yet.

Files changed (1) hide show
  1. modeling_florence2.py +13 -4
modeling_florence2.py CHANGED
@@ -2335,17 +2335,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"""
 
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. PreTrainedModel.__init__ reads this before
2339
+ self.language_model is assigned (it's set partway through this class's
2340
+ own __init__, after super().__init__() runs), so fall back to the
2341
+ constant Florence2LanguageForConditionalGeneration always declares.
2342
  """
2343
+ language_model = getattr(self, "language_model", None)
2344
+ if language_model is None:
2345
+ return True
2346
+ return language_model._supports_flash_attn_2
2347
 
2348
  @property
2349
  def _supports_sdpa(self):
2350
  """
2351
  Retrieve language_model's attribute to check whether the model supports
2352
+ SDPA or not. Same early-access issue as _supports_flash_attn_2 above.
2353
  """
2354
+ language_model = getattr(self, "language_model", None)
2355
+ if language_model is None:
2356
+ return True
2357
+ return language_model._supports_sdpa
2358
 
2359
 
2360
  FLORENCE2_INPUTS_DOCSTRING = r"""