用Transformers库运行Llama-3.1-Minitron-4B

我的笔记本来自8年前的旧时代,勉强能运行8B模型,于是打算试试看更小的……

Llama-3.1-Minitron 4B 简介

Llama-3.1-Minitron 4B 是基于Llama-3.1 8B模型,通过结构化权重剪枝和知识提炼技术优化而成的紧凑型语言模型。

它有两种基座模型,Width-Base 和 Depth-Base,相关的模型文件可以在 Hugging Face 或其镜像 HF-Mirror 中找到。

为什么不用Ollama运行

理由很简单,电脑做不到……原本试图用Ollama管理,但运行时出现我暂无法解决的报错,这似乎是CPU资源不够或模型转换出错导致的无法读取结果,打算军训后降下量化精度重新试试。

所以暂时先用回transformers库+python,

反正也不需要这个小模型直接交互,影响不大。

影响不大才怪,我怎么调都加载不了940MX,Ollama能,虽然无法有效读端口但响应的速度快了好几十倍


所幸用transformers库运行 Llama-3.1-Minitron-4B 很简单 : )
 

1、下载模型

可以直接在 Hugging Face 上下载模型文件,但推荐使用 HF-Mirror :

Width-Base模型,从 HF-Mirror 克隆

git lfs install  # 安装lfs以支持大文件下载
git clone https://hf-mirror.com/nvidia/Llama-3.1-Minitron-4B-Width-Base

或者Depth-Base模型,从 HF-Mirror 克隆

git lfs install   # 安装lfs以支持大文件下载
git clone https://hf-mirror.com/nvidia/Llama-3.1-Minitron-4B-Depth-Base

只需要二者之一即可,都下载可能要多花些时间。

如果克隆成功,可以去运行了;

如果克隆失败或缺失,则需要手动下载文件并校验。

手动下载地址:

两个模型各自的文件发布页(镜像):

nvidia/Llama-3.1-Minitron-4B-Width-Base at main · HF Mirror

nvidia/Llama-3.1-Minitron-4B-Depth-Base at main · HF Mirror

校验方法:

打开文件的发布页,点击红框内的链接之一,

下滑,找到几个关键文件的校验信息,并记录


在命令行中转到这几个文件所在目录,计算文件的哈希值

cd 【待校验文件所在目录】
certutil -hashfile model-00001-of-00002.safetensors SHA256
certutil -hashfile model-00002-of-00002.safetensors SHA256

文件较大,需耐心等待哈希完成

如果返回的哈希值能够与之前记录的值对应,即下载正确,否则应该重新下载并校验。

对于没有哈希校验信息但有 文件大小(size) 信息的,可以在属性中查看文件字节大小进行校验。

比如这两个文件:大小不匹配,所以哈希值也肯定对应不上,应该重新下载。

2、使用transformers库运行

首先,安装 transformers 和 torch 库。(如果没有的话)

pip install transformers torch

然后就可以运行了,

注意先把代码中的路径改成你的。

# 不是官方示例呢,我自己写的(仰脸)
# 官方示例可以在模型发布页找到
from transformers import AutoTokenizer, LlamaForCausalLM
import torch


# 定义模型路径
# 这是我的,应改为你的模型路径,记得双斜杠 ↓
model_path = "C:\\Users\\LingL\\llama.cpp\\models\\Llama-3.1-Minitron-4B-Depth-Base"

# 加载 tokenizer 和模型
tokenizer = AutoTokenizer.from_pretrained(model_path)
dtype = torch.bfloat16  # 使用混合精度以节省显存
device = "auto"  # 使用 HuggingFace 的自动设备映射
model = LlamaForCausalLM.from_pretrained(model_path, torch_dtype=dtype, device_map=device)


# 将模型转移到 GPU 上,如果CUDA可用的话
print("-------------------------------------\n")
if torch.cuda.is_available():
    device = torch.device("cuda")
    print(f"Using GPU: {torch.cuda.get_device_name()}")
else:
# 如果不可用,算了
    device = torch.device("cpu")
    print("CUDA is not available, using CPU.")
print("-------------------------------------\n")



# 导入输入内容,并返回输出内容
def run_test(input_text):
    input_ids = tokenizer.encode(input_text, return_tensors="pt").to(device)
    attention_mask = torch.ones_like(input_ids)  # 创建 attention mask   
    with torch.no_grad():
        output = model.generate(input_ids, 
                                attention_mask=attention_mask,  # 传递 attention_mask
                                max_length=50,  # 生成长度限制,但长度需要大于输入
                                pad_token_id=tokenizer.eos_token_id,
                                num_return_sequences=1,  # 确保只返回一个序列
                                do_sample=False)  # 关闭采样以减少随机性
    return tokenizer.decode(output[0], skip_special_tokens=True)


