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

本站现有博文339篇,共被浏览991572次

本站已经建立2703天!

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