Skip to content

Commit 9a0be65

Browse files
committed
[ggma] Introduce GGMA api
It introduces GGMA, which provides high-level API for text generation. It uses ONERT internally (nnpkg, nnfw api, ...) to run kernels. ONE-DCO-1.0-Signed-off-by: Sanggyu Lee <sg5.lee@samsung.com>
1 parent c4ab691 commit 9a0be65

18 files changed

Lines changed: 1375 additions & 2 deletions

packaging/nnfw.spec

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ Source2001: EXTERNALS_FOR_ODC.tar.gz
2121
%{!?odc_build: %define odc_build 1}
2222
%endif
2323
%{!?test_build: %define test_build 0}
24+
%{!?ggma_build: %define ggma_build 1}
2425
%{!?extra_option: %define extra_option %{nil}}
2526
# Define nproc on gbs build option if you want to set number of build threads manually (ex. CI/CD infra)
2627
%define build_jobs %{?!nproc:%{?_smp_mflags}%{?!_smp_mflags:-j4}}%{?nproc:-j%nproc}
@@ -89,6 +90,23 @@ Summary: NNFW On-Device Compilation Package
8990
NNFW package for on-device compilation
9091
%endif # odc_build
9192

93+
%if %{ggma_build} == 1
94+
%package ggma
95+
Summary: GGMA generative AI framework
96+
97+
%description ggma
98+
GGMA package for on-device generative AI framework
99+
100+
%files ggma
101+
%manifest %{name}.manifest
102+
%defattr(-,root,root,-)
103+
%ifarch arm armv7l armv7hl aarch64 x86_64 %ix86 riscv64
104+
%{_libdir}/ggma/libggma_api.so
105+
%{_includedir}/ggma/*
106+
%{_libdir}/pkgconfig/ggma.pc
107+
%endif
108+
%endif # ggma_build
109+
92110
%if %{test_build} == 1
93111
%package test
94112
Summary: NNFW Test
@@ -136,11 +154,17 @@ If you want to use test package, you should install runtime package which is bui
136154
%define option_config -DASAN_BUILD=ON
137155
%endif # asan
138156

157+
# Set option for GGMA build
158+
%define option_ggma -DBUILD_GGMA_API=OFF
159+
%if %{ggma_build} == 1
160+
%define option_ggma -DBUILD_GGMA_API=ON
161+
%endif # ggma_build
162+
139163
# CMAKE_INSTALL_PREFIX is used for config files
140164
# Actual install path is set by --prefix option in cmake install command
141165
%define build_options -DCMAKE_BUILD_TYPE=%{build_type} -DTARGET_ARCH=%{target_arch} -DTARGET_OS=tizen \\\
142166
-DEXTERNALS_BUILD_THREAD=%{nproc} -DBUILD_MINIMAL_SAMPLE=OFF -DNNFW_OVERLAY_DIR=$(pwd)/%{overlay_path} \\\
143-
-DCMAKE_INSTALL_PREFIX=%{_prefix} %{option_test} %{option_config} %{extra_option}
167+
-DCMAKE_INSTALL_PREFIX=%{_prefix} %{option_test} %{option_config} %{option_ggma} %{extra_option}
144168

145169
%prep
146170
%setup -q
@@ -202,6 +226,9 @@ install -m 644 %{overlay_path}/lib/libloco*.so %{buildroot}%{_libdir}/nnfw/odc
202226
%endif # asan
203227
%endif # odc_build
204228

229+
%if %{ggma_build} == 1
230+
%endif # ggma_build
231+
205232
%endif
206233

207234
%post -p /sbin/ldconfig

runtime/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,5 +153,6 @@ add_subdirectory(compute)
153153
add_subdirectory(contrib)
154154
add_subdirectory(libs)
155155
add_subdirectory(onert)
156+
add_subdirectory(ggma)
156157
add_subdirectory(tests)
157158
add_subdirectory(tools)

runtime/ggma/CMakeLists.txt

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
if (NOT BUILD_GGMA_API)
2+
return()
3+
endif()
4+
5+
file(GLOB_RECURSE API_SRC "src/*.cc")
6+
7+
set(GGMA_DEV ggma-dev)
8+
add_library(${GGMA_DEV} SHARED ${API_SRC})
9+
10+
# Public headers to publish
11+
set(GGMA_API_HEADERS include/ggma_api.h include/ggma_macro.h include/ggma_types.h
12+
include/ggma_context.h include/ggma_generate.h)
13+
14+
# GGMA install directory (same as ONERT_INSTALL_APIDIR)
15+
set(GGMA_INSTALL_LIBDIR ${CMAKE_INSTALL_LIBDIR}/ggma)
16+
set(GGMA_INSTALL_APIDIR ${CMAKE_INSTALL_LIBDIR}/ggma)
17+
18+
target_link_libraries(${GGMA_DEV} PRIVATE jsoncpp ${LIB_PTHREAD})
19+
target_link_libraries(${GGMA_DEV} PRIVATE nnfw-dev)
20+
21+
# NOTE Below line is added to remove warning for android build
22+
# It will be removed after android build uses gold linker
23+
if (ANDROID)
24+
target_link_libraries(${GGMA_DEV} INTERFACE log)
25+
endif (ANDROID)
26+
27+
target_include_directories(${GGMA_DEV} PUBLIC include)
28+
29+
# GGMA RPATH configuration: find ggma libraries
30+
set(GGMA_RPATH_API "$ORIGIN:$ORIGIN/..")
31+
32+
set_target_properties(${GGMA_DEV} PROPERTIES
33+
PUBLIC_HEADER "${GGMA_API_HEADERS}"
34+
OUTPUT_NAME ggma_api
35+
INSTALL_RPATH ${GGMA_RPATH_API})
36+
37+
install(TARGETS ${GGMA_DEV}
38+
LIBRARY DESTINATION ${GGMA_INSTALL_APIDIR}
39+
PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/ggma)
40+
41+
# Install pkg-config file for GGMA API
42+
configure_file(ggma.pc.in ggma.pc @ONLY)
43+
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/ggma.pc DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig)

runtime/ggma/ggma.pc.in

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
prefix=@CMAKE_INSTALL_PREFIX@
2+
libdir=${prefix}/@GGMA_INSTALL_APIDIR@
3+
includedir=${prefix}/@CMAKE_INSTALL_INCLUDEDIR@
4+
version=@CMAKE_PROJECT_VERSION@
5+
6+
Name: ggma
7+
Description: GGMA API
8+
Version: ${version}
9+
Libs: -L${libdir} -lggma
10+
Cflags: -I${includedir}/ggma

runtime/ggma/include/ggma_api.h

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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+
/**
18+
* @file ggma_api.h
19+
* @brief This file defines the Generative Model API (GGMA) - main include header.
20+
*/
21+
#ifndef __GGMA_GGMA_API_H__
22+
#define __GGMA_GGMA_API_H__
23+
24+
/* Include all GGMA API headers */
25+
#include "ggma_context.h"
26+
#include "ggma_generate.h"
27+
28+
#ifdef __cplusplus
29+
extern "C" {
30+
#endif
31+
32+
#ifdef __cplusplus
33+
}
34+
#endif
35+
36+
#endif // __GGMA_GGMA_API_H__
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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+
/**
18+
* @file ggma_context.h
19+
* @brief This file defines the GGMA context management API.
20+
*/
21+
#ifndef __GGMA_GGMA_CONTEXT_H__
22+
#define __GGMA_GGMA_CONTEXT_H__
23+
24+
#include "ggma_types.h"
25+
26+
#ifdef __cplusplus
27+
extern "C" {
28+
#endif
29+
30+
/**
31+
* @brief Opaque handle to a GGMA inference context.
32+
*
33+
* A GGMA context represents the environment for inference.
34+
* It includes model, computation graph, KV caches, and other resources for token generation.
35+
*/
36+
typedef struct ggma_context ggma_context;
37+
38+
/**
39+
* @brief Creates a context from a package path.
40+
*
41+
* This function initializes a context for inference from the specified package path.
42+
* Once the context is no longer needed, it must be destroyed by calling
43+
* {@link ggma_free_context}.
44+
*
45+
* @param[out] context A pointer to the variable that will receive the new context handle.
46+
* @param[in] package_path The path to the GGMA package directory.
47+
* @return @c GGMA_STATUS_NO_ERROR on success, or an appropriate error code on failure.
48+
*/
49+
GGMA_STATUS ggma_create_context(ggma_context **context, const char *package_path);
50+
51+
/**
52+
* @brief Closes a GGMA context and releases its resources.
53+
*
54+
* This function deallocates all resources associated with the context, including
55+
* the GGMA package it was created with. After this function returns, the context
56+
* handle is invalid and must not be used.
57+
*
58+
* @param[in] context The context to close.
59+
* @return @c GGMA_STATUS_NO_ERROR on success, or an appropriate error code on failure.
60+
*/
61+
GGMA_STATUS ggma_free_context(ggma_context *context);
62+
63+
#ifdef __cplusplus
64+
}
65+
#endif
66+
67+
#endif // __GGMA_GGMA_CONTEXT_H__
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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+
/**
18+
* @file ggma_generate.h
19+
* @brief This file defines the GGMA text generation API.
20+
*/
21+
#ifndef __GGMA_GENERATE_H__
22+
#define __GGMA_GENERATE_H__
23+
24+
#include "ggma_types.h"
25+
26+
#ifdef __cplusplus
27+
extern "C" {
28+
#endif
29+
30+
/* Forward declaration */
31+
struct ggma_context;
32+
33+
/**
34+
* @brief Generates a sequence of tokens based on the provided prompt tokens.
35+
*
36+
* This function performs the core inference step, taking an initial sequence of
37+
* prompt tokens and generating new tokens autoregressively.
38+
*
39+
* @param[in] context The GGMA context to use for generation.
40+
* @param[inout] tokens An array of input prompt tokens. The generated tokens will
41+
* be placed in this buffer
42+
* @param[in] n_tokens The number of tokens in the input @p tokens array. This also
43+
* often specifies the maximum number of tokens to generate.
44+
* @param[in] n_tokens_max The maximum number of tokens that the @p tokens buffer can hold.
45+
* @param[out] n_tokens_out A pointer to a variable that will receive the number of
46+
* element in the @p tokens after generation
47+
* @return @c GGMA_STATUS_NO_ERROR on success, or an appropriate error code on failure.
48+
*/
49+
GGMA_STATUS ggma_generate(struct ggma_context *context, ggma_token *tokens, size_t n_tokens,
50+
size_t n_tokens_max, size_t *n_tokens_out);
51+
52+
#ifdef __cplusplus
53+
}
54+
#endif
55+
56+
#endif // __GGMA_GENERATE_H__

