EADST

llama.cpp: Efficient 6-bit Data Packing in an 8-bit Array

This code snippet, adapted from llama.cpp by ggerganov, demonstrates a method for efficiently packing 6-bit values into an 8-bit uint8 array. It involves scaling, clamping, and bitwise manipulation to optimize or compress data, suitable for specific processing or hardware requirements.

// Initialize inverse scale factor with a fixed scaling offset and the maximum scale value.
float iscale = -32.f/max_scale;
// QK_K = 256. Iterate over a subset of the scales array, determined by QK_K divided by 16.
for (int j = 0; j < QK_K/16; ++j) {
    // Scale and round the j-th element of the scales array to the nearest integer.
    int8_t l = nearest_int(iscale * scales[j]);

    // Clamp the value of l to the range [-32, 31] and normalize it to [0, 63].
    l = MAX(-32, MIN(31, l)) + 32;

    // Store the 0-7th scale lower 4 bits of l in y[i].scales if in the first half of the loop.
    if (j < 8) {
        y[i].scales[j] = l & 0xF;
    } 
    // In the second half, store the 8-15th scale lower 4 bits of l into the higher 4 bits of y[i].scales at j-8.
    else {
        y[i].scales[j-8] |= ((l & 0xF) << 4);
    }

    // Shift the higher 4 bits of l to the lower positions.
    l >>= 4;

    // Calculate the index for storing the lower 2 bits(previous l 2 higher bits) of the shifted l and store them in y[i].scales.
    // The specific position in the array is determined by a combination of modulo and division operations.
    y[i].scales[j % 4 + 8] |= (l << (2 * (j / 4)));
}

The key aspects of this code include:

  • Scaling and Normalization: Adjusts the data values to a suitable range for bit manipulation.
  • Bitwise Operations: Utilizes masking (&), shifting (<<, >>), and bitwise OR (|=) to pack data efficiently.
  • Data Optimization: The method packs data into a smaller space, allowing for efficient use of memory and potentially faster processing.

This approach is particularly useful in scenarios where memory optimization is crucial, such as in embedded systems or when dealing with large datasets.

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

本站现有博文337篇,共被浏览961564次

本站已经建立2673天!

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