用 Ollama 加上 ollama-ui (Chrome extension) 測試

ChatGPT 類的應用我最常還是拿來跑翻譯,有些東西不希望透過雲端去翻,試著找本機的方案跑看看。

因為桌機是 Linux 系統,目前比較成熟的方案看起來是 Ollama,可以在本地端跑起來並且開一個 HTTP API 讓其他程式呼叫。

我的作法是寫 systemd 設定,但是不要開機就跑起來,需要的時候再跑 sudo service ollama start 再使用就好,只是要注意第一次跑會需要從 NVMe disk 裡面讀 model,會比較慢一點,後續就正常了...

另外找了一下 GUI,看起來 ollama-ui 算是還 OK,兩個搭起來後拿 mistral-openorca 這組 7B model 跑翻譯,這邊是拿 NHK 網站上面的「政党支持率 自民下落20%台に 立民 維新も伸びず NHK世論調査」這篇翻,看起來還可以:

不過 7b 的幾個 model 測了一下都不到 GPT-3.5 的程度,要多跑幾次才會出現堪用的 (像上面那樣),之後再多測看看其他的 model...

AMD 推出 16GB 的 RX 7600 XT

看到「AMD Unveils AMD Radeon RX 7600 XT Graphics Card – Incredible Gaming at 1080p and Beyond for Under $350」這篇,16GB VRAM 官方的定價在 US$329...

剛好昨天寫的「Mixtral 8x7B 的論文出來了」提到了 Nvidia 的 3060 Ti 的 16GB 版本是跑 LLM 的窮人選擇,因為 12GB VRAM 的卡官方訂在 US$329,目前售價大約在 NT$9000 (~US$300) 左右。

這次 AMD 這張 16GB VRAM 美國定價是 US$329,剛好跟 3060 Ti 12GB 版本相同,這下 entry level 的市場就瞬間變得有趣了起來,雖然說 AMD 這邊的軟體支援度是差了一些,但最近算是急起直追,對於想要追求 CP 值的群眾來說還蠻有吸引力的?

後續來追看看台灣的售價...

Mixtral 8x7B 的論文出來了

Hacker News 上看到 Mixtral-8x7B-v0.1 以及 Mixtral-8x7B-Instruct-v0.1 的論文出來了:「Mixtral 8x7B: A sparse Mixture of Experts language model (arxiv.org)」,arXiv 上的連結:「Mixtral of Experts」。

跟先前大家從公開資料研究的差不多,這個研究成果主要不是降低參數的大小,而是降低運算的量:

As a result, each token has access to 47B parameters, but only uses 13B active parameters during inference.

然後仍然是超越 GPT-3.5 的水準:

Mixtral was trained with a context size of 32k tokens and it outperforms or matches Llama 2 70B and GPT-3.5 across all evaluated benchmarks.

先看計算量的問題,Mixtral 8x7B 的 model 對 VRAM 要求仍然不是消費級 GPU 可以達到的,對一般家用電腦來說,還是需要 quantisation 降低精度換取對 VRAM 空間的壓力下降。

這點可以在 TheBloke/Mixtral-8x7B-v0.1-GGUF 這邊看到各種 quantisation 後需要的 VRAM 大小。

如果用 CPU 計算的話目前應該不是大問題,目前 LLM 的大小對於一般主機的 RAM 來說還不是問題 (單條 32GB,四條就有 128GB 了),加上現在 llama.cpp 主力已經是用 mmap 的方式在存取檔案,filesystem cache 可以在多次執行中重複使用,只是用 CPU 就不能對速度有太多想法了。

但如果往 GPU 這邊看的話就得取捨了,目前 GPU 中能跑 Mixtral 8x7B 最便宜的方案應該是兩張 3060 12GB 組成 24GB VRAM,一張約 NT$9k (~US$300),兩張約 NT$18k (~US$600),這樣的話有機會跑 mixtral-8x7b-v0.1.Q3_K_M.gguf,不過這邊寫「very small, high quality loss」。

如果 mixtral-8x7b-v0.1.Q3_K_M.gguf 的品質不能接受,希望計算品質好一點的話,三張 3060 Ti 12GB 組 36GB VRAM 的方案約 NT$27000 (~US$900),不過主機板可能要挑一下;這樣就有機會用需求 34.73 GB VRAM 的 mixtral-8x7b-v0.1.Q5_K_M.gguf 了,評語是「large, very low quality loss - recommended」。

