-
Notifications
You must be signed in to change notification settings - Fork 192
feat: improve offline inference interface and fix several tp and vlm bugs. #968
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
RobbieLeung
merged 3 commits into
jd-opensource:main
from
weizhehuang0827:offline_rebase_main
Mar 17, 2026
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| # python examples/beam_search.py --model='/path/models/Qwen2-7B-Instruct' --devices='npu:0' | ||
| # python beam_search.py --model='/path/models/Qwen2-7B-Instruct' --devices='npu:0,npu:1' | ||
|
|
||
| from xllm import ArgumentParser, BeamSearchParams, LLM | ||
|
|
||
| # Create an LLM. | ||
| parser = ArgumentParser() | ||
| llm = LLM(**vars(parser.parse_args())) | ||
|
|
||
| beam_search_params = BeamSearchParams( | ||
| beam_width=2, | ||
| max_tokens=20, | ||
| ) | ||
|
|
||
| outputs = llm.beam_search( | ||
| [ | ||
| {"prompt": "Hello, my name is "}, | ||
| {"prompt": "The president of the United States is "}, | ||
| {"prompt": "The capital of France is "}, | ||
| {"prompt": "The future of AI is "} | ||
| ], | ||
| params=beam_search_params, | ||
| ) | ||
|
|
||
| for output in outputs: | ||
| generated_text = output.sequences[0].text | ||
| print(f"Generated text: {generated_text!r}") | ||
|
|
||
| llm.finish() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,61 +1,75 @@ | ||
| # python generate_vlm.py --model /path/to/Qwen2.5-VL-7B-Instruct/ --disable_prefix_cache --disable_chunked_prefill --max_seqs_per_batch 4 --devices='npu:0' --enable_shm | ||
|
|
||
| from xllm import ArgumentParser, SamplingParams | ||
| from xllm import LLM | ||
| # from xllm import VLM | ||
| import base64 | ||
| import os | ||
| import signal | ||
|
|
||
| from xllm import ArgumentParser, VLM, RequestParams, MMType, MMData | ||
| def encode_image_from_file(file_path: str) -> str: | ||
| if not os.path.exists(file_path): | ||
| raise FileNotFoundError(f"not found image: {file_path}") | ||
| with open(file_path, "rb") as image_file: | ||
| result = base64.b64encode(image_file.read()).decode("utf-8") | ||
| return result | ||
|
|
||
| from PIL import Image | ||
| from transformers import AutoImageProcessor | ||
|
|
||
| # Create an VLM. | ||
| parser = ArgumentParser() | ||
| args = parser.parse_args() | ||
|
|
||
| vlm = VLM(**vars(args)) | ||
| processor = AutoImageProcessor.from_pretrained(args.model, trust_remote_code=True) | ||
|
|
||
| questions = ["简单介绍下图片"] | ||
| prompts = [ | ||
| ( | ||
| "<|im_start|>system\nYou are a helpful assistant.<|im_end|>\n" | ||
| "<|im_start|>user\n<|vision_start|><|image_pad|><|vision_end|>" | ||
| f"{question}<|im_end|>\n" | ||
| "<|im_start|>assistant\n" | ||
| ) | ||
| for question in questions | ||
| # vlm = VLM(**vars(args)) | ||
| vlm = LLM(**vars(args)) | ||
|
|
||
|
|
||
| image_1 = "./images/3.jpg" | ||
| image_2 = "./images/4.jpg" | ||
|
|
||
| # image_base64_1 = encode_image_from_file(image_1) | ||
| # image_base64_2 = encode_image_from_file(image_2) | ||
|
|
||
| # image_1 = f"data:image/jpeg;base64,{image_base64_1}" | ||
| # image_2 = f"data:image/jpeg;base64,{image_base64_2}" | ||
|
|
||
| requests = [ | ||
| { | ||
| "prompt": ( | ||
| "<|im_start|>system\nYou are a helpful assistant.<|im_end|>\n" | ||
| "<|im_start|>user\n" | ||
| "<|vision_start|><|image_pad|><|vision_end|>" | ||
| "请描述这张图片。<|im_end|>\n" | ||
| "<|im_start|>assistant\n" | ||
| ), | ||
| "multi_modal_data": { | ||
| "image": image_1, | ||
| }, | ||
| }, | ||
| { | ||
| "prompt": ( | ||
| "<|im_start|>system\nYou are a helpful assistant.<|im_end|>\n" | ||
| "<|im_start|>user\n" | ||
| "<|vision_start|><|image_pad|><|vision_end|>" | ||
| "<|vision_start|><|image_pad|><|vision_end|>" | ||
| "请对比这两张图片的主要区别。<|im_end|>\n" | ||
| "<|im_start|>assistant\n" | ||
| ), | ||
| "multi_modal_data": { | ||
| "image": [image_1, image_2], | ||
| }, | ||
| }, | ||
| ] | ||
|
|
||
| paths = ["00307664d4ce393b.png"] | ||
| images = [] | ||
| for path in paths: | ||
| images.append(Image.open(path).convert("RGB")) | ||
|
|
||
| multi_modal_datas = [] | ||
| for idx in range(len(images)): | ||
| print(f"Processing image: {paths[idx]}") | ||
| image = images[idx] | ||
|
|
||
| data = processor.preprocess([image], return_tensors="pt").data | ||
| mm_data = { | ||
| "pixel_values": data['pixel_values'], | ||
| "image_grid_thw": data['image_grid_thw'], | ||
| } | ||
| multi_modal_datas.append(MMData(MMType.IMAGE, mm_data)) | ||
| sampling_params = SamplingParams( | ||
| temperature=0.8, | ||
| top_p=0.95, | ||
| max_tokens=50, | ||
| ) | ||
|
|
||
|
|
||
| # Create a reqeust params, include sampling params | ||
| request_params = RequestParams() | ||
| request_params.temperature = 0 | ||
| request_params.max_tokens = 1024 | ||
|
|
||
| outputs = vlm.generate(prompts, multi_modal_datas, request_params, True) | ||
| outputs = vlm.generate( | ||
| requests, | ||
| sampling_params=sampling_params | ||
| ) | ||
|
|
||
| for output in outputs: | ||
| prompt = output.prompt | ||
| generated_text = output.outputs[0].text | ||
| print(f"Prompt: {prompt!r}, Generated text: {generated_text!r}") | ||
|
|
||
| vlm.finish() | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.