From d769d398651a2d3b8fcfa8fa9526cf6fddb0c982 Mon Sep 17 00:00:00 2001 From: Christian Medina <37550954+cmedinasoriano@users.noreply.github.com> Date: Thu, 2 Jul 2026 22:39:25 -0400 Subject: [PATCH] feat: add single shard test script --- test_quantize_single_shard.py | 62 +++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 test_quantize_single_shard.py diff --git a/test_quantize_single_shard.py b/test_quantize_single_shard.py new file mode 100644 index 0000000..c235595 --- /dev/null +++ b/test_quantize_single_shard.py @@ -0,0 +1,62 @@ +#!/usr/bin/env python3 +"""Test quantization on single shard only.""" + +import gc +import torch +from pathlib import Path +from safetensors.torch import load_file, save_file +from bitsandbytes.functional import quantize_nf4 +from transformers import AutoConfig + + +def test_single_shard(model_path, output_dir): + output_dir = Path(output_dir) + output_dir.mkdir(parents=True, exist_ok=True) + + config = AutoConfig.from_pretrained(model_path, trust_remote_code=True) + + import glob + shards = sorted(glob.glob(f"{model_path}/*.safetensors")) + + if len(shards) == 0: + print("No shards found!") + return + + # Use only first shard + shard_file = shards[0] + print(f"Testing with single shard: {shard_file}") + + state_dict = load_file(shard_file, device="cuda:0") + + weight_keys = [ + k for k, v in state_dict.items() + if "weight" in k and isinstance(v, torch.Tensor) and v.dim() == 2 + ] + + print(f"Found {len(weight_keys)} weight tensors\n") + + quantized = 0 + failed = 0 + + for key in weight_keys[:5]: # Test first 5 layers + try: + weight = state_dict[key] + qweight, qstate = quantize_nf4(weight, blocksize=64, compress_statistics=True) + state_dict[key] = qweight + quantized += 1 + print(f"✓ {key}") + except Exception as e: + failed += 1 + print(f"✗ {key}: {e}") + + print(f"\nResults: {quantized} quantized, {failed} failed") + + # Save test output + test_output = output_dir / "test_shard.safetensors" + state_dict_cpu = {k: v.cpu() if isinstance(v, torch.Tensor) else v for k, v in state_dict.items()} + save_file(state_dict_cpu, test_output) + print(f"Saved test output to: {test_output}") + + +if __name__ == "__main__": + test_single_shard("/data/models/Ornith-1.0-35B", "/data/models/test_quantize")