-
Notifications
You must be signed in to change notification settings - Fork 179
[tests/tools] Introduce ggma_run #16270
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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,22 @@ | ||
| if(NOT BUILD_GGMA_RUN) | ||
| return() | ||
| endif(NOT BUILD_GGMA_RUN) | ||
|
|
||
| list(APPEND GGMA_RUN_SRCS "src/args.cc") | ||
| list(APPEND GGMA_RUN_SRCS "src/ggma_run.cc") | ||
|
|
||
| add_executable(ggma_run ${GGMA_RUN_SRCS}) | ||
|
|
||
| target_include_directories(ggma_run PRIVATE src) | ||
|
|
||
| target_link_libraries(ggma_run arser) | ||
| target_link_libraries(ggma_run ggma-dev) | ||
| target_link_libraries(ggma_run nnfw_common) | ||
| target_link_libraries(ggma_run nnfw-dev) | ||
|
|
||
| # Set RPATH to find GGMA library in lib/ggma directory | ||
| set_target_properties(ggma_run PROPERTIES | ||
| INSTALL_RPATH "$ORIGIN/../lib/ggma:$ORIGIN/../lib/:$ORIGIN/:/usr/local/lib" | ||
| ) | ||
|
|
||
| install(TARGETS ggma_run DESTINATION ${CMAKE_INSTALL_BINDIR}) |
This file contains hidden or 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,22 @@ | ||
| # ggma_run | ||
|
|
||
| `ggma_run` is a tool to run LLM model. | ||
|
|
||
| It takes GGMA package as input. It uses **GGMA API** internally. | ||
|
|
||
| ## Usage | ||
|
|
||
| ``` | ||
| $ ./ggma_run path_to_ggma_package | ||
| ``` | ||
|
|
||
| It will run a GGML package to generate the output using the default prompt. | ||
|
|
||
| ## Example | ||
|
|
||
| ``` | ||
| $ Product/out/bin/ggma_run tinyllama | ||
| prompt: Lily picked up a flower. | ||
| generated: { 1100, 7899, 289, 826, 351, 600, 2439, 288, 266, 3653, 31843, 1100, 7899, 289, 1261, 291, 5869, 291, 1261, 31843, 1100, 7899 } | ||
| detokenized: She liked to play with her friends. She liked to run and jump in the water. She was | ||
| ``` |
This file contains hidden or 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,91 @@ | ||
| /* | ||
| * Copyright (c) 2025 Samsung Electronics Co., Ltd. All Rights Reserved | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| #include "args.h" | ||
| #include "nnfw.h" | ||
|
|
||
| #include <iostream> | ||
| #include <string> | ||
| #include <sys/stat.h> | ||
|
|
||
| #define NNPR_ENSURE_STATUS(a) \ | ||
| do \ | ||
| { \ | ||
| if ((a) != NNFW_STATUS_NO_ERROR) \ | ||
| { \ | ||
| exit(-1); \ | ||
| } \ | ||
| } while (0) | ||
|
|
||
|
Comment on lines
+24
to
+32
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Beyond this PR. (Just for recording) The
This is why I tried to define |
||
| static void print_version() | ||
| { | ||
| uint32_t version; | ||
| NNPR_ENSURE_STATUS(nnfw_query_info_u32(NULL, NNFW_INFO_ID_VERSION, &version)); | ||
| std::cout << "ggma_run v0.1.0 (nnfw runtime: v" << (version >> 24) << "." | ||
| << ((version & 0x0000FF00) >> 8) << "." << (version & 0xFF) << ")" << std::endl; | ||
| } | ||
|
|
||
| namespace ggma_run | ||
| { | ||
|
|
||
| Args::Args(const int argc, char **argv) | ||
| { | ||
| initialize(); | ||
| parse(argc, argv); | ||
| } | ||
|
|
||
| void Args::initialize(void) | ||
| { | ||
| _arser.add_argument("path").type(arser::DataType::STR).help("nnpackage path"); | ||
| arser::Helper::add_version(_arser, print_version); | ||
| } | ||
|
|
||
| void Args::parse(const int argc, char **argv) | ||
| { | ||
| try | ||
| { | ||
| _arser.parse(argc, argv); | ||
|
|
||
| if (_arser.get<bool>("--version")) | ||
| { | ||
| _print_version = true; | ||
| return; | ||
| } | ||
|
|
||
| if (_arser["path"]) | ||
| { | ||
| auto path = _arser.get<std::string>("path"); | ||
| struct stat sb; | ||
| if (stat(path.c_str(), &sb) == 0) | ||
| { | ||
| if (sb.st_mode & S_IFDIR) | ||
| _package_path = path; | ||
| } | ||
| else | ||
| { | ||
| std::cerr << "Cannot find: " << path << "\n"; | ||
| exit(1); | ||
| } | ||
| } | ||
| } | ||
| catch (const std::bad_cast &e) | ||
| { | ||
| std::cerr << "Bad cast error - " << e.what() << '\n'; | ||
| exit(1); | ||
| } | ||
| } | ||
|
|
||
| } // end of namespace ggma_run | ||
This file contains hidden or 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,51 @@ | ||
| /* | ||
| * Copyright (c) 2025 Samsung Electronics Co., Ltd. All Rights Reserved | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| #ifndef __GGMA_RUN_ARGS_H__ | ||
| #define __GGMA_RUN_ARGS_H__ | ||
|
|
||
| #include "arser/arser.h" | ||
|
|
||
| #include <string> | ||
| #include <unordered_map> | ||
| #include <vector> | ||
|
|
||
| namespace ggma_run | ||
| { | ||
|
|
||
| class Args | ||
| { | ||
| public: | ||
| Args(const int argc, char **argv); | ||
| void print(void); | ||
|
|
||
| const std::string &packagePath() const { return _package_path; } | ||
| bool printVersion() const { return _print_version; } | ||
|
|
||
| private: | ||
| void initialize(); | ||
| void parse(const int argc, char **argv); | ||
|
|
||
| private: | ||
| arser::Arser _arser; | ||
|
|
||
| std::string _package_path; | ||
| bool _print_version = false; | ||
| }; | ||
|
|
||
| } // end of namespace ggma_run | ||
|
|
||
| #endif // __GGMA_RUN_ARGS_H__ |
This file contains hidden or 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,88 @@ | ||
| /* | ||
| * Copyright (c) 2025 Samsung Electronics Co., Ltd. All Rights Reserved | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| #include "args.h" | ||
| #include "ggma_api.h" | ||
|
|
||
| #include <cstdlib> | ||
| #include <iostream> | ||
| #include <stdexcept> | ||
|
|
||
| #define GGMA_ENSURE(a) \ | ||
| do \ | ||
| { \ | ||
| if ((a) != GGMA_STATUS_NO_ERROR) \ | ||
| { \ | ||
| exit(-1); \ | ||
| } \ | ||
| } while (0) | ||
|
|
||
| int main(const int argc, char **argv) | ||
| { | ||
| using namespace ggma_run; | ||
|
|
||
| try | ||
| { | ||
| Args args(argc, argv); | ||
|
|
||
| std::string prompt = "Lily picked up a flower."; | ||
| constexpr size_t n_tokens_max = 32; | ||
| ggma_token tokens[n_tokens_max]; | ||
| size_t n_tokens; | ||
|
|
||
| // Create tokenizer first | ||
| ggma_tokenizer *tokenizer = nullptr; | ||
| GGMA_ENSURE(ggma_create_tokenizer(&tokenizer, (args.packagePath() + "/tokenizer").c_str())); | ||
|
|
||
| // Tokenize using the created tokenizer | ||
| GGMA_ENSURE( | ||
| ggma_tokenize(tokenizer, prompt.c_str(), prompt.size(), tokens, n_tokens_max, &n_tokens)); | ||
|
|
||
| ggma_context *context = nullptr; | ||
| GGMA_ENSURE(ggma_create_context(&context, args.packagePath().c_str())); | ||
|
|
||
| size_t n_predict = 22; | ||
| GGMA_ENSURE(ggma_generate(context, tokens, n_tokens, n_tokens_max, &n_predict)); | ||
|
|
||
| // Output generated token IDs | ||
| std::cout << "prompt: " << prompt << std::endl; | ||
| std::cout << "generated: { "; | ||
| for (size_t i = n_tokens; i < n_tokens + n_predict; ++i) | ||
| { | ||
| std::cout << tokens[i]; | ||
| if (i < n_tokens + n_predict - 1) | ||
| { | ||
| std::cout << ", "; | ||
| } | ||
| } | ||
| std::cout << " }" << std::endl; | ||
|
|
||
| // Detokenize and output the generated text | ||
| constexpr size_t detokenize_max = 256; | ||
| char detokenized[detokenize_max]; | ||
| GGMA_ENSURE( | ||
| ggma_detokenize(tokenizer, tokens + n_tokens, n_predict, detokenized, detokenize_max)); | ||
| std::cout << "detokenized: " << detokenized << std::endl; | ||
|
|
||
| GGMA_ENSURE(ggma_free_context(context)); | ||
| GGMA_ENSURE(ggma_free_tokenizer(tokenizer)); | ||
| } | ||
| catch (std::runtime_error &e) | ||
| { | ||
| std::cerr << "E: Fail to run by runtime error: " << e.what() << std::endl; | ||
| exit(-1); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reviewers, I will turn
BUILD_GGMA_APIandBUILD_GGMA_RUNon after sentencepiece is handled for Tizen.