Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 5 additions & 10 deletions cmake/onnxruntime.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ foreach(f ${ONNXRUNTIME_PROVIDER_NAMES})
list(APPEND SYMBOL_FILES "${ONNXRUNTIME_ROOT}/core/providers/${f}/symbols.txt")
endforeach()

add_custom_command(OUTPUT ${SYMBOL_FILE}
COMMAND ${PYTHON_EXECUTABLE} "${REPO_ROOT}/tools/ci_build/gen_def.py" --version_file "${ONNXRUNTIME_ROOT}/../VERSION_NUMBER" --src_root "${ONNXRUNTIME_ROOT}" --config ${ONNXRUNTIME_PROVIDER_NAMES} --style=${OUTPUT_STYLE} --output ${SYMBOL_FILE}
add_custom_command(OUTPUT ${SYMBOL_FILE} ${CMAKE_CURRENT_BINARY_DIR}/generated_source.c
COMMAND ${PYTHON_EXECUTABLE} "${REPO_ROOT}/tools/ci_build/gen_def.py" --version_file "${ONNXRUNTIME_ROOT}/../VERSION_NUMBER" --src_root "${ONNXRUNTIME_ROOT}" --config ${ONNXRUNTIME_PROVIDER_NAMES} --style=${OUTPUT_STYLE} --output ${SYMBOL_FILE} --output_source ${CMAKE_CURRENT_BINARY_DIR}/generated_source.c
DEPENDS ${SYMBOL_FILES}
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})

add_custom_target(onnxruntime_generate_def ALL DEPENDS ${SYMBOL_FILE})
add_library(onnxruntime SHARED ${onnxruntime_session_srcs})
add_custom_target(onnxruntime_generate_def ALL DEPENDS ${SYMBOL_FILE} ${CMAKE_CURRENT_BINARY_DIR}/generated_source.c)
add_library(onnxruntime SHARED ${CMAKE_CURRENT_BINARY_DIR}/generated_source.c)
set_target_properties(onnxruntime PROPERTIES VERSION ${ORT_VERSION})
add_dependencies(onnxruntime onnxruntime_generate_def ${onnxruntime_EXTERNAL_DEPENDENCIES})
target_include_directories(onnxruntime PRIVATE ${ONNXRUNTIME_ROOT})
Expand All @@ -37,12 +37,8 @@ endif()

if(UNIX)
if (APPLE)
set(BEGIN_WHOLE_ARCHIVE -Xlinker -all_load)
set(END_WHOLE_ARCHIVE -Xlinker -noall_load)
set(ONNXRUNTIME_SO_LINK_FLAG "-Xlinker -dead_strip")
else()
set(BEGIN_WHOLE_ARCHIVE -Xlinker --whole-archive)
set(END_WHOLE_ARCHIVE -Xlinker --no-whole-archive)
set(ONNXRUNTIME_SO_LINK_FLAG "-Xlinker --version-script=${SYMBOL_FILE} -Xlinker --no-undefined -Xlinker --gc-sections")
endif()
else()
Expand All @@ -59,7 +55,7 @@ endif()

