Skip to content

Disable keras backend

Stub the Keras import surface so keras-tuner can be used without Keras.

Installs minimal fakes in sys.modules for the keras namespace (and a couple of keras-tuner internal submodules) so that import keras_tuner succeeds without pulling in TensorFlow / JAX / PyTorch.

Call this once, before importing keras_tuner.

Import-order rules

  • If keras is already in sys.modules (real or stubbed), this function is a no-op. So if you need real Keras alongside synalinks+kt, import keras before calling disable_keras_backend() — the stub will detect Keras and stay out of the way. kt will use real Keras.
  • If keras_tuner is already imported when this is called, kt has already bound to whatever keras it found at import time, so the stub cannot help anymore. A RuntimeWarning fires and this function is a no-op. Move the call before import keras_tuner.
  • You cannot use real Keras and the stub simultaneously in the same process: keras_tuner caches its backend at module-load. If you want separate kt.search runs against Keras and against synalinks, run them in separate Python processes.

Idempotent: calling more than once is a no-op once sys.modules["keras"] is set.

Example
import synalinks
synalinks.disable_keras_backend()
import keras_tuner as kt
Source code in synalinks/src/utils/keras_backend.py
@synalinks_export(
    [
        "synalinks.disable_keras_backend",
        "synalinks.utils.disable_keras_backend",
    ]
)
def disable_keras_backend() -> None:
    """Stub the Keras import surface so `keras-tuner` can be used without Keras.

    Installs minimal fakes in `sys.modules` for the `keras` namespace (and a
    couple of `keras-tuner` internal submodules) so that `import keras_tuner`
    succeeds without pulling in TensorFlow / JAX / PyTorch.

    Call this **once, before importing `keras_tuner`**.

    Import-order rules
    ------------------
    - If `keras` is already in `sys.modules` (real or stubbed), this function
      is a **no-op**. So if you need real Keras alongside `synalinks`+`kt`,
      `import keras` *before* calling `disable_keras_backend()` — the stub
      will detect Keras and stay out of the way. kt will use real Keras.
    - If `keras_tuner` is already imported when this is called, kt has
      already bound to whatever keras it found at import time, so the stub
      cannot help anymore. A `RuntimeWarning` fires and this function is a
      no-op. Move the call before `import keras_tuner`.
    - You cannot use real Keras and the stub simultaneously in the same
      process: `keras_tuner` caches its backend at module-load. If you want
      separate `kt.search` runs against Keras *and* against synalinks, run
      them in separate Python processes.

    Idempotent: calling more than once is a no-op once `sys.modules["keras"]`
    is set.

    Example:
        ```python
        import synalinks
        synalinks.disable_keras_backend()
        import keras_tuner as kt
        ```
    """
    if "keras" in sys.modules:
        return
    if "keras_tuner" in sys.modules:
        warnings.warn(
            "`disable_keras_backend()` was called after `keras_tuner` was "
            "imported; keras-tuner has already bound to whatever keras it "
            "found at import time, so installing the stub now has no effect. "
            "Move this call before `import keras_tuner`.",
            RuntimeWarning,
            stacklevel=2,
        )
        return
    for name, module in _build_keras_stubs().items():
        sys.modules[name] = module