如何在 macOS 12 上的 M1 Mac 上安装 trax、jax、jaxlib?

art*_*omp 6 python anaconda jax trax apple-m1

trax

\n

作为 新手trax,我尝试在本地运行它(macOS 12.1、Apple Silicon ARM M1 处理器、8GB RAM、Anaconda),但遇到了一些问题。

\n

在 python 3.8.5 的环境中,我安装了在 (Anaconda) conda 环境中trax运行。后来,我在尝试使用以下命令在代码中pip3 install trax==1.3.9导入图层时遇到了问题traxfrom trax import layers as tl

\n
RuntimeError: This version of jaxlib was built using AVX instructions, which your CPU and/or operating system do not support. You may be able work around this issue by building jaxlib from source.\n
Run Code Online (Sandbox Code Playgroud)\n

我赶紧用python 3.10启动了一个新的conda环境。然后在尝试安装时遇到了问题trax

\n
macos ERROR: Could not find a version that satisfies the requirement tensorflow-text (from trax) (from versions: none)\nERROR: No matching distribution found for tensorflow-text\n
Run Code Online (Sandbox Code Playgroud)\n

然后我用 python 3.9 创建了一个新环境。安装很顺利,但随后在导入图层时遇到了相同的错误:

\n
RuntimeError: This version of jaxlib was built using AVX instructions, which your CPU and/or operating system do not support. You may be able work around this issue by building jaxlib from source.\n
Run Code Online (Sandbox Code Playgroud)\n

安装jaxlib之前jaxtrax

\n

然后我尝试按照这些说明从源代码构建 jaxlib并收到此错误:

\n
Building XLA and installing it in the jaxlib source tree...\n./bazel-4.2.1-darwin-x86_64 run --verbose_failures=true --config=avx_posix --config=mkl_open_source_only :build_wheel -- --output_path=/my path/jax/dist --cpu=x86_64\nERROR: bazel does not currently work properly from paths containing spaces (/my path/jax).\nb\'\'\nTraceback (most recent call last):\n  File "/my path/jax/build/build.py", line 524, in <module>\n    main()\n  File "/my path/jax/build/build.py", line 519, in main\n    shell(command)\n  File "/my path/jax/build/build.py", line 53, in shell\n    output = subprocess.check_output(cmd)\n  File "/myuserpath/opt/anaconda3/envs/mytraxenv/lib/python3.9/subprocess.py", line 424, in check_output\n    return run(*popenargs, stdout=PIPE, timeout=timeout, check=True,\n  File "/myuserpath/opt/anaconda3/envs/mytraxenv/lib/python3.9/subprocess.py", line 528, in run\n    raise CalledProcessError(retcode, process.args,\nsubprocess.CalledProcessError: Command \'[\'./bazel-4.2.1-darwin-x86_64\', \'run\', \'--verbose_failures=true\', \'--config=avx_posix\', \'--config=mkl_open_source_only\', \':build_wheel\', \'--\', \'--output_path=/my path/jax/dist\', \'--cpu=x86_64\']\' returned non-zero exit status 36.\n
Run Code Online (Sandbox Code Playgroud)\n

强调我最初错过的部分:bazel 目前无法在包含空格的路径(/my path/jax)中正常工作

\n

我将/my path/目录移动到没有空格的路径/mypath/。删除并重新下载 jax 目录。尽管如此,构建(针对CPU)与python build/build.py失败了:

