Home

Awesome

<div align="center"> <!-- <h1>KTransformers</h1> --> <p align="center"> <picture> <img alt="KTransformers" src="https://github.com/user-attachments/assets/d5a2492f-a415-4456-af99-4ab102f13f8b" width=50%> </picture> </p> <h3>A Flexible Framework for Experiencing Cutting-edge LLM Inference Optimizations</h3> <strong><a href="#show-cases">🔥 Show Cases</a> | <a href="#quick-start">🚀 Quick Start</a> | <a href="#tutorial">📃 Tutorial</a> | <a href="https://github.com/kvcache-ai/ktransformers/discussions">💬 Discussion </a> </strong> </div> <h2 id="intro">🎉 Introduction</h2> KTransformers, pronounced as Quick Transformers, is designed to enhance your 🤗 <a href="https://github.com/huggingface/transformers">Transformers</a> experience with advanced kernel optimizations and placement/parallelism strategies. <br/><br/> KTransformers is a flexible, Python-centric framework designed with extensibility at its core. By implementing and injecting an optimized module with a single line of code, users gain access to a Transformers-compatible interface, RESTful APIs compliant with OpenAI and Ollama, and even a simplified ChatGPT-like web UI. <br/><br/> Our vision for KTransformers is to serve as a flexible platform for experimenting with innovative LLM inference optimizations. Please let us know if you need any other features. <h2 id="Updates">🔥 Updates</h2> <h2 id="show-cases">🔥 Show Cases</h2> <h3>1M Context Local Inference on a Desktop with Only 24GB VRAM</h3> <p align="center">

https://github.com/user-attachments/assets/a865e5e4-bca3-401e-94b8-af3c080e6c12

<p align="center"> <picture> <img alt="Single Needle Retrieval 128K" src="./doc/assets/needle_128K.png" width=100%> </picture> </p> <p align="center"> <picture> <img alt="Single Needle Retrieval 1000K" src="./doc/assets/needle_1M.png" width=100%> </picture> </p> <div> <h3>GPT-4-level Local VSCode Copilot on a Desktop with only 24GB VRAM</h3> </div>

https://github.com/user-attachments/assets/0b9fa2da-66f0-48eb-b4b9-f0e1f06f8927

</p> <p align="center"> <picture> <img alt="DeepSeek-Coder-V2 Score" src="https://github.com/user-attachments/assets/d052924e-8631-44de-aad2-97c54b965693" width=100%> </picture> </p> <p align="center">

https://github.com/user-attachments/assets/4c6a8a38-05aa-497d-8eb1-3a5b3918429c

</p>

<strong>More advanced features will coming soon, so stay tuned!</strong>

<h2 id="quick-start">🚀 Quick Start</h2> <h3>Preparation</h3> Some preparation: <h3>Installation</h3>
  1. Use a Docker image, see documentation for Docker

  2. You can install using Pypi (for linux):

    pip install ktransformers --no-build-isolation
    

    for windows we prepare a pre compiled whl package in ktransformers-0.1.1+cu125torch24avx2-cp311-cp311-win_amd64.whl, which require cuda-12.5, torch-2.4, python-3.11, more pre compiled package are being produced.

  3. Or you can download source code and compile:

    • init source code

      git clone https://github.com/kvcache-ai/ktransformers.git
      cd ktransformers
      git submodule init
      git submodule update
      
    • [Optional] If you want to run with website, please compile the website before execute bash install.sh

    • Compile and install (for Linux)

      bash install.sh
      
    • Compile and install(for Windows)

      install.bat
      
  4. If you are developer, you can make use of the makefile to compile and format the code. <br> the detailed usage of makefile is here

<h3>Local Chat</h3> We provide a simple command-line local chat Python script that you can run for testing.

Note that this is a very simple test tool only support one round chat without any memory about last input, if you want to try full ability of the model, you may go to RESTful API and Web UI. We use the DeepSeek-V2-Lite-Chat-GGUF model as an example here. But we also support other models, you can replace it with any other model that you want to test.

<h4>Run Example</h4>
# Begin from root of your cloned repo!
# Begin from root of your cloned repo!!
# Begin from root of your cloned repo!!! 

# Download mzwing/DeepSeek-V2-Lite-Chat-GGUF from huggingface
mkdir DeepSeek-V2-Lite-Chat-GGUF
cd DeepSeek-V2-Lite-Chat-GGUF

wget https://huggingface.co/mzwing/DeepSeek-V2-Lite-Chat-GGUF/resolve/main/DeepSeek-V2-Lite-Chat.Q4_K_M.gguf -O DeepSeek-V2-Lite-Chat.Q4_K_M.gguf

cd .. # Move to repo's root dir

# Start local chat
python -m ktransformers.local_chat --model_path deepseek-ai/DeepSeek-V2-Lite-Chat --gguf_path ./DeepSeek-V2-Lite-Chat-GGUF

# If you see “OSError: We couldn't connect to 'https://huggingface.co' to load this file”, try:
# GIT_LFS_SKIP_SMUDGE=1 git clone https://huggingface.co/deepseek-ai/DeepSeek-V2-Lite
# python  ktransformers.local_chat --model_path ./DeepSeek-V2-Lite --gguf_path ./DeepSeek-V2-Lite-Chat-GGUF

It features the following arguments:

