How to enable deterministic TensorFlow operations

Parallel kernels can schedule floating-point work in different orders, so identical TensorFlow runs may diverge by small amounts even when their inputs do not change. Operation determinism selects deterministic implementations for supported operations, allowing exact comparisons during debugging and regression testing.

TensorFlow exposes a process-level operation-determinism API. An unseeded tf.random.normal() call succeeds before the switch but raises RuntimeError afterward, which distinguishes an enabled process from TensorFlow's default state.

The switch does not seed random-number generators. Repeatable random values also require a shared seed and the same hardware and software environment; deterministic operations can run more slowly, and an unsupported operation may raise tf.errors.UnimplementedError.

Steps to enable deterministic TensorFlow operations:

  1. Create the import and shared seed in determinism-demo.py.
    determinism-demo.py
    import tensorflow as tf
     
     
    SEED = 2026
  2. Define a seeded tensor helper below the initialization section.
    def draw_seeded_tensor():
        tf.keras.utils.set_random_seed(SEED)
        return tf.random.normal((4,))

    The Keras helper resets Python, NumPy, and TensorFlow random state each time the function runs.

  3. Add an unseeded TensorFlow random operation below the helper to establish the default behavior.
    tf.random.normal((4,))
  4. Enable operation determinism immediately after the unseeded baseline.
    tf.config.experimental.enable_op_determinism()
  5. Capture the RuntimeError from the same unseeded operation after enablement.
    guard_raised = False
    try:
        tf.random.normal((4,))
    except RuntimeError:
        guard_raised = True
  6. Compare two tensors generated by the shared-seed helper.
    first = draw_seeded_tensor()
    second = draw_seeded_tensor()
    seeded_match = bool(
        tf.reduce_all(
            tf.equal(first, second)
        )
    )
  7. Assert that operation determinism blocked the post-enablement unseeded operation.
    if not guard_raised:
        raise AssertionError(
            "Operation determinism did not reject an unseeded random operation"
        )
  8. Assert that resetting the shared seed reproduced the TensorFlow random tensor.
    tf.debugging.assert_equal(
        seeded_match,
        True,
        message="Resetting the shared seed did not reproduce the random tensor",
    )
  9. Add result reporting below the assertions.
    print("Unseeded before enablement: allowed")
    print("Unseeded after enablement: blocked")
    print("Seeded repeatability: exact match")
  10. Run the completed script to confirm the determinism guard and seeded comparison.
    $ python3 determinism-demo.py
    Unseeded before enablement: allowed
    Unseeded after enablement: blocked
    Seeded repeatability: exact match

    An AssertionError about the unseeded operation means the determinism call is absent or did not run. An UnimplementedError identifies an operation without a deterministic implementation in the current environment.