{"record":{"id":"a524c69f03f9b4ed","repo":"jax-ml/jax","slug":"unknown-algorithm-algorithm-expected-fast-o","errorCode":null,"errorMessage":"Unknown algorithm '{algorithm}'. Expected 'fast' or 'stable'.","messagePattern":"Unknown algorithm '(.+?)'\\. Expected 'fast' or 'stable'\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"jax/_src/nn/functions.py","lineNumber":710,"sourceCode":"  if mean is None:\n    mean = jnp.mean(x, axis, keepdims=True, where=where)\n  if variance is None:\n    if algorithm == \"stable\":\n      variance = jnp.mean(\n          jnp.square(jnp.subtract(x, mean)), axis, keepdims=True, where=where)\n    elif algorithm == \"fast\":\n      # This definition is traditionally seen as less accurate than the\n      # two-pass mean((x - mean(x))**2) but may be faster and even, given\n      # typical activation distributions and low-precision arithmetic, more\n      # accurate when used in neural network normalization layers.\n      variance = jnp.mean(\n          jnp.square(x), axis, keepdims=True, where=where) - jnp.square(mean)\n      # Because we're using a less accurate variance definition, it may\n      # return negative values. This is problematic for the rsqrt, so we\n      # clip to 0.\n      variance = jnp.clip(variance, 0)\n    else:\n      raise ValueError(\n          f\"Unknown algorithm '{algorithm}'. Expected 'fast' or 'stable'.\")\n  return jnp.subtract(x, mean) * lax.rsqrt(variance + epsilon)\n\n# TODO(slebedev): Change the type of `x` to `ArrayLike`.\n@api.jit(static_argnames=(\"num_classes\", \"dtype\", \"axis\", \"out_sharding\"))\ndef _one_hot(x: Array, num_classes: int, *,\n             dtype: DTypeLike, axis: int | AxisName,\n             out_sharding: NamedSharding | None) -> Array:\n  num_classes = core.concrete_dim_or_error(\n      num_classes,\n      \"The error arose in jax.nn.one_hot argument `num_classes`.\")\n  try:\n    out_axis = util.canonicalize_axis(axis, x.ndim + 1)  # pyrefly: ignore[bad-argument-type]\n  except TypeError:\n    axis_size = lax.axis_size(axis)\n    if num_classes != axis_size:\n      raise ValueError(f\"Expected num_classes to match the size of axis {axis}, \"\n                       f\"but {num_classes} != {axis_size}\") from None","sourceCodeStart":692,"sourceCodeEnd":728,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jax/_src/nn/functions.py#L692-L728","documentation":"jax.nn.standardize supports only two variance algorithm strings: 'fast' (uses the less accurate E[x^2]-E[x]^2 formula) and 'stable' (two-pass variance). Any other string for the `algorithm` parameter raises this ValueError.","triggerScenarios":"Calling jax.nn.standardize(x, algorithm='unbiased'), algorithm='population', algorithm=None, or a typo like 'Stable' (case-sensitive).","commonSituations":"Porting torch/TF normalization code where variance parameter names differ (torch uses unbiased=True/False); passing None expecting a default; version drift if custom algorithm names were removed/renamed.","solutions":["Pass algorithm='fast' or algorithm='stable' exactly (lowercase)","If you wanted unbiased/biased variance behavior, pick 'stable' for the numerically safer two-pass computation"],"exampleFix":"// before\ny = jax.nn.standardize(x, algorithm='unbiased')\n\n// after\ny = jax.nn.standardize(x, algorithm='stable')","handlingStrategy":"validation","validationCode":"import jax.numpy as jnp\n\ndef standardize(x, algorithm):\n    if algorithm not in ('fast', 'stable'):\n        raise ValueError(f\"algorithm must be 'fast' or 'stable', got {algorithm!r}\")\n    return jax.nn.standardize(x, algorithm=algorithm)","typeGuard":"def is_standardize_algorithm(a) -> bool: return a in ('fast', 'stable')","tryCatchPattern":null,"preventionTips":["Use Literal['fast','stable'] type hints for wrapper functions","Centralize algorithm strings as module constants instead of free-form strings"],"tags":["jax","nn","standardize","enum-argument"],"backgroundTag":"invalid-enum-argument","analyzedSha":"1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb","analyzedAt":"2026-08-27T09:53:25.647Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}