Tutorial: Building TensorFlow from Source and Customizing Compiler Flags

Posted by

Compiling and Rebuilding TensorFlow from Source

Compiling and Rebuilding TensorFlow from Source

If you want to customize TensorFlow or optimize its performance for your specific hardware, you may need to compile it from source and rebuild it with custom compiler flags. In this tutorial, we’ll walk through the process of compiling TensorFlow from source and then rebuilding it with custom compiler flags.

Step 1: Compile TensorFlow from Source

First, you’ll need to clone the TensorFlow repository from GitHub:

git clone https://github.com/tensorflow/tensorflow.git

Next, navigate to the TensorFlow directory and run the configure script to set up the build environment:

./configure

This will prompt you to select various build options, such as the TensorFlow version, Python interpreter, and CUDA support. Once the configuration is complete, you can build TensorFlow using the bazel build system:

bazel build //tensorflow:libtensorflow_cc.so

Step 2: Rebuild TensorFlow with Compiler Flags

Once you have compiled TensorFlow from source, you can rebuild it with custom compiler flags to optimize its performance. For example, you might want to enable certain CPU instructions or use a specific optimization level. To do this, you can modify the BUILD file in the TensorFlow source tree to add your compiler flags.

For instance, to enable AVX and FMA instructions on an Intel CPU, you can add the following lines to the BUILD file:

copts = [
"-march=haswell",
"-mfma",
"-O3"
]

Once you have added your custom compiler flags, you can rebuild TensorFlow using bazel:

bazel build --config=opt //tensorflow:libtensorflow_cc.so

Conclusion

Compiling and rebuilding TensorFlow from source with custom compiler flags can be a powerful way to optimize its performance for your specific hardware. Whether you’re looking to squeeze the most performance out of your machine or enable specific CPU features, this tutorial should help you get started with the process.

0 0 votes
Article Rating
7 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@rossanamur7225
10 months ago

please i want a help … i want to rebuild tensorflow in cpanel server 😭 how i can install the compiler ???

@prathameshsaisankar9246
10 months ago

It didn't work. I get "ERROR: /root/.cache/bazel/_bazel_root/6dca306fa8d519b766333998e9fd499a/external/XNNPACK/BUILD.bazel:2884:19: Compiling src/operators/prelu-nc.c failed: (Exit 1): gcc failed: error executing command (from target @XNNPACK//:operators) /usr/bin/gcc -U_FORTIFY_SOURCE -fstack-protector -Wall -Wunused-but-set-parameter -Wno-free-nonheap-object -fno-omit-frame-pointer -g0 -O2 '-D_FORTIFY_SOURCE=1' -DNDEBUG -ffunction-sections … (remaining 97 arguments skipped)
gcc: fatal error: cannot execute '/usr/lib/gcc/x86_64-linux-gnu/9/cc1': execv: Invalid argument
compilation terminated.
Target //tensorflow/tools/pip_package:build_pip_package failed to build
Use –verbose_failures to see the command lines of failed build steps.
INFO: Elapsed time: 14924.841s, Critical Path: 446.49s
INFO: 2815 processes: 658 internal, 2157 local.
FAILED: Build did NOT complete successfully" when I use bazel to build

@SachinBharadwajManjunath
10 months ago

I got the same issue when training my reinforcement learning model. First of all thanks for the video and the simple explanation given.
My question is – Can I use the above commands if I have intel processor. By seeing amd many times in the video I assumed it can't be used for intel … please clarify.

@gokulakrishnanm
10 months ago

Please test A770 with cuDNN in windows native

@Quancept
10 months ago

fantastic. just the one I needed.

@OneStepToday
10 months ago

can you explain the google colab process for non programmer, step by step, coz we dont wanna download tensorflow from source and go through programming complex stuff. Is there a way the google colab or anything does the job for us, and simply gives the ckpt or safetensor file using our image files as input? I use tensor dot art website to easily generate ai images, and that just wants those chekpoint files for my new model. thanks

@laze_amvs424
10 months ago

How do you do this with CUDA?