大模型高效微调详解-从Adpter、PrefixTuning到LoRA

大模型高效微调详解-从Adpter、PrefixTuning到LoRA 一、配置环境1、打开colab创建一个空白notebook在[修改运行时环境]中选择15GB显存的T4 GPU.2、pip安装依赖python包!pip install--upgrade accelerate !pip install bitsandbytes transformers_stream_generator!pip install transformers !pip install sentencepiece !pip install torch !pip install accelerate注意此时安装完accelerate后需要重启notebook不然报如下错误ImportError: Usinglow_cpu_mem_usageTrueor adevice_maprequires Accelerate:pip install accelerate注参考文章内容[1]不能直接运行二、模型推理运行加载模型代码importaccelerateimporttorchfromtransformersimportAutoTokenizer,AutoModelForCausalLM,TextStreamer# 待加载的预模型model_pathLinkSoul/Chinese-Llama-2-7b-4bit# 分词器tokenizerAutoTokenizer.from_pretrained(model_path,use_fastFalse)modelAutoModelForCausalLM.from_pretrained(model_path,load_in_4bitTrue,torch_dtypetorch.float16,device_mapauto)streamerTextStreamer(tokenizer,skip_promptTrue,skip_special_tokensTrue)instruction[INST] SYS\nYou are a helpful, respectful and honest assistant. Always answer as helpfully as possible, while being safe. Your answers should not include any harmful, unethical, racist, sexist, toxic, dangerous, or illegal content. Please ensure that your responses are socially unbiased and positive in nature. If a question does not make any sense, or is not factually coherent, explain why instead of answering something not correct. If you dont know the answer to a question, please dont share false information.\n/SYS\n\n{} [/INST]下载模型需要耗费一点时间You are using the default legacy behaviour of theclasstransformers.models.llama.tokenization_llama.LlamaTokenizer.Thisisexpected,andsimply means that the legacy(previous)behavior will be used so nothing changesforyou.If you want to use the new behaviour,setlegacyFalse.This should only besetifyou understand what it means,andthouroughly read the reason why this was addedasexplainedinhttps://github.com/huggingface/transformers/pull/24565Downloading(…)model.bin.index.json:100%26.8k/26.8k[00:0000:00,1.13MB/s]Downloading shards:0%0/2[00:00?,?it/s]Downloading(…)l-00001-of-00002.bin:100%9.97G/9.98G[04:5800:00,38.5MB/s]Downloading(…)l-00002-of-00002.bin:0%||0.00/3.50G[00:00?,?B/s]Loading checkpoint shards:0%||0/2[00:00?,?it/s]Downloading(…)neration_config.json:100%132/132[00:0000:00,4.37kB/s]demo1promptinstruction.format(What is the meaning of life)generate_idsmodel.generate(tokenizer(prompt,return_tensorspt).input_ids.cuda(),max_new_tokens4096,streamerstreamer)输出/usr/local/lib/python3.10/dist-packages/transformers/generation/utils.py:1421:UserWarning:You have modified the pretrained model configuration to control generation.Thisisa deprecated strategy to control generationandwill be removed soon,ina future version.Please useandmodify the model generation configuration(see https://huggingface.co/docs/transformers/generation_strategies#default-text-generation-configuration )warnings.warn(/usr/local/lib/python3.10/dist-packages/bitsandbytes/nn/modules.py:224:UserWarning:Inputtypeinto Linear4bitistorch.float16,but bnb_4bit_compute_typetorch.float32(default).This will lead to slow inferenceortraining speed.warnings.warn(fInput type into Linear4bit is torch.float16, but bnb_4bit_compute_typetorch.float32 (default). This will lead to slow inference or training speed.)The meaning of lifeisa philosophical question that has been debatedforcenturies.Thereisno one definitive answer,asdifferent peopleandcultures may have different beliefsandvalues.Some people believe that the meaning of lifeisto seek happiness,whileothers believe that itisto fulfill a higher purposeorto serve a greater good.Ultimately,the meaning of lifeisa personalandsubjective question that each individual must answerforthemselves.demo2promptinstruction.format(如何做个不拖延的人)generate_idsmodel.generate(tokenizer(prompt,return_tensorspt).input_ids.cuda(),max_new_tokens4096,streamerstreamer)输出答案不拖延的人是一个很好的目标但是要成为一个不拖延的人并不容易。以下是一些建议可以帮助你成为一个不拖延的人1.制定计划制定一个详细的计划包括每天要完成的任务和时间表。这样可以帮助你更好地组织时间并避免拖延。2.设定目标设定个明确的目标并制定一个实现这个目标的计划。这样可以帮助你更好地了解自己的目标并更有动力地去完成任务。3.克服拖延的心理延的心理是一个常见的问题但是可以通过一些方法克服。例如你可以尝试使用一些技巧来克服拖延如分解任务、使用时间管理工具等。4.坚持自己的计划坚持自己的计划是非常重要的。如果你经常拖延那么你需要坚持自己的计划并尽可能地按照计划去完成任务5.寻求帮助最后的最后感谢你们的阅读和喜欢我收藏了很多技术干货可以共享给喜欢我文章的朋友们如果你肯花时间沉下心去学习它们一定能帮到你。因为这个行业不同于其他行业知识体系实在是过于庞大知识更新也非常快。作为一个普通人无法全部学完所以我们在提升技术的时候首先需要明确一个目标然后制定好完整的计划同时找到好的学习方法这样才能更快的提升自己。这份完整版的大模型 AI 学习资料已经上传CSDN朋友们如果需要可以微信扫描下方CSDN官方认证二维码免费领取【保证100%免费】大模型知识脑图为了成为更好的 AI大模型 开发者这里为大家提供了总的路线图。它的用处就在于你可以按照上面的知识点去找对应的学习资源保证自己学得较为全面。经典书籍阅读阅读AI大模型经典书籍可以帮助读者提高技术水平开拓视野掌握核心技术提高解决问题的能力同时也可以借鉴他人的经验。对于想要深入学习AI大模型开发的读者来说阅读经典书籍是非常有必要的。实战案例光学理论是没用的要学会跟着一起敲要动手实操才能将自己的所学运用到实际当中去这时候可以搞点实战案例来学习。面试资料我们学习AI大模型必然是想找到高薪的工作下面这些面试题都是总结当前最新、最热、最高频的面试题并且每道题都有详细的答案面试前刷完这套面试题资料小小offer不在话下640套AI大模型报告合集这套包含640份报告的合集涵盖了AI大模型的理论研究、技术实现、行业应用等多个方面。无论您是科研人员、工程师还是对AI大模型感兴趣的爱好者这套报告合集都将为您提供宝贵的信息和启示。这份完整版的大模型 AI 学习资料已经上传CSDN朋友们如果需要可以微信扫描下方CSDN官方认证二维码免费领取【保证100%免费】