{"record":{"id":"ca2094a9a4554c6a","repo":"huggingface/transformers","slug":"ngram-size-has-to-be-a-strictly-positive-integer","errorCode":null,"errorMessage":"`ngram_size` has to be a strictly positive integer, but is {ngram_size}","messagePattern":"`ngram_size` has to be a strictly positive integer, but is (.+?)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"src/transformers/generation/logits_process.py","lineNumber":1117,"sourceCode":"\n    >>> model = AutoModelForCausalLM.from_pretrained(\"distilbert/distilgpt2\")\n    >>> tokenizer = AutoTokenizer.from_pretrained(\"distilbert/distilgpt2\")\n    >>> inputs = tokenizer([\"Today I\"], return_tensors=\"pt\")\n\n    >>> output = model.generate(**inputs)\n    >>> print(tokenizer.decode(output[0], skip_special_tokens=True))\n    Today I'm not sure if I'm going to be able to do it.\n\n    >>> # Now let's add ngram size using `no_repeat_ngram_size`. This stops the repetitions (\"I'm\") in the output.\n    >>> output = model.generate(**inputs, no_repeat_ngram_size=2)\n    >>> print(tokenizer.decode(output[0], skip_special_tokens=True))\n    Today I'm not sure if I can get a better understanding of the nature of this issue\n    ```\n    \"\"\"\n\n    def __init__(self, ngram_size: int):\n        if not isinstance(ngram_size, int) or ngram_size <= 0:\n            raise ValueError(f\"`ngram_size` has to be a strictly positive integer, but is {ngram_size}\")\n        self.ngram_size = ngram_size\n\n    @add_start_docstrings(LOGITS_PROCESSOR_INPUTS_DOCSTRING)\n    def __call__(self, input_ids: torch.LongTensor, scores: torch.FloatTensor) -> torch.FloatTensor:\n        cur_len = input_ids.shape[-1]\n        # No complete ngram yet, so nothing to ban\n        if cur_len < self.ngram_size:\n            return scores\n\n        # An ngram can only be completed by the next token if it starts with the current suffix, so we match that one\n        # prefix against every window instead of building all ngrams. A matching window bans its own last token. (The\n        # window starting at the prefix needs one token more than we have, so a prefix never bans its own successor.)\n        prefix = input_ids[:, cur_len + 1 - self.ngram_size :]\n        windows = input_ids.unfold(dimension=1, size=self.ngram_size, step=1)\n        matches = (windows[..., :-1] == prefix.unsqueeze(1)).all(dim=-1)\n\n        # Non-matching windows go to a spare column past the vocab, where they can't unban another window's token\n        vocab_size = scores.shape[-1]","sourceCodeStart":1099,"sourceCodeEnd":1135,"githubUrl":"https://github.com/huggingface/transformers/blob/a597f974857b3d92939971296bc0deb93d33d780/src/transformers/generation/logits_process.py#L1099-L1135","documentation":"Thrown by NoRepeatNGramLogitsProcessor.__init__ when ngram_size is not a Python int or is <= 0. The processor bans any token that would complete an n-gram of this size already seen in the sequence, so the size must be a strictly positive integer (1 degenerates to banning every seen token and is rarely wanted).","triggerScenarios":"NoRepeatNGramLogitsProcessor(0); ngram_size=2.0 (float); model.generate(no_repeat_ngram_size=0) intending 'disabled'; numpy integers.","commonSituations":"Configs using 0 to disable the constraint (here you must omit the key); floats from templated configs; very small values like 1 producing degenerate outputs that loop back to invalid setups.","solutions":["To disable n-gram blocking, remove no_repeat_ngram_size from the generate call / generation config","Otherwise pass a positive int, typically 2–4: no_repeat_ngram_size=2","Coerce external values: int(x) after asserting x >= 1"],"exampleFix":"# before\nout = model.generate(**inputs, no_repeat_ngram_size=0)  # 'disable' -> ValueError\n\n# after\nout = model.generate(**inputs)  # disabled by omission\n# or:\nout = model.generate(**inputs, no_repeat_ngram_size=2)","handlingStrategy":"validation","validationCode":"def valid_ngram_size(n):\n    return isinstance(n, int) and n > 0","typeGuard":"def is_valid_ngram_size(n) -> bool:\n    return type(n) is int and n > 0","tryCatchPattern":"try:\n    proc = NoRepeatNGramLogitsProcessor(int(n))\nexcept ValueError as e:\n    raise ValueError(f'no_repeat_ngram_size={n!r} must be >= 1; omit to disable') from e","preventionTips":["Omit no_repeat_ngram_size to disable — 0 raises","Values 2-4 work best; 1 degenerates to banning all seen tokens","Keep the value as int in configs"],"tags":["generation","no-repeat-ngram","argument-validation"],"backgroundTag":null,"analyzedSha":"a597f974857b3d92939971296bc0deb93d33d780","analyzedAt":"2026-08-14T18:24:08.354Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}