## CausalLM-7B API on CPU for kaggle

!apt -y install -qq aria2
!pip install llama-cpp-python[server] --prefer-binary --extra-index-url=https://jllllll.github.io/llama-cpp-python-cuBLAS-wheels/AVX2/cpu
!pip install pyngrok

!aria2c --console-log-level=error -c -x 16 -s 16 -k 1M https://huggingface.co/TheBloke/CausalLM-7B-GGUF/resolve/main/causallm_7b.Q5_K_M.gguf -d ~ -o model.gguf

import subprocess
def iframe_thread():
    cmd = 'python -m llama_cpp.server --model /root/model.gguf --chat_format chatml --host 127.0.0.1 --n_ctx 8192 --n_threads 4 --n_threads_batch 4'
    p = subprocess.Popen(cmd.split(' '), stdout=subprocess.PIPE)

from threading import Thread
Thread(target=iframe_thread, daemon=True).start()

!ngrok config add-authtoken 🔑NGROK_TOKEN_HERE🔑
!ngrok http --domain=🚪YOUR_DOMAIN.ngrok-free.app🚪 8000

你需要自行在 ngrok.com 注册一个账号,从中获得验证 token,以及一个永久 domain,替换上文中 emoji 的部分,完成内网穿透。
从 log 开始输出到执行完毕约需要 1-2 分钟,末尾出现 http://127.0.0.1:8000 时,尝试访问你的域名(可能需要点击一次按钮)。以YOUR_DOMAIN.ngrok-free.app 为例,如果访问 https://YOUR_DOMAIN.ngrok-free.app/ 显示一行 {"detail":"Not Found"} ,则代表运行成功。你可以使用任何一个支持 OpenAI 接口的应用,将 https://YOUR_DOMAIN.ngrok-free.app 作为代理地址使用(部分应用可能需要改为 https://YOUR_DOMAIN.ngrok-free.app/v1 )。
8192 是该模型的默认支持的上下文长度,kaggle 是 2 核 CPU,但实际测试,threads 设置为 4 可以使 CPU 使用率达到 400%,生成速度明显加快。
14B 在纯 CPU 下吐字非常缓慢,没有尝试的必要。
查看 GeekBench6 的跑分

## CausalLM-13B-GPTQ on text-generation-webui for colab

%cd /content
!apt-get -y install -qq aria2
# !apt install nvidia-cuda-toolkit nvidia-cuda-toolkit-gcc

!git clone https://github.com/oobabooga/text-generation-webui
%cd /content/text-generation-webui
!pip install -q -r requirements.txt
!pip install auto-gptq --upgrade --force-reinstall

!aria2c --console-log-level=error -c -x 16 -s 16 -k 1M https://huggingface.co/TheBloke/CausalLM-14B-GPTQ/resolve/main/added_tokens.json -d /content/text-generation-webui/models/CausalLM-14B-GPTQ -o added_tokens.json
!aria2c --console-log-level=error -c -x 16 -s 16 -k 1M https://huggingface.co/TheBloke/CausalLM-14B-GPTQ/resolve/main/config.json -d /content/text-generation-webui/models/CausalLM-14B-GPTQ -o config.json
!aria2c --console-log-level=error -c -x 16 -s 16 -k 1M https://huggingface.co/TheBloke/CausalLM-14B-GPTQ/resolve/main/generation_config.json -d /content/text-generation-webui/models/CausalLM-14B-GPTQ -o generation_config.json
!aria2c --console-log-level=error -c -x 16 -s 16 -k 1M https://huggingface.co/TheBloke/CausalLM-14B-GPTQ/resolve/main/merges.txt -d /content/text-generation-webui/models/CausalLM-14B-GPTQ -o merges.txt
!aria2c --console-log-level=error -c -x 16 -s 16 -k 1M https://huggingface.co/TheBloke/CausalLM-14B-GPTQ/resolve/main/model.safetensors -d /content/text-generation-webui/models/CausalLM-14B-GPTQ -o model.safetensors
!aria2c --console-log-level=error -c -x 16 -s 16 -k 1M https://huggingface.co/TheBloke/CausalLM-14B-GPTQ/resolve/main/quantize_config.json -d /content/text-generation-webui/models/CausalLM-14B-GPTQ -o quantize_config.json
!aria2c --console-log-level=error -c -x 16 -s 16 -k 1M https://huggingface.co/TheBloke/CausalLM-14B-GPTQ/resolve/main/special_tokens_map.json -d /content/text-generation-webui/models/CausalLM-14B-GPTQ -o special_tokens_map.json
!aria2c --console-log-level=error -c -x 16 -s 16 -k 1M https://huggingface.co/TheBloke/CausalLM-14B-GPTQ/resolve/main/tokenizer.model -d /content/text-generation-webui/models/CausalLM-14B-GPTQ -o tokenizer.model
!aria2c --console-log-level=error -c -x 16 -s 16 -k 1M https://huggingface.co/TheBloke/CausalLM-14B-GPTQ/resolve/main/tokenizer_config.json -d /content/text-generation-webui/models/CausalLM-14B-GPTQ -o tokenizer_config.json
!aria2c --console-log-level=error -c -x 16 -s 16 -k 1M https://huggingface.co/TheBloke/CausalLM-14B-GPTQ/resolve/main/vocab.json -d /content/text-generation-webui/models/CausalLM-14B-GPTQ -o vocab.json

%cd /content/text-generation-webui
!python server.py --share --settings /content/settings.yaml --wbits 4 --groupsize 128 --loader AutoGPTQ --model /content/text-generation-webui/models/CausalLM-14B-GPTQ --no_inject_fused_attention

weibui 的依赖很多,如果运行错误,考虑执行 !apt install nvidia-cuda-toolkit nvidia-cuda-toolkit-gcc,正常启动至少需要 7 分钟。进入 webui 后聊天格式手动选择 ChatML 格式。

Edit

Pub: 12 Nov 2023 16:41 UTC

Edit: 12 Nov 2023 17:13 UTC

Views: 765