[Refactor] Refactor async chunk and fix the shape mismatch issue (#1151)
Signed-off-by: amy-why-3459 <wuhaiyan17@huawei.com>
This commit is contained in:
1 parent
0aa610d82a
commit
5304bc1e16
7 files changed
+138
-166
No files matched your search
@@ -599,11 +599,16 @@ class Qwen3OmniMoeForConditionalGeneration(
|
||||
)
|
||||
update_dict["code_predictor_codes"] = code_predictor_codes
|
||||
else:
|
||||
# decode
|
||||
if info_dict.get("num_processed_tokens", 0) < len(info_dict.get("thinker_input_ids", [])):
|
||||
info_dict["num_processed_tokens"] = len(info_dict.get("thinker_input_ids", [])) + 1
|
||||
|
||||
last_talker_hidden, text_step, update_dict = self.talker_preprocess_decode(
|
||||
input_ids, input_embeds, **info_dict
|
||||
)
|
||||
update_dict["mtp_inputs"] = last_talker_hidden, text_step
|
||||
|
||||
update_dict["num_processed_tokens"] = info_dict.get("num_processed_tokens", 0) + span_len
|
||||
return input_ids, input_embeds, update_dict
|
||||
|
||||
def talker_mtp(
|
||||
@@ -662,7 +667,8 @@ class Qwen3OmniMoeForConditionalGeneration(
|
||||
update_dict: dict[str, dict] = {}
|
||||
# TODO(Peiqi): add voice_type support
|
||||
voice_type = self.voice_type
|
||||
|
||||
start_index = info_dict.get("num_processed_tokens", 0)
|
||||
end_index = start_index + input_embeds.shape[0]
|
||||
# Read thinker outputs for prefill
|
||||
thinker_sequence_embeds = info_dict.get("thinker_embeddings").to(
|
||||
device=self._module_device(self.talker), dtype=torch.bfloat16
|
||||
@@ -762,7 +768,7 @@ class Qwen3OmniMoeForConditionalGeneration(
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
return req_input_ids, req_embeds, update_dict
|
||||
return req_input_ids[start_index:end_index], req_embeds[start_index:end_index], update_dict
|
||||
|
||||
def _thinker_to_talker_prefill(
|
||||
self,
|
||||
@@ -858,13 +864,14 @@ class Qwen3OmniMoeForConditionalGeneration(
|
||||
(input_ids, input_embeds) for talker
|
||||
"""
|
||||
thinker_embed = info_dict.get("thinker_embeddings", None)
|
||||
if thinker_embed is None:
|
||||
start_index = info_dict.get("num_processed_tokens", 0)
|
||||
if start_index >= thinker_embed.shape[0]:
|
||||
if info_dict.get("finished_flag"):
|
||||
return self.tts_pad_embed.to(device)
|
||||
update_dict["finished_flag"] = True
|
||||
return self.tts_eos_embed.to(device)
|
||||
|
||||
thinker_embed = thinker_embed.to(device)
|
||||
thinker_embed = thinker_embed[start_index : start_index + 1].to(device)
|
||||
return self.talker.text_projection(thinker_embed).to(device)
|
||||
|
||||
def talker_preprocess_decode(self, input_ids: torch.Tensor, input_embeds: torch.Tensor, **info_dict: dict):
|
||||
|
||||
@@ -63,7 +63,7 @@ stage_args:
|
||||
default_sampling_params:
|
||||
temperature: 0.9
|
||||
top_k: 50
|
||||
max_tokens: 2048 # TODO: The max_tokens of the async_chunk feature cannot exceed 2048.
|
||||
max_tokens: 4096
|
||||
seed: 42
|
||||
detokenize: False
|
||||
repetition_penalty: 1.05
|
||||
|
||||
@@ -84,6 +84,7 @@ def _validate_stage_inputs(stage_list, engine_input_source):
|
||||
|
||||
|
||||
def thinker2talker_async_chunk(
|
||||
connector: Any,
|
||||
pooling_output: dict[str, Any],
|
||||
request: OmniEngineCoreRequest,
|
||||
) -> list[dict[str, Any]]:
|
||||
@@ -93,27 +94,50 @@ def thinker2talker_async_chunk(
|
||||
2. Split hidden states into: prompt embeddings + generated embeddings
|
||||
3. Package for talker with additional information
|
||||
"""
|
||||
all_token_ids = request.all_token_ids # prefill + decode
|
||||
prompt_token_ids = request.prompt_token_ids
|
||||
|
||||
# Convert ConstantList to regular list for OmniSerializer serialization
|
||||
all_token_ids = _ensure_list(all_token_ids)
|
||||
prompt_token_ids = _ensure_list(prompt_token_ids)
|
||||
|
||||
thinker_output = pooling_output
|
||||
|
||||
talker_additional_info = {
|
||||
"thinker_embeddings": thinker_output.get("0").detach().cpu(),
|
||||
"thinker_hidden_states": thinker_output.get("24").detach().cpu(),
|
||||
"thinker_sequences": all_token_ids,
|
||||
"thinker_input_ids": prompt_token_ids,
|
||||
# Provide thinker-side TTS token embeddings for talker projection
|
||||
"tts_bos_embed": thinker_output.get("tts_bos_embed").detach().cpu(),
|
||||
"tts_eos_embed": thinker_output.get("tts_eos_embed").detach().cpu(),
|
||||
"tts_pad_embed": thinker_output.get("tts_pad_embed").detach().cpu(),
|
||||
"finished": torch.tensor(request.is_finished(), dtype=torch.bool),
|
||||
}
|
||||
request_id = request.external_req_id
|
||||
chunk_id = connector.put_requests[request_id]
|
||||
if chunk_id == 0:
|
||||
all_token_ids = request.all_token_ids # prefill + decode
|
||||
prompt_token_ids = request.prompt_token_ids
|
||||
# Convert ConstantList to regular list for OmniSerializer serialization
|
||||
all_token_ids = _ensure_list(all_token_ids)
|
||||
prompt_token_ids = _ensure_list(prompt_token_ids)
|
||||
talker_additional_info = {
|
||||
"thinker_embeddings": pooling_output.get("0").detach().cpu(),
|
||||
"thinker_hidden_states": pooling_output.get("24").detach().cpu(),
|
||||
"thinker_sequences": all_token_ids,
|
||||
"thinker_input_ids": prompt_token_ids,
|
||||
# Provide thinker-side TTS token embeddings for talker projection
|
||||
"tts_bos_embed": pooling_output.get("tts_bos_embed").detach().cpu(),
|
||||
"tts_eos_embed": pooling_output.get("tts_eos_embed").detach().cpu(),
|
||||
"tts_pad_embed": pooling_output.get("tts_pad_embed").detach().cpu(),
|
||||
"finished": torch.tensor(request.is_finished(), dtype=torch.bool),
|
||||
}
|
||||
if connector.request_payload.get(request_id) is None:
|
||||
if not request.is_finished():
|
||||
connector.request_payload[request_id] = talker_additional_info
|
||||
return None
|
||||
else:
|
||||
save_payload = connector.request_payload.pop(request_id)
|
||||
talker_additional_info["thinker_embeddings"] = torch.cat(
|
||||
(save_payload.get("thinker_embeddings"), talker_additional_info.get("thinker_embeddings")), dim=0
|
||||
)
|
||||
talker_additional_info["thinker_hidden_states"] = torch.cat(
|
||||
(save_payload.get("thinker_hidden_states"), talker_additional_info.get("thinker_hidden_states")),
|
||||
dim=0,
|
||||
)
|
||||
else:
|
||||
output_token_ids = request.output_token_ids
|
||||
# Convert ConstantList to regular list for OmniSerializer serialization
|
||||
output_token_ids = _ensure_list(output_token_ids)
|
||||
|
||||
talker_additional_info = {
|
||||
"thinker_embeddings": pooling_output.get("0").detach().cpu(),
|
||||
"thinker_hidden_states": pooling_output.get("24").detach().cpu(),
|
||||
"thinker_sequences": output_token_ids,
|
||||
"finished": torch.tensor(request.is_finished(), dtype=torch.bool),
|
||||
}
|
||||
return talker_additional_info
|
||||
|
||||
|
||||
@@ -182,6 +206,7 @@ def thinker2talker(
|
||||
|
||||
|
||||
def talker2code2wav_async_chunk(
|
||||
connector: Any,
|
||||
pooling_output: dict[str, Any],
|
||||
request: OmniEngineCoreRequest,
|
||||
):
|
||||
@@ -189,35 +214,49 @@ def talker2code2wav_async_chunk(
|
||||
Pooling version.
|
||||
"""
|
||||
if "code_predictor_codes" not in pooling_output:
|
||||
return []
|
||||
return None
|
||||
|
||||
code_predictor_codes = pooling_output["code_predictor_codes"]
|
||||
|
||||
if code_predictor_codes is None:
|
||||
return []
|
||||
return None
|
||||
if isinstance(code_predictor_codes, torch.Tensor):
|
||||
if code_predictor_codes.numel() == 0:
|
||||
return []
|
||||
return None
|
||||
elif hasattr(code_predictor_codes, "__len__"):
|
||||
if len(code_predictor_codes) == 0:
|
||||
return []
|
||||
return None
|
||||
|
||||
if isinstance(code_predictor_codes, torch.Tensor):
|
||||
if not code_predictor_codes.any():
|
||||
return []
|
||||
return None
|
||||
else:
|
||||
code_tensor = torch.tensor(code_predictor_codes, dtype=torch.long)
|
||||
if not code_tensor.any():
|
||||
return []
|
||||
return None
|
||||
|
||||
codec_codes = code_predictor_codes.to(torch.long).transpose(0, 1).cpu().to(torch.long).reshape(-1).tolist()
|
||||
if sum(codec_codes) == 0:
|
||||
return []
|
||||
return None
|
||||
|
||||
return {
|
||||
"code_predictor_codes": codec_codes,
|
||||
request_id = request.external_req_id
|
||||
chunk_size = left_context_size = 25
|
||||
connector.code_prompt_token_ids[request_id].append(codec_codes)
|
||||
length = len(connector.code_prompt_token_ids[request_id])
|
||||
chunk_length = length % chunk_size
|
||||
if chunk_length != 0 and not request.is_finished():
|
||||
return None
|
||||
|
||||
context_length = chunk_length if chunk_length != 0 else chunk_size
|
||||
end_index = min(length, left_context_size + context_length)
|
||||
|
||||
info = {
|
||||
"code_predictor_codes": (
|
||||
torch.tensor(connector.code_prompt_token_ids[request_id][-end_index:]).transpose(0, 1).reshape(-1).tolist()
|
||||
),
|
||||
"finished": torch.tensor(request.is_finished(), dtype=torch.bool),
|
||||
}
|
||||
return info
|
||||
|
||||
|
||||
def talker2code2wav(
|
||||
|
||||
Reference in new issue
Block a user