TensorFlow on 32-bit Linux? - tensorflow

Is there a version of TensorFlow for 32-bit Linux? I only see the 64-bit wheel available, and didn't find anything about it on the site.

We have only tested the TensorFlow distribution on 64-bit Linux and Mac OS X, and distribute binary packages for those platforms only. Try following the source installation instructions to build a version for your platform.
EDIT: One user has published instructions for running TensorFlow on a 32-bit ARM processor, which is promising for other 32-bit architectures. These instructions may have useful pointers for getting TensorFlow and Bazel to work in a 32-bit environment.

I've built a CPU-only version of TensorFlow on 32-bit Ubuntu (16.04.1 Xubuntu). It went a lot more smoothly than anticipated, for such a complex library that doesn't support 32-bit architectures officially.
It can be done by following a subset of the intersection of these two guides:
November 2015 walkthrough about Jetson TK1.
November 2016 walkthrough about Jetson TX1.
If I haven't forgotten anything, here are the steps I've taken:
Install Oracle Java 8 JDK:
$ sudo apt-get remove icedtea-8-plugin #This is just in case
$ sudo add-apt-repository ppa:webupd8team/java
$ sudo apt-get update
$ sudo apt-get install oracle-java8-installer
(This is all you need in a pristine Xubuntu install, but google the above keywords otherwise, to read about selecting a default JRE and javac.)
Dependencies:
sudo apt-get update
sudo apt-get install git zip unzip swig python-numpy python-dev python-pip python-wheel
pip install --upgrade pip
Following the instructions that come with Bazel, download a Bazel source zip (I got bazel-0.4.3-dist.zip), make a directory like ~/tf/bazel/ and unzip it there.
I was getting an OutOfMemoryError during the following build, but this fix took care of it (i.e. adding the -J-Xmx512m for the bootstrap build).
Call bash ./compile.sh, and wait for a long time (overnight for me, but see the remarks at the end).
$ git clone -b r0.12 https://github.com/tensorflow/tensorflow
This seems like the only change to the source code that was necessary!
$ cd tensorflow
$ grep -Rl "lib64"| xargs sed -i 's/lib64/lib/g'
Then $ ./configure and say no to everything. (Accept defaults where relevant.)
The following took quite a few hours with my setup:
$ bazel build -c opt --jobs 1 --local_resources 1024,0.5,1.0 --verbose_failures //tensorflow/tools/pip_package:build_pip_package
$ bazel-bin/tensorflow/tools/pip_package/build_pip_package /tmp/tensorflow_pkg
$ pip install --user /tmp/tensorflow_pkg/ten<Press TAB here>
To see that it's installed, see if it works on the TensorFlow Beginners tutorial. I use jupyter qtconsole (i.e. the new name of IPython). Run the code in the mnist_softmax.py. It should take little time even on very limited machines.
For some reason, TensorFlow's guide to building from source doesn't suggest running the unit tests:
$ bazel test //tensorflow/...
(Yes, type in the ellipses.)
Though I couldn't run them — it spent 19 hours trying to link libtensorflow_cc.so, and then something killed the linker. This was with half a core and 1536 MB memory limit. Maybe someone else, with a larger machine, can report on how the unit tests go.
Why didn't we need to do the other things mentioned in those two walkthroughs? Firstly, most of that work is about taking care of GPU interfacing. Secondly, both Bazel and TensorFlow have become more self-contained since the first of those walkthroughs was written.
Note that the above settings provided to Bazel for the build are very conservative (1024 MB RAM, half a core, one job at a time), because I'm running this through VirtualBox using a single core of a $200 netbook of the type that Intel makes for disadvantaged kids in Venezuela, Pakistan and Nigeria. (By the way, if you do this, make sure the virtual HDD is 20 GB at the very least — trying to build the unit tests above took about 5 GB of space.) The build of the wheel took almost 20 hours and the modest deep CNN from the second tutorial, which is quoted to take up to half an hour to run on modern desktop CPUs, takes about 80 hours under this setup. One might wonder why I don't get a desktop, but the truth is that actual training with TensorFlow only makes sense on a high-end GPU (or a bunch thereof), and when we can hire an AWS spot instance with such a GPU for about 10 cents an hour without commitment and on a workable ad-hoc basis, it doesn't make a lot of sense to be training elsewhere. The 480000% speed-up is really noticeable. On the other hand, the convenience of having a local installation is well worth going through a process such as above.