#The BEGIN_WHOLE_ARCHIVE/END_WHOLE_ARCHIVE part should contain the implementations of all the C API functions
target_link_libraries(onnxruntime PRIVATE
${BEGIN_WHOLE_ARCHIVE}
onnxruntime_session
${onnxruntime_libs}
${PROVIDERS_CUDA}
${PROVIDERS_MKLDNN}
Expand All @@ -72,7 +68,6 @@ target_link_libraries(onnxruntime PRIVATE
onnxruntime_util
${onnxruntime_tvm_libs}
onnxruntime_framework
${END_WHOLE_ARCHIVE}
onnxruntime_graph
onnxruntime_common
onnxruntime_mlas
Expand Down
34 changes: 16 additions & 18 deletions onnxruntime/core/framework/allocator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@
namespace onnxruntime {

void* CPUAllocator::Alloc(size_t size) {
if (size <= 0)
return nullptr;
if (size <= 0) return nullptr;
void* p;
size_t alignment = MlasGetPreferredBufferAlignment();
#if _MSC_VER
Expand All @@ -35,54 +34,53 @@ void CPUAllocator::Free(void* p) {
#endif
}

const OrtAllocatorInfo& CPUAllocator::Info() const {
return *allocator_info_;
}
const OrtAllocatorInfo& CPUAllocator::Info() const { return *allocator_info_; }
} // namespace onnxruntime

std::ostream& operator<<(std::ostream& out, const OrtAllocatorInfo& info) {
return (out << info.ToString());
}
std::ostream& operator<<(std::ostream& out, const OrtAllocatorInfo& info) { return (out << info.ToString()); }

ORT_API_STATUS_IMPL(OrtCreateAllocatorInfo, _In_ const char* name1, OrtAllocatorType type, int id1,
OrtMemType mem_type1, _Out_ OrtAllocatorInfo** out) {
if (strcmp(name1, onnxruntime::CPU) == 0) {
*out = new OrtAllocatorInfo(name1, type, OrtDevice(), id1, mem_type1);
} else if (strcmp(name1, onnxruntime::CUDA) == 0) {
*out = new OrtAllocatorInfo(name1, type, OrtDevice(OrtDevice::GPU, OrtDevice::MemType::DEFAULT, static_cast<OrtDevice::DeviceId>(id1)), id1, mem_type1);
*out = new OrtAllocatorInfo(
name1, type, OrtDevice(OrtDevice::GPU, OrtDevice::MemType::DEFAULT, static_cast<OrtDevice::DeviceId>(id1)), id1,
mem_type1);
} else if (strcmp(name1, onnxruntime::CUDA_PINNED) == 0) {
*out = new OrtAllocatorInfo(name1, type, OrtDevice(OrtDevice::CPU, OrtDevice::MemType::CUDA_PINNED, static_cast<OrtDevice::DeviceId>(id1)), id1, mem_type1);
*out = new OrtAllocatorInfo(
name1, type, OrtDevice(OrtDevice::CPU, OrtDevice::MemType::CUDA_PINNED, static_cast<OrtDevice::DeviceId>(id1)),
id1, mem_type1);
} else {
return OrtCreateStatus(ORT_INVALID_ARGUMENT, "Specified device is not supported.");
}
return nullptr;
}

ORT_API(void, OrtReleaseAllocatorInfo, _Frees_ptr_opt_ OrtAllocatorInfo* p) {
delete p;
}
ORT_API(void, OrtReleaseAllocatorInfo, _Frees_ptr_opt_ OrtAllocatorInfo* p) { delete p; }

ORT_API_STATUS_IMPL(OrtAllocatorInfoGetName, _In_ OrtAllocatorInfo* ptr, _Out_ const char** out) {
ORT_API_STATUS_IMPL(OrtAllocatorInfoGetName, _In_ const OrtAllocatorInfo* ptr, _Out_ const char** out) {
*out = ptr->name;
return nullptr;
}

ORT_API_STATUS_IMPL(OrtAllocatorInfoGetId, _In_ OrtAllocatorInfo* ptr, _Out_ int* out) {
ORT_API_STATUS_IMPL(OrtAllocatorInfoGetId, _In_ const OrtAllocatorInfo* ptr, _Out_ int* out) {
*out = ptr->id;
return nullptr;
}

ORT_API_STATUS_IMPL(OrtAllocatorInfoGetMemType, _In_ OrtAllocatorInfo* ptr, _Out_ OrtMemType* out) {
ORT_API_STATUS_IMPL(OrtAllocatorInfoGetMemType, _In_ const OrtAllocatorInfo* ptr, _Out_ OrtMemType* out) {
*out = ptr->mem_type;
return nullptr;
}

ORT_API_STATUS_IMPL(OrtAllocatorInfoGetType, _In_ OrtAllocatorInfo* ptr, _Out_ OrtAllocatorType* out) {
ORT_API_STATUS_IMPL(OrtAllocatorInfoGetType, _In_ const OrtAllocatorInfo* ptr, _Out_ OrtAllocatorType* out) {
*out = ptr->type;
return nullptr;
}

ORT_API_STATUS_IMPL(OrtCompareAllocatorInfo, _In_ const OrtAllocatorInfo* info1, _In_ const OrtAllocatorInfo* info2, _Out_ int* out) {
ORT_API_STATUS_IMPL(OrtCompareAllocatorInfo, _In_ const OrtAllocatorInfo* info1, _In_ const OrtAllocatorInfo* info2,
_Out_ int* out) {
*out = (*info1 == *info2) ? 0 : -1;
return nullptr;
}
12 changes: 11 additions & 1 deletion tools/ci_build/gen_def.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ def parse_arguments():
parser = argparse.ArgumentParser()
parser.add_argument("--src_root", required=True, help="input symbol file")
parser.add_argument("--output", required=True, help="output file")
parser.add_argument("--output_source", required=True, help="output file")
parser.add_argument("--version_file", required=True, help="VERSION_NUMBER file")
parser.add_argument("--style", required=True, choices=["gcc", "vc"])
parser.add_argument("--config", required=True, nargs="+")
return parser.parse_args()

args = parse_arguments()
print("Generating symbol file for %s" % str(args.config))

with open(args.version_file, 'r') as f:
VERSION_STRING=f.read().strip();

Expand Down Expand Up @@ -52,3 +52,13 @@ def parse_arguments():
file.write(" local:\n")
file.write(" *;\n")
file.write("}; \n")

with open(args.output_source, 'w') as file:
file.write("#include <onnxruntime_c_api.h>\n")
for c in args.config:
file.write("#include <core/providers/%s/%s_provider_factory.h>\n" % (c,c))
file.write("void* GetFunctionEntryByName(const char* name){\n")
for symbol in symbols:
file.write("if(strcmp(name,\"%s\") ==0) return (void*)&%s;\n" % (symbol,symbol))
file.write("return NULL;\n");
file.write("}\n");