{"record":{"id":"ed8884b9a82d2a7c","repo":"jax-ml/jax","slug":"shard-map-requires-a-non-empty-mesh-got-mesh","errorCode":null,"errorMessage":"shard_map requires a non-empty mesh. Got {mesh}","messagePattern":"shard_map requires a non-empty mesh\\. Got (.+?)","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"jax/_src/shard_map.py","lineNumber":383,"sourceCode":"  if mesh is None:\n    mesh = get_abstract_mesh()\n    if mesh.empty:\n      raise ValueError(\n          \"The context mesh cannot be empty. Use\"\n          \" `jax.set_mesh(mesh)` to enter into a mesh context\")\n  else:\n    ctx_mesh = get_abstract_mesh()\n    if not ctx_mesh.empty and mesh.abstract_mesh != ctx_mesh:\n      raise ValueError(\n          f\"The context mesh {ctx_mesh} should match the mesh passed to\"\n          f\" shard_map {mesh}\")\n\n  if not isinstance(mesh, (Mesh, AbstractMesh)):\n    raise TypeError(\"shard_map requires a `jax.sharding.Mesh` or a \"\n                    \"`jax.sharding.AbstractMesh` instance for its \"\n                    f\"second argument, but got {mesh} of type {type(mesh)}.\")\n  if mesh.empty:\n    raise ValueError(f\"shard_map requires a non-empty mesh. Got {mesh}\")\n\n  mesh_axis_names_wo_vmap = (\n      frozenset(mesh.axis_names) - core.get_axis_env().explicit_mesh_axis_names\n  )\n\n  if not isinstance(axis_names, (frozenset, set)):\n    raise TypeError(\n        \"`axis_names` argument of shard_map should be of type `frozenset` or\"\n        f\" `set`. Got type: {type(axis_names)}\")\n  if isinstance(axis_names, set):\n    axis_names = frozenset(axis_names)\n  if not axis_names:\n    axis_names = mesh_axis_names_wo_vmap\n  if not axis_names.issubset(mesh_axis_names_wo_vmap):\n    raise ValueError(\n        f\"jax.shard_map requires axis_names={axis_names} to be a subset of \"\n        f\"mesh.axis_names={mesh_axis_names_wo_vmap}\")\n","sourceCodeStart":365,"sourceCodeEnd":401,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jax/_src/shard_map.py#L365-L401","documentation":"Even when a mesh object is supplied, it must be non-empty: it must contain at least one device/axis. An empty mesh (zero devices, e.g. Mesh(np.empty(0, dtype=object), axis_names=())) passes the isinstance check but fails here, since there is nothing to shard over.","triggerScenarios":"Constructing a Mesh from an empty device array and passing it to shard_map, or programmatically building meshes from a device selection that returned no devices.","commonSituations":"Test harnesses or CPU-only environments where device enumeration unexpectedly yields nothing; scripts where devices are filtered by a predicate (e.g. GPU-only) that matches none; refactors leaving a placeholder empty mesh.","solutions":["Check jax.devices() is non-empty before building the mesh and shard_map call","Build the mesh from all available devices: Mesh(jax.devices(), axis_names=('i',))"],"exampleFix":"// before\nmesh = jax.sharding.Mesh(np.empty((0,), dtype=object), axis_names=())\njax.shard_map(f, mesh=mesh, ...)(x)\n\n// after\nmesh = jax.sharding.Mesh(jax.devices(), axis_names=('i',))\njax.shard_map(f, mesh=mesh, ...)(x)","handlingStrategy":"validation","validationCode":"assert len(jax.devices()) > 0, 'no devices available'\nassert mesh.size > 0 and not mesh.empty, f'empty mesh: {mesh}'","typeGuard":"def is_nonempty_mesh(m) -> bool:\n    return isinstance(m, jax.sharding.Mesh) and not m.empty","tryCatchPattern":null,"preventionTips":["Build meshes from jax.devices(), never from filtered/empty lists","Guard device selection predicates in multi-backend scripts"],"tags":["jax","shard-map","mesh","empty-mesh","distributed"],"backgroundTag":"empty-resource-pool","analyzedSha":"1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb","analyzedAt":"2026-08-27T09:53:25.647Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}