diff --git a/examples/c/CMakeLists.txt b/examples/c/CMakeLists.txt index a3f225710..3ff3c5247 100644 --- a/examples/c/CMakeLists.txt +++ b/examples/c/CMakeLists.txt @@ -5,6 +5,9 @@ set(CMAKE_CXX_STANDARD 20) option(USE_CUDA "Build with CUDA support" OFF) option(USE_CXX "Invoke the C++ example" ON) +option(PHI3 "Build the Phi example" OFF) +option(PHI3V "Build the Phi3v example" OFF) +option(WHISPER "Build the Whisper example" OFF) if(USE_CXX) add_compile_definitions(USE_CXX) @@ -28,67 +31,39 @@ else() set(ONNXRUNTIME_GENAI_DEPENDENCY "*.so") endif() -add_executable(phi3 ${CMAKE_SOURCE_DIR}/src/main.cpp) -add_executable(phi3v ${CMAKE_SOURCE_DIR}/src/phi3v.cpp) -add_executable(whisper ${CMAKE_SOURCE_DIR}/src/whisper.cpp) +file(GLOB ort_genai_libs "${CMAKE_SOURCE_DIR}/lib/${ONNXRUNTIME_GENAI_DEPENDENCY}") -target_link_directories(phi3 PRIVATE ${ORT_GENAI_LIB_DIR}) -target_link_libraries(phi3 PRIVATE ${ONNXRUNTIME_GENAI_LIB}) -target_include_directories(phi3 PRIVATE ${CMAKE_SOURCE_DIR}/include) -target_link_directories(phi3v PRIVATE ${ORT_GENAI_LIB_DIR}) -target_link_libraries(phi3v PRIVATE ${ONNXRUNTIME_GENAI_LIB}) -target_include_directories(phi3v PRIVATE ${CMAKE_SOURCE_DIR}/include) -target_link_directories(whisper PRIVATE ${ORT_GENAI_LIB_DIR}) -target_link_libraries(whisper PRIVATE ${ONNXRUNTIME_GENAI_LIB}) -target_include_directories(whisper PRIVATE ${CMAKE_SOURCE_DIR}/include) +function(prepare_executable executable) + target_link_directories(${executable} PRIVATE ${ORT_GENAI_LIB_DIR}) + target_link_libraries(${executable} PRIVATE ${ONNXRUNTIME_GENAI_LIB}) + target_include_directories(${executable} PRIVATE ${CMAKE_SOURCE_DIR}/include) -if(USE_CUDA) - set_target_properties(phi3 PROPERTIES LINKER_LANGUAGE CUDA) - set_target_properties(phi3v PROPERTIES LINKER_LANGUAGE CUDA) - set_target_properties(whisper PROPERTIES LINKER_LANGUAGE CUDA) -endif() + if(USE_CUDA) + set_target_properties(${executable} PROPERTIES LINKER_LANGUAGE CUDA) + target_link_libraries(${executable} PRIVATE cublas curand cudart) + endif() -target_link_libraries( - phi3 - PUBLIC - onnxruntime-genai) -target_link_libraries( - phi3v - PUBLIC - onnxruntime-genai) -target_link_libraries( - whisper - PUBLIC - onnxruntime-genai) + target_link_libraries(${executable} PUBLIC onnxruntime-genai) -if(USE_CUDA) - target_link_libraries( - phi3 - PUBLIC - cublas curand cudart) - target_link_libraries( - phi3v - PUBLIC - cublas curand cudart) - target_link_libraries( - whisper - PUBLIC - cublas curand cudart) + foreach(DLL_FILE ${ort_genai_libs}) + add_custom_command( + TARGET ${executable} POST_BUILD + COMMAND ${CMAKE_COMMAND} -E copy_if_different ${DLL_FILE} $ + ) + endforeach() +endfunction() + +if(PHI3) + add_executable(phi3 ${CMAKE_SOURCE_DIR}/src/phi3.cpp) + prepare_executable(phi3) endif() -file(GLOB ort_genai_libs "${CMAKE_SOURCE_DIR}/lib/${ONNXRUNTIME_GENAI_DEPENDENCY}") +if(PHI3V) + add_executable(phi3v ${CMAKE_SOURCE_DIR}/src/phi3v.cpp) + prepare_executable(phi3v) +endif() -foreach(DLL_FILE ${ort_genai_libs}) - add_custom_command( - TARGET phi3 POST_BUILD - COMMAND ${CMAKE_COMMAND} -E copy_if_different ${DLL_FILE} $ - ) - add_custom_command( - TARGET phi3v POST_BUILD - COMMAND ${CMAKE_COMMAND} -E copy_if_different ${DLL_FILE} $ - ) - add_custom_command( - TARGET whisper POST_BUILD - COMMAND ${CMAKE_COMMAND} -E copy_if_different ${DLL_FILE} $ - ) -endforeach() \ No newline at end of file +if(WHISPER) + add_executable(whisper ${CMAKE_SOURCE_DIR}/src/whisper.cpp) + prepare_executable(whisper) +endif() diff --git a/examples/c/README.md b/examples/c/README.md index d006e5fcd..0c044107e 100644 --- a/examples/c/README.md +++ b/examples/c/README.md @@ -90,7 +90,7 @@ Change into the onnxruntime-genai directory. #### Windows ```bash -cmake -A x64 -S . -B build +cmake -A x64 -S . -B build -DPHI3=ON cd build cmake --build . --config Release ``` @@ -102,14 +102,14 @@ Build with CUDA: ```bash mkdir build cd build -cmake ../ -DCMAKE_CUDA_COMPILER=/usr/local/cuda/bin/nvcc -DCMAKE_CUDA_ARCHITECTURES=80 -DUSE_CUDA=ON +cmake ../ -DCMAKE_CUDA_COMPILER=/usr/local/cuda/bin/nvcc -DCMAKE_CUDA_ARCHITECTURES=80 -DUSE_CUDA=ON -DPHI3=ON cmake --build . --config Release ``` Build for CPU: ```bash -cmake . +cmake . -DPHI3=ON cd build cmake --build . --config Release ``` @@ -165,7 +165,7 @@ Change into the onnxruntime-genai folder. #### Build this sample ```bash -cmake -G "Visual Studio 17 2022" -A x64 -S . -B build +cmake -G "Visual Studio 17 2022" -A x64 -S . -B build -DPHI3V=ON cd build cmake --build . --config Release ``` @@ -212,7 +212,7 @@ Change into the onnxruntime-genai directory. Build to run with CUDA: ```bash -cmake . -B build -DCMAKE_CUDA_COMPILER=/usr/local/cuda/bin/nvcc -DCMAKE_CUDA_ARCHITECTURES=80 -DUSE_CUDA=ON +cmake . -B build -DCMAKE_CUDA_COMPILER=/usr/local/cuda/bin/nvcc -DCMAKE_CUDA_ARCHITECTURES=80 -DUSE_CUDA=ON -DPHI3V=ON cd build cmake --build . --config Release ``` @@ -220,7 +220,7 @@ cmake --build . --config Release Build for CPU: ```bash -cmake . -B build +cmake . -B build -DPHI3V=ON cd build cmake --build . --config Release ``` diff --git a/examples/c/src/main.cpp b/examples/c/src/phi3.cpp similarity index 100% rename from examples/c/src/main.cpp rename to examples/c/src/phi3.cpp diff --git a/examples/c/src/whisper.cpp b/examples/c/src/whisper.cpp index 63c06b89b..8125f59b9 100644 --- a/examples/c/src/whisper.cpp +++ b/examples/c/src/whisper.cpp @@ -57,8 +57,8 @@ void CXX_API(const char* model_path, int32_t num_beams) { std::cout << "Processing audio..." << std::endl; auto mel = processor->ProcessAudios(audios.get()); - const std::array prompt_tokens = {"<|startoftranscript|>", "<|en|>", "<|transcribe|>", - "<|notimestamps|>"}; + const std::vector prompt_tokens = {"<|startoftranscript|>", "<|en|>", "<|transcribe|>", + "<|notimestamps|>"}; auto input_ids = OgaSequences::Create(); const size_t batch_size = audio_paths.size(); for (size_t i = 0; i < batch_size; ++i) { @@ -148,8 +148,8 @@ void C_API(const char* model_path, int32_t num_beams) { std::cout << "Processing audio..." << std::endl; OgaNamedTensors* mel; CheckResult(OgaProcessorProcessAudios(processor, audios, &mel)); - const std::array prompt_tokens = {"<|startoftranscript|>", "<|en|>", "<|transcribe|>", - "<|notimestamps|>"}; + const std::vector prompt_tokens = {"<|startoftranscript|>", "<|en|>", "<|transcribe|>", + "<|notimestamps|>"}; OgaSequences* input_ids; CheckResult(OgaCreateSequences(&input_ids)); const size_t batch_size = audio_paths.size();