Skip to content

Feature Request: Add Min-P sampling layer #1154

Closed

Description

@aikitoria

It would be very nice if the library supported using Min-P sampling as an alternative to Top-P/Top-K. This became popular for local LLMs in the past few months because it provides significantly more useful results, or at least feels like it does. More info here: https://www.reddit.com/r/LocalLLaMA/comments/17vonjo/your_settings_are_probably_hurting_your_model_why/

image

Most other libraries already support it, examples:
turboderp-org/exllamav2@0d436d7
ggml-org/llama.cpp#3841

This only requires a single parameter - consider all tokens whose probability is greater than than the probability of the first one scaled down by some number.

Activity

  1. added
    feature request
    on Feb 27, 2024
  2. aikitoria commented on Feb 29, 2024

    @aikitoria
    ContributorAuthor

    Forgot to mention: this sampling method should be applied before temperature

  3. 0xymoro commented on Mar 11, 2024

    @0xymoro
    Contributor

    @ncomly-nvidia seconded on adding min p - makes a noticeable impact on production, doesn't seem too bad to implement compared to some others.

  4. aikitoria commented on Apr 12, 2024

    @aikitoria
    ContributorAuthor

    @byshiue any chance of this being added soon? 👀

    Most other engines have it now, it's in vLLM and HF transformers is also adding it.

  5. nivekgnaij commented on Apr 14, 2024

    @nivekgnaij

    ^^ has a huge impact on production

  6. pathorn commented on May 2, 2024

    @pathorn
    Contributor

    @aikitoria I found that it was far easier and performant to implement in decodingCommon.cu since the same math used for logprobs can be used for calculating the relative threshold used in min-p sampling
    See my PR in #1536

    I'll need to review that things are done in the correct order: I'm still grappling with the codebase but I assumed it should be doing min-p before sampling

  7. byshiue commented on May 29, 2024

    @byshiue
    Collaborator

    Also mentioned in this issue #1683.

  8. cyr0930 commented on May 29, 2024

    @cyr0930

    I hope this feature being added any time soon!

  9. DreamGenX commented on May 29, 2024

    @DreamGenX
    Contributor

    You can implement this as a logit processor as far as I can tell:

    def _get_min_p_fn(min_p: float):
        def _fn(
            logits: torch.Tensor,
        ) -> torch.Tensor:
            probs = torch.softmax(logits)
            top_prob = probs.max()
            scaled_min_p = min_p * top_prob
            tokens_to_remove = probs < scaled_min_p
            logits = logits.masked_fill(tokens_to_remove, -float("inf"))
    
            return logits
    
        return _fn
  10. aikitoria commented on May 29, 2024

    @aikitoria
    ContributorAuthor

    Is that not much slower than it would be if properly implemented it in the CUDA sampling layers?

    Just saw the PR above also. That's an interesting way. Highly doubt nvidia would accept it given it seems more like a hack, but it gives us something to experiment with...

  11. DreamGenX commented on May 29, 2024

    @DreamGenX
    Contributor

    @aikitoria Yeah, since it's computed per request and not in a batch (see also #1681). But if you are already using other logit processors, it might not have a big of an effect.

  12. DreamGenX commented on Jun 14, 2024

    @DreamGenX
    Contributor

    FWIW the new executor API does not allow parametrizing logit processors per-request anymore -- they are fixed at startup -- so one can't implement MinP that way. You have to go lower-level to GptManager in C++, so bumping this thread @ncomly-nvidia @AdamzNV

  13. hello-11 commented on Nov 15, 2024

    @hello-11
    Contributor
  14. aikitoria commented on Nov 20, 2024

    @aikitoria
    ContributorAuthor

    This would still be great to have by default, so we don't have to maintain a custom build just to change what the sampling layer does :)

  15. aikitoria commented on Dec 30, 2024

    @aikitoria
    ContributorAuthor

    I'm tired of waiting so I'm just doing it myself the way I thought it should be done... but it is EXTREMELY FRUSTRATING that this library has closed source parts for no reason. For example, I can't modify executor to actually pass the min_p parameter through from the frontend, or allow top_p and min_p to exist at the same time by grouping them in separate batches for sampling. Why???

    FWIW I don't think the PR linked above is the correct way to do this. When min_p is in use, top_k and top_p layers should not run, and temperature needs to be applied after min_p filter. We can also fuse min_p filter, late temperature and sampling in a single kernel for best performance if logprobs is not required (I don't use it).

  16. aikitoria commented on Dec 31, 2024

    @aikitoria
    ContributorAuthor

    I've created a WIP branch with my experiment here: main...aikitoria:TensorRT-LLM:min-p

  17. added
    triaged
    on May 16, 2025
  18. poweiw commented on May 16, 2025

    @poweiw
    Contributor

    Closing since feature merged in #2755. Feel free to reopen

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

feature requesttriaged

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions