File size: 22,525 Bytes
1938217
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
import copy
import torch
import torch.nn as nn
import torch.nn.functional as F

from transformers.models.llama.modeling_llama import LlamaDecoderLayer
from transformers.models.qwen2.modeling_qwen2 import Qwen2DecoderLayer
from omni_speech.constants import IGNORE_INDEX

torch.autograd.set_detect_anomaly(True)

try:
    import sys
    sys.path.append('/mnt/lzy/LLaMA-Omni/CosyVoice/')
    from cosyvoice.cli.cosyvoice import CosyVoice
except:
    print('CosyVoice not found')

import os
if 'SPEECH_GEN_CONV_KERNEL' in os.environ:
    SPEECH_GEN_CONV_KERNEL = int(os.environ['SPEECH_GEN_CONV_KERNEL'])
    print(f'Using SPEECH_GEN_CONV_KERNEL={SPEECH_GEN_CONV_KERNEL}')
else:
    SPEECH_GEN_CONV_KERNEL = -1

if 'DISTILL_EMBEDDING' in os.environ:
    DISTILL_EMBEDDING = True
    print(f'DISTILL_EMBEDDING is set.')
else:
    DISTILL_EMBEDDING = False

def lengths_to_padding_mask(lens):
    bsz, max_lens = lens.size(0), torch.max(lens).item()
    mask = torch.arange(max_lens).to(lens.device).view(1, max_lens)
    mask = mask.expand(bsz, -1) >= lens.view(bsz, 1).expand(-1, max_lens)
    return mask


def _uniform_assignment(src_lens, tgt_lens):
    tgt_indices = torch.arange(torch.max(tgt_lens)).expand(len(tgt_lens), -1).to(tgt_lens.device)
    ratio = tgt_lens / src_lens
    index_t = (tgt_indices / ratio.view(-1, 1)).long()
    return index_t


class SpeechGeneratorCTC(nn.Module):
    def __init__(self, config):
        super().__init__()
        n_layers, n_dims, n_heads, n_inter_dims = list(map(int, config.ctc_decoder_config[1:-1].split(",")))
        _config = copy.deepcopy(config)
        _config.hidden_size = n_dims
        _config.num_hidden_layers = n_layers
        _config.num_attention_heads = n_heads
        _config.num_key_value_heads = n_heads
        _config.intermediate_size = n_inter_dims
        _config._attn_implementation = "flash_attention_2"
        self.upsample_factor = config.ctc_upsample_factor
        self.input_proj = nn.Linear(config.hidden_size, n_dims)
        self.layers = nn.ModuleList(
            [LlamaDecoderLayer(_config, layer_idx) for layer_idx in range(n_layers)]
        )
        self.unit_vocab_size = config.unit_vocab_size
        self.output_proj = nn.Linear(n_dims, config.unit_vocab_size + 1)

    def upsample(self, reps, tgt_units=None):
        src_lens = torch.LongTensor([len(rep) for rep in reps]).to(reps[0].device)
        up_lens = src_lens * self.upsample_factor
        if tgt_units is not None:
            tgt_lens = tgt_units.ne(IGNORE_INDEX).long().sum(dim=-1)
            up_lens = torch.max(up_lens, tgt_lens)
        reps = torch.nn.utils.rnn.pad_sequence(reps, batch_first=True)
        padding_mask = lengths_to_padding_mask(up_lens)
        mapped_inputs = _uniform_assignment(src_lens, up_lens).masked_fill(
            padding_mask, 0
        )
        copied_reps = torch.gather(
            reps,
            1,
            mapped_inputs.unsqueeze(-1).expand(
                *mapped_inputs.size(), reps.size(-1)
            ),
        )
        copied_reps = copied_reps.masked_fill(padding_mask.unsqueeze(-1), 0)
        position_ids = torch.arange(0, max(up_lens)).unsqueeze(0).expand(len(reps), -1).to(device=copied_reps.device)
        return copied_reps, ~padding_mask, position_ids
    
    def forward(self, tgt_reps, labels, tgt_units):
        tgt_label_reps = []
        for tgt_rep, label in zip(tgt_reps, labels):
            tgt_label_reps.append(tgt_rep[label != IGNORE_INDEX])
        hidden_states, attention_mask, position_ids = self.upsample(tgt_label_reps, tgt_units)
        hidden_states = self.input_proj(hidden_states)
        for layer in self.layers:
            layer_outputs = layer(
                hidden_states,
                attention_mask=attention_mask,
                position_ids=position_ids,
            )
            hidden_states = layer_outputs[0]
        ctc_logits = self.output_proj(hidden_states)
        ctc_lprobs = F.log_softmax(ctc_logits.float(), dim=-1, dtype=torch.float32)
        ctc_lens = attention_mask.long().sum(dim=-1)
        ctc_tgt_lens = tgt_units.ne(IGNORE_INDEX).long().sum(dim=-1)
        ctc_tgt_mask = ~lengths_to_padding_mask(ctc_tgt_lens)
        ctc_tgt_flat = tgt_units.masked_select(ctc_tgt_mask)
        ctc_loss = F.ctc_loss(
            ctc_lprobs.transpose(0, 1),
            ctc_tgt_flat,
            ctc_lens,
            ctc_tgt_lens,
            reduction="sum",
            zero_infinity=True,
            blank=self.unit_vocab_size
        )
        ctc_loss /= ctc_tgt_lens.sum().item()
        return ctc_loss
    
    def predict(self, tgt_reps):
        hidden_states, attention_mask, position_ids = self.upsample([tgt_reps])
        hidden_states = self.input_proj(hidden_states)
        for layer in self.layers:
            layer_outputs = layer(
                hidden_states,
                attention_mask=attention_mask,
                position_ids=position_ids,
            )
            hidden_states = layer_outputs[0]
        ctc_logits = self.output_proj(hidden_states)
        ctc_lprobs = F.log_softmax(ctc_logits.float(), dim=-1, dtype=torch.float32)
        ctc_pred = ctc_lprobs.argmax(dim=-1).masked_fill_(~attention_mask, self.unit_vocab_size)
        return ctc_pred


