如果你尝试在huggingface.co的Spaces中推理你的模型,结果遇到错误:

The model is offloaded on CPU or disk - CPU & disk offloading is not supported for ValueHead models.

这表明模型架构需要使用GPU来运行,而不是CPU,当前没有使用CPU来运行

或者你尝试指定'cuda',但是得到RuntimeError: No CUDA GPUs are available这表明它没有找到GPU

如何在huggingface.co的Spaces中使用GPU

我这里的GPU指的是ZeroGPU
你首先要确保在创建Spaces的时候勾选了ZeroGPU,而不是CPU
ps:有些人说即使勾选了CPU仍然可以调用ZeroGPU,我不确定。

如果你的项目足够好,社区会赞助你ZeroGPU,否则你需要充值 9$ 的PRO会员,充值的时候需要扣押额外的 10$ 它声称一旦完成就会返还,但是美国的银行系统决定了这可能是一个缓慢的过程。

其次,你需要将代码中执行推理的地方封装在一个函数中,并且使用修饰符

import spaces

@spaces.GPU
def run_my_big():
	pass

自此,你应该可以使用GPU了。


如果你得到如下错误,这表明你的模型被加载到GPU和CPU两个设备上,它期望加载到一个设备上(加载到GPU上)

RuntimeError: Expected all tensors to be on the same device, but found at least two devices, cuda:0 and cpu! (when checking argument for argument index in method wrapper_CUDA__index_select)

你可以这样做

import torch
device = torch.device("cuda:0")  # 明确指定设备
xxx.to(device)  # 确保模型在cuda:0设备上,写法取决于你使用的框架,请查阅文档
Logo

有“AI”的1024 = 2048,欢迎大家加入2048 AI社区

更多推荐