Installing TensorFlow
Preparing for Installation
TensorFlow 2.6.5 matches Python 3.7.x (3.7.5-3.7.11), Python 3.8.x, and Python 3.9.x.
- For the x86 architecture, skip the installation preparations.
- For the AArch64 architecture:
TensorFlow depends on h5py, and h5py depends on HDF5. Therefore, you need to compile and install HDF5 first. Otherwise, an error is reported when you use pip to install h5py. Perform the following operations as the root user.
- Ensure that Python of a matching version has been installed.
- Compile and install HDF5 1.10.5.
- Download the HDF5 source package and upload it to any directory in the installation environment.
- Go to the directory where the source package is stored, and run the following command to decompress the source package:
tar -zxvf hdf5-1.10.5.tar.gz
- Go to the decompressed folder and run the following configuration, build, and installation commands:
cd hdf5-1.10.5/ ./configure --prefix=/usr/local/hdf5 make -j16 && make install
- Set the environment variable:
export CPATH=/usr/local/hdf5/include/:/usr/local/hdf5/lib/ export LD_LIBRARY_PATH=/usr/local/hdf5/lib/:$LD_LIBRARY_PATH
- Install h5py.
- Run the following command as the root user to install the h5py dependency package:
pip3 install "Cython<3" pip3 install wheel
- Run the following command as the root user to install h5py:
pip3 install h5py==3.1.0
If h5py 3.1.0 fails to be installed online, click here to obtain the source package, and use the source code to compile and install h5py 3.1.0:
unzip h5py-3.1.0.zip cd h5py-3.1.0 python3 setup.py build python3 setup.py install
- Run the following command as the root user to install the h5py dependency package:
Installing TensorFlow
TensorFlow is necessary to develop and verify operators and develop training services.
- For the x86 architecture, download the software package from the pip source. For details about system requirements, see the TensorFlow official website. Note that the instructions provided by the TensorFlow website are incorrect. To download the CPU version from the pip source, you need to explicitly specify tensorflow-cpu. Otherwise, the GPU version is downloaded by default. The installation command is as follows.If you run the following command as a non-root user, add --user to the end of the installation command, for example, pip3 install tensorflow-cpu==2.6.5 --user.
pip3 install tensorflow-cpu==2.6.5
- For the AArch64 architecture, the pip source does not provide the corresponding version. Therefore, you need to use linux_gcc7.3.0 to compile TensorFlow 2.6.5. For details about the compilation procedure, see the TensorFlow official website. Pay attention to the following points:
After downloading the tensorflow tag v2.6.5 source code, perform the following steps:
- Download the nsync-1.22.0.tar.gz source package.
- Go to the source code directory, open the tensorflow/workspace2.bzl file, and find the tf_http_archive definition whose name is nsync.
tf_http_archive( name = "nsync", sha256 = "caf32e6b3d478b78cff6c2ba009c3400f8251f646804bcb65465666a9cea93c4", strip_prefix = "nsync-1.22.0", system_build_file = clean_dep("//third_party/systemlibs:nsync.BUILD"), urls = [ "https://storage.googleapis.com/mirror.tensorflow.org/github.com/google/nsync/archive/1.22.0.tar.gz", "https://github.com/google/nsync/archive/1.22.0.tar.gz", ], ) - Download the nsync-1.22.0.tar.gz source package from any path in urls and save it to any path.
- Go to the source code directory, open the tensorflow/workspace2.bzl file, and find the tf_http_archive definition whose name is nsync.
- Modify the nsync-1.22.0.tar.gz source package.
- Go to the directory where nsync-1.22.0.tar.gz is stored and decompress the source package. Find the decompressed nsync-1.22.0 folder and the pax_global_header file.
- Edit the nsync-1.22.0/platform/c++11/atomic.h file.
Append the following information in bold to NSYNC_CPP_START_.
#include "nsync_cpp.h" #include "nsync_atomic.h" NSYNC_CPP_START_ #define ATM_CB_() __sync_synchronize() static INLINE int atm_cas_nomb_u32_ (nsync_atomic_uint32_ *p, uint32_t o, uint32_t n) { int result = (std::atomic_compare_exchange_strong_explicit (NSYNC_ATOMIC_UINT32_PTR_ (p), &o, n, std::memory_order_relaxed, std::memory_order_relaxed)); ATM_CB_(); return result; } static INLINE int atm_cas_acq_u32_ (nsync_atomic_uint32_ *p, uint32_t o, uint32_t n) { int result = (std::atomic_compare_exchange_strong_explicit (NSYNC_ATOMIC_UINT32_PTR_ (p), &o, n, std::memory_order_acquire, std::memory_order_relaxed)); ATM_CB_(); return result; } static INLINE int atm_cas_rel_u32_ (nsync_atomic_uint32_ *p, uint32_t o, uint32_t n) { int result = (std::atomic_compare_exchange_strong_explicit (NSYNC_ATOMIC_UINT32_PTR_ (p), &o, n, std::memory_order_release, std::memory_order_relaxed)); ATM_CB_(); return result; } static INLINE int atm_cas_relacq_u32_ (nsync_atomic_uint32_ *p, uint32_t o, uint32_t n) { int result = (std::atomic_compare_exchange_strong_explicit (NSYNC_ATOMIC_UINT32_PTR_ (p), &o, n, std::memory_order_acq_rel, std::memory_order_relaxed)); ATM_CB_(); return result; }
- Compress the nsync-1.22.0.tar.gz source package.
Compress the content obtained in the previous step into a new nsync-1.22.0.tar.gz source package and save it to the directory, for example, /tmp/nsync-1.22.0.tar.gz.
- Generate a sha256sum checksum for the nsync-1.22.0.tar.gz source package.The obtained sha256sum checksum is a combination of digits and letters.
sha256sum /tmp/nsync-1.22.0.tar.gz
- Change the sha256sum checksum and urls.Go to the tensorflow tag source code directory, open the tensorflow/workspace2.bzl file, find the definition of tf_http_archive whose name is nsync, enter the verification code obtained in 4 after sha256=, enter the first line of the list after urls=, and enter the file:// index for storing the nsync-1.22.0.tar.gz file.
tf_http_archive( name = "nsync", sha256 = "caf32e6b3d478b78cff6c2ba009c3400f8251f646804bcb65465666a9cea93c4", strip_prefix = "nsync-1.22.0", system_build_file = clean_dep("//third_party/systemlibs:nsync.BUILD"), urls = [ "https://storage.googleapis.com/mirror.tensorflow.org/github.com/google/nsync/archive/1.22.0.tar.gz", "file:///tmp/nsync-1.22.0.tar.gz ", "https://github.com/google/nsync/archive/1.22.0.tar.gz", ], ) - Complete the compilation by referring to the official document (https://www.tensorflow.org/install/source).
The ABI configuration in TensorFlow and FwkPlugin must be the same (that is, set to 0). Otherwise, the TFA fails to be imported.
- Install the compiled TensorFlow.
After the preceding steps are complete, TensorFlow is packaged to the specified directory. Go to the specified directory and run the following installation command:
If you run the following command as a non-root user, add --user to the end of the installation command, for example, pip3 install tensorflow-2.6.5-*.whl --user.
pip3 install tensorflow-2.6.5-*.whl
- Run the following command to verify the installation:
python3 -c "import tensorflow as tf; print(tf.reduce_sum(tf.random.normal([1000, 1000])))"
If a tensor is returned, the installation is successful. NumPy is automatically reinstalled when you install TensorFlow. If a message is displayed indicating that the NumPy version is incompatible, reinstall NumPy of the matching version manually by referring to An Error Occurs When Executing "import tensorflow" After TensorFlow 2.6.5 Is Installed.