{"record":{"id":"695b8847f06c12b4","repo":"jax-ml/jax","slug":"memref-ty-static-offset-is-not-divisible-by","errorCode":null,"errorMessage":"{memref_ty} {static_offset=} is not divisible by {packing=}`","messagePattern":"(.+?) (.+?) is not divisible by (.+?)`","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"jax/experimental/mosaic/gpu/utils.py","lineNumber":1860,"sourceCode":"  address_space = get_memref_llvm_address_space(memref_ty)\n  ptr_ty = llvm.PointerType.get(address_space)\n  desc_ty_fields = [ptr_ty, ptr_ty, i64]\n  if rank > 0:\n    desc_ty_fields += [llvm.ArrayType.get(i64, rank)] * 2\n  desc_ty = llvm.StructType.get_literal(desc_ty_fields)\n  desc = builtin.unrealized_conversion_cast([desc_ty], [memref_arg])\n  assert isinstance(desc, ir.Value)\n  aligned_ptr = llvm.extractvalue(ptr_ty, desc, [1])\n  offset_elems = llvm.extractvalue(i64, desc, [2])\n\n  elem_bitwidth = bitwidth(memref_ty.element_type)\n  if elem_bitwidth < 8:\n    *_, static_offset = memref_ty.get_strides_and_offset()\n    if static_offset != ir.ShapedType.get_dynamic_stride_or_offset():\n      assert elem_bitwidth.bit_count() == 1\n      packing = 8 // elem_bitwidth\n      if static_offset % packing != 0:\n        raise ValueError(\n            f\"{memref_ty} {static_offset=} is not divisible by {packing=}`\"\n        )\n      offset_bytes = c(static_offset // packing, i64)\n    else:\n      offset_bits = llvm.mul(\n          offset_elems,\n          c(elem_bitwidth, i64),\n          overflow_flags=llvm.IntegerOverflowFlags.none,\n      )\n      offset_bytes = llvm.udiv(offset_bits, c(8, i64))\n  else:\n    assert elem_bitwidth % 8 == 0\n    offset_bytes = llvm.mul(\n        offset_elems,\n        c(elem_bitwidth // 8, i64),\n        overflow_flags=llvm.IntegerOverflowFlags.none,\n    )\n  return llvm.inttoptr(","sourceCodeStart":1842,"sourceCodeEnd":1878,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jax/experimental/mosaic/gpu/utils.py#L1842-L1878","documentation":"When loading a memref whose element bitwidth is below 8 bits (e.g. i1, i4), the code packs elements into bytes (packing = 8 // elem_bitwidth). A static memref offset not divisible by that packing factor cannot be byte-addressed, so it raises. Note the message has a stray trailing backtick — a cosmetic bug in the format string.","triggerScenarios":"Calling the load/pointer-bitcast path with a sub-byte element type (i1 giving packing=8, i4 giving packing=2) where the memref's static offset is, say, 3 for i4 (3 % 2 != 0). Arises with offset注意力 with packed quantized or boolean tensors.","commonSituations":"Loading quantized (4-bit) or boolean data through a strided/offset view created by slicing or padding; offsetting a sub-byte-type memref by an odd number of elements; using layouts that produce non-multiple static offsets on Blackwell paths.","solutions":["Adjust the memref layout/slice so the static offset is a multiple of 8 // elem_bitwidth elements (e.g. even offsets for i4, multiple of 8 for i1)","Use a byte-aligned base pointer and express the element offset via strides instead of the static memref offset","If the offset is genuinely dynamic, ensure it is marked as a dynamic stride/offset rather than a misaligned static one"],"exampleFix":"# before\nmemref<64xi4, strided<[1], offset: 3>>  # 3 % 2 != 0 -> raises\n# after\nmemref<64xi4, strided<[1], offset: 4>>  # byte-aligned for i4","handlingStrategy":"validation","validationCode":"bw = elem_bitwidth(memref_ty.element_type)\nif bw < 8:\n    packing = 8 // bw\n    _, static_offset = memref_ty.get_strides_and_offset()\n    assert static_offset % packing == 0, f'offset {static_offset} misaligned for packing {packing}'","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Keep sub-byte-type memref offsets multiples of 8/elem_bitwidth","Prefer byte-aligned base pointers with strides instead of static offsets"],"tags":["mosaic-gpu","memref","alignment","sub-byte-types"],"backgroundTag":"misaligned-offset","analyzedSha":"1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb","analyzedAt":"2026-08-27T09:53:25.647Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}