[Full Duplex] Feat: Support Full-Duplex realtime runtime & add MiniCPM-o 4.5 demo (#3907)
Signed-off-by: Sy03 <1370724210@qq.com> Signed-off-by: lishunyang12 <lishunyang03@gmail.com> Signed-off-by: Yueqian Lin <linyueqian@outlook.com> Co-authored-by: lishunyang12 <lishunyang03@gmail.com> Co-authored-by: Yueqian Lin <linyueqian@outlook.com> Co-authored-by: StormMelody <22371184@zju.edu.cn> Co-authored-by: Gao Han <hgaoaf@connect.ust.hk> Co-authored-by: Hongsheng Liu <liuhongsheng4@huawei.com>
This commit is contained in:
142 files changed
+48389
-1013
No files matched your search
File diff suppressed because it is too large.
Load diff
@@ -105,6 +105,11 @@ from vllm.multimodal.inputs import (
|
||||
MultiModalKwargsItems,
|
||||
NestedTensors,
|
||||
)
|
||||
|
||||
try:
|
||||
from vllm.multimodal.inputs import ModalityData, MultiModalDataDict
|
||||
except ImportError:
|
||||
from vllm.multimodal.parse import ModalityData, MultiModalDataDict
|
||||
from vllm.multimodal.parse import (
|
||||
AudioItem,
|
||||
AudioProcessorItems,
|
||||
@@ -127,9 +132,15 @@ from vllm.multimodal.processing import (
|
||||
)
|
||||
from vllm.sequence import IntermediateTensors
|
||||
|
||||
try:
|
||||
from vllm.transformers_utils.tokenizer import encode_tokens as _vllm_encode_tokens
|
||||
except ImportError:
|
||||
_vllm_encode_tokens = None
|
||||
|
||||
def encode_tokens(tokenizer, prompt: str) -> list[int]:
|
||||
"""Tokenize ``prompt`` without adding special tokens."""
|
||||
|
||||
def _encode_tokens(tokenizer: Any, prompt: str) -> list[int]:
|
||||
if _vllm_encode_tokens is not None:
|
||||
return _vllm_encode_tokens(tokenizer, prompt)
|
||||
return tokenizer.encode(prompt, add_special_tokens=False)
|
||||
|
||||
|
||||
@@ -2561,7 +2572,11 @@ class MiniCPMWhisperEncoder(WhisperEncoder):
|
||||
return_dict=None,
|
||||
past_key_values: EncoderDecoderCache | None = None,
|
||||
use_cache: bool | None = None,
|
||||
) -> BaseModelOutputWithPast | tuple:
|
||||
use_extra_context: bool | None = False,
|
||||
prefix_extra_frames: int | None = 1,
|
||||
suffix_extra_frames: int | None = 1,
|
||||
cnn_min_length: int | None = None,
|
||||
) -> BaseModelOutputWithPast | tuple[Any, ...]:
|
||||
r"""
|
||||
Forward pass of the Whisper encoder.
|
||||
|
||||
@@ -2672,9 +2687,37 @@ class MiniCPMWhisperEncoder(WhisperEncoder):
|
||||
# Ignore copy
|
||||
input_features = input_features.to(dtype=self.conv1.weight.dtype, device=self.conv1.weight.device)
|
||||
|
||||
original_length = input_features.shape[2]
|
||||
padded_for_cnn = False
|
||||
if cnn_min_length is not None and original_length < cnn_min_length:
|
||||
padded_features = torch.zeros(
|
||||
input_features.shape[0],
|
||||
input_features.shape[1],
|
||||
cnn_min_length,
|
||||
dtype=input_features.dtype,
|
||||
device=input_features.device,
|
||||
)
|
||||
padded_features[:, :, :original_length] = input_features
|
||||
input_features = padded_features
|
||||
padded_for_cnn = True
|
||||
|
||||
inputs_embeds = nn.functional.gelu(self.conv1(input_features))
|
||||
inputs_embeds = nn.functional.gelu(self.conv2(inputs_embeds))
|
||||
|
||||
if padded_for_cnn:
|
||||
actual_cnn_output_length = (original_length + 1) // 2
|
||||
inputs_embeds = inputs_embeds[:, :, :actual_cnn_output_length]
|
||||
|
||||
if use_extra_context:
|
||||
prefix_extra_frames = int(prefix_extra_frames or 0)
|
||||
suffix_extra_frames = int(suffix_extra_frames or 0)
|
||||
prefix_to_remove = (prefix_extra_frames + 1) // 2 if prefix_extra_frames > 0 else 0
|
||||
suffix_to_remove = (suffix_extra_frames + 1) // 2 if suffix_extra_frames > 0 else 0
|
||||
if prefix_to_remove > 0:
|
||||
inputs_embeds = inputs_embeds[:, :, prefix_to_remove:]
|
||||
if 0 < suffix_to_remove < inputs_embeds.shape[2]:
|
||||
inputs_embeds = inputs_embeds[:, :, :-suffix_to_remove]
|
||||
|
||||
inputs_embeds = inputs_embeds.permute(0, 2, 1)
|
||||
|
||||
embed_pos = self.embed_positions.weight
|
||||
@@ -3328,7 +3371,7 @@ class MiniCPMO45OmniLLMMultiModalProcessor(BaseMultiModalProcessor[MiniCPMO45Omn
|
||||
tokenization_kwargs=tokenization_kwargs,
|
||||
)
|
||||
tokenizer = self.info.get_tokenizer()
|
||||
prompt_ids = encode_tokens(tokenizer, prompt)
|
||||
prompt_ids = _encode_tokens(tokenizer, prompt)
|
||||
else:
|
||||
prompt_ids = self._apply_hf_processor_tokens_only(prompt)
|
||||
if use_tts:
|
||||
@@ -3817,8 +3860,18 @@ class MiniCPMO45OmniLLMForConditionalGeneration(nn.Module, SupportsMultiModal, S
|
||||
else:
|
||||
text_config = Qwen2Config.from_dict(config_dict)
|
||||
llm_arch = "Qwen2ForCausalLM"
|
||||
from vllm_omni.experimental.fullduplex.minicpmo45.compat import (
|
||||
patch_minicpmo_remote_config,
|
||||
)
|
||||
|
||||
patch_minicpmo_remote_config(text_config)
|
||||
text_vllm_config = (
|
||||
vllm_config.with_hf_config(text_config)
|
||||
if callable(getattr(vllm_config, "with_hf_config", None))
|
||||
else vllm_config
|
||||
)
|
||||
self.llm = init_vllm_registered_model(
|
||||
vllm_config=vllm_config,
|
||||
vllm_config=text_vllm_config,
|
||||
prefix=maybe_prefix(prefix, "llm"),
|
||||
hf_config=text_config,
|
||||
architectures=[llm_arch],
|
||||
@@ -3846,11 +3899,13 @@ class MiniCPMO45OmniLLMForConditionalGeneration(nn.Module, SupportsMultiModal, S
|
||||
self.audio_avg_pooler = nn.AvgPool1d(config.audio_pool_step, stride=config.audio_pool_step)
|
||||
self.audio_projection_layer = MultiModalProjector(in_dim=audio_output_dim, out_dim=embed_dim)
|
||||
self.audio_encoder_layer = -1
|
||||
self.audio_past_key_values = None
|
||||
else:
|
||||
self.apm = None
|
||||
self.audio_avg_pooler = None
|
||||
self.audio_projection_layer = None
|
||||
self.audio_encoder_layer = None
|
||||
self.audio_past_key_values = None
|
||||
|
||||
self.mm_token_ids = set[int]()
|
||||
self.make_empty_intermediate_tensors = self.llm.make_empty_intermediate_tensors
|
||||
@@ -4279,6 +4334,100 @@ class MiniCPMO45OmniLLMForConditionalGeneration(nn.Module, SupportsMultiModal, S
|
||||
|
||||
return final_audio_embeds
|
||||
|
||||
def get_audio_embedding_streaming(
|
||||
self,
|
||||
data: MiniCPMOAudioFeatureInputs,
|
||||
use_extra_context: bool = False,
|
||||
prefix_extra_frames: int = 1,
|
||||
suffix_extra_frames: int = 1,
|
||||
cnn_min_length: int | None = None,
|
||||
) -> list[list[torch.Tensor]]:
|
||||
if self.apm is None or self.audio_projection_layer is None or self.audio_avg_pooler is None:
|
||||
return []
|
||||
|
||||
wavforms = data["audio_features"]
|
||||
audio_feature_lens_raw = data["audio_feature_lens"]
|
||||
if isinstance(wavforms, list):
|
||||
if not wavforms:
|
||||
return []
|
||||
batch_size = len(wavforms)
|
||||
channels = wavforms[0].shape[-2]
|
||||
max_len = max(item.shape[-1] for item in wavforms)
|
||||
wavforms_tensor = torch.zeros(
|
||||
(batch_size, channels, max_len),
|
||||
dtype=wavforms[0].dtype,
|
||||
device=wavforms[0].device,
|
||||
)
|
||||
for idx, item in enumerate(wavforms):
|
||||
wavforms_tensor[idx, ..., : item.shape[-1]] = item
|
||||
wavforms = wavforms_tensor
|
||||
if isinstance(audio_feature_lens_raw, torch.Tensor):
|
||||
audio_feature_lens_raw = audio_feature_lens_raw.unbind(0)
|
||||
if wavforms.shape[0] == 0:
|
||||
return []
|
||||
audio_feature_lens = torch.hstack(audio_feature_lens_raw)
|
||||
batch_size, _, max_mel_seq_len = wavforms.shape
|
||||
assert batch_size == 1
|
||||
|
||||
current_seq_len = (max_mel_seq_len - 1) // 2 + 1
|
||||
if use_extra_context:
|
||||
prefix_to_remove = (int(prefix_extra_frames) + 1) // 2 if prefix_extra_frames > 0 else 0
|
||||
suffix_to_remove = (int(suffix_extra_frames) + 1) // 2 if suffix_extra_frames > 0 else 0
|
||||
current_seq_len = current_seq_len - prefix_to_remove - suffix_to_remove
|
||||
if current_seq_len <= 0:
|
||||
return []
|
||||
|
||||
if self.audio_past_key_values is not None:
|
||||
cache_length = self.audio_past_key_values[0][0].shape[2]
|
||||
apm_max_len = self.apm.embed_positions.weight.shape[0]
|
||||
if cache_length + current_seq_len >= apm_max_len:
|
||||
logger.warning(
|
||||
"audio_past_key_values length %s exceeds %s, reset.",
|
||||
cache_length + current_seq_len,
|
||||
apm_max_len,
|
||||
)
|
||||
self.audio_past_key_values = None
|
||||
|
||||
past_len = 0
|
||||
if self.audio_past_key_values is not None:
|
||||
past_len = self.audio_past_key_values[0][0].shape[2]
|
||||
total_seq_len = past_len + current_seq_len
|
||||
audio_attention_mask = torch.zeros(
|
||||
(batch_size, 1, current_seq_len, total_seq_len),
|
||||
dtype=self.apm.conv1.weight.dtype,
|
||||
device=wavforms.device,
|
||||
)
|
||||
|
||||
audio_outputs = self.apm(
|
||||
wavforms,
|
||||
past_key_values=self.audio_past_key_values,
|
||||
use_cache=True,
|
||||
output_hidden_states=True,
|
||||
attention_mask=audio_attention_mask,
|
||||
use_extra_context=use_extra_context,
|
||||
prefix_extra_frames=prefix_extra_frames,
|
||||
suffix_extra_frames=suffix_extra_frames,
|
||||
cnn_min_length=cnn_min_length,
|
||||
)
|
||||
audio_states = audio_outputs.hidden_states[self.audio_encoder_layer]
|
||||
self.audio_past_key_values = audio_outputs.past_key_values
|
||||
|
||||
audio_embeds = self.audio_projection_layer(audio_states)
|
||||
audio_embeds = audio_embeds.transpose(1, 2)
|
||||
audio_embeds = self.audio_avg_pooler(audio_embeds)
|
||||
audio_embeds = audio_embeds.transpose(1, 2)
|
||||
|
||||
_, feature_lens_after_pooling = self._get_feat_extract_output_lengths(audio_feature_lens)
|
||||
final_audio_embeds: list[list[torch.Tensor]] = []
|
||||
idx = 0
|
||||
for i in range(len(audio_feature_lens_raw)):
|
||||
target_audio_embeds = []
|
||||
for _ in range(len(audio_feature_lens_raw[i])):
|
||||
target_audio_embeds.append(audio_embeds[idx, : feature_lens_after_pooling[idx], :])
|
||||
idx += 1
|
||||
final_audio_embeds.append(target_audio_embeds)
|
||||
return final_audio_embeds
|
||||
|
||||
def _process_audio_input(
|
||||
self,
|
||||
audio_input: MiniCPMOAudioInputs,
|
||||
|
||||
File diff suppressed because it is too large.
Load diff
@@ -24,6 +24,11 @@ MINICPMO_4_5_PIPELINE = PipelineConfig(
|
||||
model_type="minicpmo_4_5",
|
||||
default_deploy_config_name="minicpmo_4_5.yaml",
|
||||
model_arch="MiniCPMO45OmniForConditionalGeneration",
|
||||
duplex_runtime_extension=("vllm_omni.experimental.fullduplex.minicpmo45.runtime.MiniCPMO45DuplexRuntimeExtension"),
|
||||
duplex_serving_adapter=(
|
||||
"vllm_omni.experimental.fullduplex.minicpmo45.serving_adapter.MiniCPMO45ServingRuntimeAdapter"
|
||||
),
|
||||
duplex_control_enabled=True,
|
||||
# MiniCPM-o 4.5's HF config.json reports `model_type="minicpmo"` and
|
||||
# `architectures=["MiniCPMO"]` — both shared verbatim with older MiniCPM-o
|
||||
# 1.0 / 2.6 checkpoints. The only field distinguishing the generations is
|
||||
|
||||
Reference in new issue
Block a user