<h3 id="suggested-model"> Suggested Model</h3>
Model NameModel SizeVRAMMinimum DRAMRecommended DRAM
DeepSeek-V2-q4_k_m133G11G136G192G
DeepSeek-V2.5-q4_k_m133G11G136G192G
DeepSeek-V2.5-IQ4_XS117G10G107G128G
Qwen2-57B-A14B-Instruct-q4_k_m33G8G34G64G
DeepSeek-V2-Lite-q4_k_m9.7G3G13G16G
Mixtral-8x7B-q4_k_m25G1.6G51G64G
Mixtral-8x22B-q4_k_m80G4G86.1G96G
InternLM2.5-7B-Chat-1M15.5G15.5G8G(32K context)150G (1M context)

More will come soon. Please let us know which models you are most interested in.

Be aware that you need to be subject to their corresponding model licenses when using DeepSeek and QWen.

<details> <summary>Click To Show how to run other examples</summary>
model nameweights download link
Qwen2-57BQwen2-57B-A14B-gguf-Q4K-M
DeepseekV2-coderDeepSeek-Coder-V2-Instruct-gguf-Q4K-M
DeepseekV2-chatDeepSeek-V2-Chat-gguf-Q4K-M
DeepseekV2-liteDeepSeek-V2-Lite-Chat-GGUF-Q4K-M
</details> <!-- pin block for jump --> <span id='id_666'> <h3>RESTful API and Web UI</h3>

Start without website:

ktransformers --model_path deepseek-ai/DeepSeek-V2-Lite-Chat --gguf_path /path/to/DeepSeek-V2-Lite-Chat-GGUF --port 10002

Start with website:

ktransformers --model_path deepseek-ai/DeepSeek-V2-Lite-Chat --gguf_path /path/to/DeepSeek-V2-Lite-Chat-GGUF  --port 10002 --web True

Or you want to start server with transformers, the model_path should include safetensors

ktransformers --type transformers --model_path /mnt/data/model/Qwen2-0.5B-Instruct --port 10002 --web True

Access website with url http://localhost:10002/web/index.html#/chat :

<p align="center"> <picture> <img alt="Web UI" src="https://github.com/user-attachments/assets/615dca9b-a08c-4183-bbd3-ad1362680faf" width=90%> </picture> </p>

More information about the RESTful API server can be found here. You can also find an example of integrating with Tabby here.

<h2 id="tutorial">📃 Brief Injection Tutorial</h2> At the heart of KTransformers is a user-friendly, template-based injection framework. This allows researchers to easily replace original torch modules with optimized variants. It also simplifies the process of combining multiple optimizations, allowing the exploration of their synergistic effects. </br> <p align="center"> <picture> <img alt="Inject-Struction" src="https://github.com/user-attachments/assets/6b4c1e54-9f6d-45c5-a3fc-8fa45e7d257e" width=65%> </picture> </p>

Given that vLLM already serves as a great framework for large-scale deployment optimizations, KTransformers is particularly focused on local deployments that are constrained by limited resources. We pay special attention to heterogeneous computing opportunities, such as GPU/CPU offloading of quantized models. For example, we support the efficient <a herf="https://github.com/Mozilla-Ocho/llamafile/tree/main">Llamafile</a> and <a herf="https://github.com/IST-DASLab/marlin">Marlin</a> kernels for CPU and GPU, respectively. More details can be found <a herf="doc/en/operators/llamafile.md">here</a>.

<h3>Example Usage</h3> To utilize the provided kernels, users only need to create a YAML-based injection template and add the call to `optimize_and_load_gguf` before using the Transformers model.
with torch.device("meta"):
    model = AutoModelForCausalLM.from_config(config, trust_remote_code=True)
optimize_and_load_gguf(model, optimize_rule_path, gguf_path, config)
...
generated = prefill_and_generate(model, tokenizer, input_tensor.cuda(), max_new_tokens=1000)

In this example, the AutoModel is first initialized on the meta device to avoid occupying any memory resources. Then, optimize_and_load_gguf iterates through all sub-modules of the model, matches rules specified in your YAML rule file, and replaces them with advanced modules as specified.

After injection, the original generate interface is available, but we also provide a compatible prefill_and_generate method, which enables further optimizations like CUDAGraph to improve generation speed.

<h3>How to custom your model</h3>

A detailed tutorial of the injection and multi-GPU using DeepSeek-V2 as an example is given here.

Below is an example of a YAML template for replacing all original Linear modules with Marlin, an advanced 4-bit quantization kernel.

- match:
    name: "^model\\.layers\\..*$"  # regular expression 
    class: torch.nn.Linear  # only match modules matching name and class simultaneously
  replace:
    class: ktransformers.operators.linear.KTransformerLinear  # optimized Kernel on quantized data types
    device: "cpu"   # which devices to load this module when initializing
    kwargs:
      generate_device: "cuda"
      generate_linear_type: "QuantizedLinearMarlin"

Each rule in the YAML file has two parts: match and replace. The match part specifies which module should be replaced, and the replace part specifies the module to be injected into the model along with the initialization keywords.

You can find example rule templates for optimizing DeepSeek-V2 and Qwen2-57B-A14, two SOTA MoE models, in the ktransformers/optimize/optimize_rules directory. These templates are used to power the local_chat.py demo.

If you are interested in our design principles and the implementation of the injection framework, please refer to the design document.

<h2 id="ack">Acknowledgment and Contributors</h2>

The development of KTransformer is based on the flexible and versatile framework provided by Transformers. We also benefit from advanced kernels such as GGUF/GGML, Llamafile, and Marlin. We are planning to contribute back to the community by upstreaming our modifications.

KTransformer is actively maintained and developed by contributors from the <a href="https://madsys.cs.tsinghua.edu.cn/">MADSys group</a> at Tsinghua University and members from <a href="http://approaching.ai/">Approaching.AI</a>. We welcome new contributors to join us in making KTransformer faster and easier to use.