Melange
API ReferenceAndroid

ZeticMLangeModel

API reference for general model inference on Android.

This page describes the API planned for ZeticMLange Android 1.13.0.

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

Import

import com.zeticai.mlange.core.model.ZeticMLangeModel

Constructor

ZeticMLangeModel(
    context: Context,
    personalKey: String,
    name: String,
    version: Int? = null,
    modelMode: ModelMode = ModelMode.RUN_AUTO,
    onDownload: ((Float) -> Unit)? = null,
    cacheHandlingPolicy: ModelCacheHandlingPolicy = ModelCacheHandlingPolicy.REMOVE_OVERLAPPING,
)
ParameterTypeDefaultDescription
contextContext-Android context used for cache and file access.
personalKeyString-Personal key for accessing the model.
nameString-Model name in account_name/project_name format.
versionInt?nullModel version. null loads the latest version.
modelModeModelModeRUN_AUTOBackend selection strategy.
onDownload((Float) -> Unit)?nullDownload progress callback from 0.0 to 1.0.
cacheHandlingPolicyModelCacheHandlingPolicyREMOVE_OVERLAPPINGManaged artifact cache cleanup policy.
val model = ZeticMLangeModel(
    context = context,
    personalKey = PERSONAL_KEY,
    name = "account_name/project_name",
    modelMode = ModelMode.RUN_AUTO,
)

run(inputs)

Runs inference with tensors matching the model input order and expected shapes and data types. If inputs is empty, the runtime uses the model's input buffers.

fun run(inputs: Array<Tensor> = emptyArray()): Array<Tensor>
ParameterTypeDescription
inputsArray<Tensor>Input tensors matching the model's expected shapes and data types.

Returns: output tensors in model output order.

val outputs = model.run(arrayOf(inputTensor))
val firstOutput = outputs[0]

Input Buffers

getInputBuffers() returns the model's input tensors in model input order. Use these buffers when writing directly into the model-owned input storage, then call run() with no arguments. Match each buffer's shape and data type to the model metadata, and do not use the buffers after closing the model.

fun getInputBuffers(): Array<Tensor>

Background Downloads

Schedule a durable model download without constructing a runtime instance. Keep the returned handle to observe or stop the job after the app moves to the background.

fun downloadInBackground(
    context: Context,
    personalKey: String,
    name: String,
    version: Int? = null,
    modelMode: ModelMode = ModelMode.RUN_AUTO,
    cacheHandlingPolicy: ModelCacheHandlingPolicy = ModelCacheHandlingPolicy.KEEP_EXISTING,
): BackgroundDownloadHandle

fun getBackgroundDownloadStatus(
    context: Context,
    handle: BackgroundDownloadHandle,
): BackgroundDownloadStatus

fun stopBackgroundDownload(
    context: Context,
    handle: BackgroundDownloadHandle,
): BackgroundDownloadStopResult

fun removeDownloadedModel(
    context: Context,
    name: String,
    version: Int? = null,
): ModelRemovalResult
val handle = ZeticMLangeModel.downloadInBackground(
    context = context,
    personalKey = PERSONAL_KEY,
    name = "account_name/project_name",
)

val status = ZeticMLangeModel.getBackgroundDownloadStatus(context, handle)
val stopResult = ZeticMLangeModel.stopBackgroundDownload(context, handle)

val removal = ZeticMLangeModel.removeDownloadedModel(
    context = context,
    name = "account_name/project_name",
)
if (removal.isInUse) {
    // Close active instances, then retry removal.
}

Stopping preserves an already installed model. removeDownloadedModel stops active downloads for the selected model before removing its managed artifacts. See Cache Management for states and result values.

Lifecycle

val isClosed: Boolean
fun close()

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

model.close()

On this page