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 
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
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
import functools
import math
import random
import uuid
from array import array

import numpy as np
import torch
import torch.nn as nn
from typing import List, Optional, Union, Iterable, Tuple, Mapping, Dict

from torch import Tensor
from transformers import PretrainedConfig, GPT2Config
from vllm.attention import AttentionMetadata
from vllm.config import CacheConfig, MultiModalConfig
from vllm.distributed import get_pp_group
from vllm.inputs import InputContext, INPUT_REGISTRY, DecoderOnlyInputs, token_inputs
from vllm.model_executor.layers.logits_processor import LogitsProcessor
from vllm.model_executor.layers.quantization import QuantizationConfig
from vllm.model_executor.layers.sampler import Sampler, SamplerOutput
from vllm.model_executor.layers.vocab_parallel_embedding import VocabParallelEmbedding, ParallelLMHead
from vllm.model_executor.model_loader.weight_utils import default_weight_loader
from vllm.model_executor.models.gpt2 import GPT2Block
from vllm.model_executor.models.utils import make_layers, make_empty_intermediate_tensors_factory
from vllm.model_executor.sampling_metadata import SamplingMetadata
from vllm.multimodal import MULTIMODAL_REGISTRY, MultiModalInputs
from vllm.sequence import IntermediateTensors, SequenceData, VLLM_TOKEN_ID_ARRAY_TYPE
from vllm.model_executor.models.interfaces import SupportsMultiModal, SupportsPP



class LearnedPositionEmbeddings(nn.Module):
    def __init__(self, seq_len, model_dim, init=0.02, relative=False, supports_pp=False):
        super().__init__()
        # nn.Embedding
        self.emb = VocabParallelEmbedding(seq_len, model_dim) if supports_pp else nn.Embedding(seq_len, model_dim)
        # Initializing this way is standard for GPT-2
        self.emb.weight.data.normal_(mean=0.0, std=init)
        self.relative = relative
        self.seq_len = seq_len

    def forward(self, x):
        sl = x.shape[1]
        if self.relative:
            start = random.randint(sl, self.seq_len) - sl
            return self.emb(torch.arange(start, start + sl, device=x.device))
        else:
            return self.emb(torch.arange(0, sl, device=x.device))

    def get_fixed_embedding(self, ind: torch.Tensor, dev: torch.device) -> torch.Tensor:
        """Get position embeddings with batch support.

        Handles both single and batched inputs, returning embeddings that can be
        directly added to input embeddings of the same shape.

        Args:
            ind: Position indices tensor. Can be single or batched
                 Shape: [..., seq_len] or [seq_len]
            dev: Target device for the embeddings

        Returns:
            Position embeddings tensor matching input shape plus embedding dimension
            Shape: [batch_size, seq_len, model_dim] or [1, 1, model_dim]

        Example:
            >>> pos_emb = LearnedPositionEmbeddings(100, 64)
            >>> # Batched input
            >>> batch_indices = torch.zeros((3, 5))  # batch_size=3, seq_len=5
            >>> embeddings = pos_emb.get_fixed_embedding(batch_indices, 'cuda')
            >>> embeddings.shape  # Returns: [3, 5, 64]
        """
        if ind.shape[0] > 1:
            pos_embeddings = []
            for index in ind:
                # Create embeddings for each position in the sequence
                pos_embeddings.append(self.emb(index))

            # Shape: [1, seq_len, model_dim] -> [batch_size, seq_len, model_dim]
            return torch.stack(pos_embeddings, dim=0)
        else:
            # Handle single input
            # Shape: [1, 1, model_dim]
            return self.emb(torch.tensor([ind], device=dev)).unsqueeze(0)


def get_xtts_max_audio_tokens(ctx: InputContext) -> int:
    """Calculate maximum audio tokens based on text context and audio duration."""
    # Based on GPT config and XTTSv2 settings
    return 608


