Dala.Ml.Nx (dala v0.0.12)

Copy Markdown View Source

Nx integration helpers for Dala.

Nx itself is pure Elixir and works on any platform. This module provides backend selection logic and inference helpers.

Summary

Functions

Checks if Axon is available.

Returns the default backend for the current iOS platform.

Checks if EMLX is available.

Example: Simple neural network layer using Axon.

Runs inference with an Axon model using the best available backend.

Initializes Nx with the best available backend for the current platform.

init_for_ios() deprecated

Initializes Nx with the best available backend for iOS.

Loads a pre-trained Axon model for inference.

Creates a tensor on iOS with platform-appropriate backend.

Functions

axon_available?()

Checks if Axon is available.

default_backend()

Returns the default backend for the current iOS platform.

emlx_available?()

Checks if EMLX is available.

example_dense_layer()

Example: Simple neural network layer using Axon.

Axon is now a direct dependency - here's how to use it:

model =
  Axon.input("input", shape: {nil, 784})
  |> Axon.dense(128, activation: :relu)
  |> Axon.dense(10, activation: :softmax)

{init_fn, predict_fn} = Axon.build(model)
params = init_fn.(Nx.template({1, 784}, :f32), %{})
predict_fn.(params, input_tensor)

inference(model, params, input_data)

Runs inference with an Axon model using the best available backend.

Example

{:ok, model, params} = Dala.ML.Nx.load_model("model.axmodel")
input = Nx.tensor([[1.0, 2.0, 3.0]])
result = Dala.ML.Nx.inference(model, params, input)

init()

Initializes Nx with the best available backend for the current platform.

Priority:

  1. EMLX (if available) - best for Apple Silicon
  2. Nx.BinaryBackend (pure Elixir fallback)

Works on all platforms, not just iOS. Also see Dala.ML for the unified entry point.

init_for_ios()

This function is deprecated. Use init/0 instead.

Initializes Nx with the best available backend for iOS.

Deprecated: Use init/0 instead, which works on all platforms.

load_model(model_path)

Loads a pre-trained Axon model for inference.

Note: Check Axon documentation for the current serialization API. Axon models can be saved/loaded using Axon's built-in serialization.

tensor(data, opts \\ [])

Creates a tensor on iOS with platform-appropriate backend.