你好,
我想使用LayoutXLM
的模型参数在CPU上进行推理(尝试过GPU,但可以工作)。由于我使用的是Hugging Face的Transformers库,我运行了以下代码:
from transformers import LayoutLMv2ForTokenClassification
import torch
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
param_dtype = torch.float16
model_id = "pierreguillou/layout-xlm-base-finetuned-with-DocLayNet-base-at-paragraphlevel-ml512"
model = LayoutLMv2ForTokenClassification.from_pretrained(model_id, torch_dtype=param_dtype)
model.to(device);
它可以正常工作,但是当我使用以下代码运行模型进行推理时,它失败了:
with torch.no_grad():
output = model(input_ids=input_id.to(device),
attention_mask=attention_mask.to(device),
bbox=bbox.to(device),
image=pixel_values.to(device)
)
错误信息:
[/usr/local/lib/python3.10/dist-packages/torch/nn/functional.py](https://localhost:8080/#) in layer_norm(input, normalized_shape, weight, bias, eps)
2513 layer_norm, (input, weight, bias), input, normalized_shape, weight=weight, bias=bias, eps=eps
2514 )
-> 2515 return torch.layer_norm(input, normalized_shape, weight, bias, eps, torch.backends.cudnn.enabled)
2516
2517
RuntimeError: "LayerNormKernelImpl" not implemented for 'Half'
看起来dtype float32
直接在LayoutLMv2
代码中实现。如何解决这个问题?
谢谢。
1条答案
按热度按时间6yoyoihd1#
同样的问题,我的程序也遇到了。有什么解决方案吗?