Ruby DSL Examples#
Runnable examples in this repository:
examples/dsl/streaming_factory.rbexamples/dsl/validation_monitor.rbexamples/dsl/memory_friendly_reporting.rbexamples/dsl/collate_schemas.rbexamples/dsl/compile_sync_and_native_checkpoint.rb
Suggested reading path:
Start with
streaming_factory.rbandvalidation_monitor.rb.Review
collate_schemas.rbfor data wiring patterns.Review
compile_sync_and_native_checkpoint.rbfor compile/sync and persistence.Use
memory_friendly_reporting.rbfor long-running report ergonomics.
require "mlx"
# Mirrors the structure used by examples/dsl/validation_monitor.rb
trainer = model.trainer(optimizer: optimizer) do |x:, y:|
MLX::NN.cross_entropy(model.call(x), y, reduction: "mean")
end
report = trainer.fit_report(
train_data,
epochs: 3,
validation_data: validation_data,
monitor: :loss
)
puts report[:best_monitor]
Generation helper example:
generator = MLX::DSL::Generate.new(
model: model,
tokenizer: tokenizer,
eos_id: tokenizer.eos_id,
sampler: { strategy: :argmax },
mode: :decoder_only
)
generator.each_token(prompt: "Hello", max_tokens: 32) do |_token_id, chunk|
print chunk if chunk
end