diff --git a/src/main/scala/com/johnsnowlabs/ml/ai/Albert.scala b/src/main/scala/com/johnsnowlabs/ml/ai/Albert.scala index 5aabd629eb753b..7fccf42c457a31 100644 --- a/src/main/scala/com/johnsnowlabs/ml/ai/Albert.scala +++ b/src/main/scala/com/johnsnowlabs/ml/ai/Albert.scala @@ -140,18 +140,20 @@ private[johnsnowlabs] class Albert( .asInstanceOf[OnnxTensor] .getFloatBuffer .array() - tokenTensors.close() - maskTensors.close() - segmentTensors.close() embeddings } finally if (results != null) results.close() } catch { case e: Exception => - // Log the exception as a warning - logger.warn("Exception: ", e) - // Rethrow the exception to propagate it further - // throw e + // Handle exceptions by logging or other means. + e.printStackTrace() + Array.empty[Float] // Return an empty array or appropriate error handling + } finally { + // Close tensors outside the try-catch to avoid repeated null checks. + // These resources are initialized before the try-catch, so they should be closed here. + tokenTensors.close() + maskTensors.close() + segmentTensors.close() } case _ => val tensors = new TensorResources() diff --git a/src/main/scala/com/johnsnowlabs/ml/ai/AlbertClassification.scala b/src/main/scala/com/johnsnowlabs/ml/ai/AlbertClassification.scala index bc332056eb8aa0..f1483553faac5d 100644 --- a/src/main/scala/com/johnsnowlabs/ml/ai/AlbertClassification.scala +++ b/src/main/scala/com/johnsnowlabs/ml/ai/AlbertClassification.scala @@ -241,20 +241,20 @@ private[johnsnowlabs] class AlbertClassification( .asInstanceOf[OnnxTensor] .getFloatBuffer .array() - tokenTensors.close() - maskTensors.close() - segmentTensors.close() embeddings - } finally { - if (results != null) results.close() - } + } finally if (results != null) results.close() } catch { case e: Exception => - // Log the exception as a warning - logger.warn("Exception: ", e) - // Rethrow the exception to propagate it further - throw e + // Handle exceptions by logging or other means. + e.printStackTrace() + Array.empty[Float] // Return an empty array or appropriate error handling + } finally { + // Close tensors outside the try-catch to avoid repeated null checks. + // These resources are initialized before the try-catch, so they should be closed here. + tokenTensors.close() + maskTensors.close() + segmentTensors.close() } } diff --git a/src/main/scala/com/johnsnowlabs/ml/ai/BGE.scala b/src/main/scala/com/johnsnowlabs/ml/ai/BGE.scala index 34c1d9f13e176d..8b681567de87f6 100644 --- a/src/main/scala/com/johnsnowlabs/ml/ai/BGE.scala +++ b/src/main/scala/com/johnsnowlabs/ml/ai/BGE.scala @@ -190,9 +190,6 @@ private[johnsnowlabs] class BGE( .asInstanceOf[OnnxTensor] .getFloatBuffer .array() - tokenTensors.close() - maskTensors.close() - segmentTensors.close() val embeddings = LinAlg.avgPooling(flattenEmbeddings, attentionMask, shape) val normalizedEmbeddings = LinAlg.l2Normalize(embeddings) @@ -200,10 +197,15 @@ private[johnsnowlabs] class BGE( } finally if (results != null) results.close() } catch { case e: Exception => - // Log the exception as a warning - logger.warn("Exception: ", e) - // Rethrow the exception to propagate it further - throw e + // Handle exceptions by logging or other means. + e.printStackTrace() + Array.empty[Array[Float]] // Return an empty array or appropriate error handling + } finally { + // Close tensors outside the try-catch to avoid repeated null checks. + // These resources are initialized before the try-catch, so they should be closed here. + tokenTensors.close() + maskTensors.close() + segmentTensors.close() } } diff --git a/src/main/scala/com/johnsnowlabs/ml/ai/Bert.scala b/src/main/scala/com/johnsnowlabs/ml/ai/Bert.scala index 8f8a4bed23ca03..6de0eabd36ce1f 100644 --- a/src/main/scala/com/johnsnowlabs/ml/ai/Bert.scala +++ b/src/main/scala/com/johnsnowlabs/ml/ai/Bert.scala @@ -121,20 +121,20 @@ private[johnsnowlabs] class Bert( .asInstanceOf[OnnxTensor] .getFloatBuffer .array() - tokenTensors.close() - maskTensors.close() - segmentTensors.close() - // runner.close() - // env.close() - // + embeddings } finally if (results != null) results.close() } catch { case e: Exception => - // Log the exception as a warning - logger.warn("Exception: ", e) - // Rethrow the exception to propagate it further - // throw e + // Handle exceptions by logging or other means. + e.printStackTrace() + Array.empty[Float] // Return an empty array or appropriate error handling + } finally { + // Close tensors outside the try-catch to avoid repeated null checks. + // These resources are initialized before the try-catch, so they should be closed here. + tokenTensors.close() + maskTensors.close() + segmentTensors.close() } case _ => val tensors = new TensorResources() @@ -242,7 +242,7 @@ private[johnsnowlabs] class Bert( // Log the exception as a warning logger.warn("Exception: ", e) // Rethrow the exception to propagate it further - // throw e + throw e } case _ => val tensors = new TensorResources() diff --git a/src/main/scala/com/johnsnowlabs/ml/ai/BertClassification.scala b/src/main/scala/com/johnsnowlabs/ml/ai/BertClassification.scala index 8ff24bb9123e12..cd73420d8d1726 100644 --- a/src/main/scala/com/johnsnowlabs/ml/ai/BertClassification.scala +++ b/src/main/scala/com/johnsnowlabs/ml/ai/BertClassification.scala @@ -252,18 +252,20 @@ private[johnsnowlabs] class BertClassification( .asInstanceOf[OnnxTensor] .getFloatBuffer .array() - tokenTensors.close() - maskTensors.close() - segmentTensors.close() embeddings } finally if (results != null) results.close() } catch { case e: Exception => - // Log the exception as a warning - logger.warn("Exception: ", e) - // Rethrow the exception to propagate it further - throw e + // Handle exceptions by logging or other means. + e.printStackTrace() + Array.empty[Float] // Return an empty array or appropriate error handling + } finally { + // Close tensors outside the try-catch to avoid repeated null checks. + // These resources are initialized before the try-catch, so they should be closed here. + tokenTensors.close() + maskTensors.close() + segmentTensors.close() } } diff --git a/src/main/scala/com/johnsnowlabs/ml/ai/CamemBert.scala b/src/main/scala/com/johnsnowlabs/ml/ai/CamemBert.scala index f7b46ad0a378a6..93a90c865452f6 100644 --- a/src/main/scala/com/johnsnowlabs/ml/ai/CamemBert.scala +++ b/src/main/scala/com/johnsnowlabs/ml/ai/CamemBert.scala @@ -108,17 +108,19 @@ private[johnsnowlabs] class CamemBert( .asInstanceOf[OnnxTensor] .getFloatBuffer .array() - tokenTensors.close() - maskTensors.close() embeddings } finally if (results != null) results.close() } catch { case e: Exception => - // Log the exception as a warning - logger.warn("Exception: ", e) - // Rethrow the exception to propagate it further - // throw e + // Handle exceptions by logging or other means. + e.printStackTrace() + Array.empty[Float] // Return an empty array or appropriate error handling + } finally { + // Close tensors outside the try-catch to avoid repeated null checks. + // These resources are initialized before the try-catch, so they should be closed here. + tokenTensors.close() + maskTensors.close() } case _ => val tensors = new TensorResources() diff --git a/src/main/scala/com/johnsnowlabs/ml/ai/CamemBertClassification.scala b/src/main/scala/com/johnsnowlabs/ml/ai/CamemBertClassification.scala index baa6aeb892d556..aa2eac4270f4c9 100644 --- a/src/main/scala/com/johnsnowlabs/ml/ai/CamemBertClassification.scala +++ b/src/main/scala/com/johnsnowlabs/ml/ai/CamemBertClassification.scala @@ -215,17 +215,19 @@ private[johnsnowlabs] class CamemBertClassification( .asInstanceOf[OnnxTensor] .getFloatBuffer .array() - tokenTensors.close() - maskTensors.close() embeddings } finally if (results != null) results.close() } catch { case e: Exception => - // Log the exception as a warning - logger.warn("Exception: ", e) - // Rethrow the exception to propagate it further - throw e + // Handle exceptions by logging or other means. + e.printStackTrace() + Array.empty[Float] // Return an empty array or appropriate error handling + } finally { + // Close tensors outside the try-catch to avoid repeated null checks. + // These resources are initialized before the try-catch, so they should be closed here. + tokenTensors.close() + maskTensors.close() } } diff --git a/src/main/scala/com/johnsnowlabs/ml/ai/DeBerta.scala b/src/main/scala/com/johnsnowlabs/ml/ai/DeBerta.scala index 418e217c80ff30..24e03b826a5a16 100644 --- a/src/main/scala/com/johnsnowlabs/ml/ai/DeBerta.scala +++ b/src/main/scala/com/johnsnowlabs/ml/ai/DeBerta.scala @@ -95,20 +95,20 @@ class DeBerta( .asInstanceOf[OnnxTensor] .getFloatBuffer .array() - tokenTensors.close() - maskTensors.close() - segmentTensors.close() - // runner.close() - // env.close() - // + embeddings } finally if (results != null) results.close() } catch { case e: Exception => - // Log the exception as a warning - logger.warn("Exception: ", e) - // Rethrow the exception to propagate it further - // throw e + // Handle exceptions by logging or other means. + e.printStackTrace() + Array.empty[Float] // Return an empty array or appropriate error handling + } finally { + // Close tensors outside the try-catch to avoid repeated null checks. + // These resources are initialized before the try-catch, so they should be closed here. + tokenTensors.close() + maskTensors.close() + segmentTensors.close() } case _ => val tensors = new TensorResources() diff --git a/src/main/scala/com/johnsnowlabs/ml/ai/DeBertaClassification.scala b/src/main/scala/com/johnsnowlabs/ml/ai/DeBertaClassification.scala index 7e2f2fab66e96c..60ea67e42dac46 100644 --- a/src/main/scala/com/johnsnowlabs/ml/ai/DeBertaClassification.scala +++ b/src/main/scala/com/johnsnowlabs/ml/ai/DeBertaClassification.scala @@ -224,17 +224,19 @@ private[johnsnowlabs] class DeBertaClassification( .asInstanceOf[OnnxTensor] .getFloatBuffer .array() - tokenTensors.close() - maskTensors.close() embeddings } finally if (results != null) results.close() } catch { case e: Exception => - // Log the exception as a warning - logger.warn("Exception: ", e) - // Rethrow the exception to propagate it further - throw e + // Handle exceptions by logging or other means. + e.printStackTrace() + Array.empty[Float] // Return an empty array or appropriate error handling + } finally { + // Close tensors outside the try-catch to avoid repeated null checks. + // These resources are initialized before the try-catch, so they should be closed here. + tokenTensors.close() + maskTensors.close() } } diff --git a/src/main/scala/com/johnsnowlabs/ml/ai/DistilBert.scala b/src/main/scala/com/johnsnowlabs/ml/ai/DistilBert.scala index 5979f8886129d1..e454e1ef5732af 100644 --- a/src/main/scala/com/johnsnowlabs/ml/ai/DistilBert.scala +++ b/src/main/scala/com/johnsnowlabs/ml/ai/DistilBert.scala @@ -128,17 +128,19 @@ private[johnsnowlabs] class DistilBert( .asInstanceOf[OnnxTensor] .getFloatBuffer .array() - tokenTensors.close() - maskTensors.close() embeddings } finally if (results != null) results.close() } catch { case e: Exception => - // Log the exception as a warning - logger.warn("Exception: ", e) - // Rethrow the exception to propagate it further - // throw e + // Handle exceptions by logging or other means. + e.printStackTrace() + Array.empty[Float] // Return an empty array or appropriate error handling + } finally { + // Close tensors outside the try-catch to avoid repeated null checks. + // These resources are initialized before the try-catch, so they should be closed here. + tokenTensors.close() + maskTensors.close() } case _ => val tensors = new TensorResources() diff --git a/src/main/scala/com/johnsnowlabs/ml/ai/DistilBertClassification.scala b/src/main/scala/com/johnsnowlabs/ml/ai/DistilBertClassification.scala index f0859a650e3290..b556c4978709f5 100644 --- a/src/main/scala/com/johnsnowlabs/ml/ai/DistilBertClassification.scala +++ b/src/main/scala/com/johnsnowlabs/ml/ai/DistilBertClassification.scala @@ -236,17 +236,19 @@ private[johnsnowlabs] class DistilBertClassification( .asInstanceOf[OnnxTensor] .getFloatBuffer .array() - tokenTensors.close() - maskTensors.close() embeddings } finally if (results != null) results.close() } catch { case e: Exception => - // Log the exception as a warning - logger.warn("Exception: ", e) - // Rethrow the exception to propagate it further - throw e + // Handle exceptions by logging or other means. + e.printStackTrace() + Array.empty[Float] // Return an empty array or appropriate error handling + } finally { + // Close tensors outside the try-catch to avoid repeated null checks. + // These resources are initialized before the try-catch, so they should be closed here. + tokenTensors.close() + maskTensors.close() } } diff --git a/src/main/scala/com/johnsnowlabs/ml/ai/E5.scala b/src/main/scala/com/johnsnowlabs/ml/ai/E5.scala index d108be4a22572f..d32b340523951e 100644 --- a/src/main/scala/com/johnsnowlabs/ml/ai/E5.scala +++ b/src/main/scala/com/johnsnowlabs/ml/ai/E5.scala @@ -189,9 +189,6 @@ private[johnsnowlabs] class E5( .asInstanceOf[OnnxTensor] .getFloatBuffer .array() - tokenTensors.close() - maskTensors.close() - segmentTensors.close() val embeddings = LinAlg.avgPooling(flattenEmbeddings, attentionMask, shape) val normalizedEmbeddings = LinAlg.l2Normalize(embeddings) @@ -199,10 +196,15 @@ private[johnsnowlabs] class E5( } finally if (results != null) results.close() } catch { case e: Exception => - // Log the exception as a warning - logger.warn("Exception: ", e) - // Rethrow the exception to propagate it further - throw e + // Handle exceptions by logging or other means. + e.printStackTrace() + Array.empty[Array[Float]] // Return an empty array or appropriate error handling + } finally { + // Close tensors outside the try-catch to avoid repeated null checks. + // These resources are initialized before the try-catch, so they should be closed here. + tokenTensors.close() + maskTensors.close() + segmentTensors.close() } } diff --git a/src/main/scala/com/johnsnowlabs/ml/ai/MPNet.scala b/src/main/scala/com/johnsnowlabs/ml/ai/MPNet.scala index 436e8d8fee9a1c..3623a9a9185fbf 100644 --- a/src/main/scala/com/johnsnowlabs/ml/ai/MPNet.scala +++ b/src/main/scala/com/johnsnowlabs/ml/ai/MPNet.scala @@ -188,8 +188,6 @@ private[johnsnowlabs] class MPNet( .asInstanceOf[OnnxTensor] .getFloatBuffer .array() - tokenTensors.close() - maskTensors.close() val embeddings = LinAlg.avgPooling(flattenEmbeddings, attentionMask, shape) val normalizedEmbeddings = LinAlg.l2Normalize(embeddings) @@ -197,10 +195,14 @@ private[johnsnowlabs] class MPNet( } finally if (results != null) results.close() } catch { case e: Exception => - // Log the exception as a warning - logger.warn("Exception: ", e) - // Rethrow the exception to propagate it further - throw e + // Handle exceptions by logging or other means. + e.printStackTrace() + Array.empty[Array[Float]] // Return an empty array or appropriate error handling + } finally { + // Close tensors outside the try-catch to avoid repeated null checks. + // These resources are initialized before the try-catch, so they should be closed here. + tokenTensors.close() + maskTensors.close() } } diff --git a/src/main/scala/com/johnsnowlabs/ml/ai/RoBerta.scala b/src/main/scala/com/johnsnowlabs/ml/ai/RoBerta.scala index dacc7664b5d537..e22c8f132d287e 100644 --- a/src/main/scala/com/johnsnowlabs/ml/ai/RoBerta.scala +++ b/src/main/scala/com/johnsnowlabs/ml/ai/RoBerta.scala @@ -104,17 +104,19 @@ private[johnsnowlabs] class RoBerta( .asInstanceOf[OnnxTensor] .getFloatBuffer .array() - tokenTensors.close() - maskTensors.close() - embeddings + embeddings } finally if (results != null) results.close() } catch { case e: Exception => - // Log the exception as a warning - logger.warn("Exception: ", e) - // Rethrow the exception to propagate it further - // throw e + // Handle exceptions by logging or other means. + e.printStackTrace() + Array.empty[Float] // Return an empty array or appropriate error handling + } finally { + // Close tensors outside the try-catch to avoid repeated null checks. + // These resources are initialized before the try-catch, so they should be closed here. + tokenTensors.close() + maskTensors.close() } case _ => val tensors = new TensorResources() diff --git a/src/main/scala/com/johnsnowlabs/ml/ai/RoBertaClassification.scala b/src/main/scala/com/johnsnowlabs/ml/ai/RoBertaClassification.scala index 7f89a0aa9a450b..4296c8bcf5a542 100644 --- a/src/main/scala/com/johnsnowlabs/ml/ai/RoBertaClassification.scala +++ b/src/main/scala/com/johnsnowlabs/ml/ai/RoBertaClassification.scala @@ -233,17 +233,19 @@ private[johnsnowlabs] class RoBertaClassification( .asInstanceOf[OnnxTensor] .getFloatBuffer .array() - tokenTensors.close() - maskTensors.close() embeddings } finally if (results != null) results.close() } catch { case e: Exception => - // Log the exception as a warning - logger.warn("Exception: ", e) - // Rethrow the exception to propagate it further - throw e + // Handle exceptions by logging or other means. + e.printStackTrace() + Array.empty[Float] // Return an empty array or appropriate error handling + } finally { + // Close tensors outside the try-catch to avoid repeated null checks. + // These resources are initialized before the try-catch, so they should be closed here. + tokenTensors.close() + maskTensors.close() } } diff --git a/src/main/scala/com/johnsnowlabs/ml/ai/XlmRoBertaClassification.scala b/src/main/scala/com/johnsnowlabs/ml/ai/XlmRoBertaClassification.scala index fce88b94779573..2654d7d6198e4c 100644 --- a/src/main/scala/com/johnsnowlabs/ml/ai/XlmRoBertaClassification.scala +++ b/src/main/scala/com/johnsnowlabs/ml/ai/XlmRoBertaClassification.scala @@ -219,17 +219,20 @@ private[johnsnowlabs] class XlmRoBertaClassification( .asInstanceOf[OnnxTensor] .getFloatBuffer .array() - tokenTensors.close() - maskTensors.close() embeddings } finally if (results != null) results.close() } catch { case e: Exception => - // Log the exception as a warning - logger.warn("Exception: ", e) - // Rethrow the exception to propagate it further - throw e + // Handle exceptions by logging or other means. + e.printStackTrace() + Array.empty[Float] // Return an empty array or appropriate error handling + } finally { + // Close tensors outside the try-catch to avoid repeated null checks. + // These resources are initialized before the try-catch, so they should be closed here. + tokenTensors.close() + maskTensors.close() + } } diff --git a/src/main/scala/com/johnsnowlabs/ml/ai/XlmRoberta.scala b/src/main/scala/com/johnsnowlabs/ml/ai/XlmRoberta.scala index 12cfdae478217d..df7dbeb4a106a9 100644 --- a/src/main/scala/com/johnsnowlabs/ml/ai/XlmRoberta.scala +++ b/src/main/scala/com/johnsnowlabs/ml/ai/XlmRoberta.scala @@ -140,17 +140,19 @@ private[johnsnowlabs] class XlmRoberta( .asInstanceOf[OnnxTensor] .getFloatBuffer .array() - tokenTensors.close() - maskTensors.close() - embeddings + embeddings } finally if (results != null) results.close() } catch { case e: Exception => - // Log the exception as a warning - logger.warn("Exception: ", e) - // Rethrow the exception to propagate it further - // throw e + // Handle exceptions by logging or other means. + e.printStackTrace() + Array.empty[Float] // Return an empty array or appropriate error handling + } finally { + // Close tensors outside the try-catch to avoid repeated null checks. + // These resources are initialized before the try-catch, so they should be closed here. + tokenTensors.close() + maskTensors.close() } case _ => val tensors = new TensorResources() diff --git a/src/main/scala/com/johnsnowlabs/ml/ai/seq2seq/OnnxT5EncoderDecoder.scala b/src/main/scala/com/johnsnowlabs/ml/ai/seq2seq/OnnxT5EncoderDecoder.scala index b0a66a6b952452..4607358eb24146 100644 --- a/src/main/scala/com/johnsnowlabs/ml/ai/seq2seq/OnnxT5EncoderDecoder.scala +++ b/src/main/scala/com/johnsnowlabs/ml/ai/seq2seq/OnnxT5EncoderDecoder.scala @@ -125,10 +125,9 @@ class OnnxT5EncoderDecoder( modelOutputs } catch { case e: Exception => - // Log the exception as a warning - logger.warn("Exception: ", e) - // Rethrow the exception to propagate it further - throw e + // Handle exceptions by logging or other means. + e.printStackTrace() + Array.empty[Array[Int]] // Return an empty array or appropriate error handling } } diff --git a/src/main/scala/com/johnsnowlabs/ml/ai/t5/OnnxT5EncoderDecoder.scala b/src/main/scala/com/johnsnowlabs/ml/ai/t5/OnnxT5EncoderDecoder.scala index ab31bd36e3fb0e..9e9dcf62ef1236 100644 --- a/src/main/scala/com/johnsnowlabs/ml/ai/t5/OnnxT5EncoderDecoder.scala +++ b/src/main/scala/com/johnsnowlabs/ml/ai/t5/OnnxT5EncoderDecoder.scala @@ -124,10 +124,9 @@ class OnnxT5EncoderDecoder( modelOutputs } catch { case e: Exception => - // Log the exception as a warning - logger.warn("Exception: ", e) - // Rethrow the exception to propagate it further - throw e + // Handle exceptions by logging or other means. + e.printStackTrace() + Array.empty[Array[Int]] // Return an empty array or appropriate error handling } }