Search in sources :

Example 1 with TensorInfo

use of ai.onnxruntime.TensorInfo in project tribuo by oracle.

the class BERTFeatureExtractor method postConfig.

@Override
public void postConfig() throws PropertyException {
    try {
        env = OrtEnvironment.getEnvironment();
        OrtSession.SessionOptions options = new OrtSession.SessionOptions();
        if (useCUDA) {
            options.addCUDA();
        }
        session = env.createSession(modelPath.toString(), options);
        // Validate model and extract the embedding dimension
        Map<String, NodeInfo> outputs = session.getOutputInfo();
        if (outputs.size() != 2) {
            throw new PropertyException("", "modelPath", "Invalid model, expected 2 outputs, found " + outputs.size());
        } else {
            // check that the outputs have the expected names
            NodeInfo outputZero = outputs.get(TOKEN_OUTPUT);
            if ((outputZero == null) || !(outputZero.getInfo() instanceof TensorInfo)) {
                throw new PropertyException("", "modelPath", "Invalid model, expected to find tensor output called '" + TOKEN_OUTPUT + "'");
            } else {
                TensorInfo outputZeroTensor = (TensorInfo) outputZero.getInfo();
                long[] shape = outputZeroTensor.getShape();
                if (shape.length != 3) {
                    throw new PropertyException("", "modelPath", "Invalid model, expected to find " + TOKEN_OUTPUT + " with 3 dimensions, found :" + Arrays.toString(shape));
                } else {
                    // Bert embedding dim is the last dimension.
                    // The first two are batch and sequence length.
                    bertDim = (int) shape[2];
                }
            }
            NodeInfo outputOne = outputs.get(CLS_OUTPUT);
            if ((outputOne == null) || !(outputOne.getInfo() instanceof TensorInfo)) {
                throw new PropertyException("", "modelPath", "Invalid model, expected to find tensor output called '" + CLS_OUTPUT + "'");
            } else {
                TensorInfo outputOneTensor = (TensorInfo) outputOne.getInfo();
                long[] shape = outputOneTensor.getShape();
                if (shape.length != 2) {
                    throw new PropertyException("", "modelPath", "Invalid model, expected to find " + CLS_OUTPUT + " with 2 dimensions, found :" + Arrays.toString(shape));
                } else if (shape[1] != bertDim) {
                    // dimension mismatch between the classification and token outputs, bail out
                    throw new PropertyException("", "modelPath", "Invalid model, expected to find two outputs with the same embedding dimension, instead found " + bertDim + " and " + shape[1]);
                }
            }
        }
        Map<String, NodeInfo> inputs = session.getInputInfo();
        if (inputs.size() != 3) {
            throw new PropertyException("", "modelPath", "Invalid model, expected 3 inputs, found " + inputs.size());
        } else {
            if (!inputs.containsKey(ATTENTION_MASK)) {
                throw new PropertyException("", "modelPath", "Invalid model, expected to find an input called '" + ATTENTION_MASK + "'");
            }
            if (!inputs.containsKey(INPUT_IDS)) {
                throw new PropertyException("", "modelPath", "Invalid model, expected to find an input called '" + INPUT_IDS + "'");
            }
            if (!inputs.containsKey(TOKEN_TYPE_IDS)) {
                throw new PropertyException("", "modelPath", "Invalid model, expected to find an input called '" + TOKEN_TYPE_IDS + "'");
            }
        }
        featureNames = generateFeatureNames(bertDim);
        TokenizerConfig config = loadTokenizer(tokenizerPath);
        Wordpiece wordpiece = new Wordpiece(config.tokenIDs.keySet(), config.unknownToken, config.maxInputCharsPerWord);
        tokenIDs = config.tokenIDs;
        unknownToken = config.unknownToken;
        classificationToken = config.classificationToken;
        separatorToken = config.separatorToken;
        tokenizer = new WordpieceTokenizer(wordpiece, new WordpieceBasicTokenizer(), config.lowercase, config.stripAccents, Collections.emptySet());
    } catch (OrtException e) {
        throw new PropertyException(e, "", "modelPath", "Failed to load model, ORT threw: ");
    } catch (IOException e) {
        throw new PropertyException(e, "", "tokenizerPath", "Failed to load tokenizer, Jackson threw: ");
    }
}
Also used : WordpieceTokenizer(org.tribuo.util.tokens.impl.wordpiece.WordpieceTokenizer) PropertyException(com.oracle.labs.mlrg.olcut.config.PropertyException) WordpieceBasicTokenizer(org.tribuo.util.tokens.impl.wordpiece.WordpieceBasicTokenizer) IOException(java.io.IOException) OrtException(ai.onnxruntime.OrtException) Wordpiece(org.tribuo.util.tokens.impl.wordpiece.Wordpiece) OrtSession(ai.onnxruntime.OrtSession) NodeInfo(ai.onnxruntime.NodeInfo) TensorInfo(ai.onnxruntime.TensorInfo)

Aggregations

NodeInfo (ai.onnxruntime.NodeInfo)1 OrtException (ai.onnxruntime.OrtException)1 OrtSession (ai.onnxruntime.OrtSession)1 TensorInfo (ai.onnxruntime.TensorInfo)1 PropertyException (com.oracle.labs.mlrg.olcut.config.PropertyException)1 IOException (java.io.IOException)1 Wordpiece (org.tribuo.util.tokens.impl.wordpiece.Wordpiece)1 WordpieceBasicTokenizer (org.tribuo.util.tokens.impl.wordpiece.WordpieceBasicTokenizer)1 WordpieceTokenizer (org.tribuo.util.tokens.impl.wordpiece.WordpieceTokenizer)1