It appears that Google does not yet support tensorflow on 32-bit machines.
On a 32-bit machine running Centos 6.5,the following error is received after the "import tensorflow as tf" command:
ImportError: tensorflow/python/_pywrap_tensorflow.so: wrong ELF class: ELFCLASS64
Until Google distributes a 32-bit version of tensorflow, I also recommend building tensorflow from source as specified here.

I have used the information from the responses to this question and generated a detailed instructions list to compile and install tensorflow in a 32 bits linux system.
The latest version of the instructions is available in github at: tensorflow-32-bits-linux
Instructions to install Tensorflow in a 32 bits linux system
I used the following steps to install tensorflow in a old Asus Eee-Pc 1000H. Granted, it has been upgraded from the original 1 GB of RAM and an 80 GB HDD, to 2 GB of RAM and to 480 GB of SSD storage.
I tested the these instructions with the following OS versions and worked without problems:
* Xubuntu 16.04.6 Xenial Xerus 32 bits.
* Xubuntu 18.04.3 Bionic Beaver 32 bits.
* Debian 9.11 Stretch 32 bits.
Choose a convenient linux system
I have tested both the Ubuntu 16.04 (Xenial) and Debian 9.11 (Stretch) systems with 2 GB of RAM.
I set up the system to have 4 GB of SWAP space. With only 1 GB of SWAP, some compilations failed.
It's critical that the distribution has the version 8 of the Java SDK: openjdk-8-jdk
Install the Java 8 SDK and build tools
sudo apt-get update
sudo apt-get install openjdk-8-jdk
sudo apt-get install git zip unzip autoconf automake libtool curl zlib1g-dev swig build-essential
Install Python libraries
Next, we install python 3 development libraries and the keras module that will be required by tensorflow.
sudo apt-get install python3-dev python3-pip python3-wheel
sudo python3 -m pip install --upgrade pip
python3 -m pip install --user keras
You can use eithr python 3 or python 2 and compile tensorflow for that version.
Install and compile Bazel from sources
We need the source code bazel 0.19.2 distribution. We can obtain it and install in a new folder.
cd $HOME
wget https://github.com/bazelbuild/bazel/releases/download/0.19.2/bazel-0.19.2-dist.zip
mkdir Bazel-0-19.2
cd Bazel-0-19.2
unzip ../bazel-0.19.2-dist.zip
Before compiling, we need to remove line 30 of ./src/tools/singlejar/mapped_file_posix.inc file (#error This code for 64 bit Unix.) that throws an error if we are not in a 64 bit machine. This bazel version works ok in 32 bits.
vi ./src/tools/singlejar/mapped_file_posix.inc
Also we need to increase the java memory available to Bazel and start compiling it.
export BAZEL_JAVAC_OPTS="-J-Xmx1g"
./compile.sh
When it finishes (It can take several hours), we move the bazel compiled executable to some location in the current user's path
sudo cp output/bazel /usr/local/bin
Compile Tensorflow from sources
Create a folder and clone tensorflow's 1.13.2 version to it. Starting from version 1.14, tensorflow uses the Intel MKL DNN optimization library that it only works in 64 bits systems. So 1.13.2 is the last version that runs in 32 bits.
cd $HOME
mkdir Tensorflow-1.13.2
cd Tensorflow-1.13.2
git clone -b v1.13.2 --depth=1 https://github.com/tensorflow/tensorflow .
Before compiling, we replace the references to 64 bit libraries to the 32 bit ones.
grep -Rl "lib64"| xargs sed -i 's/lib64/lib/g'
We start the tensorflow configuration. We need to explicity disable the use of several optional libraries that are not available or not supported on 32 bit systems.
export TF_NEED_CUDA=0
export TF_NEED_AWS=0
./configure
We have to take the following considerations:
* When asked to specify the location of python. [Default is /usr/bin/python]: We should respond /usr/bin/python3 to use python 3.
* When asked to input the desired Python library path to use. Default is [/usr/local/lib/python3.5/dist-packages] we just hit Enter
* We should respond N to all the Y/N questions.
* When asked to specify optimization flags to use during compilation when bazel option "--config=opt" is specified [Default is -march=native -Wno-sign-compare]: Just hit Enter
Now we start compiling tensorflow disabling optional components like aws, kafka, etc.
bazel build --config=noaws --config=nohdfs --config=nokafka --config=noignite --config=nonccl -c opt --verbose_failures //tensorflow/tools/pip_package:build_pip_package
If everything went ok, now we generate the pip package.
bazel-bin/tensorflow/tools/pip_package/build_pip_package /tmp/tensorflow_pkg
And we install the pip package
python3 -m pip install --user /tmp/tensorflow_pkg/tensorflow-1.13.2-cp35-cp35m-linux_i686.whl
Test tensorflow
Now we run a small test to check that it works. We create a test.py file with the following contents:
import tensorflow as tf
mnist = tf.keras.datasets.mnist
(x_train, y_train),(x_test, y_test) = mnist.load_data()
x_train, x_test = x_train / 255.0, x_test / 255.0
model = tf.keras.models.Sequential([
tf.keras.layers.Flatten(input_shape=(28, 28)),
tf.keras.layers.Dense(512, activation=tf.nn.relu),
tf.keras.layers.Dropout(0.2),
tf.keras.layers.Dense(10, activation=tf.nn.softmax)
])
model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
model.fit(x_train, y_train, epochs=5)
model.evaluate(x_test, y_test)
And we run the test
python3 test.py
Here is the output
Epoch 1/5
60000/60000 [==============================] - 87s 1ms/sample - loss: 0.2202 - acc: 0.9348
Epoch 2/5
60000/60000 [==============================] - 131s 2ms/sample - loss: 0.0963 - acc: 0.9703
Epoch 3/5
60000/60000 [==============================] - 135s 2ms/sample - loss: 0.0685 - acc: 0.9785
Epoch 4/5
60000/60000 [==============================] - 128s 2ms/sample - loss: 0.0526 - acc: 0.9828
Epoch 5/5
60000/60000 [==============================] - 128s 2ms/sample - loss: 0.0436 - acc: 0.9863
10000/10000 [==============================] - 3s 273us/sample - loss: 0.0666 - acc: 0.9800
Enjoy your new Tensorflow library !!

Related

GPU error: Sagemaker mp.p2.xlarge instance using tensorflow==2.3.0

I got the following error when trying to train my tensorflow model on sagemaker ml.p2.xlarge instance. I use tensorflow==2.3.0. I wonder whether this is because of the tensorflow version incompatibility with cuda. sagemaker ml.p2.xlarge seems to use cuda 10.0
GPU error:
2020-08-31 08:46:46.429756: W tensorflow/stream_executor/platform/default/dso_loader.cc:59] Could not load dynamic library 'libcudart.so.10.1'; dlerror: libcudart.so.10.1: cannot open shared object file: No such file or directory; LD_LIBRARY_PATH: /usr/local/openmpi/lib:/usr/local/nvidia/lib:/usr/local/nvidia/lib64
2020-08-31 08:47:02.170819: W tensorflow/stream_executor/platform/default/dso_loader.cc:59] Could not load dynamic library 'libcudart.so.10.1'; dlerror: libcudart.so.10.1: cannot open shared object file: No such file or directory; LD_LIBRARY_PATH: /usr/local/openmpi/lib:/usr/local/nvidia/lib:/usr/local/nvidia/lib64
2020-08-31 08:47:02.764874: W tensorflow/core/common_runtime/gpu/gpu_device.cc:1753] Cannot dlopen some GPU libraries. Please make sure the missing libraries mentioned above are installed properly if you would like to use GPU. Follow the guide at https://www.tensorflow.org/install/gpu for how to download and setup the required libraries for your platform.
Skipping registering GPU devices...
This question is probably old, but it falls back on an open issue found at the beginning of choosing which versions of frameworks to use.
The problem does not depend on the type of instance that you specified (which has NVidia GPU).
From the official documentation "Available Deep Learning Containers Images", to date 20/10/2022, precompiled versions higher than 2.2 do not seem to be usable:
Framework
Job Type
Horovod Options
CPU/GPU
Python Version Options
Example URL
TensorFlow 2.2 (Cuda 10.2)
training
Yes
GPU
3.7 (py37)
763104351884.dkr.ecr.us-east-1.amazonaws.com/tensorflow-training:2.2.0-gpu-py37-cu102-ubuntu18.04
TensorFlow 2.2
inference
No
GPU
3.7 (py37)
763104351884.dkr.ecr.us-east-1.amazonaws.com/tensorflow-inference:2.2.0-gpu-py37-cu102-ubuntu18.04
Within the dockerfile that is used to use the container is the instruction to install the libraries that your custom version is missing:
RUN apt-get update && apt-get install -y --no-install-recommends --allow-unauthenticated \
python3-dev \
python3-pip \
python3-setuptools \
ca-certificates \
cuda-command-line-tools-10-1 \
cuda-cudart-dev-10-1 \
cuda-cufft-dev-10-1 \
cuda-curand-dev-10-1 \
cuda-cusolver-dev-10-1 \
cuda-cusparse-dev-10-1 \
curl \
libcudnn7=7.6.2.24-1+cuda10.1 \
# TensorFlow doesn't require libnccl anymore but Open MPI still depends on it
libnccl2=2.4.7-1+cuda10.1 \
libgomp1 \
libnccl-dev=2.4.7-1+cuda10.1 \
....
Then you can install the required libraries from your custom version directly with a requirements.txt file or run the install command directly in the training script.
If there are no special project requirements, I recommend using the precompiled versions of sagemaker. Otherwise, build a docker image from scratch instead of installing libraries this way..

