Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Text generation application using ML offloading. #363

Merged
merged 5 commits into from
Dec 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
658 changes: 658 additions & 0 deletions Tizen.web/TextGenerationLlama2/NsdService/.cproject

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions Tizen.web/TextGenerationLlama2/NsdService/.exportMap
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
global: main;
_IO_*;
local: *;
};
46 changes: 46 additions & 0 deletions Tizen.web/TextGenerationLlama2/NsdService/.project
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>NsdService</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.cdt.managedbuilder.core.genmakebuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.cdt.managedbuilder.core.ScannerConfigBuilder</name>
<triggers>full,incremental,</triggers>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.cdt.core.cnature</nature>
<nature>org.eclipse.cdt.core.ccnature</nature>
<nature>org.eclipse.cdt.managedbuilder.core.managedBuildNature</nature>
<nature>org.eclipse.cdt.managedbuilder.core.ScannerConfigNature</nature>
</natures>
<filteredResources>
<filter>
<id>1729128501799</id>
<name></name>
<type>26</type>
<matcher>
<id>org.eclipse.ui.ide.multiFilter</id>
<arguments>1.0-projectRelativePath-matches-false-false-*/.tpk</arguments>
</matcher>
</filter>
<filter>
<id>1729128501802</id>
<name></name>
<type>6</type>
<matcher>
<id>org.eclipse.ui.ide.multiFilter</id>
<arguments>1.0-name-matches-false-false-project_def.prop</arguments>
</matcher>
</filter>
</filteredResources>
</projectDescription>
12 changes: 12 additions & 0 deletions Tizen.web/TextGenerationLlama2/NsdService/.tproject
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<tproject xmlns="http://www.tizen.org/tproject">
<platforms>
<platform>
<name>tizen-8.0</name>
</platform>
</platforms>
<package>
<blacklist/>
<resFallback autoGen="true"/>
</package>
</tproject>
21 changes: 21 additions & 0 deletions Tizen.web/TextGenerationLlama2/NsdService/inc/nsdservice.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
* @file main.h
* @date 04 Jul 2024
* @brief Tizen native service for hybrid application
* @author Yelin Jeong <[email protected]>
* @bug No known bugs.
*/

#ifndef __NSD_SERVICE_H__
#define __NSD_SERVICE_H__

#include <dlog.h>

#ifdef LOG_TAG
#undef LOG_TAG
#endif
#define LOG_TAG "nsd_service"

#define REMOTE_APP_ID "lfebC6UrMY.Llama2"

#endif /* __NSD_SERVICE_H__ */
11 changes: 11 additions & 0 deletions Tizen.web/TextGenerationLlama2/NsdService/project_def.prop
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
APPNAME = nsdservice

type = app
profile = tizen-8.0

USER_SRCS = src/nsdservice.c
USER_DEFS =
USER_INC_DIRS = inc
USER_OBJS =
USER_LIBS =
USER_EDCS =
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
170 changes: 170 additions & 0 deletions Tizen.web/TextGenerationLlama2/NsdService/src/nsdservice.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
/**
* @file main.c
* @date 18 October 2024
* @brief Tizen native service for hybrid application
* @author Yelin Jeong <[email protected]>
* @bug No known bugs.
*/

#include "nsdservice.h"
#include <bundle.h>
#include <dns-sd.h>
#include <message_port.h>
#include <service_app.h>
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <tizen.h>

/**
* @brief Service app create callback
*/
static bool _create_cb(void *user_data) {
dlog_print(DLOG_INFO, LOG_TAG, "Callback create\n");
return true;
}

/**
* @brief Service app terminate callback
*/
static void _terminate_cb(void *user_data) {
dlog_print(DLOG_INFO, LOG_TAG, "Callback terminate\n");
}

/**
* @brief Service app app control callback
*/
static void _app_control_cb(app_control_h app_control, void *user_data) {
dlog_print(DLOG_INFO, LOG_TAG, "Callback app_control\n");
}

