#!/usr/bin/env python3 """Test loading bf16 model with BnB 4-bit to CPU, then report size.""" import torch from transformers import AutoModelForCausalLM, BitsAndBytesConfig model_path = "/data/models/Ornith-1.0-35B" print(f"Loading model from: {model_path}") print(f"\nApplying BnB 4-bit quantization...") bnb_config = BitsAndBytesConfig( load_in_4bit=True, bnb_4bit_quant_type="nf4", bnb_4bit_compute_dtype=torch.float16, ) model = AutoModelForCausalLM.from_pretrained( model_path, quantization_config=bnb_config, device_map="cpu", torch_dtype=torch.float16, trust_remote_code=True, low_cpu_mem_usage=True, ) print("āœ“ Model loaded to CPU with BnB 4-bit") # Count parameters total_params = sum(p.numel() for p in model.parameters()) print(f"\nTotal parameters: {total_params / 1e9:.2f}B") # Check for quantized parameters bnb_params = 0 bf16_params = 0 for name, p in model.named_parameters(): if hasattr(p, 'quant_state') and p.quant_state is not None: bnb_params += p.numel() else: bf16_params += p.numel() print(f"BnB 4-bit parameters: {bnb_params / 1e9:.2f}B") print(f"BF16 parameters: {bf16_params / 1e9:.2f}B") print(f"Estimated size: {(bnb_params * 0.5 + bf16_params * 2) / 1e9:.2f} GB") if bnb_params / total_params > 0.9: print("\nāœ“ SUCCESS: Model is properly quantized to 4-bit!") else: print(f"\nāœ— FAILED: Only {bnb_params/total_params*100:.1f}% of parameters are 4-bit") print(" Expected: ~100%, Got: this percentage") del model import gc gc.collect() torch.cuda.empty_cache()