| 1 |
# Copyright 2024 The HuggingFace Inc. team. All rights reserved. |
| 2 |
# |
| 3 |
# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
# you may not use this file except in compliance with the License. |
| 5 |
# You may obtain a copy of the License at |
| 6 |
# |
| 7 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
# |
| 9 |
# Unless required by applicable law or agreed to in writing, software |
| 10 |
# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
# See the License for the specific language governing permissions and |
| 13 |
# limitations under the License. |
| 14 |
|
| 15 |
import torch |
| 16 |
from diffusers import DiffusionPipeline |
| 17 |
|
| 18 |
from accelerate import PartialState # Can also be Accelerator or AcceleratorState |
| 19 |
|
| 20 |
|
| 21 |
pipe = DiffusionPipeline.from_pretrained("stable-diffusion-v1-5/stable-diffusion-v1-5", torch_dtype=torch.float16) |
| 22 |
distributed_state = PartialState() |
| 23 |
pipe.to(distributed_state.device) |
| 24 |
|
| 25 |
# Assume two processes |
| 26 |
# On the first GPU, the prompts will be ["a dog", "a cat"], |
| 27 |
# and on the second GPU it will be ["a chicken", "a chicken"]. |
| 28 |
# Make sure to drop the final sample, as it will be a duplicate of the previous one. |
| 29 |
with distributed_state.split_between_processes(["a dog", "a cat", "a chicken"], apply_padding=True) as prompt: |
| 30 |
result = pipe(prompt).images |