2noise/ChatTTS · error · ValueError

top_k must be -1 (disable), or at least 1, got {self.top_k}.

Error message

top_k must be -1 (disable), or at least 1, got {self.top_k}.

What it means

SamplingParams._verify_args enforces top_k == -1 (which disables top-k filtering) or top_k >= 1. Zero and values below -1 are rejected because top-k selects the k most likely tokens and k=0 selects nothing.

Source

Thrown at ChatTTS/model/velocity/sampling_params.py:208

                "presence_penalty must be in [-2, 2], got " f"{self.presence_penalty}."
            )
        if not -2.0 <= self.frequency_penalty <= 2.0:
            raise ValueError(
                "frequency_penalty must be in [-2, 2], got "
                f"{self.frequency_penalty}."
            )
        if not 0.0 < self.repetition_penalty <= 2.0:
            raise ValueError(
                "repetition_penalty must be in (0, 2], got "
                f"{self.repetition_penalty}."
            )
        # if self.temperature < 0.0:
        #     raise ValueError(
        #         f"temperature must be non-negative, got {self.temperature}.")
        if not 0.0 < self.top_p <= 1.0:
            raise ValueError(f"top_p must be in (0, 1], got {self.top_p}.")
        if self.top_k < -1 or self.top_k == 0:
            raise ValueError(
                f"top_k must be -1 (disable), or at least 1, " f"got {self.top_k}."
            )
        if not 0.0 <= self.min_p <= 1.0:
            raise ValueError("min_p must be in [0, 1], got " f"{self.min_p}.")
        if self.max_tokens < 1:
            raise ValueError(f"max_tokens must be at least 1, got {self.max_tokens}.")
        if self.logprobs is not None and self.logprobs < 0:
            raise ValueError(f"logprobs must be non-negative, got {self.logprobs}.")
        if self.prompt_logprobs is not None and self.prompt_logprobs < 0:
            raise ValueError(
                f"prompt_logprobs must be non-negative, got " f"{self.prompt_logprobs}."
            )

    def _verify_beam_search(self) -> None:
        if self.best_of == 1:
            raise ValueError(
                "best_of must be greater than 1 when using beam "
                f"search. Got {self.best_of}."

View on GitHub (pinned to 77b89ee281)

Solutions

  1. Use top_k=-1 to disable top-k filtering
  2. Use top_k=1 (with temperature 0) for greedy decoding
  3. Clamp computed top_k values to >= 1 or exactly -1

Example fix

# before
params = SamplingParams(top_k=0)  # trying to disable

# after
params = SamplingParams(top_k=-1)  # disabled
Defensive patterns

Strategy: validation

Validate before calling

def normalize_top_k(k):
    k = int(k)
    if k == 0:
        return -1  # caller meant "disabled"
    return k if k >= 1 else -1

Type guard

def is_valid_top_k(k) -> bool:
    return isinstance(k, int) and (k == -1 or k >= 1)

Prevention

When it happens

Trigger: Constructing SamplingParams with top_k=0, top_k=-2, or a non-integer value that fails the comparison.

Common situations: Trying to disable top-k by setting 0 instead of -1; passing num_candidates=0 from an empty config; off-by-one when computing top_k dynamically.

Related errors


AI-assisted analysis of 2noise/ChatTTS@77b89ee281 (2026-08-26). Data as JSON: /api/errors/13c029361695ebba. Report an issue: GitHub.