.predict runs only on CPU even though GPU is available
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 inference runs on the CPU even though a GPU is installed, the problem is usually not the predict method itself. It is almost always one of four things: the framework cannot see the GPU, the model or tensors were placed on the CPU, some operations do not have GPU kernels, or the workload is too small to show obvious GPU utilization. The fix is to verify device visibility first, then confirm that both the model and the actual prediction inputs are on the device you expect.
Check Whether the Framework Really Sees the GPU
Do not rely on the fact that nvidia-smi shows a device. Your ML framework must be built with GPU support and linked to compatible drivers and CUDA libraries.
A quick TensorFlow check:
A quick PyTorch check:
If these checks fail, prediction will stay on the CPU no matter what your code looks like.
In PyTorch, Move Both Model and Inputs
In PyTorch, a GPU-visible environment is still not enough. The model and input tensors must live on the same CUDA device.
A common failure mode is moving the model to CUDA during training, then creating new prediction tensors on the CPU during inference. In that case PyTorch usually raises a device mismatch error, but wrappers can obscure it.
In TensorFlow, Check Placement and Unsupported Ops
TensorFlow places many operations automatically, but not all operations have a GPU kernel. You can inspect placement decisions with device logging.
If logs show CPU placement, that can be legitimate. Some preprocessing steps, string operations, control-flow patterns, or custom layers may not run on the GPU.
Small Predictions Often Look Like CPU Work
Another subtle issue is workload size. A tiny inference batch can finish so quickly that GPU usage barely registers. Kernel launch overhead and data transfer can dominate, making the GPU appear idle.
This is especially common when:
- you call
predictone sample at a time - preprocessing runs on the CPU before every call
- the model is small compared with transfer overhead
- the monitoring tool samples utilization too slowly
To test this, increase the batch size and time the run.
This kind of test is more informative than watching utilization while predicting a single row.
Watch for Hidden CPU Steps
In end-to-end pipelines, .predict() may be only one part of the work. Tokenization, image decoding, feature assembly, Pandas transformations, and postprocessing are usually CPU tasks.
That means your overall prediction service can appear CPU-bound even if the model forward pass uses the GPU correctly. Profiling is the right tool here, not guesswork.
For TensorFlow, use the profiler. For PyTorch, use the profiler or insert targeted timers around preprocessing, model execution, and postprocessing separately.
Common Pitfalls
A common mistake is checking only whether a GPU exists on the machine, not whether the framework can use it.
Another mistake is moving the model to the GPU but leaving inference inputs on the CPU, or converting tensors back to NumPy too early.
People also often expect strong GPU utilization from tiny batches or tiny models. In those cases the CPU may genuinely be competitive.
Finally, unsupported operations can force parts of the graph onto the CPU. Custom layers and preprocessing code are frequent culprits.
Summary
- If prediction stays on the CPU, first verify that TensorFlow or PyTorch actually sees the GPU
- In PyTorch, both the model and the input tensors must be on the same CUDA device
- In TensorFlow, placement is automatic, but unsupported operations can still run on the CPU
- Tiny inference workloads may not show meaningful GPU utilization even when GPU execution is correct
- Pipeline preprocessing often dominates runtime and is usually CPU-bound
- Profile the full inference path instead of assuming the
predictcall alone explains performance
Related reading
- Predicting a single image with Keras' ImageDataGenerator
- predicting class for new data using neuralnet
- Predicting the next word using the LSTM ptb model tensorflow example
- Prediction from model saved with tf.estimator.Estimator in Tensorflow
- Predict single Image after training model in tensorflow
- Predicting a probability of a sentence using tensorflow
- Prefix sums weighted by a polynomial expression, can you do faster?
- Prepare array in linear time to find k smallest elements in Ok

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the 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.