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

本站现有博文338篇,共被浏览969390次

本站已经建立2681天!

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