🚀 波斯智慧(PersianMind)
波斯智慧(PersianMind)是一款跨语言的波斯语 - 英语大语言模型。该模型在Belebele基准测试的波斯语子集以及ParsiNLU多项选择问答任务中取得了最先进的成果。在波斯语阅读理解任务中,它的表现也与GPT - 3.5 - turbo相媲美。
🚀 快速开始
模型使用入门
使用以下代码开始使用该模型。请注意,要运行此代码,你需要安装sentencepiece
和accelerate
库,以及PyTorch
和🤗Transformers
。
from transformers import AutoTokenizer, AutoModelForCausalLM
import torch
device = "cuda" if torch.cuda.is_available() else "cpu"
model = AutoModelForCausalLM.from_pretrained(
"universitytehran/PersianMind-v1.0",
torch_dtype=torch.bfloat16,
low_cpu_mem_usage=True,
device_map={"": device},
)
tokenizer = AutoTokenizer.from_pretrained(
"universitytehran/PersianMind-v1.0",
)
TEMPLATE = "{context}\nYou: {prompt}\nPersianMind: "
CONTEXT = "This is a conversation with PersianMind. It is an artificial intelligence model designed by a team of " \
"NLP experts at the University of Tehran to help you with various tasks such as answering questions, " \
"providing recommendations, and helping with decision making. You can ask it anything you want and " \
"it will do its best to give you accurate and relevant information."
PROMPT = "در مورد هوش مصنوعی توضیح بده."
model_input = TEMPLATE.format(context=CONTEXT, prompt=PROMPT)
input_tokens = tokenizer(model_input, return_tensors="pt")
input_tokens = input_tokens.to(device)
generate_ids = model.generate(**input_tokens, max_new_tokens=512, do_sample=False, repetition_penalty=1.1)
model_output = tokenizer.batch_decode(generate_ids, skip_special_tokens=True, clean_up_tokenization_spaces=False)[0]
print(model_output[len(model_input):])
模型量化方法
量化模型可以在资源受限的设备上运行。要对模型进行量化,你需要安装bitsandbytes
库。以下是在8位(INT8
)下量化模型的代码:
model = AutoModelForCausalLM.from_pretrained(
"universitytehran/PersianMind-v1.0",
device_map="auto",
low_cpu_mem_usage=True,
load_in_8bit=True
)
或者,你可以使用以下代码在4位(NormalFloat4
)下量化模型:
from transformers import BitsAndBytesConfig
quantization_config = BitsAndBytesConfig(
load_in_4bit=True,
bnb_4bit_use_double_quant=True,
bnb_4bit_quant_type="nf4",
)
model = AutoModelForCausalLM.from_pretrained(
"universitytehran/PersianMind-v1.0",
quantization_config=quantization_config,
device_map="auto"
)
量化模型评估
我们对量化模型在各种任务中与原始模型进行了评估。具体来说,我们使用Belebele(波斯语子集)的阅读理解多项选择问答基准对所有模型进行了评估,并报告了每个模型的准确率。此外,我们还对模型进行了波斯语到英语和英语到波斯语的翻译任务评估。为此,我们使用了Flores - 200数据集的波斯语 - 英语子集,并使用Comet指标报告了结果。此外,我们还计算了每个模型在运行翻译任务时每秒生成的平均标记数。为了了解资源效率,我们使用get_memory_footprint()
函数测量了每个模型的内存使用情况。
模型 |
Belebele(波斯语) |
波斯语到英语翻译 (Comet) |
英语到波斯语翻译 (Comet) |
模型大小 |
每秒标记数 |
波斯智慧(PersianMind) (BF16 ) |
73.9 |
83.61 |
79.44 |
13.7G |
25.35 |
波斯智慧(PersianMind) (INT8 ) |
73.7 |
82.32 |
78.61 |
7.2G |
11.36 |
波斯智慧(PersianMind) (NF4 ) |
70.2 |
82.07 |
80.36 |
3.9G |
24.36 |
📄 许可证
波斯智慧(PersianMind)遵循Meta的LLaMa2社区许可证。它还根据CC BY - NC - SA 4.0许可,允许非商业使用该模型。商业使用此模型需要获得版权持有者的书面协议,版权持有者在本页面中列为开发者。如果你怀疑有任何违规行为,请与我们联系。
引用
如果你发现此模型有帮助,请引用以下论文:
BibTeX:
@misc{persianmind,
title={{PersianMind: A Cross-Lingual Persian-English Large Language Model}},
author={Rostami, Pedram and Salemi, Ali and Dousti, Mohammad Javad},
year={2024}
eprint={2401.06466},
archivePrefix={arXiv},
primaryClass={cs.CL}
}