2#include <onnxruntime/onnxruntime_cxx_api.h>
6 Ort::Env
Env{ ORT_LOGGING_LEVEL_WARNING,
"PanoptesML" };
13static std::string Narrow(
const std::wstring& Text)
15 int length = WideCharToMultiByte(CP_UTF8, 0, Text.c_str(), (
int)Text.size(), NULL, 0, NULL, NULL);
16 std::string out(length,
'\0');
17 WideCharToMultiByte(CP_UTF8, 0, Text.c_str(), (
int)Text.size(), out.data(), length, NULL, NULL);
25static void QuietNodeDumps()
27 for (
const char* name : {
"ORT_DEBUG_NODE_IO_DUMP_SHAPE_DATA",
"ORT_DEBUG_NODE_IO_DUMP_NODE_PLACEMENT" }) {
28 if (GetEnvironmentVariableA(name, NULL, 0) == 0) {
29 SetEnvironmentVariableA(name,
"0");
34static Ort::SessionOptions MakeSessionOptions()
36 Ort::SessionOptions options;
39 options.SetIntraOpNumThreads(1);
40 options.SetGraphOptimizationLevel(GraphOptimizationLevel::ORT_ENABLE_ALL);
45 : m_impl((QuietNodeDumps(), std::make_unique<
Impl>()))
47 m_name = Narrow(ModelPath.substr(ModelPath.find_last_of(L
"\\/") + 1));
48 m_impl->Session = Ort::Session(m_impl->Env, ModelPath.c_str(), MakeSessionOptions());
53 : m_impl((QuietNodeDumps(), std::make_unique<
Impl>())), m_name(
Name)
56 m_impl->Session = Ort::Session(m_impl->Env, Data, Size, MakeSessionOptions());
60void MlModel::Inspect()
62 Ort::AllocatorWithDefaultOptions allocator;
63 if (m_impl->Session.GetInputCount() != 1) {
64 throw std::runtime_error(m_name +
" must have one input, the feature vector");
66 m_impl->InputName = m_impl->Session.GetInputNameAllocated(0, allocator).get();
70 Ort::TypeInfo inputType = m_impl->Session.GetInputTypeInfo(0);
71 auto input = inputType.GetTensorTypeAndShapeInfo();
72 std::vector<int64_t> shape = input.GetShape();
73 if (input.GetElementType() != ONNX_TENSOR_ELEMENT_DATA_TYPE_FLOAT || shape.size() != 2 ||
75 throw std::runtime_error(m_name +
" must take a float tensor of shape [N, " +
82 for (
size_t i = 0; i < m_impl->Session.GetOutputCount(); i++) {
83 m_impl->OutputNames.push_back(m_impl->Session.GetOutputNameAllocated(i, allocator).get());
84 Ort::TypeInfo type = m_impl->Session.GetOutputTypeInfo(i);
85 if (!found && type.GetONNXType() == ONNX_TYPE_TENSOR &&
86 type.GetTensorTypeAndShapeInfo().GetElementType() == ONNX_TENSOR_ELEMENT_DATA_TYPE_FLOAT) {
87 m_impl->ScoreOutput = i;
92 throw std::runtime_error(m_name +
" has no float tensor output to read the score from "
93 "(export scikit-learn classifiers with zipmap disabled)");
102 throw std::runtime_error(
"expected " + std::to_string(
FEATURE_COUNT) +
" features, got " + std::to_string(Features.size()));
105 Ort::MemoryInfo memory = Ort::MemoryInfo::CreateCpu(OrtArenaAllocator, OrtMemTypeDefault);
107 Ort::Value input = Ort::Value::CreateTensor<float>(memory,
const_cast<float*
>(Features.data()),
108 Features.size(), shape, 2);
110 const char* inputName = m_impl->InputName.c_str();
111 const char* outputName = m_impl->OutputNames[m_impl->ScoreOutput].c_str();
112 std::vector<Ort::Value> outputs = m_impl->Session.Run(Ort::RunOptions{
nullptr }, &inputName, &input, 1, &outputName, 1);
114 auto info = outputs[0].GetTensorTypeAndShapeInfo();
115 size_t count = info.GetElementCount();
117 throw std::runtime_error(m_name +
" returned an empty score");
119 return outputs[0].GetTensorData<
float>()[count - 1];
124 HMODULE module = NULL;
125 GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT,
128 DWORD length = GetModuleFileNameW(module, path,
MAX_PATH);
129 std::wstring directory(path, length);
130 return directory.substr(0, directory.find_last_of(L
'\\') + 1);
std::wstring ModuleDirectory()
The folder this DLL was loaded from, with a trailing backslash.
#define PANO_ML_FEATURE_VERSION
Version of the feature layout below. A model is trained for one layout; change this whenever a featur...
float Score(const std::vector< float > &Features)
Runs the model on one file's features. Returns the malicious probability.
MlModel(const std::wstring &ModelPath)
Loads the model. Throws Ort::Exception or std::runtime_error when it can't be loaded or doesn't take ...
const std::string & Name() const
std::wstring ModuleDirectory()
The folder this DLL was loaded from, with a trailing backslash.
std::vector< std::string > OutputNames