Implementation:Microsoft Onnxruntime OnnxMap
Appearance
| Knowledge Sources | Description |
|---|---|
| Source File | java/src/main/java/ai/onnxruntime/OnnxMap.java |
| Repository | Microsoft/onnxruntime |
Domains
- Machine Learning Runtime
- Map Value Container
- JNI Native Interop
Overview
OnnxMap is a container for map values returned by OrtSession.run(). It wraps a native ORT map value and provides access to the underlying key-value pairs. Supported key types are String and Long; supported value types are String, Long, Float, and Double. The class implements the OnnxValue interface and contains the OnnxMapValueType inner enum.
Description
The OnnxMap class provides:
- Value extraction:
getValue()returns aMap<Object, Object>containing all entries. Keys and values are boxed as needed. - Size:
size()returns the number of entries from theMapInfo. - Type info:
getInfo()returns theMapInfodescribing key and value types. - Native key extraction: Private native methods
getStringKeysandgetLongKeys. - Native value extraction: Private native methods
getStringValues,getLongValues,getFloatValues,getDoubleValues. - Lifecycle:
close()releases native memory;isClosed()checks state.
The inner enum OnnxMapValueType defines INVALID, STRING, LONG, FLOAT, DOUBLE value types with mapping methods.
Code Reference
Source Location
// File: java/src/main/java/ai/onnxruntime/OnnxMap.java
// Package: ai.onnxruntime
Signature
public class OnnxMap implements OnnxValue {
public int size();
public OnnxValueType getType();
public Map<? extends Object, ? extends Object> getValue() throws OrtException;
public MapInfo getInfo();
public synchronized boolean isClosed();
public synchronized void close();
public enum OnnxMapValueType {
INVALID(0), STRING(1), LONG(2), FLOAT(3), DOUBLE(4);
public static OnnxMapValueType mapFromInt(int value);
public static OnnxMapValueType mapFromOnnxJavaType(OnnxJavaType type);
}
}
Import
import ai.onnxruntime.OnnxMap;
I/O Contract
Inputs
| Name | Type | Description |
|---|---|---|
| nativeHandle | long | Pointer to the native ORT map value (set internally) |
| allocatorHandle | long | Pointer to the allocator that created the map |
| info | MapInfo | Type information about keys and values |
Outputs
| Name | Type | Description |
|---|---|---|
| getValue() | Map<?, ?> | Java HashMap containing all key-value pairs |
| size() | int | Number of entries in the map |
| getInfo() | MapInfo | Map type metadata |
Usage Examples
import ai.onnxruntime.*;
import java.util.*;
OrtEnvironment env = OrtEnvironment.getEnvironment();
try (OrtSession session = env.createSession("/path/to/model.onnx")) {
Map<String, OnnxTensorLike> inputs = new HashMap<>();
// ... prepare inputs ...
try (OrtSession.Result results = session.run(inputs)) {
// If output is a map
OnnxValue output = results.get(0);
if (output.getType() == OnnxValue.OnnxValueType.ONNX_TYPE_MAP) {
OnnxMap map = (OnnxMap) output;
Map<?, ?> javaMap = map.getValue();
System.out.println("Map size: " + map.size());
System.out.println("Key type: " + map.getInfo().keyType);
System.out.println("Value type: " + map.getInfo().valueType);
for (Map.Entry<?, ?> entry : javaMap.entrySet()) {
System.out.println(entry.getKey() + " -> " + entry.getValue());
}
}
}
}
Related Pages
- MapInfo.java - Type metadata for OnnxMap
- OnnxSequence.java - Sequence container that may contain maps
- OrtSession.java - Session that produces OnnxMap outputs
- OnnxJavaType.java - Type enum used for key/value types
Page Connections
Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment