来源与Qwen-chat中finetune.py的警告。

/root/miniconda3/lib/python3.8/site-packages/torch/utils/checkpoint.py:295: FutureWarning: `torch.cpu.amp.autocast(args...)` is deprecated. Please use `torch.amp.autocast('cpu', args...)` instead.

device_autocast_ctx = torch.amp.autocast(
                device_type=ctx.device, **ctx.device_autocast_kwargs
            ) if torch.amp.is_autocast_available(ctx.device) else contextlib.nullcontext()
            with torch.enable_grad(), device_autocast_ctx, torch.cpu.amp.autocast(**ctx.cpu_autocast_kwargs):  # type: ignore[attr-defined]
                outputs = ctx.run_function(*detached_inputs)

with torch.enable_grad(), device_autocast_ctx, torch.cpu.amp.autocast(**ctx.cpu_autocast_kwargs): # type: ignore[attr-defined]

从错误信息来看,你正在使用一个已经被弃用的torch.cpu.amp.autocast上下文管理器。PyTorch 1.9版本引入了自动混合精度(AMP)的新API,并且从那时起,torch.cpu.amp.autocast已经被标记为弃用。

为了解决这个问题,你应该按照警告信息中建议的那样,使用新的torch.amp.autocastAPI,并且指定'cpu'作为设备类型。

更改之后:

device_autocast_ctx = torch.amp.autocast(
    device_type=ctx.device, **ctx.device_autocast_kwargs
) if torch.amp.is_autocast_available(ctx.device) else contextlib.nullcontext()

with torch.enable_grad(), device_autocast_ctx:
    with torch.amp.autocast('cpu', **ctx.cpu_autocast_kwargs):  # 使用新的API
        outputs = ctx.run_function(*detached_inputs)

注意,torch.amp.autocast('cpu', ...)只能在CPU上使用,并且通常用于测试或在不支持GPU的环境下进行开发。如果你的代码需要在GPU上运行,你应该确保ctx.device是指向一个CUDA设备,并且使用torch.amp.autocast(device_type=ctx.device, ...)

Logo

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

更多推荐