{"record":{"id":"ebf13aeee03cee14","repo":"jax-ml/jax","slug":"requires-8-16-32-or-64-bit-field-width","errorCode":null,"errorMessage":"requires 8-, 16-, 32- or 64-bit field width.","messagePattern":"requires 8-, 16-, 32- or 64-bit field width\\.","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"jax/_src/random/philox2x32.py","lineNumber":190,"sourceCode":"  assert not data.shape\n  return _philox2x32_fold_in(key, jnp.asarray(data, dtype=\"uint32\"))\n\n\n@api.jit\ndef _philox2x32_fold_in(key: typing.Array, data: typing.Array) -> typing.Array:\n  \"\"\"Internal implementation of philox2x32_fold_in.\"\"\"\n  out0, _ = philox2x32_p.bind(key[0], np.uint32(0), data)\n  return jnp.array([out0], dtype=np.uint32)\n\n\ndef philox2x32_random_bits(\n    key: typing.Array, bit_width: int, shape: tuple[int, ...]\n) -> typing.Array:\n  \"\"\"Sample uniform random bits using a Philox 2x32 key.\"\"\"\n  if not _is_philox2x32_key(key):\n    raise TypeError(\"philox2x32_random_bits got invalid prng key.\")\n  if bit_width not in (8, 16, 32, 64):\n    raise TypeError(\"requires 8-, 16-, 32- or 64-bit field width.\")\n  return _philox2x32_random_bits(key, bit_width, shape)\n\n\n@api.jit(static_argnums=(1, 2), inline=True)\ndef _philox2x32_random_bits(\n    key: typing.Array, bit_width: int, shape: tuple[int, ...]\n) -> typing.Array:\n  \"\"\"Internal implementation of philox2x32_random_bits.\"\"\"\n  if all(core.is_constant_dim(d) for d in shape) and math.prod(shape) > 2**64:\n    raise NotImplementedError(\"random bits array of size exceeding 2 ** 64\")\n\n  counts1, counts2 = prng.iota_2x32_shape(shape)\n  out0, out1 = philox2x32_p.bind(key[0], counts1, counts2)\n\n  dtype = prng.UINT_DTYPES[bit_width]\n  if bit_width == 64:\n    bits_hi = lax.convert_element_type(out0, dtype)\n    bits_lo = lax.convert_element_type(out1, dtype)","sourceCodeStart":172,"sourceCodeEnd":208,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jax/_src/random/philox2x32.py#L172-L208","documentation":"philox2x32_random_bits validates bit_width and only accepts 8, 16, 32, or 64 — the widths for which it packs uint outputs. Any other integer (or non-integer) width raises this TypeError.","triggerScenarios":"Calling random.philox2x32_random_bits(key, 12, shape) or bit_width=128, or passing a width stored in a config as an arbitrary int.","commonSituations":"Trying to generate arbitrary-width random integers; porting code that assumed any bit count works; typos like 33 for 32.","solutions":["Use one of 8, 16, 32, or 64","For narrower widths, generate 8/16/32 bits and mask: bits & ((1 << w) - 1)","Prefer jax.random.bits(key, shape, dtype) with dtype of uint8/16/32/64"],"exampleFix":"# before\nb = random.philox2x32_random_bits(key, 12, (1000,))\n# after\nb = random.philox2x32_random_bits(key, 16, (1000,)) & 0xFFF","handlingStrategy":"validation","validationCode":"assert bit_width in (8, 16, 32, 64), f'invalid bit_width {bit_width}'","typeGuard":"def is_valid_bit_width(w) -> bool:\n    return w in (8, 16, 32, 64)","tryCatchPattern":null,"preventionTips":["Mask wider draws for non-standard widths","Prefer jax.random.bits with uint dtypes"],"tags":["jax","prng","philox","argument-validation"],"backgroundTag":"invalid-bit-width","analyzedSha":"1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb","analyzedAt":"2026-08-27T09:53:25.647Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}