Guide · Tools

Packages, PyTorch and Core ML

pip per project, everything bundled, npm with esbuild, PyTorch and pythilo.coreml.

Bundled with the app

These ship inside Pythilo, compiled for iOS where they need to be, and work offline in every project:

AreaPackages
WebDjango, Flask, FastAPI, Pydantic, Starlette, Uvicorn, Werkzeug, Jinja2
Science and MLNumPy, PyTorch, SymPy, NetworkX
HTTPRequests, httpx
Toolspytest, pip, Jedi, pyflakes, Pygments

Their own dependencies come along too. pip list shows every bundled package and its version, and Open-source licences lists them all with their licences.

pip

pip install name downloads wheels from PyPI and installs them into the project's .pythilo/site-packages folder, which comes first on the path when the project runs. Every project has its own, so two projects can use different versions, and nothing is installed for the whole app.

The Packages screen

Open Packages from the file drawer or the ⋯ menu. Type a Package name and tap Install. In this project lists what you installed, each with a button to remove it. Bundled with Pythilo, work offline lists the main bundled packages.

PyTorch

PyTorch 2.15 is built for iOS from source by torch-ios: the full library with autograd and the Python API, running on the CPU with matrix maths through Apple's Accelerate. Training, torch.func, torch.jit, DataLoader, saving and loading all work. The PyTorch template trains a small network on the device, and the machine learning course uses it throughout.

There is no MPS (GPU) backend, no torch.compile and no DataLoader workers: keep num_workers=0.

The terminal in dark mode after python main.py trained a PyTorch network to 97 percent test accuracyTerminal output comparing Core ML timings on CPU, GPU and all compute units
The PyTorch template training on the device, and Core ML timings.

Core ML and the Neural Engine

pythilo.coreml runs Core ML models (.mlpackage or .mlmodel) from Python, with NumPy arrays in and out:

from pythilo import coreml

print(coreml.devices())
model = coreml.load("model.mlpackage", compute="ane")   # cpu, gpu, ane or all
outputs = model.predict({"input": array})

rows = coreml.benchmark()          # the built-in sample model on every compute unit
print(coreml.report(rows))

benchmark loads the model on each compute unit, times the first and the median prediction, and checks the results agree with the CPU. The Neural Engine template shows it end to end. The simulator has no Neural Engine, so its numbers only mean something on a real device.

npm and esbuild

There is no Node on iOS, but Pythilo has its own npm for installing and esbuild for building:

The React and FastAPI template puts it together: npm install, npm run build, then uvicorn main:app serves the built site and its API.

Network access is needed only to download: pip from PyPI and npm from the npm registry. Everything bundled works offline.