Lightning-AI/pytorch-lightning · error · MisconfigurationException

`amount` should be provided and be either an int, a float or

Error message

`amount` should be provided and be either an int, a float or Callable function.

What it means

amount defines the fraction/quantity of weights to prune and must be an int, float, or a callable (so it can change over training). Any other type (None, str, list, tuple) raises MisconfigurationException in ModelPruning.__init__.

Source

Thrown at src/lightning/pytorch/callbacks/pruning.py:224

            raise MisconfigurationException(
                f"`pruning_fn` is expected to be a str in {list(_PYTORCH_PRUNING_FUNCTIONS.keys())}"
                f" or a PyTorch `BasePruningMethod`. Found: {pruning_fn}."
                " HINT: if passing a `BasePruningMethod`, pass the class, not an instance"
            )

        # need to ignore typing here since pytorch base class does not define the PRUNING_TYPE attribute
        if use_global_unstructured and pruning_fn.PRUNING_TYPE != "unstructured":  # type: ignore
            raise MisconfigurationException(
                'Only the "unstructured" PRUNING_TYPE is supported with `use_global_unstructured=True`.'
                f" Found method {pruning_fn} of type {pruning_fn.PRUNING_TYPE}. "  # type: ignore[union-attr]
            )

        self.pruning_fn = pruning_fn
        self._apply_pruning = apply_pruning
        self._make_pruning_permanent = make_pruning_permanent

        if not (isinstance(amount, (int, float)) or callable(amount)):
            raise MisconfigurationException(
                "`amount` should be provided and be either an int, a float or Callable function."
            )

        self.amount = amount

        if verbose not in (0, 1, 2):
            raise MisconfigurationException("`verbose` must be any of (0, 1, 2)")

        self._verbose = verbose

    def filter_parameters_to_prune(self, parameters_to_prune: _PARAM_LIST = ()) -> _PARAM_LIST:
        """This function can be overridden to control which module to prune."""
        return parameters_to_prune

    def _create_pruning_fn(self, pruning_fn: str, **kwargs: Any) -> Union[Callable, pytorch_prune.BasePruningMethod]:
        """This function takes `pruning_fn`, a function name.

        IF use_global_unstructured, pruning_fn will be resolved into its associated ``PyTorch BasePruningMethod`` ELSE,

View on GitHub (pinned to 9fed5c27d2)

Solutions

  1. Pass amount as a number (e.g., amount=0.5 for 50%) or a callable like amount=lambda epoch: min(0.1 * epoch, 0.9)
  2. Coerce config values: amount=float(cfg['prune_amount']) before constructing the callback
  3. For schedules, pass a function of the epoch rather than a list

Example fix

# before
ModelPruning(pruning_fn='l1_unstructured', amount='0.5')
# after
ModelPruning(pruning_fn='l1_unstructured', amount=0.5)
# or scheduled:
ModelPruning(pruning_fn='l1_unstructured', amount=lambda epoch: min(0.05 * epoch, 0.5))
Defensive patterns

Strategy: type-guard

Validate before calling

amount = float(cfg['amount']) if isinstance(cfg.get('amount'), str) else cfg.get('amount')
assert isinstance(amount, (int, float)) or callable(amount)

Type guard

def is_valid_amount(a) -> bool:
    return isinstance(a, (int, float)) or callable(a)

Prevention

When it happens

Trigger: ModelPruning(...) with amount=None, amount='0.5' (a string from YAML/JSON that wasn't coerced), or amount=[0.1, 0.5]; forgetting the parameter entirely when it has no default.

Common situations: Config-driven training where YAML values stay strings; sweep scripts passing tuples for multi-stage amounts; expecting a default amount to exist.

Understand the failure class

Background: "Wrong argument type", "must be a string", "expected Array or Prism::Scope": TypeError and ArgumentError when a library receives a value of the wrong type — this error's family across 28 libraries.

Related errors


AI-assisted analysis of Lightning-AI/pytorch-lightning@9fed5c27d2 (2026-08-28). Data as JSON: /api/errors/eb7f88da3a83c7f9. Report an issue: GitHub.