77 lines
2.9 KiB
C++
77 lines
2.9 KiB
C++
#ifndef ANSMODELSOURCE_H
|
|||
|
|
#define ANSMODELSOURCE_H
|
||
|
|
#pragma once
|
||
|
|
// ANSModelSource — where a model's bytes come from: a file, or memory.
|
||
|
|
//
|
||
|
|
// Every engine here built its session from a file path, which is why loading
|
||
|
|
// an encrypted model package means inflating it to a directory first: the
|
||
|
|
// unprotected model lands on disk, is optimised, and is zipped back into a
|
||
|
|
// cache. Handing the runtime a buffer instead removes both the round trip and
|
||
|
|
// the plaintext on disk, and it is the one change the new .ansmodel package
|
||
|
|
// format cannot work without.
|
||
|
|
//
|
||
|
|
// A model in memory still needs two things a path gave for free. `name` is
|
||
|
|
// what logs call it, since there is no file name to fall back on. And
|
||
|
|
// ONNX Runtime copies what it needs out of the buffer while the session is
|
||
|
|
// being built, so the caller may wipe its plaintext the moment MakeSession
|
||
|
|
// returns — which is exactly what a package reader should do.
|
||
|
|
|
||
|
|
#include <string>
|
||
|
|
|
||
|
|
#include <onnxruntime_cxx_api.h>
|
||
|
|
|
||
|
|
namespace ANSCENTER {
|
||
|
|
|
||
|
|
struct ModelSource {
|
||
|
|
std::string path; // file on disk; empty for a model already in memory
|
||
|
|
std::string name; // what to call it in logs
|
||
|
|
const void* data = nullptr;
|
||
|
|
size_t size = 0;
|
||
|
|
|
||
|
|
bool inMemory() const { return data != nullptr && size > 0; }
|
||
|
|
|
||
|
|
// Named factories, never implicit conversions: an engine that offers
|
||
|
|
// both Init(std::string) and Init(ModelSource) must not become
|
||
|
|
// ambiguous for a caller passing a plain string literal.
|
||
|
|
static ModelSource File(std::string p) {
|
||
|
|
ModelSource s;
|
||
|
|
s.name = p;
|
||
|
|
s.path = std::move(p);
|
||
|
|
return s;
|
||
|
|
}
|
||
|
|
static ModelSource Memory(const void* d, size_t n, std::string name) {
|
||
|
|
ModelSource s;
|
||
|
|
s.data = d;
|
||
|
|
s.size = n;
|
||
|
|
s.name = std::move(name);
|
||
|
|
return s;
|
||
|
|
}
|
||
|
|
};
|
||
|
|
|
||
|
|
// What to call this model in a log line.
|
||
|
|
inline std::string ModelSourceName(const ModelSource& src) {
|
||
|
|
if (!src.name.empty()) return src.name;
|
||
|
|
if (!src.path.empty()) return src.path;
|
||
|
|
return "<memory>";
|
||
|
|
}
|
||
|
|
|
||
|
|
// Build a session from wherever the bytes are. One place, so the Windows
|
||
|
|
// wide-path conversion and the from-memory call are not repeated in every
|
||
|
|
// engine — they were already duplicated eleven times.
|
||
|
|
inline Ort::Session MakeSession(Ort::Env& env, const ModelSource& src,
|
||
|
|
const Ort::SessionOptions& options) {
|
||
|
|
if (src.inMemory())
|
||
|
|
return Ort::Session(env, src.data, src.size, options);
|
||
|
|
#ifdef _WIN32
|
||
|
|
// ORT's Windows API takes a wide (ORTCHAR_T = wchar_t) path.
|
||
|
|
const std::wstring wide(src.path.begin(), src.path.end());
|
||
|
|
return Ort::Session(env, wide.c_str(), options);
|
||
|
|
#else
|
||
|
|
return Ort::Session(env, src.path.c_str(), options);
|
||
|
|
#endif
|
||
|
|
}
|
||
|
|
|
||
|
|
} // namespace ANSCENTER
|
||
|
|
|
||
|
|
#endif // ANSMODELSOURCE_H
|