{"record":{"id":"cea9dcc52d059f5a","repo":"jax-ml/jax","slug":"requires-8-16-32-or-64-bit-field-width-cea9dc","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/philox4x32.py","lineNumber":218,"sourceCode":"@api.jit\ndef _philox4x32_fold_in(key: typing.Array, data: typing.Array) -> typing.Array:\n  \"\"\"Internal implementation of philox4x32_fold_in.\"\"\"\n  # Hash the key with the data used as part of the counter.\n  k0, k1 = key[0], key[1]\n  out0, out1, _, _ = philox4x32_p.bind(\n      k0, k1, np.uint32(0), np.uint32(0), np.uint32(0), data\n  )\n  return jnp.array([out0, out1], dtype=np.uint32)\n\n\ndef philox4x32_random_bits(\n    key: typing.Array, bit_width: int, shape: tuple[int, ...]\n) -> typing.Array:\n  \"\"\"Sample uniform random bits using a Philox 4x32 key.\"\"\"\n  if not _is_philox4x32_key(key):\n    raise TypeError(\"philox4x32_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 _philox4x32_random_bits(key, bit_width, shape)\n\n\n@api.jit(static_argnums=(1, 2), inline=True)\ndef _philox4x32_random_bits(\n    key: typing.Array, bit_width: int, shape: tuple[int, ...]\n) -> typing.Array:\n  \"\"\"Internal implementation of philox4x32_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  k0, k1 = key[0], key[1]\n  counts1, counts2 = prng.iota_2x32_shape(shape)\n  zeros = jnp.zeros(shape, dtype=np.uint32)\n\n  out0, out1, out2, out3 = philox4x32_p.bind(\n      k0, k1, counts1, counts2, zeros, zeros\n  )","sourceCodeStart":200,"sourceCodeEnd":236,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jax/_src/random/philox4x32.py#L200-L236","documentation":"philox4x32_random_bits only supports bit widths 8, 16, 32, and 64; any other bit_width raises this TypeError since output packing is defined only for those widths.","triggerScenarios":"Calling random.philox4x32_random_bits(key, 24, shape) or any width outside {8,16,32,64}.","commonSituations":"Generating custom-width random integers; typos or off-by-one widths in config-driven pipelines.","solutions":["Use one of 8, 16, 32, 64","Mask a wider draw for odd widths: bits & ((1 << w) - 1)","Use jax.random.bits(key, shape, dtype=jnp.uintN) for the same effect"],"exampleFix":"# before\nb = random.philox4x32_random_bits(key, 24, (100,))\n# after\nb = random.philox4x32_random_bits(key, 32, (100,)) & 0xFFFFFF","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":["Use masking for odd widths","Validate config-driven widths at load time"],"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"}