Download runtime/verify_export.py from Reza2kn/drama-3-nvfp4: direct link, hf CLI and curl.
- Browser
- Download file 4.28 kB
-
https://huggingface.co/Reza2kn/drama-3-nvfp4/resolve/main/runtime/verify_export.py
- Command line
-
hf download hf://Reza2kn/drama-3-nvfp4/runtime/verify_export.py
-
curl -L -o verify_export.py https://huggingface.co/Reza2kn/drama-3-nvfp4/resolve/main/runtime/verify_export.py
4.28 kB
| """Verify coverage, scale validity, and unchanged dense weights of the export.""" | |
| import hashlib | |
| import json | |
| import socket | |
| from contextlib import ExitStack | |
| from pathlib import Path | |
| import torch | |
| from safetensors import safe_open | |
| ROOT = Path('/home/rezo/drama3') | |
| def main(): | |
| assert socket.gethostname().lower() in {'redza', 'stallion'} | |
| torch.set_num_threads(2) | |
| source = ROOT / 'source' | |
| destination = ROOT / 'artifacts/drama-3-nvfp4' | |
| source_map = json.loads((source / 'model.safetensors.index.json').read_text())['weight_map'] | |
| index = json.loads((destination / 'model.safetensors.index.json').read_text()) | |
| output_map = index['weight_map'] | |
| tensor_bytes = 0 | |
| expert_projections = 0 | |
| dense_tensors = 0 | |
| with ExitStack() as stack: | |
| output_shards = {name: stack.enter_context(safe_open(str(destination / name), framework='pt')) | |
| for name in sorted(set(output_map.values()))} | |
| source_shards = {name: stack.enter_context(safe_open(str(source / name), framework='pt')) | |
| for name in sorted(set(source_map.values()))} | |
| actual_keys = {name for shard in output_shards.values() for name in shard.keys()} | |
| assert actual_keys == set(output_map) | |
| expected = set() | |
| for name, filename in source_map.items(): | |
| original = source_shards[filename].get_tensor(name) | |
| if '.mlp.experts.' not in name: | |
| expected.add(name) | |
| actual = output_shards[output_map[name]].get_tensor(name) | |
| assert actual.dtype == original.dtype and torch.equal(actual, original), name | |
| dense_tensors += 1 | |
| elif name.endswith('.weight'): | |
| prefix = name.removesuffix('.weight') | |
| expected.update(prefix + suffix for suffix in ['.weight', '.weight_scale', '.weight_scale_2', '.input_scale']) | |
| packed = output_shards[output_map[name]].get_tensor(name) | |
| assert packed.dtype == torch.uint8 | |
| assert packed.shape == (original.shape[0], original.shape[1] // 2), name | |
| scales = output_shards[output_map[prefix + '.weight_scale']].get_tensor(prefix + '.weight_scale') | |
| assert scales.dtype == torch.float8_e4m3fn | |
| assert scales.shape == (original.shape[0], original.shape[1] // 16), name | |
| assert torch.isfinite(scales.float()).all() and (scales.float() >= 0).all(), name | |
| global_scale = output_shards[output_map[prefix + '.weight_scale_2']].get_tensor(prefix + '.weight_scale_2') | |
| assert torch.isfinite(global_scale).all() and (global_scale > 0).all(), name | |
| if '.gate_proj' in prefix: | |
| up_name = prefix.replace('.gate_proj', '.up_proj') + '.weight_scale_2' | |
| assert torch.equal(global_scale, output_shards[output_map[up_name]].get_tensor(up_name)) | |
| expert_projections += 1 | |
| assert expected == actual_keys, (len(expected - actual_keys), len(actual_keys - expected)) | |
| for name, filename in output_map.items(): | |
| tensor = output_shards[filename].get_tensor(name) | |
| tensor_bytes += tensor.numel() * tensor.element_size() | |
| assert expert_projections == 40 * 256 * 3 | |
| index['metadata']['total_size'] = tensor_bytes | |
| (destination / 'model.safetensors.index.json').write_text(json.dumps(index, indent=2)) | |
| canary = destination / 'canary.safetensors' | |
| if canary.exists(): | |
| canary.rename(ROOT / 'artifacts/expert-canary.safetensors') | |
| files = [] | |
| for path in sorted(destination.iterdir()): | |
| if path.is_file(): | |
| files.append({'name': path.name, 'bytes': path.stat().st_size}) | |
| result = {'coverage_passed': True, 'expert_projections': expert_projections, | |
| 'unchanged_dense_tensors': dense_tensors, 'tensor_payload_bytes': tensor_bytes, | |
| 'package_bytes': sum(row['bytes'] for row in files), 'files': files, | |
| 'audio_validated': False, 'uploaded': False} | |
| (ROOT / 'reports/export-verification.json').write_text(json.dumps(result, indent=2)) | |
| print(json.dumps({k: v for k, v in result.items() if k != 'files'}, indent=2), flush=True) | |
| if __name__ == '__main__': | |
| main() | |