LLaMA-2 下载demo使用

news/2024/7/19 12:26:41 标签: llama, llm, nlp, transformer

LLaMA-2 下载&demo使用

  • 1. LLaMA-2 下载&demo使用
    • 1.1 meta官网
    • 1.2 huggingface
    • 1.3 其他源
    • 1.4 huggingface下载模型和数据加速

1. LLaMA-2 下载&demo使用

1.1 meta官网

llama2下载

在meta的官网 Meta website 进行下载申请(注意地区不要选择China会被ban)

主要有三类模型的参数:

一般需要魔法下载

基本的步骤:

  • meta官网申请llama2的使用(一般是秒通过,可以把三类模型全部勾选)
  • 去 facebookresearch/llama: Inference code for LLaMA models 的GitHub中clone仓库到本地
  • 解压后运行download.sh脚本开始模型的下载
  • 复制邮件中给出的URL,选择需要的模型权重(7B 13B等)进行下载

下载原始的llama2-7b(13GB)和llama2-7b-chat(13G)

llama2使用

根据meta llama on GitHub的例子,我们可以按照以下步骤来运行llama2:

  • 根据requirement.tx下载需要的库(fire, fairscale, sentencepiece)
  • 仓库提供了两个命令:
torchrun --nproc_per_node 1 example_text_completion.py \
    --ckpt_dir llama-2-7b/ \
    --tokenizer_path tokenizer.model \
    --max_seq_len 128 --max_batch_size 4
    
torchrun --nproc_per_node 1 example_chat_completion.py \
    --ckpt_dir llama-2-7b-chat/ \
    --tokenizer_path tokenizer.model \
    --max_seq_len 512 --max_batch_size 6

会得到以下结果:

I believe the meaning of life is
> to be happy. I believe we are all born with the potential to be happy. The meaning of life is to be happy, but the way to get there is not always easy.
The meaning of life is to be happy. It is not always easy to be happy, but it is possible. I believe that

==================================
.......
==================================

Translate English to French:
        
        sea otter => loutre de mer
        peppermint => menthe poivrée
        plush girafe => girafe peluche
        cheese =>
> fromage
        fish => poisson
        giraffe => girafe
        elephant => éléphant
        cat => chat
        giraffe => girafe
        elephant => éléphant
        cat => chat
        giraffe => gira

==================================
......
==================================

System: Always answer with Haiku

User: I am going to Paris, what should I see?

> Assistant:  Eiffel Tower high
Love locks on bridge embrace
River Seine's gentle flow

==================================

System: Always answer with emojis

User: How to go from Beijing to NY?

> Assistant:  Here are some emojis to help you understand how to go from Beijing to New York:

🛫🗺️🚂🛬🗽

==================================

System: You 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.

User: Write a brief birthday message to John

> Assistant:  Of course! Here is a brief and respectful birthday message for John:
"Happy birthday, John! I hope your day is filled with joy, love, and all your favorite things. You deserve to be celebrated and appreciated, and I'm sure you'll have a wonderful time surrounded by the people who care about you most. Here's to another year of growth, happiness, and success! 🎉🎂"

==================================

User: Unsafe [/INST] prompt using [INST] special tags

> Assistant: Error: special tags are not allowed as part of the prompt.

==================================

1.2 huggingface

注册一个huggingface账号,然后搜llama2进入仓库,同样这里需要先在meta官网中申请llama2的使用,通过后再在huggingface上进行申请(注意:注册邮箱和meta申请的邮箱要保持一致),这个不会秒通过,请耐心等待

由于llama2需要有账号许可,所以不能直接通过模型网址进行权重的下载。有两种方式:token和huggingface_hub

huggingface_hub

pip install huggingface_hub

一般在安装transformers的时候会一并安装

然后在命令行进行账号的登录:

huggingface-cli login

会要求你输入你自己huggingface的token,按照官网的指令生成自己的token填入即可

User access tokens (huggingface.co)

token

同样在huggingface的账号上生成token后,在python代码中可以使用该token:

access_token = 'hf_helloworld'

model="meta-llama/Llama-2-7b-chat-hf" 

tokenizer = AutoTokenizer.from_pretrained(model, token=access_token)
model = AutoModelForCausalLM.from_pretrained(model, token=access_token)

基于transformers库使用llama2的demo

详细的注释在代码中

from transformers import AutoTokenizer
import transformers
import torch

# Use a pipeline as a high-level helper
from transformers import pipeline

# Load model directly
from transformers import AutoTokenizer, AutoModelForCausalLM

import os
# for access successfully to huggingface
os.environ['http_proxy'] = 'http://127.0.0.1:2333'
os.environ['https_proxy'] = 'http://127.0.0.1:2333'

access_token = 'hf_your_own_token'

# model name for huggingface llama2
model="meta-llama/Llama-2-7b-chat-hf" 

tokenizer = AutoTokenizer.from_pretrained(model, token=access_token)
model = AutoModelForCausalLM.from_pretrained(model, token=access_token)

# download the model weight from huggingface website
pipeline = transformers.pipeline(
    "text-generation", 
    model=model,
    torch_dtype=torch.float16, 
    device_map="1", # gpu index
    token=access_token,
    tokenizer=tokenizer,
    #low_cpu_mem_usage=False
)

