首页 > 其他分享 >LLM实践-在Colab上使用免费T4 GPU进行Chinese-Llama-2-7b-4bit推理

LLM实践-在Colab上使用免费T4 GPU进行Chinese-Llama-2-7b-4bit推理

时间:2023-10-06 23:34:33浏览次数:43  
标签:Llama Chinese generation 4bit torch install LLM pip model

一、配置环境

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: Using low_cpu_mem_usage=True or a device_map requires Accelerate: pip install accelerate

注:参考文章内容[1]不能直接运行

二、模型推理

运行加载模型代码

import accelerate
import torch
from transformers import AutoTokenizer, AutoModelForCausalLM, TextStreamer

# 待加载的预模型
model_path = "LinkSoul/Chinese-Llama-2-7b-4bit"

# 分词器
tokenizer = AutoTokenizer.from_pretrained(model_path, use_fast=False)
model = AutoModelForCausalLM.from_pretrained(
        model_path,
        load_in_4bit=True,
        torch_dtype=torch.float16,
        device_map='auto'
    )
streamer = TextStreamer(tokenizer, skip_prompt=True, skip_special_tokens=True)        

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 don't know the answer to a question, please don't share false information.\n<</SYS>>\n\n{} [/INST]"""

下载模型需要耗费一点时间

You are using the default legacy behaviour of the <class 'transformers.models.llama.tokenization_llama.LlamaTokenizer'>. This is expected, and simply means that the `legacy` (previous) behavior will be used so nothing changes for you. If you want to use the new behaviour, set `legacy=False`. This should only be set if you understand what it means, and thouroughly read the reason why this was added as explained in https://github.com/huggingface/transformers/pull/24565
Downloading (…)model.bin.index.json: 100%
26.8k/26.8k [00:00<00: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:58<00: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:00<00:00, 4.37kB/s]

demo1

prompt = instruction.format("What is the meaning of life")
generate_ids = model.generate(tokenizer(prompt, return_tensors='pt').input_ids.cuda(), max_new_tokens=4096, streamer=streamer)

输出:

/usr/local/lib/python3.10/dist-packages/transformers/generation/utils.py:1421: UserWarning: You have modified the pretrained model configuration to control generation. This is a deprecated strategy to control generation and will be removed soon, in a future version. Please use and modify 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: Input type into Linear4bit is torch.float16, but bnb_4bit_compute_type=torch.float32 (default). This will lead to slow inference or training speed.
  warnings.warn(f'Input type into Linear4bit is torch.float16, but bnb_4bit_compute_type=torch.float32 (default). This will lead to slow inference or training speed.')
  
The meaning of life is a philosophical question that has been debated for centuries. There is no one definitive answer, as different people and cultures may have different beliefs and values. Some people believe that the meaning of life is to seek happiness, while others believe that it is to fulfill a higher purpose or to serve a greater good. Ultimately, the meaning of life is a personal and subjective question that each individual must answer for themselves.

demo2

prompt = instruction.format("如何做个不拖延的人")
generate_ids = model.generate(tokenizer(prompt, return_tensors='pt').input_ids.cuda(), max_new_tokens=4096, streamer=streamer)

输出:

答案:不拖延的人是一个很好的目标,但是要成为一个不拖延的人并不容易。以下是一些建议,可以帮助你成为一个不拖延的人:

1. 制定计划:制定一个详细的计划,包括每天要完成的任务和时间表。这样可以帮助你更好地组织时间,并避免拖延。
2. 设定目标:设定个明确的目标,并制定一个实现这个目标的计划。这样可以帮助你更好地了解自己的目标,并更有动力地去完成任务。
3. 克服拖延的心理延的心理是一个常见的问题,但是可以通过一些方法克服。例如,你可以尝试使用一些技巧来克服拖延,如分解任务、使用时间管理工具等。
4. 坚持自己的计划:坚持自己的计划是非常重要的。如果你经常拖延,那么你需要坚持自己的计划,并尽可能地按照计划去完成任务5. 寻求帮助

三、参考链接

[1] Llama-2-7b-4bit推理 https://www.bilibili.com/read/cv25258378/
[2] 原始Kaggle Notebook链接:https://www.kaggle.com/code/tiansztianszs/chinese-llama-2-7b-4bit/notebook

标签:Llama,Chinese,generation,4bit,torch,install,LLM,pip,model
From: https://www.cnblogs.com/justLittleStar/p/17745291.html

相关文章

  • 论文阅读:iterator zero-shot llm prompting for knowledge graph construction
    Abstract知识图谱,一种相互连接和可解释的结构。生成需要更多的人力、领域知识、并需要适用于不同的应用领域。本论文提出借助LLM,通过0-shot和外部知识不可知的情况下生成知识图谱。主要贡献:迭代的prompting提取最终图的相关部分0-shot,不需要examples一个可扩展的解决方案,......
  • 解密Prompt系列16. LLM对齐经验之数据越少越好?LTD & LIMA & AlpaGasus
    LLMAgent中间插个队,总结下指令微调、对齐数据相关的方案,已经凑够7篇论文可以召唤神龙啦!论文都是以优化指令样本为核心,Data-Centric的观点比较一致:指令微调也就是对齐阶段的数据质量>>数量,少量+多样+高质量的对齐数据,就能让你快速拥有效果杠杠的模型。注意以上三者是充分必要关系,......
  • 大语言模型LLM推理及训练显存计算方法
    一、推理:显存计算推理的显存大头就是:参数量,参数类型版本一般有以下四种: float32位浮点数4字节half/BF1616位浮点数2字节int88位整数1字节int44位整数0.5字节 以7B-BF16版本为例,需要显存=数量*类型大小=70亿*2字节=140亿字节 所以140亿......
  • Llama2-Chinese项目:3.2-LoRA微调和模型量化
      提供LoRA微调和全量参数微调代码,训练数据为data/train_sft.csv,验证数据为data/dev_sft.csv,数据格式为"<s>Human:"+问题+"\n</s><s>Assistant:"+答案。本文主要介绍Llama-2-7b模型LoRA微调以及4bit量化的实践过程。1.LoRA微调脚本  LoRA微调脚本train/sft/finetune_lora......
  • 手把手教你在Ubuntu上部署中文LLAMA-2大模型
     一、前言 llama2作为目前最优秀的的开源大模型,相较于chatGPT,llama2占用的资源更少,推理过程更快,本文将借助llama.cpp工具在ubuntu(x86\ARM64)平台上搭建纯CPU运行的中文LLAMA2中文模型。二、准备工作 1、一个Ubuntu环境(本教程基于Ubuntu20LTS版操作) 2、确保你的环境可......
  • 全新注意力算法PagedAttention:LLM吞吐量提高2-4倍,模型越大效果越好
    前言 吞吐量上不去有可能是内存背锅!无需修改模型架构,减少内存浪费就能提高吞吐量!本文转载自新智元仅用于学术分享,若侵权请联系删除欢迎关注公众号CV技术指南,专注于计算机视觉的技术总结、最新技术跟踪、经典论文解读、CV招聘信息。CV各大方向专栏与各个部署框架最全教程整理......
  • Llama2-Chinese项目:2.3-预训练使用QA还是Text数据集?
      Llama2-Chinese项目给出pretrain的data为QA数据格式,可能会有疑问pretrain不应该是Text数据格式吗?而在Chinese-LLaMA-Alpaca-2和open-llama2预训练使用的LoRA技术,给出pretrain的data为Text数据格式。所以推测应该pretrain时QA和Text数据格式都应该支持。然后马上就会有一个疑问......
  • Llama2-Chinese项目:3.1-全量参数微调
      提供LoRA微调和全量参数微调代码,训练数据为data/train_sft.csv,验证数据为data/dev_sft.csv,数据格式如下所示:"<s>Human: "+问题+"\n</s><s>Assistant: "+答案  举个例子,如下所示:<s>Human: 用一句话描述地球为什么是独一无二的。</s><s>Assistant: 因为地球是目前为止......
  • Llama2-Chinese项目:2.1-Atom-7B预训练
    虽然Llama2的预训练数据相对于第一代LLaMA扩大了一倍,但是中文预训练数据的比例依然非常少,仅占0.13%,这也导致了原始Llama2的中文能力较弱。为了能够提升模型的中文能力,可以采用微调和预训练两种路径,其中:微调需要的算力资源少,能够快速实现一个中文Llama的雏形。但缺点也显而易见,只......
  • Llama2-Chinese项目:1-项目介绍和模型推理
    Atom-7B与Llama2间的关系:Atom-7B是基于Llama2进行中文预训练的开源大模型。为什么叫原子呢?因为原子生万物,Llama中文社区希望原子大模型未来可以成为构建AI世界的基础单位。目前社区发布了6个模型,如下所示:FlagAlpha/Atom-7BFlagAlpha/Llama2-Chinese-7b-ChatFlagAlpha/Llama2-Ch......