Class ONNXExternalModel<T extends Output<T>>
- All Implemented Interfaces:
com.oracle.labs.mlrg.olcut.provenance.Provenancable<ModelProvenance>
,Serializable
,AutoCloseable
,ProtoSerializable<org.tribuo.protos.core.ModelProto>
N.B. ONNX support is experimental, and may change without a major version bump.
- See Also:
-
Field Summary
Modifier and TypeFieldDescriptionstatic final int
Protobuf serialization version.Fields inherited from class org.tribuo.interop.ExternalModel
DEFAULT_BATCH_SIZE, featureBackwardMapping, featureForwardMapping
Fields inherited from class org.tribuo.Model
ALL_OUTPUTS, BIAS_FEATURE, featureIDMap, generatesProbabilities, name, outputIDInfo, provenance, provenanceOutput
Fields inherited from interface org.tribuo.protos.ProtoSerializable
DESERIALIZATION_METHOD_NAME, PROVENANCE_SERIALIZER
-
Method Summary
Modifier and TypeMethodDescriptionvoid
close()
protected ai.onnxruntime.OnnxTensor
convertFeatures
(SparseVector input) Converts from a SparseVector using the external model's indices into the ingestion format for the external model.protected ai.onnxruntime.OnnxTensor
convertFeaturesList
(List<SparseVector> input) Converts from a list of SparseVector using the external model's indices into the ingestion format for the external model.protected List<Prediction<T>>
convertOutput
(List<ai.onnxruntime.OnnxValue> output, int[] numValidFeatures, List<Example<T>> examples) Converts a tensor into a prediction.protected Prediction<T>
convertOutput
(List<ai.onnxruntime.OnnxValue> output, int numValidFeatures, Example<T> example) Converts a tensor into a prediction.copy
(String newName, ModelProvenance newProvenance) Copies a model, replacing its provenance and name with the supplied values.static <T extends Output<T>>
ONNXExternalModel<T>createOnnxModel
(OutputFactory<T> factory, Map<String, Integer> featureMapping, Map<T, Integer> outputMapping, ExampleTransformer featureTransformer, OutputTransformer<T> outputTransformer, ai.onnxruntime.OrtSession.SessionOptions opts, String filename, String inputName) Creates anONNXExternalModel
by loading the model from disk.static <T extends Output<T>>
ONNXExternalModel<T>createOnnxModel
(OutputFactory<T> factory, Map<String, Integer> featureMapping, Map<T, Integer> outputMapping, ExampleTransformer featureTransformer, OutputTransformer<T> outputTransformer, ai.onnxruntime.OrtSession.SessionOptions opts, Path path, String inputName) Creates anONNXExternalModel
by loading the model from disk.static ONNXExternalModel<?>
deserializeFromProto
(int version, String className, com.google.protobuf.Any message) Deserialization factory.protected List<ai.onnxruntime.OnnxValue>
externalPrediction
(ai.onnxruntime.OnnxTensor input) Runs the session to make a prediction.getTopFeatures
(int n) Gets the topn
features associated with this model.Returns the model provenance from the ONNX model if that model was trained in Tribuo.void
rebuild
(ai.onnxruntime.OrtSession.SessionOptions newOptions) Closes the session and rebuilds it using the supplied options.org.tribuo.protos.core.ModelProto
Serializes this object to a protobuf.Methods inherited from class org.tribuo.interop.ExternalModel
createFeatureMap, createOutputInfo, getBatchSize, getExcuse, innerPredict, predict, setBatchSize, validateFeatureMapping
Methods inherited from class org.tribuo.Model
castModel, copy, createDataCarrier, deserialize, deserializeFromFile, deserializeFromStream, generatesProbabilities, getExcuses, getFeatureIDMap, getName, getOutputIDInfo, getProvenance, predict, predict, serializeToFile, serializeToStream, setName, toString, validate
-
Field Details
-
CURRENT_VERSION
public static final int CURRENT_VERSIONProtobuf serialization version.- See Also:
-
-
Method Details
-
deserializeFromProto
public static ONNXExternalModel<?> deserializeFromProto(int version, String className, com.google.protobuf.Any message) throws com.google.protobuf.InvalidProtocolBufferException, ai.onnxruntime.OrtException Deserialization factory.- Parameters:
version
- The serialized object version.className
- The class name.message
- The serialized data.- Returns:
- The deserialized object.
- Throws:
com.google.protobuf.InvalidProtocolBufferException
- If the protobuf could not be parsed from themessage
.ai.onnxruntime.OrtException
- If the ONNX model could not be instantiated.
-
rebuild
public void rebuild(ai.onnxruntime.OrtSession.SessionOptions newOptions) throws ai.onnxruntime.OrtException Closes the session and rebuilds it using the supplied options.Used to select a different backend, or change the number of inference threads etc.
- Parameters:
newOptions
- The new session options.- Throws:
ai.onnxruntime.OrtException
- If the model failed to rebuild the session with the supplied options.
-
convertFeatures
Description copied from class:ExternalModel
Converts from a SparseVector using the external model's indices into the ingestion format for the external model.- Specified by:
convertFeatures
in classExternalModel<T extends Output<T>,
ai.onnxruntime.OnnxTensor, List<ai.onnxruntime.OnnxValue>> - Parameters:
input
- The features using external indices.- Returns:
- The ingestion format for the external model.
-
convertFeaturesList
Description copied from class:ExternalModel
Converts from a list of SparseVector using the external model's indices into the ingestion format for the external model.- Specified by:
convertFeaturesList
in classExternalModel<T extends Output<T>,
ai.onnxruntime.OnnxTensor, List<ai.onnxruntime.OnnxValue>> - Parameters:
input
- The features using external indices.- Returns:
- The ingestion format for the external model.
-
externalPrediction
Runs the session to make a prediction.Closes the input tensor after the prediction has been made.
- Specified by:
externalPrediction
in classExternalModel<T extends Output<T>,
ai.onnxruntime.OnnxTensor, List<ai.onnxruntime.OnnxValue>> - Parameters:
input
- The input in the external model's format.- Returns:
- A tensor representing the output.
-
convertOutput
protected Prediction<T> convertOutput(List<ai.onnxruntime.OnnxValue> output, int numValidFeatures, Example<T> example) Converts a tensor into a prediction. Closes the output tensor after it's been converted.- Specified by:
convertOutput
in classExternalModel<T extends Output<T>,
ai.onnxruntime.OnnxTensor, List<ai.onnxruntime.OnnxValue>> - Parameters:
output
- The output of the external model.numValidFeatures
- The number of valid features in the input.example
- The input example, used to construct the Prediction.- Returns:
- A
Prediction
representing this tensor output.
-
convertOutput
protected List<Prediction<T>> convertOutput(List<ai.onnxruntime.OnnxValue> output, int[] numValidFeatures, List<Example<T>> examples) Converts a tensor into a prediction. Closes the output tensor after it's been converted.- Specified by:
convertOutput
in classExternalModel<T extends Output<T>,
ai.onnxruntime.OnnxTensor, List<ai.onnxruntime.OnnxValue>> - Parameters:
output
- The output of the external model.numValidFeatures
- An array with the number of valid features in each example.examples
- The input examples, used to construct the Predictions.- Returns:
- A list of
Prediction
representing this tensor output.
-
getTopFeatures
Description copied from class:Model
Gets the topn
features associated with this model.If the model does not produce per output feature lists, it returns a map with a single element with key Model.ALL_OUTPUTS.
If the model cannot describe it's top features then it returns
Collections.emptyMap()
.- Specified by:
getTopFeatures
in classModel<T extends Output<T>>
- Parameters:
n
- the number of features to return. If this value is less than 0, all features should be returned for each class, unless the model cannot score it's features.- Returns:
- a map from string outputs to an ordered list of pairs of feature names and weights associated with that feature in the model
-
copy
Description copied from class:Model
Copies a model, replacing its provenance and name with the supplied values.Used to provide the provenance removal functionality.
-
close
public void close()- Specified by:
close
in interfaceAutoCloseable
-
getTribuoProvenance
Returns the model provenance from the ONNX model if that model was trained in Tribuo.Tribuo's ONNX export functionality stores the model provenance inside the ONNX file in the metadata field
ONNXExportable.PROVENANCE_METADATA_FIELD
, and this method provides the access point for it.Note it is different from the
Model.getProvenance()
call which returns information about the ONNX file itself, and when theONNXExternalModel
was created. It does not replace that provenance because instantiating this provenance may require classes which are not present on the classpath at deployment time.- Returns:
- The model provenance from the original Tribuo training run, if it exists, and
returns
Optional.empty()
otherwise.
-
serialize
public org.tribuo.protos.core.ModelProto serialize()Description copied from interface:ProtoSerializable
Serializes this object to a protobuf. -
createOnnxModel
public static <T extends Output<T>> ONNXExternalModel<T> createOnnxModel(OutputFactory<T> factory, Map<String, Integer> featureMapping, Map<T, throws ai.onnxruntime.OrtExceptionInteger> outputMapping, ExampleTransformer featureTransformer, OutputTransformer<T> outputTransformer, ai.onnxruntime.OrtSession.SessionOptions opts, String filename, String inputName) Creates anONNXExternalModel
by loading the model from disk.- Type Parameters:
T
- The type of the output.- Parameters:
factory
- The output factory to use.featureMapping
- The feature mapping between Tribuo names and ONNX integer ids.outputMapping
- The output mapping between Tribuo outputs and ONNX integer ids.featureTransformer
- The transformation function for the features.outputTransformer
- The transformation function for the outputs.opts
- The session options for the ONNX model.filename
- The model path.inputName
- The name of the input node.- Returns:
- An ONNXExternalModel ready to score new inputs.
- Throws:
ai.onnxruntime.OrtException
- If the onnx-runtime native library call failed.
-
createOnnxModel
public static <T extends Output<T>> ONNXExternalModel<T> createOnnxModel(OutputFactory<T> factory, Map<String, Integer> featureMapping, Map<T, throws ai.onnxruntime.OrtExceptionInteger> outputMapping, ExampleTransformer featureTransformer, OutputTransformer<T> outputTransformer, ai.onnxruntime.OrtSession.SessionOptions opts, Path path, String inputName) Creates anONNXExternalModel
by loading the model from disk.- Type Parameters:
T
- The type of the output.- Parameters:
factory
- The output factory to use.featureMapping
- The feature mapping between Tribuo names and ONNX integer ids.outputMapping
- The output mapping between Tribuo outputs and ONNX integer ids.featureTransformer
- The transformation function for the features.outputTransformer
- The transformation function for the outputs.opts
- The session options for the ONNX model.path
- The model path.inputName
- The name of the input node.- Returns:
- An ONNXExternalModel ready to score new inputs.
- Throws:
ai.onnxruntime.OrtException
- If the onnx-runtime native library call failed.
-