Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Microsoft Onnxruntime OrtLoraAdapter

From Leeroopedia
Revision as of 10:46, 27 September 2026 by Agent (talk | contribs) (Sync from local file)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)


Knowledge Sources Description
Source File java/src/main/java/ai/onnxruntime/OrtLoraAdapter.java
Repository Microsoft/onnxruntime

Domains

  • Machine Learning Runtime
  • LoRA Fine-Tuning Adapters

Overview

OrtLoraAdapter is a final class that wraps a LoRA (Low-Rank Adaptation) adapter for use with ONNX Runtime inference sessions. It provides factory methods to create adapters from file paths, byte arrays, or direct ByteBuffers. Once created, an adapter can be activated on a specific run via OrtSession.RunOptions.addActiveLoraAdapter(). The class implements AutoCloseable.

Description

Factory methods:

  • create(String adapterPath): Loads a LoRA adapter from a file path (memory-mapped).
  • create(byte[] loraArray): Creates from a byte array.
  • create(ByteBuffer loraBuffer): Creates from a direct ByteBuffer. Non-direct buffers are rejected.

Package-private overloads accept an optional OrtAllocator to copy adapter parameters to allocator memory.

The adapter holds a native handle released by close(). Calling methods after close throws IllegalStateException.

Code Reference

Source Location

// File: java/src/main/java/ai/onnxruntime/OrtLoraAdapter.java
// Package: ai.onnxruntime

Signature

public final class OrtLoraAdapter implements AutoCloseable {
    public static OrtLoraAdapter create(byte[] loraArray) throws OrtException;
    public static OrtLoraAdapter create(ByteBuffer loraBuffer) throws OrtException;
    public static OrtLoraAdapter create(String adapterPath) throws OrtException;
    public void close();
}

Import

import ai.onnxruntime.OrtLoraAdapter;

I/O Contract

Inputs

Name Type Description
adapterPath String File path to the LoRA adapter (memory-mapped)
loraArray byte[] LoRA adapter data in a byte array
loraBuffer ByteBuffer Direct ByteBuffer containing LoRA adapter data

Outputs

Name Type Description
OrtLoraAdapter OrtLoraAdapter The loaded LoRA adapter wrapper

Usage Examples

import ai.onnxruntime.*;

OrtEnvironment env = OrtEnvironment.getEnvironment();

// Load a LoRA adapter from disk
try (OrtLoraAdapter adapter = OrtLoraAdapter.create("/path/to/lora_adapter.onnx_adapter")) {
    try (OrtSession session = env.createSession("/path/to/base_model.onnx")) {
        // Activate the adapter for a specific run
        try (OrtSession.RunOptions runOptions = new OrtSession.RunOptions()) {
            runOptions.addActiveLoraAdapter(adapter);

            java.util.Map<String, OnnxTensorLike> inputs = new java.util.HashMap<>();
            // ... prepare inputs ...

            try (OrtSession.Result results = session.run(inputs,
                    session.getOutputNames(), java.util.Collections.emptyMap(), runOptions)) {
                // Process results with LoRA-adapted model
            }
        }
    }
}

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment