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

Support multiple additions to ModelStream #639

Merged
merged 1 commit into from
Feb 22, 2024

Conversation

shawnz
Copy link
Contributor

@shawnz shawnz commented Feb 18, 2024

Currently when using the streaming feature, multiple successive additions to the ModelStream object will overwrite each other. This change adds the grammars together instead to preserve each addition.

For example, consider the following code:

import guidance
import guidance.models
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig


def main():
    model_id = "HuggingFaceH4/zephyr-7b-beta"
    quantization_config = BitsAndBytesConfig(
        load_in_4bit=True,
        bnb_4bit_compute_dtype=torch.bfloat16,
        bnb_4bit_use_double_quant=True,
    )
    tokenizer = AutoTokenizer.from_pretrained(model_id)
    model = AutoModelForCausalLM.from_pretrained(
        model_id, quantization_config=quantization_config
    )

    prompt = "Who was the first president of the United States? It was "

    print("Without streaming, but with multiple additions:")
    lm1 = guidance.models.Transformers(model=model, tokenizer=tokenizer)
    lm1 += prompt
    lm1 += guidance.gen(max_tokens=3)
    print(str(lm1))

    print("\nWith streaming and single addition:")
    lm2 = guidance.models.Transformers(model=model, tokenizer=tokenizer).stream()
    lm2 += prompt + guidance.gen(max_tokens=3)
    *_, last_lm2 = lm2
    print(str(last_lm2))

    print("\nWith streaming and multiple additions:")
    lm3 = guidance.models.Transformers(model=model, tokenizer=tokenizer).stream()
    lm3 += prompt
    lm3 += guidance.gen(max_tokens=3)
    *_, last_lm3 = lm3
    print(str(last_lm3))


if __name__ == "__main__":
    main()

Prior to this change, only the first two generations will give sensical output whereas the third will output garbage.

@slundberg
Copy link
Collaborator

Thanks @shawnz ! Can't believe that bug was still there...good catch :)

@slundberg slundberg merged commit 6873870 into guidance-ai:main Feb 22, 2024
5 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