class SpeechGeneratorCTCQwen(nn.Module):
    def __init__(self, config):
        super().__init__()
        n_layers, n_dims, n_heads, n_inter_dims, n_kv_heads = list(map(int, config.ctc_decoder_config[1:-1].split(",")))
        _config = copy.deepcopy(config)
        _config.hidden_size = n_dims
        _config.num_hidden_layers = n_layers
        _config.num_attention_heads = n_heads
        _config.num_key_value_heads = n_kv_heads
        _config.intermediate_size = n_inter_dims
        _config._attn_implementation = "flash_attention_2"
        self.upsample_factor = config.ctc_upsample_factor
        self.input_proj = nn.Linear(config.hidden_size, n_dims)
        self.layers = nn.ModuleList(
            [Qwen2DecoderLayer(_config, layer_idx) for layer_idx in range(n_layers)]
        )
        self.unit_vocab_size = config.unit_vocab_size
        self.output_proj = nn.Linear(n_dims, config.unit_vocab_size + 1)

        if SPEECH_GEN_CONV_KERNEL > 0:
            self.temporal_conv = nn.Conv1d(n_dims, n_dims, SPEECH_GEN_CONV_KERNEL, padding=0)
            self.learnable_pad_left = nn.Parameter(torch.zeros(SPEECH_GEN_CONV_KERNEL // 2, n_dims))
            self.learnable_pad_right = nn.Parameter(torch.zeros(SPEECH_GEN_CONV_KERNEL // 2, n_dims))
            # self.conv_layer_id = n_layers // 2  # Insert temporal conv layer in the middle of the decoder layers

    def upsample(self, reps, tgt_units=None):
        src_lens = torch.LongTensor([len(rep) for rep in reps]).to(reps[0].device)
        up_lens = src_lens * self.upsample_factor
        if tgt_units is not None:
            tgt_lens = tgt_units.ne(IGNORE_INDEX).long().sum(dim=-1)
            up_lens = torch.max(up_lens, tgt_lens)
        reps = torch.nn.utils.rnn.pad_sequence(reps, batch_first=True)
        padding_mask = lengths_to_padding_mask(up_lens)
        mapped_inputs = _uniform_assignment(src_lens, up_lens).masked_fill(
            padding_mask, 0
        )
        copied_reps = torch.gather(
            reps,
            1,
            mapped_inputs.unsqueeze(-1).expand(
                *mapped_inputs.size(), reps.size(-1)
            ),
        )
        copied_reps = copied_reps.masked_fill(padding_mask.unsqueeze(-1), 0)
        position_ids = torch.arange(0, max(up_lens)).unsqueeze(0).expand(len(reps), -1).to(device=copied_reps.device)
        return copied_reps, ~padding_mask, position_ids
    
    def forward(self, tgt_reps, labels, tgt_units):
        tgt_label_reps = []
        for tgt_rep, label in zip(tgt_reps, labels):
            if SPEECH_GEN_CONV_KERNEL > 0:
                now_rep = tgt_rep[label != IGNORE_INDEX]
                now_rep = torch.cat([self.learnable_pad_left, now_rep, self.learnable_pad_right], dim=0)
                now_rep = self.input_proj(now_rep)[None]
                now_rep = self.temporal_conv(now_rep.transpose(1, 2)).transpose(1, 2)[0]
                tgt_label_reps.append(now_rep)
            else:
                tgt_label_reps.append(tgt_rep[label != IGNORE_INDEX])
        
        hidden_states, attention_mask, position_ids = self.upsample(tgt_label_reps, tgt_units)
        
        if SPEECH_GEN_CONV_KERNEL < 0:
            hidden_states = self.input_proj(hidden_states)

        for layer_id, layer in enumerate(self.layers):
            # if SPEECH_GEN_CONV_KERNEL:
            #     if layer_id == self.conv_layer_id:
            #         hidden_states = self.temporal_conv(hidden_states.transpose(1, 2)).transpose(1, 2)

            layer_outputs = layer(
                hidden_states,
                attention_mask=attention_mask,
                position_ids=position_ids,
            )
            hidden_states = layer_outputs[0]

        ctc_logits = self.output_proj(hidden_states)
        ctc_lprobs = F.log_softmax(ctc_logits.float(), dim=-1, dtype=torch.float32)
        ctc_lens = attention_mask.long().sum(dim=-1)
        ctc_tgt_lens = tgt_units.ne(IGNORE_INDEX).long().sum(dim=-1)
        ctc_tgt_mask = ~lengths_to_padding_mask(ctc_tgt_lens)
        ctc_tgt_flat = tgt_units.masked_select(ctc_tgt_mask)
        ctc_loss = F.ctc_loss(
            ctc_lprobs.transpose(0, 1),
            ctc_tgt_flat,
            ctc_lens,
            ctc_tgt_lens,
            reduction="sum",
            zero_infinity=True,
            blank=self.unit_vocab_size
        )
        ctc_loss /= ctc_tgt_lens.sum().item()
        return ctc_loss
    
    def predict(self, tgt_reps):
        hidden_states, attention_mask, position_ids = self.upsample([tgt_reps])
        hidden_states = self.input_proj(hidden_states)
        for layer in self.layers:
            layer_outputs = layer(
                hidden_states,
                attention_mask=attention_mask,
                position_ids=position_ids,
            )
            hidden_states = layer_outputs[0]
        ctc_logits = self.output_proj(hidden_states)
        ctc_lprobs = F.log_softmax(ctc_logits.float(), dim=-1, dtype=torch.float32)
        ctc_pred = ctc_lprobs.argmax(dim=-1).masked_fill_(~attention_mask, self.unit_vocab_size)
        return ctc_pred


class SpeechGeneratorCEQwen(nn.Module):
    def __init__(self, config):
        super().__init__()
        n_layers, n_dims, n_heads, n_inter_dims, n_kv_heads = list(map(int, config.ctc_decoder_config[1:-1].split(",")))
        _config = copy.deepcopy(config)
        _config.hidden_size = n_dims
        _config.num_hidden_layers = n_layers
        _config.num_attention_heads = n_heads
        _config.num_key_value_heads = n_kv_heads
        _config.intermediate_size = n_inter_dims
        _config._attn_implementation = "flash_attention_2"
        self.upsample_factor = 1
        self.input_proj = nn.Linear(config.hidden_size, n_dims)
        self.layers = nn.ModuleList(
            [Qwen2DecoderLayer(_config, layer_idx) for layer_idx in range(n_layers)]
        )
        self.unit_vocab_size = config.unit_vocab_size
        self.output_proj = nn.Linear(n_dims, config.unit_vocab_size + 1)

    def upsample(self, reps, tgt_units=None):
        src_lens = torch.LongTensor([len(rep) for rep in reps]).to(reps[0].device)
        up_lens = src_lens * self.upsample_factor
        if tgt_units is not None:
            tgt_lens = tgt_units.ne(IGNORE_INDEX).long().sum(dim=-1)
            up_lens = torch.max(up_lens, tgt_lens)
        reps = torch.nn.utils.rnn.pad_sequence(reps, batch_first=True)
        padding_mask = lengths_to_padding_mask(up_lens)
        mapped_inputs = _uniform_assignment(src_lens, up_lens).masked_fill(
            padding_mask, 0
        )
        copied_reps = torch.gather(
            reps,
            1,
            mapped_inputs.unsqueeze(-1).expand(
                *mapped_inputs.size(), reps.size(-1)
            ),
        )
        copied_reps = copied_reps.masked_fill(padding_mask.unsqueeze(-1), 0)
        position_ids = torch.arange(0, max(up_lens)).unsqueeze(0).expand(len(reps), -1).to(device=copied_reps.device)
        return copied_reps, ~padding_mask, position_ids
    
    def forward(self, tgt_reps, labels, tgt_units):
        tgt_label_reps = []
        for tgt_rep, label in zip(tgt_reps, labels):
            # if SPEECH_GEN_CONV_KERNEL > 0:
            #     now_rep = tgt_rep[label != IGNORE_INDEX]
            #     now_rep = torch.cat([self.learnable_pad_left, now_rep, self.learnable_pad_right], dim=0)
            #     now_rep = self.input_proj(now_rep)[None]
            #     now_rep = self.temporal_conv(now_rep.transpose(1, 2)).transpose(1, 2)[0]
            #     tgt_label_reps.append(now_rep)
            # else:
            tgt_label_reps.append(tgt_rep[label != IGNORE_INDEX])
        hidden_states, attention_mask, position_ids = self.upsample(tgt_label_reps, tgt_units)
        # if SPEECH_GEN_CONV_KERNEL < 0:
        hidden_states = self.input_proj(hidden_states)

        for layer_id, layer in enumerate(self.layers):
            # if SPEECH_GEN_CONV_KERNEL:
            #     if layer_id == self.conv_layer_id:
            #         hidden_states = self.temporal_conv(hidden_states.transpose(1, 2)).transpose(1, 2)

            layer_outputs = layer(
                hidden_states,
                attention_mask=attention_mask,
                position_ids=position_ids,
            )
            hidden_states = layer_outputs[0]

        shift_hidden_states = hidden_states[..., :-1, :].contiguous().reshape(-1, hidden_states.size(-1))
        logits = self.output_proj(shift_hidden_states)
        shift_labels = tgt_units[..., 1:].contiguous().reshape(-1)
        assert shift_labels.size(0) == shift_hidden_states.size(0)
        loss_fct = nn.CrossEntropyLoss()
        logits = logits.float()
        loss = loss_fct(logits, shift_labels)
        # loss = (loss / 1.0).sum().item()
        # loss = loss.sum().item()
        return loss
    
    # def predict(self, tgt_reps):
    #     hidden_states, attention_mask, position_ids = self.upsample([tgt_reps])
    #     hidden_states = self.input_proj(hidden_states)
    #     for layer in self.layers:
    #         layer_outputs = layer(
    #             hidden_states,
    #             attention_mask=attention_mask,
    #             position_ids=position_ids,
    #         )
    #         hidden_states = layer_outputs[0]
    #     ctc_logits = self.output_proj(hidden_states)
    #     ctc_lprobs = F.log_softmax(ctc_logits.float(), dim=-1, dtype=torch.float32)
    #     ctc_pred = ctc_lprobs.argmax(dim=-1).masked_fill_(~attention_mask, self.unit_vocab_size)
    #     return ctc_pred

# class SpeechGeneratorCosyVoice(nn.Module):
#     def __init__(self, config):
#         super().__init__()
#         self.input_proj = nn.Sequential(
#             nn.Linear(config.hidden_size, 1024),
#             nn.GELU(),
#             nn.Linear(1024, 512)
#         )
#         self.cosyvoice1 = CosyVoice('CosyVoice/pretrained_models/CosyVoice-300M-SFT', load_jit=False, load_onnx=False, fp16=False)
#         self.cosyvoice = CosyVoice('CosyVoice/pretrained_models/CosyVoice-300M-SFT', load_jit=True, load_onnx=False, fp16=True)
#         self.llm = self.cosyvoice1.model.llm

#         if DISTILL_EMBEDDING:
#             self.criterion = nn.CosineEmbeddingLoss()

#     def forward(self, tgt_reps, labels, answer):
#         tgt_label_reps = []
#         batch_speech_tokens = []
#         embeddings = []
#         target_embeddings = []
#         if DISTILL_EMBEDDING:
#             for tgt_rep, label, ans in zip(tgt_reps, labels, answer):
#                 # make all label id in [151644,151645,198] to IGNORE_INDEX
#                 label[label == 151644] = IGNORE_INDEX
#                 label[label == 151645] = IGNORE_INDEX
#                 label[label == 198] = IGNORE_INDEX
#                 tgt_label_reps.append(tgt_rep[label != IGNORE_INDEX])
#                 normalized_text = self.cosyvoice1.frontend.text_normalize(ans, split=True)
#                 tts_text_token_all = []
#                 for norm_text in normalized_text:
#                     tts_text_token, tts_text_token_len = self.cosyvoice1.frontend._extract_text_token(norm_text)
#                     tts_text_token_all.append(tts_text_token)
#                 tts_text_token_all = torch.cat(tts_text_token_all, dim=0)
#                 target_embedding = self.cosyvoice1.model.llm.text_embedding(tts_text_token)
#                 target_embeddings.append(target_embedding)
#             import pdb;pdb.set_trace()
#             tgt_label_reps = torch.stack(tgt_label_reps)
#             target_embeddings = torch.stack(target_embeddings).squeeze(1)
#             hidden_states = self.input_proj(tgt_label_reps).reshape(-1, 512)
#             target_embeddings = target_embeddings.reshape(-1, 512)
#             loss = self.criterion(hidden_states, target_embeddings, torch.ones(hidden_states.size(0)).to(hidden_states.device))
#         else:
#             for tgt_rep, label, ans in zip(tgt_reps, labels, answer):
#                 # make all label id in [151644,151645,198] to IGNORE_INDEX
#                 label[label == 151644] = IGNORE_INDEX
#                 label[label == 151645] = IGNORE_INDEX
#                 label[label == 198] = IGNORE_INDEX
#                 tgt_label_reps.append(tgt_rep[label != IGNORE_INDEX])
#                 speech_token = self.cosyvoice.inference_label(ans, '英文女', stream=False)
#                 speech_tokens = []
#                 for i,j in enumerate(speech_token):
#                     speech_tokens.append(j['tts_speech_token'].squeeze(0))
#                 speech_tokens.append(torch.tensor([0]))
#                 speech_tokens = torch.cat(speech_tokens, dim=0)
#                 if speech_tokens.size(0) > 1:
#                     speech_tokens = speech_tokens[:-1]
#                 batch_speech_tokens.append(speech_tokens)
#                 embedding = self.cosyvoice.frontend.frontend_embedding('英文女')
#                 embeddings.append(embedding['llm_embedding'].squeeze(0))

#             tgt_label_reps = torch.stack(tgt_label_reps)
#             batch_speech_token = torch.stack(batch_speech_tokens)
#             embeddings = torch.stack(embeddings)
#             hidden_states = self.input_proj(tgt_label_reps)
#             batch = {'text_feature': hidden_states, 'text_token_len': torch.tensor([hidden_states.size(1)]).repeat(hidden_states.size(0)),
#                     'speech_token': batch_speech_token, 'speech_token_len': torch.tensor([batch_speech_token.size(1)]).repeat(hidden_states.size(0)),
#                     'embedding': embeddings}
#             output = self.llm.forward_ours(batch, 'cuda')
#             loss = output['loss']
#         return loss

class SpeechGeneratorCosyVoice(nn.Module):
    def __init__(self, config):
        super().__init__()
        self.cosyvoice = CosyVoice('CosyVoice/pretrained_models/CosyVoice-300M-SFT', load_jit=True, load_onnx=False, fp16=True)

    def forward(self, tgt_reps, labels, answer):
        tgt_label_reps = []
        batch_speech_tokens = []
        embeddings = []
        target_embeddings = []
        if DISTILL_EMBEDDING:
            for tgt_rep, label, ans in zip(tgt_reps, labels, answer):
                # make all label id in [151644,151645,198] to IGNORE_INDEX
                label[label == 151644] = IGNORE_INDEX
                label[label == 151645] = IGNORE_INDEX
                label[label == 198] = IGNORE_INDEX
                tgt_label_reps.append(tgt_rep[label != IGNORE_INDEX])
                normalized_text = self.cosyvoice1.frontend.text_normalize(ans, split=True)
                tts_text_token_all = []
                for norm_text in normalized_text:
                    tts_text_token, tts_text_token_len = self.cosyvoice1.frontend._extract_text_token(norm_text)
                    tts_text_token_all.append(tts_text_token)
                tts_text_token_all = torch.cat(tts_text_token_all, dim=0)
                target_embedding = self.cosyvoice1.model.llm.text_embedding(tts_text_token)
                target_embeddings.append(target_embedding)
            import pdb;pdb.set_trace()
            tgt_label_reps = torch.stack(tgt_label_reps)
            target_embeddings = torch.stack(target_embeddings).squeeze(1)
            hidden_states = self.input_proj(tgt_label_reps).reshape(-1, 512)
            target_embeddings = target_embeddings.reshape(-1, 512)
            loss = self.criterion(hidden_states, target_embeddings, torch.ones(hidden_states.size(0)).to(hidden_states.device))
        else:
            for tgt_rep, label, ans in zip(tgt_reps, labels, answer):
                # make all label id in [151644,151645,198] to IGNORE_INDEX
                label[label == 151644] = IGNORE_INDEX
                label[label == 151645] = IGNORE_INDEX
                label[label == 198] = IGNORE_INDEX
                tgt_label_reps.append(tgt_rep[label != IGNORE_INDEX])
                speech_token = self.cosyvoice.inference_label(ans, '英文女', stream=False)
                speech_tokens = []
                for i,j in enumerate(speech_token):
                    speech_tokens.append(j['tts_speech_token'].squeeze(0))
                speech_tokens.append(torch.tensor([0]))
                speech_tokens = torch.cat(speech_tokens, dim=0)
                if speech_tokens.size(0) > 1:
                    speech_tokens = speech_tokens[:-1]
                batch_speech_tokens.append(speech_tokens)
                embedding = self.cosyvoice.frontend.frontend_embedding('英文女')
                embeddings.append(embedding['llm_embedding'].squeeze(0))

            tgt_label_reps = torch.stack(tgt_label_reps)
            batch_speech_token = torch.stack(batch_speech_tokens)
            embeddings = torch.stack(embeddings)
            hidden_states = self.input_proj(tgt_label_reps)
            batch = {'text_feature': hidden_states, 'text_token_len': torch.tensor([hidden_states.size(1)]).repeat(hidden_states.size(0)),
                    'speech_token': batch_speech_token, 'speech_token_len': torch.tensor([batch_speech_token.size(1)]).repeat(hidden_states.size(0)),
                    'embedding': embeddings}
            output = self.llm.forward_ours(batch, 'cuda')
            loss = output['loss']
        return loss