-
Notifications
You must be signed in to change notification settings - Fork 6.1k
[Modular] support standard repo #11944
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
base: main
Are you sure you want to change the base?
Conversation
cc @vladmandic let me know if this works for you |
The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update. |
thanks @yiyixuxu, this is pretty much what i asked for! few comments:
warningswarnings during convert:
warnings during generate:
imagescode# pip install git+https://github.com/huggingface/diffusers@modular-standard-repo
import torch
import diffusers
model = '/mnt/models/stable-diffusion/mine/tempest-by-vlad-0.1.safetensors' # https://civitai.com/models/1157409/tempest-by-vlad
cache_dir = '/mnt/models/huggingface'
modular_map= {
'StableDiffusionXLPipeline': 'StableDiffusionXLAutoBlocks',
}
def get_args():
return {
'prompt': 'a photo of an astronaut in a diner',
'num_inference_steps': 20,
'generator': torch.Generator(device='cuda').manual_seed(42),
}
def convert_to_modular(standard_pipe):
try:
modular_cls = modular_map.get(standard_pipe.__class__.__name__, None)
if modular_cls is None:
raise ValueError(f'unknown: cls={standard_pipe.__class__.__name__}')
modular_cls = getattr(diffusers, modular_cls, None)
if modular_cls is None:
raise ValueError(f'invalid: cls={standard_pipe.__class__.__name__}')
modular_blocks = modular_cls()
modular_pipe = modular_blocks.init_pipeline()
components_dct = {k: v for k, v in standard_pipe.components.items() if v is not None}
modular_pipe.update_components(**components_dct, **standard_pipe.parameters)
modular_pipe.original_pipe = standard_pipe
print(f'convert: from={standard_pipe.__class__.__name__} to={modular_pipe.__class__.__name__}')
return modular_pipe
except Exception as e:
print(f'convert: error={e}')
raise e
def restore_standard(modular_pipe):
if hasattr(modular_pipe, 'original_pipe'):
print(f'convert: from={modular_pipe.__class__.__name__} to={modular_pipe.original_pipe.__class__.__name__}')
return modular_pipe.original_pipe
def gc():
import gc
gc.collect()
torch.cuda.empty_cache()
torch.cuda.ipc_collect()
print(f'memory: free={torch.cuda.mem_get_info()[0]} peak={torch.cuda.memory_stats()["allocated_bytes.all.peak"]}')
torch.cuda.reset_peak_memory_stats()
if __name__ == '__main__':
print('loading')
pipe = diffusers.StableDiffusionXLPipeline.from_single_file(model, torch_dtype=torch.bfloat16, cache_dir=cache_dir).to('cuda')
gc()
print('generate: standard')
output = pipe(**get_args())
print(f'output: {output}')
gc()
image = output.images[0]
image.save('standard-default1.png')
print('generate: modular')
pipe = convert_to_modular(pipe)
output = pipe(**get_args())
gc()
print(f'output: {output}')
image = output.intermediates['images'][0]
image.save('standard-modular1.png')
print('generate: standard')
pipe = restore_standard(pipe)
output = pipe(**get_args())
gc()
image = output.images[0]
image.save('standard-default2.png')
pipe = None
gc() |
warningsI removed the warnings during the generation (changed to However, we are still iterating everything, so that may change in future too image differenceThe results from modular and standard pipelines are not going to be the same: Other than the guider, we are also taking this opportunity to refactor all our pipeline methods to make them more modular and easy to customize upon - this is an ongoing effort, we are likely to change more as we integrate more pipelines into modular diffusers However, once modular is out of experimental feature, moving forward, we will make sure new pipelines will have same results in both systems for now, I think if reproducibility is important for SD.NEXT users, I think we can only convert if they need to use a new guider class that's not supported in standard pipeline outputI updated image = t2i_pipe(prompt=prompt, num_inference_steps=25, output="images")[0]
image.save(f"{output_name}_1.png")
image = t2i_pipe(prompt=prompt, num_inference_steps=25).images[0]
image.save(f"{output_name}_2.png")
image = t2i_pipe(prompt=prompt, num_inference_steps=25).intermediates['images'][0]
image.save(f"{output_name}_3.png") |
thanks @yiyixuxu re: warnings - all good |
fix #11915
this PR support using standard repo in Modular system
option1
option2
option3