|
| 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