最後岔題,剛剛算了一下成本,發現 3060 Ti 12GB 這張還是穩穩的 LLM 窮人卡,先前在「雲端上面的 GPU 資源費用,以及地端的 GPU 決策圖」這邊提到的決策圖,即使在 2023 年七月 4060 Ti 16GB 出了以後還是很好用... (約 NT$15k,~US$500)

微軟 Phi-2 model 的授權改成 MIT License

Hacker News 的「Microsoft Phi-2 model changes licence to MIT (huggingface.co)」這邊看到的消息,連結是改成 MIT License 的 commit:「Upload 3 files · microsoft/phi-2 at 7e10f3e」。

看了一下 model 的參數是 2.7B,宣稱在 13B 以下 model 中是前段班:

Phi-2 showcased a nearly state-of-the-art performance among models with less than 13 billion parameters.

再回頭查一下這段宣稱的時間,當初發表的時間是 2023/12/12:「Phi-2: The surprising power of small language models」。

應該是希望在行動裝置上用更少的運算量達到效果...

另外找了一下 GGUF 格式,看起來 TheBloke/phi-2-GGUF 這邊已經有轉好的了,可以直接上 llama.cpp 跑。

用 GPT-4 重現 Google Deepmind 的 Gemini Demo 影片

Google Deepmind 前幾天發表了 Gemini:「Introducing Gemini: our largest and most capable AI model」,同時也釋出了 Demo 影片:

但後來大家發現 Demo 影片中人並不是直接透過語音與 Gemini 互動,而是把輸入進去的指令讓人讀出來,而且省略掉中間的各種 delay,是個被後製不少的影片:「Google’s best Gemini demo was faked」。

然後就有人用 GPT-4 實作出一個可以互動的版本了,雖然是 PoC 等級的,但反而更真實:「Show HN: I Remade the Fake Google Gemini Demo, Except Using GPT-4 and It's Real (greg.technology)」。

記得 Google 年初的 Bart Demo 也出包,可以來看看後面第三次的情況?

Mistral 推出新的 Mixtral 8x7B,另外也開始提供付費 API 服務

首先是「Mixtral of experts」這個公告,Mistral 推出了新的 model,叫做 Mixtral 8x7B。這是一個 46.7B 的 model,但計算每個 token 時只需要計算 12.9B 的值:

Concretely, Mixtral has 46.7B total parameters but only uses 12.9B parameters per token. It, therefore, processes input and generates output at the same speed and for the same cost as a 12.9B model.

這就可以大幅降低計算需要的量,卻可以達到與 Llama 2 70B 或是 GPT-3.5 同一個等級的品質:

另外在「Mistral: Our first AI endpoints are available in early access (mistral.ai)」這邊看到官方推出了 API,正式的公告在「La plateforme」這邊,同時價錢也已經出來了:「Pricing」。

依照官方的說明,Mistral Small 用的是上面提到的 Mixtral 8x7B,所以表現的數字是一樣的。另外一個 Mistral Medium 沒有提到太多細節,只丟出簡單的說明:

然後 API 看起來會相容 OpenAI 家的 API:

Our API follows the specifications of the popular chat interface initially proposed by our dearest competitor.

Hacker News 上面的 id=38599156 有整理出重點,把價格數字都換算成 1m output 的費用,可以看到表現相近的 Mistral Small (Mixtral 8x7B) 與 GPT-3.5 在價錢上也差不多:

Per 1 million output tokens:
Mistral-medium $8
Mistral-small $1.94
gpt-3.5-turbo-1106 $2
gpt-4-1106-preview $30
gpt-4 $60
gpt-4-32k $120

沒有拿 GPT-4 的數字來比,代表 Mistral Medium 與 GPT-4 有一定的差距,這點也可以從價錢上面看出來。

不過我更在意的是 Mistral Medium 的 model 會不會放出來?

一個檔案直接跑起大型語言模型的 llamafile

llamafile 是昨天很紅的一個專案,由 Mozilla Internet Ecosystem (MIECO) 弄出來的專案,可以使用一個檔案直接跑起大型語言模型的 HTTP server,讓你可以在瀏覽器裡面直接使用。

直接看官方的 README.md 就可以蠻簡單的跑起來,不過 Simon Willison 也有寫一篇文章介紹一下,可以看看:「llamafile is the new best way to run a LLM on your own computer」。