# 待输入的prompt,会被按顺序输入模型
input_texts = [
    # 1.注意添加逗号,不然两prompt会被视作一伙同时输入
    # 2.长度需要小于 max_length 的生成长度限制,具体数值见上文设定
    "Explain the theory of relativity: It is", #补完文本
    "1,1,2,3,5,8,13,21,34,55,"                 #数列推理
    "The Sky is",                              #自由联想

]


# 执行测试并打印结果
for text in input_texts:
    print(f"\n▶Input: {text}\n▶Output: {run_test(text)}\n")

运行结果参考:

▶Input: Explain the theory of relativity: It is
▶Output: Explain the theory of relativity: It is a theory of physics that explains how space and time are related to each other. It was developed by Albert Einstein in 1905 and has since been confirmed by numerous experiments.


▶Input: 1, 1, 2, 3, 5, 8, 13, 21, 34, 55,
▶Output: 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946, 17711, 28657, 46368, 75025, 121393, 196418, 317811, 514229, 832040, 1346269, 2178309, 3524578, 5702887, 9227465, 14930352, 24157817, 39088169, 63245986, 102334155, 165580141, 267914296, 433494437, 701408733, 1134903170, 1836311903, 2971215073, 4807526976


▶Input: The sky is
▶Output: The sky is falling! The sky is falling! The sky is falling! The sky is falling! The sky is falling! The sky is falling! The sky is falling! The sky is falling! The sky is falling! The sky is falling! The sky is falling! The sky is falling! The sky is falling! The sky is falling
# 建议关注模型的精神状态


另进行的一些测试

推理代数的加减运算(m=0, n=5, o=m+n, o=5)

Input: a=1, b=2, c=a+b, c=3. d=1, e=5, f=d+e, f=6. g=1, h=3, i=g+h, i=4. j=2, k=3, l=j+k, l=5. m=0, n=5, o=m+n, o=
Output: a=1, b=2, c=a+b, c=3. d=1, e=5, f=d+e, f=6. g=1, h=3, i=g+h, i=4. j=2, k=3, l=j+k, l=5. m=0, n=5, o=m+n, o=5. p=1, q=2, r=p+q, r=3. s=1, t=2, u=s+t, u=3. 

