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

[PPDiffusers] implement save_to_hf_hub for pipelines, models, and Preprocessor #4640

Merged
merged 5 commits into from
Feb 7, 2023

Conversation

sijunhe
Copy link
Collaborator

@sijunhe sijunhe commented Feb 3, 2023

PR types

PR changes

Description

# feature extractor
from paddlenlp.transformers import CLIPFeatureExtractor
feature_processing_config = {
  "crop_size": 32,
  "do_center_crop": True,
  "do_convert_rgb": True,
  "do_normalize": True,
  "do_resize": True,
  "feature_extractor_type": "CLIPFeatureExtractor",
  "image_mean": [
    0.48145466,
    0.4578275,
    0.40821073
  ],
  "image_std": [
    0.26862954,
    0.26130258,
    0.27577711
  ],
  "resample": 3,
  "size": 224
}
feature_extractor = CLIPFeatureExtractor(**feature_processing_config)
# scheduler
from ppdiffusers import DDIMScheduler
scheduler_config = {
  "beta_end": 0.012,
  "beta_schedule": "scaled_linear",
  "beta_start": 0.00085,
  "num_train_timesteps": 1000,
  "steps_offset": 1,
  "clip_sample": False
}
scheduler = DDIMScheduler(**scheduler_config)
# clip
from paddlenlp.transformers import CLIPTextConfig, CLIPTextModel, CLIPTokenizer
clip_text_config = CLIPTextConfig(**{
  "architectures": [
    "CLIPTextModel"
  ],
  "hidden_size": 32,
  "intermediate_size": 37,
  "max_position_embeddings": 77,
  "model_type": "clip_text_model",
  "num_attention_heads": 4,
  "num_hidden_layers": 5,
  "vocab_size": 1000
})
text_encoder = CLIPTextModel(clip_text_config)
tokenizer = CLIPTokenizer.from_pretrained("test_clip_tokenizer")
from paddlenlp.transformers import CLIPVisionConfig
from ppdiffusers.pipelines.stable_diffusion import StableDiffusionSafetyChecker
vision_config = CLIPVisionConfig(**{
    "hidden_size": 32,
    "image_size": 32,
    "model_type": "clip_vision_model",
    "num_attention_heads": 4,
    "num_channels": 3,
    "num_hidden_layers": 5,
    "patch_size": 2,
    "return_dict": True,
  })
safety_checker = StableDiffusionSafetyChecker(vision_config)
# unet
from ppdiffusers import UNet2DConditionModel
unet = UNet2DConditionModel(**{
  "attention_head_dim": 8,
  "block_out_channels": [
    32,
    64
  ],
  "cross_attention_dim": 32,
  "down_block_types": [
    "DownBlock2D",
    "CrossAttnDownBlock2D"
  ],
  "in_channels": 4,
  "layers_per_block": 2,
  "out_channels": 4,
  "sample_size": 32,
  "up_block_types": [
    "CrossAttnUpBlock2D",
    "UpBlock2D"
  ]
})
# vae 
from ppdiffusers import AutoencoderKL
vae = AutoencoderKL(**{
  "act_fn": "silu",
  "block_out_channels": [
    32,
    64
  ],
  "down_block_types": [
    "DownEncoderBlock2D",
    "DownEncoderBlock2D"
  ],
  "in_channels": 3,
  "latent_channels": 4,
  "layers_per_block": 1,
  "norm_num_groups": 32,
  "out_channels": 3,
  "sample_size": 32,
  "up_block_types": [
    "UpDecoderBlock2D",
    "UpDecoderBlock2D"
  ]
})
from ppdiffusers import StableDiffusionPipeline
from ppdiffusers import StableDiffusionPipeline
pipe = StableDiffusionPipeline(
    vae=vae,
    text_encoder=text_encoder,
    tokenizer=tokenizer,
    unet=unet,
    scheduler=scheduler,
    safety_checker=safety_checker,
    feature_extractor=feature_extractor
)
pipe.save_to_hf_hub("tiny-random-stable-diffusion-pipe")

https://huggingface.co/sijunhe/tiny-random-stable-diffusion-pipe/tree/main

@sijunhe sijunhe requested a review from JunnYu February 3, 2023 09:28
@paddle-bot
Copy link

paddle-bot bot commented Feb 3, 2023

Thanks for your contribution!

@codecov
Copy link

codecov bot commented Feb 3, 2023

Codecov Report

Merging #4640 (6edb3b4) into develop (68156b6) will decrease coverage by 0.02%.
The diff coverage is 16.21%.

@@             Coverage Diff             @@
##           develop    #4640      +/-   ##
===========================================
- Coverage    42.11%   42.10%   -0.02%     
===========================================
  Files          433      433              
  Lines        61821    61849      +28     
===========================================
+ Hits         26038    26041       +3     
- Misses       35783    35808      +25     
Impacted Files Coverage Δ
paddlenlp/transformers/model_utils.py 77.32% <0.00%> (-0.44%) ⬇️
paddlenlp/transformers/tokenizer_utils_base.py 78.14% <0.00%> (-0.24%) ⬇️
paddlenlp/transformers/image_processing_utils.py 24.41% <20.83%> (-1.59%) ⬇️
...lenlp/ops/fast_transformer/transformer/decoding.py 7.72% <100.00%> (ø)

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

Copy link
Member

@JunnYu JunnYu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@sijunhe sijunhe merged commit 06124c8 into PaddlePaddle:develop Feb 7, 2023
@sijunhe sijunhe deleted the ppdiffuser_save_hf branch February 7, 2023 02:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants