Specifying CPUs for use in Keras Tensorflow Model Inference
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
TensorFlow does not let you pin a Keras inference call to an arbitrary named CPU core with one high-level Keras argument. In practice, CPU control for inference usually means forcing execution onto the CPU device, limiting TensorFlow thread counts, and optionally using operating-system affinity tools if you truly need core-level pinning.
Force TensorFlow to Use the CPU Device
If a GPU is present and you want CPU inference only, place the work on the CPU device.
This selects the CPU device class, not a specific CPU core.
If the machine has a GPU and you want TensorFlow to ignore it globally:
Do that before TensorFlow initializes devices fully.
Control CPU Parallelism with Thread Settings
For most inference workloads, the meaningful CPU knob is thread count, not device string.
TensorFlow exposes two important settings:
- intra-op threads for parallelism inside one operation
- inter-op threads for parallelism between operations
These settings affect how aggressively TensorFlow uses available CPU resources.
This is often what people really want when they ask to "specify CPUs."
Example: CPU-Limited Inference
This forces CPU execution and limits TensorFlow's CPU parallelism to a smaller thread budget.
Operating-System Affinity for True Core Pinning
If you really need to bind the process to specific CPU cores, that is usually an OS-level concern rather than a Keras API concern.
Examples outside TensorFlow itself:
- '
taskseton Linux' - CPU affinity APIs on Windows
- container or orchestration CPU pinning rules
For example, on Linux:
That constrains the Python process to selected cores. TensorFlow then operates within that process-level CPU affinity.
This is the path to use when exact core placement matters.
Environment Variables and BLAS Backends
TensorFlow may also interact with lower-level numeric libraries that use their own thread controls. In some environments, variables such as these matter:
The exact effect depends on the build and runtime environment, so use explicit TensorFlow API settings where possible and benchmark rather than assuming.
Measure, Do Not Guess
CPU inference performance depends on:
- batch size
- model size
- thread counts
- cache behavior
- whether other workloads share the same host
So the correct workflow is:
- force CPU only if needed
- set thread limits
- benchmark latency and throughput
- adjust based on actual measurements
There is no universal "best number of CPUs" for all models.
Common Pitfalls
- Assuming
with tf.device("/CPU:0")pins inference to a specific CPU core. - Forgetting to disable GPU visibility when the real goal is CPU-only execution.
- Changing thread settings after TensorFlow runtime initialization and expecting full effect.
- Confusing TensorFlow thread limits with operating-system CPU affinity.
- Tuning CPU settings without measuring latency and throughput on the target workload.
Summary
- In Keras and TensorFlow, CPU control usually means device selection plus thread-count tuning.
- Use
tf.device("/CPU:0")for CPU execution andset_visible_devices([], "GPU")for CPU-only mode. - Limit intra-op and inter-op threads to control CPU parallelism.
- Use OS-level affinity tools if you truly need specific core pinning.
- Benchmark on the actual model and workload instead of relying on generic settings.

