Skip to content

Commit

Permalink
Replace hard exception with soft logs (#14179)
Browse files Browse the repository at this point in the history
This reverts commit eb91fde.
  • Loading branch information
maziyarpanahi authored Feb 19, 2024
1 parent 5067417 commit 94f6900
Show file tree
Hide file tree
Showing 19 changed files with 156 additions and 129 deletions.
16 changes: 9 additions & 7 deletions src/main/scala/com/johnsnowlabs/ml/ai/Albert.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
20 changes: 10 additions & 10 deletions src/main/scala/com/johnsnowlabs/ml/ai/AlbertClassification.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}

Expand Down
16 changes: 9 additions & 7 deletions src/main/scala/com/johnsnowlabs/ml/ai/BGE.scala
Original file line number Diff line number Diff line change
Expand Up @@ -190,20 +190,22 @@ 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)
LinAlg.denseMatrixToArray(normalizedEmbeddings)
} 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()
}
}

Expand Down
22 changes: 11 additions & 11 deletions src/main/scala/com/johnsnowlabs/ml/ai/Bert.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down
16 changes: 9 additions & 7 deletions src/main/scala/com/johnsnowlabs/ml/ai/BertClassification.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

}
Expand Down
14 changes: 8 additions & 6 deletions src/main/scala/com/johnsnowlabs/ml/ai/CamemBert.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}

Expand Down
20 changes: 10 additions & 10 deletions src/main/scala/com/johnsnowlabs/ml/ai/DeBerta.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}

Expand Down
14 changes: 8 additions & 6 deletions src/main/scala/com/johnsnowlabs/ml/ai/DistilBert.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}

Expand Down
16 changes: 9 additions & 7 deletions src/main/scala/com/johnsnowlabs/ml/ai/E5.scala
Original file line number Diff line number Diff line change
Expand Up @@ -189,20 +189,22 @@ 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)
LinAlg.denseMatrixToArray(normalizedEmbeddings)
} 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()
}
}

Expand Down
14 changes: 8 additions & 6 deletions src/main/scala/com/johnsnowlabs/ml/ai/MPNet.scala
Original file line number Diff line number Diff line change
Expand Up @@ -188,19 +188,21 @@ private[johnsnowlabs] class MPNet(
.asInstanceOf[OnnxTensor]
.getFloatBuffer
.array()
tokenTensors.close()
maskTensors.close()

val embeddings = LinAlg.avgPooling(flattenEmbeddings, attentionMask, shape)
val normalizedEmbeddings = LinAlg.l2Normalize(embeddings)
LinAlg.denseMatrixToArray(normalizedEmbeddings)
} 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()
}
}

Expand Down
Loading

0 comments on commit 94f6900

Please sign in to comment.