Skip to content

Commit

Permalink
[PPDiffusers] StableDiffusion Pipeline 升级 (#390)
Browse files Browse the repository at this point in the history
#377

完成1,2,3代码和单测,4和3单测相同
暂时测试可以,有其他问题再找时间修改

pytest test_stable_diffusion_img2img.py

![image](https://github.com/PaddlePaddle/PaddleMIX/assets/4617245/1e5bbbef-7090-43e4-b0ba-c2bfd13e98aa)

pytest test_stable_diffusion_inpaint.py

![image](https://github.com/PaddlePaddle/PaddleMIX/assets/4617245/7ca9d9b8-620d-4090-b09c-5b8653ab0d64)

pytest test_stable_diffusion_inpaint_legacy.py

![image](https://github.com/PaddlePaddle/PaddleMIX/assets/4617245/cd08e49f-6e90-4b7d-8c7a-6cffe7e22b90)
  • Loading branch information
co63oc authored Jan 22, 2024
1 parent c77b249 commit 64bfe32
Show file tree
Hide file tree
Showing 17 changed files with 7,521 additions and 13 deletions.
5 changes: 5 additions & 0 deletions ppdiffusers/ppdiffusers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,3 +117,8 @@
from .utils.dummy_paddle_and_paddlenlp_and_einops_objects import * # noqa F403
else:
from .pipelines import UniDiffuserPipeline
from .pipelines.stable_diffusion import (
StableDiffusionImg2ImgPipeline,
StableDiffusionInpaintPipeline,
StableDiffusionInpaintPipelineLegacy,
)
8 changes: 7 additions & 1 deletion ppdiffusers/ppdiffusers/pipelines/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,13 @@
)
from .lvdm import LVDMTextToVideoPipeline, LVDMUncondPipeline
from .pipeline_utils import DiffusionPipeline
from .stable_diffusion import StableDiffusionPipeline, StableDiffusionPipelineOutput
from .stable_diffusion import (
StableDiffusionImg2ImgPipeline,
StableDiffusionInpaintPipeline,
StableDiffusionInpaintPipelineLegacy,
StableDiffusionPipeline,
StableDiffusionPipelineOutput,
)
from .stable_diffusion_xl import (
StableDiffusionXLPipeline,
StableDiffusionXLPipelineOutput,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@
StableDiffusionPipeline,
StableDiffusionPipelineOutput,
)
from .pipeline_stable_diffusion_img2img import StableDiffusionImg2ImgPipeline
from .pipeline_stable_diffusion_inpaint import StableDiffusionInpaintPipeline
from .pipeline_stable_diffusion_inpaint_legacy import (
StableDiffusionInpaintPipelineLegacy,
)
from .safety_checker import StableDiffusionSafetyChecker

try:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@ def encode_prompt(
prompt_embeds = self.text_encoder.text_model.final_layer_norm(prompt_embeds)

if self.text_encoder is not None:
prompt_embeds_dtype = self.text_encoder.dtype
prompt_embeds_dtype = self.text_encoder._dtype
elif self.unet is not None:
prompt_embeds_dtype = self.unet.dtype
else:
Expand Down

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -276,8 +276,8 @@ def set_timesteps(
self.sigmas_down = paddle.concat([sigmas_down[:1], sigmas_down[1:].repeat_interleave(2), sigmas_down[-1:]])

timesteps = paddle.to_tensor(timesteps, dtype=paddle.float32)
sigmas_interpol = sigmas_interpol.cpu()
log_sigmas = self.log_sigmas.cpu()
sigmas_interpol = sigmas_interpol.cpu().numpy()
log_sigmas = self.log_sigmas.cpu().numpy()
timesteps_interpol = np.array(
[self._sigma_to_t(sigma_interpol, log_sigmas) for sigma_interpol in sigmas_interpol]
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -268,8 +268,8 @@ def set_timesteps(
timesteps = paddle.to_tensor(timesteps, dtype=paddle.float32)

# interpolate timesteps
sigmas_interpol = sigmas_interpol.cpu()
log_sigmas = self.log_sigmas.cpu()
sigmas_interpol = sigmas_interpol.cpu().numpy()
log_sigmas = self.log_sigmas.cpu().numpy()
timesteps_interpol = np.array(
[self._sigma_to_t(sigma_interpol, log_sigmas) for sigma_interpol in sigmas_interpol]
)
Expand Down
15 changes: 15 additions & 0 deletions ppdiffusers/ppdiffusers/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,21 @@
convert_state_dict_to_ppdiffusers,
convert_unet_state_dict_to_peft,
)
from .testing_utils import ( # load_image,
floats_tensor,
image_grid,
load_hf_numpy,
load_numpy,
load_pd,
load_ppnlp_numpy,
nightly,
paddle_all_close,
paddle_device,
parse_flag_from_env,
print_tensor_test,
require_paddle_gpu,
slow,
)

logger = get_logger(__name__)

Expand Down
4 changes: 1 addition & 3 deletions ppdiffusers/ppdiffusers/utils/paddle_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,7 @@ def randn_tensor(

if isinstance(generator, (list, tuple)):
batch_size = shape[0]
shape = [
1,
] + shape[1:]
shape = (1,) + tuple(shape[1:])
latents = [randn_pt(shape, generator=generator[i], dtype=dtype) for i in range(batch_size)]
latents = paddle.concat(latents, axis=0)
else:
Expand Down
13 changes: 13 additions & 0 deletions ppdiffusers/tests/pipelines/stable_diffusion/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Copyright (c) 2024 PaddlePaddle Authors. All Rights Reserved.
#
# 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.
Loading

0 comments on commit 64bfe32

Please sign in to comment.