TensorFlow
GPUOptions
ErrorFix
MachineLearning
Python

module 'tensorflow' has no attribute 'GPUOptions'

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

Introduction

The error AttributeError: module 'tensorflow' has no attribute 'GPUOptions' occurs when code written for TensorFlow 1.x is run on TensorFlow 2.x. In TF1, tf.GPUOptions was used to configure GPU memory allocation (memory growth, per-process memory fraction, visible devices). TF2 reorganized the GPU configuration API, moving these settings to tf.config.experimental and later to tf.config.

The Error

python
1import tensorflow as tf
2
3# TF1 code — fails on TF2
4gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.5)
5# AttributeError: module 'tensorflow' has no attribute 'GPUOptions'

The Fix: TF2 GPU Configuration

Memory Growth (Allocate as Needed)

The most common use case — prevent TensorFlow from grabbing all GPU memory at startup:

python
1import tensorflow as tf
2
3# TF2 way — enable memory growth
4gpus = tf.config.list_physical_devices('GPU')
5if gpus:
6    for gpu in gpus:
7        tf.config.experimental.set_memory_growth(gpu, True)

Limit GPU Memory

Set a hard memory limit instead of using a fraction:

python
1import tensorflow as tf
2
3gpus = tf.config.list_physical_devices('GPU')
4if gpus:
5    tf.config.set_logical_device_configuration(
6        gpus[0],
7        [tf.config.LogicalDeviceConfiguration(memory_limit=4096)]  # 4 GB
8    )

Restrict Visible GPUs

python
1import tensorflow as tf
2
3# Only use GPU 0 (hide GPU 1, 2, etc.)
4gpus = tf.config.list_physical_devices('GPU')
5if gpus:
6    tf.config.set_visible_devices(gpus[0], 'GPU')

Or use the environment variable:

bash
# Before running the script
export CUDA_VISIBLE_DEVICES=0

Migration Table: TF1 → TF2

TF1 CodeTF2 Equivalent
tf.GPUOptions(per_process_gpu_memory_fraction=0.5)tf.config.set_logical_device_configuration(gpu, [LogicalDeviceConfiguration(memory_limit=MB)])
tf.GPUOptions(allow_growth=True)tf.config.experimental.set_memory_growth(gpu, True)
tf.GPUOptions(visible_device_list='0')tf.config.set_visible_devices(gpus[0], 'GPU')
tf.ConfigProto(gpu_options=...)Not needed — use tf.config directly
tf.Session(config=config)Not needed — TF2 uses eager execution

Full TF1 → TF2 Migration Example

TF1 (Old)

python
1import tensorflow as tf
2
3gpu_options = tf.GPUOptions(
4    per_process_gpu_memory_fraction=0.5,
5    allow_growth=True,
6    visible_device_list='0'
7)
8config = tf.ConfigProto(gpu_options=gpu_options)
9sess = tf.Session(config=config)

TF2 (New)

python
1import tensorflow as tf
2
3gpus = tf.config.list_physical_devices('GPU')
4if gpus:
5    # Restrict to first GPU
6    tf.config.set_visible_devices(gpus[0], 'GPU')
7
8    # Enable memory growth
9    tf.config.experimental.set_memory_growth(gpus[0], True)
10
11    # Or set a hard limit (4 GB)
12    # tf.config.set_logical_device_configuration(
13    #     gpus[0],
14    #     [tf.config.LogicalDeviceConfiguration(memory_limit=4096)]
15    # )
16
17# No session needed — TF2 uses eager execution by default

Using tf.compat.v1 (Temporary Fix)

If you cannot migrate immediately, use the compatibility module:

python
1import tensorflow as tf
2
3# TF1 compatibility mode
4gpu_options = tf.compat.v1.GPUOptions(allow_growth=True)
5config = tf.compat.v1.ConfigProto(gpu_options=gpu_options)
6sess = tf.compat.v1.Session(config=config)

This is a temporary bridge — plan to migrate to native TF2 APIs.

Checking GPU Availability

python
1import tensorflow as tf
2
3# List available GPUs
4print(tf.config.list_physical_devices('GPU'))
5# [PhysicalDevice(name='/physical_device:GPU:0', device_type='GPU')]
6
7# Check if TensorFlow can see GPUs
8print(f"GPUs available: {len(tf.config.list_physical_devices('GPU'))}")
9
10# Get GPU details
11for gpu in tf.config.list_physical_devices('GPU'):
12    details = tf.config.experimental.get_device_details(gpu)
13    print(f"{gpu.name}: {details}")

Common Pitfalls

  • Must configure before any TF operation: GPU configuration (set_memory_growth, set_visible_devices) must be called before any TensorFlow operation. Once TF initializes the GPU runtime, configuration changes raise a RuntimeError.
  • set_memory_growth vs memory_limit: You cannot use both on the same GPU. Choose one approach: either let TF grow memory as needed (set_memory_growth) or set a hard cap (LogicalDeviceConfiguration).
  • Environment variable precedence: CUDA_VISIBLE_DEVICES is applied before TF sees any GPUs. If you set CUDA_VISIBLE_DEVICES=1, then tf.config.list_physical_devices('GPU') only shows one GPU at index 0 (which is actually physical GPU 1).
  • Mixed precision: TF2 mixed precision (tf.keras.mixed_precision) can reduce GPU memory usage significantly. Consider this before hard-limiting memory.
  • Docker containers: In Docker, you need --gpus flag: docker run --gpus all. Without it, list_physical_devices('GPU') returns an empty list regardless of configuration.

Summary

  • tf.GPUOptions was removed in TF2 — use tf.config APIs instead
  • Use tf.config.experimental.set_memory_growth(gpu, True) to enable dynamic memory allocation
  • Use tf.config.set_logical_device_configuration to set a hard memory limit
  • Use tf.config.set_visible_devices to restrict which GPUs are used
  • For temporary compatibility, use tf.compat.v1.GPUOptions
  • Configure GPU settings before any TensorFlow operation

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

All Rights Reserved.