Generate text with multiple LoRA adapters
Source https://github.com/NVIDIA/TensorRT-LLM/tree/main/examples/llm-api/llm_multilora.py.
1### Generate text with multiple LoRA adapters
2from huggingface_hub import snapshot_download
3
4from tensorrt_llm import LLM, BuildConfig
5from tensorrt_llm.executor import LoRARequest
6from tensorrt_llm.lora_manager import LoraConfig
7
8# Download the LoRA adapters from huggingface hub.
9lora_dir1 = snapshot_download(repo_id="snshrivas10/sft-tiny-chatbot")
10lora_dir2 = snapshot_download(
11 repo_id="givyboy/TinyLlama-1.1B-Chat-v1.0-mental-health-conversational")
12lora_dir3 = snapshot_download(repo_id="barissglc/tinyllama-tarot-v1")
13
14# Currently, we need to pass at least one lora_dir to LLM constructor via build_config.lora_config.
15# This is necessary because it requires some configuration in the lora_dir to build the engine with LoRA support.
16build_config = BuildConfig()
17build_config.lora_config = LoraConfig(lora_dir=[lora_dir1])
18llm = LLM(model="TinyLlama/TinyLlama-1.1B-Chat-v1.0",
19 enable_lora=True,
20 max_lora_rank=64,
21 build_config=build_config)
22
23# Sample prompts
24prompts = [
25 "Hello, tell me a story: ",
26 "Hello, tell me a story: ",
27 "I've noticed you seem a bit down lately. Is there anything you'd like to talk about?",
28 "I've noticed you seem a bit down lately. Is there anything you'd like to talk about?",
29 "In this reading, the Justice card represents a situation where",
30 "In this reading, the Justice card represents a situation where",
31]
32
33# At runtime, multiple LoRA adapters can be specified via lora_request; None means no LoRA used.
34for output in llm.generate(prompts,
35 lora_request=[
36 None,
37 LoRARequest("chatbot", 1, lora_dir1), None,
38 LoRARequest("mental-health", 2, lora_dir2), None,
39 LoRARequest("tarot", 3, lora_dir3)
40 ]):
41 prompt = output.prompt
42 generated_text = output.outputs[0].text
43 print(f"Prompt: {prompt!r}, Generated text: {generated_text!r}")
44
45# Got output like
46# Prompt: 'Hello, tell me a story: ', Generated text: '1. Start with a question: "What\'s your favorite color?" 2. Ask a question that leads to a story: "What\'s your'
47# Prompt: 'Hello, tell me a story: ', Generated text: '1. A person is walking down the street. 2. A person is sitting on a bench. 3. A person is reading a book.'
48# Prompt: "I've noticed you seem a bit down lately. Is there anything you'd like to talk about?", Generated text: "\n\nJASON: (smiling) No, I'm just feeling a bit overwhelmed lately. I've been trying to"
49# Prompt: "I've noticed you seem a bit down lately. Is there anything you'd like to talk about?", Generated text: "\n\nJASON: (sighs) Yeah, I've been struggling with some personal issues. I've been feeling like I'm"
50# Prompt: 'In this reading, the Justice card represents a situation where', Generated text: 'you are being asked to make a decision that will have a significant impact on your life. The card suggests that you should take the time to consider all the options'
51# Prompt: 'In this reading, the Justice card represents a situation where', Generated text: 'you are being asked to make a decision that will have a significant impact on your life. It is important to take the time to consider all the options and make'