這邊說的「一個檔案」是指同一個檔案同時可以在 WindowsmacOSLinuxFreeBSDOpenBSD 以及 NetBSD 上面跑,而且這個檔案也把大型語言模型 (LLM) 的 model 檔案包進去,所以檔案會蠻大的,但畢竟就是方便讓人使用:

下載下來直接執行,預設就會在 port 8080 跑起來,可以直接連到 http://127.0.0.1:8080/ 連進去使用。

llamafile 用到的技術是 Cosmopolitan 專案,可以把多個平台的執行檔包在同一個檔案裡面使用。

另外用到的專案是 llama.cpp,這個蠻多人都用過了,可以很方便的用 CPU 或是 GPU 跑 LLM。

我在 Linux 上面跑剛好遇到幾個問題,都是在 README.md 上面有提到的。

第一個是 zsh 無法直接跑 llamafile (Ubuntu 22.04 內建 zsh 的是 5.8.1),這邊官方的建議是用 sh -c ./llamafile 避開:

If you use zsh and have trouble running llamafile, try saying sh -c ./llamafile. This is due to a bug that was fixed in zsh 5.9+. The same is the case for Python subprocess, old versions of Fish, etc.

另外一個對是 GPU 的支援,這邊跟你說加上 --n-gpu-layers 35 就可以用,所以一開始先用 sh -c ./llamafile --n-gpu-layers 35 試著跑:

On Linux, Nvidia cuBLAS GPU support will be compiled on the fly if (1) you have the cc compiler installed, (2) you pass the --n-gpu-layers 35 flag (or whatever value is appropriate) to enable GPU, and (3) the CUDA developer toolkit is installed on your machine and the nvcc compiler is on your path.

但可以看到沒有被 offload 到 GPU 上面:

llm_load_tensors: ggml ctx size =    0.11 MB
llm_load_tensors: using CUDA for GPU acceleration
llm_load_tensors: mem required  = 4165.47 MB
llm_load_tensors: offloading 0 repeating layers to GPU
llm_load_tensors: offloaded 0/35 layers to GPU
llm_load_tensors: VRAM used: 0.00 MB

嘗試了不同的方法,發現要跑 sh -c "./llamafile --n-gpu-layers 35",也就是把參數一起包進去,這樣就會出現對應的 offload 資訊,而且輸出也快很多:

llm_load_tensors: ggml ctx size =    0.11 MB
llm_load_tensors: using CUDA for GPU acceleration
llm_load_tensors: mem required  =   70.42 MB
llm_load_tensors: offloading 32 repeating layers to GPU
llm_load_tensors: offloading non-repeating layers to GPU
llm_load_tensors: offloaded 35/35 layers to GPU
llm_load_tensors: VRAM used: 4095.05 MB

玩了一下像是這樣:

Georgi Gerganov 給了在 AWS 上面用 GPU instance 跑 llama.cpp 的說明

Georgi Gerganov 寫了一篇怎麼在 AWS 上面用 GPU instance 跑 llama.cpp 的說明:「Using llama.cpp with AWS instances #4225」。

先跳到最後面的懶人套件,直接提供了 shell script 幫你弄完:

bash -c "$(curl -s https://ggml.ai/server-llm.sh)"

回到開頭的部分,機器的選擇上面,他選了一台最便宜的 4 vCPU + 16GB RAM + 16GB VRAM 的機器來跑。

然後他提到了 OpenHermes-2.5-Mistral-7B 這個模型最近很紅,也許有機會看一下:

We have just 16GB VRAM to work with, so we likely want to choose a 7B model. Lately, the OpenHermes-2.5-Mistral-7B model is getting some traction so let's go with it.

用 llama.cpp 裡面的 server 跑起 API server:

./server -m models/openhermes-7b-v2.5/ggml-model-q4_k.gguf --port 8888 --host 0.0.0.0 --ctx-size 10240 --parallel 4 -ngl 99 -n 512

接著就可以用 cURL 測試:

curl -s http://XXX.XXX.XXX.XXX:8888/v1/chat/completions \
    -H "Content-Type: application/json" \
    -H "Authorization: Bearer no-key" \
    -d '{
        "model": "gpt-3.5-turbo",
        "messages": [
            {
                "role": "system",
                "content": "You are ChatGPT, an AI assistant. Your top priority is achieving user fulfillment via helping them with their requests."
            },
            {
                "role": "user",
                "content": "Write a limerick about python exceptions"
            }
        ]
    }' | jq

都包好了...

用 llama.cpp 玩 Mistral 7B Instruct,補一下 llama.cpp 的發展

