M1芯片上训练yolov5出现“PYTORCH_ENABLE_MPS_FALLBACK=1”报错,如何解决

问题描述

NotImplementedError: The operator 'aten::hardswish_' is not currently implemented
for the MPS device. If you want this op to be added in priority during the
prototype phase of this feature, please comment on
https://github.com/pytorch/pytorch/issues/77764. As a temporary fix, you can set
the environment variable `PYTORCH_ENABLE_MPS_FALLBACK=1` to use the CPU as a
fallback for this op. WARNING: this will be slower than running natively on MPS.

在这里插入图片描述

解决办法

此时在终端命令行输入:

export PYTORCH_ENABLE_MPS_FALLBACK=1

即可解决

解决原理:

PYTORCH_ENABLE_MPS_FALLBACK=1 是 PyTorch 中的一个环境变量,其作用是指示 PyTorch 在使用 CUDA 的 MPS(Multi-Process Service)时,是否启用 fallback 模式。当该值为 1 时,PyTorch 将尽可能地使用 MPS 工作,如果某些条件不符合,则会使用 fallback 模式进行计算。

MPS 是一种 CUDA 提供的多进程服务,它允许多个 CUDA 应用程序共享同一块 GPU 上的资源,从而提高 GPU 利用率和计算性能。在 PyTorch 中,启用 MPS 可以减少显存的占用,加速模型的训练和推断过程。

然而,由于硬件和软件环境等因素的限制,在某些情况下可能无法正常使用 MPS,例如硬件资源不足、操作系统版本不受支持或驱动程序更新等原因。此时,fallback 模式将会被启用,这使得 PyTorch 可以回退到传统的单进程工作模式,保证程序的正确性和可健壮性。

总之,开启 PYTORCH_ENABLE_MPS_FALLBACK=1 可以有效地提高 PyTorch 程序的性能,并避免一些 MPS 的兼容性问题。

猜你喜欢

转载自blog.csdn.net/weixin_45277161/article/details/130884093