def dummy_seq_data_for_xtts(
        ctx: InputContext,
        seq_len: int,
        audio_count: int,
) -> SequenceData:
    """Create dummy sequence data for XTTS profiling."""
    # Calculate audio token space needed
    max_audio_token_conditioning = ctx.model_config.hf_config.max_prompt_tokens # in xtts prompt = voice conditioning
    audio_placeholder = array(
        VLLM_TOKEN_ID_ARRAY_TYPE,
        [1]
    ) * max_audio_token_conditioning

    # Add separator between chunks
    audio_token_ids = (audio_placeholder + array(VLLM_TOKEN_ID_ARRAY_TYPE, [1])) * audio_count

    # Fill remaining sequence with padding
    other_token_ids = array(VLLM_TOKEN_ID_ARRAY_TYPE, [1]) * (seq_len - len(audio_token_ids))
    # not -1 since we add the start audio token

    return SequenceData(
        audio_token_ids +
        other_token_ids
    )

def dummy_conditioning_for_xtts(
        ctx: InputContext,
        seq_len: int,
        audio_count: int,
) -> dict:
    """Create dummy conditioning data for XTTS."""
    return {
        "audio": {
            "embeds":[
            torch.zeros(
                (seq_len, ctx.model_config.hf_config.hidden_size),
                dtype=ctx.model_config.dtype) for _ in range(audio_count)
        ],
            "is_logits_only_mode": False,
        }
    }


def dummy_data_for_xtts(
        ctx: InputContext,
        seq_len: int,
        mm_counts: Mapping[str, int],
) -> Tuple[SequenceData, dict]:
    """Create complete dummy data for XTTS profiling."""
    audio_count = mm_counts["audio"]
    seq_data = dummy_seq_data_for_xtts(ctx, seq_len, audio_count)
    cond_data = dummy_conditioning_for_xtts(ctx, seq_len, audio_count)
    return seq_data, cond_data


def input_mapper_for_xtts(ctx: InputContext, data: Union[Dict, List[Tensor]]) -> MultiModalInputs:
    """Map input data to XTTS format."""

    assert isinstance(data, dict), "XTTS MultiModal input data must be a dictionary with keys: 'embeds', 'is_logits_only_mode'"

    embeds = data.get("embeds")
    is_logits_only_mode = data.get("is_logits_only_mode", False)

    # Each item should be a torch tensor
    for audio_input in embeds:
        if not isinstance(audio_input, Tensor):
            raise NotImplementedError(f"Unsupported data type: {type(audio_input)}")

    return MultiModalInputs({"cond_latents": embeds,
                             "is_logits_only_mode": is_logits_only_mode,
                             })


def input_processor_for_xtts2_gpt(ctx: InputContext, inputs: DecoderOnlyInputs):
    """
    We'll accomodate for the extra contditioning token and for the start audio token,
    we actually insert a -1 repeated for the differecne in length between the conditioning and the tokenized text
    and then we add 1 for the start audio token
    Args:
        ctx:
        inputs:

    Returns:

    """
    multi_modal_data = inputs.get("multi_modal_data")
    audio_dict = multi_modal_data['audio']
    audio = audio_dict.get('embeds')

    is_last_decoding_pass = audio_dict.get("is_logits_only_mode", False)

    prompt_token_ids = inputs.get("prompt_token_ids")

    if not is_last_decoding_pass:
        # we fill everything with 0 since we don't actually needs text token ids, it would mess up in the sampling step
        new_token_ids = [1] * (audio.shape[0] + 1) # +1 for the start audio generation token
    else:
        new_token_ids = ([1] * audio.shape[0]) + prompt_token_ids
    # the encoding had already been done externally to reuse the embeddings for later use but we
    # account for the new token that will be added before generation
    new_prompt = None
    return token_inputs(prompt_token_ids=new_token_ids,
                 prompt=new_prompt,
                 multi_modal_data=multi_modal_data)


