|
|
@@ -0,0 +1,556 @@
|
|
|
+# Copyright (c) 2024 Alibaba Inc (authors: Xiang Lyu, Zhihao Du)
|
|
|
+#
|
|
|
+# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
+# you may not use this file except in compliance with the License.
|
|
|
+# You may obtain a copy of the License at
|
|
|
+#
|
|
|
+# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
+#
|
|
|
+# Unless required by applicable law or agreed to in writing, software
|
|
|
+# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
+# See the License for the specific language governing permissions and
|
|
|
+# limitations under the License.
|
|
|
+from typing import Dict, Optional, Callable, List, Generator
|
|
|
+import torch
|
|
|
+from torch import nn
|
|
|
+import torch.nn.functional as F
|
|
|
+from transformers import Qwen2ForCausalLM
|
|
|
+from torch.nn.utils.rnn import pad_sequence, unpad_sequence
|
|
|
+from cosyvoice.utils.common import IGNORE_ID
|
|
|
+from cosyvoice.transformer.label_smoothing_loss import LabelSmoothingLoss
|
|
|
+from cosyvoice.utils.common import th_accuracy
|
|
|
+from cosyvoice.utils.file_utils import logging
|
|
|
+from cosyvoice.utils.mask import make_pad_mask
|
|
|
+
|
|
|
+
|
|
|
+class TransformerLM(torch.nn.Module):
|
|
|
+ def __init__(
|
|
|
+ self,
|
|
|
+ text_encoder_input_size: int,
|
|
|
+ llm_input_size: int,
|
|
|
+ llm_output_size: int,
|
|
|
+ text_token_size: int,
|
|
|
+ speech_token_size: int,
|
|
|
+ text_encoder: torch.nn.Module,
|
|
|
+ llm: torch.nn.Module,
|
|
|
+ sampling: Callable,
|
|
|
+ length_normalized_loss: bool = True,
|
|
|
+ lsm_weight: float = 0.0,
|
|
|
+ spk_embed_dim: int = 192,
|
|
|
+ ):
|
|
|
+ super().__init__()
|
|
|
+ self.llm_input_size = llm_input_size
|
|
|
+ self.speech_token_size = speech_token_size
|
|
|
+ # 1. build text token inputs related modules
|
|
|
+ self.text_embedding = torch.nn.Embedding(text_token_size, text_encoder_input_size)
|
|
|
+ self.text_encoder = text_encoder
|
|
|
+ self.text_encoder_affine_layer = nn.Linear(
|
|
|
+ self.text_encoder.output_size(),
|
|
|
+ llm_input_size
|
|
|
+ )
|
|
|
+
|
|
|
+ # 2. build speech token language model related modules
|
|
|
+ self.sos_eos = 0
|
|
|
+ self.task_id = 1
|
|
|
+ self.llm_embedding = torch.nn.Embedding(2, llm_input_size)
|
|
|
+ self.llm = llm
|
|
|
+ self.llm_decoder = nn.Linear(llm_output_size, speech_token_size + 1)
|
|
|
+ self.criterion_ce = LabelSmoothingLoss(
|
|
|
+ size=speech_token_size + 1,
|
|
|
+ padding_idx=IGNORE_ID,
|
|
|
+ smoothing=lsm_weight,
|
|
|
+ normalize_length=length_normalized_loss,
|
|
|
+ )
|
|
|
+
|
|
|
+ # 3. [Optional] build speech token related modules
|
|
|
+ self.speech_embedding = torch.nn.Embedding(speech_token_size, llm_input_size)
|
|
|
+ self.spk_embed_affine_layer = torch.nn.Linear(spk_embed_dim, llm_input_size)
|
|
|
+
|
|
|
+ # 4. sampling method
|
|
|
+ self.sampling = sampling
|
|
|
+
|
|
|
+ def encode(
|
|
|
+ self,
|
|
|
+ text: torch.Tensor,
|
|
|
+ text_lengths: torch.Tensor,
|
|
|
+ ):
|
|
|
+ encoder_out, encoder_mask = self.text_encoder(text, text_lengths, decoding_chunk_size=1, num_decoding_left_chunks=-1)
|
|
|
+ encoder_out_lens = encoder_mask.squeeze(1).sum(1)
|
|
|
+ encoder_out = self.text_encoder_affine_layer(encoder_out)
|
|
|
+ return encoder_out, encoder_out_lens
|
|
|
+
|
|
|
+ def pad_unpad_sequence(self, sos_eos_emb, embedding, text_token, text_token_len, task_id_emb, speech_token, speech_token_len):
|
|
|
+ text_token = unpad_sequence(text_token, text_token_len.cpu(), batch_first=True)
|
|
|
+ speech_token = unpad_sequence(speech_token, speech_token_len.cpu(), batch_first=True)
|
|
|
+ lm_input = [torch.concat([sos_eos_emb.squeeze(dim=0), embedding[i], text_token[i], task_id_emb.squeeze(dim=0), speech_token[i]], dim=0)
|
|
|
+ for i in range(len(text_token))]
|
|
|
+ lm_input_len = torch.tensor([i.size(0) for i in lm_input], dtype=torch.int32)
|
|
|
+ lm_input = pad_sequence(lm_input, batch_first=True, padding_value=IGNORE_ID)
|
|
|
+ return lm_input, lm_input_len
|
|
|
+
|
|
|
+ def forward(
|
|
|
+ self,
|
|
|
+ batch: dict,
|
|
|
+ device: torch.device,
|
|
|
+ ) -> Dict[str, Optional[torch.Tensor]]:
|
|
|
+ """
|
|
|
+ Args:
|
|
|
+ text: (B, L, D)
|
|
|
+ text_lengths: (B,)
|
|
|
+ audio: (B, T, N) or (B, T)
|
|
|
+ audio_lengths: (B,)
|
|
|
+ """
|
|
|
+ text_token = batch['text_token'].to(device)
|
|
|
+ text_token_len = batch['text_token_len'].to(device)
|
|
|
+ speech_token = batch['speech_token'].to(device)
|
|
|
+ speech_token_len = batch['speech_token_len'].to(device)
|
|
|
+ embedding = batch['embedding'].to(device)
|
|
|
+
|
|
|
+ # 1. prepare llm_target
|
|
|
+ lm_target = [torch.tensor([IGNORE_ID] * (2 + text_token_len[i]) + speech_token[i, :speech_token_len[i]].tolist() +
|
|
|
+ [self.speech_token_size]) for i in range(text_token.size(0))]
|
|
|
+ lm_target = pad_sequence(lm_target, batch_first=True, padding_value=IGNORE_ID).to(device)
|
|
|
+
|
|
|
+ # 1. encode text_token
|
|
|
+ text_token = self.text_embedding(text_token)
|
|
|
+ text_token, text_token_len = self.encode(text_token, text_token_len)
|
|
|
+
|
|
|
+ # 2. embedding projection
|
|
|
+ embedding = F.normalize(embedding, dim=1)
|
|
|
+ embedding = self.spk_embed_affine_layer(embedding)
|
|
|
+ embedding = embedding.unsqueeze(1)
|
|
|
+
|
|
|
+ # 3. eos and task_id
|
|
|
+ sos_eos_emb = self.llm_embedding.weight[self.sos_eos].reshape(1, 1, -1)
|
|
|
+ task_id_emb = self.llm_embedding.weight[self.task_id].reshape(1, 1, -1)
|
|
|
+
|
|
|
+ # 4. encode speech_token
|
|
|
+ speech_token = self.speech_embedding(speech_token)
|
|
|
+
|
|
|
+ # 5. unpad and pad
|
|
|
+ lm_input, lm_input_len = self.pad_unpad_sequence(sos_eos_emb, embedding, text_token, text_token_len,
|
|
|
+ task_id_emb, speech_token, speech_token_len)
|
|
|
+
|
|
|
+ # 6. run lm forward
|
|
|
+ lm_output, lm_output_mask = self.llm(lm_input, lm_input_len.to(device))
|
|
|
+ logits = self.llm_decoder(lm_output)
|
|
|
+ loss = self.criterion_ce(logits, lm_target)
|
|
|
+ acc = th_accuracy(logits.view(-1, self.speech_token_size + 1), lm_target, ignore_label=IGNORE_ID)
|
|
|
+ return {'loss': loss, 'acc': acc}
|
|
|
+
|
|
|
+ def sampling_ids(
|
|
|
+ self,
|
|
|
+ weighted_scores: torch.Tensor,
|
|
|
+ decoded_tokens: List,
|
|
|
+ sampling: int,
|
|
|
+ ignore_eos: bool = True,
|
|
|
+ ):
|
|
|
+ num_trials, max_trials = 0, 100
|
|
|
+ while True:
|
|
|
+ top_ids = self.sampling(weighted_scores, decoded_tokens, sampling)
|
|
|
+ if (not ignore_eos) or (self.speech_token_size not in top_ids):
|
|
|
+ break
|
|
|
+ num_trials += 1
|
|
|
+ if num_trials > max_trials:
|
|
|
+ raise RuntimeError('sampling reaches max_trials {} and still get eos when ignore_eos is True, check your input!'.format(max_trials))
|
|
|
+ return top_ids
|
|
|
+
|
|
|
+ @torch.inference_mode()
|
|
|
+ def inference(
|
|
|
+ self,
|
|
|
+ text: torch.Tensor,
|
|
|
+ text_len: torch.Tensor,
|
|
|
+ prompt_text: torch.Tensor,
|
|
|
+ prompt_text_len: torch.Tensor,
|
|
|
+ prompt_speech_token: torch.Tensor,
|
|
|
+ prompt_speech_token_len: torch.Tensor,
|
|
|
+ embedding: torch.Tensor,
|
|
|
+ sampling: int = 25,
|
|
|
+ max_token_text_ratio: float = 20,
|
|
|
+ min_token_text_ratio: float = 2,
|
|
|
+ ) -> Generator[torch.Tensor, None, None]:
|
|
|
+ if self.fp16 is True:
|
|
|
+ embedding = embedding.half()
|
|
|
+
|
|
|
+ device = text.device
|
|
|
+ text = torch.concat([prompt_text, text], dim=1)
|
|
|
+ text_len += prompt_text_len
|
|
|
+ text = self.text_embedding(text)
|
|
|
+
|
|
|
+ # 1. encode text
|
|
|
+ text, text_len = self.encode(text, text_len)
|
|
|
+
|
|
|
+ # 2. encode embedding
|
|
|
+ if embedding.shape[0] != 0:
|
|
|
+ embedding = F.normalize(embedding, dim=1)
|
|
|
+ embedding = self.spk_embed_affine_layer(embedding)
|
|
|
+ embedding = embedding.unsqueeze(dim=1)
|
|
|
+ else:
|
|
|
+ embedding = torch.zeros(1, 0, self.llm_input_size, dtype=text.dtype).to(device).to(text.dtype)
|
|
|
+
|
|
|
+ # 3. concat llm_input
|
|
|
+ sos_eos_emb = self.llm_embedding.weight[self.sos_eos].reshape(1, 1, -1)
|
|
|
+ task_id_emb = self.llm_embedding.weight[self.task_id].reshape(1, 1, -1)
|
|
|
+ if prompt_speech_token_len != 0:
|
|
|
+ prompt_speech_token_emb = self.speech_embedding(prompt_speech_token)
|
|
|
+ else:
|
|
|
+ prompt_speech_token_emb = torch.zeros(1, 0, self.llm_input_size, dtype=text.dtype).to(device)
|
|
|
+ lm_input = torch.concat([sos_eos_emb, embedding, text, task_id_emb, prompt_speech_token_emb], dim=1)
|
|
|
+
|
|
|
+ # 4. cal min/max_length
|
|
|
+ min_len = int((text_len - prompt_text_len) * min_token_text_ratio)
|
|
|
+ max_len = int((text_len - prompt_text_len) * max_token_text_ratio)
|
|
|
+
|
|
|
+ # 5. step by step decode
|
|
|
+ out_tokens = []
|
|
|
+ offset = 0
|
|
|
+ att_cache, cnn_cache = torch.zeros((0, 0, 0, 0), device=lm_input.device), torch.zeros((0, 0, 0, 0), device=lm_input.device)
|
|
|
+ for i in range(max_len):
|
|
|
+ y_pred, att_cache, cnn_cache = self.llm.forward_chunk(lm_input, offset=offset, required_cache_size=-1,
|
|
|
+ att_cache=att_cache, cnn_cache=cnn_cache,
|
|
|
+ att_mask=torch.tril(torch.ones((1, lm_input.shape[1], lm_input.shape[1]),
|
|
|
+ device=lm_input.device)).to(torch.bool))
|
|
|
+ logp = self.llm_decoder(y_pred[:, -1]).log_softmax(dim=-1)
|
|
|
+ # force continue decode first token
|
|
|
+ if i == 0:
|
|
|
+ logp[:, self.speech_token_size] = -float('inf')
|
|
|
+ top_ids = self.sampling_ids(logp.squeeze(dim=0), out_tokens, sampling, ignore_eos=True if i < min_len else False).item()
|
|
|
+ if top_ids == self.speech_token_size:
|
|
|
+ break
|
|
|
+ # in stream mode, yield token one by one
|
|
|
+ yield top_ids
|
|
|
+ out_tokens.append(top_ids)
|
|
|
+ offset += lm_input.size(1)
|
|
|
+ lm_input = self.speech_embedding.weight[top_ids].reshape(1, 1, -1)
|
|
|
+
|
|
|
+
|
|
|
+class Qwen2Encoder(torch.nn.Module):
|
|
|
+ def __init__(self, pretrain_path):
|
|
|
+ super().__init__()
|
|
|
+ self.model = Qwen2ForCausalLM.from_pretrained(pretrain_path)
|
|
|
+
|
|
|
+ def forward_one_step(self, xs, masks, cache=None):
|
|
|
+ input_masks = masks[:, -1, :]
|
|
|
+ outs = self.model(
|
|
|
+ inputs_embeds=xs,
|
|
|
+ attention_mask=input_masks,
|
|
|
+ output_hidden_states=True,
|
|
|
+ return_dict=True,
|
|
|
+ use_cache=True,
|
|
|
+ past_key_values=cache,
|
|
|
+ )
|
|
|
+ xs = outs.hidden_states[-1]
|
|
|
+ new_cache = outs.past_key_values
|
|
|
+ return xs, new_cache
|
|
|
+
|
|
|
+
|
|
|
+class Qwen2LM(TransformerLM):
|
|
|
+ def __init__(
|
|
|
+ self,
|
|
|
+ llm_input_size: int,
|
|
|
+ llm_output_size: int,
|
|
|
+ speech_token_size: int,
|
|
|
+ llm: torch.nn.Module,
|
|
|
+ sampling: Callable,
|
|
|
+ length_normalized_loss: bool = True,
|
|
|
+ lsm_weight: float = 0.0,
|
|
|
+ mix_ratio: List[int] = [5, 15],
|
|
|
+ dpo: bool = False,
|
|
|
+ ):
|
|
|
+ torch.nn.Module.__init__(self)
|
|
|
+ self.llm_input_size = llm_input_size
|
|
|
+ self.llm_output_size = llm_output_size
|
|
|
+ self.speech_token_size = speech_token_size
|
|
|
+
|
|
|
+ # 2. build speech token language model related modules
|
|
|
+ self.sos_eos = 0
|
|
|
+ self.task_id = 1
|
|
|
+ self.fill_token = 2
|
|
|
+
|
|
|
+ self.llm_embedding = torch.nn.Embedding(2, llm_input_size)
|
|
|
+ self.llm = llm
|
|
|
+ self.llm_decoder = nn.Linear(llm_output_size, speech_token_size + 3)
|
|
|
+ self.criterion_ce = LabelSmoothingLoss(
|
|
|
+ size=speech_token_size + 3,
|
|
|
+ padding_idx=IGNORE_ID,
|
|
|
+ smoothing=lsm_weight,
|
|
|
+ normalize_length=length_normalized_loss,
|
|
|
+ )
|
|
|
+
|
|
|
+ # 3. [Optional] build speech token related modules
|
|
|
+ self.speech_embedding = torch.nn.Embedding(speech_token_size + 3, llm_input_size)
|
|
|
+
|
|
|
+ # 4. sampling method
|
|
|
+ self.sampling = sampling
|
|
|
+ self.mix_ratio = mix_ratio
|
|
|
+
|
|
|
+ # 5. [Optional] set dpo
|
|
|
+ self.dpo = dpo
|
|
|
+
|
|
|
+
|
|
|
+ def forward(
|
|
|
+ self,
|
|
|
+ batch: dict,
|
|
|
+ device: torch.device,
|
|
|
+ ) -> Dict[str, Optional[torch.Tensor]]:
|
|
|
+ text_token = batch['text_token'].to(device)
|
|
|
+ text_token_len = batch['text_token_len'].to(device)
|
|
|
+ speech_token = batch['speech_token'].to(device)
|
|
|
+ speech_token_len = batch['speech_token_len'].to(device)
|
|
|
+ if self.dpo:
|
|
|
+ reject_speech_token = batch['reject_speech_token'].to(device)
|
|
|
+ reject_speech_token_len = batch['reject_speech_token_len'].to(device)
|
|
|
+ # 1. prepare llm_target
|
|
|
+ sos_eos_emb = self.llm_embedding.weight[self.sos_eos].reshape(1, 1, -1)
|
|
|
+ task_id_emb = self.llm_embedding.weight[self.task_id].reshape(1, 1, -1)
|
|
|
+ target_ids = [torch.tensor([IGNORE_ID] * (1 + text_token_len[i]) + speech_token[i, :speech_token_len[i]].tolist() +
|
|
|
+ [self.speech_token_size]) for i in range(text_token.size(0))]
|
|
|
+ if self.dpo:
|
|
|
+ reject_target_ids = [torch.tensor([IGNORE_ID] * (1 + text_token_len[i]) + reject_speech_token[i, :reject_speech_token_len[i]].tolist() +
|
|
|
+ [self.speech_token_size]) for i in range(text_token.size(0))]
|
|
|
+ target_ids.extend(reject_target_ids)
|
|
|
+ target_ids = pad_sequence(target_ids, batch_first=True, padding_value=IGNORE_ID).to(device)
|
|
|
+
|
|
|
+ # 2. speech token projection
|
|
|
+ speech_emb = self.speech_embedding(speech_token)
|
|
|
+ if self.dpo:
|
|
|
+ reject_speech_emb = self.speech_embedding(reject_speech_token)
|
|
|
+
|
|
|
+ # 3. text token projection
|
|
|
+ text_token_lst = unpad_sequence(text_token, text_token_len, batch_first=True)
|
|
|
+ text_emb = [self.llm.model.model.embed_tokens(y) for y in text_token_lst]
|
|
|
+
|
|
|
+ # 4. prepare llm_input
|
|
|
+ speech_emb = unpad_sequence(speech_emb, speech_token_len.cpu(), batch_first=True)
|
|
|
+ input_emb = [torch.concat([sos_eos_emb.squeeze(dim=0), text_emb[i], task_id_emb.squeeze(dim=0), speech_emb[i]], dim=0)
|
|
|
+ for i in range(len(text_emb))]
|
|
|
+ if self.dpo:
|
|
|
+ reject_speech_emb = unpad_sequence(reject_speech_emb, reject_speech_token_len.cpu(), batch_first=True)
|
|
|
+ reject_input_emb = [torch.concat([sos_eos_emb.squeeze(dim=0), text_emb[i], task_id_emb.squeeze(dim=0), reject_speech_emb[i]], dim=0)
|
|
|
+ for i in range(len(text_emb))]
|
|
|
+ input_emb.extend(reject_input_emb)
|
|
|
+ input_emb_lengths = torch.tensor([i.size(0) for i in input_emb], dtype=torch.int32).to(device)
|
|
|
+ input_emb = pad_sequence(input_emb, batch_first=True, padding_value=IGNORE_ID).to(device)
|
|
|
+
|
|
|
+ attention_mask = ~make_pad_mask(input_emb_lengths)
|
|
|
+
|
|
|
+ result = self.llm.model(
|
|
|
+ inputs_embeds=input_emb,
|
|
|
+ attention_mask=attention_mask,
|
|
|
+ return_dict=True
|
|
|
+ )
|
|
|
+ hidden_states = result.hidden_states
|
|
|
+ logits = self.llm_decoder(hidden_states[-1])
|
|
|
+ loss = self.criterion_ce(logits[: speech_token.shape[0]], target_ids[: speech_token.shape[0]])
|
|
|
+ acc = th_accuracy(
|
|
|
+ logits[: speech_token.shape[0]].view(-1, self.speech_token_size + 3),
|
|
|
+ target_ids[: speech_token.shape[0]],
|
|
|
+ ignore_label=IGNORE_ID,
|
|
|
+ )
|
|
|
+ if not self.dpo:
|
|
|
+ return {
|
|
|
+ "loss": loss,
|
|
|
+ "acc": acc,
|
|
|
+ }
|
|
|
+ else:
|
|
|
+ all_logps_sum, all_logps_mean = self.get_batch_logps(
|
|
|
+ logits, target_ids, attention_mask, text_token_len, average_log_prob=False, ignore_id=IGNORE_ID
|
|
|
+ )
|
|
|
+ chosen_logps = all_logps_sum[: speech_token.shape[0]]
|
|
|
+ rejected_logps = all_logps_sum[speech_token.shape[0]:]
|
|
|
+ return {
|
|
|
+ "loss": loss,
|
|
|
+ "acc": acc,
|
|
|
+ "chosen_logps": chosen_logps,
|
|
|
+ "rejected_logps": rejected_logps
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ def get_batch_logps(
|
|
|
+ self,
|
|
|
+ logits: torch.FloatTensor,
|
|
|
+ labels: torch.LongTensor,
|
|
|
+ attention_mask,
|
|
|
+ prompt_token_lens,
|
|
|
+ average_log_prob: bool = False,
|
|
|
+ ignore_id: int = -1,
|
|
|
+ ) -> torch.FloatTensor:
|
|
|
+ """Compute the log probabilities of the given labels under the given logits.
|
|
|
+
|
|
|
+ Args:
|
|
|
+ logits: Logits of the model (unnormalized). Shape: (batch_size, sequence_length, vocab_size)
|
|
|
+ labels: Labels for which to compute the log probabilities. Label tokens with a value of -100 are ignored. Shape: (batch_size, sequence_length)
|
|
|
+ average_log_prob: If True, return the average log probability per (non-masked) token. Otherwise, return the sum of the log probabilities of the (non-masked) tokens.
|
|
|
+
|
|
|
+ Returns:
|
|
|
+ A tensor of shape (batch_size,) containing the average/sum log probabilities of the given labels under the given logits.
|
|
|
+ """
|
|
|
+ assert average_log_prob == False
|
|
|
+ assert logits.shape[:-1] == labels.shape
|
|
|
+ labels = labels[:, 1:].clone()
|
|
|
+ logits = logits[:, :-1, :]
|
|
|
+ loss_masks = attention_mask.clone().bool()
|
|
|
+ # mask prompts
|
|
|
+ for mask, text_token_len in zip(loss_masks, prompt_token_lens):
|
|
|
+ mask[:text_token_len + 1] = False
|
|
|
+ loss_masks = loss_masks[:, 1:]
|
|
|
+ labels[loss_masks == False] = 0
|
|
|
+ # dummy token; we'll ignore the losses on these tokens later
|
|
|
+ ignore = labels == ignore_id
|
|
|
+ labels = labels.masked_fill(ignore, 0) # avoid -1 index
|
|
|
+ per_token_logps = torch.gather(logits.log_softmax(-1), dim=2, index=labels.unsqueeze(2)).squeeze(2) # (bs, time,)
|
|
|
+ logprobs_sums = (per_token_logps * loss_masks).sum(-1)
|
|
|
+ logprobs_means = (per_token_logps * loss_masks).sum(-1) / loss_masks.sum(-1)
|
|
|
+ return logprobs_sums, logprobs_means
|
|
|
+
|
|
|
+
|
|
|
+ @torch.inference_mode()
|
|
|
+ def inference(
|
|
|
+ self,
|
|
|
+ text: torch.Tensor,
|
|
|
+ text_len: torch.Tensor,
|
|
|
+ prompt_text: torch.Tensor,
|
|
|
+ prompt_text_len: torch.Tensor,
|
|
|
+ prompt_speech_token: torch.Tensor,
|
|
|
+ prompt_speech_token_len: torch.Tensor,
|
|
|
+ embedding: torch.Tensor,
|
|
|
+ sampling: int = 25,
|
|
|
+ max_token_text_ratio: float = 20,
|
|
|
+ min_token_text_ratio: float = 2,
|
|
|
+ ) -> Generator[torch.Tensor, None, None]:
|
|
|
+ device = text.device
|
|
|
+ text = torch.concat([prompt_text, text], dim=1)
|
|
|
+ text_len += prompt_text_len
|
|
|
+ text = self.llm.model.model.embed_tokens(text)
|
|
|
+
|
|
|
+ # 3. concat llm_input
|
|
|
+ sos_eos_emb = self.llm_embedding.weight[self.sos_eos].reshape(1, 1, -1)
|
|
|
+ task_id_emb = self.llm_embedding.weight[self.task_id].reshape(1, 1, -1)
|
|
|
+ if prompt_speech_token_len != 0:
|
|
|
+ prompt_speech_token_emb = self.speech_embedding(prompt_speech_token)
|
|
|
+ else:
|
|
|
+ prompt_speech_token_emb = torch.zeros(1, 0, self.llm_input_size, dtype=text.dtype).to(device)
|
|
|
+ lm_input = torch.concat([sos_eos_emb, text, task_id_emb, prompt_speech_token_emb], dim=1)
|
|
|
+
|
|
|
+ # 4. cal min/max_length
|
|
|
+ min_len = int((text_len - prompt_text_len) * min_token_text_ratio)
|
|
|
+ max_len = int((text_len - prompt_text_len) * max_token_text_ratio)
|
|
|
+
|
|
|
+ # 5. step by step decode
|
|
|
+ out_tokens = []
|
|
|
+ cache = None
|
|
|
+ for i in range(max_len):
|
|
|
+ y_pred, cache = self.llm.forward_one_step(lm_input,
|
|
|
+ masks=torch.tril(torch.ones((1, lm_input.shape[1], lm_input.shape[1]), device=lm_input.device)).to(torch.bool),
|
|
|
+ cache=cache)
|
|
|
+ logp = self.llm_decoder(y_pred[:, -1]).log_softmax(dim=-1)
|
|
|
+ top_ids = self.sampling_ids(logp.squeeze(dim=0), out_tokens, sampling, ignore_eos=True if i < min_len else False).item()
|
|
|
+ if top_ids == self.speech_token_size:
|
|
|
+ break
|
|
|
+ if top_ids > self.speech_token_size:
|
|
|
+ continue
|
|
|
+ # in stream mode, yield token one by one
|
|
|
+ yield top_ids
|
|
|
+ out_tokens.append(top_ids)
|
|
|
+ lm_input = self.speech_embedding.weight[top_ids].reshape(1, 1, -1)
|
|
|
+
|
|
|
+ @torch.inference_mode()
|
|
|
+ def inference_bistream(
|
|
|
+ self,
|
|
|
+ text: Generator,
|
|
|
+ prompt_text: torch.Tensor,
|
|
|
+ prompt_text_len: torch.Tensor,
|
|
|
+ prompt_speech_token: torch.Tensor,
|
|
|
+ prompt_speech_token_len: torch.Tensor,
|
|
|
+ embedding: torch.Tensor,
|
|
|
+ sampling: int = 25,
|
|
|
+ max_token_text_ratio: float = 20,
|
|
|
+ min_token_text_ratio: float = 2,
|
|
|
+ ) -> Generator[torch.Tensor, None, None]:
|
|
|
+
|
|
|
+ device = prompt_text.device
|
|
|
+ # 1. prepare input
|
|
|
+ sos_eos_emb = self.llm_embedding.weight[self.sos_eos].reshape(1, 1, -1)
|
|
|
+ task_id_emb = self.llm_embedding.weight[self.task_id].reshape(1, 1, -1)
|
|
|
+ if prompt_speech_token_len != 0:
|
|
|
+ prompt_speech_token_emb = self.speech_embedding(prompt_speech_token)
|
|
|
+ else:
|
|
|
+ prompt_speech_token_emb = torch.zeros(1, 0, self.llm_input_size, dtype=prompt_text.dtype).to(device)
|
|
|
+ lm_input = torch.concat([sos_eos_emb], dim=1)
|
|
|
+
|
|
|
+ # 2. iterate text
|
|
|
+ out_tokens = []
|
|
|
+ cache = None
|
|
|
+ # NOTE init prompt_text as text_cache as it is basically impossible prompt_speech_token/prompt_text < 15/5
|
|
|
+ text_cache = self.llm.model.model.embed_tokens(prompt_text)
|
|
|
+ next_fill_index = -1
|
|
|
+ for this_text in text:
|
|
|
+ text_cache = torch.concat([text_cache, self.llm.model.model.embed_tokens(this_text)], dim=1)
|
|
|
+ # prompt_speech_token_emb not empty, try append to lm_input
|
|
|
+ while prompt_speech_token_emb.size(1) != 0:
|
|
|
+ if text_cache.size(1) >= self.mix_ratio[0]:
|
|
|
+ lm_input_text, lm_input_speech = text_cache[:, :self.mix_ratio[0]], prompt_speech_token_emb[:, :self.mix_ratio[1]]
|
|
|
+ logging.info('append {} text token {} speech token'.format(lm_input_text.size(1), lm_input_speech.size(1)))
|
|
|
+ lm_input = torch.concat([lm_input, lm_input_text, lm_input_speech], dim=1)
|
|
|
+ text_cache, prompt_speech_token_emb = text_cache[:, self.mix_ratio[0]:], prompt_speech_token_emb[:, self.mix_ratio[1]:]
|
|
|
+ else:
|
|
|
+ logging.info('not enough text token to decode, wait for more')
|
|
|
+ break
|
|
|
+ # no prompt_speech_token_emb remain, can decode some speech token
|
|
|
+ if prompt_speech_token_emb.size(1) == 0:
|
|
|
+ if (len(out_tokens) != 0 and out_tokens[-1] == self.speech_token_size + 2) or (len(out_tokens) == 0 and lm_input.size(1) == 1):
|
|
|
+ logging.info('get fill token, need to append more text token')
|
|
|
+ if text_cache.size(1) >= self.mix_ratio[0]:
|
|
|
+ lm_input_text = text_cache[:, :self.mix_ratio[0]]
|
|
|
+ logging.info('append {} text token'.format(lm_input_text.size(1)))
|
|
|
+ if len(out_tokens) != 0 and out_tokens[-1] == self.speech_token_size + 2:
|
|
|
+ lm_input = lm_input_text
|
|
|
+ else:
|
|
|
+ lm_input = torch.concat([lm_input, lm_input_text], dim=1)
|
|
|
+ text_cache = text_cache[:, self.mix_ratio[0]:]
|
|
|
+ else:
|
|
|
+ logging.info('not enough text token to decode, wait for more')
|
|
|
+ continue
|
|
|
+ while True:
|
|
|
+ seq_len = lm_input.shape[1] if cache is None else lm_input.shape[1] + cache[0][0].size(2)
|
|
|
+ y_pred, cache = self.llm.forward_one_step(lm_input,
|
|
|
+ masks=torch.tril(torch.ones((1, seq_len, seq_len), device=lm_input.device)).to(torch.bool),
|
|
|
+ cache=cache)
|
|
|
+ logp = self.llm_decoder(y_pred[:, -1]).log_softmax(dim=-1)
|
|
|
+ if next_fill_index != -1 and len(out_tokens) == next_fill_index:
|
|
|
+ top_ids = self.speech_token_size + 2
|
|
|
+ next_fill_index += (self.mix_ratio[1] + 1)
|
|
|
+ else:
|
|
|
+ top_ids = self.sampling_ids(logp.squeeze(dim=0), out_tokens, sampling, ignore_eos=True).item()
|
|
|
+ if top_ids == self.speech_token_size + 2:
|
|
|
+ next_fill_index = len(out_tokens) + self.mix_ratio[1] + 1
|
|
|
+ logging.info('fill_token index {} next fill_token index {}'.format(len(out_tokens), next_fill_index))
|
|
|
+ out_tokens.append(top_ids)
|
|
|
+ if top_ids >= self.speech_token_size:
|
|
|
+ if top_ids == self.speech_token_size + 2:
|
|
|
+ break
|
|
|
+ else:
|
|
|
+ raise ValueError('should not get token {}'.format(top_ids))
|
|
|
+ yield top_ids
|
|
|
+ lm_input = self.speech_embedding.weight[top_ids].reshape(1, 1, -1)
|
|
|
+
|
|
|
+ # 3. final decode
|
|
|
+ lm_input = torch.concat([lm_input, text_cache, task_id_emb], dim=1)
|
|
|
+ logging.info('no more text token, decode until met eos')
|
|
|
+ while True:
|
|
|
+ seq_len = lm_input.shape[1] if cache is None else lm_input.shape[1] + cache[0][0].size(2)
|
|
|
+ y_pred, cache = self.llm.forward_one_step(lm_input,
|
|
|
+ masks=torch.tril(torch.ones((1, seq_len, seq_len), device=lm_input.device)).to(torch.bool),
|
|
|
+ cache=cache)
|
|
|
+ logp = self.llm_decoder(y_pred[:, -1]).log_softmax(dim=-1)
|
|
|
+ top_ids = self.sampling_ids(logp.squeeze(dim=0), out_tokens, sampling, ignore_eos=False).item()
|
|
|
+ out_tokens.append(top_ids)
|
|
|
+ if top_ids >= self.speech_token_size:
|
|
|
+ if top_ids == self.speech_token_size:
|
|
|
+ break
|
|
|
+ else:
|
|
|
+ raise ValueError('should not get token {}'.format(top_ids))
|
|
|
+ # in stream mode, yield token one by one
|
|
|
+ yield top_ids
|
|
|
+ lm_input = self.speech_embedding.weight[top_ids].reshape(1, 1, -1)
|