"""Full-width, depth-pruned text student; initialization is NOT an exact resume. Caller pins source checkpoint/files and supplies upstream import paths. No model downloads, teacher/decoder loading, device movement, or training are performed. The standard TextStudent27 forward/infer and state-key conventions are retained. """ from collections import OrderedDict from copy import deepcopy import hashlib import json import torch from inflect_kokoro_student27 import TextStudent27 _CONFIG = dict(speaker_count=6, preset="native33_fullwidth_depth1+mlp", n_token=178, teacher_dim=512, max_dur=50, widths=[512, 8, 8, 384, 64, 1], variant="baseline") SOURCE_CONFIG = dict(speaker_count=6, preset="inflect27+mlp", n_token=178, teacher_dim=512, max_dur=50, widths=[512, 8, 8, 384, 64, 3], variant="baseline") EXPECTED_PARAMETERS = 11_914_548 SOURCE_PARAMETERS = 15_464_244 ACOUSTIC_PARAMETERS = 22_396_242 SOURCE_CHECKPOINT_SHA256 = "e3d106ce49338985d1bd23f6ceaa9468b2f861f5d6427a4d9024a4ee4449ecfd" _DROPPED_PREFIXES = tuple("text_encoder.cnn.%d." % i for i in (1, 2)) + tuple( "predictor.text_encoder.lstms.%d." % i for i in (2, 3, 4, 5)) def _require(value, message): if not value: raise ValueError(message) def default_config(): return deepcopy(_CONFIG) class Native33TextModel(TextStudent27): def __init__(self, speaker_count=6, preset="native33_fullwidth_depth1+mlp", n_token=178, teacher_dim=512, max_dur=50, widths=None, variant="baseline"): config = dict(speaker_count=speaker_count, preset=preset, n_token=n_token, teacher_dim=teacher_dim, max_dur=max_dur, widths=list(_CONFIG["widths"] if widths is None else widths), variant=variant) _require(config == _CONFIG, "Unqualified native33 architecture/config") super().__init__(**config) def build_model(config): _require(isinstance(config, dict) and config == _CONFIG, "Expected exact default_config(); unknown keys/configs are rejected") model = Native33TextModel.from_config(deepcopy(config)) _require(sum(p.numel() for p in model.parameters()) == EXPECTED_PARAMETERS, "Unexpected resident parameter count; check pinned upstream source") return model def parameter_report(model): count = sum(p.numel() for p in model.parameters()) return dict(text_parameters=count, acoustic_parameters=ACOUSTIC_PARAMETERS, core_total_parameters=count+ACOUSTIC_PARAMETERS, buffer_values=sum(b.numel() for b in model.buffers()), submodules={k: sum(p.numel() for p in module.parameters()) for k, module in model.named_children()}, caveat="Native length-dependent style banks/affine tables are additional; no quality claim") def _digest(state): """Canonical tensor-state hash, independent of torch.save container encoding.""" result = hashlib.sha256() for name, tensor in sorted(state.items()): tensor = tensor.detach().cpu().contiguous() header = json.dumps([name, str(tensor.dtype), list(tensor.shape)], separators=(",", ":")).encode() data = tensor.view(torch.uint8).numpy().tobytes() result.update(len(header).to_bytes(8, "big")); result.update(header) result.update(len(data).to_bytes(8, "big")); result.update(data) return result.hexdigest() def _normalize_source(raw, expected): _require(isinstance(raw, dict) and bool(raw), "Source model state required") result, aliases = OrderedDict(), {} for name, tensor in raw.items(): _require(isinstance(name, str) and torch.is_tensor(tensor), "Only tensor model state accepted") _require(tensor.device.type == "cpu", "Supply CPU source checkpoint; no GPU allocation here") _require(bool(torch.isfinite(tensor).all()), "Nonfinite source: " + name) destination = name if name.endswith((".weight_g", ".weight_v")): prefix, suffix = name.rsplit(".", 1) destination = prefix+".parametrizations.weight.original"+("0" if suffix == "weight_g" else "1") _require(destination in expected and destination not in raw, "Unknown/colliding legacy weight-norm alias: " + name) _require(prefix+".weight_g" in raw and prefix+".weight_v" in raw, "Incomplete legacy weight-norm pair: " + name) aliases[name] = destination _require(destination not in result, "Duplicate normalized state key") result[destination] = tensor _require(set(result) == set(expected), "Source keys differ from full original15M architecture") for name, tensor in result.items(): _require(tensor.shape == expected[name].shape and tensor.dtype == expected[name].dtype, "Source shape/dtype mismatch: " + name) return result, aliases def initialize_from_checkpoint(model, state): """Copy every retained tensor exactly; caller MUST pin trusted source file SHA. state is the full loaded CPU checkpoint, not just its model mapping. Source optimizer/RNG/provenance remain untouched; parent creates a NEW optimizer/RNG contract for this deliberately changed architecture. No trained-weight reset or low-rank/weight-normalization arithmetic is used. """ _require(isinstance(model, Native33TextModel) and model.get_config() == _CONFIG, "Expected fresh Native33TextModel") _require(all(p.device.type == "cpu" for p in model.parameters()), "Initialize on CPU before .to(device)") _require(isinstance(state, dict) and state.get("model_config") == SOURCE_CONFIG, "Exact source15M model_config required") _require(isinstance(state.get("config"), dict) and state["config"].get("stage") == "text", "Source must be a text checkpoint") _require(type(state.get("step")) is int and state["step"] >= 0, "Explicit source step required") # Construct only a CPU schema donor. Its random initialization is unused and # caller RNG is restored; no teacher/decoder is constructed. with torch.random.fork_rng(devices=[]): donor = TextStudent27.from_config(deepcopy(SOURCE_CONFIG)) _require(sum(p.numel() for p in donor.parameters()) == SOURCE_PARAMETERS, "Unexpected source architecture count") source, aliases = _normalize_source(state.get("model"), donor.state_dict()) target = model.state_dict() dropped = sorted(set(source)-set(target)) _require(bool(dropped) and all(k.startswith(_DROPPED_PREFIXES) for k in dropped), "Only documented extra CNN/duration blocks may be removed") _require(set(target) <= set(source), "Target requires unknown/uninitialized tensors") transferred = OrderedDict() for name, expected in target.items(): tensor = source[name] _require(tensor.shape == expected.shape and tensor.dtype == expected.dtype, "Retained tensor mismatch: " + name) transferred[name] = tensor.detach().clone() model.load_state_dict(transferred, strict=True) _require(all(torch.equal(tensor, model.state_dict()[name]) for name, tensor in transferred.items()), "Transfer readback differs") receipt = dict(schema="native33-fullwidth-depth-prune-v1", exact_training_resume=False, functional_equivalence_claimed=False, fresh_optimizer_required=True, source_step=state["step"], source_model_config=deepcopy(SOURCE_CONFIG), target_model_config=default_config(), source_state_sha256=_digest(source), transferred_state_sha256=_digest(transferred), dropped_state_sha256=_digest( {k: source[k] for k in dropped}), retained_tensors=len(transferred), random_target_tensors=0, retained_values=sum(t.numel() for t in transferred.values()), dropped_values=sum(source[k].numel() for k in dropped), dropped_keys=dropped, source_key_aliases=aliases, retained_cnn_blocks=[0], retained_duration_blocks=[0], source_file_sha256_verification="REQUIRED from caller; not inferable from loaded dict", selected_source_file_sha256=SOURCE_CHECKPOINT_SHA256, source_optimizer_rng_mutated=False, budget=parameter_report(model), caveat="Depth changes hidden distributions; train/evaluate before promotion. ALBERT8 shared iterations unchanged.") return receipt