\n
ERROR: /private/var/tmp/_bazel_a/2caf512c3c5e3f3f654bc58b48b8333a/external/llvm-project/llvm/BUILD.bazel:610:11: Generating code from table: include/llvm/IR/Intrinsics.td @llvm-project//llvm:intrinsic_XCore_gen__gen_intrinsic_enums__intrinsic_prefix_xcore_genrule failed: (Illegal instruction): bash failed: error executing command \n  (cd /private/var/tmp/_bazel_a/2caf512c3c5e3f3f654bc58b48b8333a/execroot/__main__ && \\\n  exec env - \\\n    PATH=/myuserpath/opt/anaconda3/envs/mytraxenv/bin:/myuserpath/opt/anaconda3/condabin:/opt/homebrew/bin:/opt/homebrew/sbin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Library/TeX/texbin:/Library/Apple/usr/bin \\\n  /bin/bash -c \'source external/bazel_tools/tools/genrule/genrule-setup.sh; bazel-out/darwin-opt/bin/external/llvm-project/llvm/llvm-tblgen -I external/llvm-project/llvm/include -I external/llvm-project/clang/include -I $(dirname external/llvm-project/llvm/include/llvm/IR/Intrinsics.td) -gen-intrinsic-enums -intrinsic-prefix=xcore external/llvm-project/llvm/include/llvm/IR/Intrinsics.td  -o bazel-out/darwin-opt/bin/external/llvm-project/llvm/include/llvm/IR/IntrinsicsXCore.h\')\nExecution platform: @local_execution_config_platform//:platform\n/bin/bash: line 1: 11140 Illegal instruction: 4  bazel-out/darwin-opt/bin/external/llvm-project/llvm/llvm-tblgen -I external/llvm-project/llvm/include -I external/llvm-project/clang/include -I $(dirname external/llvm-project/llvm/include/llvm/IR/Intrinsics.td) -gen-intrinsic-enums -intrinsic-prefix=xcore external/llvm-project/llvm/include/llvm/IR/Intrinsics.td -o bazel-out/darwin-opt/bin/external/llvm-project/llvm/include/llvm/IR/IntrinsicsXCore.h\nTarget //build:build_wheel failed to build\nINFO: Elapsed time: 620.950s, Critical Path: 45.35s\nINFO: 589 processes: 132 internal, 457 local.\nFAILED: Build did NOT complete successfully\nERROR: Build failed. Not running target\nFAILED: Build did NOT complete successfully\nb\'\'\nTraceback (most recent call last):\n  File "/mypath/jax/build/build.py", line 524, in <module>\n    main()\n  File "/mypath/jax/build/build.py", line 519, in main\n    shell(command)\n  File "/mypath/jax/build/build.py", line 53, in shell\n    output = subprocess.check_output(cmd)\n  File "/myuserpath/opt/anaconda3/envs/mytraxenv/lib/python3.9/subprocess.py", line 424, in check_output\n    return run(*popenargs, stdout=PIPE, timeout=timeout, check=True,\n  File "/myuserpath/opt/anaconda3/envs/mytraxenv/lib/python3.9/subprocess.py", line 528, in run\n    raise CalledProcessError(retcode, process.args,\nsubprocess.CalledProcessError: Command \'[\'./bazel-4.2.1-darwin-x86_64\', \'run\', \'--verbose_failures=true\', \'--config=avx_posix\', \'--config=mkl_open_source_only\', \':build_wheel\', \'--\', \'--output_path=/mypath/jax/dist\', \'--cpu=x86_64\']\' returned non-zero exit status 1.\n
Run Code Online (Sandbox Code Playgroud)\n

再次尝试了几次(删除并重新下载了 jax 目录),并且出现了相同的行 528.../lib/python3.9/subprocess.py导致了问题,但输出虽然主要是上面的内容,但有时略有不同。让我怀疑内存有问题,因为我(诚然)已经几周没有重新启动我的机器了,并且它开始变得无响应缓慢。

\n

我将 XCode 命令行工具更新到版本 12.2(非常确定)。

\n

我重新启动了我的 (8GB) 机器。删除并重新下载 jax 目录。我使用 homebrew 安装了 bazel 版本 5.0.0,以防有帮助。我有点担心它不断下载适用于我的 ARM 处理器的 x86 版本。bazel安装顺利。

\n

从这些指示重新开始。这jaxlib构建明确表示需要 bazel 的早期版本 (4.2.1),并像以前一样下载了它:

\n
b\'\\x1b[31mERROR: The project you\\\'re trying to build requires Bazel 4.2.1 (specified in /mypath/jax/.bazelversion), but it wasn\\\'t found in /opt/homebrew/Cellar/bazel/5.0.0/libexec/bin.\\x1b[0m\\n\\nBazel binaries for all official releases can be downloaded from here:\\n  https://github.com/bazelbuild/bazel/releases\\n\\nYou can download the required version directly using this command:\\n  (cd "/opt/homebrew/Cellar/bazel/5.0.0/libexec/bin" && curl -fLO https://releases.bazel.build/4.2.1/release/bazel-4.2.1-darwin-x86_64 && chmod +x bazel-4.2.1-darwin-x86_64)\\n\'\n
Run Code Online (Sandbox Code Playgroud)\n

