Why Use the C++ API?
The C++ API is ideal when you need:- Maximum Performance: Direct C++ access eliminates Python overhead
- Standalone Applications: Build native macOS applications without Python dependencies
- Custom Extensions: Create custom operations and Metal kernels
- Integration: Embed MLX into existing C++ projects
Core Components
The MLX C++ API is organized into several key namespaces:Array Operations
Themlx::core namespace contains all core array operations:
- Array Creation:
zeros(),ones(),arange(),linspace() - Array Manipulation:
reshape(),transpose(),concatenate(),split() - Mathematical Operations:
add(),multiply(),matmul(),exp(),log() - Reductions:
sum(),mean(),max(),min() - Comparison:
equal(),greater(),less()
Additional Modules
mlx::core::random: Random number generationmlx::core::fft: Fast Fourier transformsmlx::core::linalg: Linear algebra operationsmlx::core::fast: Fast custom Metal kernels
Basic Example
Here’s a simple example showing array creation and operations:Key Concepts
Lazy Evaluation
MLX uses lazy evaluation by default. Operations build a computation graph without executing immediately:- Accessing values with
.item<T>() - Printing arrays with
std::cout
Streams and Devices
MLX supports multiple compute devices and streams for parallelism:Data Types
MLX supports various data types:float32, float16, bfloat16, int32, int64, int16, int8, uint32, uint64, uint16, uint8, bool_, complex64
Automatic Differentiation
MLX provides composable function transformations including automatic differentiation:Next Steps
Operations Reference
Complete reference for C++ operations
Usage Guide
How to use MLX in C++ projects
Building Extensions
Create custom C++ extensions
Metal Kernels
Write custom Metal GPU kernels