Skip to content

Commit

Permalink
Update C examples so they can be build individually
Browse files Browse the repository at this point in the history
  • Loading branch information
baijumeswani committed Sep 18, 2024
1 parent 8158ada commit e5d47ed
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 67 deletions.
89 changes: 32 additions & 57 deletions examples/c/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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} $<TARGET_FILE_DIR:${executable}>
)
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} $<TARGET_FILE_DIR:phi3>
)
add_custom_command(
TARGET phi3v POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_if_different ${DLL_FILE} $<TARGET_FILE_DIR:phi3v>
)
add_custom_command(
TARGET whisper POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_if_different ${DLL_FILE} $<TARGET_FILE_DIR:whisper>
)
endforeach()
if(WHISPER)
add_executable(whisper ${CMAKE_SOURCE_DIR}/src/whisper.cpp)
prepare_executable(whisper)
endif()
12 changes: 6 additions & 6 deletions examples/c/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
Expand All @@ -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
```
Expand Down Expand Up @@ -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
```
Expand Down Expand Up @@ -212,15 +212,15 @@ 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
```

Build for CPU:

```bash
cmake . -B build
cmake . -B build -DPHI3V=ON
cd build
cmake --build . --config Release
```
Expand Down
File renamed without changes.
8 changes: 4 additions & 4 deletions examples/c/src/whisper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<const char*, 4> prompt_tokens = {"<|startoftranscript|>", "<|en|>", "<|transcribe|>",
"<|notimestamps|>"};
const std::vector<const char*> 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) {
Expand Down Expand Up @@ -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<const char*, 4> prompt_tokens = {"<|startoftranscript|>", "<|en|>", "<|transcribe|>",
"<|notimestamps|>"};
const std::vector<const char*> prompt_tokens = {"<|startoftranscript|>", "<|en|>", "<|transcribe|>",
"<|notimestamps|>"};
OgaSequences* input_ids;
CheckResult(OgaCreateSequences(&input_ids));
const size_t batch_size = audio_paths.size();
Expand Down

0 comments on commit e5d47ed

Please sign in to comment.