Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG]: DefragThreshold default is not matching llama.cpp and probably not intended #716

Closed
dlyz opened this issue May 4, 2024 · 6 comments · Fixed by #729
Closed

[BUG]: DefragThreshold default is not matching llama.cpp and probably not intended #716

dlyz opened this issue May 4, 2024 · 6 comments · Fixed by #729
Labels
bug Something isn't working good first issue Good for newcomers

Comments

@dlyz
Copy link
Contributor

dlyz commented May 4, 2024

Description

DefragThreshold is 0 by default in LLamaSharp with the intent that it will disable defragmentation on the threshold excess.

/// defragment the KV cache if holes/size > defrag_threshold, Set to < 0 to disable (default)

public float DefragThreshold { get; set; }

But actually it should be negative to disable this mechanics (-1). 0 means "do defrag when the smallest fragmentation discovered".

This may negatively (maybe sometimes positively) impact performance in some advanced cases, for example with batching.

See llama.cpp default https://github.com/ggerganov/llama.cpp/blob/3ab8b3a92ede46df88bc5a2dfca3777de4a2b2b6/llama.cpp#L11630

Reproduction Steps

-

Environment & Configuration

-

Known Workarounds

No response

@martindevans
Copy link
Member

martindevans commented May 4, 2024

Good catch! Would you be itnerested in putting together a PR to fix this?

The easiest fix would be to just add a default value of -1.

I think a better fix would be to change float DefragThreshold to float? DefragThreshold and then to convert null to -1 in here. That way the default C# value (null) converts to the default llama.cpp value and there are no "magic" numbers.

@AsakusaRinne AsakusaRinne added bug Something isn't working good first issue Good for newcomers labels May 6, 2024
@Neilblaze
Copy link

@AsakusaRinne Is this up for grabs?

@martindevans
Copy link
Member

Unless @dlyz is already working on it, I think it's open to whoever moves first :)

@dlyz
Copy link
Contributor Author

dlyz commented May 7, 2024

Yeah, sorry, couldn't find time for it yet, maybe closer to the end of the week. @Neilblaze if you can do it earlier, you should definitely take it.

If we go with nullable DefragThreshold, we should probably make Seed nullable too, because 'no seed' constant is not obvious. And in LLamaContext.SetSeed too.

@Neilblaze
Copy link

Yeah, sorry, couldn't find time for it yet, maybe closer to the end of the week. @Neilblaze if you can do it earlier, you should definitely take it.

Cool I'll try my best to raise a PR asap, but meanwhile, I'm waiting for this confirmation ↓

If we go with nullable DefragThreshold, we should probably make Seed nullable too, because 'no seed' constant is not obvious. And in LLamaContext.SetSeed too.

Once @martindevans confirms, I can make the updates. Thanks!

@martindevans
Copy link
Member

martindevans commented May 7, 2024

Making seed nullable for the default is a good idea. Go for it @Neilblaze :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working good first issue Good for newcomers
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants