Skip to content

Commit 22c4e94

Browse files
committed
Add tf-text tutorials.
1 parent e78e4db commit 22c4e94

File tree

5 files changed

+169
-1
lines changed

5 files changed

+169
-1
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.DS_store
2+
.idea/

README-zh.md

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# 用于 Apple Silicon 的 TensorFlow 库和扩展
2+
3+
这个仓库将提供需要编译才能构建的 TensorFlow 库和拓展的构建教程, 同时也提供预编译的`wheel`文件. 本教程提供[简体中文]()[English]()支持.
4+
5+
## 目前提供的教程👉
6+
7+
* [addons](https://github.com/tensorflow/addons)
8+
* [text](https://github.com/tensorflow/text)
9+
* 未完待续
10+

README.md

+10-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,10 @@
1-
# Libraries-and-Extensions-for-TensorFlow-for-Apple-Silicon
1+
# Libraries and Extensions for TensorFlow for Apple Silicon
2+
3+
This Repo will provide TensorFlow libraries and extended build tutorials that require compilation to build, as well as pre-compiled `wheel` files. This tutorial provides [English]() and [简体中文]() support.
4+
5+
## Currently available tutorials 👉🏻
6+
7+
* [addons](https://github.com/tensorflow/addons)
8+
* [text](https://github.com/tensorflow/text)
9+
* To be continued...
10+

tutorials/text/text-zh.md

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# 从源码构建TensorFlow Text
2+
3+
## 必要条件
4+
5+
这里假设了您有必要的类Unix知识, 已经在您的终端内安装好了[`brew`](https://brew.sh)[`conda`](https://github.com/conda-forge/miniforge), 这里不再赘述`brew``conda`安装和使用方法; 最重要的是, 这个教程完全基于Apple Silicon(比如M1, M1 Pro 或者 M1 Max)构建, 所以确保您手中的Mac是Apple Silicon.
6+
7+
## Step by Step
8+
9+
1. 创建新的环境并安装Apple提供的依赖项.
10+
11+
```shell
12+
conda create -n tensorflow-macos python=3.9 # 这里Python版本也可以使用Python 3.8
13+
conda activate tensorflow-macos
14+
conda install -c apple tensorflow-deps==2.8.0
15+
```
16+
17+
2. 安装`tensorFlow-macos``tensorflow-metal`插件.
18+
19+
```shell
20+
pip install tensorflow-macos
21+
pip install tensorflow-metal
22+
```
23+
24+
3. 安装`bazel 4.2.2`.
25+
26+
```shell
27+
wget https://raw.githubusercontent.com/Homebrew/homebrew-core/210bef2d570f0635e07009f4c5242b5e6645ae31/Formula/bazel.rb
28+
brew install ./bazel.rb
29+
bazel --version # 确保版本是4.2.2
30+
```
31+
32+
* 通常情况下`brew`安装的`bazel`会是最新版的, 最新版往往和`text`要求的版本不匹配, 这可能会出现很多意想不到的问题, 所以我们通过手动指定版本安装.
33+
34+
4. 下载并解压`text 2.8.1 `.
35+
36+
```shell
37+
wget https://github.com/tensorflow/text/archive/refs/tags/v2.8.1.zip
38+
unzip ./v2.8.1.zip
39+
cd text-2.8.1
40+
```
41+
42+
5. 修改源码的一些参数以此确保能正确构建.
43+
44+
* `oss_scripts/configure.sh `修改第49行为
45+
46+
```shell
47+
pip install tensorflow-macos==2.8.0
48+
```
49+
50+
* `oss_scripts/run_build.sh `注释掉第17行
51+
52+
```shell
53+
# source oss_scripts/prepare_tf_dep.sh
54+
```
55+
56+
* `oss_scripts/pip_package/setup.py` 修改第76, 77行为
57+
58+
```python
59+
'tensorflow>=2.8.0, <2.9.0; platform_machine != "arm64" or platform_system != "Darwin"',
60+
'tensorflow-macos>=2.8.0, <2.9.0; platform_machine == "arm64" and platform_system == "Darwin"'
61+
```
62+
63+
6. 运行脚本构建.
64+
65+
```shell
66+
./oss_scripts/run_build.sh
67+
```
68+
69+
## Tips&Refer
70+
71+
1. `text`需要和`tensorflow`的次要版本对应(比如`tensorflow-macos==2.7.0``tensorflow-text==2.7.3`)
72+
2. 编译过程中请保证你的网络稳定, 编译需要使用网络.
73+
3. [本人添加Apple Silicon支持的PR.](https://github.com/tensorflow/text/pull/756)
74+

tutorials/text/text.md

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# Build TensorFlow Text from source
2+
3+
## Necessary conditions
4+
5+
It is assumed here that you have the necessary Unix-Like knowledge, [`brew`](https://brew.sh) and [`conda`](https://github.com/conda-forge/miniforge) have been installed in your terminal, and the installation and use methods of `brew` and `conda` will not be repeated here; most importantly, this tutorial is completely based on Apple Silicon (such as M1, M1 Pro or M1 Max) build, so make sure your Mac is Apple Silicon.
6+
7+
## Step by Step
8+
9+
1. Create a new Env and install the dependencies provided by Apple.
10+
11+
```shell
12+
conda create -n tensorflow-macos python=3.9 # 这里Python版本也可以使用Python 3.8
13+
conda activate tensorflow-macos
14+
conda install -c apple tensorflow-deps==2.8.0
15+
````
16+
17+
2. Install the `tensorflow-macos` and `tensorflow-metal` plugins.
18+
19+
```shell
20+
pip install tensorflow-macos
21+
pip install tensorflow-metal
22+
````
23+
24+
3. Install `bazel 4.2.2`.
25+
26+
```shell
27+
wget https://raw.githubusercontent.com/Homebrew/homebrew-core/210bef2d570f0635e07009f4c5242b5e6645ae31/Formula/bazel.rb
28+
brew install ./bazel.rb
29+
bazel --version # make sure the version is 4.2.2
30+
````
31+
32+
* Usually, the `bazel` installed by `brew` will be the latest version. The latest version often does not match the version required by `text`, which may cause many unexpected problems, so we install it by specifying the version manually.
33+
34+
4. Download and extract `text 2.8.1`.
35+
36+
```shell
37+
wget https://github.com/tensorflow/text/archive/refs/tags/v2.8.1.zip
38+
unzip ./v2.8.1.zip
39+
cd text-2.8.1
40+
````
41+
42+
5. Modify some parameters of the source code to ensure correct build.
43+
44+
* `oss_scripts/configure.sh` to modify line 49:
45+
46+
```shell
47+
pip install tensorflow-macos==2.8.0
48+
````
49+
50+
* `oss_scripts/run_build.sh` comment out line 17:
51+
52+
```shell
53+
# source oss_scripts/prepare_tf_dep.sh
54+
````
55+
56+
* `oss_scripts/pip_package/setup.py` modify behavior 76, 77:
57+
58+
````python
59+
'tensorflow>=2.8.0, <2.9.0; platform_machine != "arm64" or platform_system != "Darwin"',
60+
'tensorflow-macos>=2.8.0, <2.9.0; platform_machine == "arm64" and platform_system == "Darwin"'
61+
````
62+
63+
6. Run the script.
64+
65+
```shell
66+
./oss_scripts/run_build.sh
67+
````
68+
69+
## Tips&Refer
70+
71+
1. `text` needs to correspond to a minor version of `tensorflow` (eg `tensorflow-macos==2.7.0` and `tensorflow-text==2.7.3`)
72+
2. Please ensure that your network is stable during the compilation process, and the compilation needs to use the network.
73+
3. [I add a PR for Apple Silicon support.](https://github.com/tensorflow/text/pull/756)

0 commit comments

Comments
 (0)