Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introducing onnx and OpenVino support to Missing Annotators #14359

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
b0e7010
adding onnx API
ahmedlone127 Aug 2, 2024
61f8e05
Adding example notebook
ahmedlone127 Aug 2, 2024
626b710
Introducing ONNX support to DistilBertForZeroShotClassification
ahmedlone127 Aug 3, 2024
0237195
adding DistilBertForZeroShotClaissifcation onnx notebook
ahmedlone127 Aug 3, 2024
bcfdcad
adding onnx support to RobertaForZeroShotClassificaiton
ahmedlone127 Aug 4, 2024
40aed29
adding onnx support to XlmRobertaForZeroShotClassification
ahmedlone127 Aug 5, 2024
f1a3de5
Create HuggingFace_ONNX_in_Spark_NLP_XlmRoBertaForZeroShotClassificat…
ahmedlone127 Aug 5, 2024
d466fd4
adding ONNX support to BartTransformer
ahmedlone127 Aug 7, 2024
fa5a0ef
Create HuggingFace_ONNX_in_Spark_NLP_Bart.ipynb
ahmedlone127 Aug 7, 2024
543160e
adding onnx support to GPT2
ahmedlone127 Aug 10, 2024
c8796d1
Refactoring GPT2 code
ahmedlone127 Aug 10, 2024
e89b280
adding gpt 2 notebook
ahmedlone127 Aug 10, 2024
960c263
adding onnx support to Wav2Vec2
ahmedlone127 Aug 11, 2024
17a0416
adding onnx support to hubert
ahmedlone127 Aug 11, 2024
4b3358b
adding ONNX support to BartForZeroShotClassificaiton
ahmedlone127 Aug 14, 2024
daee6ed
adding ONNX support to DebertaForZeroShotClassificaiton
ahmedlone127 Aug 14, 2024
357c5ca
adding openVino support to Whisper
ahmedlone127 Aug 22, 2024
44450de
Create HuggingFace_OpenVINO_in_Spark_NLP_Whisper.ipynb
ahmedlone127 Aug 22, 2024
3159d3c
adding OpenVino Support to AlbertForXXX
ahmedlone127 Aug 25, 2024
d7e32f2
changing unit tests back to origanl model paths
ahmedlone127 Aug 25, 2024
729d3e6
adding BartForZeroShotClassiifcation
ahmedlone127 Aug 27, 2024
e51bd35
adding openvino suppor tto BertForXXX classificaition
ahmedlone127 Aug 29, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

100 changes: 94 additions & 6 deletions src/main/scala/com/johnsnowlabs/ml/ai/AlbertClassification.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,16 @@
package com.johnsnowlabs.ml.ai

import ai.onnxruntime.OnnxTensor
import com.johnsnowlabs.ml.ai.util.PrepareEmbeddings
import com.johnsnowlabs.ml.onnx.{OnnxSession, OnnxWrapper}
import com.johnsnowlabs.ml.openvino.OpenvinoWrapper
import com.johnsnowlabs.ml.tensorflow.sentencepiece.{SentencePieceWrapper, SentencepieceEncoder}
import com.johnsnowlabs.ml.tensorflow.sign.{ModelSignatureConstants, ModelSignatureManager}
import com.johnsnowlabs.ml.tensorflow.{TensorResources, TensorflowWrapper}
import com.johnsnowlabs.ml.util.{ONNX, TensorFlow}
import com.johnsnowlabs.ml.util.{ONNX, Openvino, TensorFlow}
import com.johnsnowlabs.nlp.annotators.common._
import com.johnsnowlabs.nlp.{ActivationFunction, Annotation}
import org.intel.openvino.Tensor
import org.tensorflow.ndarray.buffer.IntDataBuffer
import org.slf4j.{Logger, LoggerFactory}

