Download models/micro/code/native26_text_model.py from owensong/Inflect-Micro-v3: direct link, hf CLI and curl.
- Browser
- Download file 5.76 kB
-
https://huggingface.co/owensong/Inflect-Micro-v3/resolve/main/models/micro/code/native26_text_model.py
- Command line
-
hf download hf://owensong/Inflect-Micro-v3/models/micro/code/native26_text_model.py
-
curl -L -o native26_text_model.py https://huggingface.co/owensong/Inflect-Micro-v3/resolve/main/models/micro/code/native26_text_model.py
5.76 kB
| """Six-voice Mini factory initialized identically from the compact1000 pilot. | |
| Caller verifies the full source checkpoint file SHA before deserialization and | |
| supplies the existing pinned upstream import paths. No training/jobs/downloads. | |
| The new full-six training contract needs a NEW optimizer, not the two-voice | |
| pilot update function that restores inactive embedding rows after every update. | |
| """ | |
| from copy import deepcopy | |
| import hashlib | |
| import json | |
| import torch | |
| from inflect_kokoro_student27 import TextStudent27 | |
| _CONFIG = dict(speaker_count=6, preset="s+mlp", n_token=178, teacher_dim=512, | |
| max_dur=50, widths=[256, 6, 4, 192, 32, 3], variant="baseline") | |
| EXPECTED_PARAMETERS = 4_231_988 | |
| ACOUSTIC_PARAMETERS = 22_396_242 | |
| SOURCE_SCHEMA = "compact-text-full-utterance-v1" | |
| SOURCE_STEP = 1000 | |
| def _require(value, message): | |
| if not value: | |
| raise ValueError(message) | |
| def default_config(): | |
| return deepcopy(_CONFIG) | |
| class Native26TextModel(TextStudent27): | |
| def __init__(self, speaker_count=6, preset="s+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 native26 architecture/config") | |
| super().__init__(**config) | |
| def build_model(config): | |
| _require(isinstance(config, dict) and config == _CONFIG, "Exact native26 default_config() required") | |
| model = Native26TextModel.from_config(deepcopy(config)) | |
| _require(sum(p.numel() for p in model.parameters()) == EXPECTED_PARAMETERS, | |
| "Unexpected resident parameter count; inspect source pins") | |
| return model | |
| def _digest(state): | |
| h = 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() | |
| h.update(len(header).to_bytes(8, "big")); h.update(header) | |
| h.update(len(data).to_bytes(8, "big")); h.update(data) | |
| return h.hexdigest() | |
| def initialize_from_checkpoint(model, state): | |
| """Load every tensor identically, preserving all source states; no row reset. | |
| state is the full trusted CPU checkpoint. File-SHA validation is the caller's | |
| responsibility, since a loaded dict does not establish its container SHA. | |
| No source optimizer/RNG is loaded or changed: full-six sampling is a NEW run. | |
| """ | |
| _require(isinstance(model, Native26TextModel) and model.get_config() == _CONFIG, | |
| "Expected Native26TextModel with exact config") | |
| _require(all(p.device.type == "cpu" and p.requires_grad for p in model.parameters()), | |
| "Initialize a CPU model with all parameters trainable") | |
| _require(isinstance(state, dict) and state.get("schema") == SOURCE_SCHEMA | |
| and state.get("stage") == "text", "Wrong compact checkpoint schema/stage") | |
| _require(type(state.get("step")) is int and state["step"] == SOURCE_STEP, "Expected compact1000 checkpoint") | |
| _require(state.get("model_config") == _CONFIG, "Source compact model_config mismatch") | |
| contract = state.get("contract") | |
| _require(isinstance(contract, dict) and contract.get("active_speakers") == [0, 3] | |
| and contract.get("model_config") == _CONFIG, "Expected original Hope0/James3 training contract") | |
| _require(isinstance(contract.get("sources"), dict) and bool(contract["sources"]), | |
| "Source provenance required") | |
| source, expected = state.get("model"), model.state_dict() | |
| _require(isinstance(source, dict) and set(source) == set(expected), "Strict source state keys mismatch") | |
| for name, target in expected.items(): | |
| tensor = source[name] | |
| _require(torch.is_tensor(tensor) and tensor.device.type == "cpu", "CPU source tensor required: " + name) | |
| _require(tensor.shape == target.shape and tensor.dtype == target.dtype, | |
| "Source tensor shape/dtype mismatch: " + name) | |
| _require(bool(torch.isfinite(tensor).all()), "Nonfinite source tensor: " + name) | |
| before = _digest(source) | |
| model.load_state_dict(source, strict=True) | |
| _require(all(torch.equal(tensor, model.state_dict()[name]) for name, tensor in source.items()), | |
| "Strict identity readback failed") | |
| _require(_digest(source) == before, "Source state mutated") | |
| return dict(schema="native26-compact1000-fullsix-init-v1", source_step=SOURCE_STEP, | |
| source_schema=SOURCE_SCHEMA, source_model_config=default_config(), | |
| target_model_config=default_config(), source_contract=deepcopy(contract), | |
| source_state_sha256=before, target_state_sha256=_digest(model.state_dict()), | |
| exact_model_weight_identity=True, exact_training_resume=False, | |
| fresh_optimizer_required=True, source_optimizer_rng_mutated=False, | |
| source_trained_speaker_ids=[0, 3], previously_untrained_speaker_ids=[1, 2, 4, 5], | |
| new_training_speaker_ids=list(range(6)), random_or_replicated_target_rows=0, | |
| parameter_count=EXPECTED_PARAMETERS, core_total_parameters=EXPECTED_PARAMETERS+ACOUSTIC_PARAMETERS, | |
| buffer_values=sum(t.numel() for t in model.buffers()), | |
| source_file_sha256_verification="REQUIRED from caller; loaded dict does not prove file SHA", | |
| caveat="Six rows exist, but only source speakers0/3 were trained. No six-voice quality claim; voice assets additional.") | |