File size: 15,816 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
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
"""Standalone phoneme-input Mini deployment. No teacher, caches, or checkpoints.

Load only an exported weights.pt and manifest.json. Put exported code/ on the
Python path; dependencies are ordinary installed Torch/Transformers packages.
CPU-canonical excitation/source STFT matches the qualified rendering recipe.
No pitch shift, output gain, filtering, trimming, or cached acoustic inputs.
"""
from contextlib import contextmanager
from copy import deepcopy
import hashlib
import importlib
import json
import math
from pathlib import Path
import threading

import torch
from torch import nn

SCHEMA = "inflect-native-mini-phonemes-v1"
VOICES = ("hope", "sophia", "cassidy", "james", "jasper", "mark")
DONORS = ("af_heart", "af_heart", "af_sarah", "am_michael", "am_michael", "am_michael")
CHOICES_SHA = "305366a448082aec037ed5267089e2a7c3461638aa2ef1e59168e0a31b9ae0f0"
GENERATOR_CONFIG = dict(resblock_kernel_sizes=[3,7,11], upsample_rates=[10,6],
    upsample_initial_channel=512, resblock_dilation_sizes=[[1,3,5]]*3,
    upsample_kernel_sizes=[20,12], gen_istft_n_fft=20, gen_istft_hop_size=5)
_RNG_LOCK = threading.RLock()


def require(ok, message):
    if not ok: raise ValueError(message)


def sha(path):
    h = hashlib.sha256()
    with Path(path).open("rb") as stream:
        for chunk in iter(lambda: stream.read(1024**2), b""): h.update(chunk)
    return h.hexdigest()


def tensor_state(state):
    require(isinstance(state, dict) and bool(state), "Nonempty tensor state required")
    for name, value in state.items():
        require(isinstance(name,str) and torch.is_tensor(value) and value.device.type=="cpu"
                and bool(torch.isfinite(value).all()), "Invalid finite CPU state: "+str(name))


def strict_load(module, state):
    expected = module.state_dict()
    require(set(state)==set(expected), "Runtime tensor keys differ")
    for name,tensor in state.items():
        require(tensor.shape==expected[name].shape and tensor.dtype==expected[name].dtype,
                "Runtime tensor shape/dtype differs: "+name)
    module.load_state_dict(state,strict=True)
    require(all(torch.equal(tensor,module.state_dict()[name]) for name,tensor in state.items()),
            "Runtime tensor readback differs")


def build_bridge(max_frames=1200):
    from student_decoder import StudentDecoder
    from native27_nativegen_bridge import NativeGeneratorBridge
    # Random donor weights are unused: every retained tensor is strict-loaded.
    donor = StudentDecoder("A", dim_in=512, style_dim=16)
    return NativeGeneratorBridge(donor, None, active_speaker_ids=tuple(range(6)), max_frames=max_frames)


def build_generator(weights, config):
    from kokoro.istftnet import Generator
    require(config == GENERATOR_CONFIG, "Unqualified native generator configuration")
    tensor_state(weights)
    generator = Generator(style_dim=128, disable_complex=False, **config)
    for name, layer in generator.named_modules():
        if isinstance(layer, nn.InstanceNorm1d) and layer.affine:
            missing = [name+"."+key not in weights for key in ("weight","bias")]
            require(all(missing) or not any(missing), "Partial identity-normalization state")
            if all(missing):
                require(torch.equal(layer.weight,torch.ones_like(layer.weight))
                        and torch.equal(layer.bias,torch.zeros_like(layer.bias)), "Nonidentity omitted normalization")
                layer.register_parameter("weight",None); layer.register_parameter("bias",None)
                layer.affine = False
    strict_load(generator,weights)
    require(sum(p.numel() for p in generator.parameters())==19_667_510, "Native generator parameter count differs")
    return generator


