Stable Diffusion is an open-source deep-learning model. It can generate (paint) images conditioned on text descriptions.
I tried to run Stable Diffusion on my Android phone and it worked. No root required.
But why…? Because why not. A developer, Raoul Straczowski, on Github ran Stable Diffusion on his Raspberry Pi 4 and it could generate images up to 400x400 pixels. I think it should work on flagship Android phones, too. Therefore I tried many ways and it finally worked. The result is not bad.
- Device: Sony Xperia 5 II (Android 13)
- CPU: Qualcomm Snapdragon 865 (Adreno 650)
- RAM: 8GB
- Storage: 30GB+
- High speed network for downloading large files.
Install proot Debian. We only need rootfs, there is no need to add a new user and install desktop environment.
Log into Proot Debian
- Install Git, Vim, Python, Pip
- Install Anaconda if you need it.
There are many versions of Stable Diffusion. I follow Raoul Straczowski’s instructions and choose Stable Diffusion v1.5 made by runwayml. It could run headlessly in terminal.
- Clone runwayml’s repository and download all models (around 30GB)
- Then install python dependences
Because my phone only has 8GB RAM, the maximum image size that it could generate is 320x320 pixels. Beyond that Termux will crash immediately.
Disable background process in Developer Settings. I also enable “Stay awake while charging”.
Termux will crash when generating large images due to the limitation of Android OS, so we have to increase the value of
max_phantom_processes. Please refer to Fix Singal 9 error to install ADB tools on PC, and execute following commands to set value of
- Create a Python script.
- Type these
- Let’s go!
Wait for 20 minutes
We can see Stable Diffusion consumes almost all memory while computing
To view the generated image, move it to the internal storage
- Here is the result. Not bad, huh?
We can run Stable Diffusion with Adreno GPU to accelerate the generation of images.
This is very experimental. Virglrenderer may not work on non-Adreno devices which do not support Vulkan.
- Install virglrenderer on Termux. Then start virgl server.
- Log into Proot. Run Python script with virgl
The time of image generation will reduce a little bit.
Note: You need root permission to do this.
Since Stable Diffusion requires a lot of RAM, why not create a big swapfile on chroot? So Termux will not crash due to out of memory.
Install chroot Ubuntu in Termux.
Install Python and Stable Diffusion (similar to Debian) or just copy files from proot-distro (the data is located at
Log into chroot Ubuntu. Create a 8GB swap file on chroot, so we will have 8 + 4 + 8 = 20GB RAM on our phone. If we don’t do this, the phone will reboot when it is out of memory.
In the following test, I increase the size of image to 512x512 but Termux still crash while computing, so I enter chroot via ADB shell.
Wait for 1 hour, and here is the result. Her face is like saying “Why did you spend so much time on doing this?”