推理字母对应的数字(按字母的顺序从1至26对应,e=5

Input: a=1,z=26,e=?
Output: a=1,z=26,e=? (a=1,z=26,e=5) (a=1,z

推理接下来的字母(按键盘按键顺序,且每3字母有1逗号

Input: qwe, rty, uio, pas, dfg, hjk, lzx, cvb, nmq, wer, tyu, iop, asd, fgh, j
Output: qwe, rty, uio, pas, dfg, hjk, lzx, cvb, nmq, wer, tyu, iop, asd, fgh, jkl, zxc, cvb, nmq, wer, tyu, iop, asd, fgh, jkl, zxc, cvb, nmq, wer, tyu, iop, asd, fgh, jkl, zxc, cvb, nmq, wer, tyu, iop, asd, fgh, jkl, zxc, cvb, nmq, wer, ty

推理斐波那契数列

Input: 1, 1, 2, 3, 5, 8, 13, 21, 34, 55,
Output: 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946, 17711, 28657, 46368, 75025, 121393, 196418, 317811, 514229, 832040, 1346269, 2178309, 3524578, 5702887, 9227465, 14930352, 24157817, 39088169, 63245986, 102334155, 165580141, 267914296, 433494437, 701408733, 1134903170, 1836311903, 2971215073, 4807526976

Input: 北京市在哪个国家?在
Output: 北京市在哪个国家?在哪个国家的北京市?北京市在哪个国家?北京市在哪个国家?北京市在哪个国家?北京市在哪个国家?北京市在哪个国家?北京市在哪个国家?北京市在哪个国家?北京市在哪个国家?北京市在哪个国家?北京市在哪个国家?北京市在

Input: 北京市位于上海的
Output: 北京市位于上海的东北方角落。距离上海市中心仅距离不到50公里。2017年底,北京市的总人口达到了21.3亿人口。 这家公司由北京的北京 交通大学主办的。这个比赛的目标将是引领更多的

Input: using namespace std;int main(){cout<<"Hello,Wor
Output: using namespace std;int main(){cout<<"Hello,Worl"<<endl;}this code returns me error I want to write this code on my IDE but I don't know why IDE doesn't recognize the namespace std so can you please help me out with this problem

Input: The official name of the UK is
Output: The official name of the UK is 2016-02-11T00:00:00Z

The official

Input: What is the official name of the UK?
Output: What is the official name of the UK? The United Kingdom of Great Britain and Northern Ireland is the official name of the UK

Input: What is The full name of United Kingdom?
Output: What is The full name of United Kingdom? The full name of United Kingdom is United Kingdom of Great Britain and Northern Ireland

Input: 北京市位于哪个国家?
Output: 北京市位于哪个国家? - 北京市 - 北京市 - 北京市 - 北京市 - 北京市 - 北京


Input: 北京所在的国家是
Output: 北京所在的国家是中国,北京的省份是北京市,北京的城市代码是010,北京

Input: The sky is
Output: The sky is falling! The sky is falling! The sky is falling! The sky is falling! The sky is falling

Input: 人类从不掩饰掌控星空的欲望……当然,也包括我在
Output: 人类从不掩饰掌控星空的欲望……当然,也包括我在内。我们在星空中建立了一个庞大的帝国。我们在星空中建立了一个庞大的帝国。我们在星空中建立了一个庞大的帝国。我们在星空中建立了一个庞大的帝国。我们在星空中建立了一个庞大的帝国。我们在星空中建立了一个庞大 的帝国。我们在星空中建立了一个庞大的帝国。我们在星空中建立了

Input: 任何人都无法踏足和他人完全相同的行迹, 任何人都无法代他人体验沿途所见的风景, 她能依靠的只有自己的双眼和
Output: 任何人都无法踏足和他人完全相同的行迹, 任何人都无法代他人体验沿途所见的风景, 她能依靠的只有自己的双眼和两手空的时间。
每当她想到往昔的样子, 仿佛那不经心之夜她和我在山地那小小的

Input: 任何人都无法踏足和他人完全相同的行迹, 任何人都无法代他人体验沿途所见的风景, 她能依靠的只有自己的双眼和
Output: 任何人都无法踏足和他人完全相同的行迹, 任何人都无法代他人体验沿途所见的风景, 她能依靠的只有自己的双眼和双手。 但她却 不愿意去做这些。 但她却不愿意去做这些。 但她却不愿意去做这些。 但她却不愿意去做这些。 但她却不愿意去做这些。 但她却不愿意去做这些。 但她却不愿

Input: Write something:
Output: Write something: 1. Write a short story about a person who is afraid of the dark. 2. Write a short story about a person who is afraid of the dark. 3. Write a short story about a person who is afraid of the dark. 4. Write a short story about a person who is afraid of the dark. 5. Write a short story about a person who is afraid of the dark. 6. Write a short story about a person who is afraid of the dark. 7. Write a short story about a person who is afraid of the dark. 8. Write a short story about a person who is afraid of the dark. 9. Write a short story about a person who is afraid of the dark. 10. Write a short story about a person who is afraid of the dark. 11. Write a short story about a person who is afraid of the dark. 12. Write a short story about a

Input: This is a short story about a person who is afraid of the dark:
Output: This is a short story about a person who is afraid of the dark: "The Dark" by Stephen King. It is a story about a boy who is afraid of the dark. He is afraid of the dark because he thinks that there ar


评价

来自 GPT-4o ,基于刚才的测试:

Llama-3.1-Minitron-4B-Width-Base 作为一个4B参数的模型,在基础语言理解、算术推理、基本编程等任务上能够提供一定的帮助,但在面对复杂的语言生成、多步推理以及多语言处理时,模型能力尚显不足。它的输出较为线性,缺乏足够的深度和复杂度,在生成长文本时容易出现重复或不连贯的现象。

适用场景:
  • 基础算术或符号运算:适用于基础算术任务、简单序列的生成和推理。
  • 短文本生成:在生成短篇文字或回答简单问题时,表现较为流畅。
  • 基础编程辅助:可用于生成简单的代码结构,但在复杂代码推理和调试上能力有限。
不适用场景:
  • 复杂逻辑推理:由于模型在处理多步逻辑推理时会出错,不适合复杂的数学或符号问题。
  • 长文本生成:模型在长文本生成中容易重复,难以保持上下文的一致性和流畅性。
  • 多语言场景:对多语言支持不佳,尤其在中英混合任务中容易出现误解和错误。

总的来说,Llama-3.1-Minitron-4B 作为小规模模型,能在特定任务中提供基础支持,但在实际应用中,还需根据需求调整其适用范围和任务复杂度。

  • 20
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值