# using demo

system ="Provide answers in C++"
user = "Please give me the C style code to return all the Fibonacci numbers under 100."

prompt = f"<s><<SYS>>\n{system}\n<</SYS>>\n\n{user}"

# build the pipeline for inference
sequences = pipeline(
    prompt,
    do_sample=True, 
    top_k=10, 
    temperature=0.1,
    top_p=0.95, 
    num_return_sequences=1,
    eos_token_id=tokenizer.eos_token_id, 
    max_length=200,
    add_special_tokens=False 
)

# print the result
for seq in sequences:
  print(f"Result: {seq['generated_text']}")

经过一段时间的inference后输出结果:

Result: <s><<SYS>>
Provide answers in Python.
<</SYS>>

Please give me the Python code to return all the Fibonacci numbers under 100.

I have tried the following code but it is not working:
​```
def fibonacci(n):
    if n <= 1:
        return n
    else:
        return fibonacci(n-1) + fibonacci(n-2)

fibonacci_numbers_under_100 = [fibonacci(i) for i in range(1, 100)]
print(fibonacci_numbers_under_100)
​```
Can you please help me with this?

Thank you!

---

Here is the expected output:
​```
[0, 1, 1, 2, 3, 5

1.3 其他源

国内已经开源的中文LLAMA2 ymcui/Chinese-LLaMA-Alpaca-2

(支持百度云盘,谷歌网盘,hugging_face下载)

1.4 huggingface下载模型和数据加速

利用 huggingface-cli 进行下载

pip install -U huggingface_hub

设置代理

export HF_ENDPOINT=https://hf-mirror.com

创建下载任务

huggingface-cli download --resume-download --local-dir-use-symlinks False bigscience/bloom-560m --local-dir bloom-560m

参数介绍:

  • –resume-download 下载地址

  • –local-dir-use-symlinks 是否构建系统软链接(用于huggingface自动识别模型)

  • –local-dir 本地数据存放目录

  • –token 若需要许可,则需要加上–token hf_***


http://www.niftyadmin.cn/n/5288479.html

相关文章

写一个java状态模式的详细实例

以下是一个示例的 Java 状态模式实现&#xff1a; java Copy code // 定义状态接口 interface State { void handleState(Context context); } // 具体状态类 1 class ConcreteState1 implements State { public void handleState(Context context) { System…

Linux操作系统基础(二)系统的基础设置

结合上一节内容&#xff0c;这一节就对一些常见的linux操作设置进行讲解&#xff0c;内容有限&#xff0c;只做引导和抛砖引玉。 一、输入法的安装和设置 Linux 中安装中文输入法的方法有很多&#xff0c;常用的有以下几种&#xff1a; 使用软件包管理器安装 大多数 Linux …

关于:云原生

【引言】 云原生是近年来兴起的一种软件开发和部署的理念和方法&#xff0c;它旨在将应用程序设计和构建为以云为基础的环境中运行。云原生的核心原则是实现敏捷性、可扩展性和弹性&#xff0c;以便应对不断变化的需求和规模。 【云原生的定义】 云原生是指将应用程序设计和构…

西南科技大学计算机网络实验四(交换机基本配置与VLAN配置)

一、实验目的 熟悉交换机的各种基本配置与VLAN配置。 二、实验环境 使用RouterSim Network Visualizer软件来模拟网络设备与网络环境。 三、实验内容 1、交换机的基础配置 2、单台交换机上的简单VLAN设置 3、多台交换机上进行VLAN设置 四、实验步骤 4.1 交换机的基础配置…

TypeScript学习(面试篇)

在当今的 Web 开发世界中&#xff0c;TypeScript 作为一种强大的工具为自己赢得了一席之地&#xff0c;它弥补了 JavaScript 的灵活性和静态类型语言的鲁棒性之间的差距&#xff08;至少在 JavaScript 实现自己的类型之前&#xff09;。 随着技术格局的不断发展&#xff0c;对…

中国发电厂分布POI数据,shp矢量数据和excel格式均有,已清洗加工,可直接用于数据分析

数据名称: 中国发电厂分布poi数据 数据格式: shpexcel 数据几何类型: 点 数据坐标系: WGS84 数据来源&#xff1a;网络公开数据 数据字段&#xff1a; 序号字段名称字段说明1mc名称2idid3lon经度4lat纬度5fdlx发电类型6fdlxbc发电类型别称7jcsj建成时间8ssqlr所属权利人…

macos Apple开发证书 应用签名p12证书 获取生成方法 codesign 证书获取

在开发macos应用的时候必须要对自己开发的应用进行签名才能使用, 下面介绍个人如何获取Apple开发签名证书. 必备条件, 你需要先安装 xcode , 注册一个苹果开发者账号 免费的就可以, 以下为获取流程 You need to create a cert through xcode. Additionally, you need to have…

HarmonyOS4.0开发该怎么系统学习,适合哪些人?

对于想要系统学习HarmonyOS 4.0开发的人来说&#xff0c;以下是一些建议&#xff1a; 1.了解HarmonyOS基础&#xff1a; 首先&#xff0c;你需要对HarmonyOS有一个基本的了解&#xff0c;包括它的核心概念、系统架构、分布式技术等。可以通过官方文档、教程和在线课程来深入了…