{"record":{"id":"0148b8d971c40a3a","repo":"jax-ml/jax","slug":"numpy-arrays-with-zero-strides-are-not-supported-a","errorCode":null,"errorMessage":"NumPy arrays with zero strides are not supported as MLIR attributes","messagePattern":"NumPy arrays with zero strides are not supported as MLIR attributes","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"jax/_src/interpreters/mlir.py","lineNumber":431,"sourceCode":"\ndef _numpy_scalar_attribute(val: Any) -> ir.Attribute:\n  mlir_type = dtype_to_ir_type(val.dtype)\n  if isinstance(mlir_type, ir.IntegerType):\n    return ir.IntegerAttr.get(mlir_type, int(val))\n  elif isinstance(mlir_type, ir.FloatType):\n    return ir.FloatAttr.get(mlir_type, val)\n  else:\n    raise TypeError(f\"Unsupported scalar attribute type: {type(val)}\")\n\ndef _numpy_array_attribute(x: np.ndarray | np.generic) -> ir.Attribute:\n  element_type = dtype_to_ir_type(x.dtype)\n  shape = x.shape\n  x = np.ascontiguousarray(x)\n  return ir.DenseElementsAttr.get(x, type=element_type, shape=shape)\n\ndef _numpy_array_attribute_handler(val: np.ndarray | np.generic) -> ir.Attribute:\n  if 0 in val.strides and val.size > 0:\n    raise ValueError(\n        \"NumPy arrays with zero strides are not supported as MLIR attributes\")\n  if val.dtype == dtypes.float0:\n    val = np.zeros(val.shape, dtype=np.bool_)\n  if dtypes.is_weakly_typed_scalar(val) or np.isscalar(val):\n    return _numpy_scalar_attribute(val)\n  else:\n    return _numpy_array_attribute(val)\n\nregister_attribute_handler(np.ndarray, _numpy_array_attribute_handler)\nregister_attribute_handler(hashable_array.HashableArray,\n                           lambda x: _numpy_array_attribute_handler(x.val))\n\nfor _scalar_type in [np.int8, np.int16, np.int32, np.int64,\n                     np.uint8, np.uint16, np.uint32, np.uint64,\n                     np.float16, np.float32, np.float64,\n                     np.complex64, np.complex128,\n                     np.bool_, np.longlong, dtypes.bfloat16]:\n  register_attribute_handler(_scalar_type, _numpy_array_attribute_handler)","sourceCodeStart":413,"sourceCodeEnd":449,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jax/_src/interpreters/mlir.py#L413-L449","documentation":"A numpy array with a zero stride (e.g. created via np.broadcast_to or repetitive slicing) cannot be embedded directly as a DenseElementsAttr, which requires a standard memory layout the MLIR attribute builder can read. JAX raises this before hitting an opaque MLIR binding error.","triggerScenarios":"Passing arrays produced by np.broadcast_to, np.lib.stride_tricks.as_strided with 0 strides, or certain reshape/transpose chains where some dimension has stride 0 and size > 0 into a jitted function as a constant.","commonSituations":"Efficient broadcasting patterns from numpy code reused with JAX; creating constant bias vectors via broadcast_to to save memory; zero-stride arrays coming from np.zeros((n,1,1)) style views after operations.","solutions":["Materialize the array with np.ascontiguousarray(arr) before passing it","Use arr.copy() or np.broadcast_to(...).copy() to get normal strides","Prefer letting JAX broadcast inside the computation (pass the small array and rely on broadcasting rules)"],"exampleFix":"# before\nbias = np.broadcast_to(np.float32(0.1), (1024,))\njitted_fn(x, bias)  # ValueError: zero strides\n\n# after\nbias = np.ascontiguousarray(bias)\njitted_fn(x, bias)","handlingStrategy":"validation","validationCode":"import numpy as np\n\ndef ensure_contiguous(a):\n    if isinstance(a, np.ndarray) and 0 in a.strides and a.size > 0:\n        return np.ascontiguousarray(a)\n    return a\n\nx = ensure_contiguous(x)\njitted_fn(x)","typeGuard":"def has_zero_strides(a) -> bool:\n    import numpy as np\n    return isinstance(a, np.ndarray) and a.size > 0 and 0 in a.strides","tryCatchPattern":null,"preventionTips":["Avoid passing broadcast_to results directly; call .copy() or ascontiguousarray","Let JAX handle broadcasting inside the computation"],"tags":["numpy","strides","jax","mlir"],"backgroundTag":"non-contiguous-array-input","analyzedSha":"1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb","analyzedAt":"2026-08-27T09:53:25.647Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}