安裝環(huán)境
- OS Platform and Distribution
macos Ventura 13.1
- Architecture
Intel x86_64
- Tensorflow Version
tf 2.11.0
- Python version
3.9.13
- Bazel version
5.3.0
- Xcode version
14.2
從源代碼構(gòu)建
macOS設(shè)置
- miniconda創(chuàng)建虛擬環(huán)境及tensorflow依賴項(xiàng)安裝
conda create -n tf python==3.9.13
conda activate tf
pip install -U pip numpy wheel
pip install -U keras_preprocessing --no-deps
- 安裝Bazel
brew install bazel
- 下載 TensorFlow 源代碼
git clone https://github.com/tensorflow/tensorflow.git
cd tensorflow
git checkout r2.11
配置 build
# Check whether script is executing in a VirtualEnv or Conda environment
if [ -z "$VIRTUAL_ENV" ] && [ -z "$CONDA_PREFIX" ] ; then
echo "VirtualEnv or Conda env is not activated"
exit -1
fi
# Set the virtual environment path
if ! [ -z "$VIRTUAL_ENV" ] ; then
VENV_PATH=$VIRTUAL_ENV
elif ! [ -z "$CONDA_PREFIX" ] ; then
VENV_PATH=$CONDA_PREFIX
fi
# Set the bin and lib directories
VENV_BIN=$VENV_PATH/bin
VENV_LIB=$VENV_PATH/lib
# bazel tf needs these env vars
export PYTHON_BIN_PATH=$VENV_BIN/python
export PYTHON_LIB_PATH=`ls -d $VENV_LIB/*/ | grep python`
# Set the native architecture optimization flag, which is a default
COPT="--copt=-march=native"
# Determine the available features of your CPU
raw_cpu_flags=`sysctl -a | grep machdep.cpu.features | cut -d ":" -f 2 | tr '[:upper:]' '[:lower:]'`
# Append each of your CPU's features to the list of optimization flags
for cpu_feature in $raw_cpu_flags
do
case "$cpu_feature" in
"sse4.1" | "sse4.2" | "ssse3" | "fma" | "cx16" | "popcnt" | "maes")
COPT+=" --copt=-m$cpu_feature"
;;
"avx1.0")
COPT+=" --copt=-mavx"
;;
*)
# noop
;;
esac
done
echo $COPT
# First ensure a clear working directory in case you've run bazel previously
bazel clean --expunge
# Run TensorFlow configuration (accept defaults unless you have a need)
python configure.py
# Build the TensorFlow pip package
bazel build -c opt $COPT -k //tensorflow/tools/pip_package:build_pip_package
bazel-bin/tensorflow/tools/pip_package/build_pip_package /tmp/tensorflow_pkg
安裝測(cè)試軟件包
cd ../
pip uninstall tensorflow # remove current version
pip install /tmp/tensorflow_pkg/tensorflow-version-tags.whl
python -c "import tensorflow as tf; print(tf.__version__)"
編譯異常處理
1、第三方源碼包無(wú)法下載
- 問(wèn)題現(xiàn)象:
Download from https://storage.googleapis.com/mirror.tensorflow.org/github.com/google/XNNPACK/archive/a50369c0fdd15f0f35b1a91c964644327a88d480.zip failed: class java.io.IOException connect timed out
-
解決方法:
- 從該鏈接直接下載文件包,https://github.com/google/XNNPACK/archive/a50369c0fdd15f0f35b1a91c964644327a88d480.zip,(國(guó)內(nèi)下載不了,方法不再贅述)。
- 本地搭建http服務(wù)器,用于存放無(wú)法下載的程序包,提供本地下載地址
- 修改bzl文件,增加本地鏈接,例如以上鏈接是在tensorflow\tensorflow\workspace2.bzl中
原文內(nèi)容:
tf_http_archive(
name = "XNNPACK",
sha256 = "7a16ab0d767d9f8819973dbea1dc45e4e08236f89ab702d96f389fdc78c5855c",
strip_prefix = "XNNPACK-e8f74a9763aa36559980a0c2f37f587794995622",
urls = tf_mirror_urls("https://github.com/google/XNNPACK/archive/e8f74a9763aa36559980a0c2f37f587794995622.zip"),
)
替換為:
tf_http_archive(
name = "XNNPACK",
sha256 = "ca3a5316b8161214f8f22a578fb638f1fccd0585eee40301363ffd026310379a",
strip_prefix = "XNNPACK-a50369c0fdd15f0f35b1a91c964644327a88d480",
urls = ["https://storage.googleapis.com/mirror.tensorflow.org/github.com/google/XNNPACK/archive/a50369c0fdd15f0f35b1a91c964644327a88d480.zip",
"http://127.0.0.1:8080/tensorflow/XNNPACK-a50369c0fdd15f0f35b1a91c964644327a88d480.zip"
],
)
2、Go無(wú)法下載安裝
-
問(wèn)題現(xiàn)象:
Download from https://golang.org/dl/?mode=json&include=all failed: class java.io.IOException connect timed out
-
解決方法:
- 該錯(cuò)誤io_bazel_rules_go需要下載go軟件包引起的,需要修改io_bazel_rules_go這個(gè)包中的sdk.bzl文件。從tensorflow\tensorflow\workspace2.bzl中找到這個(gè)包的下載地址,下載到本地,放到本地http服務(wù)器
- 修改tensorflow\tensorflow\workspace2.bzl文件,注意sha256也需要修改,可以通過(guò)tensorflow編譯錯(cuò)誤獲取,同時(shí)需要?jiǎng)h除/var/tmp/bazel[loginName]/cache/repos/v1/content_addressable/sha256下面和sha256同名文件,避免無(wú)法使用修改后的文件:
原文內(nèi)容:
tf_http_archive(
name = "io_bazel_rules_go",
sha256 = "80cbfe287bacbe6cec97f0446a413fee7c21dafda3981b9de71a35eebbc89e1f",
urls = tf_mirror_urls("https://github.com/bazelbuild/rules_go/releases/download/v0.34.0/rules_go-v0.34.0.zip"),
],
)
tf_http_archive(
name = "io_bazel_rules_go",
sha256 = "80cbfe287bacbe6cec97f0446a413fee7c21dafda3981b9de71a35eebbc89e1f",
#urls = tf_mirror_urls("https://github.com/bazelbuild/rules_go/releases/download/v0.34.0/rules_go-v0.34.0.zip"),
urls = ["http://127.0.0.1:8080/tensorflow/rules_go-v0.34.0.zip"
],
)
2、下載go.json文件,放到本地http服務(wù)器,sha256值需要計(jì)算傳入,可以通過(guò)替換bzl中某個(gè)包,讓tensorflow編譯時(shí)報(bào)錯(cuò),提示出該文件的sha256值
3、解壓下載的zip包,打開(kāi) rules*_go-v0.34.0\go\private\sdk.bzl,修改以下內(nèi)容*
原文內(nèi)容:
ctx.download(
url = [
"https://golang.org/dl/?mode=json&include=all",
"https://golang.google.cn/dl/?mode=json&include=all",
],
output = "versions.json",
)
......
_go_download_sdk = repository_rule(
implementation = _go_download_sdk_impl,
attrs = {
"goos": attr.string(),
"goarch": attr.string(),
"sdks": attr.string_list_dict(),
"urls": attr.string_list(default = ["https://dl.google.com/go/{}"]),
"version": attr.string(),
"strip_prefix": attr.string(default = "go"),
},
)
替換為
ctx.download(
url = [
#"https://golang.org/dl/?mode=json&include=all",
#"https://golang.google.cn/dl/?mode=json&include=all",
"http://127.0.0.1:8080/tensorflow/go.json",
],
output = "versions.json",
sha256 = "47bd74d7eac125a61194d1aa2688c2da3e3bc1bb0981d41a7dd0e9f9cc88d57c",
)
......
_go_download_sdk = repository_rule(
implementation = _go_download_sdk_impl,
attrs = {
"goos": attr.string(),
"goarch": attr.string(),
"sdks": attr.string_list_dict(),
"urls": attr.string_list(default = ["http://127.0.0.1:8080/tensorflow/{}"]),
"version": attr.string(),
"strip_prefix": attr.string(default = "go"),
},
)
4、重新zip壓縮rules_go-v0.34.0.zip,需要根據(jù)編譯錯(cuò)誤提示更改sha256
3、ld: malformed trie, node past end file ‘bazel-out/host/bin/_solib_darwin_x86_64/libtensorflow_Spython_S_Upywrap_Utensorflow_Uinternal.so’
1、該問(wèn)題是由于xcode14升級(jí)引起的,將ld切換成13版本可以暫時(shí)解決該問(wèn)題,查看該鏈接
2、從蘋(píng)果官網(wǎng)下載xcode13,解壓后,將ld拷貝到本機(jī)目錄下,本機(jī)的備份
4、安裝打包好的wheel時(shí),提示:error:tensorflow-*.whl is not suported wheel on this platform
1、tensorflow, 版本不對(duì),目前編譯打包成功的時(shí)v2.11.0版本
2、python版本不對(duì),必須用編譯打包時(shí)使用的python pip安裝文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-861185.html
5、dlopen(/Users/davidlaxer/tensorflow-metal/lib/python3.8/site-packages/tensorflow-plugins/libmetal_plugin.dylib, 6): Symbol not found:
1、本地原因是之前安裝了tensorflow-macos,需要卸載之前安裝的所有tensorflow依賴包文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-861185.html
到了這里,關(guān)于MacOS 系統(tǒng)成功安裝 tensorflow 步驟的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!