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

add callable object to convert frame into control_frame to reduce cpu memory usage. #10501

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

chaowenguo
Copy link
Contributor

@chaowenguo chaowenguo commented Jan 8, 2025

What does this PR do?

Fixes # (issue)

Before submitting

Who can review?

Anyone in the community is free to review the PR once the tests have passed. Feel free to tag
members/contributors who may be interested in your PR.

@hlky

@hlky
Copy link
Collaborator

hlky commented Jan 8, 2025

Hi @chaowenguo. Could you share a usage example and some benchmarks/profiling to show the reduction in cpu memory usage?

@chaowenguo
Copy link
Contributor Author

chaowenguo commented Jan 9, 2025

@hlky
example:

import torch, torch_xla, cv2, PIL.Image, diffusers, imageio, builtins, easy_dwpose, rerender_a_video, numpy

strength = [0.9, 0.95, 0.98, 1]

def process(index):
    openpose = easy_dwpose.DWposeDetector()
    with imageio.get_reader('pose0.mp4') as reader, imageio.get_writer(f'out{index}.mp4', fps=reader.get_meta_data().get('fps')) as writer:
        frames = [PIL.Image.fromarray(reader.get_data(_)) for _ in builtins.range(reader.count_frames())]
        controlnet = diffusers.ControlNetModel.from_pretrained('chaowenguo/control_v11p_sd15_openpose', torch_dtype=torch.bfloat16, variant='fp16', use_safetensors=True)
        pipeline = rerender_a_video.RerenderAVideoPipeline.from_single_file('https://huggingface.co/chaowenguo/pal/blob/main/chilloutMix-Ni.safetensors', config='chaowenguo/stable-diffusion-v1-5', safety_checker=None, controlnet=controlnet, use_safetensors=True, torch_dtype=torch.bfloat16, device=torch_xla.core.xla_model.xla_device())
        pipeline.vae = diffusers.AutoencoderKL.from_single_file('https://huggingface.co/chaowenguo/pal/blob/main/vae-ft-mse-840000-ema-pruned.safetensors', torch_dtype=torch.bfloat16, use_safetensors=True).to(torch_xla.core.xla_model.xla_device())
        pipeline.scheduler = diffusers.DDIMScheduler.from_config(pipeline.scheduler.config)

        for _ in pipeline(prompt='A gorgeous smiling slim young japanese girl, befautiful face, hands with five fingers, light background, best quality, extremely detailed, HD, ultra-realistic, 8K, HQ, masterpiece, trending on artstation, art, smooth', frames=frames, control_frames=openpose, generator=torch.manual_seed(0), strength=strength[index], negative_prompt='monochrome, dark background, longbody, lowres, bad anatomy, bad hands, fused fingers, missing fingers, too many fingers, extra digit, fewer difits, cropped, worst quality, low quality, deformed body, bloated, ugly, unrealistic, extra hands and arms').frames: writer.append_data(numpy.asarray(_))

if __name__ == '__main__': torch_xla.launch(process)

You see the origin version require two long lists of PIL.Image before hand. one for frames and one for control_frames. Now the control_frames can be calculated on the fly which only need one list of PIL.Image before hand. so the cpu memory usage can be half compare to the original version

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.

2 participants