Skip to content
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

Porting Stable Video Diffusion ControNet to HPU #1037

Merged
merged 3 commits into from
Oct 3, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Merge remote-tracking branch 'optimum-habana/main' into port_svd_cn
  • Loading branch information
regisss committed Oct 3, 2024
commit 3ca2e39163b1546489c58bbfb24b4f19b99d74db
8 changes: 6 additions & 2 deletions examples/stable-diffusion/image_to_video_generation.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,8 +228,12 @@ def main():
from optimum.habana.diffusers import GaudiStableVideoDiffusionControlNetPipeline
from optimum.habana.diffusers.models import ControlNetSDVModel, UNetSpatioTemporalConditionControlNetModel

controlnet = ControlNetSDVModel.from_pretrained(args.controlnet_model_name_or_path, subfolder="controlnet", **kwargs)
unet = UNetSpatioTemporalConditionControlNetModel.from_pretrained(args.model_name_or_path, subfolder="unet", **kwargs)
controlnet = ControlNetSDVModel.from_pretrained(
args.controlnet_model_name_or_path, subfolder="controlnet", **kwargs
)
unet = UNetSpatioTemporalConditionControlNetModel.from_pretrained(
args.model_name_or_path, subfolder="unet", **kwargs
)
pipeline = GaudiStableVideoDiffusionControlNetPipeline.from_pretrained(
args.model_name_or_path, controlnet=controlnet, unet=unet, **kwargs
)
Expand Down
3 changes: 1 addition & 2 deletions optimum/habana/diffusers/models/controlnet_sdv.py
Original file line number Diff line number Diff line change
Expand Up @@ -551,8 +551,7 @@ def from_unet(
return controlnet

# Copied from diffusers.models.unet_2d_condition.UNet2DConditionModel.set_attn_processor
def set_attn_processor(
self, processor: Union[AttentionProcessor, Dict[str, AttentionProcessor]]):
def set_attn_processor(self, processor: Union[AttentionProcessor, Dict[str, AttentionProcessor]]):
r"""
Sets the attention processor to use to compute attention.

Expand Down
53 changes: 41 additions & 12 deletions tests/test_diffusers.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,13 +109,13 @@
GaudiStableDiffusionXLImg2ImgPipeline,
GaudiStableDiffusionXLInpaintPipeline,
GaudiStableDiffusionXLPipeline,
GaudiStableVideoDiffusionPipeline,
GaudiStableVideoDiffusionControlNetPipeline,
GaudiStableVideoDiffusionPipeline,
GaudiTextToVideoSDPipeline,
)
from optimum.habana.diffusers.models import (
ControlNetSDVModel,
UNetSpatioTemporalConditionControlNetModel,
ControlNetSDVModel,
UNetSpatioTemporalConditionControlNetModel,
)
from optimum.habana.utils import set_seed

Expand Down Expand Up @@ -2832,6 +2832,7 @@ def test_stable_video_diffusion_no_throughput_regression_bf16(self):
if IS_GAUDI2:
self.assertGreaterEqual(outputs.throughput, 0.95 * 0.012)


class GaudiStableVideoDiffusionControlNetPipelineTester(TestCase):
"""
Tests the StableVideoDiffusionControlNetPipeline for Gaudi.
Expand Down Expand Up @@ -2866,7 +2867,7 @@ def get_dummy_components(self):
)
torch.manual_seed(0)
controlnet = ControlNetSDVModel(
sample_size=96,#
sample_size=96, #
in_channels=8,
out_channels=4,
down_block_types=(
Expand All @@ -2887,7 +2888,7 @@ def get_dummy_components(self):
layers_per_block=2,
cross_attention_dim=1024,
transformer_layers_per_block=1,
num_attention_heads=(5, 10, 20, 20),#
num_attention_heads=(5, 10, 20, 20), #
num_frames=14,
conditioning_channels=3,
conditioning_embedding_out_channels=(16, 32, 96, 256),
Expand All @@ -2912,10 +2913,7 @@ def get_dummy_components(self):
vae = AutoencoderKLTemporalDecoder(
in_channels=3,
out_channels=3,
down_block_types=('DownEncoderBlock2D',
'DownEncoderBlock2D',
'DownEncoderBlock2D',
'DownEncoderBlock2D'),
down_block_types=("DownEncoderBlock2D", "DownEncoderBlock2D", "DownEncoderBlock2D", "DownEncoderBlock2D"),
block_out_channels=(128, 256, 512, 512),
layers_per_block=2,
latent_channels=4,
Expand Down Expand Up @@ -2984,6 +2982,7 @@ def test_stable_video_diffusion_single_video(self):
components = self.get_dummy_components()
gaudi_config = GaudiConfig(use_torch_autocast=True)
sd_pipe = GaudiStableVideoDiffusionControlNetPipeline(use_habana=True, gaudi_config=gaudi_config, **components)

def _get_image_from_pipeline(pipeline, device=device):
for component in pipeline.components.values():
if hasattr(component, "set_default_attn_processor"):
Expand All @@ -2997,13 +2996,43 @@ def _get_image_from_pipeline(pipeline, device=device):
self.assertEqual(len(outputs), 1)
self.assertEqual(image.shape, (14, 3, 64, 64))
return image[0::6, -3:, -3:, -1]
expected_slice = np.array([0.5463, 0.5432, 0.5576, 0.4749, 0.4736, 0.4888, 0.3865, 0.3912, 0.3869,
0.5402, 0.5389, 0.5326, 0.4747, 0.4920, 0.4760, 0.4115, 0.4248, 0.4222,
0.5359, 0.5335, 0.5221, 0.4835, 0.5044, 0.4884, 0.4266, 0.4409, 0.4295])

expected_slice = np.array(
[
0.5463,
0.5432,
0.5576,
0.4749,
0.4736,
0.4888,
0.3865,
0.3912,
0.3869,
0.5402,
0.5389,
0.5326,
0.4747,
0.4920,
0.4760,
0.4115,
0.4248,
0.4222,
0.5359,
0.5335,
0.5221,
0.4835,
0.5044,
0.4884,
0.4266,
0.4409,
0.4295,
]
)
image_slice = _get_image_from_pipeline(sd_pipe)

self.assertLess(np.abs(image_slice.flatten() - expected_slice).max(), 1e-1)


class GaudiStableDiffusionInstructPix2PixPipelineTests(TestCase):
"""
Tests the class StableDiffusionInstructPix2PixPipeline for Gaudi.
Expand Down
Loading
You are viewing a condensed version of this merge commit. You can view the full changes here.