# `Dala.ML`
[🔗](https://github.com/manhvu/dala/blob/main/lib/dala/ml.ex#L1)

Unified ML API for Dala apps on iOS and Android.

Single entry point for machine learning in Dala, integrating:

- **Nx** - Core tensor library (pure Elixir, works everywhere)
- **Scholar** - Traditional ML (regression, clustering, SVM, etc.)
- **NxSignal** - Digital signal processing (audio, time series)
- **Axon** - Neural networks (deep learning)
- **EMLX** - Apple Silicon GPU acceleration (iOS only, auto-configured)
- **CoreML** - iOS-native ML framework (Neural Engine, iOS only)
- **ONNX Runtime** - Cross-platform inference engine (iOS/Android)
- **GPU Compute (EXCubeCL)** - GPU compute via CubeCL (iOS/Android/desktop)
- **ExBurn (Burn)** - Burn deep learning framework via Rust NIF (Metal/Vulkan/CUDA)

## Quick Start

    # Zero-config setup (call once at app startup)
    Dala.ML.setup()

    # Now use any of the integrated libraries
    tensor = Nx.tensor([1.0, 2.0, 3.0])
    Nx.sum(tensor)

## GPU Compute

For heavy workloads (image processing, custom kernels, realtime effects),
use the GPU compute backend via EXCubeCL:

    # Check GPU availability
    Dala.Gpu.Compute.device_info()

    # Create buffers and run kernels
    a = Dala.Gpu.Compute.buffer([1.0, 2.0, 3.0], {3}, :f32)
    b = Dala.Gpu.Compute.buffer([4.0, 5.0, 6.0], {3}, :f32)
    c = Dala.Gpu.Compute.buffer_zeros({3}, :f32)
    Dala.Gpu.Compute.add(a, b, c)
    Dala.Gpu.Compute.read(c)

## Platform Support

| Library | iOS Device | iOS Sim | Android |
|---------|-----------|--------|---------|
| Nx | ✅ | ✅ | ✅ |
| Scholar | ✅ | ✅ | ✅ |
| NxSignal | ✅ | ✅ | ✅ |
| Axon | ✅ | ✅ | ✅ |
| EMLX (GPU) | ✅ | ✅ | ❌ |
| CoreML | ✅ | ✅ | ❌ |
| ONNX Runtime | ✅ | ✅ | ✅ |
| GPU Compute (CubeCL) | ✅ | ✅ | ✅ |
| ExBurn (Burn) | ✅ | ✅ | ✅ |

# `android?`

```elixir
@spec android?() :: boolean()
```

Returns `true` if running on Android.

# `available_backends`

```elixir
@spec available_backends() :: [atom()]
```

Returns a list of available ML backends for the current platform.

# `benchmark`

```elixir
@spec benchmark(keyword()) :: map()
```

Benchmarks a simple ML operation on the current backend.

Returns `%{time_ms: float, backend: term, gflops: float}`.

# `burn_smoke_test`

```elixir
@spec burn_smoke_test() :: :ok | {:error, String.t()}
```

Runs a smoke test of the ExBurn pipeline.

Returns `:ok` if ExBurn is working, or `{:error, reason}` if not.

# `burn_summary`

```elixir
@spec burn_summary() :: String.t()
```

Returns a formatted summary of the ExBurn environment.

# `enable_burn_defn!`

```elixir
@spec enable_burn_defn!() :: :ok
```

Enables the ExBurn defn compiler for GPU-accelerated Nx.Defn expressions.

After calling this, all `defn` functions will be compiled through
ExBurn's custom defn compiler and executed on the GPU via Burn.

# `ios?`

```elixir
@spec ios?() :: boolean()
```

Returns `true` if running on any iOS platform (device or simulator).

# `ios_device?`

```elixir
@spec ios_device?() :: boolean()
```

Returns `true` if running on a real iOS device (not simulator).

# `ios_simulator?`

```elixir
@spec ios_simulator?() :: boolean()
```

Returns `true` if running in iOS Simulator.

# `nx_available?`

```elixir
@spec nx_available?() :: boolean()
```

Returns `true` if Nx is loaded (i.e. the app depends on `:nx`).

# `predict`

```elixir
@spec predict(term(), term()) :: {:ok, term()} | {:error, term()}
```

Runs inference using the best available backend.

Dispatches based on model type:
- Binary (string) on iOS → CoreML
- Integer → ONNX session
- Tuple → Axon model

# `setup`

```elixir
@spec setup() :: :ok | {:error, :nx_not_available}
```

Sets up the ML stack for the current platform.

Call once at app startup. Detects platform and configures the best
available backend automatically.

Returns `{:error, :nx_not_available}` if Nx is not a dependency of the app.

# `status`

```elixir
@spec status() :: map()
```

Gets the current ML stack status.

# `verify`

```elixir
@spec verify() :: map()
```

Quick verification that the ML stack is working.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
