# FLUX.1-dev image generation on M1 Mac using GPU

**URL:** </static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fflux-1-dev-image-generation-on-m1-mac-using-gpu%2F112659%3E%5C
**Category:** Beginners\
**Created:** [October 18, 2024, 11:31am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fflux-1-dev-image-generation-on-m1-mac-using-gpu%2F112659 "2024-10-18T11:31:10Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![fhashim](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/fhashim/32/33098_2.png) [@fhashim](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Ffhashim)%5C
**Post date:** [October 18, 2024, 11:31am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fflux-1-dev-image-generation-on-m1-mac-using-gpu%2F112659%2F1 "2024-10-18T11:31:10Z")

</div>

I want to make use of GPU on my M1 Mac to generate images through prompt using `black-forest-labs/FLUX.1-dev`. Adding the code below.

I’ve specified `mps` for `torch.Generator` but the script fails stating with error `latents = torch.randn(shape, generator=generator, device=rand_device, dtype=dtype, layout=layout).to(device) RuntimeError: Placeholder storage has not been allocated on MPS device!`

How can I make use of GPU to generate image?

```auto
prompt = "A cat holding a sign that says hello world"
image = pipe(
    prompt,
    height=1024,
    width=1024,
    guidance_scale=3.5,
    num_inference_steps=1,
    max_sequence_length=512,
    generator=torch.Generator("mps").manual_seed(0)
).images[0]
image.save("flux-dev.png")

```

---

<div class="post-metadata">

**Author:** ![John6666](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/john6666/32/27664_2.png) [@John6666](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2FJohn6666)%5C
**Post date:** [October 18, 2024, 2:30pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fflux-1-dev-image-generation-on-m1-mac-using-gpu%2F112659%2F2 "2024-10-18T14:30:38Z")

</div>

How about this?

```py
import torch
device = torch.device("mps" if torch.backends.mps.is_available() else "cpu")
prompt = "A cat holding a sign that says hello world"
pipe.to(device)
image = pipe(
    prompt,
    height=1024,
    width=1024,
    guidance_scale=3.5,
    num_inference_steps=1,
    max_sequence_length=512,
    generator=torch.Generator(device).manual_seed(0)
).images[0]
image.save("flux-dev.png")

```

---

<div class="post-metadata">

**Author:** ![fhashim](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/fhashim/32/33098_2.png) [@fhashim](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Ffhashim)%5C
**Post date:** [October 18, 2024, 3:31pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fflux-1-dev-image-generation-on-m1-mac-using-gpu%2F112659%2F3 "2024-10-18T15:31:08Z")

</div>

Although now it picks GPU but still fails adding error below. I’ve added `pipe.enable_model_cpu_offload()` but still fails. Also I’ve executed `PYTORCH_MPS_HIGH_WATERMARK_RATIO=0.0 ` still fails

```auto
RuntimeError: MPS backend out of memory (MPS allocated: 36.21 GB, other allocations: 384.00 KB, max allowed: 36.27 GB). Tried to allocate 144.00 MB on private pool. Use PYTORCH_MPS_HIGH_WATERMARK_RATIO=0.0 to disable upper limit for memory allocations (may cause system failure).

```

---

<div class="post-metadata">

**Author:** ![John6666](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/john6666/32/27664_2.png) [@John6666](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2FJohn6666)%5C
**Post date:** [October 18, 2024, 3:35pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fflux-1-dev-image-generation-on-m1-mac-using-gpu%2F112659%2F4 "2024-10-18T15:35:22Z")

</div>

How about this?

```auto
pip install -U accelerate

```

```py
import torch
device = torch.device("mps" if torch.backends.mps.is_available() else "cpu")
pipe = DiffusionPipeline.from_pretrained("black-forest-labs/FLUX.1-dev", torch_dtype=torch.bfloat16, device_map="auto") # reweite it
pipe.enable_model_cpu_offload()
prompt = "A cat holding a sign that says hello world"
#pipe.to(device) # it causes OOM
image = pipe(
    prompt,
    height=1024,
    width=1024,
    guidance_scale=3.5,
    num_inference_steps=1,
    max_sequence_length=512,
    generator=torch.Generator(device).manual_seed(0)
).images[0]
image.save("flux-dev.png")

```

---

<div class="post-metadata">

**Author:** ![fhashim](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/fhashim/32/33098_2.png) [@fhashim](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Ffhashim)%5C
**Post date:** [October 18, 2024, 3:40pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fflux-1-dev-image-generation-on-m1-mac-using-gpu%2F112659%2F5 "2024-10-18T15:40:55Z")

</div>

there is syntax error in this code. pipe is referenced before assignment

---

<div class="post-metadata">

**Author:** ![John6666](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/john6666/32/27664_2.png) [@John6666](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2FJohn6666)%5C
**Post date:** [October 18, 2024, 3:44pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fflux-1-dev-image-generation-on-m1-mac-using-gpu%2F112659%2F6 "2024-10-18T15:44:11Z")

</div>

Sorry, I failed to copy and paste.
