Skip to content

Building TensorFlow

linuxonz edited this page Feb 29, 2024 · 40 revisions

Building TensorFlow

The instructions provided below specify the steps to build TensorFlow version 2.13.0 on Linux on IBM Z for the following distributions:

  • Ubuntu (20.04, 22.04)

General Notes:

  • When following the steps below please use a standard permission user unless otherwise specified.
  • A directory /<source_root>/ will be referred to in these instructions, this is a temporary writable directory anywhere you'd like to place it.

Step 1: Build and Install TensorFlow v2.13.0

1.1) Build using script

If you want to build TensorFlow using manual steps, go to STEP 1.2.

Use the following commands to build TensorFlow using the build script. Please make sure you have wget installed.

wget -q https://raw.githubusercontent.com/linux-on-ibm-z/scripts/master/Tensorflow/2.13.0/build_tensorflow.sh

# Build Tensorflow
bash build_tensorflow.sh [Provide -t option for executing build with tests, -p option for choosing the Python version from {3.8, 3.9, 3.10, 3.11}, if not specified, the script will use the distro provided Python version (i.e., Python 3.8 on Ubuntu 20.04 and Python 3.10 on Ubuntu 22.04).]

If the build completes successfully, go to STEP 2. In case of error, check logs for more details or go to STEP 1.2 to follow manual build steps.

1.2) Install the dependencies

export SOURCE_ROOT=/<source_root>/
export NUMPY_VERSION="1.22.4"
export SCIPY_VERSION="1.7.3"
  • Only when choosing Python version 3.8

    PYTHON_VERSION=3.8.6
  • Only when choosing Python version 3.9

    PYTHON_VERSION=3.9.7
  • Only when choosing Python version 3.10

    PYTHON_VERSION=3.10.6
  • Only when choosing Python version 3.11

    PYTHON_VERSION=3.11.4
    NUMPY_VERSION=1.23.4
    SCIPY_VERSION=1.9.2
    SCIPY_DEP="libopenblas-dev"  # Extra Dependency required by Scipy 1.9.2
  • Ubuntu 20.04

    sudo apt-get update
    sudo apt-get install wget git unzip zip openjdk-11-jdk pkg-config libhdf5-dev libssl-dev libblas-dev liblapack-dev gfortran curl patchelf $SCIPY_DEP -y
    sudo ldconfig
  • Ubuntu 22.04

    sudo apt-get update
    sudo apt-get install wget git unzip zip openjdk-11-jdk pkg-config libhdf5-dev libssl-dev gcc-9* g++-9* curl libblas-dev liblapack-dev gfortran patchelf $SCIPY_DEP -y
    sudo ldconfig
    sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-9 60 --slave /usr/bin/g++ g++ /usr/bin/g++-9
    sudo update-alternatives --auto gcc
  • Build/Install Python

    • Ubuntu 20.04
      if [[ ! -z "$PYTHON_VERSION" && "$PYTHON_VERSION" != "3.8"* ]]; then
        wget -q https://raw.githubusercontent.com/linux-on-ibm-z/scripts/master/Python3/$PYTHON_VERSION/build_python3.sh
        sed -i 's/apt-get install/DEBIAN_FRONTEND=noninteractive apt-get install/g' build_python3.sh
        bash build_python3.sh -y
        sudo update-alternatives --install /usr/local/bin/python python /usr/local/bin/python3 40
      else
        sudo apt-get install python3-dev python3-pip -y
        sudo update-alternatives --install /usr/bin/python python /usr/bin/python3 40
      fi
    • Ubuntu 22.04
      if [[ ! -z "$PYTHON_VERSION" && "$PYTHON_VERSION" != "3.10"* ]]; then
        wget -q https://raw.githubusercontent.com/linux-on-ibm-z/scripts/master/Python3/$PYTHON_VERSION/build_python3.sh
        sed -i 's/apt-get install/DEBIAN_FRONTEND=noninteractive apt-get install/g' build_python3.sh
        sed -i 's/ubuntu-20.04/ubuntu-22.04/g' build_python3.sh
        bash build_python3.sh -y
        sudo update-alternatives --install /usr/local/bin/python python /usr/local/bin/python3 40
      else
        sudo apt-get install python3-dev python3-pip -y
        sudo update-alternatives --install /usr/bin/python python /usr/bin/python3 40
      fi
  • Install additional python packages

  sudo pip3 install --upgrade pip
  sudo pip3 install --no-cache-dir numpy==$NUMPY_VERSION wheel packaging requests opt_einsum portpicker protobuf scipy==$SCIPY_VERSION psutil setuptools==59.5.0

Note: At the time of creating these build instructions, Python v3.8.10 and Python v3.10.12 is available in Ubuntu 20.04 and Ubuntu 22.04 distributions respectively.

  • Build ICU data in big-endian format

    cd $SOURCE_ROOT
    export ICU_MAJOR_VERSION="69"
    export ICU_RELEASE="release-${ICU_MAJOR_VERSION}-1"
    git clone --depth 1 --single-branch --branch "$ICU_RELEASE" https://github.com/unicode-org/icu.git
    cd icu/icu4c/source/
    # create ./filters.json
    cat << 'EOF' > filters.json
    {
      "localeFilter": {
        "filterType": "language",
        "includelist": [
          "en"
        ]
      }
    }
    EOF
    ICU_DATA_FILTER_FILE=filters.json ./runConfigureICU Linux
    make clean && make
    # Workaround makefile issue where not all of the resource files may have been processed
    find data/out/build/ -name '*pool.res' -print0 | xargs -0 touch
    make
    cd data/out/tmp
    LD_LIBRARY_PATH=../../../lib ../../../bin/genccode "icudt${ICU_MAJOR_VERSION}b.dat"
    echo "U_CAPI const void * U_EXPORT2 uprv_getICUData_conversion() { return icudt${ICU_MAJOR_VERSION}b_dat.bytes; }" >> "icudt${ICU_MAJOR_VERSION}b_dat.c"
    cp icudt${ICU_MAJOR_VERSION}b_dat.c icu_conversion_data_big_endian.c
    gzip icu_conversion_data_big_endian.c
    split -a 3 -b 100000 icu_conversion_data_big_endian.c.gz icu_conversion_data_big_endian.c.gz.
  • Install grpcio

    cd $SOURCE_ROOT
    export GRPC_PYTHON_BUILD_SYSTEM_OPENSSL=True
    sudo -E pip3 install grpcio
  • Build Bazel v5.3.0

    cd $SOURCE_ROOT
    wget -q https://raw.githubusercontent.com/linux-on-ibm-z/scripts/master/Bazel/5.3.2/build_bazel.sh
    sed -i 's/5.3.2/5.3.0/g' build_bazel.sh
    sed -i 's#Bazel/${PACKAGE_VERSION}/patch#Bazel/5.3.2/patch#g' build_bazel.sh
    sed -i 's/apt-get install/DEBIAN_FRONTEND=noninteractive apt-get install/g' build_bazel.sh
    bash build_bazel.sh -y
    sudo cp $SOURCE_ROOT/bazel/output/bazel /usr/local/bin/bazel

1.3) Build TensorFlow

  • Download source code

    cd $SOURCE_ROOT
    git clone https://github.com/tensorflow/tensorflow
    cd tensorflow
    git checkout v2.13.0
  • Apply patches

    curl -o tf_v2.13.0.patch https://raw.githubusercontent.com/linux-on-ibm-z/scripts/master/Tensorflow/2.13.0/patch/tf_v2.13.0.patch
    patch -p1 < tf_v2.13.0.patch
    rm -f tf_v2.13.0.patch
  • Copy the big-endian version of ICU data

    cp ${SOURCE_ROOT}/icu/icu4c/source/data/out/tmp/icu_conversion_data_big_endian.c.gz.* third_party/icu/data/
  • Configure

      yes "" | ./configure || true
  • Build TensorFlow

      bazel build //tensorflow/tools/pip_package:build_pip_package

    Note:
    1. TensorFlow build is resource intensive operation. If build continues to fail try increasing the swap space and reduce the number of concurrent jobs by specifying --jobs=n in the build command above, where n is the number of concurrent jobs.

    2. Building TensorFlow from source can use a lot of RAM. If your system is memory-constrained, limit Bazel's RAM usage with: --local_ram_resources=2048.

1.4) Build tensorflow_io_gcs_filesystem wheel

sudo pip install --upgrade pip
cd $SOURCE_ROOT
git clone https://github.com/tensorflow/io.git
cd io/
git checkout v0.29.0
python3 setup.py -q bdist_wheel --project tensorflow_io_gcs_filesystem          
cd dist
sudo pip3 install ./tensorflow_io_gcs_filesystem-0.29.0-cp*-cp*-linux_s390x.whl

1.5) Build and Install TensorFlow wheel

cd $SOURCE_ROOT/tensorflow
bazel-bin/tensorflow/tools/pip_package/build_pip_package /tmp/tensorflow_wheel
sudo pip3 install /tmp/tensorflow_wheel/tensorflow-2.13.0-cp*-linux_s390x.whl

Step 2: Verify TensorFlow (Optional)

  • Run TensorFlow from command Line and check the installed version
     $ cd $SOURCE_ROOT
     $ python -c "import tensorflow as tf; print(tf.__version__)"
       2.13.0
    
     $ python
      >>> import tensorflow as tf
      >>> tf.add(1, 2).numpy()
      3
      >>> hello = tf.constant('Hello, TensorFlow!')
      >>> hello.numpy()
      b'Hello, TensorFlow!'
      >>>

Step 3: Execute Test Suite (Optional)

  • Run complete testsuite

    cd $SOURCE_ROOT/tensorflow
    
    bazel test -- //tensorflow/... -//tensorflow/compiler/... -//tensorflow/java/... -//tensorflow/python/kernel_tests/random:stateless_random_ops_test_cpu -//tensorflow/lite/delegates/flex:buffer_map_test -//tensorflow/lite/experimental/acceleration/mini_benchmark:big_little_affinity_test -//tensorflow/lite/delegates/gpu/cl/kernels/...
    
  • Run individual test

    bazel test //tensorflow/<module_name>:<testcase_name>

    For example,

    bazel test //tensorflow/core/kernels:shape_ops_test

    Note:

    1. Few test cases from //tensorflow/java/... fail on s390x and Intel, refer to this for more details.

    2. Test case //tensorflow/python/kernel_tests/random:stateless_random_ops_test_cpu and a few tests from compiler module fail due to lack of certain FP16 intrinsics in SystemZ LLVM backend. Refer to this for more details. Part of them can pass by passing --copt=-fno-ipa-cp flag to bazel command.

    3. A few tests (such as //tensorflow/lite/delegates/gpu/cl/kernels/... and //tensorflow/lite/delegates/flex:buffer_map_test) from lite module fail on both s390x and Intel.

Step 4: Model Training Verification (Optional)

Follow the instructions provided here to verify TensorFlow v2.13.0.

References:

Clone this wiki locally