Native C++ Library¶
We also provide C++ library for you to link to in case:
You are only interested in one or two specific algorithms implemented in some operators.
You don’t like the concept or methodology that PyIS imposes on you, e.g., defination of a model, deployment with libtorch or onnxruntime. It is a pity. But we understand that and are with you.
Use the Operator in C++¶
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
#include <cstdio>
#include "pyis/ops/text/cedar_trie.h"
int main() {
pyis::ops::CedarTrie trie;
trie.Insert("the answer", 42);
auto answer = trie.Lookup("the answer");
printf("the answer is %d", answer.value());
return 0;
}
Build the Example as a C++ Project with CMake¶
All core algorithms are implemented in the CMake target pyis_operators.
cmake_minimum_required(VERSION 3.12 FATAL_ERROR)
project(cpp_library_test VERSION 0.1)
include(FetchContent)
FetchContent_Declare(
pyis
GIT_REPOSITORY https://github.com/microsoft/python-inference-script
GIT_TAG main)
set(PYTHON_BACKEND
OFF
CACHE BOOL "" FORCE)
set(TORCH_BACKEND
OFF
CACHE BOOL "" FORCE)
set(ONNX_BACKEND
OFF
CACHE BOOL "" FORCE)
add_executable(cpp_library_test cpp_library_test.cpp)
target_link_libraries(cpp_library_test PRIVATE pyis_operators)
To optimize the binary size, PyIS allows you to disable C++ exceptions or RTTI.
# disable C++ exceptions
set(PYIS_NO_EXCEPTIONS
ON
CACHE BOOL "" FORCE)
# disable C++ RTTI
set(PYIS_NO_RTTI
ON
CACHE BOOL "" FORCE)
Run¶
cmake -S . -B build -A x64
cmake --build build
Now we can run the successfully built executable:
> .\build\Debug\cpp_library_test.exe
the answer is 42