Melange
API ReferenceiOS

ZeticMLangeModel

API reference for general model inference on iOS.

This page describes the API planned for ZeticMLange iOS 1.13.0.

ZeticMLangeModel loads a general on-device model from the Melange registry and runs tensor inference on iOS.

Import

import ZeticMLange

Initializer

public init(
  personalKey: String,
  name: String,
  version: Int? = nil,
  modelMode: ModelMode = .RUN_AUTO,
  cacheHandlingPolicy: ModelCacheHandlingPolicy = .REMOVE_OVERLAPPING,
  onDownload: ((Float) -> Void)? = nil
) async throws
ParameterTypeDefaultDescription
personalKeyString-Personal key for accessing the model.
nameString-Model name in account_name/project_name format.
versionInt?nilModel version. nil loads the latest version.
modelModeModelMode.RUN_AUTOBackend selection strategy.
cacheHandlingPolicyModelCacheHandlingPolicy.REMOVE_OVERLAPPINGManaged artifact cache cleanup policy.
onDownload((Float) -> Void)?nilDownload progress callback from 0.0 to 1.0.
let model = try await ZeticMLangeModel(
  personalKey: PERSONAL_KEY,
  name: "account_name/project_name"
)

run(inputs:)

inputTensors describes the expected inputs, including each tensor's name, shape, and data type. Pass inputs to run(inputs:) in model input order and with matching shapes and data types.

public var inputTensors: [TensorInfo] { get }
public func run(inputs: [Tensor]) throws -> [Tensor]
ParameterTypeDescription
inputs[Tensor]Input tensors matching the model's expected shapes and data types.

Returns: output tensors in model output order.

let outputs = try model.run(inputs: [inputTensor])
let firstOutput = outputs[0]

getOutputTensors() returns the outputs from the most recent successful inference.

public func getOutputTensors() -> [Tensor]

Load a local Core ML model

For a Core ML model already available as a local file, use fromLocalCoreml. This public factory method does not fetch a model from the Melange registry.

public static func fromLocalCoreml(
  modelKey: String,
  modelPath: URL,
  apType: APType = .CPU
) throws -> ZeticMLangeModel

modelKey identifies this local instance inside the SDK; it does not refer to a registry model. modelPath is a file URL to the Core ML model. The default compute-unit preference is .CPU.

let model = try ZeticMLangeModel.fromLocalCoreml(
  modelKey: "bundled-model",
  modelPath: modelURL
)
defer { model.close() }

Background Downloads

Schedule a general-model download without constructing a runtime instance. Await this call while the app is foregrounded so iOS can register the background URL-session task before the app moves to the background.

static func downloadInBackground(
  personalKey: String,
  name: String,
  version: Int? = nil,
  modelMode: ModelMode = .RUN_AUTO,
  cacheHandlingPolicy: ModelCacheHandlingPolicy = .KEEP_EXISTING
) async throws -> BackgroundDownloadHandle

static func getBackgroundDownloadStatus(
  _ handle: BackgroundDownloadHandle
) async throws -> BackgroundDownloadStatus

static func stopBackgroundDownload(
  _ handle: BackgroundDownloadHandle
) async throws -> BackgroundDownloadStopResult

static func removeDownloadedModel(
  name: String,
  version: Int? = nil
) async throws -> ModelRemovalResult
let handle = try await ZeticMLangeModel.downloadInBackground(
  personalKey: PERSONAL_KEY,
  name: "account_name/project_name"
)

let status = try await ZeticMLangeModel.getBackgroundDownloadStatus(handle)
let stopResult = try await ZeticMLangeModel.stopBackgroundDownload(handle)
let removal = try await ZeticMLangeModel.removeDownloadedModel(
  name: "account_name/project_name"
)

Forward application(_:handleEventsForBackgroundURLSession:completionHandler:) to ZeticMLangeModel.handleBackgroundURLSessionEvents(...) so a relaunched app can finish verification and installation. See iOS advanced configuration for the host-app setup. Stopping preserves an already installed model; if removal.isInUse is true, close active instances and retry removal.

Lifecycle

public private(set) var isClosed: Bool
public func close()

Call close() when the model is no longer needed.

model.close()

On this page