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)执行结果

相关推荐
Tbisnic2 小时前
AI大模型学习第十一天:技术选型、安全防护与金融实战
python·学习·ai·大模型·提示词工程
DisonTangor4 小时前
谷歌开源首个扩散大语言模型——DiffusionGemma
人工智能·语言模型·自然语言处理·开源·aigc·transformer
祭曦念4 小时前
古诗小集开发实战:从零开发一款 HarmonyOS 古诗鉴赏应用
pytorch·深度学习·harmonyos
财经资讯数据_灵砚智能5 小时前
基于全球经济类多源新闻的NLP情感分析与数据可视化(夜间-次晨)2026年6月10日
大数据·人工智能·python·ai·信息可视化·自然语言处理·灵砚智能
YOLO数据集集合6 小时前
无人机航拍街道巡检数据集 | 空中视角车辆检测、交通流量统计、违停识别、智能交通YOLO数据集10399期
深度学习·yolo·目标检测·无人机
放下华子我只抽RuiKe56 小时前
FastAPI 全栈后端(四):认证与授权
开发语言·前端·javascript·python·深度学习·react.js·fastapi
菜鸟‍7 小时前
【论文学习】Segment Anything 分割一切
深度学习·学习·计算机视觉
装不满的克莱因瓶7 小时前
自然语言处理发展历史——从规则系统到大语言模型的演进之路
网络·人工智能·python·深度学习·语言模型·自然语言处理
weixin_408266349 小时前
H20训练CPGNET环境搭建
深度学习
装不满的克莱因瓶9 小时前
RLHF中的PPO算法——大语言模型对齐优化的核心引擎
人工智能·python·深度学习·算法·机器学习·语言模型·自然语言处理