def canonical_source(generator):
    """Same CPU source/STFT arithmetic, without importing job/teacher helpers."""
    source, upsample = generator.m_source, generator.f0_upsamp
    cpu_source, cpu_upsample = deepcopy(source).cpu(), deepcopy(upsample).cpu()
    cpu_source.forward = type(source).forward.__get__(cpu_source)
    cpu_upsample.forward = type(upsample).forward.__get__(cpu_upsample)
    def source_forward(x):
        return tuple(t.to(x.device) if torch.is_tensor(t) else t for t in cpu_source(x.cpu()))
    def upsample_forward(x): return cpu_upsample(x.cpu()).to(x.device)
    source.forward, upsample.forward = source_forward, upsample_forward
    stft, transform = generator.stft, type(generator.stft).transform
    def canonical_transform(x):
        mag, phase = transform(stft,x.cpu())
        return mag.to(x.device),phase.to(x.device)
    stft.transform = canonical_transform


def validate_styles(styles, generator):
    require(isinstance(styles,dict) and set(styles)=={"banks","offsets","strengths","affines"}, "Style schema differs")
    banks = styles["banks"]
    require(isinstance(banks,dict) and set(banks)==set(DONORS), "Exactly three donor acoustic banks required")
    for bank in banks.values():
        require(torch.is_tensor(bank) and bank.shape==(510,128) and bank.dtype==torch.float32
                and bank.device.type=="cpu" and bool(torch.isfinite(bank).all()), "Bad acoustic bank")
    offsets = styles["offsets"]
    require(torch.is_tensor(offsets) and offsets.shape==(6,128) and offsets.dtype==torch.float32
            and offsets.device.type=="cpu" and bool(torch.isfinite(offsets).all()), "Bad six-row acoustic offsets")
    strengths = styles["strengths"]
    require(isinstance(strengths,list) and len(strengths)==6 and all(type(x) in (float,int)
            and math.isfinite(x) and 0<=x<=1 for x in strengths), "Bad selected strengths")
    registry = {name: m.fc for name,m in generator.named_modules() if type(m).__name__=="AdaIN1d"}
    require(len(registry)==48 and sum(m.out_features for m in registry.values())==18432, "Native G affine registry differs")
    recipes = styles["affines"]
    require(isinstance(recipes,dict) and set(recipes)==set(VOICES), "Six exact generator recipes required")
    for sid,voice in enumerate(VOICES):
        recipe = recipes[voice]; needed = voice in ("sophia","cassidy")
        require(isinstance(recipe,dict) and set(recipe)=={"required","strength","biases_by_module","checkpoint_sha256"}
                and type(recipe["required"]) is bool and recipe["required"]==needed
                and recipe["strength"]==(strengths[sid] if needed else 0.), "Selected affine recipe mismatch")
        require(isinstance(recipe["checkpoint_sha256"],str) and len(recipe["checkpoint_sha256"])==64, "Adapter SHA required")
        biases = recipe["biases_by_module"]
        require(isinstance(biases,dict) and set(biases)==(set(registry) if needed else set()), "Missing/extra G-local bias names")
        for name,bias in biases.items():
            require(torch.is_tensor(bias) and bias.dtype==torch.float32 and bias.device.type=="cpu"
                    and bias.shape==(registry[name].out_features,) and bool(torch.isfinite(bias).all()), "Bad G affine bias")


