一個檔案直接跑起大型語言模型的 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

玩了一下像是這樣:

號稱目前最強的 Mistral 7B

Hacker News 上看到「Mistral 7B (mistral.ai)」,Mistral 7B 是目前號稱最強的 7B model。

宣稱在所有項目超越 Llama 2 13B,以及在許多項目超越 Llama 1 34B:

Outperforms Llama 2 13B on all benchmarks
Outperforms Llama 1 34B on many benchmarks

很重要的是以 open source license 放出來的,選的是 Apache License, Version 2.0

We’re releasing Mistral 7B under the Apache 2.0 license, it can be used without restrictions.

這個 model 大小是可以用 CPU 跑的,馬上就有人推 patch 進 llama.cpp 了:「Added the fact that llama.cpp supports Mistral AI release 0.1 #3362」。

我記得 Llama 2 13B 的輸出結果還有點微妙,但如果說是全部都超過的話,也許可以期待看看品質...

問 LLM 台灣是不是獨立國家...

Hacker News 上看到「Comparing 60 LLMs with a set of 20 prompts (llmonitor.com)」這篇 (看到的時候在第一名),原文在「Asking 60+ LLMs a set of 20 questions」這邊。

作者寫了 20 個問題讓一堆 LLM 回答,把回答的結果以及時間記錄起來,其中看到「Is Taiwan an independent country?」這個問題,共有 54 個 LLM 的結果,可以看到各家 LLM 的回答。

作者雖然註解解釋這題的回答是「是」(Note: Correct answer: yes it is.),但考慮到訓練的語料,大多數的回答都會提到全世界的政治情勢,或是帶出「這個問題很複雜」的說明。

不過我就是想看其他類型的回答 XD

直接拒絕回答,出現空白的有 Code Llama Instruct (7B)、Dolly v2 (3B)、Dolly v2 (7B)、Falcon Instruct (7B)、Koala (13B)、Luminous Supreme Control、Vicuna v1.3 (7B)。

然後出現沒意義的輸出的是 Vicuna v1.5 (13B),這邊丟出 48 行的 <bot>:,沒有其他內容。

另外一個頗歡樂的回答是 Vicuna v1.3 (13B),直接出現簡體中文回答的,而且獨立了 XDDD

台湾是一个独立的国家。

這邊 Koala 與 Vicuna 系列的都是 LMSYS 的作品,這邊的學生團隊 (Student Team) 都是華人名字:「About | LMSYS Org」,可能是丟了不少中文資料進去才會冒出簡體中文的回答?

另外一個有趣的是 Databricks 的 Dolly v2 (12B) 的回答直接說「不是」,沒有人和其他解釋:

<bot>: No.

而 Dolly 在專案頁面上有提到是基於 pythia-12b

Based on pythia-12b, Dolly is trained on ~15k instruction/response fine tuning records databricks-dolly-15k generated by Databricks employees in capability domains from the InstructGPT paper, including brainstorming, classification, closed QA, generation, information extraction, open QA and summarization.

不過回頭看同樣是 Pythia 家系的 Open-Assistant Pythia SFT-4 (12B),他的回答是:

<bot>: Yes, Taiwan is an independent country.

以及 Pythia-Chat-Base (7B) 的回答:

Yes, Taiwan is an independent country.

所以 Databricks 怎麼 train 的,把 pythia 的結果直接反過來 XDDD

不是 open source license 的 Falcon 180B 釋出

看到「Spread Your Wings: Falcon 180B is here」這個,Falcon 180B 釋出,號稱跟 LLaMA 2 站在同一個平台上,但目前看到的授權不是 open source license,大概就是留個記錄下來,實際上應該就不會去碰...

關於 license 的討論在 Hacker News 上有不少,可以參考:「Falcon 180B (huggingface.co)」。

日本 LINE 推出的 LLM (以日語材料訓練)

看到「36億パラメータの日本語言語モデルを公開しました」這篇,日本的 LINE 丟出 Apache License 2.0 的 LLM,拿起來跑看看還蠻有趣的:

他的特點是用日語資料訓練出來的 LLM:

最終的な学習には約650GBのコーパスを利用していますが、英語の大規模コーパスとして一般的に用いられているもの(Pileコーパス)が約800GBであることを踏まえると、我々のデータも遜色ない大きさであると言えます。

我拿 1.7B 跑,小修改一下故意給英文的 prompt 後,可以看到輸出頗有趣的,畢竟是從日文資料訓練出來的:

{'generated_text': 'An apple a day keeps the doctor away.\n「一日リンゴ1個」は apple days で'}
{'generated_text': 'An apple a day keeps the doctor away thinking happier. The biggest happ'}
{'generated_text': 'An apple a day keeps the doctor away from here.」と英語で訳しましょう。「I have a dream'}
{'generated_text': 'An apple a day keeps the doctor away(sometimes usually thinks far a'}
{'generated_text': 'An apple a day keeps the doctor away. 日はまたのぼり、 医者は去って行った。 They'}
{'generated_text': 'An apple a day keeps the doctor away thought about being in the center of the'}
{'generated_text': 'An apple a day keeps the doctor away from all the time.\n16. I feel like'}
{'generated_text': 'An apple a day keeps the doctor away and draws and eats around one table'}
{'generated_text': 'An apple a day keeps the doctor away from your mother\nAnd another male you are'}
{'generated_text': "An apple a day keeps the doctor away. What's the opinion you wrote in"}

這邊有訓練的運算量計算,1.7B 的 model 訓練換成起來會用道 4000 小時的 A100 80GB (假設你有 100 張的話,就是 40 小時):

