{"record":{"id":"d13ee6ee062a0c5f","repo":"jax-ml/jax","slug":"factor-factor-is-not-used-in-the-rule-but-size","errorCode":null,"errorMessage":"Factor {factor} is not used in the rule, but size is provided","messagePattern":"Factor (.+?) is not used in the rule, but size is provided","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"jax/_src/custom_partitioning_sharding_rule.py","lineNumber":144,"sourceCode":"               *, reduction_factors: tuple[str, ...] = (),\n               need_replication_factors: tuple[str, ...] = (),\n               permutation_factors: tuple[str, ...] = (),\n               **factor_sizes: int):\n    # Find all factors and mark whether their size can be inferred.\n    factors_inferrable = {}\n    for value in operand_mappings + result_mappings:\n      for dim in value:\n        if isinstance(dim, str):\n          factors_inferrable[dim] = True\n        else:\n          for factor in dim:\n            if factor not in factors_inferrable.keys():\n              factors_inferrable[factor] = False\n\n    # Check that factors in factor_sizes are used in the rule.\n    for factor in factor_sizes:\n      if factor not in factors_inferrable:\n        raise ValueError(\n          f\"Factor {factor} is not used in the rule, but size is provided\")\n\n    # Check that factors that are used for a whole dimension aren't in\n    # factor_sizes and factors that are never used for a whole dimension are\n    # in factor_sizes.\n    for factor, inferable in factors_inferrable.items():\n      if factor not in factor_sizes and not inferable:\n        raise ValueError(\n          f\"Factor {factor} is only used in compound factors; must specify\"\n          \" its size\")\n      if factor in factor_sizes and inferable:\n        raise ValueError(\n          f\"Factor {factor} represents a whole dimension; do not specify its\"\n          \" size\")\n\n    special_factors = set()\n    def check_special_factors(kind, factors):\n      if not isinstance(factors, tuple):","sourceCodeStart":126,"sourceCodeEnd":162,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jax/_src/custom_partitioning_sharding_rule.py#L126-L162","documentation":"Thrown by SdyShardingRule.__init__ when a factor name present in factor_sizes does not appear anywhere in the sharding rule string (operands or results). The rule parser builds the set of factors actually referenced; any extra key in factor_sizes is rejected because its size can never be used.","triggerScenarios":"Calling jax.experimental.custom_partitioning.def_partition (or str_to_sdy_sharding_rule) with factor_sizes={'a':2,'b':4} while the rule string only mentions 'a', e.g. rule='(a,a)->(a)' with a stray 'b' key.","commonSituations":"Renaming factors in the rule but forgetting to update factor_sizes; copy-pasting a rule from another op and leaving stale factor_sizes keys; typos in factor names inside the rule string.","solutions":["Remove the unused factor key from factor_sizes (or fix its spelling so it matches the rule)","Check every key of factor_sizes against the factor tokens (including those inside compound factors like '(a,b)') in the rule string","Regenerate factor_sizes from the rule programmatically to avoid drift"],"exampleFix":"# before\ndef_partition(fn, rule='(i,j)->(i,j)', factor_sizes={'i':8,'k':4})\n# after\ndef_partition(fn, rule='(i,j)->(i,j)', factor_sizes={'i':8})","handlingStrategy":"validation","validationCode":"rule_factors = set(re.findall(r'[A-Za-z_][A-Za-z0-9_]*', rule.split('->')[0] + rule.split('->')[1]))\nassert set(factor_sizes) <= rule_factors, f'unused factor_sizes keys: {set(factor_sizes) - rule_factors}'","typeGuard":"def has_only_used_factors(rule: str, factor_sizes: dict[str, int]) -> bool:\n    import re\n    used = set(re.findall(r'[A-Za-z_][A-Za-z0-9_]*', rule))\n    return set(factor_sizes) <= used","tryCatchPattern":null,"preventionTips":["Derive factor_sizes keys from the rule string, never maintain them by hand","Add a unit test asserting factor_sizes.keys() equals expected compound-only factors"],"tags":["jax","sharding","factor-sizes","validation"],"backgroundTag":"config-key-mismatch","analyzedSha":"1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb","analyzedAt":"2026-08-27T09:53:25.647Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}