runtime/ggma/include/ggma_macro.h

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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 <cstdlib>
18+
19+
/**
20+
* @file ggma_macro.h
21+
* @brief Common macros for GGMA error handling and utilities
22+
*/
23+
#ifndef __GGMA_GGMA_MACRO_H__
24+
#define __GGMA_GGMA_MACRO_H__
25+
26+
#define GGMA_ENSURE(a) \
27+
do \
28+
{ \
29+
if ((a) != GGMA_STATUS_NO_ERROR) \
30+
{ \
31+
exit(-1); \
32+
} \
33+
} while (0)
34+
35+
#define GGMA_UNUSED(x) (void)(x)
36+
37+
#ifdef __cplusplus
38+
extern "C" {
39+
#endif
40+
41+
#ifdef __cplusplus
42+
}
43+
#endif
44+
45+
#endif // __GGMA_GGMA_MACRO_H__

runtime/ggma/include/ggma_types.h

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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+
/**
18+
* @file ggma_types.h
19+
* @brief This file defines the core types and status codes for GGMA API.
20+
*/
21+
#ifndef __GGMA_GGMA_TYPES_H__
22+
#define __GGMA_GGMA_TYPES_H__
23+
24+
#include <stddef.h>
25+
#include <stdint.h>
26+
27+
#ifdef __cplusplus
28+
extern "C" {
29+
#endif
30+
31+
/**
32+
* @brief Enumeration of status codes returned by GGMA API functions.
33+
*/
34+
typedef enum
35+
{
36+
/** The operation completed successfully. */
37+
GGMA_STATUS_NO_ERROR = 0,
38+
/**
39+
* A general error occurred.
40+
* This code is typically used when no more specific error code is applicable.
41+
*/
42+
GGMA_STATUS_ERROR = 1,
43+
/** Unexpected null argument is given. */
44+
GGMA_STATUS_UNEXPECTED_NULL = 2,
45+
/** The system ran out of memory. */
46+
GGMA_STATUS_OUT_OF_MEMORY = 4,
47+
/** The called API function is deprecated and should no longer be used. */
48+
GGMA_STATUS_DEPRECATED_API = 6,
49+
} GGMA_STATUS;
50+
51+
/* Forward declarations */
52+
typedef struct ggma_context ggma_context;
53+
typedef int32_t ggma_token;
54+
typedef struct ggma_tokenizer ggma_tokenizer;
55+
56+
#ifdef __cplusplus
57+
}
58+
#endif
59+
60+
#endif // __GGMA_GGMA_TYPES_H__

0 commit comments

Comments
 (0)