"""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 00, "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