Download projects/sparse-world-dynamics/check_gpu_dequant.py from KineJIng/kinejing-open-source: direct link, hf CLI and curl.
- Browser
- Download file 3.35 kB
-
https://huggingface.co/datasets/KineJIng/kinejing-open-source/resolve/main/projects/sparse-world-dynamics/check_gpu_dequant.py
- Command line
-
hf download hf://datasets/KineJIng/kinejing-open-source/projects/sparse-world-dynamics/check_gpu_dequant.py
-
curl -L -o check_gpu_dequant.py https://huggingface.co/datasets/KineJIng/kinejing-open-source/resolve/main/projects/sparse-world-dynamics/check_gpu_dequant.py
3.35 kB
| """Verify translated packed GPU decoders against upstream NumPy definitions.""" | |
| import argparse | |
| import json | |
| from pathlib import Path | |
| import time | |
| import numpy as np | |
| import gguf | |
| from gguf.quants import dequantize | |
| import torch | |
| from torch_kquants import decode_k | |
| from gguf_stream import GGUFStore, StreamedLinear | |
| def main(): | |
| p = argparse.ArgumentParser() | |
| p.add_argument('--out', type=Path, required=True) | |
| p.add_argument('--model', type=Path) | |
| a = p.parse_args() | |
| a.out.mkdir(parents=True, exist_ok=False) | |
| rng = np.random.default_rng(991) | |
| records = [] | |
| for name, size, half_positions in [('Q2_K', 84, [80, 82]), ('Q3_K', 110, [108]), | |
| ('Q5_K', 176, [0, 2]), ('Q6_K', 210, [208])]: | |
| raw = rng.integers(0, 256, (257, size), dtype=np.uint8) | |
| for pos in half_positions: | |
| raw[:, pos:pos+2] = rng.uniform(.0001, .003, (257, 1)).astype(np.float16).view(np.uint8) | |
| ref = dequantize(raw, getattr(gguf.GGMLQuantizationType, name)).reshape(257, 256) | |
| for device in ['cpu'] + (['cuda'] if torch.cuda.is_available() else []): | |
| actual = decode_k(torch.tensor(raw, device=device), name).cpu().numpy() | |
| np.testing.assert_array_equal(actual, ref) | |
| records.append(dict(name=name, device=device, blocks=len(raw), max_abs_error=float(np.max(abs(actual-ref))))) | |
| real = [] | |
| if a.model: | |
| cpu_store, gpu_store = GGUFStore(a.model), GGUFStore(a.model, gpu_dequant=True) | |
| available = a.model.stat().st_size | |
| for name in ['blk.0.ffn_gate.weight', 'blk.0.ffn_down.weight', 'blk.0.attn_v.weight']: | |
| count = 3 | |
| expected = cpu_store.rows(name, 0, count) | |
| actual = gpu_store.rows_tensor(name, 0, count, 'cuda', torch.float32) | |
| np.testing.assert_array_equal(actual.cpu().numpy(), expected) | |
| real.append(dict(name=name, max_abs_error=0., rows=count)) | |
| x = torch.randn(1, 8, 8192, device='cuda', requires_grad=True) | |
| ref_x = x.detach().clone().requires_grad_(True) | |
| # One complete actual small K-projection, not the whole model. | |
| left = StreamedLinear(gpu_store, 'blk.0.attn_k.weight', 128)(x) | |
| right = StreamedLinear(cpu_store, 'blk.0.attn_k.weight', 128)(ref_x) | |
| probe = torch.randn_like(left) | |
| (left*probe).sum().backward() | |
| (right*probe).sum().backward() | |
| torch.testing.assert_close(left, right, atol=1e-6, rtol=1e-6) | |
| torch.testing.assert_close(x.grad, ref_x.grad, atol=1e-6, rtol=1e-6) | |
| real.append(dict(name='blk.0.attn_k.weight', output_max_error=float((left-right).detach().abs().max()), | |
| input_gradient_max_error=float((x.grad-ref_x.grad).abs().max()))) | |
| report = dict(status='PACKED_GPU_DECODERS_MATCH_UPSTREAM', synthetic=records, actual_prefix=real, | |
| limitations=['Exact match is against upstream Python decoder, not a separate C implementation.', | |
| 'Actual checkpoint prefix may still await complete-file SHA verification.', | |
| 'Decoder correctness does not establish world-model capability.']) | |
| (a.out/'result.json').write_text(json.dumps(report, indent=2), encoding='utf-8') | |
| print(json.dumps(report, indent=2), flush=True) | |
| if __name__ == '__main__': | |
| main() | |