Generate Text Using Eagle Decoding#

Source NVIDIA/TensorRT-LLM.

 1### Generate Text Using Eagle Decoding
 2
 3from tensorrt_llm import LLM, SamplingParams
 4from tensorrt_llm.llmapi import (LLM, EagleDecodingConfig, KvCacheConfig,
 5                                 SamplingParams)
 6
 7
 8def main():
 9    # Sample prompts.
10    prompts = [
11        "Hello, my name is",
12        "The president of the United States is",
13        "The capital of France is",
14        "The future of AI is",
15    ]
16    # The end user can customize the sampling configuration with the SamplingParams class
17    sampling_params = SamplingParams(temperature=0.8, top_p=0.95)
18
19    # The end user can customize the kv cache configuration with the KVCache class
20    kv_cache_config = KvCacheConfig(enable_block_reuse=True)
21
22    llm_kwargs = {}
23
24    model = "lmsys/vicuna-7b-v1.3"
25
26    # The end user can customize the eagle decoding configuration by specifying the
27    # speculative_model, max_draft_len, num_eagle_layers, max_non_leaves_per_layer, eagle_choices
28    # greedy_sampling,posterior_threshold, use_dynamic_tree and dynamic_tree_max_topK
29    # with the EagleDecodingConfig class
30
31    speculative_config = EagleDecodingConfig(
32        speculative_model="yuhuili/EAGLE-Vicuna-7B-v1.3",
33        max_draft_len=63,
34        num_eagle_layers=4,
35        max_non_leaves_per_layer=10,
36                            eagle_choices=[[0], [0, 0], [1], [0, 1], [2], [0, 0, 0], [1, 0], [0, 2], [3], [0, 3], [4], [0, 4], [2, 0], \
37                                            [0, 5], [0, 0, 1], [5], [0, 6], [6], [0, 7], [0, 1, 0], [1, 1], [7], [0, 8], [0, 0, 2], [3, 0], \
38                                            [0, 9], [8], [9], [1, 0, 0], [0, 2, 0], [1, 2], [0, 0, 3], [4, 0], [2, 1], [0, 0, 4], [0, 0, 5], \
39                                            [0, 0, 0, 0], [0, 1, 1], [0, 0, 6], [0, 3, 0], [5, 0], [1, 3], [0, 0, 7], [0, 0, 8], [0, 0, 9], \
40                                            [6, 0], [0, 4, 0], [1, 4], [7, 0], [0, 1, 2], [2, 0, 0], [3, 1], [2, 2], [8, 0], \
41                                            [0, 5, 0], [1, 5], [1, 0, 1], [0, 2, 1], [9, 0], [0, 6, 0], [0, 0, 0, 1], [1, 6], [0, 7, 0]]
42    )
43
44    llm = LLM(model=model,
45              kv_cache_config=kv_cache_config,
46              speculative_config=speculative_config,
47              max_batch_size=1,
48              max_seq_len=1024,
49              **llm_kwargs)
50
51    outputs = llm.generate(prompts, sampling_params)
52
53    # Print the outputs.
54    for output in outputs:
55        prompt = output.prompt
56        generated_text = output.outputs[0].text
57        print(f"Prompt: {prompt!r}, Generated text: {generated_text!r}")
58
59
60if __name__ == '__main__':
61    main()