-
Notifications
You must be signed in to change notification settings - Fork 234
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support synthetic image generation in GenAI-Perf (#754)
* support synthetic image generation for VLM model * add test * integrate sythetic image generator into LlmInputs * add source images for synthetic image data * use abs to get positive int
- Loading branch information
Showing
12 changed files
with
225 additions
and
131 deletions.
There are no files selected for viewing
This file contains 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
Binary file added
BIN
+147 KB
src/c++/perf_analyzer/genai-perf/genai_perf/llm_inputs/source_images/dlss.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+149 KB
src/c++/perf_analyzer/genai-perf/genai_perf/llm_inputs/source_images/h100.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+99.3 KB
src/c++/perf_analyzer/genai-perf/genai_perf/llm_inputs/source_images/h200.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+107 KB
src/c++/perf_analyzer/genai-perf/genai_perf/llm_inputs/source_images/jensen.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
111 changes: 64 additions & 47 deletions
111
src/c++/perf_analyzer/genai-perf/genai_perf/llm_inputs/synthetic_image_generator.py
This file contains 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,62 +1,79 @@ | ||
import base64 | ||
# Copyright 2024, NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
# | ||
# Redistribution and use in source and binary forms, with or without | ||
# modification, are permitted provided that the following conditions | ||
# are met: | ||
# * Redistributions of source code must retain the above copyright | ||
# notice, this list of conditions and the following disclaimer. | ||
# * Redistributions in binary form must reproduce the above copyright | ||
# notice, this list of conditions and the following disclaimer in the | ||
# documentation and/or other materials provided with the distribution. | ||
# * Neither the name of NVIDIA CORPORATION nor the names of its | ||
# contributors may be used to endorse or promote products derived | ||
# from this software without specific prior written permission. | ||
# | ||
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY | ||
# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | ||
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR | ||
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | ||
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | ||
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR | ||
# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY | ||
# OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
|
||
import glob | ||
import random | ||
from enum import Enum, auto | ||
from io import BytesIO | ||
from typing import Optional, cast | ||
from pathlib import Path | ||
|
||
import numpy as np | ||
from genai_perf import utils | ||
from PIL import Image | ||
|
||
|
||
class ImageFormat(Enum): | ||
JPEG = auto() | ||
PNG = auto() | ||
JPEG = auto() | ||
|
||
|
||
class SyntheticImageGenerator: | ||
def __init__( | ||
self, | ||
"""A simple synthetic image generator that generates multiple synthetic | ||
images from the source images. | ||
""" | ||
|
||
@classmethod | ||
def create_synthetic_image( | ||
cls, | ||
image_width_mean: int, | ||
image_height_mean: int, | ||
image_width_stddev: int, | ||
image_height_mean: int, | ||
image_height_stddev: int, | ||
image_format: ImageFormat = ImageFormat.PNG, | ||
rng: Optional[np.random.Generator] = None, | ||
): | ||
self._image_width_mean = image_width_mean | ||
self._image_height_mean = image_height_mean | ||
self._image_width_stddev = image_width_stddev | ||
self._image_height_stddev = image_height_stddev | ||
self.image_format = image_format | ||
self.rng = cast(np.random.Generator, rng or np.random.default_rng()) | ||
|
||
def __iter__(self): | ||
return self | ||
|
||
def _sample_random_positive_integer(self, mean: int, stddev: int) -> int: | ||
while True: | ||
n = int(self.rng.normal(mean, stddev)) | ||
if n > 0: | ||
break | ||
return n | ||
|
||
def _get_next_image(self): | ||
width = self._sample_random_positive_integer( | ||
self._image_width_mean, self._image_width_stddev | ||
image_format: ImageFormat, | ||
) -> str: | ||
"""Generate base64 encoded synthetic image using the source images.""" | ||
width = cls._sample_random_positive_integer( | ||
image_width_mean, image_width_stddev | ||
) | ||
height = self._sample_random_positive_integer( | ||
self._image_height_mean, self._image_height_stddev | ||
height = cls._sample_random_positive_integer( | ||
image_height_mean, image_height_stddev | ||
) | ||
shape = width, height, 3 | ||
noise = self.rng.integers(0, 256, shape, dtype=np.uint8) | ||
return Image.fromarray(noise) | ||
|
||
def _encode(self, image): | ||
buffered = BytesIO() | ||
image.save(buffered, format=self.image_format.name) | ||
data = base64.b64encode(buffered.getvalue()).decode("utf-8") | ||
return f"data:image/{self.image_format.name.lower()};base64,{data}" | ||
|
||
def __next__(self) -> str: | ||
image = self._get_next_image() | ||
base64_string = self._encode(image) | ||
return base64_string | ||
|
||
image = cls._sample_source_image() | ||
image = image.resize(size=(width, height)) | ||
|
||
img_base64 = utils.encode_image(image, image_format.name) | ||
return f"data:image/{image_format.name.lower()};base64,{img_base64}" | ||
|
||
@classmethod | ||
def _sample_source_image(cls): | ||
"""Sample one image among the source images.""" | ||
filepath = Path(__file__).parent.resolve() / "source_images" / "*" | ||
filenames = glob.glob(str(filepath)) | ||
return Image.open(random.choice(filenames)) | ||
|
||
@classmethod | ||
def _sample_random_positive_integer(cls, mean: int, stddev: int) -> int: | ||
n = int(abs(random.gauss(mean, stddev))) | ||
return n if n != 0 else 1 # avoid zero |
This file contains 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 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 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 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 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.