Inflect-Micro-v3 / models /micro /code /qualify_native27_native_generator_budget.py
owensong's picture
Stage verified private early-access package
a775b3f verified
Raw History Blame Contribute Delete
16 kB
"""CPU-only native-generator budget proposal. Never runs a model forward.
Default mode prints arithmetic, NOT qualification. cpu-shape strict-loads trusted,
SHA-pinned local assets and constructs an intentionally non-runnable container.
No downloads, checkpoint writes, hooks, training, or selected-model replacement.
"""
import argparse
import hashlib
import importlib
import json
from pathlib import Path
import sys
import time
VOICES = ('hope', 'sophia', 'cassidy', 'james', 'jasper', 'mark')
TAG = 'native27-native-generator-budget-proposal-v1'
PARADEE_PINS = {
'pytorch/text_side.pt': '0995bee746043e4f06f72be68fb8634530b95d6467b4a141759e721122648062',
'pytorch/decoder.pt': 'f231b9765ffdddd30df798530af0c557c06cb9cf029a475b8bfdca7b051b9d6c',
}
NATIVE_PINS = {
'config.json': '5abb01e2403b072bf03d04fde160443e209d7a0dad49a423be15196b9b43c17f',
'kokoro-v1_0.pth': '496dba118d1a58f5f3db2efc88dbdc216e0483fc89fe6e47ee1f2c53f18ad1e4',
}
SOURCE_PINS = {
'student.py': '92bac7593a2d95c427650e659536379a73a1f38775b8b155c56f1395d9e6c54b',
'student_decoder.py': '825acd09513ef6f1be3d59cc64863a64d96f5be4b670596e24488336c7f4f324',
}
KOKORO_PINS = {
'__init__.py': 'bc79f40bcd8ffcb313c87e516dd7f2c803422acd41daf260eb57edcf22f54387',
'model.py': 'c74c53eff48417cee24f8132b40ebb047f1238a96fa767199cba38b2997f1280',
'modules.py': '9a123d4650fdbe2b65142df7c690129abd7e70128499b1c12d6c9facbbb9b9d7',
'istftnet.py': '31a8e4e13f2a3a7c9a5535cd8e56695bf44a76cf5cb9a24aaf50248e8435b6fa',
'custom_stft.py': '7274793ffde780f0aba91d593b4a5332b8e94a5e9abeb7f9209be12362bbdecd',
}
CAP = 27_000_000
EXPECTED = dict(text=4_231_988, trunk=2_662_684, generator=19_667_510,
projection=66_048, generator_styles=768, generator_affines=110_592)
CAVEATS = [
'Static budget/shape proof only; NOT a trained or inference-qualified model.',
'The biased128-to512 projection is new/random. Hidden feature distributions require aligned feature distillation/retraining.',
'Six-row text32/trunk16 styles clone the single donor; native128 styles and affine banks start at zero. Six distinct voices are NOT learned.',
'No style routing or affine hooks are executed. Original text/trunk forwards are incompatible with replaced styles until explicitly implemented.',
'Native generator AdaIN biases apply AFTER each native fc(style128); never to student-trunk AdaIN(style16). Full-decoder layer indices must be remapped by exact native module name.',
'Minimal constant style lookups do NOT preserve native donor length-dependent styles. Native-trunk affine biases cannot transfer directly to the smaller trunk.',
'A frozen native generator does not guarantee quality with mismatched trunk features, F0/N, excitation, timing or styles.',
'No audio, quality, speed, peak-memory, or six-voice identity claim; runtime workspaces and unregistered weight-norm caches are not model parameters/buffers.',
]
def require(ok, message):
if not ok:
raise ValueError(message)
def proposal():
parameters = sum(EXPECTED.values())
return dict(schema=1, tag=TAG, status='dry_proposal_NOT_SOURCE_QUALIFIED',
expected_parameters=parameters, expected_registered_buffer_values=1024,
expected_parameter_plus_buffer_values=parameters+1024,
components=EXPECTED.copy(), cap_values=CAP,
within_cap=parameters+1024 <= CAP, voices=list(VOICES),
forward_calls=0, gpu_used=False, training_performed=False,
source_qualified=False, selected_model_changed=False,
automatic_promotion=False, caveats=CAVEATS.copy())
def sha(path):
with Path(path).open('rb') as stream:
return hashlib.file_digest(stream, 'sha256').hexdigest()
def pinned(path, digest, deadline):
deadline()
path = Path(path).absolute()
require(path.is_file() and not path.is_symlink(), 'Missing or symlinked input: '+str(path))
require(all(not p.is_symlink() and not (hasattr(p, 'is_junction') and p.is_junction())
for p in (path, *path.parents)), 'Symlink/junction input refused')
require(path.stat().st_size <= 400*1024**2, 'Input exceeds400MiB bound')
require(sha(path) == digest, 'Pinned input SHA mismatch: '+str(path))
deadline()
return path.resolve()
def tensor_state(torch, state):
require(isinstance(state, dict) and state, 'Nonempty tensor state required')
for key, value in state.items():
require(isinstance(key, str) and isinstance(value, torch.Tensor)
and value.device.type == 'cpu' and bool(torch.isfinite(value).all()),
'Invalid/nonfinite/non-CPU state tensor')
def native_generator_state(torch, state):
tensor_state(torch, state)
prefixed = [k.startswith('module.') for k in state]
require(all(prefixed) or not any(prefixed), 'Mixed native module prefixes')
clean = {k[7:] if all(prefixed) else k: v for k, v in state.items()}
subset = {k[len('generator.'):]: v for k, v in clean.items() if k.startswith('generator.')}
require(subset, 'Native generator state missing')
return clean, subset
def strip_absent_identity_norms(torch, generator, weights):
compatibility = []
for name, layer in generator.named_modules():
if not isinstance(layer, torch.nn.InstanceNorm1d) or not layer.affine:
continue
prefix = name+'.' if name else ''
absent = [prefix+k not in weights for k in ('weight', 'bias')]
require(all(absent) or not any(absent), 'Partial native normalization pair')
if all(absent):
require(torch.equal(layer.weight, torch.ones_like(layer.weight))
and torch.equal(layer.bias, torch.zeros_like(layer.bias)),
'Uncheckpointed normalization must be identity')
layer.register_parameter('weight', None)
layer.register_parameter('bias', None)
layer.affine = False
compatibility.append(name)
return compatibility
def affine_registry(torch, generator, native_decoder_state):
full_names = sorted(k[:-len('.fc.weight')] for k, value in native_decoder_state.items()
if k.endswith('.fc.weight') and value.ndim == 2 and value.shape[1] == 128)
registry = []
for name, layer in sorted(generator.named_modules()):
if type(layer).__name__ != 'AdaIN1d':
continue
require(isinstance(layer.fc, torch.nn.Linear) and layer.fc.in_features == 128,
'Native generator AdaIN must consume128D styles')
full_name = 'generator.'+name
require(full_name in full_names, 'Missing full native affine registry name')
require(native_decoder_state[full_name+'.fc.weight'].shape == layer.fc.weight.shape
and native_decoder_state[full_name+'.fc.bias'].shape == layer.fc.bias.shape,
'Native affine state shape mismatch')
registry.append(dict(native_module=full_name, bank_key='layer_%03d'%len(registry),
original_full_decoder_key='layer_%03d'%full_names.index(full_name),
width=layer.fc.out_features, style_dim=128,
bank_shape=[6, layer.fc.out_features],
routing='fc(style[voice]) + bank[voice]; BEFORE AdaIN split into gamma/beta'))
require(len(registry) == 48 and sum(r['width'] for r in registry) == 18432,
'Native generator affine registry differs')
return registry
def inventory(module):
parameters, buffers = list(module.parameters()), list(module.buffers())
require(all(x.device.type == 'cpu' for x in parameters+buffers), 'CPU tensors only')
persistent = {id(x) for m in module.modules() for n, x in m._buffers.items()
if x is not None and n not in m._non_persistent_buffers_set}
return dict(parameters=sum(x.numel() for x in parameters),
registered_buffer_values=sum(x.numel() for x in buffers),
persistent_buffer_values=sum(x.numel() for x in buffers if id(x) in persistent),
parameter_bytes=sum(x.numel()*x.element_size() for x in parameters),
registered_buffer_bytes=sum(x.numel()*x.element_size() for x in buffers),
state_dict_elements=sum(x.numel() for x in module.state_dict().values()),
counting='Unique parameter/buffer objects; shared ALBERT layers counted once; includes weight-norm originals; excludes optimizer/critic/workspace')
def assemble(torch, text, decoder, native_generator, registry):
require(text.style.shape == (1, 32) and decoder.style.shape == (1, 16), 'Donor style dimensions differ')
require(decoder.decode[-1].conv2.out_channels == decoder.generator.ups[0].in_channels == 128,
'Paradee trunk must output128 channels')
require(native_generator.ups[0].in_channels == 512, 'Native generator must consume512 channels')
class BudgetProposal(torch.nn.Module):
def forward(self, *args, **kwargs):
raise RuntimeError('Proposal only: voice routing/feature KD and forward qualification not implemented')
result = BudgetProposal()
for donor in (text, decoder):
style = donor.style.detach().clone()
del donor.style
donor.speaker_embedding = torch.nn.Embedding(6, style.shape[1])
with torch.no_grad():
donor.speaker_embedding.weight.copy_(style.expand(6, -1))
del decoder.generator
result.text, result.trunk, result.generator = text, decoder, native_generator
result.projection = torch.nn.Conv1d(128, 512, kernel_size=1, bias=True)
result.generator_styles = torch.nn.Embedding(6, 128)
torch.nn.init.zeros_(result.generator_styles.weight)
result.generator_affines = torch.nn.ParameterDict({r['bank_key']: torch.nn.Parameter(
torch.zeros(6, r['width'])) for r in registry})
result.eval().requires_grad_(False)
return result
def load_types(paradee_source, kokoro_source):
expected = {'student': paradee_source/'student.py', 'student_decoder': paradee_source/'student_decoder.py',
'kokoro': kokoro_source/'kokoro/__init__.py',
**{'kokoro.'+name[:-3]: kokoro_source/'kokoro'/name
for name in KOKORO_PINS if name != '__init__.py'}}
for name, path in expected.items():
if name in sys.modules:
require(Path(sys.modules[name].__file__).resolve() == path.resolve(),
'Conflicting already-imported model module: '+name)
old_path = sys.path.copy()
try:
sys.path[:0] = [str(paradee_source), str(kokoro_source)]
modules = {name: importlib.import_module(name) for name in expected}
for name, path in expected.items():
require(Path(modules[name].__file__).resolve() == path.resolve(), 'Wrong imported source: '+name)
finally:
sys.path[:] = old_path
return modules['student'].TextStudent, modules['student_decoder'].StudentDecoder, modules['kokoro.istftnet'].Generator
def qualify(args):
import torch
started = time.monotonic()
require(not torch.cuda.is_initialized() and str(torch.get_default_device()) == 'cpu',
'Fresh CPU process/default device required')
torch.set_num_threads(2)
def deadline():
require(time.monotonic()-started < args.max_seconds, 'CPU diagnostic deadline exceeded')
roots = [(args.paradee_source, SOURCE_PINS), (args.kokoro_source/'kokoro', KOKORO_PINS),
(args.paradee_weights, PARADEE_PINS), (args.native_assets, NATIVE_PINS)]
pins = {str(pinned(root/name, digest, deadline)): digest for root, entries in roots for name, digest in entries.items()}
types = load_types(args.paradee_source.resolve(), args.kokoro_source.resolve())
deadline()
def load(path):
state = torch.load(path, map_location='cpu', weights_only=True)
deadline()
return state
config = json.loads((args.native_assets/'config.json').read_text(encoding='utf-8'))
require(config['style_dim'] == 128 and config['istftnet']['upsample_initial_channel'] == 512,
'Native style/channel configuration differs')
text, trunk = types[0]('s+mlp'), types[1]('A')
for module, file in ((text, 'text_side.pt'), (trunk, 'decoder.pt')):
checkpoint = load(args.paradee_weights/'pytorch'/file)
require(set(checkpoint) == {'model'}, 'Unexpected released checkpoint keys')
tensor_state(torch, checkpoint['model'])
module.load_state_dict(checkpoint['model'], strict=True)
native = load(args.native_assets/'kokoro-v1_0.pth')
clean, weights = native_generator_state(torch, native['decoder'])
generator = types[2](style_dim=128, **config['istftnet'])
compatibility = strip_absent_identity_norms(torch, generator, weights)
generator.load_state_dict(weights, strict=True)
registry = affine_registry(torch, generator, clean)
rng = torch.get_rng_state()
try:
model = assemble(torch, text, trunk, generator, registry)
finally:
torch.set_rng_state(rng)
counts = inventory(model)
components = {name: inventory(child)['parameters'] for name, child in model.named_children()}
require(components == EXPECTED, 'Actual component counts differ from proposal')
require(counts['parameters'] == sum(EXPECTED.values()) and counts['registered_buffer_values'] == 1024,
'Actual complete inventory differs')
require(counts['parameters']+counts['registered_buffer_values'] <= CAP, 'Deployment value cap exceeded')
for path, digest in pins.items():
require(sha(path) == digest, 'Source/weight changed during diagnostic')
deadline()
require(not torch.cuda.is_initialized(), 'Unexpected CUDA initialization')
report = proposal()
report.update(status='CPU_STATIC_SHAPES_AND_BUDGET_PASSED_NOT_FORWARD_QUALIFIED',
source_qualified=True, actual=counts, actual_components=components,
input_sha256=pins, helper_sha256=sha(__file__),
native_identity_norm_compatibility=compatibility, affine_registry=registry,
routing=dict(voices=list(VOICES), text_style_shape=[6, 32], trunk_style_shape=[6, 16],
native_style_shape=[6, 128], projection_weight_shape=list(model.projection.weight.shape),
original_native_trunk_affines='EXCLUDED; different channel/style interface',
initialized_voice_identity=False, hooks_installed=False),
seconds=time.monotonic()-started,
runtime=dict(python=sys.version.split()[0], torch=torch.__version__))
return report
def main(argv=None):
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument('--mode', choices=('proposal', 'cpu-shape'), default='proposal')
for name in ('paradee-source', 'kokoro-source', 'paradee-weights', 'native-assets'):
parser.add_argument('--'+name, type=Path)
parser.add_argument('--max-seconds', type=int, default=120)
args = parser.parse_args(argv)
if not 1 <= args.max_seconds <= 180:
parser.error('max-seconds must be1..180; externally bound native calls with timeout --kill-after')
if args.mode == 'proposal':
report = proposal()
else:
if any(getattr(args, name) is None for name in ('paradee_source','kokoro_source','paradee_weights','native_assets')):
parser.error('cpu-shape requires all four explicit local asset/source directories')
import os
for name in ('OMP_NUM_THREADS', 'MKL_NUM_THREADS', 'OPENBLAS_NUM_THREADS'):
os.environ[name] = '2'
os.environ.update(HF_HUB_OFFLINE='1', TRANSFORMERS_OFFLINE='1')
sys.dont_write_bytecode = True
report = qualify(args)
print(json.dumps(report, indent=2, allow_nan=False))
return report
if __name__ == '__main__':
main()