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

本站现有博文332篇,共被浏览887379

本站已经建立2591天!

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