Offline Inference Encoder Decoder
Source vllm-project/vllm.
1'''2Demonstrate prompting of text-to-text3encoder/decoder models, specifically BART4'''56from vllm import LLM, SamplingParams7from vllm.inputs import (ExplicitEncoderDecoderPrompt, TextPrompt,8 TokensPrompt, zip_enc_dec_prompts)910dtype = "float"1112# Create a BART encoder/decoder model instance13llm = LLM(14 model="facebook/bart-large-cnn",15 dtype=dtype,16)1718# Get BART tokenizer19tokenizer = llm.llm_engine.get_tokenizer_group()2021# Test prompts22#23# This section shows all of the valid ways to prompt an24# encoder/decoder model.25#26# - Helpers for building prompts27text_prompt_raw = "Hello, my name is"28text_prompt = TextPrompt(prompt="The president of the United States is")29tokens_prompt = TokensPrompt(prompt_token_ids=tokenizer.encode(30 prompt="The capital of France is"))31# - Pass a single prompt to encoder/decoder model32# (implicitly encoder input prompt);33# decoder input prompt is assumed to be None3435single_text_prompt_raw = text_prompt_raw # Pass a string directly36single_text_prompt = text_prompt # Pass a TextPrompt37single_tokens_prompt = tokens_prompt # Pass a TokensPrompt3839# - Pass explicit encoder and decoder input prompts within one data structure.40# Encoder and decoder prompts can both independently be text or tokens, with41# no requirement that they be the same prompt type. Some example prompt-type42# combinations are shown below, note that these are not exhaustive.4344enc_dec_prompt1 = ExplicitEncoderDecoderPrompt(45 # Pass encoder prompt string directly, &46 # pass decoder prompt tokens47 encoder_prompt=single_text_prompt_raw,48 decoder_prompt=single_tokens_prompt,49)50enc_dec_prompt2 = ExplicitEncoderDecoderPrompt(51 # Pass TextPrompt to encoder, and52 # pass decoder prompt string directly53 encoder_prompt=single_text_prompt,54 decoder_prompt=single_text_prompt_raw,55)56enc_dec_prompt3 = ExplicitEncoderDecoderPrompt(57 # Pass encoder prompt tokens directly, and58 # pass TextPrompt to decoder59 encoder_prompt=single_tokens_prompt,60 decoder_prompt=single_text_prompt,61)6263# - Finally, here's a useful helper function for zipping encoder and64# decoder prompts together into a list of ExplicitEncoderDecoderPrompt65# instances66zipped_prompt_list = zip_enc_dec_prompts(67 ['An encoder prompt', 'Another encoder prompt'],68 ['A decoder prompt', 'Another decoder prompt'])6970# - Let's put all of the above example prompts together into one list71# which we will pass to the encoder/decoder LLM.72prompts = [73 single_text_prompt_raw, single_text_prompt, single_tokens_prompt,74 enc_dec_prompt1, enc_dec_prompt2, enc_dec_prompt375] + zipped_prompt_list7677print(prompts)7879# Create a sampling params object.80sampling_params = SamplingParams(81 temperature=0,82 top_p=1.0,83 min_tokens=0,84 max_tokens=20,85)8687# Generate output tokens from the prompts. The output is a list of88# RequestOutput objects that contain the prompt, generated89# text, and other information.90outputs = llm.generate(prompts, sampling_params)9192# Print the outputs.93for output in outputs:94 prompt = output.prompt95 encoder_prompt = output.encoder_prompt96 generated_text = output.outputs[0].text97 print(f"Encoder prompt: {encoder_prompt!r}, "98 f"Decoder prompt: {prompt!r}, "99 f"Generated text: {generated_text!r}")