jax-ml/jax · error · ValueError

dtype argument to `rayleigh` must be a float dtype, got {dty

Error message

dtype argument to `rayleigh` must be a float dtype, got {dtype}

What it means

jax.random.rayleigh only accepts floating-point dtypes for its dtype argument (default float). Any non-float dtype (int, bool, complex) fails dtypes.issubdtype(dtype, np.floating) and raises this ValueError before broadcast/cast checks run.

Source

Thrown at jax/_src/random/core.py:3203

      jax_enable_x64 is true, otherwise float32).
    out_sharding: Optional. Specifies how the output array should be sharded
      across devices in multi-device computation. Can be a
      :class:`~jax.sharding.NamedSharding`, a :class:`~jax.sharding.PartitionSpec`
      (``P``), or ``None`` (default). When specified, the output will be sharded
      according to the given sharding specification. Primarily used in explicit
      sharding mode.
      See the `explicit sharding tutorial <https://docs.jax.dev/en/latest/parallel.html>`_
      for more details.

  Returns:
    A random array with the specified dtype and with shape given by ``shape`` if
    ``shape`` is not None, or else by ``scale.shape``.
  """
  key, _ = _check_prng_key("rayleigh", key)
  dtype = dtypes.check_and_canonicalize_user_dtype(
      float if dtype is None else dtype)
  if not dtypes.issubdtype(dtype, np.floating):
    raise ValueError("dtype argument to `rayleigh` must be a float "
                     f"dtype, got {dtype}")
  shape = _check_broadcast_shapes("rayleigh", shape, scale)
  out_sharding = canonicalize_sharding_for_samplers(out_sharding, "rayleigh", shape)
  _check_all_safe_to_cast("rayleigh", dtype, scale)
  return maybe_auto_axes(_rayleigh, out_sharding,
                         shape=shape, dtype=dtype)(key, scale)

@jit(static_argnums=(2, 3))
def _rayleigh(key, scale, shape, dtype) -> Array:
  u = uniform(key, shape, dtype)
  scale = scale.astype(dtype)
  scale = jnp.broadcast_to(scale, shape)
  log_u = lax.log(u)
  n_two = lax._const(scale, -2)
  sqrt_u = lax.sqrt(lax.mul(log_u, n_two))
  ray = lax.mul(scale, sqrt_u)
  return ray

View on GitHub (pinned to 1e1c6a8fc0)

Solutions

  1. Pass np.float32/np.float64 or omit dtype
  2. Cast integer scale arrays to float so dtype inference is consistent

Example fix

// before
r = jax.random.rayleigh(key, scale, dtype=jnp.int32)
// after
r = jax.random.rayleigh(key, scale, dtype=jnp.float32)
Defensive patterns

Strategy: validation

Validate before calling

import numpy as np
if dtype is not None:
    assert np.issubdtype(np.dtype(dtype).type, np.floating), f'rayleigh needs float dtype, got {dtype}'

Type guard

def is_float_dtype(d) -> bool:
    import numpy as np
    return d is None or np.issubdtype(np.dtype(d).type, np.floating)

Prevention

When it happens

Trigger: Calling jax.random.rayleigh(key, scale, shape, dtype=np.uint8) or passing a complex/integer dtype from configuration.

Common situations: Parametrizing a sampling utility with a single dtype shared across discrete and continuous samplers; passing np.int_ when generating counts instead of magnitudes.

Related errors


AI-assisted analysis of jax-ml/jax@1e1c6a8fc0 (2026-08-27). Data as JSON: /api/errors/720850b895339a90. Report an issue: GitHub.