# SPDX-FileCopyrightText: Copyright (c) 2024 NVIDIA CORPORATION & AFFILIATES. All rights reserved. # SPDX-License-Identifier: Apache-2.0 # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import subprocess import sys import pytest import torch from _test_utils.examples.models import SDXL_PATH from _test_utils.examples.run_command import MODELOPT_ROOT from diffusers import DiffusionPipeline, PixArtAlphaPipeline sys.path.append(str(MODELOPT_ROOT / "examples/diffusers/cache_diffusion")) from cache_diffusion import cachify from cache_diffusion.utils import PIXART_DEFAULT_CONFIG, SDXL_DEFAULT_CONFIG def test_sdxl_cachify(): pipe = DiffusionPipeline.from_pretrained( SDXL_PATH, torch_dtype=torch.float16, use_safetensors=True, ).to("cuda") cachify.prepare(pipe, SDXL_DEFAULT_CONFIG) prompt = "A random person with a head that is made of flowers, photo by James C. Leyendecker, \ Afrofuturism, studio portrait, dynamic pose, national geographic photo, retrofuturism, biomorphicy" generator = torch.Generator(device="cuda").manual_seed(2946901) # 8 steps still exercises the step-modulo cache pattern; this is a runs-without-error smoke test. pipe(prompt=prompt, generator=generator, num_inference_steps=8).images[0] # Clear cuda memory as pytest doesnt clear it between tests del pipe torch.cuda.empty_cache() def test_pixart_cachify(tiny_pixart_path): # Fail test if apex is installed if "apex" in subprocess.check_output(["pip", "list"]).decode("utf-8"): pytest.xfail("Apex is installed, test is expected to fail") pipe = PixArtAlphaPipeline.from_pretrained(tiny_pixart_path, torch_dtype=torch.float16).to( "cuda" ) cachify.prepare(pipe, PIXART_DEFAULT_CONFIG) prompt = "a small cactus with a happy face in the Sahara desert" generator = torch.Generator(device="cuda").manual_seed(2946901) # 8 steps still exercises the step-modulo cache pattern; this is a runs-without-error smoke test. pipe( prompt=prompt, generator=generator, num_inference_steps=8, use_resolution_binning=False, # the tiny transformer's sample_size has no resolution bin ).images[0] # Clear cuda memory as pytest doesnt clear it between tests del pipe torch.cuda.empty_cache()