Skip to content

Error crash๐Ÿ’”๐Ÿ˜žย #6

@Togaroda

Description

@Togaroda

error start
11-18 18:49:33.512 4128 4290 F libc : Fatal signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 0x8 in tid 4290 (DefaultDispatch), pid 4128 (.my.newproject7)
error end

___ code:

package com.my.newproject7

import android.content.Context
import android.util.Log
import com.dilivva.inferkt.Inference
import com.dilivva.inferkt.createInference
import com.dilivva.inferkt.GenerationEvent
import com.dilivva.inferkt.ModelSettings
import com.dilivva.inferkt.SamplingSettings
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import java.lang.reflect.Field
import java.lang.reflect.Method

class InferKtHelper(private val context: Context) {

    private var inference: Inference? = null
    private var modelLoaded: Boolean = false

    private fun extractTextFromEvent(event: Any): String? {
        try {
            val getterNames = arrayOf("getText", "getValue", "getToken", "text", "value", "token")
            for (name in getterNames) {
                try {
                    val m: Method? = try { event.javaClass.getMethod(name) } catch (_: NoSuchMethodException) { null }
                    if (m != null) {
                        val res = m.invoke(event)
                        if (res is String) return res
                    }
                } catch (_: Throwable) {}
            }

            val fieldNames = arrayOf("text", "value", "token")
            for (fName in fieldNames) {
                try {
                    val f: Field = event.javaClass.getDeclaredField(fName)
                    f.isAccessible = true
                    val res = f.get(event)
                    if (res is String) return res
                } catch (_: Throwable) {}
            }
        } catch (_: Throwable) {}
        return null
    }

    private fun extractErrorFromEvent(event: Any): String {
        try {
            try {
                val m: Method? = try { event.javaClass.getMethod("getError") } catch (_: NoSuchMethodException) { null }
                if (m != null) {
                    val errObj = m.invoke(event)
                    return errObj?.toString() ?: "Unknown generation error"
                }
            } catch (_: Throwable) {}
            try {
                val f: Field = event.javaClass.getDeclaredField("error")
                f.isAccessible = true
                val errObj = f.get(event)
                return errObj?.toString() ?: "Unknown generation error"
            } catch (_: Throwable) {}
        } catch (_: Throwable) {}
        return "Unknown generation error"
    }

    /**
     * ุชุญู…ูŠู„ ุงู„ู†ู…ูˆุฐุฌ ููŠ ุงู„ุฎู„ููŠุฉ ุชู„ู‚ุงุฆูŠู‹ุง
     */
    fun loadModel(
        modelPath: String,
        numberOfGpuLayers: Int = 0,
        useMmap: Boolean = true,
        useMlock: Boolean = false,
        numberOfThreads: Int = 4,
        contextSize: Int = 512,
        batchSize: Int = 1,
        onProgress: (Float) -> Unit,
        onLoaded: () -> Unit,
        onError: (String) -> Unit
    ) {
        CoroutineScope(Dispatchers.IO).launch {
            try {
                inference = createInference()

                val settings = ModelSettings(
                    modelPath,
                    numberOfGpuLayers,
                    useMmap,
                    useMlock,
                    numberOfThreads,
                    contextSize,
                    batchSize
                )

                val ok = inference!!.preloadModel(settings) { prog: Float ->
                    try {
                        onProgress(prog)
                        if (prog >= 1.0f) {
                            modelLoaded = true
                            CoroutineScope(Dispatchers.Main).launch { onLoaded() }
                        }
                    } catch (e: Throwable) {
                        Log.e("InferKtHelper", "onProgress callback error", e)
                    }
                    true
                }

                if (!ok) {
                    CoroutineScope(Dispatchers.Main).launch { onError("preloadModel returned false") }
                }
            } catch (e: Exception) {
                CoroutineScope(Dispatchers.Main).launch { onError(e.message ?: "Unknown error during loadModel") }
            }
        }
    }

    fun setSampling(
        temperature: Float = 0.7f,
        topP: Float = 0.95f,
        minP: Float = 0.0f,
        topK: Int = 40
    ) {
        try {
            val ss = SamplingSettings(temperature, topP, minP, topK)
            inference?.setSamplingParams(ss)
        } catch (e: Throwable) {
            Log.e("InferKtHelper", "setSampling error", e)
        }
    }

    /**
     * ุชูˆู„ูŠุฏ ุงู„ู†ุตูˆุต ููŠ ุงู„ุฎู„ููŠุฉ ุชู„ู‚ุงุฆูŠู‹ุง
     */
    fun generate(
        prompt: String,
        maxTokens: Int = 128,
        onStream: (String) -> Unit,
        onDone: (String) -> Unit,
        onError: (String) -> Unit
    ) {
        if (!modelLoaded) {
            onError("Model not loaded")
            return
        }

        CoroutineScope(Dispatchers.IO).launch {
            var fullText = ""

            try {
                inference!!.completion(prompt, maxTokens) { event: GenerationEvent ->
                    try {
                        when (event) {
                            is GenerationEvent.Loading -> {}
                            is GenerationEvent.Generating -> {
                                val textChunk = extractTextFromEvent(event as Any) ?: ""
                                if (textChunk.isNotEmpty()) {
                                    fullText += textChunk
                                    CoroutineScope(Dispatchers.Main).launch { onStream(textChunk) }
                                }
                            }
                            is GenerationEvent.Generated -> {
                                val final = extractTextFromEvent(event as Any) ?: fullText
                                CoroutineScope(Dispatchers.Main).launch { onDone(final) }
                            }
                            is GenerationEvent.Error -> {
                                val err = extractErrorFromEvent(event as Any)
                                CoroutineScope(Dispatchers.Main).launch { onError(err) }
                            }
                            else -> {
                                val maybe = extractTextFromEvent(event as Any)
                                if (maybe != null) {
                                    fullText += maybe
                                    CoroutineScope(Dispatchers.Main).launch { onStream(maybe) }
                                }
                            }
                        }
                    } catch (e: Throwable) {
                        Log.e("InferKtHelper", "generation callback error", e)
                    }
                    true
                }
            } catch (e: Exception) {
                CoroutineScope(Dispatchers.Main).launch { onError(e.message ?: "Unknown error during generation") }
            }
        }
    }

    fun release() {
        try {
            inference = null
            modelLoaded = false
        } catch (_: Throwable) {}
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions