Skip to content

Commit 3f7800a

Browse files
committed
[ml service] Add Flare as a new nnfw type
- Implemented support for 'flare' nnfw type in ML service API - Included test cases to validate flare functionality Signed-off-by: hyunil park <hyunil46.park@samsung.com>
1 parent 920ebc4 commit 3f7800a

File tree

4 files changed

+73
-23
lines changed

4 files changed

+73
-23
lines changed

c/include/nnstreamer-tizen-internal.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
extern "C" {
2020
#endif /* __cplusplus */
2121

22+
#define ML_NNFW_TYPE_FLARE 23 /**< FLARE framework */
23+
2224
/**
2325
* @brief Constructs the pipeline (GStreamer + NNStreamer).
2426
* @details This function is to construct the pipeline without checking the permission in platform internally. See ml_pipeline_construct() for the details.

c/src/ml-api-inference-single.c

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ static const char *ml_nnfw_subplugin_name[] = {
114114
[ML_NNFW_TYPE_QNN] = "qnn",
115115
[ML_NNFW_TYPE_LLAMACPP] = "llamacpp",
116116
[ML_NNFW_TYPE_TIZEN_HAL] = "tizen-hal",
117+
[ML_NNFW_TYPE_FLARE] = "flare",
117118
NULL
118119
};
119120

@@ -979,16 +980,24 @@ ml_single_open_custom (ml_single_h * single, ml_single_preset * info)
979980
for (i = 0; i < num_models; i++)
980981
g_strstrip (list_models[i]);
981982

982-
status = _ml_validate_model_file ((const char **) list_models, num_models,
983-
&nnfw);
984-
if (status != ML_ERROR_NONE) {
985-
_ml_error_report_continue
986-
("Cannot validate the model (1st model: %s. # models: %d). Error code: %d",
987-
list_models[0], num_models, status);
988-
g_strfreev (list_models);
989-
return status;
983+
/* Note : OpenVINO and flare use the bin extension.
984+
* _ml_validate_model_file() infers nnfw based on the file extension.
985+
* The .bin extension is recognized as OpenVINO (ML_NNFW_TYPE_OPENVINO) by default
986+
* If "flare" is specified, it forces ML_NNFW_TYPE_FLARE.
987+
*/
988+
if (info->fw_name && strcasecmp (info->fw_name, "flare") == 0) {
989+
nnfw = ML_NNFW_TYPE_FLARE;
990+
} else {
991+
status = _ml_validate_model_file ((const char **) list_models, num_models,
992+
&nnfw);
993+
if (status != ML_ERROR_NONE) {
994+
_ml_error_report_continue
995+
("Cannot validate the model (1st model: %s. # models: %d). Error code: %d",
996+
list_models[0], num_models, status);
997+
g_strfreev (list_models);
998+
return status;
999+
}
9901000
}
991-
9921001
g_strfreev (list_models);
9931002

9941003
/**
@@ -1089,14 +1098,14 @@ ml_single_open_custom (ml_single_h * single, ml_single_preset * info)
10891098
}
10901099

10911100
if (single_h->klass && info->invoke_async) {
1092-
if (info->invoke_async_cb != NULL && info->invoke_async_data!= NULL) {
1101+
if (info->invoke_async_cb != NULL && info->invoke_async_data != NULL) {
10931102
NNSFilterInvokeAsyncCallback invoke_async_cb =
10941103
(NNSFilterInvokeAsyncCallback) info->invoke_async_cb;
10951104
single_h->klass->set_invoke_async_callback (single_h->filter,
10961105
invoke_async_cb, info->invoke_async_data);
10971106
} else {
10981107
_ml_error_report
1099-
("The parameters invoke_async_cb and invoke_async_data in the info argument are invalid");
1108+
("The parameters invoke_async_cb and invoke_async_data in the info argument are invalid");
11001109
status = ML_ERROR_INVALID_PARAMETER;
11011110
goto error;
11021111
}

tests/capi/unittest_capi_service_extension.cc

Lines changed: 40 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
#include <gtest/gtest.h>
1111
#include <glib.h>
12-
12+
#include <iostream>
1313
#include <ml-api-service-private.h>
1414
#include <ml-api-service.h>
1515
#include "ml-api-service-extension.h"
@@ -394,8 +394,7 @@ _extension_test_imgclf (ml_service_h handle, gboolean is_pipeline)
394394
* @brief Callback function for scenario test.
395395
*/
396396
static void
397-
_extension_test_llamacpp_cb (
398-
ml_service_event_e event, ml_information_h event_data, void *user_data)
397+
_extension_test_llm_cb (ml_service_event_e event, ml_information_h event_data, void *user_data)
399398
{
400399
extension_test_data_s *tdata = (extension_test_data_s *) user_data;
401400
ml_tensors_data_h data = NULL;
@@ -413,7 +412,8 @@ _extension_test_llamacpp_cb (
413412
status = ml_tensors_data_get_tensor_data (data, 0U, &_raw, &_size);
414413
EXPECT_EQ (status, ML_ERROR_NONE);
415414

416-
g_print ("%s", (char *) _raw);
415+
std::cout.write (static_cast<const char *> (_raw), _size); /* korean output */
416+
std::cout.flush ();
417417

418418
if (tdata)
419419
tdata->received++;
@@ -427,7 +427,7 @@ _extension_test_llamacpp_cb (
427427
* @brief Internal function to run test with ml-service extension handle.
428428
*/
429429
static inline void
430-
_extension_test_llamacpp (ml_service_h handle, gboolean is_pipeline)
430+
_extension_test_llm (ml_service_h handle, gboolean is_pipeline, gchar *file_name, guint sleep_us)
431431
{
432432
extension_test_data_s *tdata;
433433
ml_tensors_info_h info;
@@ -436,14 +436,14 @@ _extension_test_llamacpp (ml_service_h handle, gboolean is_pipeline)
436436
gsize len = 0;
437437
gchar *contents = NULL;
438438

439-
g_autofree gchar *data_file = _get_data_path ("input.txt");
439+
g_autofree gchar *data_file = _get_data_path (file_name);
440440
ASSERT_TRUE (g_file_test (data_file, G_FILE_TEST_EXISTS));
441441
ASSERT_TRUE (g_file_get_contents (data_file, &contents, &len, NULL));
442442

443443
tdata = _create_test_data (is_pipeline);
444444
ASSERT_TRUE (tdata != NULL);
445445

446-
status = ml_service_set_event_cb (handle, _extension_test_llamacpp_cb, tdata);
446+
status = ml_service_set_event_cb (handle, _extension_test_llm_cb, tdata);
447447
EXPECT_EQ (status, ML_ERROR_NONE);
448448

449449
/* Create and push input data. */
@@ -457,7 +457,7 @@ _extension_test_llamacpp (ml_service_h handle, gboolean is_pipeline)
457457
status = ml_service_request (handle, NULL, input);
458458
EXPECT_EQ (status, ML_ERROR_NONE);
459459

460-
g_usleep (5000000U);
460+
g_usleep (sleep_us);
461461
EXPECT_GT (tdata->received, 0);
462462

463463
/* Clear callback before releasing tdata. */
@@ -477,8 +477,9 @@ TEST_REQUIRE_TFLITE (MLServiceExtension, scenarioConfigLlamacpp)
477477
{
478478
ml_service_h handle;
479479
int status;
480-
480+
g_autofree gchar *input_file = g_strdup ("input.txt");
481481
g_autofree gchar *model_file = _get_model_path ("llama-2-7b-chat.Q2_K.gguf");
482+
482483
if (!g_file_test (model_file, G_FILE_TEST_EXISTS)) {
483484
g_critical ("Skipping scenarioConfigLlamacpp test due to missing model file. "
484485
"Please download model file from https://huggingface.co/TheBloke/Llama-2-7B-Chat-GGUF");
@@ -490,7 +491,7 @@ TEST_REQUIRE_TFLITE (MLServiceExtension, scenarioConfigLlamacpp)
490491
status = ml_service_new (config, &handle);
491492
ASSERT_EQ (status, ML_ERROR_NONE);
492493

493-
_extension_test_llamacpp (handle, FALSE);
494+
_extension_test_llm (handle, FALSE, input_file, 5000000U);
494495

495496
status = ml_service_destroy (handle);
496497
EXPECT_EQ (status, ML_ERROR_NONE);
@@ -503,8 +504,9 @@ TEST_REQUIRE_TFLITE (MLServiceExtension, scenarioConfigLlamacppAsync)
503504
{
504505
ml_service_h handle;
505506
int status;
506-
507+
g_autofree gchar *input_file = g_strdup ("input.txt");
507508
g_autofree gchar *model_file = _get_model_path ("llama-2-7b-chat.Q2_K.gguf");
509+
508510
if (!g_file_test (model_file, G_FILE_TEST_EXISTS)) {
509511
g_critical ("Skipping scenarioConfigLlamacppAsync test due to missing model file. "
510512
"Please download model file from https://huggingface.co/TheBloke/Llama-2-7B-Chat-GGUF");
@@ -516,7 +518,33 @@ TEST_REQUIRE_TFLITE (MLServiceExtension, scenarioConfigLlamacppAsync)
516518
status = ml_service_new (config, &handle);
517519
ASSERT_EQ (status, ML_ERROR_NONE);
518520

519-
_extension_test_llamacpp (handle, FALSE);
521+
_extension_test_llm (handle, FALSE, input_file, 5000000U);
522+
523+
status = ml_service_destroy (handle);
524+
EXPECT_EQ (status, ML_ERROR_NONE);
525+
}
526+
527+
/**
528+
* @brief Usage of ml-service extension API.
529+
*/
530+
TEST_REQUIRE_TFLITE (MLServiceExtension, scenarioConfigFlare)
531+
{
532+
ml_service_h handle;
533+
int status;
534+
g_autofree gchar *input_file = g_strdup ("flare_input.txt");
535+
g_autofree gchar *model_file = _get_model_path ("sflare_if_4bit_3b.bin");
536+
537+
if (!g_file_test (model_file, G_FILE_TEST_EXISTS)) {
538+
g_critical ("Skipping scenarioConfigFlare test due to missing model file.Please download model file");
539+
return;
540+
}
541+
542+
g_autofree gchar *config = get_config_path ("config_single_flare.conf");
543+
544+
status = ml_service_new (config, &handle);
545+
ASSERT_EQ (status, ML_ERROR_NONE);
546+
547+
_extension_test_llm (handle, FALSE, input_file, 40000000U);
520548

521549
status = ml_service_destroy (handle);
522550
EXPECT_EQ (status, ML_ERROR_NONE);
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"single" :
3+
{
4+
"framework" : "flare",
5+
"model" : ["../tests/test_models/models/sflare_if_4bit_3b.bin"],
6+
"adapter" : ["../tests/test_models/models/history_lora.bin"],
7+
"custom" : "tokenizer_path:../tests/test_models/data/tokenizer.json,backend:CPU,output_size:1024,model_type:3B,data_type:W4A32",
8+
"invoke_dynamic" : "true",
9+
"invoke_async" : "false"
10+
}
11+
}

0 commit comments

Comments
 (0)