科技之巅 编辑:好困 【新智元导读】现在,Meta最新的大语言模型LLaMA,可以在搭载苹果芯片的Mac上跑了! 前不久,Meta前脚发布完开源大语言模型LLaMA,后脚就被网友放出了无门槛下载链接,惨遭开放。 消息一出,圈内瞬间就热闹了起来,大家纷纷开始下载测试。 但那些手头没有顶级显卡的朋友们,就只能望模型兴叹了。 不过,问题不大。GeorgiGerganov在最近做了一个名为llama。cpp的项目没有GPU也能跑LLaMA。 项目地址:https:github。comggerganovllama。cpp 是的,这也包括搭载了苹果芯片的Mac。并且还获得了LeCun的转发支持。 在M1M2的Mac上跑LLaMA 目前来说,比较全面的教程有两个,分别基于苹果的M1和M2处理器。 第一篇:https:dev。l1x。beposts20230312usingllamawithm1mac 第二篇:https:til。simonwillison。netllmsllama7bm2 话不多说,先看效果。 比如在最小的7B模型上提问:第一个登上月球的人是。 很快,几秒之后,结果就出来了。 p第一个登上月球的人是 第一个登上月球的人是38岁的宇航员NeilA。Armstrong。 1969年7月20日,阿波罗11号在月球上着陆。 阿波罗11号是第一个登陆月球的载人任务,也是太空竞赛的高潮。1969年7月,NeilArmstrong和EdwinBuzzAldrin成为第一批在月球上登陆的人类。 阿波罗11号于7月16日从肯尼迪航天中心发射。 从阿姆斯特朗登月的年龄、中间名和日期来看,并没有出现明显的事实性错误。这对于如此体量的小模型来说,实属不易。 如果用下面这个提示,则可以生成了一些实用的Python代码。pdefopenandreturncontent(filename):defopenandreturncontent(filename):Opensfile(returningthecontent)andperformsbasicsanitychecksifos。path。isfile(filename):withopen(filename)asf:contentf。read()returncontentelse:print(WARNING:file{}doesnotexist。format(filename),filesys。stderr)returndefgetfileinfo(filename,fullpath):Getfileinformation(i。e。,permission,owner,group,size)接下来,我们就来看看具体是如何实现的。 第一步:下载模型 首先要做的就是下载LLaMA模型。 你可以通过官方的表格向Meta提交申请,或者从网友分享的链接里直接获取。 总之,完成后你会看到下面这堆东西: 正如你所看到的,不同的模型都在不同的文件夹里。每个模型都有一个params。json,包含关于该模型的细节。比如: 第二步:安装依赖项 首先,你需要安装Xcode来编译C项目。xcodeselectinstall 接下来,是构建C项目的依赖项(pkgconfig和cmake)。brewinstallpkgconfigcmake 在环境的配置上,假如你用的是Python3。11,则可以创建一个虚拟环境:opthomebrewbinpython3。11mvenvvenv 然后激活venv。(如果是fish以外的shell,只要去掉。fish后缀即可)。venvbinactivate。fish 最后,安装Torch。pip3installpretorchtorchvisionextraindexurlhttps:download。pytorch。orgwhlnightlycpu 如果你对利用新的Metal性能着色器(MPS)后端进行GPU训练加速感兴趣,可以通过运行以下程序来进行验证。但这不是在M1上运行LLaMA的必要条件。pythonPython3。11。2(main,Feb162023,02:55:59)〔Clang14。0。0(clang1400。0。29。202)〕ondarwinTypehelp,copyright,creditsorlicenseformoreinformation。importtorch;torch。backends。mps。isavailable()True 第三步:编译LLaMACPP gitclonegitgithub。com:ggerganovllama。cpp。git 在安装完所有的依赖项后,你可以运行make:makeIllama。cppbuildinfo:IUNAMES:DarwinIUNAMEP:armIUNAMEM:arm64ICFLAGS:I。O3DNDEBUGstdc11fPICpthreadDGGMLUSEACCELERATEICXXFLAGS:I。I。examplesO3DNDEBUGstdc11fPICpthreadILDFLAGS:frameworkAccelerateICC:Appleclangversion14。0。0(clang1400。0。29。202)ICXX:Appleclangversion14。0。0(clang1400。0。29。202)ccI。O3DNDEBUGstdc11fPICpthreadDGGMLUSEACCELERATEcggml。coggml。ocI。I。examplesO3DNDEBUGstdc11fPICpthreadcutils。cppoutils。ocI。I。examplesO3DNDEBUGstdc11fPICpthreadmain。cppggml。outils。oomainframeworkAccelerate。mainhusage:。main〔options〕options:h,helpshowthishelpmessageandexitsSEED,seedSEEDRNGseed(default:1)tN,threadsNnumberofthreadstouseduringcomputation(default:4)pPROMPT,promptPROMPTprompttostartgenerationwith(default:random)nN,npredictNnumberoftokenstopredict(default:128)topkNtopksampling(default:40)toppNtoppsampling(default:0。9)tempNtemperature(default:0。8)bN,batchsizeNbatchsizeforpromptprocessing(default:8)mFNAME,modelFNAMEmodelpath(default:modelsllama7Bggmlmodel。bin)cI。I。examplesO3DNDEBUGstdc11fPICpthreadquantize。cppggml。outils。ooquantizeframeworkAccelerate 第四步:转换模型 假设你已经把模型放在llama。cpprepo中的models下。pythonconvertpthtoggml。pymodels7B1 那么,应该会看到像这样的输出:{dim:4096,multipleof:256,nheads:32,nlayers:32,normeps:1e06,vocabsize:32000}nparts1Processingpart0Processingvariable:tokembeddings。weightwithshape:torch。Size(〔32000,4096〕)andtype:torch。float16Processingvariable:norm。weightwithshape:torch。Size(〔4096〕)andtype:torch。float16Convertingtofloat32Processingvariable:output。weightwithshape:torch。Size(〔32000,4096〕)andtype:torch。float16Processingvariable:layers。0。attention。wq。weightwithshape:torch。Size(〔4096,4096〕)andtype:torch。float16Processingvariable:layers。0。attention。wk。weightwithshape:torch。Size(〔4096,4096〕)andtype:torch。float16Processingvariable:layers。0。attention。wv。weightwithshape:torch。Size(〔4096,4096〕)andtype:torch。float16Processingvariable:layers。0。attention。wo。weightwithshape:torch。Size(〔4096,4096〕)andtype:torch。float16Processingvariable:layers。0。feedforward。w1。weightwithshape:torch。Size(〔11008,4096〕)andtype:torch。float16Processingvariable:layers。0。feedforward。w2。weightwithshape:torch。Size(〔4096,11008〕)andtype:torch。float16Processingvariable:layers。0。feedforward。w3。weightwithshape:torch。Size(〔11008,4096〕)andtype:torch。float16Processingvariable:layers。0。attentionnorm。weightwithshape:torch。Size(〔4096〕)andtype:torch。float16。。。Done。Outputfile:models7Bggmlmodelf16。bin,(part0) 下一步将是进行量化处理:。quantize。models7Bggmlmodelf16。bin。models7Bggmlmodelq40。bin2 输出如下:llamamodelquantize:loadingmodelfrom。models7Bggmlmodelf16。binllamamodelquantize:nvocab32000llamamodelquantize:nctx512llamamodelquantize:nembd4096llamamodelquantize:nmult256llamamodelquantize:nhead32llamamodelquantize:nlayer32llamamodelquantize:f161。。。layers。31。attentionnorm。weight〔4096,1〕,typef32size0。016MBlayers。31。ffnnorm。weight〔4096,1〕,typef32size0。016MBllamamodelquantize:modelsize25705。02MBllamamodelquantize:quantsize4017。27MBllamamodelquantize:hist:0。0000。0220。0190。0330。0530。0780。1040。1250。1340。1250。1040。0780。0530。0330。0190。022main:quantizetime29389。45msmain:totaltime29389。45ms 第五步:运行模型 。mainm。models7Bggmlmodelq40。bint8n128pThefirstpresidentoftheUSAwasmain:seed1678615879llamamodelload:loadingmodelfrom。models7Bggmlmodelq40。binpleasewait。。。llamamodelload:nvocab32000llamamodelload:nctx512llamamodelload:nembd4096llamamodelload:nmult256llamamodelload:nhead32llamamodelload:nlayer32llamamodelload:nrot128llamamodelload:f162llamamodelload:nff11008llamamodelload:nparts1llamamodelload:ggmlctxsize4529。34MBllamamodelload:memorysize512。00MB,nmem16384llamamodelload:loadingmodelpart11from。models7Bggmlmodelq40。binllamamodelload:。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。donellamamodelload:modelsize4017。27MBnumtensors291main:prompt:ThefirstpresidentoftheUSAwasmain:numberoftokensinprompt911576The937first6673president310of278the8278USA471was29871samplingparameters:temp0。800000,topk40,topp0。950000ThefirstpresidentoftheUSAwas57yearsoldwhenheassumedoffice(GeorgeWashington)。Nowadays,theUSelectorateexpectsthenewpresidenttobemoreyoungatheart。PresidentDonaldTrumpwas70yearsoldwhenhewasinaugurated。Incontrasttohispredecessors,heisphysicallyfit,healthyandactive。Andhisfitnesshasbeenaprominentthemeofhispresidency。Duringthepresidentialcampaign,hefamouslysaidhewouldbethemostactivepresidenteverastatementTrumphasnotyetachieved,butonethatfitshisapproachtotheoffice。Histweetsdemonstratehisphysicalactivity。main:mempertoken14434244bytesmain:loadtime1311。74msmain:sampletime278。96msmain:predicttime7375。89ms54。23mspertokenmain:totaltime9216。61ms 资源使用情况 第二位博主表示,在运行时,13B模型使用了大约4GB的内存,以及748的CPU。(设定的就是让模型使用8个CPU核心) 没有指令微调 GPT3和ChatGPT效果如此之好的关键原因之一是,它们都经过了指令微调, 这种额外的训练使它们有能力对人类的指令做出有效的反应。比如总结一下这个或写一首关于水獭的诗或从这篇文章中提取要点。 撰写教程的博主表示,据他观察,LLaMA并没有这样的能力。 也就是说,给LLaMA的提示需要采用经典的形式:一些将由。。。。。。完成的文本。这也让提示工程变得更加困难。 举个例子,博主至今都还没有想出一个正确的提示,从而让LLaMA实现文本的总结。 参考资料: https:github。comggerganovllama。cpp https:dev。l1x。beposts20230312usingllamawithm1mac https:til。simonwillison。netllmsllama7bm2