aphrodite-engine icon indicating copy to clipboard operation
aphrodite-engine copied to clipboard

feat: Classifer-Free Guidance (take 2)

Open AlpinDale opened this issue 1 year ago • 1 comments

Redoing #651

FIX #36

Example usage:

from typing import List
from aphrodite import LLM, SamplingParams
from aphrodite.inputs import PromptInputs

llm = LLM(
    model="NousResearch/Meta-Llama-3.1-8B-Instruct",
    use_v2_block_manager=True,
    cfg_model="NousResearch/Meta-Llama-3.1-8B-Instruct"
)

prompt_pairs = [
    {
        "prompt": "Hello, my name is",
        "negative_prompt": "I am uncertain and confused about who I am"
    },
    {
        "prompt": "The president of the United States is",
        "negative_prompt": "I don't know anything about US politics or leadership"
    },
]

tokenizer = llm.get_tokenizer()

inputs: List[PromptInputs] = [
    {
        "prompt_token_ids": tokenizer.encode(text=pair["prompt"]),
        "negative_prompt_token_ids": tokenizer.encode(text=pair["negative_prompt"])
    }
    for pair in prompt_pairs
]

sampling_params = SamplingParams(guidance_scale=5.0)
outputs = llm.generate(inputs, sampling_params)

TODO:

  • [ ] See if we can skip the double forward pass for the model
  • [ ] Pipe into OpenAI API

AlpinDale avatar Nov 25 '24 18:11 AlpinDale

It works, but will fail due to changed logit shapes when running without CFG.

AlpinDale avatar Nov 25 '24 22:11 AlpinDale