-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
App/Language: Add a LLM example about text generation
This patch adds a draft of the LLM example. It uses llama2 model to generate text using input prompt. Signed-off-by: Yelin Jeong <[email protected]>
- Loading branch information
Showing
3 changed files
with
101 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
...erence_offloading/src/main/java/ai/nnstreamer/ml/inference/offloading/domain/LlamaUtil.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package ai.nnstreamer.ml.inference.offloading.domain | ||
|
||
import ai.nnstreamer.ml.inference.offloading.network.findPort | ||
import org.nnsuite.nnstreamer.NNStreamer | ||
import org.nnsuite.nnstreamer.Pipeline | ||
import org.nnsuite.nnstreamer.TensorsData | ||
import org.nnsuite.nnstreamer.TensorsInfo | ||
import java.nio.ByteBuffer | ||
|
||
fun runLlama2(input: String, hostAddress: String, servicePort: Int, newDataCb: NewDataCb) { | ||
val port = findPort() | ||
val desc = | ||
"appsrc name=srcx ! application/octet-stream ! tensor_converter ! other/tensors,format=flexible ! tensor_query_client host=${hostAddress} port=${port} dest-host=${hostAddress} dest-port=${servicePort} timeout=1000000 ! tensor_sink name=sinkx" | ||
val pipeline = Pipeline(desc, null) | ||
|
||
pipeline.registerSinkCallback("sinkx", newDataCb) | ||
// todo: Reuse or destroy the client pipeline | ||
pipeline.start() | ||
|
||
val info = TensorsInfo() | ||
info.addTensorInfo(NNStreamer.TensorType.UINT8, intArrayOf(input.length, 1, 1, 1)) | ||
|
||
val size = info.getTensorSize(0) | ||
val data = TensorsData.allocate(info) | ||
val byteBuffer: ByteBuffer = ByteBuffer.wrap(input.toByteArray()) | ||
|
||
val buffer = TensorsData.allocateByteBuffer(size) | ||
buffer.put(byteBuffer) | ||
data.setTensorData(0, buffer) | ||
pipeline.inputData("srcx", data) | ||
} |
25 changes: 25 additions & 0 deletions
25
...erence_offloading/src/main/java/ai/nnstreamer/ml/inference/offloading/domain/NewDataCb.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package ai.nnstreamer.ml.inference.offloading.domain | ||
|
||
import android.os.Message | ||
import android.os.Messenger | ||
import org.nnsuite.nnstreamer.Pipeline | ||
import org.nnsuite.nnstreamer.TensorsData | ||
|
||
class NewDataCb(private val messenger: Messenger?) : Pipeline.NewDataCallback { | ||
override fun onNewDataReceived(data: TensorsData?) { | ||
val received = data?.getTensorData(0) | ||
received?.let { | ||
val result = mutableListOf<Byte>() | ||
|
||
for (byte in received.array()) { | ||
if (byte != 0.toByte()) { | ||
result.add(byte) | ||
} | ||
} | ||
|
||
val response = Message.obtain() | ||
response.data.putString("response", String(result.toByteArray(), Charsets.UTF_8)) | ||
messenger?.send(response) | ||
} | ||
} | ||
} |