Skip to content

Commit

Permalink
[CI/Build] vLLM cache directory for images (vllm-project#6444)
Browse files Browse the repository at this point in the history
  • Loading branch information
DarkLight1337 authored Jul 16, 2024
1 parent 37d7766 commit d970115
Show file tree
Hide file tree
Showing 13 changed files with 123 additions and 140 deletions.
14 changes: 0 additions & 14 deletions .buildkite/download-images.sh

This file was deleted.

4 changes: 0 additions & 4 deletions .buildkite/test-pipeline.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ steps:
fast_check_only: true
commands:
- pytest -v -s async_engine # Async Engine
- bash ../.buildkite/download-images.sh # Inputs
- pytest -v -s test_inputs.py
- pytest -v -s multimodal
- pytest -v -s test_utils.py # Utils
Expand Down Expand Up @@ -82,7 +81,6 @@ steps:
working_dir: "/vllm-workspace/tests"
num_gpus: 2
commands:
- bash ../.buildkite/download-images.sh
- VLLM_TEST_SAME_HOST=1 torchrun --nproc-per-node=4 distributed/test_same_node.py
- TEST_DIST_MODEL=facebook/opt-125m DISTRIBUTED_EXECUTOR_BACKEND=ray pytest -v -s distributed/test_basic_distributed_correctness.py
- TEST_DIST_MODEL=meta-llama/Llama-2-7b-hf DISTRIBUTED_EXECUTOR_BACKEND=ray pytest -v -s distributed/test_basic_distributed_correctness.py
Expand Down Expand Up @@ -155,7 +153,6 @@ steps:
- label: Inputs Test
#mirror_hardwares: [amd]
commands:
- bash ../.buildkite/download-images.sh
- pytest -v -s test_inputs.py
- pytest -v -s multimodal

Expand All @@ -175,7 +172,6 @@ steps:
- label: Vision Language Models Test
mirror_hardwares: [amd]
commands:
- bash ../.buildkite/download-images.sh
- pytest -v -s models -m vlm

- label: Prefix Caching Test
Expand Down
33 changes: 3 additions & 30 deletions examples/llava_example.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,13 @@
import os
import subprocess

from PIL import Image

from vllm import LLM

# The assets are located at `s3://air-example-data-2/vllm_opensource_llava/`.
# You can use `.buildkite/download-images.sh` to download them
from vllm.assets.image import ImageAsset


def run_llava():
llm = LLM(model="llava-hf/llava-1.5-7b-hf")

prompt = "USER: <image>\nWhat is the content of this image?\nASSISTANT:"

image = Image.open("images/stop_sign.jpg")
image = ImageAsset("stop_sign").pil_image

outputs = llm.generate({
"prompt": prompt,
Expand All @@ -28,25 +21,5 @@ def run_llava():
print(generated_text)


def main():
run_llava()


if __name__ == "__main__":
# Download from s3
s3_bucket_path = "s3://air-example-data-2/vllm_opensource_llava/"
local_directory = "images"

# Make sure the local directory exists or create it
os.makedirs(local_directory, exist_ok=True)

# Use AWS CLI to sync the directory, assume anonymous access
subprocess.check_call([
"aws",
"s3",
"sync",
s3_bucket_path,
local_directory,
"--no-sign-request",
])
main()
run_llava()
33 changes: 3 additions & 30 deletions examples/paligemma_example.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,13 @@
import os
import subprocess

from PIL import Image

from vllm import LLM

# The assets are located at `s3://air-example-data-2/vllm_opensource_llava/`.
# You can use `.buildkite/download-images.sh` to download them
from vllm.assets.image import ImageAsset


def run_paligemma():
llm = LLM(model="google/paligemma-3b-mix-224")

prompt = "caption es"

image = Image.open("images/stop_sign.jpg")
image = ImageAsset("stop_sign").pil_image

outputs = llm.generate({
"prompt": prompt,
Expand All @@ -28,25 +21,5 @@ def run_paligemma():
print(generated_text)


def main():
run_paligemma()


if __name__ == "__main__":
# Download from s3
s3_bucket_path = "s3://air-example-data-2/vllm_opensource_llava/"
local_directory = "images"

# Make sure the local directory exists or create it
os.makedirs(local_directory, exist_ok=True)

# Use AWS CLI to sync the directory, assume anonymous access
subprocess.check_call([
"aws",
"s3",
"sync",
s3_bucket_path,
local_directory,
"--no-sign-request",
])
main()
run_paligemma()
26 changes: 2 additions & 24 deletions examples/phi3v_example.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
import os
import subprocess

from PIL import Image

from vllm import LLM, SamplingParams

# The assets are located at `s3://air-example-data-2/vllm_opensource_llava/`.
# You can use `.buildkite/download-images.sh` to download them
from vllm.assets.image import ImageAsset


def run_phi3v():
Expand All @@ -24,7 +17,7 @@ def run_phi3v():
max_num_seqs=5,
)

image = Image.open("images/cherry_blossom.jpg")
image = ImageAsset("cherry_blossom").pil_image

# single-image prompt
prompt = "<|user|>\n<|image_1|>\nWhat is the season?<|end|>\n<|assistant|>\n" # noqa: E501
Expand All @@ -44,19 +37,4 @@ def run_phi3v():


if __name__ == "__main__":
s3_bucket_path = "s3://air-example-data-2/vllm_opensource_llava/"
local_directory = "images"

# Make sure the local directory exists or create it
os.makedirs(local_directory, exist_ok=True)

# Use AWS CLI to sync the directory, assume anonymous access
subprocess.check_call([
"aws",
"s3",
"sync",
s3_bucket_path,
local_directory,
"--no-sign-request",
])
run_phi3v()
25 changes: 2 additions & 23 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,7 @@
import os
import sys
from collections import UserList
from dataclasses import dataclass
from functools import cached_property
from pathlib import Path
from typing import (Any, Dict, List, Literal, Optional, Tuple, TypedDict,
TypeVar)
from typing import Any, Dict, List, Optional, Tuple, TypedDict, TypeVar

import pytest
import torch
Expand All @@ -18,12 +14,12 @@
AutoTokenizer, BatchEncoding)

from vllm import LLM, SamplingParams
from vllm.assets.image import ImageAsset
from vllm.config import TokenizerPoolConfig
from vllm.distributed import (destroy_distributed_environment,
destroy_model_parallel)
from vllm.inputs import TextPrompt
from vllm.logger import init_logger
from vllm.multimodal.utils import fetch_image
from vllm.sequence import SampleLogprobs
from vllm.utils import cuda_device_count_stateless, is_cpu

Expand All @@ -33,30 +29,13 @@
_TEST_PROMPTS = [os.path.join(_TEST_DIR, "prompts", "example.txt")]
_LONG_PROMPTS = [os.path.join(_TEST_DIR, "prompts", "summary.txt")]

_IMAGE_DIR = Path(_TEST_DIR) / "images"
"""You can use `.buildkite/download-images.sh` to download the assets."""


def _read_prompts(filename: str) -> List[str]:
with open(filename, "r") as f:
prompts = f.readlines()
return prompts


@dataclass(frozen=True)
class ImageAsset:
name: Literal["stop_sign", "cherry_blossom", "boardwalk"]

@cached_property
def pil_image(self) -> Image.Image:
if self.name == "boardwalk":
return fetch_image(
"https://upload.wikimedia.org/wikipedia/commons/thumb/d/dd/Gfp-wisconsin-madison-the-nature-boardwalk.jpg/2560px-Gfp-wisconsin-madison-the-nature-boardwalk.jpg"
)

return Image.open(_IMAGE_DIR / f"{self.name}.jpg")


class _ImageAssetPrompts(TypedDict):
stop_sign: str
cherry_blossom: str
Expand Down
Empty file added vllm/assets/__init__.py
Empty file.
11 changes: 11 additions & 0 deletions vllm/assets/base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from pathlib import Path

import vllm.envs as envs


def get_cache_dir():
"""Get the path to the cache for storing downloaded assets."""
path = Path(envs.VLLM_ASSETS_CACHE)
path.mkdir(parents=True, exist_ok=True)

return path
47 changes: 47 additions & 0 deletions vllm/assets/image.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import shutil
from dataclasses import dataclass
from functools import cached_property, lru_cache
from typing import Literal

import requests
from PIL import Image

from vllm.multimodal.utils import fetch_image

from .base import get_cache_dir


@lru_cache
def get_air_example_data_2_asset(filename: str) -> Image.Image:
"""
Download and open an image from
``s3://air-example-data-2/vllm_opensource_llava/``.
"""
image_directory = get_cache_dir() / "air-example-data-2"
image_directory.mkdir(parents=True, exist_ok=True)

image_path = image_directory / filename
if not image_path.exists():
base_url = "https://air-example-data-2.s3.us-west-2.amazonaws.com/vllm_opensource_llava"

with requests.get(f"{base_url}/{filename}", stream=True) as response:
response.raise_for_status()

with image_path.open("wb") as f:
shutil.copyfileobj(response.raw, f)

return Image.open(image_path)


@dataclass(frozen=True)
class ImageAsset:
name: Literal["stop_sign", "cherry_blossom", "boardwalk"]

@cached_property
def pil_image(self) -> Image.Image:
if self.name == "boardwalk":
return fetch_image(
"https://upload.wikimedia.org/wikipedia/commons/thumb/d/dd/Gfp-wisconsin-madison-the-nature-boardwalk.jpg/2560px-Gfp-wisconsin-madison-the-nature-boardwalk.jpg"
)

return get_air_example_data_2_asset(f"{self.name}.jpg")
Original file line number Diff line number Diff line change
Expand Up @@ -189,10 +189,10 @@ def gpu_p2p_access_check(src: int, tgt: int) -> bool:
cuda_visible_devices = envs.CUDA_VISIBLE_DEVICES
if cuda_visible_devices is None:
cuda_visible_devices = ",".join(str(i) for i in range(num_dev))
VLLM_CONFIG_ROOT = envs.VLLM_CONFIG_ROOT
path = os.path.expanduser(
f"{VLLM_CONFIG_ROOT}/vllm/gpu_p2p_access_cache_for_{cuda_visible_devices}.json"
)

path = os.path.join(
envs.VLLM_CACHE_ROOT,
f"gpu_p2p_access_cache_for_{cuda_visible_devices}.json")
os.makedirs(os.path.dirname(path), exist_ok=True)
from vllm.distributed.parallel_state import get_world_group
if ((not is_distributed or get_world_group().local_rank == 0)
Expand Down
Loading

0 comments on commit d970115

Please sign in to comment.