MLX - Day 1: Getting started

April 23, 20243 min read#swift, #ios, #mlx, #llm

Following my post yesterday to introduce you about my motivation to learn MLX, I’ll start writing a serie of MLX tutorials. My goal is to explore different aspect of MLX everyday in the next 30 days.

My target audiences are iOS developers, who might not have much experiences with other programming languages such as Python (like me). Hence the tutorials are written with as much details and as simple as possible.

Setup Python environment

Dependening on your own local setup, you might need to install python to get the latest python version.

I’m doing that with brew on my macOS Sonoma system

~ brew install python

After python is installed successfully, you can verify it:

~ which python3
/opt/homebrew/bin/python3

Install MLX

If you are new to Python, it’s recommended to create a virtual environment for your project. A virtual environment is a tool that helps to keep dependencies required by different projects separate by creating isolated Python virtual environments for them. This is one of the most important tools that most Python developers use.

Step 1: Create a directory to host our virtual env

~ mkdir mlx
~ cd mlx

Step 2: Create and activate a virtual env

~ python3 -m venv env
~ source env/bin/activate

Step 3: Install MLX

pip3 install mlx

Simple usage

Now we can start testing if the package is installed properly by following the quick start guide

(env) ➜  mlx python
Python 3.12.2 (main, Feb  6 2024, 20:19:44) [Clang 15.0.0 (clang-1500.1.0.2.5)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import mlx.core as mx
>>> a = mx.array([1, 2, 3, 4])
>>> a.shape
(4,)
>>> a.dtype
mlx.core.int32
>>> b = mx.array([1.0, 2.0, 3.0, 4.0])
>>> b.dtype
mlx.core.float32

MLX is now setup properly in our virtual environment. We will use this setup in the future for further tasks.

In the next blog, we will start exploring some Swift APIs from MLX, which can be used to build on-device machine learning apps using the power of MLX library.

Please let me know in the comment section what you think about this tutorial. I really appreciate your feedback so that I can make this tutorial serie useful for as many Swift devleopers as possible 🙏.

Quick Drop logo

Profile picture

Personal blog by An Tran. I'm focusing on creating useful apps.
#Swift #Kotlin #Mobile #MachineLearning #Minimalist


© An Tran - 2024