EADST

Pytorch Q4_1 Quantize and Dequantize aligning with llama.cpp

Pytorch Q4_1 Quantize and Dequantize aligning with llama.cpp

import torch

# Check if CUDA is available
use_cuda = torch.cuda.is_available()
device = torch.device("cuda" if use_cuda else "cpu")

def q4_1_quantize_and_dequantize_tensor(tensor):
    tensor = tensor.to(dtype=torch.float32, device=device)

    # Reshape tensor to process each 4-value block independently
    orig_shape = tensor.shape
    tensor = tensor.view(-1, 32)

    # Find the min and max values per block
    min_vals = torch.min(tensor, dim=1)[0]
    max_vals = torch.max(tensor, dim=1)[0]

    # Calculate scale d for each block
    d = (max_vals - min_vals) / (2**4 - 1)
    d[d == 0] = 1.0  # Prevent division by zero

    # Calculate inverse of d
    ids = 1.0 / d

    # Quantize tensor elements
    quantized_tensors = (tensor - min_vals[:, None]) * ids[:, None]

    # Clamp values to be between 0 and 15 (for 4 bits)
    quantized_tensors = torch.clamp(quantized_tensors + 0.5, 0, 15).to(torch.uint8)

    # Dequantize the tensor
    dequantized_tensors = (quantized_tensors.float() * d[:, None]) + min_vals[:, None]

    # Reshape back to the original shape
    dequantized_tensors = dequantized_tensors.view(orig_shape).to(dtype=torch.float16)

    return dequantized_tensors

# Assuming 'model_part' is already loaded and on CPU
model_part = torch.load(f"your_model_path/pytorch_model.bin", map_location="cpu")
keywords = [
    "embed_tokens.weight",
    "self_attn.q_proj.weight",
    "self_attn.k_proj.weight",
    "self_attn.v_proj.weight",
    "self_attn.o_proj.weight",
    "mlp.up_proj.weight",
    "mlp.gate_proj.weight",
    "mlp.down_proj.weight",
    "lm_head.weight"
]
for name, data in model_part.items():
    for word in keywords:
        if word in name:
            # Quantize and dequantize the entire tensor
            model_part[name] = q4_1_quantize_and_dequantize_tensor(data)

# Save the updated model parts
torch.save(model_part, "pytorch_model_quantized.bin")

Reference:

相关标签
About Me
XD
Goals determine what you are going to be.
Category
标签云
Hilton CV FP16 Nginx Crawler FP8 Math Conda Quantize Disk llama.cpp Template 强化学习 ModelScope BeautifulSoup PIP Cloudreve C++ Land Hotel Vmess LeetCode Shortcut Quantization 飞书 Markdown PyCharm Django CSV Plate Jupyter Gemma 阿里云 Anaconda RGB Algorithm Interview ONNX Excel tar Ubuntu Datetime 继承 torchinfo EXCEL Windows Linux Bitcoin diffusers 顶会 Docker logger CC GIT FlashAttention Qwen GPTQ Attention SVR Paper COCO XGBoost Safetensors CEIR Qwen2.5 OpenCV Heatmap CLAP Knowledge Tensor OCR Firewall 签证 Pytorch Bert PDB Random LLAMA OpenAI Google NLTK 关于博主 Password uwsgi Review Use Mixtral 多线程 Base64 净利润 IndexTTS2 SQL Transformers Input AI CAM NLP MD5 BTC CUDA HuggingFace 递归学习法 Tiktoken scipy FastAPI Github tqdm GoogLeNet BF16 Streamlit 搞笑 Diagram Pandas Dataset WAN 版权 LoRA v2ray InvalidArgumentError 公式 Augmentation git-lfs FP32 Bin DeepStream mmap Logo API WebCrawler Baidu XML Hungarian News Tracking Ptyhon Numpy 报税 Web CTC Miniforge 算法题 Permission GGML Agent HaggingFace Video SPIE Qwen2 Jetson Freesound Domain Llama git GPT4 FP64 TensorFlow Git Search 财报 Python Paddle Michelin Pickle 第一性原理 ChatGPT PDF SAM UI Food 域名 v0.dev Bipartite SQLite Plotly Sklearn RAR LaTeX Translation printf NameSilo PyTorch UNIX QWEN TSV hf LLM Proxy VPN Claude VGG-16 TensorRT 图形思考法 Statistics Website Zip transformers Vim TTS VSCode 音频 Animate Pillow 腾讯云 Clash Distillation DeepSeek YOLO 证件照 uWSGI Breakpoint Image2Text Magnet Color JSON ResNet-50 多进程 Data Card
站点统计

本站现有博文320篇,共被浏览760659

本站已经建立2432天!

热门文章
文章归档
回到顶部