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

本站现有博文327篇,共被浏览833395

本站已经建立2538天!

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