{"record":{"id":"c71d50b2dfd2043e","repo":"jax-ml/jax","slug":"unsafe-buffer-pointer-is-supported-only-for-unsh","errorCode":null,"errorMessage":"unsafe_buffer_pointer() is supported only for unsharded arrays.","messagePattern":"unsafe_buffer_pointer\\(\\) is supported only for unsharded arrays\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"jax/_src/array.py","lineNumber":497,"sourceCode":"        raise BufferError(\"Couldn't get local_hardware_id for __dlpack__\")\n\n      return dl_device_type, local_hardware_id\n\n    else:\n      raise BufferError(\n          \"__dlpack__ device only supported for CPU, GPU and TPU pinned host,\"\n          f\" got platform: {self.platform()}\"\n      )\n\n  def __reduce__(self):\n    fun, args, arr_state = self._value.__reduce__()\n    aval_state = {'weak_type': self.aval.weak_type}\n    return (_reconstruct_array, (fun, args, arr_state, aval_state))\n\n  @use_cpp_method()\n  def unsafe_buffer_pointer(self):\n    if len(self._arrays) != 1:\n      raise ValueError(\"unsafe_buffer_pointer() is supported only for unsharded\"\n                       \" arrays.\")\n    return self._arrays[0].unsafe_buffer_pointer()\n\n  @property\n  @use_cpp_method()\n  def __cuda_array_interface__(self):\n    if len(self._arrays) != 1:\n      raise ValueError(\"__cuda_array_interface__() is supported only for \"\n                       \"unsharded arrays.\")\n    return self._arrays[0].__cuda_array_interface__  # bind-properties\n\n  @use_cpp_method()\n  def on_device_size_in_bytes(self):\n    \"\"\"Returns the total global on-device size of the array in bytes.\"\"\"\n    arr = self._arrays[0]\n    per_shard_size = arr.on_device_size_in_bytes()\n    return per_shard_size * self.sharding.num_devices\n","sourceCodeStart":479,"sourceCodeEnd":515,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jax/_src/array.py#L479-L515","documentation":"Array.unsafe_buffer_pointer() returns the raw device/host address of the array's underlying buffer and therefore requires the array to be backed by exactly one buffer (unsharded). Sharded or multi-buffer arrays have no single pointer, so ValueError is raised.","triggerScenarios":"Calling x.unsafe_buffer_pointer() (often indirectly via __cuda_array_interface__ or custom C++/CUDA kernel plumbing) on an array with len(x._arrays) != 1 — i.e. any sharded array on multi-device setups or non-addressable committed arrays.","commonSituations":"Passing jax buffers into custom CUDA kernels or profiling tools that need a raw pointer; TPU/multi-GPU pjit outputs fed to low-level code; assuming jit outputs are always single-buffer.","solutions":["Consolidate first: x = jax.device_put(x, jax.devices()[0]) (or device_get for host)","Guard: if len(x.addressable_data(0)._arrays) ... — practically, check x.is_fully_addressable and single-device sharding before calling","Use x.__cuda_array_interface__ after making it unsharded, which exposes the same pointer for CUDA consumers"],"exampleFix":"// before\nptr = sharded_x.unsafe_buffer_pointer()  # ValueError\n// after\nx = jax.device_put(sharded_x, jax.devices()[0])\nptr = x.unsafe_buffer_pointer()","handlingStrategy":"validation","validationCode":"def to_single_buffer(x):\n    if len(x.sharding.device_set) > 1 or not x.is_fully_addressable:\n        return jax.device_put(x, jax.devices()[0])\n    return x\n\nx = to_single_buffer(x)\nptr = x.unsafe_buffer_pointer()","typeGuard":"def has_single_buffer(x) -> bool:\n    return x.is_fully_addressable and len(x.sharding.device_set) == 1","tryCatchPattern":"try:\n    ptr = x.unsafe_buffer_pointer()\nexcept ValueError:\n    x = jax.device_put(x, jax.devices()[0])\n    ptr = x.unsafe_buffer_pointer()","preventionTips":["Never call raw-pointer APIs on pjit/shard_map outputs without gathering","Encapsulate kernel-launch plumbing behind helpers that normalize sharding","Assert single-buffer preconditions in debug builds of interop code"],"tags":["jax","cuda","raw-pointer","sharding","unsupported-operation"],"backgroundTag":"raw-pointer-on-sharded-array","analyzedSha":"1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb","analyzedAt":"2026-08-27T09:53:25.647Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}