本モデルの構築に要した時間について、例えば1.7BモデルについてはA100 80GBで換算し、約4000GPU時間を費やしています。学習時間は特に日本語の大規模言語モデルの学習では公開されていないことが多く、適切な比較はできませんが、例えば rinna 0.3Bモデルの学習はV100 32GBで約8600GPU時間を費やしているようで、費やした時間に比して効率の良い学習が行えていると考えられます。

目前是提到有計畫要放出 instruction tuning 的版本:

また、これらのモデルについて、指示文に対して適切な出力を行えるようにチューニング(Instruction tuning)したモデルを近日中に公開予定です。続報は@LINE_DEVをフォローしてお待ち下さい。

這個 LLM 先記起來,以後也許在其他場景有機會用到?

AMD 平台上的 LLM 計算

前幾天在 Hacker News 上看到的文章:「Making AMD GPUs competitive for LLM inference (mlc.ai)」,原文在「Making AMD GPUs competitive for LLM inference」這邊。

Nvidia 在 GPU 上的各種運算這塊進來的很早,除了本家開發了很多工具以外,社群的支援度也很好。而 AMD 這邊就差了不少,但這也反應在顯卡的售價上面。

作者整理了同樣是 24GB VRAM 的顯卡出來,分別是 AMD 的 7900XTX,以及 Nvidia 的 3090 Ti 與新的 4090

可以看出來縮然同樣 fp16 對應到的功耗差蠻多的,但單價低很多,對於業餘玩家偶而用來說,其實是個可以考慮的方案。

而他們的成果可以看出來效果其實不差,跑 Llama 2 的 model 可以看到 CP 值相當高:

看起來支援的主力在 ROCm 上,就效能與功耗的筆直來說其實是超越的?(或者保守一點的說,是在同一個水平上的)

現在算是 AMD 顯卡在追趕的過程,社群的力量看起來會是主力...

OpenLLM,用 Python 包裝 open source LLM 的套件

Hacker News 上看到「OpenLLM (github.com/bentoml)」,是一個用 Python 寫的軟體,把 open source LLM 包裝起來讓你用。

先拿 Mac 簡單測了一下,看起來包的不錯,可以用 HTTP API 來打。

先用 pip 裝:

pip install openllm

然後就可以把 server 跑起來了,依照範例跑 dolly-v2,第一次跑會比較久,需要下載 model:

openllm start dolly-v2

接下來就可以直接開 http://127.0.0.1:3000/ 來操作了,另外也可以用 command line 跑,像是依照官方的範例來跑:

openllm query --endpoint http://127.0.0.1:3000 "What is the meaning of life?"

目前測到比較明顯的問題是 CPU 模式下只有 single thread,所以雖然會動,但相當慢... 之後再來測試 GPU 的部分。

很多 MTurk 的接案者都用 LLM 在解決文字類的問題

剛剛在 Hacker News 上翻到的:「33-46% of workers on MTurk used LLMs in a text production task (arxiv.org)」,論文在「Artificial Artificial Artificial Intelligence: Crowd Workers Widely Use Large Language Models for Text Production Tasks」這邊,這個標題取的很故意... XD

Hacker News 上的標題主要是出自論文 abstract 的這段:

We reran an abstract summarization task from the literature on Amazon Mechanical Turk and, through a combination of keystroke detection and synthetic text classification, estimate that 33-46% of crowd workers used LLMs when completing the task.

想想還蠻正常的?能輕鬆賺當然就輕鬆賺... 但這也代表開發者可以思考 offload 給 LLM 的品質,以及如果需要外部的工人智慧,是不是可以搭配 LLM 再 offload 一些簡單的處理給人類就好?

話說好久沒聽到 MTurk 這個服務了,翻了 wiki 看起來是 2005 年就有的服務。

AWS 把 Falcon 40B 丟進 SageMaker 服務了

AWS 宣布在 SageMaker JumpStart 裡面可以用 Falcon 40B 了:「Falcon 40B foundation model from TII available on SageMaker JumpStart」,不愧是 AWS...

話說 llama.cpp 對 Falcon 的支援好像有點卡關的感覺,大概還要再折騰一陣子吧,雖然有些人已經能跑 7B 了,但大家還是想跑 40B 看看...:「Falcon LLM Support #1602」。

Apache License 2.0 的 RedPajama 7B 釋出

LLaMA 出來以後,打造 open source license 的 LLM 變成大家期待的事情,而 RedPajama 算是蠻多人看好的項目。

結果還在算的過程中間,路上殺出來 Falcon LLM,在釋出當下以一個比較寬鬆的 license (但還不是 open source license),到了六月初直接宣布改用 Apache License, Version 2.0,而且同時放出 7B 與 40B 兩個 model,讓 RedPajama 的消息瞬間被壓下去...

現在 RedPajama 放出 7B 了,而且也宣稱在 HELM 上比 Falcon 7B 好:「RedPajama 7B now available, instruct model outperforms all open 7B models on HELM benchmarks」,在 Hacker News 上對應的討論串在「RedPajama 7B (an Apache 2.0-licensed LLaMa) is now available (together.xyz)」這邊。

不過從這幾個月社群討論的感覺,可以看到大家都覺得 7B 太小了,目前大家都希望是 3090/4090 等級可以跑的顯示卡在當標準,差不多會是 LLaMA 13B 或是 30B (4-bit) 的 model。

這幾個月的競爭太激烈,放話完還沒 release 就被幹掉...