File size: 3,916 Bytes
a775b3f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Eval-only single-pass inference for the frozen native33 text architecture.

No weight/config changes. Train with the original forward. install_fast_inference
patches only one instance's infer method and is explicitly reversible. Parity is
with eval-mode TextStudent27.infer; this module makes no measured speed claim.
"""
from types import MethodType

import torch
from torch.nn.utils.rnn import pack_padded_sequence, pad_packed_sequence

from inflect_kokoro_student27 import _speaker_ids, duration_alignment
from native33_text_model import Native33TextModel, default_config


def _validate_model(model):
    if not isinstance(model, Native33TextModel) or model.get_config() != default_config():
        raise ValueError("Expected the frozen native33 architecture/config")
    if any(module.training for module in model.modules()):
        raise ValueError("Fast inference is eval-only; call model.eval() first")


@torch.no_grad()
def infer_once(model, ids, lengths, speaker_ids=None, speed=1.0):
    """Same output dictionary/clocks/masks as original infer; each frontend once."""
    _validate_model(model)
    if ids.ndim != 2 or ids.dtype != torch.long or ids.shape[0] < 1:
        raise ValueError("ids must be a nonempty LongTensor [B,T]")
    batch, tokens = ids.shape
    lengths = lengths.to(ids.device)
    if lengths.shape != (batch,) or lengths.dtype != torch.long:
        raise ValueError("lengths must be a LongTensor [B]")
    if ((lengths < 1) | (lengths > tokens)).any():
        raise ValueError("lengths must be within [1,T]")
    mask = torch.arange(tokens, device=ids.device)[None] >= lengths[:, None]
    speakers = _speaker_ids(speaker_ids, batch, ids.device, model.config["speaker_count"])
    style = model.speaker_embedding(speakers)
    bert = model.bert(ids, attention_mask=(~mask).int())
    encoded = model.bert_encoder(bert).transpose(-1, -2)
    duration_features = model.predictor.text_encoder(encoded, style, lengths, mask)
    packed = pack_padded_sequence(duration_features, lengths.cpu(), batch_first=True,
                                  enforce_sorted=False)
    duration_hidden, _ = model.predictor.lstm(packed)
    duration_hidden, _ = pad_packed_sequence(duration_hidden, batch_first=True, total_length=tokens)
    durations = torch.sigmoid(model.predictor.duration_proj(duration_hidden)).sum(-1).masked_fill(mask, 0)
    alignment, frame_lengths = duration_alignment(durations, lengths, speed)
    token_hidden = model.text_encoder(ids, lengths, mask)
    asr_tokens = model.asr_proj(token_hidden.transpose(1, 2)).transpose(1, 2)
    asr_tokens = asr_tokens.masked_fill(mask[:, None], 0)
    f0, noise = model.predictor.F0Ntrain(duration_features.transpose(-1, -2) @ alignment, style)
    return dict(dur=durations, F0=f0, N=noise, asr_tok=asr_tokens,
                asr=asr_tokens @ alignment, aln=alignment, frame_lengths=frame_lengths)


def install_fast_inference(model):
    """Opt-in instance-only patch; never modifies forward or any class globally."""
    _validate_model(model)
    if getattr(model, "_native33_fast_infer_installed", False):
        if getattr(model.infer, "__func__", None) is not infer_once:
            raise ValueError("Fast-inference marker/method mismatch")
        return model
    if "infer" in model.__dict__:
        raise ValueError("Refusing to overwrite an existing instance inference override")
    model.infer = MethodType(infer_once, model)
    model._native33_fast_infer_installed = True
    return model


def restore_inference(model):
    """Remove only our own patch, restoring normal class-method lookup."""
    if not getattr(model, "_native33_fast_infer_installed", False):
        return model
    if getattr(model.infer, "__func__", None) is not infer_once:
        raise ValueError("Refusing to remove another inference override")
    del model.infer
    del model._native33_fast_infer_installed
    return model