forked from microsoft/onnxruntime-extensions
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
A tutorial of build ort-extensions from source as a static library (m…
…icrosoft#703) * The tutorial of build from source as a static library * update test flag control * add the tutorial
- Loading branch information
Showing
5 changed files
with
114 additions
and
5 deletions.
There are no files selected for viewing
This file contains 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 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 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,19 @@ | ||
cmake_minimum_required(VERSION 3.25) | ||
|
||
project(ortx_api_test) | ||
|
||
set(CMAKE_CXX_STANDARD 17) | ||
include(FetchContent) | ||
|
||
FetchContent_Declare( | ||
ortx | ||
GIT_REPOSITORY https://github.com/microsoft/onnxruntime-extensions.git | ||
GIT_TAG a7043c56e4f19c4bf11642d390f7b502f80a34ba) | ||
|
||
set(OCOS_BUILD_PRESET token_api_only) | ||
FetchContent_MakeAvailable(ortx) | ||
|
||
file(GLOB_RECURSE SOURCES "src/*.cc") | ||
add_executable(ortx_api_test ${SOURCES}) | ||
target_link_libraries(ortx_api_test onnxruntime_extensions) | ||
target_include_directories(ortx_api_test PRIVATE ${ortx_SOURCE_DIR}/include) |
This file contains 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,3 @@ | ||
# Running the Demo | ||
|
||
To run this demo, you'll need a developer tool like Visual Studio Code or a command line tool that supports CMake to configure the project. Once configured, compile the C++ project `ortx_api_test` to build the test program, and then run it. |
This file contains 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,80 @@ | ||
#include <iostream> | ||
#include <string> | ||
#include <vector> | ||
|
||
#include "ortx_tokenizer.h" | ||
|
||
extError_t tokenize_text(const OrtxTokenizer *tokenizer, | ||
const char *text, std::string &decoded_text, std::vector<extTokenId_t> &ids) | ||
{ | ||
OrtxTokenId2DArray *tok_2d_output = NULL; | ||
const char *tok_input[] = {text}; | ||
extError_t err = OrtxTokenize(tokenizer, tok_input, 1, &tok_2d_output); | ||
if (err != kOrtxOK) | ||
{ | ||
return err; | ||
} | ||
|
||
size_t length = 0; | ||
const extTokenId_t *token_ids = NULL; | ||
OrtxTokenId2DArrayGetItem(tok_2d_output, 0, &token_ids, &length); | ||
|
||
OrtxStringArray *detok_output = NULL; | ||
err = OrtxDetokenize1D(tokenizer, token_ids, length, &detok_output); | ||
if (err != kOrtxOK) | ||
{ | ||
ORTX_DISPOSE(tok_2d_output); | ||
return err; | ||
} | ||
ids.insert(ids.end(), token_ids, token_ids + length); | ||
|
||
const char *decoded_str = NULL; | ||
OrtxStringArrayGetItem(detok_output, 0, &decoded_str); | ||
decoded_text = decoded_str; | ||
|
||
ORTX_DISPOSE(tok_2d_output); | ||
ORTX_DISPOSE(detok_output); | ||
return kOrtxOK; | ||
} | ||
|
||
int main() | ||
{ | ||
int ver = OrtxGetAPIVersion(); | ||
std::cout << "Ortx API version: " << ver << std::endl; | ||
OrtxTokenizer *tokenizer = NULL; | ||
|
||
std::cout << "Please specify the tokenizer model file path (like <root>/test/data/llama2)" << std::endl; | ||
std::string model_path; | ||
std::cin >> model_path; | ||
|
||
extError_t err = OrtxCreateTokenizer(&tokenizer, model_path.c_str()); | ||
if (err != kOrtxOK) | ||
{ | ||
std::cerr << "Failed to create tokenizer" << std::endl; | ||
return 1; | ||
} | ||
|
||
const char *input = "How many hours does it take a man to eat a Helicopter?"; | ||
std::string decoded_text; | ||
std::vector<extTokenId_t> ids; | ||
err = tokenize_text(tokenizer, input, decoded_text, ids); | ||
if (err != kOrtxOK) | ||
{ | ||
std::cerr << "Failed to tokenize text" << std::endl; | ||
return 1; | ||
} | ||
|
||
std::cout << "Input : " << input << std::endl; | ||
// output the token ids | ||
std::cout << "Token IDs: "; | ||
for (const auto &id : ids) | ||
{ | ||
std::cout << id << " "; | ||
} | ||
std::cout << std::endl; | ||
|
||
std::cout << "Decoded: " << decoded_text << std::endl; | ||
|
||
OrtxDisposeOnly(tokenizer); // Clean up the tokenizer | ||
return 0; | ||
} |