Run and profile ML Drift on Arm Mali GPUs
Prepare the model data
After setting up tools and connecting your Android device, run Stable Diffusion.
Start by preparing data for the model. From the ML Drift repository root, create a model data directory and download the text vocabulary:
mkdir -p /tmp/sd_1_5
curl --fail --location \
https://huggingface.co/OpenGVLab/ViCLIP-B-16-hf/resolve/main/bpe_simple_vocab_16e6.txt.gz \
--output /tmp/sd_1_5/bpe_simple_vocab_16e6.txt.gz
The original OpenAI vocabulary is available under an MIT License .
Download the model weights in 16-bit floating-point format, available under the CreativeML Open RAIL-M License . Ensure that you have at least 2.13 GB of storage on both your host and Android device:
curl --fail --location \
https://huggingface.co/Comfy-Org/stable-diffusion-1.5/resolve/main/v1-5-pruned-emaonly-fp16.safetensors \
--output /tmp/v1-5-pruned-emaonly-fp16.safetensors
On your host computer, decompress the vocabulary and extract the model weights into /tmp/sd_1_5/:
gzip -dc /tmp/sd_1_5/bpe_simple_vocab_16e6.txt.gz > /tmp/sd_1_5/bpe_simple_vocab_16e6.txt
bazel run //ml_drift/samples/stable_diffusion:extract_weights -- \
--model_path=/tmp/v1-5-pruned-emaonly-fp16.safetensors \
--output_dir=/tmp/sd_1_5/
Build and deploy the executable
On macOS, the build can fail with Apple C or C++ toolchain configuration errors, including InvalidDottedVersionException. If this happens, try disabling the Apple toolchain and retrying the build:
export BAZEL_NO_APPLE_CPP_TOOLCHAIN=1
If this introduces an absolute path inclusion error referencing SDKSettings.json, restore the default toolchain. Then, restart Bazel and retry the build:
unset BAZEL_NO_APPLE_CPP_TOOLCHAIN
bazel shutdown
Android devices can use either 4 KB or 16 KB memory pages. Check the memory page size of your connected device:
adb shell getconf PAGE_SIZE
If the command returns 16384, use the following linker flags to ensure compatible executable alignment. If it returns 4096, you can omit the two linker flags.
Build the optimized OpenCL sample for Android arm64:
bazel build -c opt --config=android_arm64 \
--linkopt=-Wl,-z,max-page-size=16384 \
--linkopt=-Wl,-z,common-page-size=16384 \
//ml_drift/samples/stable_diffusion:sd_gpu_opencl
Copy the executable and model data to /data/local/tmp/ on your Android device, then make the executable runnable:
adb push bazel-bin/ml_drift/samples/stable_diffusion/sd_gpu_opencl /data/local/tmp/
adb push /tmp/sd_1_5 /data/local/tmp/
adb shell chmod +x /data/local/tmp/sd_gpu_opencl
Start the sample on your Android device from /data/local/tmp/:
adb shell -t 'cd /data/local/tmp && ./sd_gpu_opencl'
At Enter phrase:, enter an image description. At Enter steps:, enter the number of diffusion steps. For example:
image of a cat in space
10
After image generation, the sample prints timing measurements. The following output was captured from a Vivo X300 with a Mali G1-Ultra GPU:
Init time: 20.07 ms.
Loop time: 11052.8 ms.
Decode time: 588.189 ms.
Total diffuser time: 11661 ms.
Your timings will vary.
The timing values can be described as follows:
- Init time: Time to prepare your text prompt and the starting noise for image generation.
- Loop time: Time spent repeatedly removing noise to form an image that matches your prompt.
- Decode time: Time to turn the compact image representation of the model into pixels, and retrieve the result.
- Total diffuser time: Total time for the three stages, excluding loading the model at startup and saving the image file.
Retrieve the generated image
When generation finishes and Enter phrase: returns, press Ctrl+C to exit the sample. Copy the generated image from the device and open it:
adb pull /data/local/tmp/result.bmp ./result.bmp
open ./result.bmp
The following image was generated by the validated 10-step run on the Android Mali device. Your result can differ:
Stable Diffusion output from a 10-step run on Mali-G1 Ultra
ML Drift lets you reuse unified shader code across GPU backends. Reuse reduces the need to maintain separate implementations for each API. Standalone ML Drift C++ APIs and the custom operation framework let you integrate GPU-accelerated inference into your own runtime. You can extend inference with specialized operations.
Understand the unified shader in the sample
The sample uses Unified Compute Language (UCL) as an abstraction layer over OpenCL. For example, ml_drift/samples/stable_diffusion/ops/diffusion_step.cc contains the following excerpt from a kernel template that’s stored in a C++ string:
int X = ucl::GetGlobalId<0>();
int Y = ucl::GetGlobalId<1>();
int S = ucl::GetGlobalId<2>();
if (X >= args.dst.Width() || Y >= args.dst.Height() || S >= args.dst.Slices()) return;
Type eta_cond = args.etaCondIn.Read(X, Y, S);
ucl::GetGlobalId identifies the coordinates of the work item. args.etaCondIn.Read expresses a tensor read without specifying its underlying GPU storage. ML Drift translates these abstractions into OpenCL source for this backend, which the driver compiles for the GPU. The same unified shader logic can target other supported backends.
What you’ve accomplished and what’s next
You’ve built and run the OpenCL sample, and retrieved its output.
Next, you’ll use Streamline to confirm Mali GPU activity during image generation. You’ll assess how effectively the workload uses GPU compute and memory resources.