/**
* @brief Send dnssd remote service information
*/
void _dnssd_send_found(dnssd_service_h dnssd_remote_service, bool is_available) {
char *service_name = NULL;
char *service_type = NULL;
char *ip_v4_address = NULL;
char *ip_v6_address = NULL;
int ret, port = 0;

ret = dnssd_service_get_type(dnssd_remote_service, &service_type);
if (ret != DNSSD_ERROR_NONE || service_type == NULL) {
dlog_print(DLOG_ERROR, LOG_TAG, "Failed to get service type (%d)", ret);
return;
}

ret = dnssd_service_get_name(dnssd_remote_service, &service_name);
if (ret != DNSSD_ERROR_NONE || service_name == NULL) {
dlog_print(DLOG_ERROR, LOG_TAG, "Failed to get service name (%d)", ret);
return;
}

ret = dnssd_service_get_ip(dnssd_remote_service, &ip_v4_address,
&ip_v6_address);
if (ret != DNSSD_ERROR_NONE || ip_v4_address == NULL) {
dlog_print(DLOG_ERROR, LOG_TAG, "Failed to get IP address (%d)", ret);
return;
}

ret = dnssd_service_get_port(dnssd_remote_service, &port);
if (ret != DNSSD_ERROR_NONE) {
dlog_print(DLOG_ERROR, LOG_TAG, "Failed to get port number (%d)", ret);
return;
}

char port_buffer[CHAR_BIT];
snprintf(port_buffer, CHAR_BIT, "%d", port);

bundle *b = bundle_create();
bundle_add_str(b, "name", service_name);
bundle_add_str(b, "ip", ip_v4_address);
bundle_add_str(b, "port", port_buffer);

if (is_available)
ret = message_port_send_message(REMOTE_APP_ID, "STATE_AVAILABLE", b);
else
ret = message_port_send_message(REMOTE_APP_ID, "STATE_UNAVAILABLE", b);

if (ret != MESSAGE_PORT_ERROR_NONE) {
dlog_print(DLOG_ERROR, LOG_TAG, "Failed to send message to %s",
REMOTE_APP_ID);
}
bundle_free(b);

if (service_name)
free(service_name);
if (service_type)
free(service_type);
if (ip_v4_address)
free(ip_v4_address);
if (ip_v6_address)
free(ip_v6_address);
}

/**
* @brief dnssd found callback
*/
void _found_cb(dnssd_service_state_e state,
dnssd_service_h dnssd_remote_service, void *user_data) {
switch (state) {
case DNSSD_SERVICE_STATE_AVAILABLE:
/* DNS-SD service found */
_dnssd_send_found(dnssd_remote_service, true);
break;
case DNSSD_SERVICE_STATE_UNAVAILABLE:
/* DNS-SD service becomes unavailable */
_dnssd_send_found(dnssd_remote_service, false);
break;
case DNSSD_SERVICE_STATE_NAME_LOOKUP_FAILED:
/* Browsing failed */
dlog_print(DLOG_ERROR, LOG_TAG, "Browse Failure\n");
break;
case DNSSD_SERVICE_STATE_HOST_NAME_LOOKUP_FAILED:
/* Resolving service name failed */
dlog_print(DLOG_ERROR, LOG_TAG, "Resolve Service Name Failure\n");
break;
case DNSSD_SERVICE_STATE_ADDRESS_LOOKUP_FAILED:
/* Resolving service address failed */
dlog_print(DLOG_ERROR, LOG_TAG, "Resolve Service Address\n");
break;
default:
dlog_print(DLOG_ERROR, LOG_TAG, "Unknown Browse State\n");
break;
}
}

