Lightning-AI/pytorch-lightning · error · MisconfigurationException

Some provided `parameters_to_prune` don't exist in the model

Error message

Some provided `parameters_to_prune` don't exist in the model. Found missing modules: {missing_modules} and missing parameters: {missing_parameters}

What it means

ModelPruning.sanitize_parameters_to_prune (invoked from the setup hook) verifies each (module, parameter_name) pair against the actual model. If a listed module object isn't among the model's modules, or a module lacks the named parameter, it raises MisconfigurationException listing the missing modules and missing parameters.

Source

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

                for m in current_modules
                if getattr(m, p, None) is not None and isinstance(getattr(m, p, None), nn.Parameter)
            ]
        elif (
            isinstance(parameters_to_prune, (list, tuple))
            and len(parameters_to_prune) > 0
            and all(len(p) == 2 for p in parameters_to_prune)
            and all(isinstance(a, nn.Module) and isinstance(b, str) for a, b in parameters_to_prune)
        ):
            missing_modules, missing_parameters = [], []
            for module, name in parameters_to_prune:
                if module not in current_modules:
                    missing_modules.append(module)
                    continue
                if not hasattr(module, name):
                    missing_parameters.append(name)

            if missing_modules or missing_parameters:
                raise MisconfigurationException(
                    "Some provided `parameters_to_prune` don't exist in the model."
                    f" Found missing modules: {missing_modules} and missing parameters: {missing_parameters}"
                )
        else:
            raise MisconfigurationException(
                "The provided `parameters_to_prune` should either be list of tuple"
                " with 2 elements: (nn.Module, parameter_name_to_prune) or None"
            )

        return parameters_to_prune

    @staticmethod
    def _is_pruning_method(method: Any) -> bool:
        if not inspect.isclass(method):
            return False
        return issubclass(method, pytorch_prune.BasePruningMethod)

View on GitHub (pinned to 9fed5c27d2)

Solutions

  1. Build parameters_to_prune from the actual model instance that will be trained, e.g., [(m, 'weight') for m in model.modules() if isinstance(m, nn.Linear)]
  2. Check hasattr(module, param_name) for every entry before constructing the callback
  3. Only use parameter names that exist on the target modules ('weight' always; 'bias' only if the layer was created with bias=True)

Example fix

# before
parameters_to_prune = [(model.features[0], 'bias')]  # layer created with bias=False
# after
parameters_to_prune = [(m, 'weight') for m in model.modules() if isinstance(m, nn.Linear)]
Defensive patterns

Strategy: validation

Validate before calling

model_modules = {id(m) for m in model.modules()}
for mod, name in parameters_to_prune:
    assert id(mod) in model_modules and hasattr(mod, name), f'{mod} lacks {name}'

Type guard

def params_exist_in_model(params, model) -> bool:
    mods = list(model.modules())
    return all(p[0] in mods and len(p) == 2 and hasattr(p[0], p[1]) for p in params)

Try / catch

try:
    pruner.sanitize_parameters_to_prune(parameters_to_prune)
except Exception as e:
    parameters_to_prune = [(m, 'weight') for m in model.modules() if isinstance(m, nn.Linear)]

Prevention

When it happens

Trigger: Passing parameters_to_prune=[(some_other_module, 'weight')] where some_other_module is not part of the LightningModule; listing parameter name 'bias' for a module that has bias=False; pruning a layer that was replaced/removed before setup runs.

Common situations: Building parameters_to_prune from a different model instance (e.g., the raw nn.Module before wrapping, or after re-instantiating the model); Conv/Linear layers created with bias=False; refactors that rename layers after the pruning list was written.

Related errors


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