diff --git a/florence2_models/configuration_florence2.py b/florence2_models/configuration_florence2.py index 622f749..3b29888 100644 --- a/florence2_models/configuration_florence2.py +++ b/florence2_models/configuration_florence2.py @@ -262,7 +262,7 @@ class Florence2LanguageConfig(PretrainedConfig): ) # ensure backward compatibility for BART CNN models - if self.forced_bos_token_id is None and kwargs.get("force_bos_token_to_be_generated", False): + if getattr(self, 'forced_bos_token_id', None) is None and kwargs.get("force_bos_token_to_be_generated", False): self.forced_bos_token_id = self.bos_token_id warnings.warn( f"Please make sure the config includes `forced_bos_token_id={self.bos_token_id}` in future versions. " diff --git a/py/florence2_ultra.py b/py/florence2_ultra.py index a6374b3..ffcd8eb 100644 --- a/py/florence2_ultra.py +++ b/py/florence2_ultra.py @@ -43,6 +43,19 @@ def fixed_get_imports(filename) -> list[str]: pass return imports +def _patch_florence2_config_compat(): + """Patch PretrainedConfig for transformers 5.x compatibility. + + In transformers 5.x, generation-related attributes like forced_bos_token_id + were moved to GenerationConfig and are no longer set by PretrainedConfig. + Florence2's custom config code accesses self.forced_bos_token_id, which + raises AttributeError without this patch. + """ + from transformers.configuration_utils import PretrainedConfig + for attr in ('forced_bos_token_id', 'forced_eos_token_id'): + if not hasattr(PretrainedConfig, attr): + setattr(PretrainedConfig, attr, None) + def load_model(version): florence_path = os.path.join(folder_paths.models_dir, "florence2") os.makedirs(florence_path, exist_ok=True) @@ -56,6 +69,8 @@ def load_model(version): from huggingface_hub import snapshot_download snapshot_download(repo_id=repo_id, local_dir=model_path, ignore_patterns=["*.md", "*.txt"]) + _patch_florence2_config_compat() + try: with patch("transformers.dynamic_module_utils.get_imports", fixed_get_imports): # model = AutoModelForCausalLM.from_pretrained(model_path, trust_remote_code=True)