unilm LayoutLMv2和LayoutXLM无法在CPU上使用半精度(float16)数据类型进行推理,

5anewei6  于 2个月前  发布在  其他
关注(0)|答案(1)|浏览(28)

你好,

我想使用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代码中实现。如何解决这个问题?
谢谢。

6yoyoihd

6yoyoihd1#

同样的问题,我的程序也遇到了。有什么解决方案吗?

相关问题