{"record":{"id":"eb7f88da3a83c7f9","repo":"Lightning-AI/pytorch-lightning","slug":"amount-should-be-provided-and-be-either-an-int","errorCode":null,"errorMessage":"`amount` should be provided and be either an int, a float or Callable function.","messagePattern":"`amount` should be provided and be either an int, a float or Callable function\\.","errorType":"exception","errorClass":"MisconfigurationException","httpStatus":null,"severity":"error","filePath":"src/lightning/pytorch/callbacks/pruning.py","lineNumber":224,"sourceCode":"            raise MisconfigurationException(\n                f\"`pruning_fn` is expected to be a str in {list(_PYTORCH_PRUNING_FUNCTIONS.keys())}\"\n                f\" or a PyTorch `BasePruningMethod`. Found: {pruning_fn}.\"\n                \" HINT: if passing a `BasePruningMethod`, pass the class, not an instance\"\n            )\n\n        # need to ignore typing here since pytorch base class does not define the PRUNING_TYPE attribute\n        if use_global_unstructured and pruning_fn.PRUNING_TYPE != \"unstructured\":  # type: ignore\n            raise MisconfigurationException(\n                'Only the \"unstructured\" PRUNING_TYPE is supported with `use_global_unstructured=True`.'\n                f\" Found method {pruning_fn} of type {pruning_fn.PRUNING_TYPE}. \"  # type: ignore[union-attr]\n            )\n\n        self.pruning_fn = pruning_fn\n        self._apply_pruning = apply_pruning\n        self._make_pruning_permanent = make_pruning_permanent\n\n        if not (isinstance(amount, (int, float)) or callable(amount)):\n            raise MisconfigurationException(\n                \"`amount` should be provided and be either an int, a float or Callable function.\"\n            )\n\n        self.amount = amount\n\n        if verbose not in (0, 1, 2):\n            raise MisconfigurationException(\"`verbose` must be any of (0, 1, 2)\")\n\n        self._verbose = verbose\n\n    def filter_parameters_to_prune(self, parameters_to_prune: _PARAM_LIST = ()) -> _PARAM_LIST:\n        \"\"\"This function can be overridden to control which module to prune.\"\"\"\n        return parameters_to_prune\n\n    def _create_pruning_fn(self, pruning_fn: str, **kwargs: Any) -> Union[Callable, pytorch_prune.BasePruningMethod]:\n        \"\"\"This function takes `pruning_fn`, a function name.\n\n        IF use_global_unstructured, pruning_fn will be resolved into its associated ``PyTorch BasePruningMethod`` ELSE,","sourceCodeStart":206,"sourceCodeEnd":242,"githubUrl":"https://github.com/Lightning-AI/pytorch-lightning/blob/9fed5c27d2a62ff0efd6c3573599921d6ff67c14/src/lightning/pytorch/callbacks/pruning.py#L206-L242","documentation":"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__.","triggerScenarios":"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.","commonSituations":"Config-driven training where YAML values stay strings; sweep scripts passing tuples for multi-stage amounts; expecting a default amount to exist.","solutions":["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)","Coerce config values: amount=float(cfg['prune_amount']) before constructing the callback","For schedules, pass a function of the epoch rather than a list"],"exampleFix":"# before\nModelPruning(pruning_fn='l1_unstructured', amount='0.5')\n# after\nModelPruning(pruning_fn='l1_unstructured', amount=0.5)\n# or scheduled:\nModelPruning(pruning_fn='l1_unstructured', amount=lambda epoch: min(0.05 * epoch, 0.5))","handlingStrategy":"type-guard","validationCode":"amount = float(cfg['amount']) if isinstance(cfg.get('amount'), str) else cfg.get('amount')\nassert isinstance(amount, (int, float)) or callable(amount)","typeGuard":"def is_valid_amount(a) -> bool:\n    return isinstance(a, (int, float)) or callable(a)","tryCatchPattern":null,"preventionTips":["Coerce config strings to float before passing","Use a callable for pruning schedules instead of lists"],"tags":["pytorch-lightning","pruning","amount","type-error"],"backgroundTag":"wrong-argument-type","analyzedSha":"9fed5c27d2a62ff0efd6c3573599921d6ff67c14","analyzedAt":"2026-08-28T11:52:41.083Z","schemaVersion":2},"datasetVersion":"2026-08-28T16:17:29.566Z"}