CPU
Keras
TensorFlow
model inference
hardware specifications

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.

python
1import tensorflow as tf
2import numpy as np
3
4model = tf.keras.Sequential([
5    tf.keras.layers.Input(shape=(4,)),
6    tf.keras.layers.Dense(8, activation="relu"),
7    tf.keras.layers.Dense(1),
8])
9
10x = np.random.randn(16, 4).astype("float32")
11
12with tf.device("/CPU:0"):
13    y = model(x, training=False)
14
15print(y.shape)

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:

python
import tensorflow as tf

tf.config.set_visible_devices([], "GPU")

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
python
1import tensorflow as tf
2
3tf.config.threading.set_intra_op_parallelism_threads(2)
4tf.config.threading.set_inter_op_parallelism_threads(1)

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

python
1import tensorflow as tf
2import numpy as np
3
4tf.config.set_visible_devices([], "GPU")
5tf.config.threading.set_intra_op_parallelism_threads(2)
6tf.config.threading.set_inter_op_parallelism_threads(1)
7
8model = tf.keras.Sequential([
9    tf.keras.layers.Input(shape=(128,)),
10    tf.keras.layers.Dense(64, activation="relu"),
11    tf.keras.layers.Dense(10),
12])
13
14inputs = np.random.randn(64, 128).astype("float32")
15
16with tf.device("/CPU:0"):
17    outputs = model.predict(inputs, verbose=0)
18
19print(outputs.shape)

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:

  • 'taskset on Linux'
  • CPU affinity APIs on Windows
  • container or orchestration CPU pinning rules

For example, on Linux:

bash
taskset -c 0,1 python infer.py

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:

bash
export OMP_NUM_THREADS=2
export TF_NUM_INTRAOP_THREADS=2
export TF_NUM_INTEROP_THREADS=1

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:

  1. force CPU only if needed
  2. set thread limits
  3. benchmark latency and throughput
  4. 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 and set_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.

Course illustration
Course illustration

All Rights Reserved.