main
py 61 lines 1.82 KB
Raw
1 # SPDX-License-Identifier: Apache-2.0
2 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project
3
4 """Example offline usage of sequence 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="Skywork/Skywork-Reward-V2-Qwen3-0.6B",
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 # Use pooling_task="classify" for sequence reward models.
48 outputs = llm.encode(prompts, pooling_task="classify")
49
50 # Print the outputs.
51 print("\nGenerated Outputs:\n" + "-" * 60)
52 for prompt, output in zip(prompts, outputs):
53 rewards = output.outputs.data
54 print(f"Prompt: {prompt!r}")
55 print_embeddings(rewards.tolist(), prefix="Reward")
56 print("-" * 60)
57
58
59 if __name__ == "__main__":
60 args = parse_args()
61 main(args)