Can TensorFlow lite can be build with custom CPU?

I'm looking the TF Lite Android App
Which can be found on GIT: https://github.com/tensorflow/tensorflow/tree/master/tensorflow/contrib/lite/java/demo
How can I compile the tensorflow lite framework to use the optimized "atom" cpu type?
Is it possible to compile it on a MAC os with the CPU optimizations for the "atom" cpu?
I want to run the app on an Android device (SDK 22) with an "Intel Atom" Processor.
When I run the application without any changes through Android Studio the rate was about 1200ms per frame.
Compering the same APK installed on my Galaxy S9 (arm - snapdragon processor) was about 30ms per frame.
In the "build.gradle" there is this section:
dependencies {
...
compile 'org.tensorflow:tensorflow-lite:0.0.0-nightly'
...
}
So it's seems that it's downloading the framework,
How can I compile it locally with the CPU optimization and set the app to use it instead of downloading the non optimized nightly version?
I tried to run this tutorial :
Installing TensorFlow from Sources with the cpu flags but not sure exactly how it's helping me with the Android scenario..
Assuming that your Atom device is x86, use the --fat_apk_cpu flag to specify the x86 ABI:
$ bazel build -c opt --cxxopt='--std=c++11' \
--fat_apk_cpu=x86 \
//tensorflow/contrib/lite/java/demo/app/src/main:TfLiteCameraDemo
Switch x86 with x86_64 if you're building for a 64-bit device.
The built APK, available at bazel-bin/tensorflow/contrib/lite/java/demo/app/src/main/TfLiteCameraDemo.apk, will contain the x86 .so file:
$ zipinfo bazel-bin/tensorflow/contrib/lite/java/demo/app/src/main/TfLiteCameraDemo.apk | grep lib
-rw---- 2.0 fat 1434712 b- defN 80-Jan-01 00:00 lib/x86/libtensorflowlite_jni.so
If your device is connected, you can use bazel mobile-install instead of bazel build to directly install the app:
$ bazel mobile-install -c opt --cxxopt='--std=c++11' \
--fat_apk_cpu=x86 \
--start_app \
//tensorflow/contrib/lite/java/demo/app/src/main:TfLiteCameraDemo

