keras-team/keras · error · ValueError
Cannot create sharding when device mesh is not set for…
Error message
Cannot create sharding when device mesh is not set for TensorLayout.
What it means
Raised by _to_backend_layout when converting a TensorLayout to a jax.sharding.NamedSharding while the layout's device_mesh is None. A sharding spec is meaningless without a mesh to shard across, so this guard rejects unset meshes before building the JAX sharding.
Solutions
- Set a DeviceMesh on the TensorLayout: layout.device_mesh = mesh before distribution calls.
- Construct the layout with an explicit mesh: TensorLayout(mesh, axes).
- Initialize the distribution (set a global DeviceMesh) before exporting or sharding.
Defensive patterns
Strategy: validation
When it happens
Trigger: Thrown at keras/src/backend/jax/distribution_lib.py:216 when the library encounters an invalid state.
Common situations: See trigger scenarios.
AI-assisted analysis of keras-team/keras@7a34a03db6 (2026-08-25).
Data as JSON: /api/errors/ea76d93517312113.
Report an issue: GitHub.
Appendix: source
Thrown at keras/src/backend/jax/distribution_lib.py:216
A `jax.sharding.Mesh` instance.
"""
shape = device_mesh.devices.shape
devices = [_to_backend_device(d) for d in device_mesh.devices.flatten()]
devices = np.array(devices).reshape(shape)
return jax.sharding.Mesh(devices, device_mesh.axis_names)
def _to_backend_layout(tensor_layout):
"""Convert the TensorLayout to JAX backend specific Sharding.
Args:
tensor_layout: TensorLayout instance to convert.
Returns:
A `jax.sharding.NamedSharding` instance.
"""
if tensor_layout.device_mesh is None:
raise ValueError(
"Cannot create sharding when device mesh is not set "
"for TensorLayout."
)
partition_spec = jax.sharding.PartitionSpec(*tensor_layout.axes)
jax_mesh = tensor_layout.device_mesh.backend_mesh
return jax.sharding.NamedSharding(jax_mesh, partition_spec)
View on GitHub (pinned to 7a34a03db6)