llama-2-7b权重文件转hf格式及模型使用

目录

[1. obtain llama weights](#1. obtain llama weights)

[2. convert llama weights files into hf format](#2. convert llama weights files into hf format)

[3. use llama2 to generate text](#3. use llama2 to generate text)


1. obtain llama weights

(1)登录huggingface官网,搜索llama-2-7b

(2)填写申请表单,VPN挂在US,表单地区选择US,大约10min,请求通过,如下图

(3)点击用户头像来获取token

Because you just need read and download the resource,so token type of 'Read' is engough.

After you access your token,please save it!if not,you have to generate it again.

(4)下载llama-2-7b的权重文件

安装依赖

bash 复制代码
pip install -U huggingface_hub

设置hugging face镜像

bash 复制代码
vim ~/.bashrc
bash 复制代码
export HF_ENDPOINT=https://hf-mirror.com
bash 复制代码
source ~/.bashrc

使用刚刚获取的token下载llama-2-7b的权重文件

bash 复制代码
huggingface-cli download --token hf_*** --resume-download meta-llama/Llama-2-7b --local-dir ./llama-2-7b

下载成功后llama-2-7b权重目录如下图

2. convert llama weights files into hf format

Follow instructions provided by Huggingface to convert it into Huggingface format.

其实就两步:

(1)点击链接,下载转换脚本convert_llama_weights_to_hf.py

(2)执行命令

bash 复制代码
python ./convert_llama_weights_to_hf.py --input_dir /hy-tmp/Llama-2-7b --model_size 7B --output_dir /hy-tmp/llama-2-7b-hf

Maybe you need a long time to solve dependencies version conflicts, be patient!

转换成功后llama-2-7b-hf目录如下图

网上有很多地方会直接提供hf格式的llama模型文件,那我们便无需上述复杂的转换操作,只需下载到实例即可,很简单。

3. use llama2 to generate text

(1)代码内容

python 复制代码
import os
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer
from torch.cuda.amp import autocast

# 设置环境变量避免显存碎片化
os.environ['PYTORCH_CUDA_ALLOC_CONF'] = 'max_split_size_mb:128'

# 清理缓存
torch.cuda.empty_cache()

# 加载Llama-2-7b模型和分词器
model_name = "/hy-tmp/llama-2-7b-hf"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForCausalLM.from_pretrained(model_name,torch_dtype=torch.float16)

# 加载模型到GPU
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
model = model.to(device)

input_text = "How to learn skiing?"

# 输入文本的编码
input_ids = tokenizer.encode(input_text, return_tensors="pt").to(device)

# 设置生成文本参数
max_length = 256
temperature = 0.7 
top_k = 50 
top_p = 0.95 

# 使用混合精度加速进行推理
with autocast():
    output = model.generate(
        input_ids,
        max_length=max_length,
        num_return_sequences=1,
        temperature=temperature,
        top_k=top_k,
        top_p=top_p,
        do_sample=True  # 使用采样,避免贪婪生成
    )

# 解码生成的文本
generated_text = tokenizer.decode(output[0], skip_special_tokens=True)

print(generated_text)

(2)执行结果

相关推荐
MYH51635 分钟前
在NLP文本处理中,将字符映射到阿拉伯数字(构建词汇表vocab)的核心目的和意义
人工智能·深度学习·自然语言处理
要努力啊啊啊42 分钟前
KV Cache:大语言模型推理加速的核心机制详解
人工智能·语言模型·自然语言处理
狂小虎3 小时前
02 Deep learning神经网络的编程基础 逻辑回归--吴恩达
深度学习·神经网络·逻辑回归
Jamence3 小时前
多模态大语言模型arxiv论文略读(108)
论文阅读·人工智能·语言模型·自然语言处理·论文笔记
tongxianchao3 小时前
双空间知识蒸馏用于大语言模型
人工智能·语言模型·自然语言处理
猫天意4 小时前
【深度学习】为什么2个3×3的卷积可以相当于一个5×5的卷积核?为什么3个3×3的卷积相当于一个7×7的卷积核,到底区别在哪里?我们该如何使用?
人工智能·深度学习·神经网络·目标检测·视觉检测
阔跃生物5 小时前
Nature Methods | OmiCLIP:整合组织病理学与空间转录组学的AI模型
人工智能·深度学习·机器学习
Mrs.Gril6 小时前
RKNN3588上部署 RTDETRV2
深度学习·yolo·rknn·rtdetr
Ama_tor7 小时前
14.AI搭建preparationのBERT预训练模型进行文本分类
人工智能·深度学习·bert
QQ676580087 小时前
基于 PyTorch 的 VGG16 深度学习人脸识别检测系统的实现+ui界面
人工智能·pytorch·python·深度学习·ui·人脸识别