Skip to content

Commit dda5053

Browse files
committed
fix: Improve lazy initialization of environment and providers
1 parent aa264e7 commit dda5053

2 files changed

Lines changed: 26 additions & 16 deletions

File tree

core/src/main/kotlin/dev/paulee/core/data/provider/EmbeddingProvider.kt

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,4 @@ caffeine.version=3.2.2
1717
api.version=1.14.1
1818
core.version=1.17.6
1919
ui.version=1.17.4
20-
app.version=1.6.0
20+
app.version=1.6.1

0 commit comments

Comments
 (0)