openassistant: support embeddings api
- Commit
e9940d697f4faf16e94162eb2cdde6a63b293d7a- Author
- Vayun Mathur <[email protected]>
- When
- 2026-07-08 18:34 UTC (5 months ago)
- Parents
8280ecb
Diff
openassistant/src/main/java/com/vayunmathur/openassistant/util/InferenceService.kt
diff --git a/openassistant/src/main/java/com/vayunmathur/openassistant/util/InferenceService.kt b/openassistant/src/main/java/com/vayunmathur/openassistant/util/InferenceService.ktindex 84b81896b1..3508eb9499 100644--- a/openassistant/src/main/java/com/vayunmathur/openassistant/util/InferenceService.kt+++ b/openassistant/src/main/java/com/vayunmathur/openassistant/util/InferenceService.kt@@ -263,10 +263,17 @@ class InferenceService : Service() { putInt("dim", dim) putString("status", "ready") })- "text" -> sendEmbedding(job.receiver, SiglipEmbedder.textEmbedding(context, job.userText))+ "text" -> {+ val t0 = System.currentTimeMillis()+ val emb = SiglipEmbedder.textEmbedding(context, job.userText)+ Log.i("InferenceService", "Text embed (${emb?.size ?: 0}d) in ${System.currentTimeMillis() - t0}ms ok=${emb != null}")+ sendEmbedding(job.receiver, emb)+ } "image" -> { val path = job.imagePath+ val t0 = System.currentTimeMillis() val emb = if (path != null) SiglipEmbedder.imageEmbedding(context, File(path)) else null+ Log.i("InferenceService", "Image embed (${emb?.size ?: 0}d) in ${System.currentTimeMillis() - t0}ms ok=${emb != null} path=$path") sendEmbedding(job.receiver, emb) // The temp copy from copyUriToFile is no longer needed. if (path != null) runCatching { File(path).delete() }@@ -277,7 +284,12 @@ class InferenceService : Service() { private fun sendEmbedding(receiver: ResultReceiver, emb: FloatArray?) { if (emb == null) {- receiver.send(-1, Bundle().apply { putString("error", "Embedding failed") })+ // Provider is up but this specific item couldn't be embedded; mark it+ // per_item so the client skips just this one rather than pausing.+ receiver.send(-1, Bundle().apply {+ putString("error", "Embedding failed")+ putBoolean("per_item", true)+ }) } else { receiver.send(0, Bundle().apply { putByteArray("embedding", SiglipEmbedder.floatsToBytes(emb))
openassistant/src/main/java/com/vayunmathur/openassistant/util/SiglipEmbedder.kt
diff --git a/openassistant/src/main/java/com/vayunmathur/openassistant/util/SiglipEmbedder.kt b/openassistant/src/main/java/com/vayunmathur/openassistant/util/SiglipEmbedder.ktindex e99efb845b..c607fd19c0 100644--- a/openassistant/src/main/java/com/vayunmathur/openassistant/util/SiglipEmbedder.kt+++ b/openassistant/src/main/java/com/vayunmathur/openassistant/util/SiglipEmbedder.kt@@ -73,6 +73,8 @@ object SiglipEmbedder { @Volatile private var visionSession: OrtSession? = null @Volatile private var textSession: OrtSession? = null @Volatile private var tokenizer: SentencePieceTokenizer? = null+ @Volatile private var visionOutputName: String? = null+ @Volatile private var textOutputName: String? = null @Volatile private var initTried = false @Volatile private var initOk = false @@ -121,9 +123,14 @@ object SiglipEmbedder { visionSession = env.createSession(modelFile(app, VISION_FILE).absolutePath, opts) textSession = env.createSession(modelFile(app, TEXT_FILE).absolutePath, opts) tokenizer = tok- cachedDim = visionSession?.let { readOutputDim(it) } ?: 0+ // These sub-model exports emit BOTH `last_hidden_state` and the+ // pooled embedding (`pooler_output`); we must read the pooled one,+ // not output index 0, or the vectors are meaningless.+ visionOutputName = bestOutputName(visionSession!!)+ textOutputName = bestOutputName(textSession!!)+ cachedDim = visionSession?.let { readOutputDim(it, visionOutputName!!) } ?: 0 initOk = true- Log.i(TAG, "SigLIP2 embedder ready (dim=$cachedDim)")+ Log.i(TAG, "SigLIP2 embedder ready (dim=$cachedDim, visionOut=$visionOutputName, textOut=$textOutputName)") true } catch (e: Throwable) { Log.e(TAG, "Failed to initialise SigLIP2 embedder", e)@@ -133,9 +140,25 @@ object SiglipEmbedder { } } - private fun readOutputDim(session: OrtSession): Int {+ /**+ * Pick the output that holds the pooled embedding. SigLIP2's vision/text+ * sub-model exports emit `last_hidden_state` (rank-3) plus the pooled+ * `pooler_output` (rank-2 `[1, dim]`) — the latter is the retrieval vector.+ * Prefer a name hinting "pool"/"embed", else the first rank-2 output, else+ * output 0.+ */+ private fun bestOutputName(session: OrtSession): String {+ val info = session.outputInfo+ info.keys.firstOrNull { it.contains("pool", ignoreCase = true) }?.let { return it }+ info.keys.firstOrNull { it.contains("embed", ignoreCase = true) }?.let { return it }+ info.entries.firstOrNull { (it.value.info as? ai.onnxruntime.TensorInfo)?.shape?.size == 2 }+ ?.let { return it.key }+ return info.keys.first()+ }++ private fun readOutputDim(session: OrtSession, outputName: String): Int { return try {- val info = session.outputInfo.values.first().info as ai.onnxruntime.TensorInfo+ val info = session.outputInfo[outputName]!!.info as ai.onnxruntime.TensorInfo info.shape.last().toInt() } catch (_: Exception) { 0@@ -163,13 +186,14 @@ object SiglipEmbedder { return try { synchronized(lock) { val inputName = session.inputNames.iterator().next()+ val outName = visionOutputName ?: bestOutputName(session) OnnxTensor.createTensor( env, FloatBuffer.wrap(input), longArrayOf(1, 3, IMAGE_SIZE.toLong(), IMAGE_SIZE.toLong()), ).use { tensor -> session.run(mapOf(inputName to tensor)).use { result ->- val out = result.get(0) as OnnxTensor+ val out = result.get(outName).get() as OnnxTensor val vec = FloatArray(out.info.shape.last().toInt()) out.floatBuffer.get(vec) l2Normalize(vec)@@ -206,7 +230,15 @@ object SiglipEmbedder { env, LongBuffer.wrap(mask), longArrayOf(1, ids.size.toLong()) ) }+ lname.contains("position") -> {+ // Sequential positions 0..seqLen-1.+ val pos = LongArray(ids.size) { it.toLong() }+ tensors[name] = OnnxTensor.createTensor(+ env, LongBuffer.wrap(pos), longArrayOf(1, ids.size.toLong())+ )+ } else -> {+ // input_ids tensors[name] = OnnxTensor.createTensor( env, LongBuffer.wrap(longIds), longArrayOf(1, ids.size.toLong()) )@@ -214,7 +246,8 @@ object SiglipEmbedder { } } session.run(tensors).use { result ->- val out = result.get(0) as OnnxTensor+ val outName = textOutputName ?: bestOutputName(session)+ val out = result.get(outName).get() as OnnxTensor val vec = FloatArray(out.info.shape.last().toInt()) out.floatBuffer.get(vec) l2Normalize(vec)@@ -239,6 +272,8 @@ object SiglipEmbedder { visionSession = null textSession = null tokenizer = null+ visionOutputName = null+ textOutputName = null cachedDim = 0 initOk = false initTried = false