Search in sources :

Example 1 with ProvenanceSerializationException

use of com.oracle.labs.mlrg.olcut.provenance.io.ProvenanceSerializationException in project tribuo by oracle.

the class ONNXExternalModel method getTribuoProvenance.

/**
 * Returns the model provenance from the ONNX model if that
 * model was trained in Tribuo.
 * <p>
 * Tribuo's ONNX export functionality stores the model provenance inside the
 * ONNX file in the metadata field {@link ONNXExportable#PROVENANCE_METADATA_FIELD},
 * and this method provides the access point for it.
 * <p>
 * Note it is different from the {@link Model#getProvenance()} call which
 * returns information about the ONNX file itself, and when the {@code ONNXExternalModel}
 * 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.
 *
 * @return The model provenance from the original Tribuo training run, if it exists, and
 * returns {@link Optional#empty()} otherwise.
 */
public Optional<ModelProvenance> getTribuoProvenance() {
    try {
        OnnxModelMetadata metadata = session.getMetadata();
        Optional<String> value = metadata.getCustomMetadataValue(ONNXExportable.PROVENANCE_METADATA_FIELD);
        if (value.isPresent()) {
            Provenance prov = ONNXExportable.SERIALIZER.deserializeAndUnmarshal(value.get());
            if (prov instanceof ModelProvenance) {
                return Optional.of((ModelProvenance) prov);
            } else {
                logger.log(Level.WARNING, "Found invalid provenance object, " + prov.toString());
                return Optional.empty();
            }
        } else {
            return Optional.empty();
        }
    } catch (OrtException e) {
        logger.log(Level.WARNING, "ORTException when reading session metadata", e);
        return Optional.empty();
    } catch (ProvenanceSerializationException e) {
        logger.log(Level.WARNING, "Failed to parse provenance from value.", e);
        return Optional.empty();
    }
}
Also used : ModelProvenance(org.tribuo.provenance.ModelProvenance) DatasetProvenance(org.tribuo.provenance.DatasetProvenance) ExternalTrainerProvenance(org.tribuo.interop.ExternalTrainerProvenance) Provenance(com.oracle.labs.mlrg.olcut.provenance.Provenance) StringProvenance(com.oracle.labs.mlrg.olcut.provenance.primitives.StringProvenance) LongProvenance(com.oracle.labs.mlrg.olcut.provenance.primitives.LongProvenance) ExternalDatasetProvenance(org.tribuo.interop.ExternalDatasetProvenance) OnnxModelMetadata(ai.onnxruntime.OnnxModelMetadata) ModelProvenance(org.tribuo.provenance.ModelProvenance) OrtException(ai.onnxruntime.OrtException) ProvenanceSerializationException(com.oracle.labs.mlrg.olcut.provenance.io.ProvenanceSerializationException)

Aggregations

OnnxModelMetadata (ai.onnxruntime.OnnxModelMetadata)1 OrtException (ai.onnxruntime.OrtException)1 Provenance (com.oracle.labs.mlrg.olcut.provenance.Provenance)1 ProvenanceSerializationException (com.oracle.labs.mlrg.olcut.provenance.io.ProvenanceSerializationException)1 LongProvenance (com.oracle.labs.mlrg.olcut.provenance.primitives.LongProvenance)1 StringProvenance (com.oracle.labs.mlrg.olcut.provenance.primitives.StringProvenance)1 ExternalDatasetProvenance (org.tribuo.interop.ExternalDatasetProvenance)1 ExternalTrainerProvenance (org.tribuo.interop.ExternalTrainerProvenance)1 DatasetProvenance (org.tribuo.provenance.DatasetProvenance)1 ModelProvenance (org.tribuo.provenance.ModelProvenance)1