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

[Request] Constrained Generation #26

Closed
scottwey opened this issue Feb 2, 2024 · 4 comments
Closed

[Request] Constrained Generation #26

scottwey opened this issue Feb 2, 2024 · 4 comments
Labels
enhancement New feature or request

Comments

@scottwey
Copy link

scottwey commented Feb 2, 2024

Given the current structure of candle-vllm, how difficult would it be to add constrained generation, similar to lm-format-enforcer?

I'm happy to help here however I can.

@EricLBuehler
Copy link
Owner

Hi @scottwey, thank you for bringing that up. Implementing methods which would affect sampling such as Constrained Generation should be doable. All one would need to do is inject code at the correct location to implement the Constrained Generation: perhaps an elegant Fn pointer would be best. This is the spot that I would direct you to:

let logits = self.pipeline.forward(
tokens,
positions,
Some(&*self.cache_engine.get_kv_cache()),
metadata,
)?;
let result = self.pipeline.sample(logits, &sampling_params, &seqs)?;
for (result, (_, seq)) in zip(result, seqs) {
match result {
Either::Left(logprobs) => {
seq.deref_mut().add_token(logprobs);
}
Either::Right(finish_reason) => {
seq.deref_mut().set_finish_reason(finish_reason)
}
}
}

As you can see, I sample the logits and then add the result to the SequenceGroup. I am not familiar with the implementation of Constrained Generation, but after reading their README, I could imagine that you would add the implementation in this region.

Please let me know if you would be interested in implementing this.

@EricLBuehler EricLBuehler added the enhancement New feature or request label Feb 2, 2024
@scottwey
Copy link
Author

@EricLBuehler I will try to take a crack at this as soon as I get some time. Thank you for the guidance. :)

@EricLBuehler
Copy link
Owner

@scottwey, I am currently working on mistral.rs. It has a simpler sampling API and overall file structure, so perhaps you could take a look there? Feel free to raise an issue for further guidance.

@EricLBuehler
Copy link
Owner

Please see EricLBuehler/mistral.rs#59 where we are developing model grammar support. If you have any questions, please feel free to reopen!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants