xiaoxiong 0225b988e0 | 1 month ago | |
---|---|---|
README.md | 1 month ago |
在云脑中新建调试任务,如下图
按照下图选择64G显存的昇腾910B NPU,选择从hf上要迁移的模型,这里以all-MiniLM-L12-v2模型为例
openMind Library是一个开源的深度学习开发套件,通过简单易用的API支持模型预训练、微调、推理、部署等流程。openMind Library通过一套接口兼容PyTorch和MindSpore等主流框架,同时原生支持昇腾NPU处理器。
模型适配主要是用openmind库去代替transformer库来进行推理,其中openmind库原生支持pytorch和mindspore两个框架,下图是当前pipeline支持的任务列表,适配时尽量选择支持这些任务的huggingface模型。
如下图所示,在bash终端内依次执行以下命令激活openmind-pt环境
conda env list
conda activate openmind-pt
新建Python脚本并执行,用于读取all-MiniLM-L12-v2模型路径
from c2net.context import prepare
#初始化导入数据集和预训练模型到容器内
c2net_context = prepare()
#获取预训练模型路径
all-MiniLM-L12-v2_path = c2net_context.pretrain_model_path+"/"+"all-MiniLM-L12-v2"
print(all-MiniLM-L12-v2_path)
然后新建inference.py,复制all-MiniLM-L12-v2模型中readme文件里的推理代码进行修改:
from openmind import AutoTokenizer, AutoModel
import torch
import torch.nn.functional as F
# 均值池化 - 考虑注意力掩码以进行正确的平均
def mean_pooling(model_output, attention_mask):
token_embeddings = model_output[0] # model_output的第一个元素包含所有token嵌入
input_mask_expanded = attention_mask.unsqueeze(-1).expand(token_embeddings.size()).float()
return torch.sum(token_embeddings * input_mask_expanded, 1) / torch.clamp(input_mask_expanded.sum(1), min=1e-9)
# 我们想要获取句子嵌入的句子
sentences = ['This is an example sentence', 'Each sentence is converted']
# 从openmind_hub加载模型
tokenizer = AutoTokenizer.from_pretrained('all-MiniLM-L12-v2')
model = AutoModel.from_pretrained('all-MiniLM-L12-v2')
# 对句子进行分词
encoded_input = tokenizer(sentences, padding=True, truncation=True, return_tensors='pt')
# 计算token嵌入
with torch.no_grad():
model_output = model(**encoded_input)
# 执行池化
sentence_embeddings = mean_pooling(model_output, encoded_input['attention_mask'])
# 归一化嵌入
sentence_embeddings = F.normalize(sentence_embeddings, p=2, dim=1)
print("Sentence embeddings:")
print(sentence_embeddings)
在bash终端执行以下命令进行推理测试,当句子成功被转换为下图中的词向量则已适配成功
python inference.py
模型需要同时上传至openi启智社区和魔乐社区,官方文档链接为:模型可用性测试 | 魔乐社区 (modelers.cn)
首先将模型上传至魔乐社区,上传之前需要在社区新建模型仓库,如下图所示
创建完成之后就是上传了,此时记住我们在openmind_hub云端上的模型的路径为:lvzhou/all-MiniLM-L12-v2,接下来在all-MiniLM-L12-v2模型目录下新建examples目录,并在examples目录内创建inference.py和requirements.txt,两个文件的内容如下:
inference.py
主要修改parse_args函数内的default参数所指定的模型路径,便于接下来将模型上传到openmind_hub中
from openmind import AutoTokenizer, AutoModel, is_torch_npu_available
from openmind_hub import snapshot_download
import torch
import argparse
import torch.nn.functional as F
# 均值池化 - 考虑注意力掩码以进行正确的平均
def mean_pooling(model_output, attention_mask):
token_embeddings = model_output[0] # model_output的第一个元素包含所有token嵌入
input_mask_expanded = attention_mask.unsqueeze(-1).expand(token_embeddings.size()).float()
return torch.sum(token_embeddings * input_mask_expanded, 1) / torch.clamp(input_mask_expanded.sum(1), min=1e-9)
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument(
"--model_name_or_path",
type=str,
help="Path to model",
default="lvzhou/all-MiniLM-L12-v2",
)
args = parser.parse_args()
return args
def main():
args = parse_args()
model_path = args.model_name_or_path
if is_torch_npu_available():
device = "npu:0"
else:
device = "cpu"
# 我们想要获取句子嵌入的句子
sentences = ['This is an example sentence', 'Each sentence is converted']
# 从openmind_hub加载模型
tokenizer = AutoTokenizer.from_pretrained(model_path)
model = AutoModel.from_pretrained(model_path)
# 对句子进行分词
encoded_input = tokenizer(sentences, padding=True, truncation=True, return_tensors='pt')
# 计算token嵌入
with torch.no_grad():
model_output = model(**encoded_input)
# 执行池化
sentence_embeddings = mean_pooling(model_output, encoded_input['attention_mask'])
# 归一化嵌入
sentence_embeddings = F.normalize(sentence_embeddings, p=2, dim=1)
print("Sentence embeddings:")
print(sentence_embeddings)
if __name__ == "__main__":
main()
requirements.txt
设置NPU下运行该脚本需要配置的对应文件,默认torch_npu, openMind Library已安装
transformers==4.39.2
模型上传文件的官方文档链接为:上传文件 | 魔乐社区 (modelers.cn)
首先打开访问令牌 | 魔乐社区 (modelers.cn)链接,新建一个令牌,将权限设置为write,并将其复制在下面的上传代码中token中
from openmind_hub import upload_folder
# 使用upload_folder向仓库中上传文件夹。
# token:对目标仓库具有可写权限的访问令牌,必选。
# folder_path:要上传的本地文件夹的路径,必选。
# repo_id:目标仓库,必选。
# 若需对上传的文件类型进行过滤,可以使用allow_patterns和ignore_patterns参数,详见upload_folder。
upload_folder(
token="****",
folder_path="all-MiniLM-L12-v2",
repo_id="lvzhou/all-MiniLM-L12-v2",
)
接下来在bash终端内执行模型上传命令
python upload.py
上传成功后在社区项目介绍页进行模型可用性测试,当出现测试成功即可,如果失败也可以下载日志进行debug
打开帐户设置链接,新建openi的令牌token,将其复制到下面的代码中,同时根据自己的用户名创建模型进行上传,如repo_id="xiaoxiong/all-MiniLM-L12-v2"
# 上传到openi
from openmind_hub import set_platform, create_repo, upload_folder, snapshot_download
# 设置默认社区
set_platform("openi")
token = "***"
# 创建模型
create_repo(repo_id="xiaoxiong/all-MiniLM-L12-v2", token=token)
# 上传模型
upload_folder(repo_id="xiaoxiong/all-MiniLM-L12-v2", folder_path="all-MiniLM-L12-v2", token=token)
接下来在bash终端内执行模型上传命令,上传完成后如下图所示
python upload.py
Dear OpenI User
Thank you for your continuous support to the Openl Qizhi Community AI Collaboration Platform. In order to protect your usage rights and ensure network security, we updated the Openl Qizhi Community AI Collaboration Platform Usage Agreement in January 2024. The updated agreement specifies that users are prohibited from using intranet penetration tools. After you click "Agree and continue", you can continue to use our services. Thank you for your cooperation and understanding.
For more agreement content, please refer to the《Openl Qizhi Community AI Collaboration Platform Usage Agreement》