Deprecate Transformers v4 support (#45161)
Signed-off-by: Harry Mellor <19981378+hmellor@users.noreply.github.com>
This commit is contained in:
@@ -618,13 +618,8 @@ class Gemma3nForConditionalGeneration(
|
||||
input_features = audio_input["input_features_padded"].squeeze(1)
|
||||
input_features_mask = audio_input["input_features_mask"].squeeze(1)
|
||||
audio_outputs = self.audio_tower(input_features, ~input_features_mask)
|
||||
if isinstance(audio_outputs, tuple):
|
||||
# Transformers v4
|
||||
audio_encodings, audio_mask = audio_outputs
|
||||
else:
|
||||
# Transformers v5
|
||||
audio_encodings = audio_outputs.last_hidden_state
|
||||
audio_mask = audio_outputs.audio_mel_mask
|
||||
audio_encodings = audio_outputs.last_hidden_state
|
||||
audio_mask = audio_outputs.audio_mel_mask
|
||||
audio_features = self.embed_audio(inputs_embeds=audio_encodings)
|
||||
|
||||
# The Gemma3nProcessor expects all audio will be 30s in length and
|
||||
|
||||
@@ -30,9 +30,7 @@ import numpy as np
|
||||
import torch
|
||||
import torch.nn as nn
|
||||
import torch.nn.functional as F
|
||||
from packaging.version import Version
|
||||
from transformers import PretrainedConfig
|
||||
from transformers import __version__ as TRANSFORMERS_VERSION
|
||||
from transformers.feature_extraction_utils import BatchFeature
|
||||
from transformers.models.qwen3_omni_moe.configuration_qwen3_omni_moe import (
|
||||
Qwen3OmniMoeAudioEncoderConfig,
|
||||
@@ -1261,40 +1259,6 @@ class Qwen3OmniMoeThinkerMultiModalProcessor(
|
||||
tok_kwargs = dict(tok_kwargs)
|
||||
mm_kwargs["audio_kwargs"] = dict(mm_kwargs.get("audio_kwargs") or {})
|
||||
mm_kwargs["text_kwargs"] = dict(mm_kwargs.get("text_kwargs") or {})
|
||||
if Version(TRANSFORMERS_VERSION) < Version("4.58.0"):
|
||||
# Extract audio_sample_rate before restructuring
|
||||
audio_sample_rate = mm_kwargs.pop("audio_sample_rate", None)
|
||||
|
||||
# move truncation to audio_kwargs level to avoid conflict
|
||||
# with tok_kwargs
|
||||
mm_kwargs["audio_kwargs"].setdefault(
|
||||
"truncation", mm_kwargs.pop("truncation", False)
|
||||
)
|
||||
mm_kwargs["text_kwargs"].setdefault(
|
||||
"truncation", tok_kwargs.pop("truncation", False)
|
||||
)
|
||||
|
||||
# Validate and conditionally pass audio_sample_rate
|
||||
# WhisperFeatureExtractor has a fixed sampling rate, and vLLM's
|
||||
# audio loader already resamples audio to the target rate.
|
||||
# Only pass the value if it matches to avoid unexpected behavior.
|
||||
if audio_sample_rate is not None:
|
||||
expected_sr = feature_extractor.sampling_rate
|
||||
if audio_sample_rate != expected_sr:
|
||||
logger.warning(
|
||||
"[%s] audio_sample_rate mismatch: user provided %dHz "
|
||||
"but model expects %dHz. Ignoring user value. "
|
||||
"vLLM's audio loader already resampled to %dHz.",
|
||||
self.__class__.__name__,
|
||||
audio_sample_rate,
|
||||
expected_sr,
|
||||
expected_sr,
|
||||
)
|
||||
else:
|
||||
# Sample rate matches, safe to pass
|
||||
mm_kwargs["audio_kwargs"]["audio_sample_rate"] = (
|
||||
audio_sample_rate
|
||||
)
|
||||
|
||||
hf_inputs = super()._call_hf_processor(
|
||||
prompt=prompt,
|
||||
|
||||
@@ -27,6 +27,10 @@ import transformers
|
||||
from packaging.version import Version
|
||||
from torch import nn
|
||||
from transformers import AutoModel
|
||||
from transformers.conversion_mapping import (
|
||||
WeightRenaming,
|
||||
get_model_conversion_mapping,
|
||||
)
|
||||
from transformers.modeling_utils import ALL_ATTENTION_FUNCTIONS
|
||||
|
||||
from vllm.compilation.decorators import support_torch_compile
|
||||
@@ -212,16 +216,9 @@ class Base(
|
||||
`create_attention_instances` are used
|
||||
- Sets the dtype to the default torch dtype set by vLLM because Transformers
|
||||
uses the config dtype when creating the model
|
||||
- Propagates this dtype to any sub-configs because Transformers model
|
||||
implementations do not support/use different dtypes in sub-models
|
||||
"""
|
||||
self.text_config._attn_implementation = "vllm"
|
||||
self.config.dtype = torch.get_default_dtype()
|
||||
# TODO(hmellor): Remove this when Transformers v4 support is dropped
|
||||
for sub_config_name in getattr(self.config, "sub_configs", {}):
|
||||
sub_config = getattr(self.config, sub_config_name)
|
||||
if sub_config.dtype != (dtype := self.config.dtype):
|
||||
sub_config.dtype = dtype
|
||||
|
||||
def _get_decoder_cls(self, **kwargs: dict) -> type[PreTrainedModel]:
|
||||
"""
|
||||
@@ -300,9 +297,7 @@ class Base(
|
||||
|
||||
This handles:
|
||||
|
||||
- Transformers weight renaming:
|
||||
- from `WeightRenaming` in Transformers v5
|
||||
- from `_checkpoint_conversion_mapping` in Transformers v4
|
||||
- Transformers weight renaming from `WeightRenaming`
|
||||
- Checkpoints saved with a base model prefix that is not `model`
|
||||
- Checkpoints saved with no base model prefix
|
||||
- Any quantization config specific mappings
|
||||
@@ -310,37 +305,16 @@ class Base(
|
||||
self.hf_to_vllm_mapper = WeightsMapper()
|
||||
orig_to_new_regex = self.hf_to_vllm_mapper.orig_to_new_regex
|
||||
|
||||
if Version(transformers.__version__) >= Version("5.0.0"):
|
||||
from transformers.conversion_mapping import (
|
||||
WeightRenaming,
|
||||
get_model_conversion_mapping,
|
||||
)
|
||||
|
||||
for mapping in get_model_conversion_mapping(self.model):
|
||||
# Handle weights which have been renamed in Transformers
|
||||
if isinstance(mapping, WeightRenaming):
|
||||
# Recompile using regex (Transformers used re)
|
||||
compiled_sources = re.compile(
|
||||
mapping.compiled_sources.pattern, mapping.compiled_sources.flags
|
||||
)
|
||||
target_pattern = mapping.target_patterns[0]
|
||||
orig_to_new_regex[compiled_sources] = target_pattern
|
||||
# TODO: Handle WeightConverter to enable layer merging
|
||||
else:
|
||||
# Replace legacy suffixes used for norms
|
||||
# TODO(hmellor): Remove this when Transformers v4 support is dropped
|
||||
orig_to_new_regex.update(
|
||||
{
|
||||
re.compile(r"\.gamma$"): ".weight",
|
||||
re.compile(r"\.beta$"): ".bias",
|
||||
}
|
||||
)
|
||||
|
||||
# Handle weights which have been renamed in Transformers
|
||||
# TODO(hmellor): Remove this when Transformers v4 support is dropped
|
||||
ccm = getattr(self.model, "_checkpoint_conversion_mapping", {})
|
||||
for source, target in ccm.items():
|
||||
orig_to_new_regex[re.compile(source)] = target
|
||||
for mapping in get_model_conversion_mapping(self.model):
|
||||
# Handle weights which have been renamed in Transformers
|
||||
if isinstance(mapping, WeightRenaming):
|
||||
# Recompile using regex (Transformers used re)
|
||||
compiled_sources = re.compile(
|
||||
mapping.compiled_sources.pattern, mapping.compiled_sources.flags
|
||||
)
|
||||
target_pattern = mapping.target_patterns[0]
|
||||
orig_to_new_regex[compiled_sources] = target_pattern
|
||||
# TODO: Handle WeightConverter to enable layer merging
|
||||
|
||||
# Handle unexpected weights which should be ignored
|
||||
if self.model._keys_to_ignore_on_load_unexpected is not None:
|
||||
@@ -377,7 +351,7 @@ class Base(
|
||||
"""
|
||||
Check if the model has tied word embeddings.
|
||||
"""
|
||||
# Transformers v4 and v5 will store this in different places
|
||||
# Models created with Transformers v4 and v5 will store this in different places
|
||||
tie_word_embeddings_v4 = getattr(self.text_config, "tie_word_embeddings", False)
|
||||
tie_word_embeddings_v5 = getattr(self.config, "tie_word_embeddings", False)
|
||||
return tie_word_embeddings_v4 or tie_word_embeddings_v5
|
||||
|
||||
@@ -101,8 +101,6 @@ Style = Literal[
|
||||
"replicate",
|
||||
"colwise_gather_output",
|
||||
"rowwise_split_input",
|
||||
"colwise_rep",
|
||||
"rowwise_rep",
|
||||
]
|
||||
|
||||
|
||||
@@ -131,12 +129,8 @@ def replace_linear_class(
|
||||
"colwise": (ColumnParallelLinear, {}),
|
||||
"rowwise": (RowParallelLinear, {}),
|
||||
"replicate": (ReplicatedLinear, {}),
|
||||
# Transformers v5
|
||||
"colwise_gather_output": (ColumnParallelLinear, {"gather_output": True}),
|
||||
"rowwise_split_input": (RowParallelLinear, {"input_is_parallel": False}),
|
||||
# Transformers v4
|
||||
"colwise_rep": (ColumnParallelLinear, {"gather_output": True}),
|
||||
"rowwise_rep": (RowParallelLinear, {"input_is_parallel": False}),
|
||||
}.get(style, (ReplicatedLinear, {}))
|
||||
|
||||
return vllm_linear_cls(
|
||||
|
||||
@@ -5,7 +5,6 @@
|
||||
"""PyTorch Ultravox model."""
|
||||
|
||||
import copy
|
||||
import inspect
|
||||
from collections.abc import Iterable, Mapping, Sequence
|
||||
from types import SimpleNamespace
|
||||
from typing import Annotated, Any, Literal, TypeAlias
|
||||
@@ -397,17 +396,10 @@ class UltravoxTransformerProjector(nn.Module, ModuleUtilsMixin):
|
||||
)
|
||||
hidden_states = hidden_states + positions
|
||||
|
||||
# Backward compatibility for Transformers v4 where layer_head_mask
|
||||
# was a required argument for WhisperEncoderLayer.forward
|
||||
kwargs = {}
|
||||
if "layer_head_mask" in inspect.signature(self.layers[0].forward).parameters:
|
||||
kwargs["layer_head_mask"] = None
|
||||
|
||||
for layer in self.layers:
|
||||
hidden_states = layer(
|
||||
hidden_states,
|
||||
attention_mask=extended_attention_mask,
|
||||
**kwargs,
|
||||
)
|
||||
# BC version that allows for the old tupled output
|
||||
if isinstance(hidden_states, tuple):
|
||||
@@ -504,17 +496,10 @@ class ModifiedWhisperEncoder(WhisperEncoder):
|
||||
|
||||
attention_mask = self.get_attention_mask_by_audio_len(audio_lens, hidden_states)
|
||||
|
||||
# Backward compatibility for Transformers v4 where layer_head_mask
|
||||
# was a required argument for WhisperEncoderLayer.forward
|
||||
kwargs = {}
|
||||
if "layer_head_mask" in inspect.signature(self.layers[0].forward).parameters:
|
||||
kwargs["layer_head_mask"] = None
|
||||
|
||||
for encoder_layer in self.layers:
|
||||
hidden_states = encoder_layer(
|
||||
hidden_states,
|
||||
attention_mask,
|
||||
**kwargs,
|
||||
)
|
||||
# BC version that allows for the old tupled output
|
||||
if isinstance(hidden_states, tuple):
|
||||
|
||||
Reference in New Issue
Block a user