Apple's Machine Learning Framework for Apple Silicon - Made Simple
This project is your complete guide to MLX, Apple's blazing-fast machine learning framework optimized for Apple Silicon. From zero to neural networks in minutes!
# Jump in and see MLX magic
cd mlx-101
source mlx-env/bin/activate
python mlx_demo.py # ๐ See MLX in action!What just happened? You just ran machine learning operations on Apple Silicon GPU using unified memory! ๐คฏ
๐ฏ Hands-on Examples - Working code for arrays, neural networks, and computer vision
๐ Apple Silicon Power - Harness the full potential of M1/M2/M3+ chips
๐ Python & Swift - Learn both language interfaces
๐ง Real Neural Networks - Build and train models from scratch
๐ Interactive Learning - Jupyter notebooks ready to go
๐ง Modern Setup - Fast dependency management with uv
| ๐ฏ Feature | โ Status | ๐ Description |
|---|---|---|
| Python Examples | โ Working | Complete tutorials from basics to neural networks |
| Swift Integration | โ Ready | Type-safe ML with Swift + MLX |
| Neural Networks | โ Implemented | Build networks from scratch |
| Computer Vision | โ Examples | Image processing and CNN basics |
| Jupyter Support | โ Configured | Interactive development environment |
| Fast Dependencies | โ Using uv | Lightning-fast package management |
| Apple Silicon | โ Optimized | Native Metal Performance Shaders |
MLX is an array framework for machine learning research on Apple Silicon. It's designed to be user-friendly, efficient, and flexible, with a Python API that closely follows NumPy and a C++ API. MLX is particularly optimized for Apple Silicon (M1, M2, M3+ chips) and provides unified memory that allows arrays to live in shared memory accessible to both CPU and GPU.
- Hardware: Apple Silicon Mac (M1, M2, M3+)
- OS: macOS 13.5+ (macOS 14+ recommended)
- Python: 3.9+ (native arm64, not x86 via Rosetta)
- Swift: 5.7+ (for Swift examples)
mlx-101/
โโโ README.md # This file
โโโ pyproject.toml # Project configuration and dependencies
โโโ requirements.txt # Frozen dependency versions
โโโ .gitignore # Git ignore rules
โโโ examples/ # Python examples
โ โโโ 01_basic_operations.py
โ โโโ 02_linear_algebra.py
โ โโโ 03_neural_networks.py
โ โโโ 04_image_processing.py
โโโ swift-examples/ # Swift examples (coming soon)
โโโ notebooks/ # Jupyter notebooks
# Clone the repository
git clone https://github.com/suh4s/mlx-101.git
cd mlx-101# Create isolated environment using uv (faster than pip)
uv venv mlx-env
# Activate the environment
source mlx-env/bin/activate# Install MLX and related packages
uv pip install mlx numpy matplotlib jupyter pillow
# Or install from requirements.txt
uv pip install -r requirements.txtpython -c "import mlx.core as mx; print(f'MLX version: {mx.__version__}'); print('MLX is working!')"# Activate environment (run this each time you start)
source mlx-env/bin/activate
# Deactivate when done
deactivate
# Install new packages
uv pip install package_name
# Update requirements after installing new packages
uv pip freeze > requirements.txtReady to dive in? Here's your fast track to MLX awesomeness:
# 1. Jump into your environment
cd mlx-101
source mlx-env/bin/activate
# 2. See MLX in action (30 seconds)
python mlx_demo.py
# 3. Explore detailed examples
python examples/01_basic_operations.py
# 4. Start experimenting
jupyter lab# Start every session
source mlx-env/bin/activate
# Run examples
python examples/01_basic_operations.py # ๐ฏ Arrays & basics
python examples/03_neural_networks.py # ๐ง Neural networks
python mlx_demo.py # โก Quick overview
# Interactive development
jupyter lab # ๐ช Modern interface
jupyter notebook # ๐ Classic interface
# When finished
deactivate# Install new packages (fast with uv!)
uv pip install transformers torch
# Keep track of dependencies
uv pip freeze > requirements.txt
# Reproduce environment elsewhere
uv pip install -r requirements.txt| Step | File | What You'll Learn | Key Concepts |
|---|---|---|---|
| 1๏ธโฃ | mlx_demo.py |
Quick MLX overview | Basic arrays, GPU acceleration |
| 2๏ธโฃ | 01_basic_operations.py |
Arrays, math, NumPy interop | Array creation, indexing, broadcasting |
| 3๏ธโฃ | 02_linear_algebra.py |
Matrix operations & linear systems | Matrix math, decompositions, solving equations |
| 4๏ธโฃ | 03_neural_networks.py |
Neural networks from scratch | Activations, loss functions, gradients |
| 5๏ธโฃ | 04_image_processing.py |
Computer vision basics | Convolutions, CNNs, image transforms |
| 6๏ธโฃ | 05_langgraph_mlx_guide.py |
AI agents with LangGraph + MLX | Agent workflows, local LLMs, tool integration |
| 7๏ธโฃ | working_examples.py |
Curated robust operations | Production-ready MLX patterns |
| 8๏ธโฃ | notebooks/ |
Interactive experimentation | Jupyter development workflow |
| 9๏ธโฃ | swift-examples/ |
Swift + MLX integration | Type-safe ML with Swift |
# What you'll see in 30 seconds:
โข MLX vs NumPy speed comparison
โข GPU acceleration in action
โข Unified memory demonstration
โข Basic neural network forward pass# Core concepts covered:
โข Array creation (zeros, ones, random, linspace)
โข Mathematical operations (+, -, *, /, power)
โข Array indexing and slicing
โข Broadcasting rules and examples
โข MLX โ NumPy interoperability
โข Memory model and device management# Advanced math operations:
โข Matrix multiplication and transposes
โข Linear system solving (Ax = b)
โข Matrix decompositions (QR, SVD)
โข Vector operations and norms
โข Geometric transformations
โข CPU stream usage for unsupported GPU ops# Neural network essentials:
โข Activation functions (ReLU, Sigmoid, Tanh)
โข Loss functions (MSE, Cross-entropy, MAE)
โข Gradient computation and backpropagation
โข Simple linear regression training
โข Multi-layer perceptron (MLP) example
โข Best practices for neural network training# Computer vision fundamentals:
โข Image creation and manipulation
โข Convolution operations and kernels
โข CNN building blocks (Conv2D, ReLU, MaxPool)
โข Image transformations (flip, rotate, resize)
โข Color space operations (RGB โ Grayscale)
โข Hybrid MLX-NumPy techniques for complex ops# Building intelligent agents with LangGraph + MLX:
โข Agent workflow orchestration with LangGraph
โข Local LLM inference with MLX-LM
โข MLX-powered mathematical reasoning tools
โข Computer vision capabilities for agents
โข Privacy-first, Apple Silicon-optimized AI workflows
โข Real-world use cases and performance benefits# Curated examples that always work:
โข Robust linear algebra operations
โข Reliable image processing patterns
โข Neural network components
โข Performance benchmarks
โข Best practices for real applicationsAfter working through this repository, you now understand:
- Unified Memory Architecture: CPU and GPU share the same memory space - no costly transfers!
- Lazy Evaluation: Operations are automatically fused for maximum efficiency
- Apple Silicon Optimization: Native Metal Performance Shaders provide blazing speed
- NumPy Compatibility: Easy migration path from existing NumPy-based workflows
- Array Operations: Creation, indexing, slicing, and broadcasting in MLX
- Linear Algebra: Matrix operations, decompositions, and solving linear systems
- Neural Networks: Building networks from scratch with proper gradients
- Computer Vision: Image processing, convolutions, and CNN architectures
- Performance Optimization: When to use CPU vs GPU streams for different operations
- Environment Management: Isolated development with
uvand virtual environments - Dependency Tracking: Proper
pyproject.tomlandrequirements.txtmanagement - Error Handling: Graceful fallbacks for GPU-unsupported operations
- Hybrid Approaches: Combining MLX with NumPy for complex operations
- Best Practices: Code patterns that work reliably across different MLX versions
During the development of this repository, we encountered and solved several important MLX compatibility issues:
- Problem: Some linear algebra operations (
mx.linalg.inv,mx.linalg.qr,mx.linalg.solve) not yet supported on GPU - Solution: Use CPU streams with proper evaluation:
with mx.stream(mx.cpu): result = operation(); mx.eval(result) - Impact: All linear algebra examples now work reliably across MLX versions
- Problem: MLX's
.at[].set()method causing'ArrayAt' object has no attribute 'set'errors - Solution: Hybrid approach using NumPy for complex assignments, converting back to MLX
- Impact: Image processing operations like max pooling and resizing now work perfectly
- Problem: Operations like
mx.rot90()don't exist in current MLX versions - Solution: Use NumPy equivalents and convert:
mx.array(np.rot90(np.array(mlx_array))) - Impact: All image transformation examples work seamlessly
- Unified Memory: True zero-copy between CPU and GPU operations
- Lazy Evaluation: Operations are automatically fused for better performance
- Broadcasting: MLX's broadcasting rules closely follow NumPy for familiar behavior
- Vectorization: Always prefer vectorized operations over Python loops
# โ
MLX Strengths: Fast unified memory operations
result = mlx_array @ other_array # GPU-accelerated, no memory transfers
# โ ๏ธ MLX Limitations: Some linalg ops need CPU streams
with mx.stream(mx.cpu):
inv_matrix = mx.linalg.inv(matrix) # Fallback to CPU when needed# โ
Hybrid MLX-NumPy approach for complex operations
mlx_data = mx.array(input_data) # Use MLX for computation
numpy_data = np.array(mlx_data) # Convert for complex assignments
result = mx.array(processed_numpy) # Back to MLX for further processing- Start Simple: Use
working_examples.pyfor guaranteed-working patterns - Understand Limitations: Know when to use CPU streams vs GPU
- Leverage Strengths: Use MLX for heavy computation, NumPy for complex indexing
- Test Incrementally: Build up from basic operations to complex models
# Always handle GPU/CPU compatibility
try:
result = mx.linalg.solve(A, b) # Try GPU first
except:
with mx.stream(mx.cpu): # Fallback to CPU
result = mx.linalg.solve(A, b)
mx.eval(result)# Leverage broadcasting and vectorization
processed = (data - mean) / std # Vectorized normalization
result = mx.sum(processed * weights, axis=1) # Efficient reductions# Clean, extensible neural network patterns
def layer_forward(x, weights, bias):
return mx.maximum(0, x @ weights + bias) # ReLU activation
# Gradient-ready operations
loss = mx.mean((predictions - targets) ** 2) # MSE lossโ
Environment Mastery: Professional development setup with uv and virtual environments
โ
Core Understanding: Deep knowledge of MLX's unified memory and lazy evaluation
โ
Practical Skills: Real examples that work across different MLX versions
โ
Performance Awareness: Know when to use GPU vs CPU streams
โ
Production Patterns: Robust code that handles edge cases gracefully
โ
Multi-Language Ready: Both Python and Swift MLX interfaces understood
โ
Computer Vision Capable: Image processing and CNN implementation skills
โ
Debugging Skills: Can identify and fix common MLX compatibility issues
You're now equipped to build serious machine learning applications on Apple Silicon! ๐
- ๐ Apple Silicon Optimized: Native Metal Performance Shaders for maximum performance
- ๐ง Unified Memory: CPU and GPU share memory seamlessly - no transfers needed!
- โก Lazy Evaluation: Operations are fused automatically for efficiency
- ๐ NumPy Compatible: Easy migration from existing NumPy code
- ๐ฆ Swift Native: Type-safe, performant Swift integration
- ๐ง Auto Differentiation: Ready for gradient-based learning out of the box
- ๐ Fast Package Management: Using
uvinstead of slow pip - ๐ Isolated Environment: No conflicts with system Python packages
- ๐ Rich Examples: Working code you can learn from immediately
- ๐ฏ Multi-language: Both Python and Swift ready to go
- ๐ Comprehensive Docs: Everything documented and explained
- ๐ช Jupyter Ready: Interactive development environment set up
# โ
Good - Vectorized operations
result = a * 2 + b
# โ Avoid - Python loops
result = [a[i] * 2 + b[i] for i in range(len(a))]- Always activate:
source mlx-env/bin/activatebefore starting - Quick test: Run
python mlx_demo.pyto verify everything works - Interactive dev: Use
jupyter labfor experimentation - Keep updated:
uv pip freeze > requirements.txtafter installing packages
- Start simple: Begin with
01_basic_operations.py - Understand by doing: Modify examples and see what happens
- Read the outputs: Examples are heavily commented for learning
- Build incrementally: Start with arrays, then move to neural networks
- MLX arrays live in unified memory - accessible by both CPU and GPU
- Use
mx.array()instead ofnp.array()for MLX operations - Leverage broadcasting for element-wise operations
- Take advantage of lazy evaluation - operations are fused when possible
- Convert between MLX and NumPy easily:
np.array(mlx_array)