File size: 955 Bytes
43c7cf2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32

# Custom loading function for quantized model
import torch
from transformers import AutoModelForCausalLM, AutoConfig

def load_quantized_model(model_name_or_path, device="auto"):
    """
    Load quantized model with proper handling of int8 parameters
    """
    print("Loading quantized model...")
    
    # Load with no gradients
    with torch.no_grad():
        model = AutoModelForCausalLM.from_pretrained(
            model_name_or_path,
            torch_dtype=torch.float32,  # This will be mixed automatically
            device_map=device,
            trust_remote_code=True,
            low_cpu_mem_usage=True
        )
    
    # Ensure int8 parameters don't require gradients
    for name, param in model.named_parameters():
        if param.dtype == torch.int8:
            param.requires_grad = False
    
    print("Model loaded successfully!")
    return model

# Usage:
# model = load_quantized_model("your-username/your-model-name")