Installing Tensorflow 2.19

Table of Contents

Categories: #ai 

Install Libraries

 1
 2#Install build dependencies
 3sudo apt update && sudo apt install -y \
 4  build-essential zlib1g-dev libncurses5-dev libgdbm-dev libnss3-dev \
 5  libssl-dev libreadline-dev libffi-dev libsqlite3-dev wget libbz2-dev
 6
 7# Download and extract Python 3.12
 8cd /usr/src
 9sudo wget https://www.python.org/ftp/python/3.12.3/Python-3.12.3.tgz
10sudo tar xzf Python-3.12.3.tgz
11cd Python-3.12.3
12
13# Compile and install to /opt (without overwriting system python)
14sudo ./configure --enable-optimizations --prefix=/opt/python3.12
15sudo make -j$(nproc)
16sudo make altinstall
17
18# Add aliases to .zshrc
19echo 'alias python3.12="/opt/python3.12/bin/python3.12"' >> ~/.bashrc
20echo 'alias pip3.12="/opt/python3.12/bin/pip3.12"' >> ~/.bashrc
21source ~/.zshrc
22
23# (Optional) Verify installation
24python3.12 --version
25pip3.12 --version
26
27#Create virtual environment
28python3.12 -m venv tensorflow_venv
29#activate virtual environment
30source tensorflow_venv/bin/activate
31
32#update pip
33pip install --upgrade pip
34#install tensorflow
35pip install tensorflow[and-cuda]

Run Workload

 1import os
 2os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'  # 0 = all logs, 1 = INFO, 2 = WARNING, 3 = ERROR
 3os.environ['CUDA_VISIBLE_DEVICES'] = '0'  # optional: controls which GPU is visible
 4
 5import logging
 6logging.getLogger('tensorflow').setLevel(logging.FATAL)
 7
 8import time
 9import numpy as np
10import tensorflow as tf
11from tensorflow.python.client import device_lib
12from tensorflow.keras import mixed_precision
13from tensorflow.keras.applications import ResNet50
14from tensorflow.keras.applications.resnet50 import preprocess_input
15
16
17# If using absl-py logging (common in TensorFlow):
18try:
19    import absl.logging
20    absl.logging.set_verbosity('fatal')
21except ImportError:
22    pass
23
24print(f"Tensorflow Version: {tf.__version__}")
25print("Num GPUs Available: ", len(tf.config.list_physical_devices('GPU')))
26#print(device_lib.list_local_devices())
27
28# Enable mixed precision
29mixed_precision.set_global_policy("mixed_float16")
30
31def run_benchmark(device_name, dtype='float32'):
32    with tf.device(device_name):
33        # Generate fake data
34        x_train = np.random.rand(256, 32, 32, 3).astype(dtype)
35        y_train = np.random.randint(0, 1000, size=(256,))
36        y_train = tf.keras.utils.to_categorical(y_train, 1000)
37        
38        
39        start = time.time()
40        # Build model
41        model = ResNet50(weights=None, input_shape=(32, 32, 3), classes=1000)
42        model.compile(optimizer='adam', loss='categorical_crossentropy')
43        end = time.time()
44        print(f"Loading ResNet50 model on {device_name} with {dtype} precision: {end - start:.4f}s")
45        
46        start = time.time()
47        # Timed run
48        model.fit(x_train, y_train, epochs=3, batch_size=32, verbose=0)
49        end = time.time()
50
51        print(f"ResNet50 training time on {device_name} with {dtype} precision: {end - start:.4f}s")
52
53# Run on CPU
54run_benchmark('/CPU:0', dtype='float32')
55
56# Run on GPU (if available)
57if tf.config.list_physical_devices('GPU'):
58    run_benchmark('/GPU:0', dtype='float32')        # Standard
59    run_benchmark('/GPU:0', dtype='float16')        # Mixed precision (activates Tensor Cores)

Output

1ResNet50 training time on /CPU:0 with float32 precision: 77.7919s 
2
3ResNet50 training time on /GPU:0 with float32 precision: 44.3151s 
4ResNet50 training time on /GPU:0 with float16 precision: 38.5496s