Skip to content

armgit / laya

Public Imported from GitHub
0

Non-autoregressive System 1 decision engine. Typed choice, score and yes/no decisions over any text in a single forward pass, in 100+ languages, with a router that picks the right checkpoint per request.

3.5 KB
Raw Blame
# The ONNX Runtime used by laya_onnx: LAYA_ONNXRUNTIME_ROOT (an unpacked release) or the pinned
# release archive, verified by sha256 at the moment of download. Defines the imported target laya_ort.
set(LAYA_ORT_VERSION "1.30.0")
include(FetchContent)

if(LAYA_ONNXRUNTIME_ROOT)
  set(_ort_root "${LAYA_ONNXRUNTIME_ROOT}")
else()
  set(_ort_base "https://github.com/microsoft/onnxruntime/releases/download/v${LAYA_ORT_VERSION}")
  string(TOLOWER "${CMAKE_SYSTEM_NAME}-${CMAKE_SYSTEM_PROCESSOR}" _ort_platform)
  if(_ort_platform STREQUAL "linux-x86_64")
    set(_ort_asset "onnxruntime-linux-x64-${LAYA_ORT_VERSION}.tgz")
    set(_ort_sha256 "a5ed5a3cac51fbb2e90da632ae43d19212faaa20e76484e62bcb7c23ddb3b3fd")
  elseif(_ort_platform STREQUAL "darwin-arm64")
    set(_ort_asset "onnxruntime-osx-arm64-${LAYA_ORT_VERSION}.tgz")
    set(_ort_sha256 "6ebb5062a934537c352937821f9fe9718e7de1a2db1122a93dd363ffd53a7012")
  else()
    message(FATAL_ERROR "No pinned ONNX Runtime for ${_ort_platform}; unpack a ${LAYA_ORT_VERSION} release and pass -DLAYA_ONNXRUNTIME_ROOT=<dir>")
  endif()
  # URL_HASH makes CMake verify the archive before it is extracted; a mismatch is a configure error.
  FetchContent_Declare(onnxruntime
    URL "${_ort_base}/${_ort_asset}"
    URL_HASH SHA256=${_ort_sha256}
    DOWNLOAD_EXTRACT_TIMESTAMP TRUE)
  FetchContent_MakeAvailable(onnxruntime)
  set(_ort_root "${onnxruntime_SOURCE_DIR}")
endif()

if(NOT EXISTS "${_ort_root}/include/onnxruntime_cxx_api.h")
  message(FATAL_ERROR "${_ort_root}/include/onnxruntime_cxx_api.h not found: not an ONNX Runtime release directory")
endif()
# The pinned release is what the tests and goldens were measured against, so an override must be it.
string(REGEX MATCH "^[0-9]+\\.([0-9]+)\\." _ort_unused "${LAYA_ORT_VERSION}")
set(_ort_want_api "${CMAKE_MATCH_1}")
file(STRINGS "${_ort_root}/include/onnxruntime_c_api.h" _ort_api_line REGEX "^#define ORT_API_VERSION [0-9]+")
string(REGEX REPLACE "^#define ORT_API_VERSION ([0-9]+).*" "\\1" _ort_api "${_ort_api_line}")
if(NOT _ort_api STREQUAL _ort_want_api)
  message(FATAL_ERROR "${_ort_root} is ONNX Runtime API version '${_ort_api}', but laya_onnx is pinned to ${LAYA_ORT_VERSION} (API version ${_ort_want_api})")
endif()
if(EXISTS "${_ort_root}/VERSION_NUMBER")
  file(STRINGS "${_ort_root}/VERSION_NUMBER" _ort_vn LIMIT_COUNT 1)
  if(NOT _ort_vn STREQUAL LAYA_ORT_VERSION)
    message(FATAL_ERROR "${_ort_root}/VERSION_NUMBER says '${_ort_vn}', but laya_onnx is pinned to ${LAYA_ORT_VERSION}")
  endif()
endif()
if(APPLE)
  set(_ort_lib "${_ort_root}/lib/libonnxruntime.${LAYA_ORT_VERSION}.dylib")
  if(NOT EXISTS "${_ort_lib}")
    file(GLOB _ort_lib "${_ort_root}/lib/libonnxruntime*.dylib")
    if(NOT _ort_lib)
      message(FATAL_ERROR "no libonnxruntime*.dylib under ${_ort_root}/lib")
    endif()
    list(GET _ort_lib 0 _ort_lib)
  endif()
elseif(WIN32)
  set(_ort_lib "${_ort_root}/lib/onnxruntime.lib")
else()
  set(_ort_lib "${_ort_root}/lib/libonnxruntime.so")
endif()
if(NOT EXISTS "${_ort_lib}")
  message(FATAL_ERROR "ONNX Runtime library not found: ${_ort_lib}")
endif()
message(STATUS "ONNX Runtime ${LAYA_ORT_VERSION}: ${_ort_lib}")

add_library(laya_ort SHARED IMPORTED)
set_target_properties(laya_ort PROPERTIES IMPORTED_LOCATION "${_ort_lib}" INTERFACE_INCLUDE_DIRECTORIES "${_ort_root}/include")
set(LAYA_ORT_LIBDIR "${_ort_root}/lib")
set(LAYA_ORT_ROOT "${_ort_root}")

# Build-tree executables find the shared library through an rpath to its directory.
function(laya_ort_runtime_path target)
  set_property(TARGET ${target} APPEND PROPERTY BUILD_RPATH "${LAYA_ORT_LIBDIR}")
endfunction()