Unverified Commit 952dfd48 authored by Raushan Turganbay's avatar Raushan Turganbay Committed by GitHub
Browse files

Deprecate `vocab_size` in other two VLMs (#31681)



* deprrecate `vocab_size` in other two VLMs

* Update src/transformers/models/fuyu/configuration_fuyu.py
Co-authored-by: default avataramyeroberts <22614925+amyeroberts@users.noreply.github.com>

* depracate until 4.44

---------
Co-authored-by: default avataramyeroberts <22614925+amyeroberts@users.noreply.github.com>
parent 594c1610
...@@ -1942,8 +1942,8 @@ class PreTrainedModel(nn.Module, ModuleUtilsMixin, GenerationMixin, PushToHubMix ...@@ -1942,8 +1942,8 @@ class PreTrainedModel(nn.Module, ModuleUtilsMixin, GenerationMixin, PushToHubMix
# Update base model and current model config # Update base model and current model config
if hasattr(self.config, "text_config"): if hasattr(self.config, "text_config"):
self.config.text_config.vocab_size = model_embeds.weight.shape[0] self.config.text_config.vocab_size = model_embeds.weight.shape[0]
# TODO: to be removed after v4.42, config.vocab_size is deprecated for models that have a config.text_config else:
self.config.vocab_size = model_embeds.weight.shape[0] self.config.vocab_size = model_embeds.weight.shape[0]
self.vocab_size = model_embeds.weight.shape[0] self.vocab_size = model_embeds.weight.shape[0]
# Tie weights again if needed # Tie weights again if needed
......
...@@ -14,6 +14,8 @@ ...@@ -14,6 +14,8 @@
# limitations under the License. # limitations under the License.
"""Fuyu model configuration""" """Fuyu model configuration"""
import warnings
from ...configuration_utils import PretrainedConfig from ...configuration_utils import PretrainedConfig
from ...utils import logging from ...utils import logging
from ..auto import CONFIG_MAPPING from ..auto import CONFIG_MAPPING
...@@ -157,7 +159,7 @@ class FuyuConfig(PretrainedConfig): ...@@ -157,7 +159,7 @@ class FuyuConfig(PretrainedConfig):
text_model_type = text_config["model_type"] if "model_type" in text_config else "persimmon" text_model_type = text_config["model_type"] if "model_type" in text_config else "persimmon"
self.text_config = CONFIG_MAPPING[text_model_type](**text_config) self.text_config = CONFIG_MAPPING[text_model_type](**text_config)
self.vocab_size = vocab_size self._vocab_size = vocab_size
self.max_position_embeddings = max_position_embeddings self.max_position_embeddings = max_position_embeddings
self.image_size = image_size self.image_size = image_size
self.patch_size = patch_size self.patch_size = patch_size
...@@ -206,3 +208,20 @@ class FuyuConfig(PretrainedConfig): ...@@ -206,3 +208,20 @@ class FuyuConfig(PretrainedConfig):
) )
if rope_scaling_factor is None or not isinstance(rope_scaling_factor, float) or rope_scaling_factor <= 1.0: if rope_scaling_factor is None or not isinstance(rope_scaling_factor, float) or rope_scaling_factor <= 1.0:
raise ValueError(f"`rope_scaling`'s factor field must be a float > 1, got {rope_scaling_factor}") raise ValueError(f"`rope_scaling`'s factor field must be a float > 1, got {rope_scaling_factor}")
@property
def vocab_size(self):
warnings.warn(
"The `vocab_size` attribute is deprecated and will be removed in v4.44, Please use `text_config.vocab_size` instead.",
FutureWarning,
)
return self._vocab_size
@vocab_size.setter
def vocab_size(self, value):
self._vocab_size = value
def to_dict(self):
output = super().to_dict()
output.pop("_vocab_size", None)
return output
...@@ -149,7 +149,7 @@ class FuyuForCausalLM(FuyuPreTrainedModel): ...@@ -149,7 +149,7 @@ class FuyuForCausalLM(FuyuPreTrainedModel):
def __init__(self, config: FuyuConfig): def __init__(self, config: FuyuConfig):
super().__init__(config) super().__init__(config)
self.padding_idx = config.pad_token_id self.padding_idx = config.pad_token_id
self.vocab_size = config.vocab_size self.vocab_size = config.text_config.vocab_size
self.language_model = AutoModelForCausalLM.from_config( self.language_model = AutoModelForCausalLM.from_config(
config.text_config, attn_implementation=config._attn_implementation config.text_config, attn_implementation=config._attn_implementation
) )
...@@ -168,6 +168,15 @@ class FuyuForCausalLM(FuyuPreTrainedModel): ...@@ -168,6 +168,15 @@ class FuyuForCausalLM(FuyuPreTrainedModel):
def set_input_embeddings(self, value): def set_input_embeddings(self, value):
self.language_model.set_input_embeddings(value) self.language_model.set_input_embeddings(value)
def resize_token_embeddings(self, new_num_tokens: Optional[int] = None, pad_to_multiple_of=None) -> nn.Embedding:
# TODO: config.vocab_size is deprecated and will be removed in v4.43.
# `resize_token_embeddings` should work from `modeling_utils.py``
model_embeds = self.language_model.resize_token_embeddings(new_num_tokens, pad_to_multiple_of)
self.config.text_config.vocab_size = model_embeds.num_embeddings
self.config.vocab_size = model_embeds.num_embeddings
self.vocab_size = model_embeds.num_embeddings
return model_embeds
def gather_continuous_embeddings( def gather_continuous_embeddings(
self, self,
word_embeddings: torch.Tensor, word_embeddings: torch.Tensor,
......
...@@ -13,6 +13,8 @@ ...@@ -13,6 +13,8 @@
# limitations under the License. # limitations under the License.
"""PaliGemmamodel configuration""" """PaliGemmamodel configuration"""
import warnings
from ...configuration_utils import PretrainedConfig from ...configuration_utils import PretrainedConfig
from ...utils import logging from ...utils import logging
from ..auto import CONFIG_MAPPING from ..auto import CONFIG_MAPPING
...@@ -86,7 +88,7 @@ class PaliGemmaConfig(PretrainedConfig): ...@@ -86,7 +88,7 @@ class PaliGemmaConfig(PretrainedConfig):
): ):
self.ignore_index = ignore_index self.ignore_index = ignore_index
self.image_token_index = image_token_index self.image_token_index = image_token_index
self.vocab_size = vocab_size self._vocab_size = vocab_size
self.projection_dim = projection_dim self.projection_dim = projection_dim
self.hidden_size = hidden_size self.hidden_size = hidden_size
self.vision_config = vision_config self.vision_config = vision_config
...@@ -124,7 +126,25 @@ class PaliGemmaConfig(PretrainedConfig): ...@@ -124,7 +126,25 @@ class PaliGemmaConfig(PretrainedConfig):
num_attention_heads=8, num_attention_heads=8,
num_key_value_heads=1, num_key_value_heads=1,
is_encoder_decoder=False, is_encoder_decoder=False,
vocab_size=vocab_size,
) )
self.text_config.num_image_tokens = (self.vision_config.image_size // self.vision_config.patch_size) ** 2 self.text_config.num_image_tokens = (self.vision_config.image_size // self.vision_config.patch_size) ** 2
self.vision_config.projection_dim = projection_dim self.vision_config.projection_dim = projection_dim
super().__init__(**kwargs) super().__init__(**kwargs)
@property
def vocab_size(self):
warnings.warn(
"The `vocab_size` attribute is deprecated and will be removed in v4.44, Please use `text_config.vocab_size` instead.",
FutureWarning,
)
return self._vocab_size
@vocab_size.setter
def vocab_size(self, value):
self._vocab_size = value
def to_dict(self):
output = super().to_dict()
output.pop("_vocab_size", None)
return output
...@@ -233,7 +233,7 @@ class PaliGemmaForConditionalGeneration(PaliGemmaPreTrainedModel): ...@@ -233,7 +233,7 @@ class PaliGemmaForConditionalGeneration(PaliGemmaPreTrainedModel):
super().__init__(config) super().__init__(config)
self.vision_tower = AutoModel.from_config(config=config.vision_config) self.vision_tower = AutoModel.from_config(config=config.vision_config)
self.multi_modal_projector = PaliGemmaMultiModalProjector(config) self.multi_modal_projector = PaliGemmaMultiModalProjector(config)
self.vocab_size = config.vocab_size self.vocab_size = config.text_config.vocab_size
self._attn_implementation = config._attn_implementation self._attn_implementation = config._attn_implementation
language_model = AutoModelForCausalLM.from_config( language_model = AutoModelForCausalLM.from_config(
...@@ -276,8 +276,9 @@ class PaliGemmaForConditionalGeneration(PaliGemmaPreTrainedModel): ...@@ -276,8 +276,9 @@ class PaliGemmaForConditionalGeneration(PaliGemmaPreTrainedModel):
return self.language_model.tie_weights() return self.language_model.tie_weights()
def resize_token_embeddings(self, new_num_tokens: Optional[int] = None, pad_to_multiple_of=None) -> nn.Embedding: def resize_token_embeddings(self, new_num_tokens: Optional[int] = None, pad_to_multiple_of=None) -> nn.Embedding:
# TODO: config.vocab_size is deprecated and will be removed in v4.43.
# `resize_token_embeddings` should work from `modeling_utils.py``
model_embeds = self.language_model.resize_token_embeddings(new_num_tokens, pad_to_multiple_of) model_embeds = self.language_model.resize_token_embeddings(new_num_tokens, pad_to_multiple_of)
# update vocab size
self.config.text_config.vocab_size = model_embeds.num_embeddings self.config.text_config.vocab_size = model_embeds.num_embeddings
self.config.vocab_size = model_embeds.num_embeddings self.config.vocab_size = model_embeds.num_embeddings
self.vocab_size = model_embeds.num_embeddings self.vocab_size = model_embeds.num_embeddings
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment