Skip to content

Add logging of ORT version and build info. #1405

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
17 changes: 11 additions & 6 deletions src/logging.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@
namespace Generators {

LogItems g_log;
static std::ostream* gp_stream{&std::cerr};

static std::ostream*& GlobalLogStreamPtr() {
static std::ostream* stream = &std::cerr;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

avoid static initialization order race by making this a function. on Windows, I encountered the case where gp_stream was nullptr.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The root problem is that our onnxruntime header was not supposed to have any dependencies and is now calling genai code, I forget what calls Ort:: functions first but if there's a way to clean that up, we should do that.

return stream;
}

static std::unique_ptr<std::ofstream> gp_logfile;

void SetLogBool(std::string_view name, bool value) {
Expand Down Expand Up @@ -54,9 +59,9 @@ void SetLogString(std::string_view name, std::string_view value) {
}

if (gp_logfile)
gp_stream = gp_logfile.get();
GlobalLogStreamPtr() = gp_logfile.get();
else
gp_stream = &std::cerr;
GlobalLogStreamPtr() = &std::cerr;
} else
throw JSON::unknown_value_error{};
}
Expand Down Expand Up @@ -86,10 +91,10 @@ std::ostream& Log(std::string_view label, std::string_view string) {
assert(g_log.enabled);

// Warnings will be yellow, all other labels will be blue
*gp_stream << SGR::Bold << (label == "warning" ? SGR::Bg_Yellow : SGR::Bg_Blue) << " " << label << " " << SGR::Reset << ' ';
*GlobalLogStreamPtr() << SGR::Bold << (label == "warning" ? SGR::Bg_Yellow : SGR::Bg_Blue) << " " << label << " " << SGR::Reset << ' ';
if (!string.empty())
*gp_stream << string << std::endl;
return *gp_stream;
*GlobalLogStreamPtr() << string << std::endl;
return *GlobalLogStreamPtr();
}

std::ostream& Log(std::string_view label, const char* fmt, ...) {
Expand Down
9 changes: 7 additions & 2 deletions src/models/onnxruntime_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,8 @@ inline void InitApi() {
Generators::SetLogBool("ort_lib", true);
}

OrtApiBaseFn ort_api_base_fn{};

#if defined(__linux__) || defined(MACOS_USE_DLOPEN)
// If the GenAI library links against the onnxruntime library, it will have a dependency on a specific
// version of OrtGetApiBase.
Expand Down Expand Up @@ -257,18 +259,21 @@ inline void InitApi() {
throw std::runtime_error(std::string("Failed to load onnxruntime. Set ORTGENAI_LOG_ORT_LIB envvar to enable detailed logging."));
}

OrtApiBaseFn ort_api_base_fn = (OrtApiBaseFn)dlsym(ort_lib_handle, "OrtGetApiBase");
ort_api_base_fn = (OrtApiBaseFn)dlsym(ort_lib_handle, "OrtGetApiBase");
if (ort_api_base_fn == nullptr) {
char* err = dlerror();
throw std::runtime_error(std::string("Failed to load symbol OrtGetApiBase: ") + (err != nullptr ? err : "Unknown"));
}

InitApiWithDynamicFn(ort_api_base_fn);
#else // defined(__linux__) || defined(MACOS_USE_DLOPEN)
api = OrtGetApiBase()->GetApi(ORT_API_VERSION);
ort_api_base_fn = &OrtGetApiBase;
api = ort_api_base_fn()->GetApi(ORT_API_VERSION);
if (!api)
throw std::runtime_error("Onnxruntime is installed but is too old, please install a newer version");
#endif // defined(__linux__) || defined(MACOS_USE_DLOPEN)

LOG_INFO("ORT Version: %s. %s", ort_api_base_fn()->GetVersionString(), api->GetBuildInfoString());
}

/** \brief All C++ methods that can fail will throw an exception of this type
Expand Down
Loading