.. _program_listing_file_Project-DJ-Engine_include_util_ai_AI.cpp: Program Listing for File AI.cpp =============================== |exhale_lsh| :ref:`Return to documentation for file ` (``Project-DJ-Engine\include\util\ai\AI.cpp``) .. |exhale_lsh| unicode:: U+021B0 .. UPWARDS ARROW WITH TIP LEFTWARDS .. code-block:: cpp #include "util/ai/AI.hpp" #include #include #include #include #include #include #include #include #include #include namespace PDJE_UTIL::ai { namespace { Ort::Env & GlobalOrtEnv() { static Ort::Env env(ORT_LOGGING_LEVEL_WARNING, "pdje_util_ai"); return env; } GraphOptimizationLevel ToOrtOptimizationLevel(const OnnxOptimizationLevel level) { switch (level) { case OnnxOptimizationLevel::DISABLE_ALL: return GraphOptimizationLevel::ORT_DISABLE_ALL; case OnnxOptimizationLevel::BASIC: return GraphOptimizationLevel::ORT_ENABLE_BASIC; case OnnxOptimizationLevel::EXTENDED: return GraphOptimizationLevel::ORT_ENABLE_EXTENDED; case OnnxOptimizationLevel::ALL: return GraphOptimizationLevel::ORT_ENABLE_ALL; } throw std::invalid_argument("unsupported onnx optimization level"); } void ValidateSessionOptions(const OnnxSessionOptions &options) { if (options.intra_op_num_threads < 0 || options.inter_op_num_threads < 0) { throw std::invalid_argument( "onnx session thread counts must not be negative"); } } std::string ReadIoName(Ort::Session &session, const bool input, const std::size_t index, Ort::AllocatorWithDefaultOptions &allocator) { Ort::AllocatedStringPtr name = input ? session.GetInputNameAllocated(index, allocator) : session.GetOutputNameAllocated(index, allocator); if (!name || name.get()[0] == '\0') { throw std::runtime_error(input ? "onnx model input name is empty" : "onnx model output name is empty"); } return std::string(name.get()); } std::vector ReadIoNames(Ort::Session &session, const bool input) { Ort::AllocatorWithDefaultOptions allocator; const std::size_t count = input ? session.GetInputCount() : session.GetOutputCount(); std::vector names; names.reserve(count); for (std::size_t idx = 0; idx < count; ++idx) { names.push_back(ReadIoName(session, input, idx, allocator)); } return names; } std::size_t ExpectedTensorElementCount(const std::vector &shape) { if (shape.empty()) { return 1u; } std::size_t elementCount = 1u; for (const int64_t dim : shape) { if (dim < 0) { throw std::invalid_argument( "onnx float tensor dimensions must not be negative"); } const std::size_t unsignedDim = static_cast(dim); if (unsignedDim == 0u) { return 0u; } if (elementCount > (std::numeric_limits::max() / unsignedDim)) { throw std::overflow_error("onnx float tensor shape is too large"); } elementCount *= unsignedDim; } return elementCount; } void ValidateFloatTensor(const FloatTensor &tensor) { const std::size_t expectedCount = ExpectedTensorElementCount(tensor.shape); if (tensor.values.size() != expectedCount) { throw std::invalid_argument( "onnx float tensor flattened storage size does not match shape"); } } std::vector ResolveOutputNames(const std::vector &knownNames, std::span requestedNames) { if (requestedNames.empty()) { return knownNames; } std::vector resolved; resolved.reserve(requestedNames.size()); for (const std::string &requestedName : requestedNames) { if (requestedName.empty()) { throw std::invalid_argument( "onnx requested output name must not be empty"); } if (std::find(knownNames.begin(), knownNames.end(), requestedName) == knownNames.end()) { throw std::invalid_argument( "onnx requested output name is not recognized: " + requestedName); } if (std::find(resolved.begin(), resolved.end(), requestedName) != resolved.end()) { throw std::invalid_argument( "onnx requested output name was duplicated: " + requestedName); } resolved.push_back(requestedName); } return resolved; } std::vector BuildNamePointers(const std::vector &names) { std::vector rawNames; rawNames.reserve(names.size()); for (const std::string &name : names) { rawNames.push_back(name.c_str()); } return rawNames; } const NamedFloatTensor & FindInputByName(std::span inputs, const std::string &name) { const NamedFloatTensor *found = nullptr; for (const NamedFloatTensor &input : inputs) { if (input.name != name) { continue; } if (found != nullptr) { throw std::invalid_argument("onnx input name was duplicated: " + name); } found = &input; } if (found == nullptr) { throw std::invalid_argument("onnx input is missing: " + name); } return *found; } std::vector BuildInputValues(std::span inputs, const std::vector &expectedNames) { if (inputs.size() != expectedNames.size()) { throw std::invalid_argument( "onnx input count does not match model input count"); } static const Ort::MemoryInfo memoryInfo = Ort::MemoryInfo::CreateCpu(OrtArenaAllocator, OrtMemTypeDefault); std::vector values; values.reserve(expectedNames.size()); for (const std::string &expectedName : expectedNames) { const NamedFloatTensor &input = FindInputByName(inputs, expectedName); if (input.name.empty()) { throw std::invalid_argument("onnx input name must not be empty"); } ValidateFloatTensor(input.tensor); values.push_back(Ort::Value::CreateTensor( memoryInfo, const_cast(input.tensor.values.data()), input.tensor.values.size(), input.tensor.shape.data(), input.tensor.shape.size())); } return values; } FloatTensor CopyOutputTensor(const Ort::Value &value) { if (!value.IsTensor()) { throw std::runtime_error("onnx output value is not a tensor"); } const Ort::TensorTypeAndShapeInfo info = value.GetTensorTypeAndShapeInfo(); if (info.GetElementType() != ONNX_TENSOR_ELEMENT_DATA_TYPE_FLOAT) { throw std::runtime_error("onnx output tensor is not float"); } const std::vector shape = info.GetShape(); const std::size_t elementCount = info.GetElementCount(); const float *data = value.GetTensorData(); return FloatTensor{ .shape = shape, .values = std::vector(data, data + elementCount), }; } } // namespace class OnnxSession::Impl { public: Impl(const std::filesystem::path &modelPath, const OnnxSessionOptions &options, std::vector &inputNames, std::vector &outputNames) { if (modelPath.empty()) { throw std::invalid_argument( "onnx session model path must not be empty"); } if (!std::filesystem::exists(modelPath)) { throw std::runtime_error("onnx model was not found: " + modelPath.string()); } ValidateSessionOptions(options); Ort::SessionOptions sessionOptions; sessionOptions.SetGraphOptimizationLevel( ToOrtOptimizationLevel(options.optimization_level)); if (options.intra_op_num_threads > 0) { sessionOptions.SetIntraOpNumThreads(options.intra_op_num_threads); } if (options.inter_op_num_threads > 0) { sessionOptions.SetInterOpNumThreads(options.inter_op_num_threads); } session_ = std::make_unique( GlobalOrtEnv(), modelPath.c_str(), sessionOptions); inputNames = ReadIoNames(*session_, true); outputNames = ReadIoNames(*session_, false); } std::vector run(std::span inputs, std::span requestedOutputNames, const std::vector &inputNames, const std::vector &outputNames) const { const std::vector resolvedOutputNames = ResolveOutputNames(outputNames, requestedOutputNames); std::vector inputValues = BuildInputValues(inputs, inputNames); const std::vector inputNamePointers = BuildNamePointers(inputNames); const std::vector outputNamePointers = BuildNamePointers(resolvedOutputNames); auto outputs = session_->Run(Ort::RunOptions{ nullptr }, inputNamePointers.data(), inputValues.data(), inputValues.size(), outputNamePointers.data(), outputNamePointers.size()); if (outputs.size() != resolvedOutputNames.size()) { throw std::runtime_error( "onnx runtime returned an unexpected number of outputs"); } std::vector results; results.reserve(outputs.size()); for (std::size_t idx = 0; idx < outputs.size(); ++idx) { results.push_back(NamedFloatTensor{ .name = resolvedOutputNames[idx], .tensor = CopyOutputTensor(outputs[idx]), }); } return results; } std::unique_ptr session_; }; struct OnnxSession::Build { std::filesystem::path model_path; OnnxSessionOptions options; std::vector input_names; std::vector output_names; std::unique_ptr impl; }; OnnxSession::Build OnnxSession::build(std::filesystem::path model_path, OnnxSessionOptions options) { Build result{ .model_path = std::move(model_path), .options = std::move(options) }; result.impl = std::make_unique(result.model_path, result.options, result.input_names, result.output_names); return result; } OnnxSession::OnnxSession(Build build) : model_path(std::move(build.model_path)), options(std::move(build.options)), input_names(std::move(build.input_names)), output_names(std::move(build.output_names)), impl_(std::move(build.impl)) { } OnnxSession::OnnxSession(std::filesystem::path model_path, OnnxSessionOptions options) : OnnxSession(build(std::move(model_path), std::move(options))) { } OnnxSession::~OnnxSession() = default; OnnxSession::OnnxSession(OnnxSession &&other) : model_path(other.model_path), options(other.options), input_names(other.input_names), output_names(other.output_names), impl_(std::move(other.impl_)) { } std::vector OnnxSession::run(const std::span inputs) const { return run(inputs, std::span{}); } std::vector OnnxSession::run( const std::span inputs, const std::span requested_output_names) const { if (!impl_) { throw std::runtime_error("onnx session is not initialized"); } return impl_->run( inputs, requested_output_names, input_names, output_names); } } // namespace PDJE_UTIL::ai