2025-02-20 12:53:51 +00:00
|
|
|
# SPDX-License-Identifier: Apache-2.0
|
2025-06-03 11:20:17 -07:00
|
|
|
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
|
2025-02-20 12:53:51 +00:00
|
|
|
|
|
|
|
|
from argparse import Namespace
|
|
|
|
|
|
|
|
|
|
from vllm import LLM, EngineArgs
|
2025-12-11 17:03:54 -06:00
|
|
|
from vllm.attention.backends.registry import AttentionBackendEnum
|
|
|
|
|
from vllm.config import AttentionConfig
|
|
|
|
|
from vllm.platforms import current_platform
|
2025-10-26 16:33:32 +05:30
|
|
|
from vllm.utils.argparse_utils import FlexibleArgumentParser
|
2025-02-20 12:53:51 +00:00
|
|
|
|
|
|
|
|
|
2025-04-15 16:05:30 +08:00
|
|
|
def parse_args():
|
|
|
|
|
parser = FlexibleArgumentParser()
|
|
|
|
|
parser = EngineArgs.add_cli_args(parser)
|
|
|
|
|
# Set example specific arguments
|
2025-05-26 17:57:54 +01:00
|
|
|
parser.set_defaults(
|
2025-07-28 10:42:40 +08:00
|
|
|
model="BAAI/bge-reranker-v2-m3",
|
|
|
|
|
runner="pooling",
|
|
|
|
|
enforce_eager=True,
|
2025-05-26 17:57:54 +01:00
|
|
|
)
|
2025-04-15 16:05:30 +08:00
|
|
|
return parser.parse_args()
|
|
|
|
|
|
|
|
|
|
|
2025-02-20 12:53:51 +00:00
|
|
|
def main(args: Namespace):
|
2025-12-11 17:03:54 -06:00
|
|
|
if current_platform.is_rocm():
|
|
|
|
|
args.attention_config = AttentionConfig(
|
|
|
|
|
backend=AttentionBackendEnum.FLEX_ATTENTION
|
|
|
|
|
)
|
|
|
|
|
|
2025-02-20 12:53:51 +00:00
|
|
|
# Sample prompts.
|
|
|
|
|
text_1 = "What is the capital of France?"
|
|
|
|
|
texts_2 = [
|
|
|
|
|
"The capital of Brazil is Brasilia.",
|
|
|
|
|
"The capital of France is Paris.",
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
# Create an LLM.
|
2025-07-28 10:42:40 +08:00
|
|
|
# You should pass runner="pooling" for cross-encoder models
|
2025-07-21 19:18:33 +08:00
|
|
|
llm = LLM(**vars(args))
|
2025-02-20 12:53:51 +00:00
|
|
|
|
|
|
|
|
# Generate scores. The output is a list of ScoringRequestOutputs.
|
2025-07-21 19:18:33 +08:00
|
|
|
outputs = llm.score(text_1, texts_2)
|
2025-02-20 12:53:51 +00:00
|
|
|
|
|
|
|
|
# Print the outputs.
|
2025-03-26 18:12:47 +08:00
|
|
|
print("\nGenerated Outputs:\n" + "-" * 60)
|
2025-02-20 12:53:51 +00:00
|
|
|
for text_2, output in zip(texts_2, outputs):
|
|
|
|
|
score = output.outputs.score
|
2025-03-26 18:12:47 +08:00
|
|
|
print(f"Pair: {[text_1, text_2]!r} \nScore: {score}")
|
|
|
|
|
print("-" * 60)
|
2025-02-20 12:53:51 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2025-04-15 16:05:30 +08:00
|
|
|
args = parse_args()
|
2025-02-20 12:53:51 +00:00
|
|
|
main(args)
|