jax-ml/jax · error · TypeError

factor_sizes must be a dict of str to int, but got {factor_s

Error message

factor_sizes must be a dict of str to int, but got {factor_sizes}

What it means

factor_sizes must map factor-name strings to Python ints. The guard checks all values with isinstance(size, int); any float, string, numpy scalar type that isn't int, or None triggers TypeError.

Source

Thrown at jax/_src/custom_partitioning_sharding_rule.py:338

  This is done by verifying that the input Einsum notation like string and
  with optional special factors and factor sizes represents a valid sharding
  rule and converting it to an internal representation.

  Args:
    rule: The Einsum notation like string for an operation.
    reduction_factors: A tuple of factors that are reduction factors.
    need_replication_factors: A tuple of factors that are need_replication factors.
    permutation_factors: A tuple of factors that are permutation factors.
    **factor_sizes: The optional factor sizes.

  Raises:
    ValueError: If there is any problem with the rule or factor_sizes.
  """
  if not isinstance(rule, str):
    raise TypeError(f"rule must be a str, but got {type(rule)}")
  if not all(isinstance(size, int) for size in factor_sizes.values()):
    raise TypeError(
        f"factor_sizes must be a dict of str to int, but got {factor_sizes}")

  # Replace ... with a single char to simplify parsing.
  if BATCHING in rule:
    raise ValueError(f"Unknown character '{BATCHING}'")
  if "." in rule:
    rule = rule.replace("...", BATCHING)
    if "." in rule:
      raise ValueError("Character '.' must be used inside ellipsis '...'")

  try:
    operands, results = rule.split("->")
  except ValueError as e:
    raise ValueError(f"There is no -> in rule: '{rule}'") from e

  operand_mappings = _parse_values(operands)
  result_mappings = _parse_values(results)
  return SdyShardingRule(operand_mappings, result_mappings,

View on GitHub (pinned to 1e1c6a8fc0)

Solutions

  1. Coerce values with int(): {k: int(v) for k, v in factor_sizes.items()}
  2. Fix the config source so sizes are integers (e.g. quote-check YAML)
  3. Cast numpy scalars via int() before building the dict

Example fix

# before
factor_sizes = {'a': 2.0}
# after
factor_sizes = {'a': int(2.0)}
Defensive patterns

Strategy: type-guard

Validate before calling

factor_sizes = {k: int(v) for k, v in factor_sizes.items()}

Type guard

def is_int_factor_sizes(fs: dict) -> bool:
    return all(isinstance(k, str) and isinstance(v, int) and not isinstance(v, bool) for k, v in fs.items())

Prevention

When it happens

Trigger: factor_sizes={'a': 2.0}, {'a': '2'}, or {'a': np.int64(2)} depending on isinstance behavior; most commonly floats from config parsing or YAML.

Common situations: Sizes loaded from JSON/YAML where numbers parse as floats; computed sizes like prod(mesh) returning numpy scalars.

Related errors


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