microsoft / onnxruntime-extensions

onnxruntime-extensions: A specialized pre- and post- processing library for ONNX Runtime
MIT License
323 stars 84 forks source link

RegisterCustomOps failing with incompatible version #642

Closed natangoldgm closed 7 months ago

natangoldgm commented 8 months ago

Hi, I want to compile ort custom ops for that I'm trying to implement RegisterCustomOps in the cpp implementation file. I compiled onnxruntime extensions and onnxruntime from main. My error is happening when creating Ort::CustomOpDomain. The error is coming when I'm calling to: so.register_custom_ops_library(custom_op_path) This is the error: The given version [18] is not supported, only version 1 to 10 is supported in this build.

This is my register function:

#include <tuple>
#include <stdio.h>
#include <torch/script.h>

#include "ocos.h"
#include "onnxruntime_extensions.h"
#include <onnxruntime_c_api.h>
#define ORT_API_MANUAL_INIT
#include "onnxruntime_cxx_api.h"
#undef ORT_API_MANUAL_INIT

#include "core/common/common.h"
#include "core/framework/ortdevice.h"
#include "core/framework/ortmemoryinfo.h"

static const char* c_OpDomainGM = "test.customop";

OrtStatus* ORT_API_CALL RegisterCustomOps(OrtSessionOptions* options, const OrtApiBase* api)
{
    printf("\nregister custom ops\n");
    OrtStatus* result = nullptr;
    printf("\ninit api\n");
    Ort::InitApi(api->GetApi(ORT_API_VERSION));
    printf("\ncreating session options\n");
    Ort::UnownedSessionOptions session_options(options);
    ORT_TRY {
      printf("\ncreating domain\n");
      Ort::CustomOpDomain domain{c_OpDomain};
      printf("\nadding domain\n");
      session_options.Add(domain);
    }
    ORT_CATCH(const std::exception& e) {
      ORT_HANDLE_EXCEPTION([&]() {
      printf("\ncaught exception\n");
        Ort::Status status{e};
        result = status.release();
      });
    }

    printf("\n\nfinished subscribing");
    return result;
}

This is my code compiling the custom op:

def PreTopK_LoadAndRegister():
    custom_op_dir = "/data2/target/uc_workspace/ultracruise/hexagon_uc/custom_ops/pre_top_k/onnx/.."
    sources = [
        custom_op_dir+"/CustomOp_Package/src/preTopK_ninja.cpp",
    ]

    extra_include_paths = ['/data2/onnxruntime-extensions/includes/',
                           "/data2/onnxruntime/include/", "/data2/onnxruntime/include/onnxruntime/",
                           "/data2/onnxruntime/include/onnxruntime/core/session/"]
    extra_libs_path=["-L/data2/onnxruntime-extensions/out/Linux/RelWithDebInfo/lib/", "-lortextensions",
                     "-L/data2/onnxruntime/build/Linux/RelWithDebInfo", "-lonnxruntime"]

    qcc_env_variable = os.environ.get("CC")
    qpp_env_variable = os.environ.get("CXX")
    os.environ["CC"] = "gcc"
    os.environ["CXX"] = "g++"

    torch.utils.cpp_extension.load(
        name="PreTopK",
        extra_cflags =['-std=c++17'],
        sources=sources,
        is_python_module=False,
        verbose=True,
        extra_include_paths=extra_include_paths,
        extra_ldflags=extra_libs_path
    )

    if qcc_env_variable is not None:
        os.environ["CC"] = qcc_env_variable
    if qpp_env_variable is not None:
        os.environ["CXX"] = qpp_env_variable

    torch.onnx.register_custom_op_symbolic('GM_COP_PTK::PreTopK', PreTopK_onnx, 9)
wenbingl commented 8 months ago

I didn't see how this issue is related to ort-extensions here. For the error message, it looks like onnxruntime complains about API_VERSION 18 is too high, it might be caused by the wrong onnxruntime.so was loaded.

natangoldgm commented 7 months ago

@wenbingl So the problem is that python onnxruntime version is not matching cpp onnxruntime?

natangoldgm commented 7 months ago

That was indeed misalignment between ort c++ version and ort python version. Closing this ticket, thanks.