Dala.ML.CoreML (dala v0.6.0)

Copy Markdown View Source

CoreML integration for iOS.

Provides an Elixir API for Apple's CoreML framework via NIF calls. CoreML uses the Apple Neural Engine (ANE) for hardware-accelerated ML inference on iOS devices and simulators.

All NIF functions run on the dirty CPU scheduler.

Prerequisites

  • iOS device or simulator
  • CoreML model file (.mlmodel or .mlpackage)

Usage

# Load a model
:ok = Dala.ML.CoreML.load_model("/path/to/model.mlmodel", "my_model")

# Check if loaded
true = Dala.ML.CoreML.loaded?("my_model")

# Make prediction
{:ok, result_json} = Dala.ML.CoreML.predict("my_model", %{
  "input": [1.0, 2.0, 3.0]
})

# Unload when done
:ok = Dala.ML.CoreML.unload_model("my_model")

Summary

Functions

Loads a CoreML model from the given path.

Checks if a model is loaded.

Lists all loaded model identifiers.

Makes a prediction using a loaded model.

Run prediction on an already-loaded model.

Unloads a previously loaded model.

Functions

load_model(model_path, identifier)

@spec load_model(String.t(), String.t()) :: :ok | {:error, term()} | :not_supported

Loads a CoreML model from the given path.

Parameters

  • model_path: Path to the .mlmodel or .mlpackage file
  • identifier: A unique identifier for this model

Returns

  • :ok on success
  • {:error, reason} on failure
  • :not_supported on non-iOS platforms

loaded?(identifier)

@spec loaded?(String.t()) :: boolean()

Checks if a model is loaded.

Returns true if loaded, false otherwise. Returns false on non-iOS platforms.

loaded_models()

@spec loaded_models() :: [String.t()]

Lists all loaded model identifiers.

predict(identifier, inputs)

@spec predict(String.t(), map()) ::
  {:ok, String.t()} | {:error, term()} | :not_supported

Makes a prediction using a loaded model.

Parameters

  • identifier: The model identifier
  • inputs: A map of input names to values (numbers, strings, lists)

Returns

  • {:ok, result_json} on success
  • {:error, reason} on failure
  • :not_supported on non-iOS platforms

predict_with_loaded_model(identifier, inputs)

@spec predict_with_loaded_model(String.t(), map()) ::
  {:ok, String.t()} | {:error, term()} | :not_supported

Run prediction on an already-loaded model.

Unlike load_model/2 + predict/2, this does NOT load the model. The model must be loaded first via load_model/2.

unload_model(identifier)

@spec unload_model(String.t()) :: :ok | :not_supported

Unloads a previously loaded model.