DrR*_*rry 6 nlp python-3.x torch huggingface-transformers apple-m1
我正在尝试使用 MPS 后端在 M1 Mac 上运行 T5 变压器:
import torch
import json
from transformers import T5Tokenizer, T5ForConditionalGeneration, T5Config
#Make sure sentencepiece is installed
device = torch.device('mps')
model = T5ForConditionalGeneration.from_pretrained('t5-3b').to("mps")
tokenizer = T5Tokenizer.from_pretrained('t5-3b')#, device = device)
preprocess_text = full_text.strip().replace("\n",".")
t5_prepared_Text = "summarize: "+preprocess_text
print ("original text preprocessed: \n", preprocess_text)
tokenized_text = tokenizer.encode(t5_prepared_Text, return_tensors="pt").to(device)
# summmarize
summary_ids = model.generate(tokenized_text,
num_beams=6,
no_repeat_ngram_size=3,
min_length=30,
max_length=9000,
early_stopping=True)
output = tokenizer.decode(summary_ids[0], skip_special_tokens=True)
print ("\n\nSummarized text: \n",output)
Run Code Online (Sandbox Code Playgroud)
其中“full_text”是之前定义的字符串。该代码适用于 CPU,但我想通过使用 MPS 来加速它(如上面的代码所示)。在这里,我收到错误:
TypeError: Operation 'abs_out_mps()' does not support input type 'int64' in MPS backend.
Run Code Online (Sandbox Code Playgroud)
因此 MPS 不支持 int64。如果发生此错误,有没有办法让模型转换为浮动,而不是仅仅破坏整个模型?
| 归档时间: |
|
| 查看次数: |
534 次 |
| 最近记录: |