-
Notifications
You must be signed in to change notification settings - Fork 6.5k
Benchmark Autoencoder #10780
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
Closed
Closed
Benchmark Autoencoder #10780
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
ef0c447
AutoencoderKLBenchmark
hlky 4902610
csv
hlky 5351d9e
log
hlky ee9a56d
max_memory_reserved
hlky 7f37c1b
tiling
hlky 73688a2
handle oom
hlky 82a238f
handle oom
hlky 265f1f2
tilng
hlky 96449cd
device
hlky 8eeee7e
Merge remote-tracking branch 'upstream/main' into benchmark-autoencoder
hlky 6714388
benchmark_autoencoderkl_encode
hlky 46a3b43
in_channels
hlky 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,6 +4,7 @@ | |
| import torch | ||
|
|
||
| from diffusers import ( | ||
| AutoencoderKL, | ||
| AutoPipelineForImage2Image, | ||
| AutoPipelineForInpainting, | ||
| AutoPipelineForText2Image, | ||
|
|
@@ -29,6 +30,8 @@ | |
| bytes_to_giga_bytes, | ||
| flush, | ||
| generate_csv_dict, | ||
| generate_csv_dict_model, | ||
| write_list_to_csv, | ||
| write_to_csv, | ||
| ) | ||
|
|
||
|
|
@@ -169,7 +172,7 @@ def benchmark(self, args): | |
| print(f"[INFO] {self.pipe.__class__.__name__}: Running benchmark with: {vars(args)}\n") | ||
|
|
||
| time = benchmark_fn(self.run_inference, self.pipe, args) # in seconds. | ||
| memory = bytes_to_giga_bytes(torch.cuda.max_memory_allocated()) # in GBs. | ||
| memory = bytes_to_giga_bytes(torch.cuda.reset_peak_memory_stats()) # in GBs. | ||
| benchmark_info = BenchmarkInfo(time=time, memory=memory) | ||
|
|
||
| pipeline_class_name = str(self.pipe.__class__.__name__) | ||
|
|
@@ -344,3 +347,164 @@ class T2IAdapterSDXLBenchmark(T2IAdapterBenchmark): | |
|
|
||
| def __init__(self, args): | ||
| super().__init__(args) | ||
|
|
||
|
|
||
| class BaseBenchmarkTestCase: | ||
| model_class = None | ||
| pretrained_model_name_or_path = None | ||
| model_class_name = None | ||
|
|
||
| def __init__(self): | ||
| super().__init__() | ||
|
|
||
| def get_result_filepath(self, suffix): | ||
| name = ( | ||
| self.model_class_name + "_" + self.pretrained_model_name_or_path.replace("/", "_") + "_" + f"{suffix}.csv" | ||
| ) | ||
| filepath = os.path.join(BASE_PATH, name) | ||
| return filepath | ||
|
|
||
|
|
||
| class AutoencoderKLBenchmark(BaseBenchmarkTestCase): | ||
| model_class = AutoencoderKL | ||
|
|
||
| def __init__(self, pretrained_model_name_or_path, dtype, tiling, **kwargs): | ||
| super().__init__() | ||
| self.dtype = getattr(torch, dtype) | ||
| model = self.model_class.from_pretrained( | ||
| pretrained_model_name_or_path, torch_dtype=self.dtype, **kwargs | ||
| ).eval() | ||
| model = model.to("cuda") | ||
| self.tiling = False | ||
| if tiling: | ||
| model.enable_tiling() | ||
| self.tiling = True | ||
| self.model = model | ||
| self.model_class_name = str(self.model.__class__.__name__) | ||
| self.pretrained_model_name_or_path = pretrained_model_name_or_path | ||
|
|
||
| @torch.no_grad() | ||
| def run_decode(self, model, tensor): | ||
| _ = model.decode(tensor) | ||
|
|
||
| @torch.no_grad() | ||
| def _test_decode(self, **kwargs): | ||
| batch = kwargs.get("batch") | ||
| height = kwargs.get("height") | ||
| width = kwargs.get("width") | ||
|
|
||
| tensor = torch.randn( | ||
| (batch, self.model.config.latent_channels, height, width), dtype=self.dtype, device="cuda" | ||
| ) | ||
|
|
||
| try: | ||
| time = benchmark_fn(self.run_decode, self.model, tensor) | ||
| memory = bytes_to_giga_bytes(torch.cuda.max_memory_reserved()) | ||
| except torch.OutOfMemoryError: | ||
| time = "OOM" | ||
| memory = "OOM" | ||
|
|
||
| benchmark_info = BenchmarkInfo(time=time, memory=memory) | ||
| csv_dict = generate_csv_dict_model( | ||
| model_cls=self.model_class_name, | ||
| ckpt=self.pretrained_model_name_or_path, | ||
| benchmark_info=benchmark_info, | ||
| **kwargs, | ||
| ) | ||
| print(f"{self.model_class_name} decode - shape: {list(tensor.shape)}, time: {time}, memory: {memory}") | ||
| return csv_dict | ||
|
|
||
| def test_decode(self): | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not needed for the first iteration but I would consider also including |
||
| benchmark_infos = [] | ||
|
|
||
| batches = (1,) | ||
| # heights = (32, 64, 128, 256,) | ||
| widths = ( | ||
| 32, | ||
| 64, | ||
| 128, | ||
| 256, | ||
| ) | ||
| for batch in batches: | ||
| # for height in heights: | ||
| for width in widths: | ||
| benchmark_info = self._test_decode(batch=batch, height=width, width=width) | ||
| benchmark_infos.append(benchmark_info) | ||
|
|
||
| suffix = "decode" | ||
| if self.tiling: | ||
| suffix = "tiled_decode" | ||
| filepath = self.get_result_filepath(suffix) | ||
| write_list_to_csv(filepath, benchmark_infos) | ||
|
|
||
|
|
||
| class AutoencoderKLEncodeBenchmark(BaseBenchmarkTestCase): | ||
| model_class = AutoencoderKL | ||
|
|
||
| def __init__(self, pretrained_model_name_or_path, dtype, tiling, **kwargs): | ||
| super().__init__() | ||
| self.dtype = getattr(torch, dtype) | ||
| model = self.model_class.from_pretrained( | ||
| pretrained_model_name_or_path, torch_dtype=self.dtype, **kwargs | ||
| ).eval() | ||
| model = model.to("cuda") | ||
| self.tiling = False | ||
| if tiling: | ||
| model.enable_tiling() | ||
| self.tiling = True | ||
| self.model = model | ||
| self.model_class_name = str(self.model.__class__.__name__) | ||
| self.pretrained_model_name_or_path = pretrained_model_name_or_path | ||
|
|
||
| @torch.no_grad() | ||
| def run_encode(self, model, tensor): | ||
| _ = model.encode(tensor) | ||
|
|
||
| @torch.no_grad() | ||
| def _test_encode(self, **kwargs): | ||
| batch = kwargs.get("batch") | ||
| height = kwargs.get("height") | ||
| width = kwargs.get("width") | ||
|
|
||
| tensor = torch.randn( | ||
| (batch, self.model.config.in_channels, height, width), dtype=self.dtype, device="cuda" | ||
| ) | ||
|
|
||
| try: | ||
| time = benchmark_fn(self.run_encode, self.model, tensor) | ||
| memory = bytes_to_giga_bytes(torch.cuda.max_memory_reserved()) | ||
| except torch.OutOfMemoryError: | ||
| time = "OOM" | ||
| memory = "OOM" | ||
|
|
||
| benchmark_info = BenchmarkInfo(time=time, memory=memory) | ||
| csv_dict = generate_csv_dict_model( | ||
| model_cls=self.model_class_name, | ||
| ckpt=self.pretrained_model_name_or_path, | ||
| benchmark_info=benchmark_info, | ||
| **kwargs, | ||
| ) | ||
| print(f"{self.model_class_name} encode - shape: {list(tensor.shape)}, time: {time}, memory: {memory}") | ||
| return csv_dict | ||
|
|
||
| def test_encode(self): | ||
| benchmark_infos = [] | ||
|
|
||
| batches = (1,) | ||
| widths = ( | ||
| 256, | ||
| 512, | ||
| 1024, | ||
| 2048, | ||
| ) | ||
| for batch in batches: | ||
| # for height in heights: | ||
| for width in widths: | ||
| benchmark_info = self._test_encode(batch=batch, height=width, width=width) | ||
| benchmark_infos.append(benchmark_info) | ||
|
|
||
| suffix = "encode" | ||
| if self.tiling: | ||
| suffix = "tiled_encode" | ||
| filepath = self.get_result_filepath(suffix) | ||
| write_list_to_csv(filepath, benchmark_infos) | ||
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,35 @@ | ||
| import argparse | ||
| import sys | ||
|
|
||
|
|
||
| sys.path.append(".") | ||
| from base_classes import AutoencoderKLBenchmark # noqa: E402 | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| parser = argparse.ArgumentParser() | ||
| parser.add_argument( | ||
| "--pretrained_model_name_or_path", | ||
| type=str, | ||
| default="stable-diffusion-v1-5/stable-diffusion-v1-5", | ||
| ) | ||
| parser.add_argument( | ||
| "--subfolder", | ||
| type=str, | ||
| default=None, | ||
| ) | ||
| parser.add_argument( | ||
| "--dtype", | ||
| type=str, | ||
| default="float16", | ||
| ) | ||
| parser.add_argument("--tiling", action="store_true") | ||
| args = parser.parse_args() | ||
|
|
||
| benchmark = AutoencoderKLBenchmark( | ||
| pretrained_model_name_or_path=args.pretrained_model_name_or_path, | ||
| dtype=args.dtype, | ||
| tiling=args.tiling, | ||
| subfolder=args.subfolder, | ||
| ) | ||
| benchmark.test_decode() |
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,35 @@ | ||
| import argparse | ||
| import sys | ||
|
|
||
|
|
||
| sys.path.append(".") | ||
| from base_classes import AutoencoderKLEncodeBenchmark # noqa: E402 | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| parser = argparse.ArgumentParser() | ||
| parser.add_argument( | ||
| "--pretrained_model_name_or_path", | ||
| type=str, | ||
| default="stable-diffusion-v1-5/stable-diffusion-v1-5", | ||
| ) | ||
| parser.add_argument( | ||
| "--subfolder", | ||
| type=str, | ||
| default=None, | ||
| ) | ||
| parser.add_argument( | ||
| "--dtype", | ||
| type=str, | ||
| default="float16", | ||
| ) | ||
| parser.add_argument("--tiling", action="store_true") | ||
| args = parser.parse_args() | ||
|
|
||
| benchmark = AutoencoderKLEncodeBenchmark( | ||
| pretrained_model_name_or_path=args.pretrained_model_name_or_path, | ||
| dtype=args.dtype, | ||
| tiling=args.tiling, | ||
| subfolder=args.subfolder, | ||
| ) | ||
| benchmark.test_encode() |
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.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we let the users define
dummy_inputs()per model class here? And then we could let them implement their own function that needs to be benchmarked.So,
BaseBenchmarkTestCasecould then have a methodbenchmark():