python tensorflow module dependency on glibc

I successfully build bazel and tensorflow from the source code, but when using the tensorflow module I am getting the following error:
./new_python/bin/python
>>>import tensorflow as tf
Error MSG: File "/home/niraj/Ansible/new_python/lib/python2.7/site-packages/‌​tensorflow/python/py‌​wrap_tensorflow.py", line 28, in <module> _pywrap_tensorflow = swig_import_helper()
ImportError: /lib64/libc.so.6: version `GLIBC_2.14' not found (required by /home/niraj/Ansible/new_python/lib/python2.7/site-packages/t‌​ensorflow/python/_py‌​wrap_tensorflow.so)
I am using RHEL6 machine. Any idea how to fix this ?
I found two bug reports on github regarding this very problem
https://github.com/tensorflow/tensorflow/issues/110
https://github.com/bazelbuild/bazel/issues/760
At least I get the impression that getting tensorflow to work on RHEL 6 is at least 'difficult' - as some claim in those two bugreports that they got it to work, with some limitations - if not, at least for now, impossible.
At least for Ubuntu 12.04 and CentOS 6.7 there are solutions. The 2nd answer (mentions CentOS) should work on RHEL 6 as well.
Old/First answer:
According to the link I gathered from this answer, RHEL 6 ships with libc 2.12, not 2.14.
You would have to compile the tensorflow stuff again and link it to an existing libc 2.14 on your system. I'm not quite sure how you were able to compile it without already having libc 2.14 somewhere on your system.
What made the trick for me was updating glibc (in my case to 2.17 version) by:
wget http://copr-be.cloud.fedoraproject.org/results/mosquito/myrepo-el6/epel-6-x86_64/glibc-2.17-55.fc20/glibc-2.17-55.el6.x86_64.rpm
wget http://copr-be.cloud.fedoraproject.org/results/mosquito/myrepo-el6/epel-6-x86_64/glibc-2.17-55.fc20/glibc-common-2.17-55.el6.x86_64.rpm
wget http://copr-be.cloud.fedoraproject.org/results/mosquito/myrepo-el6/epel-6-x86_64/glibc-2.17-55.fc20/glibc-devel-2.17-55.el6.x86_64.rpm
wget http://copr-be.cloud.fedoraproject.org/results/mosquito/myrepo-el6/epel-6-x86_64/glibc-2.17-55.fc20/glibc-headers-2.17-55.el6.x86_64.rpm
sudo rpm -Uvh glibc-2.17-55.el6.x86_64.rpm \
glibc-common-2.17-55.el6.x86_64.rpm \
glibc-devel-2.17-55.el6.x86_64.rpm \
glibc-headers-2.17-55.el6.x86_64.rpm --force --nodeps
I link original answer

Tensorflow compilation error with latest tensorflow source on MacOS

I am trying to build the tensorflow source on my Mac OSx Yosemite (10.10.5). After I run this command
bazel build --config=opt //tensorflow/tools/pip_package:build_pip_package
I get this error
C++ compilation of rule '//tensorflow/core:candidate_sampling_ops_op_lib' failed: cc_wrapper.sh failed: error executing command external/local_config_cc/cc_wrapper.sh -U_FORTIFY_SOURCE -fstack-protector -Wall -Wthread-safety -Wself-assign -fcolor-diagnostics -fno-omit-frame-pointer -g0 -O2 '-D_FORTIFY_SOURCE=1' -DNDEBUG ... (remaining 95 argument(s) skipped): com.google.devtools.build.lib.shell.BadExitStatusException: Process exited with status 1.
tensorflow/core/ops/candidate_sampling_ops.cc:392:7: error: return type 'tensorflow::Status' must match previous return type 'const ::tensorflow::Status' when lambda expression has unspecified explicit return type
return Status::OK();
^
tensorflow/core/ops/candidate_sampling_ops.cc:376:17: error: no viable conversion from 'tensorflow::(lambda at tensorflow/core/ops/candidate_sampling_ops.cc:376:17)' to 'tensorflow::Status (*)(shape_inference::InferenceContext )'
.SetShapeFn([](InferenceContext c) {
What may I be doing wrong ?
(outdated but still relevant for this version of TF)
The latest version of tensorflow is NOT compileable/working for mac os x.
Here is my script to get tensorflow working on mac-osx sierra tensorflow 1.0 on mac-osx sierra i7 no gpu. I'm still working on getting SSE and such to compile correctly and a later version of tensorflow - but whatever. Tensorflow is not friently with macs - but DL4J is!
UPDATE:
You shouldn't need to update from Yosemite. I was able to get r1.3 to compile with SSE and AVX! So the 'latest release' at time of writing has known issues - r1.3 is the latest stable build. I've included the script to do a proper build below, but also including http://www.josephmiguel.com/building-tensorflow-1-3-from-source-on-mac-osx-sierra-macbook-pro-i7-with-sse-and-avx/ for all the details on the matter.
one time install
install anaconda3 pkg # manually download this and install the package
conda update conda
conda create -n dl python=3.6 anaconda
source activate dl
cd /
brew install bazel
pip install six numpy wheel
pip install –upgrade https://storage.googleapis.com/tensorflow/mac/cpu/protobuf-3.1.0-cp35-none-macosx_10_11_x86_64.whl
sudo -i
cd /
rm -rf tensorflow # if rerunning the script
cd /
git clone https://github.com/tensorflow/tensorflow
Step 1
cd /tensorflow
git checkout r1.3 -f
cd /
chmod -R 777 tensorflow
cd /tensorflow
./configure # accept all default settings
Step 2
// https://stackoverflow.com/questions/41293077/how-to-compile-tensorflow-with-sse4-2-and-avx-instructions
bazel build –config=opt –copt=-mavx –copt=-mavx2 –copt=-mfma //tensorflow/tools/pip_package:build_pip_package
Step 3
bazel-bin/tensorflow/tools/pip_package/build_pip_package /tmp/tensorflow_pkg
pip install /tmp/tensorflow_pkg/tensorflow-1.0.1-cp36-cp36m-macosx_10_7_x86_64.whl
Step 4
cd ~
ipython
Step 5
import tensorflow as tf
hello = tf.constant(‘Hello, TensorFlow!’)
sess = tf.Session()
print(sess.run(hello))
Step 6
pip uninstall /tmp/tensorflow_pkg/tensorflow-1.0.1-cp36-cp36m-macosx_10_7_x86_64.whl

speed benchmark for testing tensorflow install

I'm doubting whether tensorflow is correctly configured on my gpu box, since it's about 100x slower per iteration to train a simple linear regression model (batchsize = 32, 1500 input features, 150 output variables) on my fancy gpu machine than on my laptop.
I'm using a Titan X, with a modern cpu, etc. nvidia-smi says that I'm only at 10% gpu utilization, but I expect that's because of the small batchsizes. I'm not using a feed_dict to move data into the computation graph. Everything is coming via a tf.decode_csv and tf.train.shuffle_batch.
Does anyone have any recommendations for how to easily test whether my install is correct? Are there any simple speed benchmarks? The speed difference between my laptop and the gpu machine is so dramatic that I'm expecting that things aren't configured properly.
Try tensorflow/tensorflow/models/image/mnist/convolutional.py, that'll print per-step timing.
On Tesla K40c that should get about 16 ms per step, while about 120 ms for CPU-only on my 3 year old machine
Edit: This moved to the models repositories: https://github.com/tensorflow/models/blob/master/tutorials/image/mnist/convolutional.py.
The convolutional.py file is now at models/tutorials/image/mnist/convolutional.py
Extending Yaroslavs answer:
Here is how to do the entire testing process (CUDA and cudNN installed already)
git clone https://github.com/tensorflow/models.git
Create a Virtual Environment for tensorflow and install tensorflow
virtualenv --system-site-packages -p python3 tf-venv3
source tf-venv3/bin/activate
pip install --upgrade pip
pip install --upgrade tensorflow-gpu
Run the model within your Virtual Environment
python models/tutorials/image/mnist/convolutional.py
My GTX 1070 needs ~5ms per step
Note: On Geforce 1050 Ti it takes ~10ms per step
The answer given by Yaroslav and extended by Patrice may be simply extended once again to work with TensorFlow v2.
Just in line 35 of convolutional.py, instead of:
import tensorflow as tf
type in:
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
The file convolutional.py was removed from the tensorflow repo but it can still be found in the repo history.