tensorflow
logging
device placement
debugging
example code

Tensorflow documentation's example code on Logging Device Placement doesn't print out anything

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

TensorFlow device-placement logging is useful when you need to verify whether operations are running on CPU, GPU, or another accelerator. When the documentation example appears to print nothing, the problem is usually not the feature itself but the interaction between TensorFlow version, execution mode, and logging configuration. The most reliable fix is to enable placement logging using the right API and then force a real computation after logging is turned on.

Core Sections

1. What device-placement logging actually shows

Device-placement logging does not print output just because a flag exists. TensorFlow emits placement messages when operations are created or executed through the relevant runtime path. That means two things matter:

  • the correct logging API for your TensorFlow version
  • an operation that actually runs after logging is enabled

If you copy a TensorFlow 1 example into a TensorFlow 2 environment, it may no longer behave the way the documentation text suggests.

2. TensorFlow 2 eager-mode solution

In TensorFlow 2, the most direct API is tf.debugging.set_log_device_placement(True).

python
1import tensorflow as tf
2
3tf.debugging.set_log_device_placement(True)
4
5print("physical devices:", tf.config.list_physical_devices())
6print("gpus:", tf.config.list_physical_devices("GPU"))
7
8a = tf.constant([[1.0, 2.0]])
9b = tf.constant([[3.0], [4.0]])
10c = tf.matmul(a, b)
11
12print(c)

This should emit placement information for operations such as MatMul, even on CPU-only systems.

3. TensorFlow 1 graph-mode pattern

Older examples often use session configuration with log_device_placement=True. That pattern still applies to graph-mode code.

python
1import tensorflow as tf
2
3tf.compat.v1.disable_eager_execution()
4
5with tf.compat.v1.Session(
6    config=tf.compat.v1.ConfigProto(log_device_placement=True)
7) as sess:
8    a = tf.constant([[1.0, 2.0]])
9    b = tf.constant([[3.0], [4.0]])
10    c = tf.matmul(a, b)
11    print(sess.run(c))

If you set graph-mode logging but never execute the graph with sess.run, nothing will print because the runtime never performs the placement work you are trying to observe.

4. Real execution is required

Some examples fail in practice because they create tensors but do not trigger an operation path that makes placement messages visible. Use a clear numeric op such as matrix multiplication and print the result immediately. This removes ambiguity about whether code executed or only defined objects.

In eager mode, the operation runs as soon as it is called. In graph mode, it runs only when fetched by the session.

5. Logging can be suppressed by environment configuration

TensorFlow logs can be filtered by environment variables or wrapper code. A common culprit is TF_CPP_MIN_LOG_LEVEL.

python
import os
print("TF_CPP_MIN_LOG_LEVEL =", os.environ.get("TF_CPP_MIN_LOG_LEVEL"))

If you previously suppressed TensorFlow logs for cleaner notebooks, temporarily remove that suppression while debugging placement output.

6. Notebook output can be misleading

In Jupyter and some IDE consoles, low-level runtime logs may appear in a different output stream or be swallowed by frontend output handling. If the example is silent in a notebook, move it into a plain script and run:

bash
python device_placement_test.py

That isolates TensorFlow behavior from notebook rendering behavior.

7. GPU absence is not the same as logging failure

A missing GPU does not explain total silence by itself. Placement logging should still show CPU placement when everything is configured correctly.

Use runtime inspection to confirm device context:

python
1import tensorflow as tf
2
3print(tf.config.list_physical_devices())
4print(tf.config.list_physical_devices("GPU"))

The purpose of this check is to understand the environment, not to assume a GPU is required for placement logs.

8. A practical troubleshooting sequence

When the example prints nothing, check in this order:

  1. confirm TensorFlow version
  2. use the matching logging API for that version
  3. run a real operation after enabling logging
  4. remove log-suppression environment settings
  5. test outside notebooks if necessary

This sequence usually resolves the issue quickly without guesswork.

Common Pitfalls

  • Using TensorFlow 1 placement examples in TensorFlow 2 eager-mode code without adapting them.
  • Enabling placement logging after the relevant operations already executed.
  • Forgetting that graph-mode logging requires sess.run.
  • Suppressing TensorFlow logs through environment variables while trying to debug them.
  • Assuming silent output automatically means GPU configuration failure.

Summary

  • Device-placement logging depends on TensorFlow version, execution mode, and actual op execution.
  • TensorFlow 2 typically uses tf.debugging.set_log_device_placement(True).
  • TensorFlow 1 examples rely on session config and graph execution.
  • Notebook environments can hide runtime logs, so script-based testing is often useful.
  • Verify version, logging config, and execution path before assuming the feature is broken.

Course illustration
Course illustration

All Rights Reserved.