{"record":{"id":"8cc102d3e90f2121","repo":"jax-ml/jax","slug":"next-power-of-2-requires-a-non-negative-integer","errorCode":null,"errorMessage":"`next_power_of_2` requires a non-negative integer.","messagePattern":"`next_power_of_2` requires a non-negative integer\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"jax/_src/pallas/utils.py","lineNumber":107,"sourceCode":"    >>> align_to(5, 4)  # 5 is not a multiple of 4, so rounds up to 8\n    8\n  \"\"\"\n  return cdiv(a, alignment) * alignment\n\n\ndef strides_from_shape(shape: tuple[int, ...]) -> tuple[int, ...]:\n  size = np.prod(shape)\n  strides = []\n  for s in shape:\n    size = size // s\n    strides.append(int(size))\n  return tuple(strides)\n\n\ndef next_power_of_2(x: int) -> int:\n  \"\"\"Returns the next power of two greater than or equal to `x`.\"\"\"\n  if x < 0:\n    raise ValueError(\"`next_power_of_2` requires a non-negative integer.\")\n  return 1 if x == 0 else 2 ** (x - 1).bit_length()\n\n\ndef pattern_match_scan_to_fori_loop(\n    jaxpr: jax_core.Jaxpr, num_consts: int, num_carry: int\n) -> tuple[jax_core.Jaxpr, bool]:\n  num_extensive_inputs = len(jaxpr.invars) - num_consts - num_carry\n  num_extensive_outputs = len(jaxpr.outvars) - num_carry\n  if num_extensive_outputs:\n    raise ValueError(\n        f\"Scan with {num_extensive_outputs} extensive output(s) is not\"\n        \" supported.\"\n    )\n  if num_extensive_inputs:\n    raise ValueError(\n        f\"Scan with {num_extensive_inputs} extensive argument(s) is not\"\n        f\" supported. Found {num_consts} consts and {num_carry} carry\"\n        \" arguments.\"","sourceCodeStart":89,"sourceCodeEnd":125,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jax/_src/pallas/utils.py#L89-L125","documentation":"pallas.utils.next_power_of_2(x) computes the smallest power of two >= x and only accepts non-negative integers. A negative input raises ValueError because bit_length-based computation is undefined for negatives.","triggerScenarios":"Calling next_power_of_2 with a negative number, e.g. next_power_of_2(-8); typically the negative comes from a computed block size or shape expression (e.g. padding - kernel).","commonSituations":"Kernel grid/block-size math producing negative values when problem dimensions are smaller than assumed; accidental sign error in a size formula.","solutions":["Fix the upstream computation so the argument is non-negative","Guard with max(0, x) if negatives are legitimately possible","Validate block sizes with an assert before calling"],"exampleFix":"# before\nnext_power_of_2(n - block_size)\n# after\nnext_power_of_2(max(0, n - block_size))","handlingStrategy":"validation","validationCode":"assert isinstance(x, int) and x >= 0, 'next_power_of_2 needs a non-negative int'","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Clamp computed sizes: max(0, n)","Assert block-size formulas for small inputs"],"tags":["jax","pallas","utils","validation"],"backgroundTag":"invalid-argument-value","analyzedSha":"1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb","analyzedAt":"2026-08-27T09:53:25.647Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}