再次,同一行 528 中出现不同的错误。现在显示完整运行:

\n
b\'\\x1b[31mERROR: The project you\\\'re trying to build requires Bazel 4.2.1 (specified in /mypath/jax/.bazelversion), but it wasn\\\'t found in /opt/homebrew/Cellar/bazel/5.0.0/libexec/bin.\\x1b[0m\\n\\nBazel binaries for all official releases can be downloaded from here:\\n  https://github.com/bazelbuild/bazel/releases\\n\\nYou can download the required version directly using this command:\\n  (cd "/opt/homebrew/Cellar/bazel/5.0.0/libexec/bin" && curl -fLO https://releases.bazel.build/4.2.1/release/bazel-4.2.1-darwin-x86_64 && chmod +x bazel-4.2.1-darwin-x86_64)\\n\'\nDownloading bazel from: https://github.com/bazelbuild/bazel/releases/download/4.2.1/bazel-4.2.1-darwin-x86_64\nbazel-4.2.1-darwin-x86_64 [########################################] 100%\nBazel binary path: ./bazel-4.2.1-darwin-x86_64\nBazel version: 4.2.1\nPython binary path: /myuserpath/opt/anaconda3/envs/mytraxenv/bin/python\nPython version: 3.9\nNumPy version: 1.21.2\nMKL-DNN enabled: yes\nTarget CPU: x86_64\nTarget CPU features: release\nCUDA enabled: no\nTPU enabled: no\nROCm enabled: no\n\nBuilding XLA and installing it in the jaxlib source tree...\n./bazel-4.2.1-darwin-x86_64 run --verbose_failures=true --config=avx_posix --config=mkl_open_source_only :build_wheel -- --output_path=/mypath/jax/dist --cpu=x86_64\nINFO: Options provided by the client:\n  Inherited \'common\' options: --isatty=0 --terminal_columns=80\nINFO: Reading rc options for \'run\' from /mypath/jax/.bazelrc:\n  Inherited \'common\' options: --experimental_repo_remote_exec\nINFO: Reading rc options for \'run\' from /mypath/jax/.bazelrc:\n  Inherited \'build\' options: --apple_platform_type=macos --macos_minimum_os=10.9 --announce_rc --define open_source_build=true --spawn_strategy=standalone --enable_platform_specific_config --define=no_aws_support=true --define=no_gcp_support=true --define=no_hdfs_support=true --define=no_kafka_support=true --define=no_ignite_support=true --define=grpc_no_ares=true -c opt --config=short_logs --copt=-DMLIR_PYTHON_PACKAGE_PREFIX=jaxlib.mlir.\nINFO: Reading rc options for \'run\' from /mypath/jax/.jax_configure.bazelrc:\n  Inherited \'build\' options: --strategy=Genrule=standalone --repo_env PYTHON_BIN_PATH=/myuserpath/opt/anaconda3/envs/mytraxenv/bin/python --action_env=PYENV_ROOT --python_path=/myuserpath/opt/anaconda3/envs/mytraxenv/bin/python --distinct_host_configuration=false\nINFO: Found applicable config definition build:short_logs in file /mypath/jax/.bazelrc: --output_filter=DONT_MATCH_ANYTHING\nINFO: Found applicable config definition build:avx_posix in file /mypath/jax/.bazelrc: --copt=-mavx --host_copt=-mavx\nINFO: Found applicable config definition build:mkl_open_source_only in file /mypath/jax/.bazelrc: --define=tensorflow_mkldnn_contraction_kernel=1\nINFO: Found applicable config definition build:macos in file /mypath/jax/.bazelrc: --config=posix\nINFO: Found applicable config definition build:posix in file /mypath/jax/.bazelrc: --copt=-fvisibility=hidden --copt=-Wno-sign-compare --cxxopt=-std=c++14 --host_cxxopt=-std=c++14\nLoading: \nLoading: 0 packages loaded\nAnalyzing: target //build:build_wheel (1 packages loaded, 0 targets configured)\nAnalyzing: target //build:build_wheel (8 packages loaded, 286 targets configured)\nAnalyzing: target //build:build_wheel (10 packages loaded, 4271 targets configured)\nAnalyzing: target //build:build_wheel (10 packages loaded, 4271 targets configured)\nAnalyzing: target //build:build_wheel (11 packages loaded, 4494 targets configured)\nINFO: Analyzed target //build:build_wheel (11 packages loaded, 7417 targets configured).\n\nINFO: Found 1 target...\n[0 / 3] [Prepa] BazelWorkspaceStatusAction stable-status.txt\n[21 / 233] Compiling src/google/protobuf/generated_enum_util.cc; 2s local ... (6 actions, 3 running)\n[28 / 233] Compiling src/google/protobuf/extension_set.cc; 4s local ... (8 actions, 7 running)\n[114 / 523] Compiling src/google/protobuf/generated_message_util.cc; 2s local ... (8 actions running)\n[140 / 594] Compiling platform/c++11/src/nsync_semaphore_mutex.cc; 0s local ... (8 actions, 7 running)\n[237 / 597] Compiling src/google/protobuf/util/message_differencer.cc; 0s local ... (8 actions running)\n[252 / 597] Compiling src/google/protobuf/util/message_differencer.cc; 4s local ... (8 actions running)\n[272 / 597] Compiling src/google/protobuf/descriptor.pb.cc; 8s local ... (8 actions running)\n[296 / 597] Compiling src/google/protobuf/descriptor.cc; 12s local ... (8 actions running)\n[315 / 597] Compiling src/google/protobuf/descriptor_database.cc; 2s local ... (5 actions running)\n[487 / 1,963] Compiling src/compiler/python_generator.cc; 2s local ... (8 actions running)\n[579 / 3,117] Compiling tensorflow/compiler/xla/service/cpu/runtime_single_threaded_conv3d.cc; 0s local ... (8 actions, 7 running)\n[619 / 3,173] Compiling tensorflow/compiler/xla/service/cpu/runtime_single_threaded_conv3d.cc; 8s local ... (8 actions running)\n[687 / 3,270] Compiling tensorflow/compiler/xla/service/cpu/runtime_single_threaded_conv3d.cc; 17s local ... (8 actions running)\n[774 / 3,464] Compiling tensorflow/compiler/xla/service/cpu/runtime_single_threaded_conv2d.cc; 27s local ... (8 actions running)\n[1,204 / 4,860] Compiling tensorflow/compiler/xla/service/cpu/runtime_single_threaded_conv2d.cc; 39s local ... (8 actions running)\n[1,255 / 4,916] Compiling tensorflow/compiler/xla/service/cpu/runtime_matmul.cc; 52s local ... (8 actions running)\n[1,340 / 5,042] Compiling tensorflow/compiler/xla/service/cpu/runtime_matmul.cc; 68s local ... (8 actions running)\n[1,456 / 5,156] Compiling tensorflow/compiler/xla/service/cpu/runtime_matmul.cc; 86s local ... (8 actions, 7 running)\n[1,661 / 5,704] Compiling tensorflow/compiler/xla/service/cpu/runtime_matmul.cc; 107s local ... (8 actions, 7 running)\n[1,688 / 5,704] Compiling tensorflow/compiler/xla/service/cpu/runtime_matmul.cc; 132s local ... (8 actions, 7 running)\n[1,721 / 5,704] Compiling tensorflow/compiler/xla/service/cpu/runtime_matmul.cc; 160s local ... (8 actions, 7 running)\n[2,106 / 6,584] Compiling tensorflow/compiler/xla/service/cpu/runtime_matmul.cc; 192s local ... (8 actions, 7 running)\n[2,342 / 7,067] Compiling tensorflow/compiler/xla/service/cpu/runtime_matmul.cc; 231s local ... (8 actions, 7 running)\n[2,378 / 7,067] Compiling tensorflow/compiler/xla/service/cpu/runtime_matmul.cc; 274s local ... (8 actions, 7 running)\n[2,422 / 7,067] Compiling src/cpu/rnn/ref_rnn.cpp; 75s local ... (8 actions, 7 running)\n[2,452 / 7,067] Compiling src/cpu/x64/gemm/f32/jit_avx512_core_f32_copy_at_kern_part1_autogen.cpp; 54s local ... (8 actions, 7 running)\n[2,500 / 7,067] Compiling src/cpu/x64/gemm/f32/jit_avx512_core_f32_copy_at_kern_part1_autogen.cpp; 119s local ... (8 actions, 7 running)\n[2,577 / 7,067] Compiling src/common/memory_zero_pad.cpp; 74s local ... (8 actions, 7 running)\nERROR: /private/var/tmp/_bazel_a/f5e9a3325f07a1f02c52d821857db47c/external/org_tensorflow/tensorflow/compiler/xla/BUILD:69:17: ProtoCompile external/org_tensorflow/tensorflow/compiler/xla/xla.pb.h failed: (Illegal instruction): protoc failed: error executing command \n  (cd /private/var/tmp/_bazel_a/f5e9a3325f07a1f02c52d821857db47c/execroot/__main__ && \\\n  exec env - \\\n    PATH=/myuserpath/opt/anaconda3/envs/mytraxenv/bin:/myuserpath/opt/anaconda3/condabin:/opt/homebrew/bin:/opt/homebrew/sbin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Library/TeX/texbin:/Library/Apple/usr/bin \\\n  bazel-out/darwin-opt/bin/external/com_google_protobuf/protoc \'--cpp_out=bazel-out/darwin-opt/bin/external/org_tensorflow\' -Iexternal/org_tensorflow -Ibazel-out/darwin-opt/bin/external/org_tensorflow -Ibazel-out/darwin-opt/bin/external/com_google_protobuf/_virtual_imports/any_proto -Ibazel-out/darwin-opt/bin/external/com_google_protobuf/_virtual_imports/source_context_proto -Ibazel-out/darwin-opt/bin/external/com_google_protobuf/_virtual_imports/type_proto -Ibazel-out/darwin-opt/bin/external/com_google_protobuf/_virtual_imports/api_proto -Ibazel-out/darwin-opt/bin/external/com_google_protobuf/_virtual_imports/descriptor_proto -Ibazel-out/darwin-opt/bin/external/com_google_protobuf/_virtual_imports/compiler_plugin_proto -Ibazel-out/darwin-opt/bin/external/com_google_protobuf/_virtual_imports/duration_proto -Ibazel-out/darwin-opt/bin/external/com_google_protobuf/_virtual_imports/empty_proto -Ibazel-out/darwin-opt/bin/external/com_google_protobuf/_virtual_imports/field_mask_proto -Ibazel-out/darwin-opt/bin/external/com_google_protobuf/_virtual_imports/struct_proto -Ibazel-out/darwin-opt/bin/external/com_google_protobuf/_virtual_imports/timestamp_proto -Ibazel-out/darwin-opt/bin/external/com_google_protobuf/_virtual_imports/wrappers_proto external/org_tensorflow/tensorflow/compiler/xla/xla.proto)\nExecution platform: @local_execution_config_platform//:platform\nTarget //build:build_wheel failed to build\nINFO: Elapsed time: 631.189s, Critical Path: 283.35s\nINFO: 2563 processes: 935 internal, 1628 local.\nFAILED: Build did NOT complete successfully\nERROR: Build failed. Not running target\nFAILED: Build did NOT complete successfully\nb\'\'\nTraceback (most recent call last):\n  File "/mypath/jax/build/build.py", line 524, in <module>\n    main()\n  File "/mypath/jax/build/build.py", line 519, in main\n    shell(command)\n  File "/mypath/jax/build/build.py", line 53, in shell\n    output = subprocess.check_output(cmd)\n  File "/myuserpath/opt/anaconda3/envs/mytraxenv/lib/python3.9/subprocess.py", line 424, in check_output\n    return run(*popenargs, stdout=PIPE, timeout=timeout, check=True,\n  File "/myuserpath/opt/anaconda3/envs/mytraxenv/lib/python3.9/subprocess.py", line 528, in run\n    raise CalledProcessError(retcode, process.args,\nsubprocess.CalledProcessError: Command \'[\'./bazel-4.2.1-darwin-x86_64\', \'run\', \'--verbose_failures=true\', \'--config=avx_posix\', \'--config=mkl_open_source_only\', \':build_wheel\', \'--\', \'--output_path=/mypath/jax/dist\', \'--cpu=x86_64\']\' returned non-zero exit status 1.\n
Run Code Online (Sandbox Code Playgroud)\n

