How to replace onnx by onnx-light#

This guide covers the C++ interface. For the other language, see How to replace onnx by onnx-light.

onnx-light mirrors the public onnx API in both Python and C++, so an existing project can switch to it by changing a few imports, includes and build targets. This page shows the C++ code before and after the replacement.

C++#

onnx-light replicates the upstream onnx C++ API under the onnx_light namespace. The ONNX_NAMESPACE macro resolves to onnx_light and the headers keep their familiar names, so the source change is the include root (onnx/ becomes onnx_lib/) and, where the namespace is spelled out explicitly, onnx:: becomes onnx_light::. Code that already uses the ONNX_NAMESPACE macro instead of a hard-coded onnx:: needs only the include-path change.

#include "onnx/onnx_pb.h"
#include "onnx/checker.h"

int main() {
  onnx::ModelProto model;
  onnx::checker::check_model(model);
  return 0;
}
#include "onnx_lib/onnx_pb.h"
#include "onnx_lib/checker.h"

int main() {
  onnx_light::ModelProto model;
  onnx_light::checker::check_model(model);
  return 0;
}

Build (CMake)#

Downstream CMake projects replace the onnx package and target with the onnx_light package and the equivalent onnx_light::... target. Use onnx_light::lib_onnx_lib for the schema / checker / shape-inference layer or a lighter target such as onnx_proto::lib_onnx_proto for proto-only parsing — see Linking onnx-light in C++ for the full list.

find_package(ONNX REQUIRED)
add_executable(my_target main.cc)
target_link_libraries(my_target PRIVATE onnx)
find_package(onnx_light REQUIRED)
add_executable(my_target main.cc)
target_link_libraries(my_target PRIVATE onnx_light::lib_onnx_lib)

If onnx-light is installed to a non-standard prefix, configure the downstream project with -DCMAKE_PREFIX_PATH=<prefix> before calling find_package(onnx_light REQUIRED).

See also#