main
py 60 lines 1.75 KB
Raw
1 # SPDX-License-Identifier: Apache-2.0
2 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project
3
4 """Example offline usage of token reward models.
5
6 The key distinction between sequence classification and token classification
7 lies in their output granularity: sequence classification produces a single
8 result for an entire input sequence, whereas token classification yields a
9 result for each individual token within the sequence.
10 """
11
12 from argparse import Namespace
13
14 from vllm import LLM, EngineArgs
15 from vllm.utils.argparse_utils import FlexibleArgumentParser
16 from vllm.utils.print_utils import print_embeddings
17
18
19 def parse_args():
20 parser = FlexibleArgumentParser()
21 parser = EngineArgs.add_cli_args(parser)
22 # Set example specific arguments
23 parser.set_defaults(
24 model="internlm/internlm2-1_8b-reward",
25 runner="pooling",
26 enforce_eager=True,
27 max_model_len=1024,
28 trust_remote_code=True,
29 )
30 return parser.parse_args()
31
32
33 def main(args: Namespace):
34 # Sample prompts.
35 prompts = [
36 "Hello, my name is",
37 "The president of the United States is",
38 "The capital of France is",
39 "The future of AI is",
40 ]
41
42 # Create an LLM.
43 # You should pass runner="pooling" for reward models
44 llm = LLM(**vars(args))
45
46 # Generate rewards. The output is a list of PoolingRequestOutput.
47 outputs = llm.encode(prompts, pooling_task="token_classify")
48
49 # Print the outputs.
50 print("\nGenerated Outputs:\n" + "-" * 60)
51 for prompt, output in zip(prompts, outputs):
52 rewards = output.outputs.data
53 print(f"Prompt: {prompt!r}")
54 print_embeddings(rewards.tolist(), prefix="Reward")
55 print("-" * 60)
56
57
58 if __name__ == "__main__":
59 args = parse_args()
60 main(args)