Ruby API Reference#
This page summarizes the Ruby-facing API surface for MLX Ruby.
The runtime entry point is:
require "mlx"
mx = MLX::Core
nn = MLX::NN
optim = MLX::Optimizers
Core Operations#
Core tensor and math operations live in MLX::Core.
Representative groups include:
Array creation and shape ops:
array,arange,reshape,transpose,concatenate,stackMath ops:
add,subtract,multiply,divide,exp,log,sqrtReductions/statistics:
sum,mean,max,min,argmax,argminDevice/memory helpers:
cpu,gpu,default_device,set_default_device
See implementation:
lib/mlx/core.rbext/mlx/native.cpp
Function Transforms#
MLX Ruby exposes higher-order transformations via MLX::Core and MLX::NN.
MLX::Core.gradMLX::Core.value_and_gradMLX::Core.vmapMLX::Core.jvpMLX::Core.vjpMLX::Core.compileMLX::NN.value_and_grad(module-parameter convenience wrapper)
Neural Network Module#
The base class for models and layers is MLX::NN::Module.
Key methods:
Parameter and state handling:
parameters,trainable_parameters,state,updateMode control:
train,evalPersistence:
save_weights,load_weights
Important pattern: register trainable members with self.<name> = ... so
they are tracked by module state and optimizer updates.
See implementation:
lib/mlx/nn/base.rblib/mlx/nn/layers/*.rb
Ruby DSL#
Ruby DSL now lives in a separate top-level docs section.
See Ruby DSL for the complete DSL reference, including model declaration, builder/graph composition, training, data pipelines, experiments, and split plans.
Optimizers#
Optimizers are under MLX::Optimizers.
Common classes:
SGDAdamAdamWRMSpropAdagradAdaDeltaAdamaxLionAdafactorMuon
Schedulers are under MLX::Optimizers::Schedulers.
See implementation:
lib/mlx/optimizers/optimizers.rblib/mlx/optimizers/schedulers.rb
Export and Import#
MLX Ruby can serialize and load compiled function artifacts.
Core entry points in MLX::Core:
export_functionimport_functionexportersave/loadsavez/savez_compressed
Distributed#
Distributed helpers are exposed via:
MLX::Coredistributed collectives (native bindings)MLX::DistributedUtilsruntime/config tooling
See implementation:
lib/mlx/distributed_utils/common.rblib/mlx/distributed_utils/config.rblib/mlx/distributed_utils/launch.rb