{"record":{"id":"81cdde48e83da11c","repo":"huggingface/transformers","slug":"ngrams-should-be-of-shape-batch-size-num-ngrams-81cdde","errorCode":null,"errorMessage":"Ngrams should be of shape (batch_size, num_ngrams, ngram_len), where ngram_len is {self.ngram_len}, but is {ngrams.shape}","messagePattern":"Ngrams should be of shape \\(batch_size, num_ngrams, ngram_len\\), where ngram_len is (.+?), but is (.+?)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"src/transformers/generation/logits_process.py","lineNumber":2813,"sourceCode":"            current_hash = torch.add(current_hash, data[..., i])\n            current_hash = torch.mul(current_hash, multiplier)\n            current_hash = torch.add(current_hash, increment)\n        return current_hash\n\n    def compute_ngram_keys(self, ngrams: torch.LongTensor) -> torch.LongTensor:\n        \"\"\"Computes random keys for each ngram and depth.\n\n        Args:\n            ngrams (`torch.LongTensor`):\n                Ngrams (batch_size, num_ngrams, ngram_len).\n\n        Returns:\n            ngram keys (batch_size, num_ngrams, depth).\n        \"\"\"\n        if len(ngrams.shape) != 3:\n            raise ValueError(f\"Ngrams should be of shape (batch_size, num_ngrams, ngram_len), but is {ngrams.shape}\")\n        if ngrams.shape[2] != self.ngram_len:\n            raise ValueError(\n                \"Ngrams should be of shape (batch_size, num_ngrams, ngram_len),\"\n                f\" where ngram_len is {self.ngram_len}, but is {ngrams.shape}\"\n            )\n        batch_size, _, _ = ngrams.shape\n\n        hash_result = torch.ones(batch_size, device=self.device, dtype=torch.long)\n        # hash_result shape [batch_size,]\n        # ngrams shape [batch_size, num_ngrams, ngram_len]\n        hash_result = torch.vmap(self.accumulate_hash, in_dims=(None, 1), out_dims=1)(hash_result, ngrams)\n        # hash_result shape [batch_size, num_ngrams]\n\n        keys = self.keys[None, None, :, None]\n        # hash_result shape [batch_size, num_ngrams]\n        # keys shape [1, 1, depth, 1]\n        hash_result = torch.vmap(self.accumulate_hash, in_dims=(None, 2), out_dims=2)(hash_result, keys)\n        # hash_result shape [batch_size, num_ngrams, depth]\n\n        return hash_result","sourceCodeStart":2795,"sourceCodeEnd":2831,"githubUrl":"https://github.com/huggingface/transformers/blob/a597f974857b3d92939971296bc0deb93d33d780/src/transformers/generation/logits_process.py#L2795-L2831","documentation":"Error \"Ngrams should be of shape (batch_size, num_ngrams, ngram_len), where ngram_len is {self.ngram_len}, but is {ngrams.shape}\" thrown in huggingface/transformers.","triggerScenarios":"Raised in an n-gram repetition penalty processor when the ngrams tensor's last dimension does not equal the configured ngram_len.","commonSituations":"Supplying n-grams of a different length than the processor's ngram_len, e.g. trigrams to a bigram processor.","solutions":["Ensure the last dimension of ngrams equals the processor's `ngram_len`.","Recreate the processor with the correct `ngram_len` for your ngram tensor."],"exampleFix":null,"handlingStrategy":null,"validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"a597f974857b3d92939971296bc0deb93d33d780","analyzedAt":"2026-08-14T18:24:08.354Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}