class NativeMini(nn.Module):
    def __init__(self, payload, *, device="cpu", allow_cuda=False, fast=False):
        super().__init__()
        require(str(torch.get_default_device())=="cpu", "CPU-default construction required before explicit device move")
        require(set(payload)=={"schema","text_kind","text_config","text_state","bridge_state","generator_config",
                              "generator_state","styles","vocab","max_frames","sample_rate","choices_sha256"}, "Export payload fields differ")
        require(payload["schema"]==SCHEMA and payload["sample_rate"]==24000
                and payload["choices_sha256"]==CHOICES_SHA, "Deployment schema/rate/recipe mismatch")
        device = torch.device(device)
        require(device.type=="cpu" or (device.type=="cuda" and allow_cuda), "CUDA requires explicit allow_cuda")
        require(type(payload["max_frames"]) is int and 2<=payload["max_frames"]<=1200, "Frame bound2..1200 required")
        self.max_frames = payload["max_frames"]
        self.text_kind = payload["text_kind"]
        require(self.text_kind in ("native26","native33"), "Unknown text factory")
        factory = importlib.import_module(self.text_kind+"_text_model")
        require(payload["text_config"]==factory.default_config(), "Text factory config differs")
        tensor_state(payload["text_state"]); tensor_state(payload["bridge_state"])
        self.text = factory.build_model(payload["text_config"])
        strict_load(self.text,payload["text_state"])
        self.bridge = build_bridge(self.max_frames)
        strict_load(self.bridge,payload["bridge_state"])
        require(sum(p.numel() for p in self.bridge.parameters())==2_728_732, "Bridge parameter count differs")
        self.generator = build_generator(payload["generator_state"],payload["generator_config"])
        validate_styles(payload["styles"],self.generator)
        self.styles = deepcopy(payload["styles"])
        self.vocab = deepcopy(payload["vocab"])
        require(isinstance(self.vocab,dict) and self.vocab and all(isinstance(k,str) and len(k)==1
                and type(v) is int and 0<v<178 for k,v in self.vocab.items()), "Native178-ID vocabulary required")
        self.to(device).eval().requires_grad_(False)
        canonical_source(self.generator)
        if fast:
            require(self.text_kind=="native33", "Fast helper currently qualifies native33 only")
            importlib.import_module("native33_text_fast").install_fast_inference(self.text)
        self._inference_lock = threading.RLock()

    def train(self, mode=True):
        require(mode is False, "Deployment is eval-only; train separate factory models")
        return super().train(False)

    @contextmanager
    def _affines(self, voice):
        recipe = self.styles["affines"][voice]
        modules = dict(self.generator.named_modules()); handles=[]; previous=[]
        try:
            for name,bias in recipe["biases_by_module"].items():
                fc = modules[name].fc; previous.append((fc,dict(fc._forward_hooks)))
                def inject(module, args, output, bias=bias, strength=recipe["strength"]):
                    require(output.ndim==2 and output.shape[1]==bias.numel(), "G affine output shape changed")
                    return output+strength*bias.to(device=output.device,dtype=output.dtype)[None]
                handles.append(fc.register_forward_hook(inject))
            yield
        finally:
            for handle in handles: handle.remove()
            require(all(dict(fc._forward_hooks)==old for fc,old in previous), "Generator affine hooks leaked")

    def phoneme_ids(self, phonemes):
        require(isinstance(phonemes,str) and 1<=len(phonemes)<=510, "Phoneme length1..510 required")
        require(all(c in self.vocab for c in phonemes), "Unknown phoneme; no silent dropping")
        return [0,*[self.vocab[c] for c in phonemes],0]

    def synthesize_phonemes(self, phonemes, voice="hope", *, speed=1., seed=None):
        return self.synthesize_ids(self.phoneme_ids(phonemes),voice,speed=speed,seed=seed)

    @torch.no_grad()
    def synthesize_ids(self, ids, voice="hope", *, speed=1., seed=None):
        sid = VOICES.index(voice) if isinstance(voice,str) and voice in VOICES else voice
        require(type(sid) is int and 0<=sid<6, "Unknown voice")
        require(isinstance(ids,(tuple,list)) and 3<=len(ids)<=512 and ids[0]==ids[-1]==0
                and all(type(x) is int for x in ids)
                and all(0<x<178 for x in ids[1:-1]), "Exact boundary-wrapped phoneme IDs required")
        require(type(speed) in (int,float) and math.isfinite(speed) and speed>0, "Finite positive speed required")
        require(seed is None or type(seed) is int and 0<=seed<2**32, "Optional seed must be uint32")
        require(not any(m.training for m in self.modules()), "All deployment modules must remain eval")
        device = next(self.text.parameters()).device
        tokens = torch.tensor([ids],device=device,dtype=torch.long)
        lengths = torch.tensor([len(ids)],device=device)
        speakers = torch.tensor([sid],device=device)
        with self._inference_lock, _RNG_LOCK:
            # Guard BEFORE allocating alignment/decoder workspaces. The optional
            # fast path still uses this safety preflight, just like qualification.
            dur = self.text(tokens,lengths,speaker_ids=speakers)[0]
            require(dur.shape==tokens.shape and bool(torch.isfinite(dur).all()), "Invalid predicted duration")
            scaled = dur/speed
            require(bool(torch.isfinite(scaled).all()) and bool((scaled.abs()<1e7).all()), "Duration overflow")
            frames = int(scaled.round().clamp(min=1).sum())
            require(2<=frames<=self.max_frames, "Predicted duration exceeds cap; no cropping/fallback")
            predicted = self.text.infer(tokens,lengths,speakers,speed=speed)
            require(torch.equal(predicted["dur"],dur) and int(predicted["frame_lengths"][0])==frames,
                    "Predicted inference clock differs from preflight")
            asr,f0,noise = predicted["asr"],predicted["F0"],predicted["N"]
            require(asr.shape==(1,512,frames) and f0.shape==noise.shape==(1,2*frames)
                    and all(bool(torch.isfinite(x).all()) for x in (asr,f0,noise)), "Predicted feature clocks/nonfinite")
            style = (self.styles["banks"][DONORS[sid]][len(ids)-3].to(device)
                     +self.styles["offsets"][sid].to(device)*self.styles["strengths"][sid])[None]
            hidden = self.bridge.forward_hidden(asr,f0,noise,speakers)
            devices = [device.index if device.index is not None else torch.cuda.current_device()] if device.type=="cuda" else []
            @contextmanager
            def seeded():
                if seed is None:
                    yield
                else:
                    with torch.random.fork_rng(devices=devices):
                        torch.random.default_generator.manual_seed(seed)
                        if devices: torch.cuda.default_generators[devices[0]].manual_seed(seed)
                        yield
            with seeded(), self._affines(VOICES[sid]):
                audio = self.generator(hidden,style,f0).reshape(-1)
            require(audio.shape==(600*frames,) and bool(torch.isfinite(audio).all()), "Native audio clock/nonfinite")
            require(bool(audio.abs().max()<1), "Clipping rejected; no normalization/filter fallback")
            return dict(audio=audio.detach().cpu(),sample_rate=24000,voice=VOICES[sid],frames=frames,
                        metadata=dict(input="phoneme_ids",raw_text_g2p=False,teacher_acoustic_inputs=False,
                                      style_index=len(ids)-3,pitch_shift_applied=0,postprocessing="none"))


def load_export(directory, *, device="cpu", allow_cuda=False, fast=False):
    directory = Path(directory).resolve()
    manifest = json.loads((directory/"manifest.json").read_text(encoding="utf-8"))
    require(manifest.get("schema")==SCHEMA and isinstance(manifest.get("files"),dict), "Export manifest differs")
    require("weights.pt" in manifest["files"], "Weights receipt missing")
    for relative,digest in manifest["files"].items():
        path = directory/relative
        require(not Path(relative).is_absolute() and ".." not in Path(relative).parts
                and path.is_file() and not path.is_symlink() and path.resolve().is_relative_to(directory)
                and sha(path)==digest, "Export hash/path mismatch: "+relative)
    # Serialized object graph is plain tensors/basic metadata, never nn.Modules.
    payload = torch.load(directory/"weights.pt",map_location="cpu",weights_only=True)
    with torch.random.fork_rng(devices=[]):
        model = NativeMini(payload,device=device,allow_cuda=allow_cuda,fast=fast)
    return model