@@ -34,15 +34,25 @@ internal object EmbeddingProvider {
3434
3535 private const val HF_URL = " https://huggingface.co/%s/resolve/main"
3636
37- private val env = OrtEnvironment .getEnvironment().apply {
38- setTelemetry(false )
37+ private val lazyEnv = lazy {
38+ runCatching {
39+ OrtEnvironment .getEnvironment().apply {
40+ setTelemetry(false )
41+ }
42+ }.getOrElse {
43+ logger.error(" Failed to create environment." , it)
44+ null
45+ }
3946 }
4047
4148 private val tokenizer = mutableMapOf<Embedding .Model , HuggingFaceTokenizer >()
4249
4350 private val sessions = mutableMapOf<Embedding .Model , OrtSession ?>()
4451
45- private val availableProvider = OrtEnvironment .getAvailableProviders().orEmpty().mapNotNull { it }.toSet()
52+ private val availableProvider by lazy {
53+ runCatching { OrtEnvironment .getAvailableProviders() }
54+ .getOrDefault(emptyList<OrtProvider >()).orEmpty().mapNotNull { it }.toSet()
55+ }
4656
4757 private val canCuda = with (FileService .Platform ) {
4858 OrtProvider .CUDA in availableProvider && isCuda12xInstalled && isCuDNNInstalled
@@ -248,12 +258,8 @@ internal object EmbeddingProvider {
248258
249259 sessions.values.forEach { it?.close() }
250260
251- with (lazyOptions) {
252- if (isInitialized())
253- value?.close()
254- }
255-
256- env.close()
261+ lazyOptions.closeIfInitialized()
262+ lazyEnv.closeIfInitialized()
257263 }
258264
259265 fun finish () {
@@ -283,7 +289,7 @@ internal object EmbeddingProvider {
283289
284290 @Suppress(" UNCHECKED_CAST" )
285291 private fun createRawEmbeddings (model : Embedding .Model , values : List <String >): Array <FloatArray > {
286- if (values.isEmpty()) return emptyArray()
292+ if (lazyEnv.value == null || values.isEmpty()) return emptyArray()
287293
288294 val embBatch = Array <FloatArray ?>(values.size) { null }
289295
@@ -382,8 +388,8 @@ internal object EmbeddingProvider {
382388 }
383389 }
384390
385- return OnnxTensor .createTensor(env , inputIds).use { idsTensor ->
386- OnnxTensor .createTensor(env , attentionMask).use { maskTensor ->
391+ return OnnxTensor .createTensor(lazyEnv.value , inputIds).use { idsTensor ->
392+ OnnxTensor .createTensor(lazyEnv.value , attentionMask).use { maskTensor ->
387393 val inputs = mutableMapOf (
388394 " input_ids" to idsTensor, " attention_mask" to maskTensor
389395 )
@@ -393,7 +399,7 @@ internal object EmbeddingProvider {
393399 if (expectsTokenTypes) {
394400 val tokenTypes = Array (inputIds.size) { LongArray (inputIds[0 ].size) { 0L } }
395401
396- OnnxTensor .createTensor(env , tokenTypes).use { typeTensor ->
402+ OnnxTensor .createTensor(lazyEnv.value , tokenTypes).use { typeTensor ->
397403 inputs[" token_type_ids" ] = typeTensor
398404
399405 runSession(inputs)
@@ -404,10 +410,10 @@ internal object EmbeddingProvider {
404410 }
405411
406412 private fun createSession (model : Embedding .Model ): OrtSession ? {
407- if (lazyOptions.value == null ) return null
413+ if (lazyEnv.value == null || lazyOptions.value == null ) return null
408414
409415 return runCatching {
410- env .createSession(
416+ lazyEnv.value !! .createSession(
411417 FileService .modelsDir.resolve(model.name).resolve(model.modelData.model).toString(),
412418 lazyOptions.value
413419 )
@@ -429,4 +435,8 @@ internal object EmbeddingProvider {
429435
430436 return withoutAccents.lowercase()
431437 }
438+
439+ private fun <T : AutoCloseable > Lazy<T?>.closeIfInitialized () {
440+ if (isInitialized()) value?.close()
441+ }
432442}
0 commit comments