Testing unigpu on an iPad (M4) and on Google Colab¶
In short¶
| Where | What runs | Hardware used | Status |
|---|---|---|---|
| iPad, Carnets, Juno | Metal (through rubicon-objc) + simulator | Apple M4 GPU | works (Carnets) |
| iPad, Code App (thebaselab) | Metal (through rubicon-objc) + simulator | Apple M4 GPU | works |
| iPad, Pythonista | Metal (through objc_util) + simulator |
Apple M4 GPU | not tried yet |
| iPad, Pyto, a-Shell | Metal (through rubicon-objc) + simulator | Apple M4 GPU | not tried yet |
| iPad, WebAssembly apps (Pyodios...) | simulator only | CPU | no Metal (no access to Objective-C) |
| iPad → Safari → Google Colab | CUDA (+ OpenCL on the NVIDIA GPU) | NVIDIA GPU (T4...) | notebooks provided, to run |
Everything is installed with pip, from PyPI, and the examples and notebooks come from the documentation site: nothing needs access to the repository. For Windows, macOS, Linux and Chromebooks, see INSTALL.md.
1. iPad: install unigpu¶
unigpu is written in pure Python: pip installs it in every Python app of the iPad that has pip, from PyPI. Nothing comes from the repository. The Metal option adds rubicon-objc, BeeWare's bridge to Objective-C, also pure Python:
pip install "unigpu[metal-rubicon]"
Requirements: Python ≥ 3.9 and NumPy ≥ 1.19 (tested from 1.19 to 2.x). An
iPad app cannot install packages that contain compiled code: NumPy must be
provided by the app. Pythonista, Pyto, a-Shell, Carnets and Juno include
it, but their versions vary: check with
import sys, numpy; print(sys.version, numpy.__version__).
The examples: in Safari, download
examples.zip from the
documentation site, then tap it in the Files app to unzip it, and move
the examples folder into the space of the Python app (On My iPad tab).
Each example takes the device as an argument, for instance
python 03_matrix_multiplication.py metal.
To update: pip install -U "unigpu[metal-rubicon]". The first line of
gpu.detect() gives the version of unigpu (for instance (unigpu 0.5.0)).
2. iPad with Pythonista: Metal through objc_util¶
Pythonista provides objc_util, the bridge to Objective-C that unigpu uses
to drive Metal: rubicon-objc is not needed. Pythonista has no pip of its
own: StaSh, the shell for Pythonista,
provides one.
- Install StaSh by following its README, then open
launch_stash.py. - In StaSh:
pip install unigpu. -
Create a script with these lines, then tap ▶:
from unigpu import detect, selftest detect() selftest.main(["all"]) -
Expected result:
- in the report of
detect(), a linePlatform metal:0 — Apple Metal (Apple) via objc_util (Pythonista)and the GPU of the iPad; - then, for the Apple GPU as for the simulator, a list of
[OK ]checks ending with11/11 checks passed.
- in the report of
-
For the examples, long-press ▶ → Run with arguments, for instance
examples/03_matrix_multiplication.pywith the argumentmetal.
3. iPad with Pyto, a-Shell, Carnets or Juno: Metal through rubicon-objc¶
These apps do not have objc_util: unigpu goes through rubicon-objc, which
works on iPadOS.
- a-Shell:
pip install "unigpu[metal-rubicon]", thenpython3 -m unigpu.selftest all; for the examples,cdinto theexamplesfolder, thenpython3 03_matrix_multiplication.py metal; - Pyto:
pip install "unigpu[metal-rubicon]"in its terminal (or its PyPI installer), then the same commands; - Carnets / Juno: download
ipad.ipynbfrom the documentation site and open it. Its first cell runs%pip install "unigpu[metal-rubicon]"; then run every cell (detection, self-test, timed Metal kernel, generated Metal code). Then,examples.ipynbinstalls unigpu and fetches the examples the same way: it shows every example and measures it on the iPad's GPU for growing sizes (tables and charts).
If rubicon-objc cannot be installed in the app, only the simulator is detected: the self-test reports it.
If only the simulator is detected, the metal line of "Unavailable
backends" gives the reason. Right after a pip install, Python does not
always see the folder where pip has just installed the packages (it only
discovers it at the next start). For rubicon-objc, unigpu looks for it in
pip's installation folders itself. If import unigpu fails right after the
installation, or if gpu.detect() already ran before it, restart the app
or the kernel of the notebook.
4. iPad with Code App (thebaselab)¶
Code App ships Python 3.9.2, with which unigpu works. NumPy must be provided by the app (section 1): check it first in the terminal (chevron button):
python -c "import numpy; print(numpy.__version__)"
If NumPy is missing, unigpu cannot run in Code App. Use a-Shell, Pyto or Pythonista instead (sections 2 and 3), or Colab.
Install unigpu in the terminal: it is pure Python, so pip installs it, choosing the version compatible with Python 3.9. With rubicon-objc, also pure Python, for Metal:
pip install "unigpu[metal-rubicon]"
Get the examples as in section 1. In Code App's explorer, tap the
open-folder icon and choose the examples folder. Put it in On My iPad or
in iCloud Drive: Code App only opens the other storage services file by
file.
Run, in the terminal, which opens in the workspace folder:
python -c "import unigpu; unigpu.detect()" # detection
python -m unigpu.selftest all # self-test on every device
python 03_matrix_multiplication.py sim # an example, on the simulator
Metal (the iPad's GPU): rubicon-objc needs the ctypes module, present
in Code App as in Carnets. If a metal:0 platform appears in the detection,
Metal works. Otherwise, the metal line of "Unavailable backends" gives the
reason (see the end of section 3). The simulator works in any case.
Code App cannot start subprocesses: the pytest suite is not meant for the
iPad. The self-test is its embedded version.
5. iPad: the basic version (simulator)¶
In all these apps, including those based on WebAssembly such as Pyodios, the simulator works. It needs neither threads nor a compiler:
import unigpu as gpu
gpu.select_device("sim")
6. Google Colab: NVIDIA GPU (also works from Safari on an iPad)¶
The notebooks/colab_gpu.ipynb notebook does
everything. It installs unigpu with pip, detects the hardware, launches a
first CUDA kernel, enables OpenCL on the NVIDIA GPU, runs the self-test on
every device, then the examples, which it fetches from the documentation
site.
- Open the notebook in Colab: download
colab_gpu.ipynbfrom the documentation site, then, in Colab, File → Upload notebook. - GPU: Runtime → Change runtime type → GPU T4.
- Runtime → Run all. If Colab offers to restart the session after the installation, accept, then resume at the cell after the installation.
Expected results:
gpu.detect()shows thecuda:0platform (Tesla T4). If the NVIDIA ICD could be enabled, the T4 also appears in an "NVIDIA CUDA" OpenCL platform.- The self-test ends, for each device, with a line such as
11/11 checks passed on Tesla T4.
Every example and performance measurements: open
examples.ipynb the same way, with a T4 GPU.
It installs unigpu and fetches the examples itself. It runs each example,
then measures it for growing sizes, with tables and charts. Finally, it
compares CUDA, OpenCL and the simulator on the same GPU.
Good to know: Colab ships CUDA 13 (PyTorch, RAPIDS). The notebooks
therefore install the cuda13 option, whose CUDA libraries are those
already there: the cuda option, CUDA 12, would replace them and break
PyTorch for the session. Both options require numpy<2.5, since
numba-cuda 0.30 does not work with NumPy ≥ 2.5.
7. What to send back¶
- iPad: everything the detection and the self-test print (or the
ipad.ipynbnotebook). With Code App, the detection and the self-test, and the result of the NumPy check (section 4). - Colab: the output of
gpu.detect()and of the self-test.
On failure, the self-test prints the error message of each check. For Metal, the compiler's error comes with the generated code, line by line: this information is usually enough to fix the problem.
Known limits of Metal (Mac and iPad)¶
- no
float64: usefloat32(the translator says so); - no
print()in Metal kernels (use the simulator to debug); - at most 31 buffers per kernel, each array counting as 1 + ndim.