How does one train multiple models in a single script in TensorFlow when there are GPUs present?
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction
To train multiple models in a single TensorFlow script with GPUs, you have several options: assign each model to a specific GPU using tf.device, limit GPU memory growth so multiple models can share a GPU, or train models sequentially by clearing the session between runs. The key challenge is GPU memory — a single model often claims all available VRAM. You must either partition GPUs across models, enable memory growth, or use virtual devices to split a physical GPU into logical ones.
Enable Memory Growth (Essential First Step)
By default, TensorFlow pre-allocates the entire GPU memory. With set_memory_growth(True), it allocates only as much memory as needed and grows on demand, leaving room for other models.
Strategy 1: One Model Per GPU
Strategy 2: Virtual GPU Devices (Split One GPU)
Strategy 3: Sequential Training with Session Clearing
tf.keras.backend.clear_session() destroys the current TF graph and releases GPU memory, allowing the next model to use it.
Strategy 4: Parallel Training with Multiprocessing
Each subprocess gets its own GPU via CUDA_VISIBLE_DEVICES. This avoids memory conflicts entirely because each process has an isolated TensorFlow runtime.
Common Pitfalls
- Not enabling memory growth before creating models: TensorFlow allocates all GPU memory by default. If you create model A without
set_memory_growth(True), it claims 100% of VRAM and model B gets an out-of-memory error. Callset_memory_growthbefore any model or tensor creation. - Setting
CUDA_VISIBLE_DEVICESafter importing TensorFlow: TensorFlow readsCUDA_VISIBLE_DEVICESat import time. Setting it afterimport tensorflowhas no effect. In multiprocessing, set the environment variable before importing TF in each subprocess. - Forgetting
clear_session()between sequential models: Without clearing the session, previous model graphs and optimizer state remain in GPU memory. After training 3-4 models sequentially, the GPU runs out of memory. Calltf.keras.backend.clear_session()andgc.collect()between each model. - Using threads instead of processes for parallel GPU training: Python threads share the same TensorFlow runtime and GPU context. Two threads training simultaneously on the same GPU cause memory conflicts and nondeterministic errors. Use
multiprocessingwith separateCUDA_VISIBLE_DEVICESfor true parallel training. - Exceeding virtual device memory limits: When splitting a GPU into virtual devices with
LogicalDeviceConfiguration(memory_limit=4096), each model must fit within that limit. A model that needs 6GB fails even though the physical GPU has 12GB. Size the virtual partitions based on each model's actual memory requirements.
Summary
- Enable
set_memory_growth(True)on all GPUs before creating any models - Use
tf.device('/GPU:N')to pin each model to a specific GPU when multiple GPUs are available - Use virtual GPU devices to split a single GPU between multiple models with fixed memory budgets
- Clear the session with
tf.keras.backend.clear_session()between sequential model training runs - Use
multiprocessingwithCUDA_VISIBLE_DEVICESfor fully parallel training on separate GPUs
Related reading
- How does one transfer CUDA constant memory in tensorflow's C API
- How does one use Pytorch cuda with an A100 GPU?
- How does pytorch backprop through argmax?
- How does Pytorch's Fold and Unfold work?
- How does reduce_sum work in tensorflow?
- How does tensorflow batch_matmul work?
- How does sample_weight compare to class_weight in scikit-learn?
- How does shuffling work with ImageDataGenerator in Machine Learning?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseNo course covers this one yet
Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.
Build my courseTrack 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.