LeCun轉讚:在蘋果M1/M2晶片上跑LLaMA! 130億參數模型僅需4GB內存
前不久,Meta前腳發布完開源大語言模型LLaMA,後腳就被網友放出了無門檻下載鏈接,“慘遭”開放。
訊息一出,圈內瞬間就熱鬧了起來,大家紛紛開始下載測試。
但那些手邊沒有頂級顯示卡的朋友們,就只能望模型興嘆了。
不過,問題不大。 Georgi Gerganov在最近做了一個名為「llama.cpp」的計畫-沒有GPU也能跑LLaMA。
計畫網址:https://github.com/ggerganov/llama.cpp
是的,這也包括搭載了蘋果晶片的Mac。並且也獲得了LeCun的轉發支援。
在M1/M2的Mac上跑LLaMA
目前來說,比較全面的教學有兩個,分別以蘋果的M1和M2處理器為基礎。
第一篇:https://dev.l1x.be/posts/2023/03/ 12/using-llama-with-m1-mac/
#第二篇:https ://til.simonwillison.net/llms/llama-7b-m2
#話不多說,先看效果。
例如在最小的7B模型上提問:「第一個登上月球的人是」。
很快,幾秒之後,結果就出來了。
-p '第一個登上月球的人是'
第一個登上月球的人是38歲的太空人Neil A. Armstrong。
1969年7月20日,阿波羅11號在月球上著陸。
阿波羅11號是第一個登陸月球的載人任務,也是太空競賽的高潮。 1969年7月,Neil Armstrong和Edwin "Buzz" Aldrin成為第一批在月球上登陸的人類。
阿波羅11號於7月16日從甘迺迪太空中心發射。
從阿姆斯壯登月的年齡、中間名和日期來看,並沒有出現明顯的事實性錯誤。這對於如此體量的小模型來說,實屬不易。
如果使用下面這個提示,可以產生了一些實用的Python程式碼。
-p 'def open_and_return_content(filename):'
def open_and_return_content(filename): """Opens file (returning the content) and performs basic sanity checks"""if os.path.isfile(filename): with open(filename) as f: content = f.read() return contentelse: print('WARNING: file "{}" does not exist'.format(filename), file=sys.stderr) return ''def get_file_info(filename, fullpath): """Get file information (i.e., permission, owner, group, size)"""
接下来,我们就来看看具体是如何实现的。
第一步:下載模型
首先要做的就是下載LLaMA模型。
你可以透過官方的表格向Meta提交申請,或是從網友分享的連結直接取得。
總之,完成後你會看到下面這堆東西:
正如你所看到的,不同的模型都在不同的資料夾裡。每個模型都有一個params.json,包含關於該模型的細節。例如:
第二步:安裝依賴項
首先,你需要安装Xcode来编译C++项目。
xcode-select --install
接下来,是构建C++项目的依赖项(pkgconfig和cmake)。
brew install pkgconfig cmake
在环境的配置上,假如你用的是Python 3.11,则可以创建一个虚拟环境:
/opt/homebrew/bin/python3.11 -m venv venv
然后激活venv。(如果是fish以外的shell,只要去掉.fish后缀即可)
. venv/bin/activate.fish
最后,安装Torch。
pip3 install --pre torch torchvision --extra-index-url https://download.pytorch.org/whl/nightly/cpu
如果你对利用新的Metal性能着色器(MPS)后端进行GPU训练加速感兴趣,可以通过运行以下程序来进行验证。但这不是在M1上运行LLaMA的必要条件。
python Python 3.11.2 (main, Feb 16 2023, 02:55:59) [Clang 14.0.0 (clang-1400.0.29.202)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> import torch; torch.backends.mps.is_available()True
第三步:编译LLaMA CPP
git clone git@github.com:ggerganov/llama.cpp.git
在安装完所有的依赖项后,你可以运行make:
make I llama.cpp build info: I UNAME_S:Darwin I UNAME_P:arm I UNAME_M:arm64 I CFLAGS: -I.-O3 -DNDEBUG -std=c11 -fPIC -pthread -DGGML_USE_ACCELERATE I CXXFLAGS: -I. -I./examples -O3 -DNDEBUG -std=c++11 -fPIC -pthread I LDFLAGS: -framework Accelerate I CC: Apple clang version 14.0.0 (clang-1400.0.29.202)I CXX:Apple clang version 14.0.0 (clang-1400.0.29.202) cc-I.-O3 -DNDEBUG -std=c11 -fPIC -pthread -DGGML_USE_ACCELERATE -c ggml.c -o ggml.o c++ -I. -I./examples -O3 -DNDEBUG -std=c++11 -fPIC -pthread -c utils.cpp -o utils.o c++ -I. -I./examples -O3 -DNDEBUG -std=c++11 -fPIC -pthread main.cpp ggml.o utils.o -o main-framework Accelerate ./main -h usage: ./main [options] options: -h, --helpshow this help message and exit -s SEED, --seed SEEDRNG seed (default: -1) -t N, --threads N number of threads to use during computation (default: 4) -p PROMPT, --prompt PROMPT prompt to start generation with (default: random) -n N, --n_predict N number of tokens to predict (default: 128) --top_k N top-k sampling (default: 40) --top_p N top-p sampling (default: 0.9) --temp Ntemperature (default: 0.8) -b N, --batch_size Nbatch size for prompt processing (default: 8) -m FNAME, --model FNAME model path (default: models/llama-7B/ggml-model.bin) c++ -I. -I./examples -O3 -DNDEBUG -std=c++11 -fPIC -pthread quantize.cpp ggml.o utils.o -o quantize-framework Accelerate
第四步:转换模型
假设你已经把模型放在llama.cpp repo中的models/下。
python convert-pth-to-ggml.py models/7B 1
那么,应该会看到像这样的输出:
{'dim': 4096, 'multiple_of': 256, 'n_heads': 32, 'n_layers': 32, 'norm_eps': 1e-06, 'vocab_size': 32000}n_parts =1Processing part0Processing variable: tok_embeddings.weight with shape:torch.Size([32000, 4096])and type:torch.float16 Processing variable: norm.weight with shape:torch.Size([4096])and type:torch.float16 Converting to float32 Processing variable: output.weight with shape:torch.Size([32000, 4096])and type:torch.float16 Processing variable: layers.0.attention.wq.weight with shape:torch.Size([4096, 4096])and type:torch.f loat16 Processing variable: layers.0.attention.wk.weight with shape:torch.Size([4096, 4096])and type:torch.f loat16 Processing variable: layers.0.attention.wv.weight with shape:torch.Size([4096, 4096])and type:torch.f loat16 Processing variable: layers.0.attention.wo.weight with shape:torch.Size([4096, 4096])and type:torch.f loat16 Processing variable: layers.0.feed_forward.w1.weight with shape:torch.Size([11008, 4096])and type:tor ch.float16 Processing variable: layers.0.feed_forward.w2.weight with shape:torch.Size([4096, 11008])and type:tor ch.float16 Processing variable: layers.0.feed_forward.w3.weight with shape:torch.Size([11008, 4096])and type:tor ch.float16 Processing variable: layers.0.attention_norm.weight with shape:torch.Size([4096])and type:torch.float 16... Done. Output file: models/7B/ggml-model-f16.bin, (part0 )
下一步将是进行量化处理:
./quantize ./models/7B/ggml-model-f16.bin ./models/7B/ggml-model-q4_0.bin 2
输出如下:
llama_model_quantize: loading model from './models/7B/ggml-model-f16.bin'llama_model_quantize: n_vocab = 32000llama_model_quantize: n_ctx = 512llama_model_quantize: n_embd= 4096llama_model_quantize: n_mult= 256llama_model_quantize: n_head= 32llama_model_quantize: n_layer = 32llama_model_quantize: f16 = 1... layers.31.attention_norm.weight - [ 4096, 1], type =f32 size =0.016 MB layers.31.ffn_norm.weight - [ 4096, 1], type =f32 size =0.016 MB llama_model_quantize: model size= 25705.02 MB llama_model_quantize: quant size=4017.27 MB llama_model_quantize: hist: 0.000 0.022 0.019 0.033 0.053 0.078 0.104 0.125 0.134 0.125 0.104 0.078 0.053 0.033 0.019 0.022 main: quantize time = 29389.45 ms main:total time = 29389.45 ms
第五步:运行模型
./main -m ./models/7B/ggml-model-q4_0.bin -t 8 -n 128 -p 'The first president of the USA was '
main: seed = 1678615879llama_model_load: loading model from './models/7B/ggml-model-q4_0.bin' - please wait ... llama_model_load: n_vocab = 32000llama_model_load: n_ctx = 512llama_model_load: n_embd= 4096llama_model_load: n_mult= 256llama_model_load: n_head= 32llama_model_load: n_layer = 32llama_model_load: n_rot = 128llama_model_load: f16 = 2llama_model_load: n_ff= 11008llama_model_load: n_parts = 1llama_model_load: ggml ctx size = 4529.34 MB llama_model_load: memory_size = 512.00 MB, n_mem = 16384llama_model_load: loading model part 1/1 from './models/7B/ggml-model-q4_0.bin'llama_model_load: .................................... donellama_model_load: model size =4017.27 MB / num tensors = 291 main: prompt: 'The first president of the USA was 'main: number of tokens in prompt = 9 1 -> ''1576 -> 'The' 937 -> ' first'6673 -> ' president' 310 -> ' of' 278 -> ' the'8278 -> ' USA' 471 -> ' was' 29871 -> ' ' sampling parameters: temp = 0.800000, top_k = 40, top_p = 0.950000 The first president of the USA was 57 years old when he assumed office (George Washington). Nowadays, the US electorate expects the new president to be more young at heart. President Donald Trump was 70 years old when he was inaugurated. In contrast to his predecessors, he is physically fit, healthy and active. And his fitness has been a prominent theme of his presidency. During the presidential campaign, he famously said he would be the “most active president ever” — a statement Trump has not yet achieved, but one that fits his approach to the office. His tweets demonstrate his physical activity. main: mem per token = 14434244 bytes main: load time =1311.74 ms main: sample time = 278.96 ms main:predict time =7375.89 ms / 54.23 ms per token main:total time =9216.61 ms
资源使用情况
第二位博主表示,在运行时,13B模型使用了大约4GB的内存,以及748%的CPU。(设定的就是让模型使用8个CPU核心)
没有指令微调
GPT-3和ChatGPT效果如此之好的关键原因之一是,它们都经过了指令微调,
这种额外的训练使它们有能力对人类的指令做出有效的反应。比如「总结一下这个」或「写一首关于水獭的诗」或「从这篇文章中提取要点」。
撰写教程的博主表示,据他观察,LLaMA并没有这样的能力。
也就是说,给LLaMA的提示需要采用经典的形式:「一些将由......完成的文本」。这也让提示工程变得更加困难。
举个例子,博主至今都还没有想出一个正确的提示,从而让LLaMA实现文本的总结。
以上是LeCun轉讚:在蘋果M1/M2晶片上跑LLaMA! 130億參數模型僅需4GB內存的詳細內容。更多資訊請關注PHP中文網其他相關文章!

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

熱門話題

想像一下,一個人工智慧模型,不僅擁有超越傳統運算的能力,還能以更低的成本實現更有效率的效能。這不是科幻,DeepSeek-V2[1],全球最強開源MoE模型來了。 DeepSeek-V2是一個強大的專家混合(MoE)語言模型,具有訓練經濟、推理高效的特點。它由236B個參數組成,其中21B個參數用於啟動每個標記。與DeepSeek67B相比,DeepSeek-V2效能更強,同時節省了42.5%的訓練成本,減少了93.3%的KV緩存,最大生成吞吐量提高到5.76倍。 DeepSeek是一家探索通用人工智

AI,的確正在改變數學。最近,一直十分關注這個議題的陶哲軒,轉發了最近一期的《美國數學學會通報》(BulletinoftheAmericanMathematicalSociety)。圍繞著「機器會改變數學嗎?」這個話題,許多數學家發表了自己的觀點,全程火花四射,內容硬核,精彩紛呈。作者陣容強大,包括菲爾茲獎得主AkshayVenkatesh、華裔數學家鄭樂雋、紐大電腦科學家ErnestDavis等多位業界知名學者。 AI的世界已經發生了天翻地覆的變化,要知道,其中許多文章是在一年前提交的,而在這一

波士頓動力Atlas,正式進入電動機器人時代!昨天,液壓Atlas剛「含淚」退出歷史舞台,今天波士頓動力就宣布:電動Atlas上崗。看來,在商用人形機器人領域,波士頓動力是下定決心要跟特斯拉硬剛一把了。新影片放出後,短短十幾小時內,就已經有一百多萬觀看。舊人離去,新角色登場,這是歷史的必然。毫無疑問,今年是人形機器人的爆發年。網友銳評:機器人的進步,讓今年看起來像人類的開幕式動作、自由度遠超人類,但這真不是恐怖片?影片一開始,Atlas平靜地躺在地上,看起來應該是仰面朝天。接下來,讓人驚掉下巴

本月初,來自MIT等機構的研究者提出了一種非常有潛力的MLP替代方法—KAN。 KAN在準確性和可解釋性方面表現優於MLP。而且它能以非常少的參數量勝過以更大參數量運行的MLP。例如,作者表示,他們用KAN以更小的網路和更高的自動化程度重現了DeepMind的結果。具體來說,DeepMind的MLP有大約300,000個參數,而KAN只有約200個參數。 KAN與MLP一樣具有強大的數學基礎,MLP基於通用逼近定理,而KAN基於Kolmogorov-Arnold表示定理。如下圖所示,KAN在邊上具

谷歌力推的JAX在最近的基準測試中表現已經超過Pytorch和TensorFlow,7項指標排名第一。而且測試並不是JAX性能表現最好的TPU上完成的。雖然現在在開發者中,Pytorch依然比Tensorflow更受歡迎。但未來,也許有更多的大型模型會基於JAX平台進行訓練和運行。模型最近,Keras團隊為三個後端(TensorFlow、JAX、PyTorch)與原生PyTorch實作以及搭配TensorFlow的Keras2進行了基準測試。首先,他們為生成式和非生成式人工智慧任務選擇了一組主流

特斯拉機器人Optimus最新影片出爐,已經可以在工廠裡打工了。正常速度下,它分揀電池(特斯拉的4680電池)是這樣的:官方還放出了20倍速下的樣子——在小小的「工位」上,揀啊揀啊揀:這次放出的影片亮點之一在於Optimus在廠子裡完成這項工作,是完全自主的,全程沒有人為的干預。而且在Optimus的視角之下,它還可以把放歪了的電池重新撿起來放置,主打一個自動糾錯:對於Optimus的手,英偉達科學家JimFan給出了高度的評價:Optimus的手是全球五指機器人裡最靈巧的之一。它的手不僅有觸覺

目標偵測在自動駕駛系統當中是一個比較成熟的問題,其中行人偵測是最早得以部署演算法之一。在多數論文當中已經進行了非常全面的研究。然而,利用魚眼相機進行環視的距離感知相對來說研究較少。由於徑向畸變大,標準的邊界框表示在魚眼相機當中很難實施。為了緩解上述描述,我們探索了擴展邊界框、橢圓、通用多邊形設計為極座標/角度表示,並定義一個實例分割mIOU度量來分析這些表示。所提出的具有多邊形形狀的模型fisheyeDetNet優於其他模型,並同時在用於自動駕駛的Valeo魚眼相機資料集上實現了49.5%的mAP

這篇論文探討了在自動駕駛中,從不同視角(如透視圖和鳥瞰圖)準確檢測物體的問題,特別是如何有效地從透視圖(PV)到鳥瞰圖(BEV)空間轉換特徵,這一轉換是透過視覺轉換(VT)模組實施的。現有的方法大致分為兩種策略:2D到3D和3D到2D轉換。 2D到3D的方法透過預測深度機率來提升密集的2D特徵,但深度預測的固有不確定性,尤其是在遠處區域,可能會引入不準確性。而3D到2D的方法通常使用3D查詢來採樣2D特徵,並透過Transformer學習3D和2D特徵之間對應關係的注意力權重,這增加了計算和部署的