看到「Workers AI Update: Hello Mistral 7B」這篇想到的,先前有提到「號稱目前最強的 Mistral 7B」,加上有一陣子沒看 llama.cpp 最近的發展,跳下去重新測試時發現有不少進展。

一個比較大的進展是 llama.cpp 推出 gguf 格式,取代之前的 ggml 格式。

新的格式可以想像是在檔案裡面放了通用性的 feature flag,就不會遇到新的 model 用到新的方法,沒辦法在 ggml 裡面指定 + 新增 feature,就得把 llama.cpp 整包 fork 拉出出去大改。

這差不多是三個月前的事情,蠻多 model 都已經支援了,像是 maddes8cht 這邊就整理了很多 OSL model (open source license) 可以直接下載下來用,不需要自己轉檔。

像是 Falcon 40B 與標題提到的 Mistral 7B,以及對應的 Instruct 版本 Mistral 7B Instruct 都有轉好的檔案可以下載。

另外一個有趣的功能是 llama.cpp 有了輸出 embedding 的能力,在編完後會多了一個 embedding 執行檔可以用;原來的主程式 main 也還在。

往 git repository 裡面看了一下記錄,差不多是在今年三月就已經有的功能,然後九月加到 README.md 裡面說明。

在蠻多情況下 embedding 輸出 vector 會比輸出 string 好用很多,像是相似度、推薦、語意分析這些計算;拿到 vector 後就算沒有專門的系統處理,用個 PostgreSQL 搭個能進行 vector 計算的 plugin 也就可以弄出不少東西了 (像是 pgvector)。

不過會用 embedding 的人應該早就用 PyTorch 或是其他工具,直接拉出來用了,llama.cpp 支援輸出 embedding 算是有趣但沒有到重要?

然後是支援 OpenCL,這個功能讓不少非 Nvidia 的平台可以利用 GPU 資源加速,像是 AMD 的顯卡,以及一些嵌入式系統,這點對於 llama.cpp 當初的目標就蠻有幫助的...

回到 Mistral 7B Instruct,其實小模型還是小模型,我問個演算法的題目:

./main -m models/7B/mistral-7b-instruct-v0.1.Q8_0.gguf -t 1 -ngl 32 -p '請給出一個 python 寫的 fibonacci sequence 演算法,需要 O(log(n)) 的時間複雜度。'

輸出長這樣:

 請給出一個 python 寫的 fibonacci sequence 演算法,需要 O(log(n)) 的時間複雜度。

```python
def fib_seq(n):
    return get_fibonacci(n, 0, 1)


def get_fibonacci(n, a=0, b=1):
    if n == 0:
        return []
    if n == 1:
        return [a]
    fib_seq = [a]
    for i in range(1, n):
        fib_seq.append(get_fibonacci(i, b, a + b)[0])
    return fib_seq
``` [end of text]

ChatGPT 的話可以給出一個正確的演算法,這邊只需要 ChatGPT-3.5 的 model 就可以了:

OpenAI 的 API 又降價了...

這次 OpenAI 的 API 又降價了,這次是倍數等級的降:「New models and developer products announced at DevDay」。

GPT-4 Turbo 的部分直接是拉高 context 以及降低價錢,從本來的 8K/32K context,直接拉高到單一 128K context 產品,而且價錢直接砍了 3/4 左右:

GPT-4 8K
Input: $0.03
Output: $0.06

GPT-4 32K
Input: $0.06
Output: $0.12

GPT-4 Turbo 128K
Input: $0.01
Output: $0.03

GPT-3.5 Turbo 則是直耶拿掉 4K context 產品,然後把價錢砍了一半:

GPT-3.5 Turbo 4K
Input: $0.0015
Output: $0.002

GPT-3.5 Turbo 16K
Input: $0.003
Output: $0.004

GPT-3.5 Turbo 16K
Input: $0.001
Output: $0.002

GPT-3.5 Turbo fine-tuning 的服務則是從本來 4K context 產品線,多了一條 16K context 的產品線,價錢也是砍了一半以上:

GPT-3.5 Turbo 4K fine-tuning
Training: $0.008
Input: $0.012
Output: $0.016

GPT-3.5 Turbo 4K and 16K fine-tuning
Training: $0.008
Input: $0.003
Output: $0.006

另外也多了一些非文字類的功能,包括了影像與聲音的內容。

記得之前有想過的一些點子,當時粗算了一下覺得太貴,好像可以重算看看...