{"record":{"id":"b257bc8e3e3c8738","repo":"oobabooga/textgen","slug":"n-sigma-must-be-a-non-negative-float-but-is-n","errorCode":null,"errorMessage":"`n_sigma` must be a non-negative float, but is {n_sigma}","messagePattern":"`n_sigma` must be a non-negative float, but is (.+?)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"modules/sampler_hijack.py","lineNumber":209,"sourceCode":"            sorted_indices_to_remove[..., : self.min_tokens_to_keep] = 0\n\n        indices_to_remove = sorted_indices_to_remove.scatter(1, sorted_indices, sorted_indices_to_remove)\n        scores = scores.masked_fill(indices_to_remove, self.filter_value)\n        return scores\n\n\nclass TopNSigmaLogitsWarper(LogitsProcessor):\n    def __init__(self, n_sigma: float = 2.0, filter_value: float = -float(\"Inf\"), min_tokens_to_keep: int = 1):\n        \"\"\"\n        Initialize Top-nσ Sampling logits warper.\n\n        Args:\n            n_sigma: The threshold multiplier for standard deviation\n            filter_value: Value to assign to filtered logits\n            min_tokens_to_keep: Minimum number of tokens to keep\n        \"\"\"\n        if n_sigma < 0:\n            raise ValueError(f\"`n_sigma` must be a non-negative float, but is {n_sigma}\")\n        self.n_sigma = n_sigma\n        self.filter_value = filter_value\n        self.min_tokens_to_keep = min_tokens_to_keep\n\n    def __call__(self, input_ids: torch.LongTensor, scores: torch.FloatTensor) -> torch.FloatTensor:\n        # Calculate max of logits\n        max_logit = torch.max(scores, dim=-1, keepdim=True)[0]\n\n        # Calculate standard deviation only on finite values\n        finite_mask = torch.isfinite(scores)\n        finite_scores = scores.masked_fill(~finite_mask, 0.0)\n        std_logit = torch.std(finite_scores, dim=-1, keepdim=True)\n\n        # Create mask where tokens with logits >= max_logit - n_sigma * std_logit are kept\n        threshold = max_logit - self.n_sigma * std_logit\n        indices_to_remove = scores < threshold\n\n        if self.min_tokens_to_keep > 1:","sourceCodeStart":191,"sourceCodeEnd":227,"githubUrl":"https://github.com/oobabooga/textgen/blob/ed888c71f221df552750e1834b3654abab8ae345/modules/sampler_hijack.py#L191-L227","documentation":"Error \"`n_sigma` must be a non-negative float, but is {n_sigma}\" thrown in oobabooga/textgen.","triggerScenarios":"Raised during sampler validation when `n_sigma` is a negative number. Triggers when the n_sigma sampling parameter is set below 0; use 0 to disable or a non-negative float.","commonSituations":"See trigger scenarios.","solutions":["Set n_sigma to a non-negative float (>= 0); use 0 to disable the n_sigma sampler."],"exampleFix":null,"handlingStrategy":null,"validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"ed888c71f221df552750e1834b3654abab8ae345","analyzedAt":"2026-08-15T05:24:21.000Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}