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

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

本站已经建立2583天!

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