Skip to content

Namespace

TF Array-API compatibility layer used when array-api-compat has no TensorFlow backend.

anytensor.namespace

Array-namespace resolution (array-api-compat + TensorFlow shim).

array-api-compat does not yet expose a TensorFlow backend in the installed release, so EagerTensors are routed through tf.experimental.numpy with a thin Array-API compatibility layer.

array_namespace

array_namespace(*arrays)

Like array_api_compat.array_namespace, with TensorFlow EagerTensor support.

Source code in anytensor/namespace.py
def array_namespace(*arrays: Any) -> Any:
    """Like ``array_api_compat.array_namespace``, with TensorFlow EagerTensor support."""
    from array_api_compat import array_namespace as aac_namespace

    arrs = [a for a in arrays if a is not None and not _is_scalar(a)]
    tf_arrs = [a for a in arrs if _is_tensorflow_tensor(a)]
    if tf_arrs:
        others = [a for a in arrs if not _is_tensorflow_tensor(a) and not _is_numpy_ndarray(a)]
        if others:
            raise TypeError(
                "Cannot mix TensorFlow tensors with other non-NumPy frameworks "
                f"in one op (got {type(others[0])})"
            )
        return _tensorflow_namespace()
    if not arrs:
        import array_api_compat.numpy as xp

        return xp
    return aac_namespace(*arrs)