Expand All @@ -43,6 +46,7 @@ import scala.collection.JavaConverters._
private[johnsnowlabs] class AlbertClassification(
val tensorflowWrapper: Option[TensorflowWrapper],
val onnxWrapper: Option[OnnxWrapper],
val openvinoWrapper: Option[OpenvinoWrapper],
val spp: SentencePieceWrapper,
configProtoBytes: Option[Array[Byte]] = None,
tags: Map[String, Int],
Expand All @@ -56,6 +60,7 @@ private[johnsnowlabs] class AlbertClassification(
val detectedEngine: String =
if (tensorflowWrapper.isDefined) TensorFlow.name
else if (onnxWrapper.isDefined) ONNX.name
else if (openvinoWrapper.isDefined) Openvino.name
else TensorFlow.name
private val onnxSessionOptions: Map[String, String] = new OnnxSession().getSessionOptions

Expand All @@ -69,6 +74,8 @@ private[johnsnowlabs] class AlbertClassification(

protected val logger: Logger = LoggerFactory.getLogger("AlbertClassification")



def tokenizeWithAlignment(
sentences: Seq[TokenizedSentence],
maxSeqLength: Int,
Expand Down Expand Up @@ -112,7 +119,8 @@ private[johnsnowlabs] class AlbertClassification(

val rawScores = detectedEngine match {
case ONNX.name => getRawScoresWithOnnx(batch, maxSentenceLength, sequence = true)
case _ => getRawScoresWithTF(batch, maxSentenceLength)
case Openvino.name => getRawScoresWithOv(batch, maxSentenceLength)
case TensorFlow.name => getRawScoresWithTF(batch, maxSentenceLength)
}

val dim = rawScores.length / (batchLength * maxSentenceLength)
Expand All @@ -132,7 +140,8 @@ private[johnsnowlabs] class AlbertClassification(

val rawScores = detectedEngine match {
case ONNX.name => getRawScoresWithOnnx(batch, maxSentenceLength, sequence = true)
case _ => getRawScoresWithTF(batch, maxSentenceLength)
case TensorFlow.name => getRawScoresWithTF(batch, maxSentenceLength)
case Openvino.name => getRawScoresWithOv(batch, maxSentenceLength)
}

val dim = rawScores.length / batchLength
Expand Down Expand Up @@ -206,6 +215,42 @@ private[johnsnowlabs] class AlbertClassification(
rawScores
}

private def getRawScoresWithOv(
batch: Seq[Array[Int]],
maxSentenceLength: Int
): Array[Float] = {



val batchLength = batch.length
val shape = Array(batchLength, maxSentenceLength)
val (tokenTensors, maskTensors) =
PrepareEmbeddings.prepareOvLongBatchTensors(batch, maxSentenceLength, batchLength)
val segmentTensors = new Tensor(shape, Array.fill(batchLength * maxSentenceLength)(0L))

val inferRequest = openvinoWrapper.get.getCompiledModel().create_infer_request()
inferRequest.set_tensor("input_ids", tokenTensors)
inferRequest.set_tensor("attention_mask", maskTensors)
inferRequest.set_tensor("token_type_ids", segmentTensors)

inferRequest.infer()

try {
try {
inferRequest
.get_tensor("logits")
.data()
}
} catch {
case e: Exception =>
// Log the exception as a warning
logger.warn("Exception in getRawScoresWithOnnx", e)
// Rethrow the exception to propagate it further
throw e
}

}

private def getRawScoresWithOnnx(
batch: Seq[Array[Int]],
maxSentenceLength: Int,
Expand Down Expand Up @@ -258,6 +303,9 @@ private[johnsnowlabs] class AlbertClassification(
}
}




def tagZeroShotSequence(
batch: Seq[Array[Int]],
entailmentId: Int,
Expand All @@ -269,7 +317,8 @@ private[johnsnowlabs] class AlbertClassification(
val maxSentenceLength = batch.map(encodedSentence => encodedSentence.length).max
val (startLogits, endLogits) = detectedEngine match {
case ONNX.name => computeLogitsWithOnnx(batch, maxSentenceLength)
case _ => computeLogitsWithTF(batch, maxSentenceLength)
case TensorFlow.name => computeLogitsWithTF(batch, maxSentenceLength)
case Openvino.name => computeLogitsWithOv(batch, maxSentenceLength)
}

val endDim = endLogits.length / batchLength
Expand Down Expand Up @@ -357,8 +406,8 @@ private[johnsnowlabs] class AlbertClassification(
}

private def computeLogitsWithOnnx(
batch: Seq[Array[Int]],
maxSentenceLength: Int): (Array[Float], Array[Float]) = {
batch: Seq[Array[Int]],
maxSentenceLength: Int): (Array[Float], Array[Float]) = {
// [nb of encoded sentences , maxSentenceLength]
val (runner, env) = onnxWrapper.get.getSession(onnxSessionOptions)

Expand Down Expand Up @@ -410,6 +459,45 @@ private[johnsnowlabs] class AlbertClassification(
}
}



private def computeLogitsWithOv(
batch: Seq[Array[Int]],
maxSentenceLength: Int): (Array[Float], Array[Float]) = {
// [nb of encoded sentences , maxSentenceLength]

val batchLength = batch.length
val shape = Array(batchLength, maxSentenceLength)
val (tokenTensors, maskTensors) =
PrepareEmbeddings.prepareOvLongBatchTensors(batch, maxSentenceLength, batchLength)
val segmentTensors = new Tensor(shape, Array.fill(batchLength * maxSentenceLength)(0L))

val inferRequest = openvinoWrapper.get.getCompiledModel().create_infer_request()
inferRequest.set_tensor("input_ids", tokenTensors)
inferRequest.set_tensor("attention_mask", maskTensors)
inferRequest.set_tensor("token_type_ids", segmentTensors)

inferRequest.infer()

try {
try {
val startLogits = inferRequest
.get_tensor("start_logits")
.data()
val endLogits = inferRequest
.get_tensor("end_logits")
.data()

(startLogits.slice(1, startLogits.length), endLogits.slice(1, endLogits.length))
}
} catch {
case e: Exception =>
// Log the exception as a warning
logger.warn("Exception in getRawScoresWithOnnx", e)
// Rethrow the exception to propagate it further
throw e
}
}
def findIndexedToken(
tokenizedSentences: Seq[TokenizedSentence],
sentence: (WordpieceTokenizedSentence, Int),
Expand Down
Loading
Loading