|
| 1 | +""" |
| 2 | +Modified From https://github.com/XXXXRT666/GPT-SoVITS |
| 3 | +""" |
| 4 | + |
| 5 | +from __future__ import annotations |
| 6 | + |
| 7 | +import os |
| 8 | +from dataclasses import dataclass |
| 9 | +from typing import Callable, List, MutableSequence, Protocol, Type, cast |
| 10 | + |
| 11 | +import mlx.core as mx |
| 12 | +import torch |
| 13 | + |
| 14 | +from ..PyTorch.structs import T2SRequest, T2SResult |
| 15 | +from .sample_funcs_mlx import SampleProtocolMLX, sample_naive |
| 16 | + |
| 17 | +Tensor = torch.Tensor |
| 18 | +Array = mx.array |
| 19 | + |
| 20 | + |
| 21 | +@dataclass(slots=True) |
| 22 | +class T2SRequestMLX: |
| 23 | + x: List[Array] |
| 24 | + x_lens: Array |
| 25 | + prompts: Array |
| 26 | + bert_feature: List[Array] |
| 27 | + valid_length: int |
| 28 | + top_k: int = 5 |
| 29 | + top_p: float = 1 |
| 30 | + early_stop_num: int = -1 |
| 31 | + temperature: float = 1.0 |
| 32 | + repetition_penalty: float = 1.35 |
| 33 | + |
| 34 | + @classmethod |
| 35 | + def from_torch(cls, request: T2SRequest) -> T2SRequestMLX: |
| 36 | + x = list(map(lambda tensor: mx.array(tensor.cpu()), request.x)) |
| 37 | + x_lens = mx.array(request.x_lens.cpu()) |
| 38 | + prompts = mx.array(request.prompts.cpu()) |
| 39 | + bert_feature = list(map(lambda tensor: mx.array(tensor.cpu()), request.bert_feature)) |
| 40 | + |
| 41 | + return cls( |
| 42 | + x, |
| 43 | + x_lens, |
| 44 | + prompts, |
| 45 | + bert_feature, |
| 46 | + request.valid_length, |
| 47 | + request.top_k, |
| 48 | + request.top_p, |
| 49 | + request.early_stop_num, |
| 50 | + request.temperature, |
| 51 | + request.repetition_penalty, |
| 52 | + ) |
| 53 | + |
| 54 | + |
| 55 | +class KVCacheProtocol(Protocol): |
| 56 | + k_cache: Array |
| 57 | + v_cache: Array |
| 58 | + |
| 59 | + def empty(self) -> None: ... |
| 60 | + |
| 61 | + def update_cache(self, input_pos: Array, k_val: Array, v_val: Array, *args, **kwds) -> tuple[Array, Array]: ... |
| 62 | + |
| 63 | + def prefill_kv(self, k_val: Array, v_val: Array) -> None: ... |
| 64 | + |
| 65 | + def sync_cache(self, kv_cache: KVCacheProtocol) -> None: ... |
| 66 | + |
| 67 | + |
| 68 | +class T2SDecoderProtocol(Protocol): |
| 69 | + max_seq_length: int |
| 70 | + EOS: int |
| 71 | + n_head: int |
| 72 | + |
| 73 | + def embed(self, x: list[Array], y: Array, bert_features: list[Array]) -> Array: ... |
| 74 | + |
| 75 | + |
| 76 | +class T2SEngineProtocol(Protocol): |
| 77 | + def _handle_request(self, request: T2SRequest) -> tuple[list[Array], float]: ... |
| 78 | + |
| 79 | + def generate(self, request: T2SRequest) -> T2SResult: ... |
| 80 | + |
| 81 | + @staticmethod |
| 82 | + def load_decoder( |
| 83 | + weights_path: os.PathLike, max_batch_size: int = 1, implement: str = "MLX" |
| 84 | + ) -> T2SDecoderProtocol: ... |
| 85 | + |
| 86 | + |
| 87 | +class T2SSessionMLX: |
| 88 | + def __init__( |
| 89 | + self, |
| 90 | + decoder: T2SDecoderProtocol, |
| 91 | + request_torch: T2SRequest, |
| 92 | + sample_func: Type[SampleProtocolMLX] = sample_naive, |
| 93 | + device: mx.Device = mx.Device(mx.cpu), |
| 94 | + dtype: mx.Dtype = mx.float32, |
| 95 | + ): |
| 96 | + with mx.stream(device): |
| 97 | + request = T2SRequestMLX.from_torch(request_torch) |
| 98 | + |
| 99 | + self.decoder = decoder |
| 100 | + self.request = request |
| 101 | + self.device = device |
| 102 | + self.dtype = dtype |
| 103 | + |
| 104 | + bsz = len(request.x) |
| 105 | + y_len: int = cast(tuple[int, ...], request.prompts.shape)[-1] |
| 106 | + self.bsz = bsz |
| 107 | + self.y_len = y_len |
| 108 | + |
| 109 | + # Cache |
| 110 | + self.kv_cache: MutableSequence[KVCacheProtocol] |
| 111 | + self.sample = sample_func() |
| 112 | + |
| 113 | + # Forward args |
| 114 | + self.x = [i.astype(mx.int32) for i in request.x] |
| 115 | + self.x_lens = request.x_lens.astype(mx.int32) |
| 116 | + self.y = mx.zeros((bsz, decoder.max_seq_length)).astype(mx.int32) |
| 117 | + self.y[:, : cast(tuple[int, ...], request.prompts.shape)[-1]] = request.prompts.astype(mx.int32) |
| 118 | + self.bert_feature = [i.astype(dtype) for i in request.bert_feature] |
| 119 | + |
| 120 | + self.prefill_len = self.x_lens + cast(tuple[int, ...], request.prompts.shape)[1] |
| 121 | + |
| 122 | + self.input_pos = mx.zeros_like(self.prefill_len) |
| 123 | + self.input_pos += self.prefill_len |
| 124 | + |
| 125 | + # EOS |
| 126 | + self.completed = mx.array([False] * len(self.x)).astype(mx.bool_) |
| 127 | + self.y_results: List[Array] = [None] * len(self.x) # type: ignore |
| 128 | + |
| 129 | + self.xy_pos = decoder.embed(self.x, request.prompts, self.bert_feature) |
| 130 | + |
| 131 | + max_len = int(self.prefill_len.max(-1)) |
| 132 | + attn_mask = mx.zeros(shape=(bsz, max_len, max_len), dtype=mx.bool_) |
| 133 | + |
| 134 | + for bs in range(bsz): |
| 135 | + pos = int(self.x_lens[bs]) |
| 136 | + seq_len = pos + y_len |
| 137 | + |
| 138 | + attn_mask[bs, :seq_len, :pos] = True |
| 139 | + |
| 140 | + ar_mask = ~mx.triu( |
| 141 | + x=mx.ones( |
| 142 | + shape=( |
| 143 | + y_len, |
| 144 | + y_len, |
| 145 | + ), |
| 146 | + dtype=mx.bool_, |
| 147 | + ), |
| 148 | + k=1, |
| 149 | + ) |
| 150 | + attn_mask[bs, pos:seq_len, pos:seq_len] = ar_mask |
| 151 | + |
| 152 | + attn_mask = mx.repeat(mx.expand_dims(attn_mask, 1), decoder.n_head, 1) |
| 153 | + self.attn_mask = attn_mask |
| 154 | + |
| 155 | + mx.eval(self.attn_mask) |
0 commit comments