我不再尝试构建 jaxlib。2021年10月下旬,发布了兼容M1的jaxlib轮,于是我尝试了:

\n
pip install -U pip\npip install -U https://storage.googleapis.com/jax-releases/mac/jaxlib-0.1.74-cp39-none-macosx_11_0_arm64.whl\n
Run Code Online (Sandbox Code Playgroud)\n

但得到了

\n
ERROR: jaxlib-0.1.74-cp39-none-macosx_11_0_arm64.whl is not a supported wheel on this platform.\n
Run Code Online (Sandbox Code Playgroud)\n

尝试将 python 从 3.9 升级到 3.10,但收到相同的消息。

\n

通过在 python 中运行进行调试

\n
import platform\nprint(platform.machine())\n
Run Code Online (Sandbox Code Playgroud)\n

这表明我的 python 仍在 x86 架构上运行,因为“Anaconda 尚未提供 M1/ARM 的软件包”

\n

[在下面自己的答案中查看成功的(到目前为止)jaxlib安装]

\n

回到trax

\n

成功安装后jaxlibjax当尝试使用trax(miniforge\xe2\x80\x99s)安装时conda install trax,我得到:

\n
Collecting package metadata (current_repodata.json): done\nSolving environment: failed with initial frozen solve. Retrying with flexible solve.\nCollecting package metadata (repodata.json): done\nSolving environment: failed with initial frozen solve. Retrying with flexible solve.\n\nPackagesNotFoundError: The following packages are not available from current channels:\n\n  - trax\n\nCurrent channels:\n\n  - https://conda.anaconda.org/conda-forge/osx-arm64\n  - https://conda.anaconda.org/conda-forge/noarch\n\nTo search for alternate channels that may provide the conda package you\'re\nlooking for, navigate to\n\n    https://anaconda.org\n\nand use the search bar at the top of the page.\n
Run Code Online (Sandbox Code Playgroud)\n

