Tensorflow Load data in multiple threads on cpu
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
If model training is waiting on the input pipeline, adding GPU power will not help much. In TensorFlow, the standard way to load and preprocess data with multiple CPU threads is not manual Python threading. It is the tf.data pipeline with parallel mapping, interleaving, and prefetching.
Why Python Threads Are Usually the Wrong Abstraction
A common first instinct is to spawn several Python threads that read files and push batches into a queue. That can work, but it is usually harder to debug, harder to tune, and less integrated with TensorFlow execution.
tf.data already knows how to overlap I/O, decoding, preprocessing, and model execution. It can parallelize CPU-bound or I/O-bound input steps while keeping the training loop cleaner.
The main building blocks are:
- '
map(..., num_parallel_calls=...)for parallel preprocessing' - '
interleave(...)for reading many files concurrently' - '
prefetch(...)for overlapping input work with training' - '
tf.data.AUTOTUNEfor automatic tuning of parallelism levels'
Parallel Mapping Example
The simplest win is parallelizing the expensive transformation step.
Here, the map stage can use multiple CPU threads to decode and resize images while the model is training on previous batches.
Reading Multiple Files Concurrently
When the bottleneck is file access rather than tensor transformation, interleave is useful. It lets TensorFlow read from several file-based datasets in parallel.
This is especially helpful when many small files create I/O latency that one-at-a-time reading cannot hide.
Keep the CPU Busy, Not Overloaded
Parallelism helps, but more threads are not always better. If preprocessing is light, pushing too much parallel work can increase contention instead of throughput.
That is why tf.data.AUTOTUNE is a good default. It lets TensorFlow estimate a reasonable level of parallelism rather than hard-coding a guess. You can still tune manually if profiling shows a persistent bottleneck.
Caching can help too, but only when it matches the memory budget.
Use in-memory caching only when the dataset is small enough. Otherwise, caching can shift the bottleneck from CPU to memory pressure.
Do Not Bring Heavy Python Into the Map Function
tf.data works best when the map function uses TensorFlow ops. If you put large amounts of pure Python logic inside the pipeline, you lose some optimization opportunities and may reintroduce Python-level bottlenecks.
As a rule:
- prefer
tf.ioandtf.imageops inside the input pipeline - keep NumPy-heavy or custom Python work outside the hot path when possible
- profile before assuming the model is slow
If you truly need Python code, TensorFlow offers wrappers such as tf.py_function, but those should be used sparingly because they are harder to optimize and serialize.
Common Pitfalls
The most common mistake is writing manual thread pools before testing tf.data properly. In many training jobs, a well-structured TensorFlow pipeline already solves the throughput problem.
Another frequent issue is parallelizing only map but forgetting prefetch. Without prefetching, the model and input pipeline may still wait on each other.
Developers also often overuse cache() with large datasets and then run into RAM pressure. Faster is not helpful if the machine starts swapping.
Finally, if the map function is dominated by Python code instead of TensorFlow ops, increasing num_parallel_calls may not deliver the expected gain.
Summary
- Use
tf.data, not ad hoc Python threading, as the default way to load data with multiple CPU threads in TensorFlow. - Parallelize preprocessing with
map(..., num_parallel_calls=tf.data.AUTOTUNE). - Use
interleavewhen reading from many files andprefetchto overlap input work with training. - Keep the pipeline mostly in TensorFlow ops for better optimization.
- Tune parallelism with measurement, not assumptions, especially when memory is limited.
Related reading
- Tensorflow logging messages do not appear
- Tensorflow logits and labels must have the same first dimension
- Tensorflow loss becomes 'NaN
- Tensorflow loss decreasing, but accuracy stable
- Tensorflow loss resets after successfully restored checkpoint
- Tensorflow LSTM Dropout Implementation
- Tensorflow Multi-GPU single input queue
- Tensorflow multiple sessions with multiple GPUs
.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 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.