{"record":{"id":"ebfbdcba80914af6","repo":"huggingface/transformers","slug":"penalty-has-to-be-a-strictly-positive-float-but","errorCode":null,"errorMessage":"`penalty` has to be a strictly positive float, but is {penalty}","messagePattern":"`penalty` has to be a strictly positive float, but is (.+?)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"src/transformers/generation/logits_process.py","lineNumber":360,"sourceCode":"    I'm not going to be able to do that. I'll just have to go out and play\n\n    >>> # We can also exclude the input prompt by creating an instance of this class\n    >>> # with a `prompt_ignore_length` and passing it as a custom logit processor\n    >>> rep_pen_processor = RepetitionPenaltyLogitsProcessor(\n    ...     penalty=1.1,\n    ...     prompt_ignore_length=inputs[\"input_ids\"].shape[-1]\n    ... )\n    >>> penalized_ids = model.generate(**inputs, logits_processor=[rep_pen_processor])\n    >>> print(tokenizer.batch_decode(penalized_ids, skip_special_tokens=True)[0])\n    I'm not going to be able to do that. I'm going to have to go through a lot of things, and\n    ```\n    \"\"\"\n\n    supports_continuous_batching = False\n\n    def __init__(self, penalty: float, prompt_ignore_length: int | None = None):\n        if not isinstance(penalty, float) or not (penalty > 0):\n            raise ValueError(f\"`penalty` has to be a strictly positive float, but is {penalty}\")\n\n        if prompt_ignore_length is not None and (\n            not isinstance(prompt_ignore_length, int) or prompt_ignore_length < 0\n        ):\n            raise ValueError(f\"`prompt_ignore_length` has to be a positive integer, but is {prompt_ignore_length}\")\n\n        self.penalty = penalty\n        self.prompt_ignore_length = prompt_ignore_length\n        self.logits_indices = None\n        self.cu_seq_lens_q = None\n\n    @add_start_docstrings(LOGITS_PROCESSOR_INPUTS_DOCSTRING)\n    def __call__(self, input_ids: torch.LongTensor, scores: torch.FloatTensor) -> torch.FloatTensor:\n        if self.prompt_ignore_length:\n            input_ids = input_ids[:, self.prompt_ignore_length :]\n\n        if scores.dim() == 3:\n            if self.logits_indices is not None and self.cu_seq_lens_q is not None:","sourceCodeStart":342,"sourceCodeEnd":378,"githubUrl":"https://github.com/huggingface/transformers/blob/a597f974857b3d92939971296bc0deb93d33d780/src/transformers/generation/logits_process.py#L342-L378","documentation":"Thrown by RepetitionPenaltyLogitsProcessor.__init__ when penalty is not a Python float or is <= 0. The penalty multiplies/divides logits of already-seen tokens, so a non-positive value has no valid meaning. The strict isinstance(penalty, float) check rejects ints even if positive (e.g. penalty=1 as int fails, 1.0 passes).","triggerScenarios":"RepetitionPenaltyLogitsProcessor(1) with an int (isinstance check fails); penalty=0.0 or a negative float; building this processor indirectly via model.generate(repetition_penalty=...) after generation config loads an int-typed value.","commonSituations":"Config files that store repetition_penalty: 1 (int) which some YAML loaders keep as int; intending penalty=1.0 (a no-op) but writing it as int; programmatic sweeps that step penalty in numpy int or int values.","solutions":["Pass a positive float literal: RepetitionPenaltyLogitsProcessor(1.2)","Wrap config-sourced values: RepetitionPenaltyLogitsProcessor(float(penalty)) after asserting penalty > 0","If penalty == 1.0, skip adding the processor entirely — it is a mathematical no-op"],"exampleFix":"# before\nproc = RepetitionPenaltyLogitsProcessor(1)  # int -> ValueError\n\n# after\nproc = RepetitionPenaltyLogitsProcessor(1.0)\n# or skip when neutral:\nprocs = [] if penalty == 1 else [RepetitionPenaltyLogitsProcessor(float(penalty))]","handlingStrategy":"validation","validationCode":"def valid_penalty(p):\n    return isinstance(p, float) and p > 0.0","typeGuard":"def is_valid_penalty(p) -> bool:\n    return type(p) is float and p > 0.0","tryCatchPattern":"try:\n    proc = RepetitionPenaltyLogitsProcessor(float(p))\nexcept ValueError as e:\n    raise ValueError(f'Bad repetition_penalty={p!r}: {e}') from e","preventionTips":["Serialize repetition_penalty as float in configs (1.2, not 1)","Skip the processor when penalty == 1.0 — it is a no-op","Convert numpy scalars with float() before passing"],"tags":["generation","repetition-penalty","sampling","argument-validation"],"backgroundTag":null,"analyzedSha":"a597f974857b3d92939971296bc0deb93d33d780","analyzedAt":"2026-08-14T18:24:08.354Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}