@MULTIMODAL_REGISTRY.register_input_mapper("audio", input_mapper_for_xtts)
@MULTIMODAL_REGISTRY.register_max_multimodal_tokens("audio", get_xtts_max_audio_tokens)
@INPUT_REGISTRY.register_dummy_data(dummy_data_for_xtts)
@INPUT_REGISTRY.register_input_processor(input_processor_for_xtts2_gpt)
class XttsGPT(nn.Module, SupportsMultiModal, SupportsPP):
    def __init__(
            self,
            config: PretrainedConfig,
            multimodal_config: MultiModalConfig,
            cache_config: Optional[CacheConfig] = None,
            quant_config: Optional[QuantizationConfig] = None,
    ):
        super().__init__()
        self.config = config
        self.quant_config = quant_config

        # Core GPT components
        self.gpt = GPT2Model(
            config,
            cache_config,
            quant_config,
            prefix="gpt"
        )
        self.final_norm =  nn.LayerNorm(config.hidden_size, bias=True, eps=config.layer_norm_epsilon)
        # Output head for mel tokens
        self.mel_head = ParallelLMHead(
            config.num_audio_tokens,
            config.hidden_size,
            bias=True,
            quant_config=quant_config,
            prefix="mel_head"
        )
        self.audio_start_generation_token = config.start_audio_token

        # Initialize logits processor and sampler
        logit_scale = getattr(config, "logit_scale", 1.0)
        self.logits_processor = LogitsProcessor(config.num_audio_tokens,
                                                config.num_audio_tokens,
                                                logit_scale)
        self.sampler = Sampler()

    @staticmethod
    def check_is_logits_only_mode(is_logits_only_mode):

        # First check if it's a boolean
        if isinstance(is_logits_only_mode, bool):
            return is_logits_only_mode

        # Then check if it's a tensor
        if torch.is_tensor(is_logits_only_mode):
            # if it's a scalar tensor, return the value
            if is_logits_only_mode.numel() == 1:
                return bool(is_logits_only_mode.item())
            # for non-scalar tensors, check if all elements are the same
            return is_logits_only_mode.any()

        # Fallback
        return bool(is_logits_only_mode)

    def _calculate_start_token_indices(self, cond_latents: List[torch.Tensor]) -> List[int]:
        """Calcola gli indici dove inserire i token di start.

        Args:
            cond_latents: Lista di tensori di condizionamento

        Returns:
            Lista di indici dove inserire i token di start
        """
        indices = []
        current_idx = 0

        for cond_latent in cond_latents:
            # Aggiungi la lunghezza del segmento corrente
            current_idx += cond_latent.shape[0]
            # Aggiungi l'indice per il token di start dopo questo segmento
            indices.append(current_idx)
            # Incrementa per il token di start che verrà aggiunto
            current_idx += 1

        return indices

    # noinspection PyMethodOverriding
    def forward(
            self,
            input_ids: torch.Tensor,
            positions: torch.Tensor,
            kv_caches: List[torch.Tensor],
            attn_metadata: AttentionMetadata,
            intermediate_tensors: Optional["IntermediateTensors"] = None,
            cond_latents: Optional[torch.Tensor] = None,
            is_logits_only_mode: bool = False,
            **kwargs,
    ) -> Union[torch.Tensor, "IntermediateTensors"]:
        """Forward pass following VLLM pattern."""
        # it is not the first iter either if the cond latents are emtpy or if the kv_caches are not empty
        is_first_iteration = (input_ids==1).all()

        #assert len(input_ids) == 1 or (cond_latents is not None and not is_first_iteration), "Conditioning data (voice conditioning+text_embeddings) is required for XTTS"

        is_logits_only_mode = self.check_is_logits_only_mode(is_logits_only_mode)

        if is_first_iteration:
            # we add it to enable the model to start the generation
            input_ids[-1] = self.audio_start_generation_token

        hidden_states = self.gpt(
            input_ids=input_ids,
            position_ids=positions,
            kv_caches=kv_caches,
            attn_metadata=attn_metadata,
            intermediate_tensors=intermediate_tensors,
            # this is the conditioning input ( voice conditioning + text_embeds )
            input_embeds=cond_latents,
            is_first_iteration=is_first_iteration,
            is_logits_only_mode=is_logits_only_mode
        )

        return hidden_states

    def compute_logits(
            self,
            hidden_states: torch.Tensor,
            sampling_metadata: SamplingMetadata,
    ) -> Optional[torch.Tensor]:

        # normalize the hidden states
        hidden_states = self.final_norm(hidden_states)

        # Check if we need to collect hidden states
        sampling_params = sampling_metadata.seq_groups[0].sampling_params
        if hasattr(sampling_params, 'hidden_state_collector'):
            # Call the collector directly with the hidden states
            sampling_params.hidden_state_collector(hidden_states, None)  # The request_id is already bound

        # Compute logits using the mel_head
        logits = self.logits_processor(self.mel_head, hidden_states, sampling_metadata)
        return logits

    def sample(
            self,
            logits: torch.Tensor,
            sampling_metadata: SamplingMetadata,
    ) -> Optional[SamplerOutput]:
        next_tokens = self.sampler(logits, sampling_metadata)
        return next_tokens

    def load_weights(self, weights: Iterable[Tuple[str, torch.Tensor]]):
        """Load weights following VLLM pattern."""
        params_dict = dict(self.named_parameters(remove_duplicate=False))
        loaded_names = set()
        for name, loaded_weight in weights:
            if name not in params_dict:
                #print(f"Skipping loading of {name} bc it is not found") # used to check if all weights were loaded
                continue

            param = params_dict[name]
            if "c_attn" in name or "c_proj" in name or "c_fc" in name:
                if name.endswith(".weight"):
                    loaded_weight = loaded_weight.t()

            weight_loader = getattr(param, "weight_loader", default_weight_loader)
            weight_loader(param, loaded_weight)
            loaded_names.add(name)
        # used to check if all weights were loaded
        assert set(params_dict.keys()) - loaded_names == set(), \
            (f"Missing weights: {set(params_dict.keys()) - loaded_names}, "
             f"this probably means you are using an incompatible model ")

