Code Monkey home page Code Monkey logo

calm's Introduction

๐Ÿ˜Œ calm

This is an implementation of language model inference, aiming to get maximum single-GPU single-batch hardware utilization for LLM architectures with a minimal implementation and no dependencies1.

The goal of this project is experimentation and prototyping; it does not aim to be production ready or stable.

If you need support for a wide range of models, computing devices or quantization methods, you're probably looking for llama.cpp or ๐Ÿค— Transformers. If you need to run inference for multiple batches, you're probably looking for vLLM.

Parts of this code are based on Andrej Karpathy's llama2.c.

Running

To build and run calm, you need to download and convert a model, build the code using make2 and run it:

git clone https://huggingface.co/mistralai/Mistral-7B-Instruct-v0.2
python tools/convert.py mistral-7b-instruct.calm Mistral-7B-Instruct-v0.2/
make && ./build/run mistral-7b-instruct.calm -i "Q: What is the meaning of life?" -t 0

You can also run the model in chat mode (for models like Mistral/Mixtral you might want to increase context size via -c from the default 4096):

make && ./build/run mistral-7b-instruct.calm -y "You are a helpful AI assistant."

Before running Python you may want to install the dependencies via pip install -r tools/requirements.txt. When using git to download models, git-lfs is required and the download size may be larger than necessary; you can use tools/download.py instead (assumes models use Safetensors by default):

python tools/download.py Mistral-7B-Instruct-v0.2/ mistralai/Mistral-7B-Instruct-v0.2

Supported models

calm supports a subset of decoder-only transformer architectures:

  • Llama-like baseline (pre/post normalization, gated FFN, sequential attention mixing and FFN, RoPE)
  • RoPE enhancements (partial rotary dimension, independent head dimension)
  • SiLU or GELU FFN gate activation
  • RMSNorm or LayerNorm* normalization (no bias support)
  • Optional minor variations (QKV bias, QKV clipping, tied embeddings)
  • Optional mixture of experts (with top-k expert selection)

It has been tested on following models:

Architecture Models
Llama TinyLlama 1.1B, Cosmo 1B, Llama2 7B, Llama2 13B, LLaMA Pro 8B, Yi 6B, Yi 9B, Yi 34B, H2O Danube 1.8B, DeepSeekMath 7B, LargeWorldModel 7B 1M, Xverse 7B
Mistral Mistral 7B, Mistral Pro 8B, SOLAR 10.7B, GritLM 7B, Starling 7B
Qwen2 Qwen1.5 0.5B, Qwen1.5 1.8B, Qwen1.5 4B, Qwen1.5 7B, Qwen1.5 14B
Mixtral Mixtral 8x7B, Mixtral 8x22B, GritLM 8x7B
OLMo OLMo 1B, OLMo 7B
Gemma Gemma 2B, Gemma 7B (note: 7B version has issues with fp8 quantization)
MiniCPM MiniCPM 2B, MiniCPM 2B 128K
Cohere Command-R
InternLM InternLM2-1.8B, InternLM2-7B, InternLM2-20B
DBRX DBRX 132B

Supported formats

Model weights support fp16, fp8 and gf4 formats; the weight type is specified at conversion time via --dtype argument to convert.py, and defaults to fp8.

fp16 corresponds to 16-bit floating point (e5m10). Note that some models store weights in bf16 which will be automatically converted.

fp8 corresponds to 8-bit floating point (e5m2). Using fp8 carries a ~0.5% perplexity penalty at almost double the inference speed and half the model size. e4m3 variant of fp8 would result in a much smaller perplexity penalty (~0.1%) with basic tensor scaling, but it's currently not used because of performance issues wrt floating-point conversion.

gf4 corresponds to 4-bit grouped floating point (8 values are stored in 32 bits using 3 bit quantized scale per value and one fp8 group scale). Using gf4 currently carries a perplexity penalty but increases inference speed by ~75% and halves the model size compared to fp8. Unlike llama.cpp's K-quants, gf4 quantization is pure and uniform - all layers are quantized to exactly 4 bits per weight.

KV cache is using fp16 by default; when using longer contexts (> 4096), CUDA implementation automatically switches to fp8 to improve memory/performance. This comes at a small perplexity cost.

Performance

Auto-regressive prediction for a single sequence needs to read the entire model and the entire KV cache (until current token) for every token. As such, given an optimal implementation we'd expect the process to be bandwidth bound. Note that the cost of token generation at the beginning of the sequence should be smaller than the cost at the end of the sequence due to the need to read data from KV cache.

When using NVidia GeForce RTX 4090, calm gets the following performance on a few models; each model is measured with fp16, fp8 and gf4 weights at the beginning of the context window (first 32 tokens) and at the end (last 32 tokens with an offset 2000 for 2048 contexts, 4000 for 4096 contexts and 16000 for 16384 contexts):

Model (context) Performance (first 32 tokens) Performance (last 32 tokens)
Llama2 7B (4096), fp16 69 tok/s (919 GB/s) 60 tok/s (921 GB/s)
Llama2 7B (4096), fp8 135 tok/s (893 GB/s) 103 tok/s (899 GB/s)
Llama2 7B (4096), gf4 246 tok/s (815 GB/s) 158 tok/s (857 GB/s)
Llama2 13B (4096), fp8 70 tok/s (910 GB/s) 56 tok/s (907 GB/s)
Llama2 13B (4096), gf4 131 tok/s (848 GB/s) 88 tok/s (863 GB/s)
Mistral 7B (4096), fp16 65 tok/s (925 GB/s) 62 tok/s (916 GB/s)
Mistral 7B (4096), fp8 127 tok/s (902 GB/s) 116 tok/s (888 GB/s)
Mistral 7B (4096), gf4 237 tok/s (843 GB/s) 203 tok/s (832 GB/s)
Mixtral 8x7B (4096), gf4 137 tok/s (875 GB/s) 125 tok/s (862 GB/s)
Mixtral 8x7B (16384), gf4 137 tok/s (879 GB/s) 105 tok/s (781 GB/s)
Yi 34B (4096), gf4 52 tok/s (884 GB/s) 47 tok/s (851 GB/s)

Currently prompts are processed serially, one token at a time; in the future, prompt processing will need to be parallelized to avoid the bandwidth bottleneck.

With smaller weights on small models, getting closer to bandwidth limit becomes more difficult. Future optimizations may increase the gap here for small models, although smaller weights are most valuable to be able to infer larger models.

RTX 4090 has a peak bandwidth of ~1008 GB/s, however it's unclear if a peak higher than ~950 GB/s is attainable in practice3.

calm can run on A100/H100 accelerators (but is mostly tuned for H100 fp8 weights). When using Mixtral 8x7B (fp8) on 1xH100 SXM, it runs at ~200 tok/s (2550 GB/s) for 256-token outputs.

Model files

calm uses ๐Ÿค— Safetensors to store model files. Note that the models require conversion (see below), because calm stores model hyperparameters in .safetensors metadata and may expect a particular set of tensor names or weight order within tensors that is not always compatible with the source. Tokenizer data is stored as tensors inside the model file as well.

Footnotes

  1. CUDA runtime and compiler is used for GPU acceleration, but no CUDA or C libraries are used. Python conversion scripts use safetensors and torch, see tools/requirements.txt. โ†ฉ

  2. Linux is the main supported OS at the moment; calm also works on macOS (on CPU) and has experimental Metal support. โ†ฉ

  3. Based on testing a specific Gigabyte GeForce RTX 4090 where both individual kernels from this repository and cuBLAS peak at about ~955 GB/s. โ†ฉ

calm's People

Contributors

zeux avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.