Skip to content

Commit 75b6afd

Browse files
authored
[tests/tools] Introduce ggma_run (#16270)
It adds ggma_run tools which uses GGMA api to generate text from given prompt. ONE-DCO-1.0-Signed-off-by: Sanggyu Lee <sg5.lee@samsung.com>
1 parent a0bf373 commit 75b6afd

File tree

8 files changed

+277
-0
lines changed

8 files changed

+277
-0
lines changed

runtime/infra/cmake/CfgOptionFlags.cmake

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ option(BUILD_RUNTIME_NNAPI_TEST "Build Runtime NN API Generated Test" ON)
2929
option(BUILD_RUNTIME_NNFW_API_TEST "Build Runtime NNFW API Tests" ON)
3030
option(BUILD_TFLITE_RUN "Build tflite_run test driver" ON)
3131
option(BUILD_ONERT_RUN "Build onert_run test driver" ON)
32+
option(BUILD_GGMA_RUN "Build ggma_run test driver" ON)
3233
option(BUILD_ONERT_TRAIN "Build onert_train test driver" ON)
3334
option(BUILD_TFLITE_COMPARATOR_TEST_TOOL "Build testing tool to compare runtime result with TFLite" ON)
3435
option(BUILD_WITH_HDF5 "Build test tool with HDF5 library" ON)

runtime/infra/cmake/options/options_armv7l-tizen.cmake

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,6 @@ option(ENVVAR_ONERT_CONFIG "Use environment variable for onert configuration" OF
1010
option(BUILD_XNNPACK "Build XNNPACK" OFF)
1111

1212
option(BUILD_GGMA_API "Build GGMA API for Generative AI" OFF)
13+
option(BUILD_GGMA_RUN "Build ggma_run test driver" OFF)
1314
option(DOWNLOAD_SENTENCEPIECE "Download SentencePiece source" OFF)
1415
option(BUILD_SENTENCEPIECE "Build SentencePiece library from the source" OFF)

runtime/infra/cmake/options/options_i686-tizen.cmake

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,6 @@ option(ENVVAR_ONERT_CONFIG "Use environment variable for onert configuration" OF
99
option(BUILD_XNNPACK "Build XNNPACK" OFF)
1010

1111
option(BUILD_GGMA_API "Build GGMA API for Generative AI" OFF)
12+
option(BUILD_GGMA_RUN "Build ggma_run test driver" OFF)
1213
option(DOWNLOAD_SENTENCEPIECE "Download SentencePiece source" OFF)
1314
option(BUILD_SENTENCEPIECE "Build SentencePiece library from the source" OFF)
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
if(NOT BUILD_GGMA_RUN)
2+
return()
3+
endif(NOT BUILD_GGMA_RUN)
4+
5+
list(APPEND GGMA_RUN_SRCS "src/args.cc")
6+
list(APPEND GGMA_RUN_SRCS "src/ggma_run.cc")
7+
8+
add_executable(ggma_run ${GGMA_RUN_SRCS})
9+
10+
target_include_directories(ggma_run PRIVATE src)
11+
12+
target_link_libraries(ggma_run arser)
13+
target_link_libraries(ggma_run ggma-dev)
14+
target_link_libraries(ggma_run nnfw_common)
15+
target_link_libraries(ggma_run nnfw-dev)
16+
17+
# Set RPATH to find GGMA library in lib/ggma directory
18+
set_target_properties(ggma_run PROPERTIES
19+
INSTALL_RPATH "$ORIGIN/../lib/ggma:$ORIGIN/../lib/:$ORIGIN/:/usr/local/lib"
20+
)
21+
22+
install(TARGETS ggma_run DESTINATION ${CMAKE_INSTALL_BINDIR})
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# ggma_run
2+
3+
`ggma_run` is a tool to run LLM model.
4+
5+
It takes GGMA package as input. It uses **GGMA API** internally.
6+
7+
## Usage
8+
9+
```
10+
$ ./ggma_run path_to_ggma_package
11+
```
12+
13+
It will run a GGML package to generate the output using the default prompt.
14+
15+
## Example
16+
17+
```
18+
$ Product/out/bin/ggma_run tinyllama
19+
prompt: Lily picked up a flower.
20+
generated: { 1100, 7899, 289, 826, 351, 600, 2439, 288, 266, 3653, 31843, 1100, 7899, 289, 1261, 291, 5869, 291, 1261, 31843, 1100, 7899 }
21+
detokenized: She liked to play with her friends. She liked to run and jump in the water. She was
22+
```
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/*
2+
* Copyright (c) 2025 Samsung Electronics Co., Ltd. All Rights Reserved
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#include "args.h"
18+
#include "nnfw.h"
19+
20+
#include <iostream>
21+
#include <string>
22+
#include <sys/stat.h>
23+
24+
#define NNPR_ENSURE_STATUS(a) \
25+
do \
26+
{ \
27+
if ((a) != NNFW_STATUS_NO_ERROR) \
28+
{ \
29+
exit(-1); \
30+
} \
31+
} while (0)
32+
33+
static void print_version()
34+
{
35+
uint32_t version;
36+
NNPR_ENSURE_STATUS(nnfw_query_info_u32(NULL, NNFW_INFO_ID_VERSION, &version));
37+
std::cout << "ggma_run v0.1.0 (nnfw runtime: v" << (version >> 24) << "."
38+
<< ((version & 0x0000FF00) >> 8) << "." << (version & 0xFF) << ")" << std::endl;
39+
}
40+
41+
namespace ggma_run
42+
{
43+
44+
Args::Args(const int argc, char **argv)
45+
{
46+
initialize();
47+
parse(argc, argv);
48+
}
49+
50+
void Args::initialize(void)
51+
{
52+
_arser.add_argument("path").type(arser::DataType::STR).help("nnpackage path");
53+
arser::Helper::add_version(_arser, print_version);
54+
}
55+
56+
void Args::parse(const int argc, char **argv)
57+
{
58+
try
59+
{
60+
_arser.parse(argc, argv);
61+
62+
if (_arser.get<bool>("--version"))
63+
{
64+
_print_version = true;
65+
return;
66+
}
67+
68+
if (_arser["path"])
69+
{
70+
auto path = _arser.get<std::string>("path");
71+
struct stat sb;
72+
if (stat(path.c_str(), &sb) == 0)
73+
{
74+
if (sb.st_mode & S_IFDIR)
75+
_package_path = path;
76+
}
77+
else
78+
{
79+
std::cerr << "Cannot find: " << path << "\n";
80+
exit(1);
81+
}
82+
}
83+
}
84+
catch (const std::bad_cast &e)
85+
{
86+
std::cerr << "Bad cast error - " << e.what() << '\n';
87+
exit(1);
88+
}
89+
}
90+
91+
} // end of namespace ggma_run
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* Copyright (c) 2025 Samsung Electronics Co., Ltd. All Rights Reserved
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#ifndef __GGMA_RUN_ARGS_H__
18+
#define __GGMA_RUN_ARGS_H__
19+
20+
#include "arser/arser.h"
21+
22+
#include <string>
23+
#include <unordered_map>
24+
#include <vector>
25+
26+
namespace ggma_run
27+
{
28+
29+
class Args
30+
{
31+
public:
32+
Args(const int argc, char **argv);
33+
void print(void);
34+
35+
const std::string &packagePath() const { return _package_path; }
36+
bool printVersion() const { return _print_version; }
37+
38+
private:
39+
void initialize();
40+
void parse(const int argc, char **argv);
41+
42+
private:
43+
arser::Arser _arser;
44+
45+
std::string _package_path;
46+
bool _print_version = false;
47+
};
48+
49+
} // end of namespace ggma_run
50+
51+
#endif // __GGMA_RUN_ARGS_H__
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/*
2+
* Copyright (c) 2025 Samsung Electronics Co., Ltd. All Rights Reserved
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#include "args.h"
18+
#include "ggma_api.h"
19+
20+
#include <cstdlib>
21+
#include <iostream>
22+
#include <stdexcept>
23+
24+
#define GGMA_ENSURE(a) \
25+
do \
26+
{ \
27+
if ((a) != GGMA_STATUS_NO_ERROR) \
28+
{ \
29+
exit(-1); \
30+
} \
31+
} while (0)
32+
33+
int main(const int argc, char **argv)
34+
{
35+
using namespace ggma_run;
36+
37+
try
38+
{
39+
Args args(argc, argv);
40+
41+
std::string prompt = "Lily picked up a flower.";
42+
constexpr size_t n_tokens_max = 32;
43+
ggma_token tokens[n_tokens_max];
44+
size_t n_tokens;
45+
46+
// Create tokenizer first
47+
ggma_tokenizer *tokenizer = nullptr;
48+
GGMA_ENSURE(ggma_create_tokenizer(&tokenizer, (args.packagePath() + "/tokenizer").c_str()));
49+
50+
// Tokenize using the created tokenizer
51+
GGMA_ENSURE(
52+
ggma_tokenize(tokenizer, prompt.c_str(), prompt.size(), tokens, n_tokens_max, &n_tokens));
53+
54+
ggma_context *context = nullptr;
55+
GGMA_ENSURE(ggma_create_context(&context, args.packagePath().c_str()));
56+
57+
size_t n_predict = 22;
58+
GGMA_ENSURE(ggma_generate(context, tokens, n_tokens, n_tokens_max, &n_predict));
59+
60+
// Output generated token IDs
61+
std::cout << "prompt: " << prompt << std::endl;
62+
std::cout << "generated: { ";
63+
for (size_t i = n_tokens; i < n_tokens + n_predict; ++i)
64+
{
65+
std::cout << tokens[i];
66+
if (i < n_tokens + n_predict - 1)
67+
{
68+
std::cout << ", ";
69+
}
70+
}
71+
std::cout << " }" << std::endl;
72+
73+
// Detokenize and output the generated text
74+
constexpr size_t detokenize_max = 256;
75+
char detokenized[detokenize_max];
76+
GGMA_ENSURE(
77+
ggma_detokenize(tokenizer, tokens + n_tokens, n_predict, detokenized, detokenize_max));
78+
std::cout << "detokenized: " << detokenized << std::endl;
79+
80+
GGMA_ENSURE(ggma_free_context(context));
81+
GGMA_ENSURE(ggma_free_tokenizer(tokenizer));
82+
}
83+
catch (std::runtime_error &e)
84+
{
85+
std::cerr << "E: Fail to run by runtime error: " << e.what() << std::endl;
86+
exit(-1);
87+
}
88+
}

0 commit comments

Comments
 (0)