class GPT2Model(nn.Module):

    def __init__(
            self,
            config: GPT2Config,
            cache_config: Optional[CacheConfig] = None,
            quant_config: Optional[QuantizationConfig] = None,
            prefix: str = "",
    ):
        super().__init__()
        self.config = config
        assert not config.add_cross_attention
        assert not config.scale_attn_by_inverse_layer_idx
        assert not config.reorder_and_upcast_attn
        self.embed_dim = config.hidden_size
        self.wte = VocabParallelEmbedding(config.num_audio_tokens, self.embed_dim)
        self.wpe = (
            LearnedPositionEmbeddings(config.max_audio_tokens + 3, config.decoder_input_dim)
            if config.max_audio_tokens != -1
            else functools.partial(config.null_position_embeddings, dim=config.decoder_input_dim)
        )
        self.start_layer, self.end_layer, self.h = make_layers(
            config.num_hidden_layers,
            lambda prefix: GPT2Block(
                config, cache_config, quant_config, prefix=prefix),
            prefix=f"{prefix}.h")
        self.ln_f = nn.LayerNorm(self.embed_dim, eps=config.layer_norm_epsilon)
        self.make_empty_intermediate_tensors = (
            make_empty_intermediate_tensors_factory(["hidden_states"],
                                                    config.hidden_size))


    def forward(
            self,
            input_ids: torch.Tensor,
            position_ids: torch.Tensor,
            kv_caches: List[torch.Tensor],
            attn_metadata: AttentionMetadata,
            intermediate_tensors: Optional[IntermediateTensors],
            # we pass this so that we can concatenate the text and conditioning input
            input_embeds: Optional[torch.Tensor] = None,
            is_first_iteration: bool = False,
            is_logits_only_mode: bool = False,
    ) -> Union[torch.Tensor, IntermediateTensors]:

        if get_pp_group().is_first_rank:
            # if we are not doing the final conversion from token to latent and it is first pass(prefill)
            if is_first_iteration and not is_logits_only_mode:
                input_ids = input_ids[-1].reshape(1, 1)
            elif is_logits_only_mode:
                # we remove the contidioning input and keep just the audio token
                if isinstance(input_embeds, list):
                    starting_idx = []
                    for input_embed in input_embeds:
                        starting_idx.append(input_embed.shape[0])
                    ending_ids = attn_metadata.seq_lens  # list

                    # First sequence: from starting_idx[0] to ending_ids[0]
                    cumulative_starts = [starting_idx[0]]  # First starts at its own index
                    cumulative_ends = [ending_ids[0]]  # First ends at its ending_id

                    # For subsequent sequences:
                    # Start = previous_end + current_start
                    # End = previous_end + current_end
                    for i in range(1, len(starting_idx)):
                        next_start = cumulative_ends[i - 1] + starting_idx[i]
                        next_end = cumulative_ends[i - 1] + ending_ids[i]
                        cumulative_starts.append(next_start)
                        cumulative_ends.append(next_end)

                    ids_for_unpacking = [end-start for start, end in zip(cumulative_starts, cumulative_ends)]

                    input_ids = torch.cat([
                        input_ids[start:end].reshape(1, -1)
                        for start, end in zip(cumulative_starts, cumulative_ends)
                    ], dim=-1)
                    position_ids = torch.cat([
                        position_ids[start:end].reshape(1, -1)
                        for start, end in zip(cumulative_starts, cumulative_ends)
                    ], dim= -1).squeeze(0)
                else:
                    input_ids = input_ids[input_embeds.shape[1]:].reshape(1, -1)
                    position_ids = position_ids[input_embeds.shape[1]:]#.reshape(1, -1)
            else:
                input_ids = input_ids

            audio_inputs_embeds = self.wte(input_ids).squeeze(0)

            # weird but they to it like this in the xtts2 model
            position_embeds = self.wpe.get_fixed_embedding(
                    position_ids, input_ids.device
            ) if not is_first_iteration \
                    else self.wpe(audio_inputs_embeds.reshape(-1, 1)) # we need to reshape to 2D tensor or useless?

            hidden_states = audio_inputs_embeds + position_embeds

            if isinstance(input_embeds, list) and is_logits_only_mode:
                hidden_states = list(hidden_states.split(ids_for_unpacking, dim=0))

            if is_first_iteration or is_logits_only_mode:
                # We concat the text and audio conditioning input in the sequence dimension
                if isinstance(input_embeds, list):
                    input_embeds = [input_embed.view(-1, input_embed.shape[-1]) for input_embed in input_embeds]
                else:
                    input_embeds = input_embeds.view(-1, input_embeds.shape[-1]) # we ensure we have a 2D tensor

                if not isinstance(input_embeds, list) and input_embeds.shape[0] == attn_metadata.num_prefill_tokens:
                    # this is during profiling, wee need to remove the last token
                    # the attn_metadata.num_prefill_tokens(prompt len) should be == to input_embeds.shape[0] - 1
                    # to account for the start audio gen embedding that will be cat to the text embeddings
                    input_embeds = input_embeds[:-1]

            if is_first_iteration or is_logits_only_mode:
                # we concatenate the conditioning input to the text conditioning input
                if isinstance(input_embeds, list):
                        hidden_states = torch.cat([
                                tensor for pair in zip(input_embeds, [hidden_states] * len(input_embeds)
                                                    if not isinstance(hidden_states, list) else hidden_states)
                                for tensor in pair
                            ], dim=0)
                else:
                    hidden_states = torch.cat([input_embeds, hidden_states], dim=0)

            #flatten the hidden state
            hidden_states = hidden_states.view(-1, self.embed_dim)
        else:
            assert intermediate_tensors is not None
            hidden_states = intermediate_tensors["hidden_states"]

        for i in range(self.start_layer, self.end_layer):
            layer = self.h[i]
            hidden_states = layer(hidden_states,
                                  kv_caches[i - self.start_layer],
                                  attn_metadata)

        if not get_pp_group().is_last_rank:
            return IntermediateTensors({"hidden_states": hidden_states})

        hidden_states = self.ln_f(hidden_states)
        return hidden_states