{"record":{"id":"bc94ac8bbdf5c591","repo":"jax-ml/jax","slug":"stride-must-be-non-negative","errorCode":null,"errorMessage":"stride must be non-negative.","messagePattern":"stride must be non-negative\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"jax/_src/pallas/mosaic/primitives.py","lineNumber":136,"sourceCode":"\n\ndef roll(\n    x: jax.Array,\n    shift: jax.Array | int,\n    axis: int,\n    *,\n    stride: int | None = None,\n    stride_axis: int | None = None,\n) -> jax.Array:\n  if isinstance(shift, int) and shift < 0:\n    raise ValueError(\"shift must be non-negative.\")\n  if axis < 0 or axis >= len(x.shape):\n    raise ValueError(\"axis is out of range.\")\n  if (stride is None) != (stride_axis is None):\n    raise ValueError(\"stride and stride_axis must be both specified or not.\")\n  if stride is not None and stride_axis is not None:\n    if stride < 0:\n      raise ValueError(\"stride must be non-negative.\")\n    if stride_axis < 0 or stride_axis >= len(x.shape):\n      raise ValueError(\"stride_axis is out of range\")\n    if axis == stride_axis:\n      raise ValueError(\"expected axis and stride_axis are different.\")\n  return roll_p.bind(\n      x, shift, axis=axis, stride=stride, stride_axis=stride_axis\n  )\n\n\n@roll_p.def_abstract_eval\ndef _roll_abstract_eval(x, shift, **_):\n  del shift\n  return x\n\n\ndef _roll_lowering_rule(\n    ctx: mlir.LoweringRuleContext, x, shift, *, axis, stride, stride_axis\n):","sourceCodeStart":118,"sourceCodeEnd":154,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jax/_src/pallas/mosaic/primitives.py#L118-L154","documentation":"When Mosaic roll is used in strided mode, the stride magnitude must be a non-negative integer. Negative strides are not supported by the underlying hardware lowering, so the public wrapper rejects them upfront.","triggerScenarios":"roll(x, shift, axis, stride=-2, stride_axis=0) — any negative stride value together with a valid stride_axis.","commonSituations":"Deriving stride from a signed difference (e.g. element_size differences or direction flags) that can be negative; porting layout code where negative strides meant reversed traversal.","solutions":["Use a non-negative stride; express direction via the axis/shift combination instead","Take abs(stride) if magnitude is what matters","Validate stride >= 0 at the call site with a clear error"],"exampleFix":"# before\ny = roll(x, 2, axis=1, stride=-4, stride_axis=0)\n\n# after\ny = roll(x, 2, axis=1, stride=4, stride_axis=0)","handlingStrategy":"validation","validationCode":"stride = abs(stride)\nassert stride >= 0","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Encode direction via axis/shift, not signed stride","Clamp user-supplied strides"],"tags":["jax","pallas","mosaic","roll","argument-validation","stride"],"backgroundTag":"negative-stride-not-allowed","analyzedSha":"1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb","analyzedAt":"2026-08-27T09:53:25.647Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}