Implementing Stable Diffusion with TensorFlow and Keras for Multi-GPU Inference

Implementing Stable Diffusion with TensorFlow and Keras for Multi-GPU Inference

MLNLP community is a well-known machine learning and natural language processing community in China and abroad, covering NLP master’s and doctoral students, university teachers, and corporate researchers.
The community’s vision is to promote communication and progress between the academic and industrial circles of natural language processing and machine learning, especially for beginners.

Reprinted from | Machine Heart

Recently, the text-to-image model Stable Diffusion has become extremely popular in the AI community. It was implemented by researchers from the University of Munich and Runway based on the CVPR 2022 paper “High-Resolution Image Synthesis with Latent Diffusion Models,” and it can run on consumer-grade GPUs.

Since its release, researchers have conducted deeper studies on Stable Diffusion. Some researchers have combined it with a Web UI, creating a painting tool that allows those without systematic UI knowledge to operate it easily.

To help more people get started quickly, researchers have also compiled a guide for running Stable Diffusion on the Apple M1 chip.

However, the original model was implemented based on Torch, and now, Divam Gupta, a researcher from Meta, has announced that Stable Diffusion implemented with TensorFlow/Keras is here.

Implementing Stable Diffusion with TensorFlow and Keras for Multi-GPU Inference

Project address: https://github.com/divamgupta/stable-diffusion-tensorflow

In summary, the project has three main features: converting pre-trained models; easy-to-understand code; and a small amount of code.

Divam Gupta stated that when running Stable Diffusion on an 8GB M1 MacBook Air, the speed is about 4 times faster compared to the Torch implementation.

Implementing Stable Diffusion with TensorFlow and Keras for Multi-GPU Inference

Keras creator François Chollet stated: it works out of the box on the M1 MacBook Pro’s GPU and can also be used for multi-GPU inference out of the box.

Implementing Stable Diffusion with TensorFlow and Keras for Multi-GPU Inference

Implementing Stable Diffusion with TensorFlow and Keras for Multi-GPU Inference

Chollet also mentioned that besides the GPU, it can also be used for TPU inference out of the box: just get a TPU VM and add the TPU strategy scope to the code. This can significantly speed up inference and reduce costs when processing large batches.

Implementing Stable Diffusion with TensorFlow and Keras for Multi-GPU Inference

Finally, Chollet expressed “great thanks to Divam Gupta for creating this port. It is a high-quality work that will benefit everyone involved in creative AI. I am always amazed at the speed of development in the open-source community.”

1

Project Introduction

How to run this project?

First, go to this address: https://colab.research.google.com/drive/1zVTa4mLeM_w44WaFwl7utTaa6JcaH1zK

Run the command line:

python text2image.py --prompt="An astronaut riding a horse"

Use the Python interface:

pip install git+https://github.com/fchollet/stable-diffusion-tensorflow
from stable_diffusion_tf.stable_diffusion import Text2Imagefrom PIL import Imagegenerator = Text2Image( img_height=512,img_width=512,jit_compile=False,)img = generator.generate("An astronaut riding a horse",num_steps=50,unconditional_guidance_scale=7.5,temperature=1,batch_size=1,)Image.fromarray(img[0]).save("output.png")

It can be completed in just two simple steps, and the output result is as follows: a Rococo-style werewolf drinking coffee in a burning café.

Implementing Stable Diffusion with TensorFlow and Keras for Multi-GPU Inference

Spider-Man in pink and white on a skyscraper:

Implementing Stable Diffusion with TensorFlow and Keras for Multi-GPU Inference

The scene in heaven:

Implementing Stable Diffusion with TensorFlow and Keras for Multi-GPU Inference

Those interested can give it a try.

Reference links:

https://twitter.com/divamgupta/status/1571234504320208897?s=20&t=kpyjZ8LLSmgJFnUKqyepAw

https://twitter.com/fchollet/status/1571874757582389250

Technical Group Invitation

Implementing Stable Diffusion with TensorFlow and Keras for Multi-GPU Inference

△Long press to add the assistant

Scan the QR code to add the assistant on WeChat

Please note: Name-School/Company-Research Direction
(e.g., Xiao Zhang-Harbin Institute of Technology-Dialogue System)
to apply for joining the Natural Language Processing/PyTorch technical group

About Us

MLNLP community is a civil academic community jointly built by machine learning and natural language processing scholars from home and abroad. It has now developed into a well-known machine learning and natural language processing community, aiming to promote progress between the academic and industrial circles of machine learning and natural language processing.
The community can provide an open communication platform for practitioners in further education, employment, and research. Everyone is welcome to follow and join us.

Implementing Stable Diffusion with TensorFlow and Keras for Multi-GPU Inference

Leave a Comment