해당 글은 TensorFlow lite를 ios용으로 로컬에서 사용하기 위해 static library로 빌드하는 절차 (원문)를 다루고 있음
해당 글은 cpu만을 사용하는 텐서플로우로 빌드
깃허브에서 Tensorflow 저장소를 로컬에 저장
소스 다운 후 원하는 버전으로 체크아웃
이 글에서는 master 브랜치의 가장 최근 커밋으로 진행
버전에 따라 M1 simulator 빌드가 불가능 할 수 있음
V1의 경우 가장 마지막인 1.15버전도 M1 시뮬레이터 빌드 옵션이 없음
bazelisk는 현재 필요한 bazel 버전을 알아서 찾아서 설치해 주는 툴 (링크)
이후 원문에서 나오는 bazel 명령어는 bazelisk로 대체
Homebrew를 통해 설치
brew install bazelisk
터미널을 통해 저장소가 위치한 디렉토리로 이동
이후 아래 명령어를 통해 .configure 실행
XXX@XXXs-MacBook-Pro tensorflow % ./configure
아래는 설정 과정
You have bazel 5.1.1 installed.
Please specify the location of python. [Default is /Library/Developer/CommandLineTools/usr/bin/python3]:
Found possible Python library paths:
/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/site-packages
Please input the desired Python library path to use. Default is [/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/site-packages]
Do you wish to build TensorFlow with ROCm support? [y/N]: N
No ROCm support will be enabled for TensorFlow.
Do you wish to build TensorFlow with CUDA support? [y/N]: N
No CUDA support will be enabled for TensorFlow.
Do you wish to download a fresh release of clang? (Experimental) [y/N]: N
Clang will not be downloaded.
Please specify optimization flags to use during compilation when bazel option "--config=opt" is specified [Default is -Wno-sign-compare]:
Would you like to interactively configure ./WORKSPACE for Android builds? [y/N]: N
Not configuring the WORKSPACE for Android builds.
Do you wish to build TensorFlow with iOS support? [y/N]: y
iOS support will be enabled for TensorFlow.
파인더에서 command + shift + . 을 눌러 숨겨진 파일을 보이게 설정
tensorflow 저장소의 최상위 디렉토리에서 .bazelrc 파일을 텍스트편집기로 열면 빌드 옵션 확인 가능
이 중 ios 빌드 옵션 확인
# iOS configs for each architecture and the fat binary builds.
build:ios --apple_platform_type=ios
build:ios --apple_bitcode=embedded --copt=-fembed-bitcode
build:ios --copt=-Wno-c++11-narrowing
build:ios_armv7 --config=ios
build:ios_armv7 --cpu=ios_armv7
build:ios_arm64 --config=ios
build:ios_arm64 --cpu=ios_arm64
build:ios_sim_arm64 --config=ios
build:ios_sim_arm64 --cpu=ios_sim_arm64
build:ios_i386 --config=ios
build:ios_i386 --cpu=ios_i386
build:ios_x86_64 --config=ios
build:ios_x86_64 --cpu=ios_x86_64
build:ios_fat --config=ios
build:ios_fat --ios_multi_cpus=armv7,arm64,i386,x86_64
원문에 나와있는 예시 스크립트와 조합하여 원하는 아키텍쳐의 빌드 파일을 생성 가능
--apple_bitcode=embedded --copt=-fembed-bitcode
해당 옵션은 비트코드를 설정하여 빌드하는 옵션
기본적으로 포함하여 빌드하는 것을 권장
텐서플로우 빌드시 cpu 로드율 99.9% 찍음
원문에서는 bazel을 사용하나 이 글에서는 bazelisk 명령어 활용
예시
# for Device
bazelisk build --apple_bitcode=embedded --copt=-fembed-bitcode --ios_multi_cpus=armv7,arm64 -c opt //tensorflow/lite/ios:TensorFlowLiteC_static_framework
# for Simulator
bazelisk build --apple_bitcode=embedded --copt=-fembed-bitcode --cpu=ios_sim_arm64 -c opt //tensorflow/lite/ios:TensorFlowLiteC_static_framework
bazelisk build --apple_bitcode=embedded --copt=-fembed-bitcode --cpu=ios_x86_64 -c opt //tensorflow/lite/ios:TensorFlowLiteC_static_framework
i386 아키텍쳐는 M1 컴퓨터에서는 안되는 것으로 보임
(아래 에러 발생)
In file included from external/eigen_archive/unsupported/Eigen/CXX11/ThreadPool:67:
external/eigen_archive/unsupported/Eigen/CXX11/src/ThreadPool/NonBlockingThreadPool.h:468:5: error: thread-local storage is not supported for the current target
EIGEN_THREAD_LOCAL PerThread per_thread_;
^
external/eigen_archive/unsupported/Eigen/CXX11/src/ThreadPool/ThreadLocal.h:22:35: note: expanded from macro 'EIGEN_THREAD_LOCAL'
#define EIGEN_THREAD_LOCAL static thread_local
^
1 error generated.
Error in child process '/usr/bin/xcrun'. 1
Target //tensorflow/lite/ios:TensorFlowLiteC_static_framework failed to build
Use --verbose_failures to see the command lines of failed build steps.
INFO: Elapsed time: 6.418s, Critical Path: 2.00s
INFO: 434 processes: 168 internal, 266 local.
FAILED: Build did NOT complete successfully
simulator는 ios_multi_cpus 옵션으로 한꺼번에 빌드시 에러 발생
각 빌드가 끝나면 결과 파일의 위치가 터미널에 표시됨
Target //tensorflow/lite/ios:TensorFlowLiteC_static_framework up-to-date:
bazel-out/applebin_ios-ios_armv7-opt-ST-b81692c76310/bin/tensorflow/lite/ios/TensorFlowLiteC_static_framework.zip
시뮬레이터 빌드파일은 아키텍쳐별로 분리되어 있기 때문에 lipo 명령어를 통해 하나의 프레임워크 생성