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

Custom Sampler Stages #961

Open
wants to merge 6 commits into
base: master
Choose a base branch
from

Conversation

martindevans
Copy link
Member

Added support for custom sampler stages, written entirely in safe C#. These stages can be added to a sampler pipeline just like any other. See the example in LLama.Examples\Examples\CustomSampler.cs for an example of a custom pipeline using a custom stage.

The internals here are fairly complex, because we need to keep some manually managed memory (so we can have persistent pointers to it) and also make sure the GC doesn't collect things. The overall flow is as follows:

  • A custom sampler is written by the user implementing ICustomSampler
  • This is added to a pipeline with AddCustom
  • A CustomSamplerHandle is allocated, to own all of the various bits of memory
  • The CustomSamplerHandle holds a GCHandle which keeps itself alive.
  • CustomSamplerHandle also allocates some memory to hold some native structures (_samplerNativePtr and _samplerNativeInterfacePtr) and passes pointers across to the native side.
  • When the native side calls free it will destroy the GCHandle keeping itself alive, free the native memory, and finally calls ICustomSampler.Free.

…he `LLamaSamplerNative*`, not the `GCHandle` value).

 - Properly typed as much of it as possible (using delegate pointers)
 - Replaced pointers with `ref` where possible, making much of the API safe
 - Created an example for custom sampling (removed test code from `DefaultSamplingPipeline`)
LLama.Examples/Examples/CustomSampler.cs Show resolved Hide resolved
LLama/Native/SafeLLamaSamplerHandle.cs Outdated Show resolved Hide resolved
…pler`

 - Improved documentation on `Apply` related to setting the `Sorted` flag
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