⚠️ 这只是 hyperonym/xlm-roberta-longformer-base-16384 的 PyTorch 版本,未做任何修改。
xlm-roberta-longformer 是一个多语言 Longformer,使用 XLM-RoBERTa 的权重进行初始化,未经过进一步预训练。该模型旨在针对下游任务进行微调。
用于复现模型的笔记本可在 GitHub 上获取:https://github.com/hyperonym/dirge/blob/master/models/xlm-roberta-longformer/convert.ipynb
from openmind import AutoTokenizer, AutoModelForSequenceClassification, is_torch_npu_available
from openmind_hub import snapshot_download
import torch.nn.functional as F
from torch import Tensor
import openmind
import torch
import argparse
import time
# Mean Pooling - Take attention mask into account for correct averaging
def mean_pooling(model_output, attention_mask):
token_embeddings = model_output[0] # First element of model_output contains all token embeddings
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="jeffding/xlm-roberta-longformer-base-16384-openmind",
)
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"
# Load model from HuggingFace Hub
tokenizer = AutoTokenizer.from_pretrained(model_path)
model = AutoModelForSequenceClassification.from_pretrained(
model_path, trust_remote_code=True,
torch_dtype=torch.float16
).to(device)
model.eval()
start_time = time.time()
pairs = [["中国的首都在哪儿","北京"], ["what is the capital of China?", "北京"],["how to implement quick sort in python?","Introduction of quick sort"]]
with torch.no_grad():
inputs = tokenizer(pairs, padding=True, truncation=True, return_tensors='pt', max_length=512).to(device)
scores = model(**inputs, return_dict=True).logits.view(-1, ).float()
print(scores)
end_time = time.time()
print(f"硬件环境:{device},推理执行时间:{end_time - start_time}秒")
if __name__ == "__main__":
main()