我尝试按照trax其建议进行安装,但遇到了构建错误hp5y

\n
ERROR: Failed building wheel for h5py\nERROR: Could not build wheels for h5py, which is required to install pyproject.toml-based projects\n
Run Code Online (Sandbox Code Playgroud)\n

现在查看此错误,并考虑miniforge再次执行此操作,但使用 Anaconda 的通道来安装trax. 当我到达时我会更新。

\n

有任何指点或任何人设法trax通过任何不同方式在 M1 Mac 上安装它吗?

\n

art*_*omp 3

jaxlib+jax

在有人声称使用 miniforge 取得成功后,我阅读了这篇文章并观看了这篇文章,以澄清如何一起使用 Anaconda 和 miniforge。

我用Apple的arm64安装了miniforge:Apple Silicon方法。由于某种原因,当我运行 miniforge 时,即使我使用的是 zsh shell,conda init它也会设置初始化代码。~/.bash_profile我尝试手动将代码放入,~/.zprofile但它不会加载到交互式 shell 上,所以我最终将其放在 Anaconda 放置其初始化代码的位置,即~/.zshrc.

这使得 miniforge 成为默认管理器。按照上面非常有用的视频,我创建了一个~/.start_anaconda.sh脚本,这样我就可以使用 Anaconda 作为替代方案。

与迷你锻造我

这在安装中起作用了jaxlib

然后,我按照以下说明进行安装jax

pip install --upgrade pip
pip install --upgrade "jax[cpu]"
Run Code Online (Sandbox Code Playgroud)

这也有效。import jax请注意,在 python 中运行时,它当前会发出警告:

/mytraxenv/lib/python3.10/site-packages/jax/_src/lib/__init__.py:32: UserWarning: JAX on Mac ARM machines is experimental and minimally tested. Please see https://github.com/google/jax/issues/5501 in the event of problems.
  warnings.warn("JAX on Mac ARM machines is experimental and minimally tested. "
Run Code Online (Sandbox Code Playgroud)

trax

安装 trax 尚未成功。

  • 根据我的个人经验:尽量避免使用 Anaconda。即使没有发生 ARM 与 X86 的情况,您也可能会得到混合的结果。如果您使用支持arm的终端,您可以brew install Python并获得ARM版本。 (2认同)