{"record":{"id":"ea76d93517312113","repo":"keras-team/keras","slug":"cannot-create-sharding-when-device-mesh-is-not-set","errorCode":null,"errorMessage":"Cannot create sharding when device mesh is not set for TensorLayout.","messagePattern":"Cannot create sharding when device mesh is not set for TensorLayout\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"keras/src/backend/jax/distribution_lib.py","lineNumber":216,"sourceCode":"        A `jax.sharding.Mesh` instance.\n    \"\"\"\n    shape = device_mesh.devices.shape\n    devices = [_to_backend_device(d) for d in device_mesh.devices.flatten()]\n    devices = np.array(devices).reshape(shape)\n    return jax.sharding.Mesh(devices, device_mesh.axis_names)\n\n\ndef _to_backend_layout(tensor_layout):\n    \"\"\"Convert the TensorLayout to JAX backend specific Sharding.\n\n    Args:\n        tensor_layout: TensorLayout instance to convert.\n\n    Returns:\n        A `jax.sharding.NamedSharding` instance.\n    \"\"\"\n    if tensor_layout.device_mesh is None:\n        raise ValueError(\n            \"Cannot create sharding when device mesh is not set \"\n            \"for TensorLayout.\"\n        )\n    partition_spec = jax.sharding.PartitionSpec(*tensor_layout.axes)\n    jax_mesh = tensor_layout.device_mesh.backend_mesh\n    return jax.sharding.NamedSharding(jax_mesh, partition_spec)\n","sourceCodeStart":198,"sourceCodeEnd":223,"githubUrl":"https://github.com/keras-team/keras/blob/7a34a03db60bf60042242d6a556fc3be119046a5/keras/src/backend/jax/distribution_lib.py#L198-L223","documentation":"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.","triggerScenarios":"Thrown at keras/src/backend/jax/distribution_lib.py:216 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","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."],"exampleFix":null,"handlingStrategy":"validation","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"7a34a03db60bf60042242d6a556fc3be119046a5","analyzedAt":"2026-08-25T21:25:25.994Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}