Skip to content

Commit

Permalink
Address bug in C API and C# API
Browse files Browse the repository at this point in the history
  • Loading branch information
baijumeswani committed Feb 13, 2024
1 parent aff981a commit 0acf678
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 18 deletions.
28 changes: 16 additions & 12 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,8 @@ cmake_minimum_required(VERSION 3.26)
include(FetchContent)
include(CMakeDependentOption)
project(Generators LANGUAGES C CXX)
option(USE_CUDA "Build with CUDA support" ON)
option(USE_TOKENIZER "Build with Tokenizer support" ON)
option(ENABLE_TESTS "Enable tests" ON)
option(ENABLE_PYTHON "Enable the Python build" ON)
cmake_dependent_option(BUILD_WHEEL "Build the python wheel" ON "ENABLE_PYTHON" OFF)

# All Options should be defined in cmake/options.cmake
include(cmake/options.cmake)
include(cmake/external/onnxruntime_external_deps.cmake)
# Checking if CUDA is supported
include(CheckLanguage)
Expand All @@ -33,7 +29,6 @@ set(GENERATORS_ROOT ${PROJECT_SOURCE_DIR}/src)
set(MODELS_ROOT ${PROJECT_SOURCE_DIR}/src/models)
set(ORT_HEADER_DIR ${CMAKE_SOURCE_DIR}/ort/include)
set(ORT_LIB_DIR ${CMAKE_SOURCE_DIR}/ort/lib)

# CUDA Being enabled will make it not a debug build without this option, so all of the C++ headers will complain
# about a mismatch with the actual debug headers and it'll fail to link. I don't know why this happens, or if this is the best fix.
if(USE_CUDA AND CMAKE_CUDA_COMPILER AND CMAKE_BUILD_TYPE STREQUAL "Debug")
Expand Down Expand Up @@ -116,11 +111,23 @@ if(ENABLE_TESTS)
message("------------------Enabling tests------------------")
endif()

set(WHEEL_FILES_DIR "${CMAKE_BINARY_DIR}/wheel")

if(ENABLE_PYTHON)
add_subdirectory("${CMAKE_SOURCE_DIR}/src/python")
message("------------------Enabling Python Wheel------------------")
endif()

file(GLOB onnxruntime_libs "${ORT_LIB_DIR}/${ONNXRUNTIME_FILES}")
add_custom_command(
TARGET onnxruntime-genai-static POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy ${onnxruntime_libs} ${WHEEL_FILES_DIR}/${TARGET_NAME}/
)
add_custom_command(
TARGET onnxruntime-genai-static POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy ${onnxruntime_libs} ${DLL_FILE} $<TARGET_FILE_DIR:onnxruntime-genai>
)

target_link_directories(onnxruntime-genai PRIVATE ${ORT_LIB_DIR})
target_link_libraries(onnxruntime-genai PRIVATE ${ONNXRUNTIME_LIB})

Expand Down Expand Up @@ -155,13 +162,10 @@ endforeach()
if(USE_TOKENIZER)
add_compile_definitions(USE_TOKENIZER=1)
target_include_directories(onnxruntime-genai PRIVATE ${TOKENIZER_ROOT})
target_include_directories(onnxruntime-genai-static PRIVATE ${TOKENIZER_ROOT})
target_include_directories(onnxruntime-genai-static PUBLIC ${TOKENIZER_ROOT})
target_link_libraries(onnxruntime-genai PRIVATE tokenizer)
target_link_libraries(onnxruntime-genai-static PRIVATE tokenizer)
target_link_libraries(onnxruntime-genai-static PUBLIC tokenizer)
endif()

# Have visual studio put all files into one single folder vs the default split of header files into a separate folder
source_group(TREE ${GENERATORS_ROOT} FILES ${generator_srcs})



1 change: 1 addition & 0 deletions build.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ def build(
csharp_build_command = [dotnet, "build", ".", "-c", "Release"]
run_subprocess(csharp_build_command, cwd=os.path.join("src", "csharp")).check_returncode()
run_subprocess(csharp_build_command, cwd=os.path.join("test", "csharp")).check_returncode()
run_subprocess([dotnet, "test", "-c", "Release"], cwd=os.path.join("test", "csharp")).check_returncode()


if __name__ == "__main__":
Expand Down
2 changes: 1 addition & 1 deletion cmake/options.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ include(CMakeDependentOption)

option(USE_CUDA "Build with CUDA support" ON)
option(USE_TOKENIZER "Build with Tokenizer support" ON)
option(ENABLE_PYTHON "Enable python buildings" ON)
option(ENABLE_PYTHON "Build the Python API." ON)
option(ENABLE_TESTS "Enable tests" ON)

cmake_dependent_option(BUILD_WHEEL "Build the python wheel" ON "ENABLE_PYTHON" OFF)
6 changes: 3 additions & 3 deletions src/csharp/Generator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@ public int[] GetSequence(int index)
{
IntPtr nullPtr = IntPtr.Zero;
UIntPtr sequenceLength = UIntPtr.Zero;
Result.VerifySuccess(NativeMethods.OgaGenerator_GetSequence(_generatorHandle, index, out nullPtr, out sequenceLength));
int[] sequence = new int[sequenceLength.ToUInt32()];
Result.VerifySuccess(NativeMethods.OgaGenerator_GetSequence(_generatorHandle, index, nullPtr, out sequenceLength));
int[] sequence = new int[sequenceLength.ToUInt64()];
GCHandle handle = GCHandle.Alloc(sequence, GCHandleType.Pinned);
try
{
IntPtr sequencePtr = handle.AddrOfPinnedObject();
Result.VerifySuccess(NativeMethods.OgaGenerator_GetSequence(_generatorHandle, index, out sequencePtr, out sequenceLength));
Result.VerifySuccess(NativeMethods.OgaGenerator_GetSequence(_generatorHandle, index, sequencePtr, out sequenceLength));
}
finally
{
Expand Down
2 changes: 1 addition & 1 deletion src/csharp/NativeMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public static extern IntPtr OgaCreateGenerator(IntPtr /* OgaModel* */ model,
[DllImport(NativeLib.DllName, CallingConvention = CallingConvention.Winapi)]
public static extern IntPtr OgaGenerator_GetSequence(IntPtr /* OgaGenerator* */ generator,
int index,
out IntPtr /* int32_t* */ tokens,
IntPtr /* int32_t* */ tokens,
out UIntPtr /* size_t* */ tokensCount);
}
}
2 changes: 2 additions & 0 deletions src/ort_genai_c.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@
#include "generators.h"
#include "models/model.h"
#include "search.h"
#include <iostream>

namespace Generators {

std::unique_ptr<OrtEnv> g_ort_env;

OrtEnv& GetOrtEnv() {
if (!g_ort_env) {
Ort::InitApi();
g_ort_env = OrtEnv::Create();
}
return *g_ort_env;
Expand Down
2 changes: 1 addition & 1 deletion test/csharp/GeneratorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public void TestGreedySearch()
var expectedOutput = new int[] { 0, 0, 0, 52, 204, 204, 204, 204, 204, 204,
0, 0, 195, 731, 731, 114, 114, 114, 114, 114 };

string modelPath = Path.Combine(Directory.GetCurrentDirectory(), "testdata", "hf_internal_testing", "tiny-random-gpt2-fp32");
string modelPath = Path.Combine(Directory.GetCurrentDirectory(), "testdata", "hf-internal-testing", "tiny-random-gpt2-fp32");
using (var model = new Model(modelPath, DeviceType.CPU))
{
Assert.NotNull(model);
Expand Down

0 comments on commit 0acf678

Please sign in to comment.