+15






![mintlify[bot]](/assets/img/avatar_default.png)


Mingyi
AdityaVKochar
mintlify[bot]
adhyan-jain
Adhyan Jain
Maitri-shah29
Adarsh Shirawalmath
Maitri Shah
Aditya Vardhan Kochar
Rishit Shivam
Rishitshivam
IshhanKheria
Ishita Joshi
Richard Chen
longGGGGGG
Richard
Nakul Sinha
Divyam Agrawal
Richardczl98
Krishang Zinzuwadia
nimeshas
Claude Opus 4.6
github-actions[bot]
Jignas Paturu
zijiexia
a3291b5654
Co-authored-by: AdityaVKochar <adityavardhankochar@gmail.com> Co-authored-by: mintlify[bot] <109931778+mintlify[bot]@users.noreply.github.com> Co-authored-by: adhyan-jain <adhyanjain2006@gmail.com> Co-authored-by: Adhyan Jain <71976554+adhyan-jain@users.noreply.github.com> Co-authored-by: Maitri-shah29 <maitrirajivshah@gmail.com> Co-authored-by: Adarsh Shirawalmath <114558126+adarshxs@users.noreply.github.com> Co-authored-by: Maitri Shah <shah29maitri@gmail.com> Co-authored-by: Aditya Vardhan Kochar <80113212+AdityaVKochar@users.noreply.github.com> Co-authored-by: Rishit Shivam <164783543+pokymono@users.noreply.github.com> Co-authored-by: Rishitshivam <164783543+Rishitshivam@users.noreply.github.com> Co-authored-by: IshhanKheria <ishhankheria06@gmail.com> Co-authored-by: Ishita Joshi <ishitata.joshi@gmail.com> Co-authored-by: Richard Chen <104477092+Richardczl98@users.noreply.github.com> Co-authored-by: longGGGGGG <553746008@qq.com> Co-authored-by: Richard <richardchen@radixark.ai> Co-authored-by: Nakul Sinha <nakul.new4socials@gmail.com> Co-authored-by: Divyam Agrawal <ludicrouslytrue@gmail.com> Co-authored-by: Richardczl98 <Zhenlinc@stanford.edu> Co-authored-by: Krishang Zinzuwadia <krishangzinzuwadia@gmail.com> Co-authored-by: nimeshas <nimesha.s106@gmail.com> Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com> Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com> Co-authored-by: Jignas Paturu <86356085+JignasP@users.noreply.github.com> Co-authored-by: zijiexia <37504505+zijiexia@users.noreply.github.com>
82 lines
3.1 KiB
Plaintext
82 lines
3.1 KiB
Plaintext
---
|
||
title: "Choices Methods in SGLang"
|
||
metatags:
|
||
description: "SGLang choices methods: token_length_normalized, greedy_token_selection, unconditional_likelihood_normalized."
|
||
---
|
||
This doc describes the choices methods supported by SGLang.
|
||
|
||
The optional `choices_method` arg determines how options supplied to SGLang's `choices` primitive are selected. Only the `RuntimeEndpoint` backend supports the `choices_method` arg. Other backends, such as `OpenAI`, have bespoke selection implementations due to API limitations.
|
||
|
||
## Methods
|
||
|
||
### Token Length Normalized
|
||
|
||
Token length normalized is the default SGLang choices method. It selects the option with the highest average logprob across all of its tokens.
|
||
|
||
Usage example (alternatively, simply omit the `choices_method` arg):
|
||
```python Example
|
||
@sgl.function
|
||
def example(s):
|
||
s += sgl.user("What is the capital of France?")
|
||
s += sgl.assistant(
|
||
sgl.gen(
|
||
"answer",
|
||
choices=["London", "Paris", "Berlin"],
|
||
choices_method=sgl.token_length_normalized,
|
||
)
|
||
)
|
||
```
|
||
|
||
|
||
This can perform poorly if an option contains many tokens, where its later tokens are predicted with high confidence based on its earlier tokens. For instance, even strong models will fail the above example if the specified options are `["Paris", "Antidisestablishmentarianism"]`.
|
||
|
||
### Greedy Token Selection
|
||
|
||
Greedy token selection simply selects the option with the highest logprob for its initial token. For overlapping options where one option is a subset of a longer option, the logprobs of the shorter option are extended using its average logprob for comparison against the longer option.
|
||
|
||
Usage example:
|
||
```python Example
|
||
@sgl.function
|
||
def example(s):
|
||
s += sgl.user("What is the capital of France?")
|
||
s += sgl.assistant(
|
||
sgl.gen(
|
||
"answer",
|
||
choices=["London", "Paris", "Berlin"],
|
||
choices_method=sgl.greedy_token_selection,
|
||
)
|
||
)
|
||
```
|
||
|
||
This can perform poorly if an option misleads the model down a bad path based on an attractive initial token. For instance, greedy selection will result in an incorrect response for this example:
|
||
```python Example
|
||
@sgl.function
|
||
def us_president_example(s):
|
||
s += sgl.user("Name a US president.")
|
||
s += sgl.assistant(
|
||
sgl.gen(
|
||
"answer",
|
||
choices=["Donald Duck", "Millard Fillmore"],
|
||
choices_method=sgl.greedy_token_selection,
|
||
)
|
||
)
|
||
```
|
||
|
||
### Unconditional Likelihood Normalized
|
||
|
||
Unconditional likelihood normalized selects the option with the highest average token logprob once normalized by the unconditional token logprobs, as described in [this EleutherAI blogpost](https://blog.eleuther.ai/multiple-choice-normalization/). This method incurs an additional LLM call to obtain the unconditional likelihoods.
|
||
|
||
Usage example:
|
||
```python Example
|
||
@sgl.function
|
||
def example(s):
|
||
s += sgl.user("What is the capital of France?")
|
||
s += sgl.assistant(
|
||
sgl.gen(
|
||
"answer",
|
||
choices=["London", "Paris", "Berlin"],
|
||
choices_method=sgl.unconditional_likelihood_normalized,
|
||
)
|
||
)
|
||
```
|