{"record":{"id":"fb806c762e62863a","repo":"jax-ml/jax","slug":"reduce-window-expected-init-values-to-be-scalars-b","errorCode":null,"errorMessage":"reduce_window expected init_values to be scalars but init_values have shapes {}.","messagePattern":"reduce_window expected init_values to be scalars but init_values have shapes (.+?)\\.","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"jax/_src/lax/windowed_reductions.py","lineNumber":371,"sourceCode":"    consts,\n    window_dimensions,\n    window_strides,\n    padding,\n    base_dilation,\n    window_dilation,\n):\n  operand_avals, init_val_avals = util.split_list(avals, [len(avals) // 2])\n  if any(\n      o.dtype != iv.dtype for o, iv in zip(operand_avals, init_val_avals)\n  ):\n    msg = (\"reduce_window got inconsistent dtypes for operands and init_values:\"\n           \" got operand dtypes {} and init_value dtypes {}.\")\n    raise TypeError(msg.format([o.dtype for o in operand_avals],\n                               [iv.dtype for iv in init_val_avals]))\n  if any(len(v.shape) != 0 for v in init_val_avals):\n    msg = (\"reduce_window expected init_values to be scalars but init_values \"\n           \"have shapes {}.\")\n    raise TypeError(msg.format([v.shape for v in init_val_avals]))\n  out_shape = _common_reduce_window_shape_rule(\n      operand_avals[0], window_dimensions, window_strides, padding,\n      base_dilation, window_dilation)\n  out_sharding = reduce_window_sharding_rule(\n      operand_avals[0], window_dimensions, window_strides, padding,\n      base_dilation, window_dilation)\n  vma = core.standard_vma_rule('reduce_window', *operand_avals)\n  if any(core.getu(a) or core.getr(a) for a in operand_avals):\n    raise NotImplementedError\n  return tuple(ShapedArray(out_shape, op.dtype, sharding=out_sharding,\n                           manual_axis_type=op.mat.update(varying=vma))\n               for op in operand_avals)\n\n\ndef _generic_reduce_window_batch_rule(\n    batched_args,\n    batch_dims,\n    *,","sourceCodeStart":353,"sourceCodeEnd":389,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jax/_src/lax/windowed_reductions.py#L353-L389","documentation":"Raised by JAX's reduce_window abstract evaluation when the init_value arguments passed to lax.reduce_window are not scalars (rank != 0). init_value acts as the reduction's identity element (e.g. -inf for max) and must be a 0-d array or Python scalar.","triggerScenarios":"Calling lax.reduce_window(operand, init_value, jaxpr, consts, ...) where init_value is a list, array of shape (1,), or any non-scalar (e.g. np.full((1,), -np.inf)).","commonSituations":"Porting NumPy pooling code where identity values were kept in arrays; building generic reduce wrappers that pass vector identities.","solutions":["Pass a Python scalar or 0-d value: -np.inf, 0, jnp.asarray(-np.inf) with shape ()","If init_value came from an array, index it: init_value[0] or use .item()"],"exampleFix":"// before\ninit = jnp.array([-np.inf])\nlax.reduce_window(x, init, jaxpr, dims, strides, padding)\n// after\ninit = jnp.asarray(-np.inf)  # scalar, shape ()\nlax.reduce_window(x, init, jaxpr, dims, strides, padding)","handlingStrategy":"validation","validationCode":"import numpy as np\nassert np.ndim(init_value) == 0, f'init_value must be scalar, got shape {np.shape(init_value)}'","typeGuard":"def is_scalar(x): return np.ndim(x) == 0","tryCatchPattern":null,"preventionTips":["Always build init_value from Python scalars (-inf, 0) or jnp.asarray(scalar)","Assert ndim==0 in pooling wrappers"],"tags":["jax","reduce-window","shape-validation","scalar"],"backgroundTag":"jax-reduce-window-invalid-arguments","analyzedSha":"1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb","analyzedAt":"2026-08-27T09:53:25.647Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}