Melange
API ReferenceAndroid

ZeticMLangeModel

Complete API reference for the ZeticMLangeModel class on Android.

The ZeticMLangeModel class is the primary interface for running on-device AI inference on Android. It handles model downloading, NPU context initialization, and hardware-accelerated execution through a single unified API.

Package

com.zeticai.mlange.core.model

Import

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

Constructor

ZeticMLangeModel(context, personalKey, modelKey)

Creates a new model instance, downloading the optimized binary if needed and initializing the NPU execution context.

ParameterTypeDescription
contextContextThe Android application or activity context. Used for file storage and native library loading.
personalKeyStringYour personal authentication key from the Melange Dashboard.
modelKeyStringThe unique identifier for your compiled model (e.g., "Steve/YOLOv11_comparison").

Throws: RuntimeException if the model cannot be downloaded or the NPU context fails to initialize.

val model = ZeticMLangeModel(this, "YOUR_PERSONAL_KEY", "YOUR_MODEL_KEY")

The constructor performs a network call on first use to download the model binary. Ensure you call it from a background thread or handle threading appropriately. The binary is cached locally after the first download.


Methods

run(inputs)

Executes inference on the loaded model using the provided input tensors.

fun run(inputs: Array<Tensor>): Array<Tensor>
ParameterTypeDescription
inputsArray<Tensor>An array of input tensors matching the model's expected input shapes and data types.

Returns: Array<Tensor>: The model's output tensors.

Throws: RuntimeException if input shapes do not match the model's expected inputs, or if inference execution fails.

val outputs = model.run(inputs)

Properties

outputBuffers

Access the raw output buffers from the most recent inference execution.

val outputBuffers: Array<ByteBuffer>

This property provides direct access to the underlying output buffers after run() has been called. Useful when you need zero-copy access to output data for custom post-processing.


Full Working Example

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

class MainActivity : AppCompatActivity() {
    private lateinit var model: ZeticMLangeModel

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        // (1) Load model
        // Downloads the optimized binary on first run, then caches locally
        model = ZeticMLangeModel(this, PERSONAL_KEY, "Steve/YOLOv11_comparison")

        // (2) Prepare model inputs
        // Ensure input shapes match your model's requirement (e.g., Float32 arrays)
        val inputs: Array<Tensor> = // Prepare your inputs

        // (3) Run Inference
        // Executes the fully automated hardware graph.
        // No manual delegate configuration or memory syncing required.
        val outputs = model.run(inputs)

        // (4) Process outputs
        // outputs is an Array<Tensor> containing the model's results
        for (output in outputs) {
            // Process each output tensor
        }
    }
}

Always ensure your input tensor shapes exactly match what the model expects. A shape mismatch will throw a RuntimeException. Check the model's input specification on the Melange Dashboard.


Gradle Setup

The ZeticMLangeModel class requires the Melange AAR dependency. Add the following to your app-level build.gradle:

android {
    ...
    packagingOptions {
        jniLibs {
            useLegacyPackaging true
        }
    }
}

dependencies {
    implementation("com.zeticai.mlange:mlange:+")
}

The useLegacyPackaging true setting is required. Without it, the native JNI libraries for NPU acceleration will not load correctly. See Common Errors for details.


See Also