Skip to content

Add topK, minP and penalty parameters to GenerateParameters#141

Merged
davidkoski merged 1 commit intoml-explore:mainfrom
adrgrondin:feat/add-parameters
Mar 12, 2026
Merged

Add topK, minP and penalty parameters to GenerateParameters#141
davidkoski merged 1 commit intoml-explore:mainfrom
adrgrondin:feat/add-parameters

Conversation

@adrgrondin
Copy link
Contributor

Proposed changes

The Swift func GenerateParameters has been lacking some parameters compared to Python mlx-lm. This is an attempt to add more parameters using mlx_lm/sample_utils.py as reference.

Motivated mainly by the new Qwen 3.5 models recommending more parameters for inference.

Example from https://huggingface.co/Qwen/Qwen3.5-4B:

We recommend using the following set of sampling parameters for generation

Thinking mode for general tasks: temperature=1.0, top_p=0.95, top_k=20, min_p=0.0, presence_penalty=1.5, repetition_penalty=1.0
Thinking mode for precise coding tasks (e.g. WebDev): temperature=0.6, top_p=0.95, top_k=20, min_p=0.0, presence_penalty=0.0, repetition_penalty=1.0
Instruct (or non-thinking) mode for general tasks: temperature=0.7, top_p=0.8, top_k=20, min_p=0.0, presence_penalty=1.5, repetition_penalty=1.0
Instruct (or non-thinking) mode for reasoning tasks: temperature=1.0, top_p=0.95, top_k=20, min_p=0.0, presence_penalty=1.5, repetition_penalty=1.0

Hard to correctly test in real world but here some examples I have used:

// 1) Deterministic top-k gate (manual test: run same prompt 5x)
// Prompt: "The capital of France is"
let parameters = GenerateParameters(
    maxTokens: 32,
    temperature: 1.0,
    topP: 1.0,
    topK: 1,
    minP: 0.0
)

// 2) Top-k diversity (manual test: run same prompt 10x)
// Prompt: "The capital of France is"
let parameters = GenerateParameters(
    maxTokens: 32,
    temperature: 1.0,
    topP: 1.0,
    topK: 40,
    minP: 0.0
)

// 3) Aggressive min-p pruning
// Prompt: "Answer with one word: true or false. 2+2=4"
let parameters = GenerateParameters(
    maxTokens: 16,
    temperature: 1.0,
    topP: 1.0,
    topK: 0,
    minP: 0.95
)

// 4) Presence penalty (reduce token re-use)
// Prompt: "Output 30 comma-separated animals."
let parameters = GenerateParameters(
    maxTokens: 80,
    temperature: 0.8,
    topP: 1.0,
    topK: 0,
    minP: 0.0,
    presencePenalty: 1.0,
    presenceContextSize: 20
)

// 5) Frequency penalty (reduce repeated words by count)
// Prompt: "Write 40 words about the ocean."
let parameters = GenerateParameters(
    maxTokens: 80,
    temperature: 0.8,
    topP: 1.0,
    topK: 0,
    minP: 0.0,
    frequencyPenalty: 1.0,
    frequencyContextSize: 20
)

Would appreciate a review from someone more knowledgeable than me on the subject, making sure no regression and correct implementation.

Checklist

  • I have read the CONTRIBUTING document
  • I have run pre-commit run --all-files to format my code / installed pre-commit prior to committing changes
  • I have added tests that prove my fix is effective or that my feature works
  • I have updated the necessary documentation (if needed)

@adrgrondin
Copy link
Contributor Author

@davidkoski could you re-run the tests? Looks like testChatSessionSync() failed (outputting an empty string). I tried to reproduce it running both main and my branch many many times and was bale to reproduce in both but very rare. This run was probably an unlucky one.

@davidkoski
Copy link
Collaborator

@davidkoski could you re-run the tests? Looks like testChatSessionSync() failed (outputting an empty string). I tried to reproduce it running both main and my branch many many times and was bale to reproduce in both but very rare. This run was probably an unlucky one.

Yes, this is #128 . Rerunning!

@adrgrondin
Copy link
Contributor Author

@davidkoski could you re-run the tests? Looks like testChatSessionSync() failed (outputting an empty string). I tried to reproduce it running both main and my branch many many times and was bale to reproduce in both but very rare. This run was probably an unlucky one.

Yes, this is #128 . Rerunning!

Thanks!

Copy link
Collaborator

@davidkoski davidkoski left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changes look good, thank you!

@davidkoski davidkoski merged commit bc3c20e into ml-explore:main Mar 12, 2026
3 of 4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants