MacBook(AMD显卡)配置PyTorch MPS加速失败求助
问题
设备是搭载Radeon Pro 555显卡、运行Ventura系统的MacBook,想配置PyTorch的MPS加速来跑Stable Diffusion。按苹果官方教程操作后,用Python脚本验证MPS支持时出现了RuntimeError。已经把Python从3.12.1降级到3.11.1,还重装了最新的PyTorch nightly版本,但问题依旧没解决。
验证代码和错误信息如下:
Python 3.11.1 (v3.11.1:a7a450f84a, Dec 6 2022, 15:24:06) [Clang 13.0.0 (clang-1300.0.29.30)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> import torch >>> if torch.backends.mps.is_available(): ... mps_device = torch.device("mps") ... x = torch.ones(1, device=mps_device) ... print (x) ... else: ... print ("MPS device not found.") ... Traceback (most recent call last): File "<stdin>", line 4, in <module> File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/torch/_tensor.py", line 461, in __repr__ return torch._tensor_str._str(self, tensor_contents=tensor_contents) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/torch/_tensor_str.py", line 677, in _str return _str_intern(self, tensor_contents=tensor_contents) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/torch/_tensor_str.py", line 597, in _str_intern tensor_str = _tensor_str(self, indent) ^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/torch/_tensor_str.py", line 349, in _tensor_str formatter = _Formatter(get_summarized_data(self) if summarize else self) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/torch/_tensor_str.py", line 137, in __init__ nonzero_finite_vals = torch.masked_select( ^^^^^^^^^^^^^^^^^^^^ RuntimeError: Failed to create indexing library, error: Error Domain=MTLLibraryErrorDomain Code=3 "program_source:168:1: error: type 'const constant ulong3 *' is not valid for attribute 'buffer' REGISTER_INDEX_OP_ALL_DTYPES(select); ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ program_source:160:5: note: expanded from macro 'REGISTER_INDEX_OP_ALL_DTYPES' REGISTER_INDEX_OP(8bit, idx64, char, INDEX_OP_TYPE, ulong3); \ ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ program_source:138:5: note: expanded from macro 'REGISTER_INDEX_OP' constant IDX_DTYPE * offsets [[buffer(3)]], \ ^ ~~~~~~~~~ program_source:168:1: note: type 'ulong3' (vector of 3 'unsigned long' values) cannot be used in buffer pointee type program_source:160:59: note: expanded from macro 'REGISTER_INDEX_OP_ALL_DTYPES' REGISTER_INDEX_OP(8bit, idx64, char, INDEX_OP_TYPE, ulong3); \ ^ program_source:168:1: error: explicit instantiation of 'index_select' does not refer to a function template, variable template, member function, member class, or static data member REGISTER_INDEX_OP_ALL_DTYPES(select); ^ program_source:160:5: note: expanded from macro 'REGISTER_INDEX_OP_ALL_DTYPES' REGISTER_INDEX_OP(8bit, idx64, char, INDEX_OP_TYPE, ulong3); \ ^ program_source:134:13: note: expanded from macro 'REGISTER_INDEX_OP' kernel void index_ ## INDEX_OP_TYPE<DTYPE, IDX_DTYPE>( \ ^ <scratch space>:9:1: note: expanded from here index_select ^ program_source:20:13: note: candidate template ignored: substitution failure [with T = char, OffsetsT = unsigned long __attribute__((ext_vector_type(3)))]: type 'unsigned long const constant * __attribute__((ext_vector_type(3)))' is not valid for attribute 'buffer' kernel void index_select( ^ program_source:168:1: error: type 'const constant ulong3 *' is not valid for attribute 'buffer' REGISTER_INDEX_OP_ALL_DTYPES(select); ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ program_source:162:5: note: expanded from macro 'REGISTER_INDEX_OP_ALL_DTYPES' REGISTER_INDEX_OP(16bit, idx64, short, INDEX_OP_TYPE, ulong3); \ ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ program_source:138:5: note: expanded from macro 'REGISTER_INDEX_OP' constant IDX_DTYPE * offsets [[buffer(3)]], \ ^ ~~~~~~~~~ program_source:168:1: note: type 'ulong3' (vector of 3 'unsigned long' values) cannot be used in buffer pointee type program_source:162:59: note: expanded from macro 'REGISTER_INDEX_OP_ALL_DTYPES' REGISTER_INDEX_OP(16bit, idx64, short, INDEX_OP_TYPE, ulong3); \ ^ program_source:168:1: error: explicit instantiation of 'index_select' does not refer to a function template, variable template, member function, member class, or static data member REGISTER_INDEX_OP_ALL_DTYPES(select); ^ program_source:162:5: note: expanded from macro 'REGISTER_INDEX_OP_ALL_DTYPES' REGISTER_INDEX_OP(16bit, idx64, short, INDEX_OP_TYPE, ulong3); \ ^ program_source:134:13: note: expanded from macro 'REGISTER_INDEX_OP' kernel void index_ ## INDEX_OP_TYPE<DTYPE, IDX_DTYPE>( \ ^ <scratch space>:17:1: note: expanded from here index_select ^ .... ... program_source:248:13: note: candidate template ignored: substitution failure [with T = metal::_atomic<int, void>, E = int, OffsetsT = unsigned long __attribute__((ext_vector_type(3)))]: type 'unsigned long const constant * __attribute__((ext_vector_type(3)))' is not valid for attribute 'buffer' kernel void index_put_accumulate_native_dtypes( ^ }
解决方案
1. 降级PyTorch到兼容版本
你的Radeon Pro 555属于AMD GCN 4架构,PyTorch nightly版本使用了较新的Metal特性,老显卡不支持。换用PyTorch 2.0.1稳定版,这个版本的MPS实现兼容老款AMD显卡:
pip install torch==2.0.1 torchvision==0.15.2 torchaudio==2.0.2
2. 重新验证MPS支持
安装完成后,再次运行验证代码,确认MPS可以正常工作:
import torch if torch.backends.mps.is_available(): mps_device = torch.device("mps") x = torch.ones(1, device=mps_device) print(x) else: print("MPS device not found.")
3. 配置Stable Diffusion使用MPS
在Stable Diffusion的启动脚本里指定MPS设备:
import torch device = torch.device("mps" if torch.backends.mps.is_available() else "cpu")
或者在启动命令里加参数--device mps(不同版本的Stable Diffusion参数可能略有不同,按需调整)。
4. 更新系统补丁
把Ventura系统更新到最新补丁版本(比如13.6及以上),Apple会在补丁里修复部分Metal兼容性问题。
内容的提问来源于stack exchange,提问作者Pica
相关产品推荐
相关产品推荐