/**
* @brief Main function.
*/
int main(int argc, char *argv[]) {
dnssd_browser_h browser_handle;
char *target = "_nsd_offloading._tcp";
int ret;
bool found;

service_app_lifecycle_callback_s event_callback = {.create = _create_cb,
.terminate = _terminate_cb,
.app_control =
_app_control_cb};

ret = dnssd_initialize();
if (ret != DNSSD_ERROR_NONE)
dlog_print(DLOG_ERROR, LOG_TAG, "Dnssd initialize failed");

ret = dnssd_start_browsing_service(target, &browser_handle, _found_cb, NULL);
if (ret == DNSSD_ERROR_NONE)
dlog_print(DLOG_DEBUG, LOG_TAG, "Start browsing");

ret = message_port_check_remote_port(REMOTE_APP_ID, "STATE_AVAILABLE", &found);
if (ret != MESSAGE_PORT_ERROR_NONE || !found) {
dlog_print(DLOG_ERROR, LOG_TAG, "Failed to check remote port");
}

ret = message_port_check_remote_port(REMOTE_APP_ID, "STATE_UNAVAILABLE", &found);
if (ret != MESSAGE_PORT_ERROR_NONE || !found) {
dlog_print(DLOG_ERROR, LOG_TAG, "Failed to check remote port");
}

return service_app_main(argc, argv, &event_callback, NULL);
}
11 changes: 11 additions & 0 deletions Tizen.web/TextGenerationLlama2/NsdService/tizen-manifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns="http://tizen.org/ns/packages" api-version="8.0" package="lfebC6UrMY.nsdservice" version="1.0.0">
<profile name="tizen" />
<service-application appid="lfebC6UrMY.nsdservice" exec="nsdservice" type="capp" multiple="false" taskmanage="false" nodisplay="true">
<icon>nsdservice.png</icon>
<label>nsdservice</label>
</service-application>
<privileges>
<privilege>http://tizen.org/privilege/internet</privilege>
</privileges>
</manifest>
19 changes: 19 additions & 0 deletions Tizen.web/TextGenerationLlama2/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Text Generation Sample App
## Description
* This is a sample application of Tizen ML offloading.
* If you want to run it on your device, Tizen 8.0 or higher is required.
* `appsrc` and `tensor_query_client` element are used.

## How to build and run
```sh
$ tizen build-web -- WebApplication
$ tizen package -t wgt -- WebApplication/.buildResult/
$ tizen build-native -a arm -c llvm -C Debug -- NsdService/
$ tizen package -t tpk -- NsdService/Debug/
$ tizen package -t wgt -r NsdService/Debug/lfebC6UrMY.nsdservice-1.0.0-arm.tpk -- WebApplication/.buildResult/Llama2.wgt
$ tizen install -n WebApplication/.buildResult/Llama2.wgt -t rpi4
$ tizen run -p lfebC6UrMY -t rpi4
```

## Demo
![Alt demo](./text_generation.png)
24 changes: 24 additions & 0 deletions Tizen.web/TextGenerationLlama2/WebApplication/.project
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>Llama2</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>json.validation.builder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.tizen.web.project.builder.WebBuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>json.validation.nature</nature>
<nature>org.eclipse.wst.jsdt.core.jsNature</nature>
<nature>org.tizen.web.project.builder.WebNature</nature>
</natures>
</projectDescription>
11 changes: 11 additions & 0 deletions Tizen.web/TextGenerationLlama2/WebApplication/.tproject
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<tproject xmlns="http://www.tizen.org/tproject">
<platforms>
<platform>
<name>tizen-8.0</name>
</platform>
</platforms>
<package>
<blacklist/>
</package>
</tproject>
13 changes: 13 additions & 0 deletions Tizen.web/TextGenerationLlama2/WebApplication/config.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<widget xmlns:tizen="http://tizen.org/ns/widgets" xmlns="http://www.w3.org/ns/widgets" id="http://yourdomain/Llama2" version="1.0.0" viewmodes="maximized">
<tizen:application id="lfebC6UrMY.Llama2" package="lfebC6UrMY" required_version="8.0"/>
<content src="index.html"/>
<feature name="http://tizen.org/feature/screen.size.all"/>
<icon src="icon.png"/>
<name>Llama2</name>
<tizen:privilege name="http://tizen.org/privilege/filesystem.read"/>
<tizen:privilege name="http://tizen.org/privilege/filesystem.write"/>
<tizen:privilege name="http://tizen.org/privilege/mediastorage"/>
<tizen:privilege name="http://tizen.org/privilege/internet"/>
<tizen:profile name="tizen"/>
</widget>
37 changes: 37 additions & 0 deletions Tizen.web/TextGenerationLlama2/WebApplication/css/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
html,
body {
width: 100%;
height: 100%;
margin: auto;
padding: 10px 10px 10px 10px;
background-color: #222222;
color: #ffffff;
}
.page {
width: 100%;
height: 100%;
}
.contents {
display: table-cell;
vertical-align: middle;
text-align: center;
-webkit-tap-highlight-color: transparent;
}
.button {
padding: 20px 100px;
font-size: 30px;
margin: auto;
}
.input {
padding: 20px 2px;
font-size: 30px;
margin: auto;
}
.output {
width: 80%;
font-size: 30px;
}
#content-text {
font-weight: bold;
font-size: 5em;
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading