Skip to content

Commit

Permalink
- add ability to run more than a single ONNX model
Browse files Browse the repository at this point in the history
  • Loading branch information
christoph-hart committed Nov 9, 2024
1 parent 056ee73 commit 2636d04
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 60 deletions.
2 changes: 1 addition & 1 deletion currentGitHash.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
bab0acff62b8f04252bb4d4cffb7999f51a537cc
056ee73fdda33d666989491af04ab1694247287f
2 changes: 1 addition & 1 deletion hi_backend/backend/currentGit.h
Original file line number Diff line number Diff line change
@@ -1 +1 @@
#define PREVIOUS_HISE_COMMIT "bab0acff62b8f04252bb4d4cffb7999f51a537cc"
#define PREVIOUS_HISE_COMMIT "056ee73fdda33d666989491af04ab1694247287f"
15 changes: 5 additions & 10 deletions hi_core/hi_core/MainController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -728,18 +728,13 @@ bool MainController::shouldUseSoftBypassRamps() const noexcept

ONNXLoader::Ptr MainController::getONNXLoader()
{
if(onnxLoader == nullptr)
{
#if USE_BACKEND || 1
File libraryPath(GET_HISE_SETTING(getMainSynthChain(), HiseSettings::Compiler::HisePath).toString());
libraryPath = libraryPath.getChildFile("tools/onnx_lib");
#if USE_BACKEND
File libraryPath(GET_HISE_SETTING(getMainSynthChain(), HiseSettings::Compiler::HisePath).toString());
libraryPath = libraryPath.getChildFile("tools/onnx_lib");
#else
auto libraryPath = FrontendHandler::getAppDataDirectory(this);
auto libraryPath = FrontendHandler::getAppDataDirectory(this);
#endif
onnxLoader = new ONNXLoader(libraryPath.getFullPathName());
}

return onnxLoader;
return new ONNXLoader(libraryPath.getFullPathName());
}

MarkdownContentProcessor* MainController::getCurrentMarkdownPreview()
Expand Down
117 changes: 71 additions & 46 deletions hi_tools/hi_neural/onnx_loader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,55 +36,12 @@ using namespace juce;
ONNXLoader::ONNXLoader(const String& rootDir):
ok(Result::fail("dll could not open"))
{
if(File::isAbsolutePath(rootDir) && File(rootDir).isDirectory())
{
auto dllFile = File(rootDir);

#if JUCE_DEBUG
#if JUCE_WINDOWS
dllFile = dllFile.getChildFile("Builds/VisualStudio2022/x64/Debug/Dynamic Library");
#else
dllFile = dllFile.getChildFile("Builds/MacOSX/build/Debug");
#endif
#endif

#if JUCE_WINDOWS
dllFile = dllFile.getChildFile("onnx_hise_library.dll");
#elif JUCE_MAC
dllFile = dllFile.getChildFile("onnx_hise_library.dylib");
#elif JUCE_LINUX
// DAVID!!!!
jassertfalse;
#endif

if(!dllFile.existsAsFile())
{
ok = Result::fail("Missing dynamic library " + dllFile.getFullPathName());
}
else
{
dll = new DynamicLibrary();

if(dll->open(dllFile.getFullPathName()))
ok = Result::ok();
else
dll = nullptr;
}
}
ok = data->initialise(rootDir);
}

ONNXLoader::~ONNXLoader()
{
if(currentModel != nullptr)
{
jassert(dll != nullptr);

if(auto f = getFunction<destroyModel_f>("destroyModel"))
f(currentModel);

currentModel = nullptr;
dll = nullptr;
}
unloadModel();
}

bool ONNXLoader::run(const Image& img, std::vector<float>& outputValues, bool isGreyscale)
Expand Down Expand Up @@ -127,7 +84,9 @@ bool ONNXLoader::run(const Image& img, std::vector<float>& outputValues, bool is

Result ONNXLoader::loadModel(const MemoryBlock& mb)
{
if(dll != nullptr)
unloadModel();

if(data->dll != nullptr)
{
if(auto f = getFunction<loadModel_f>("loadModel"))
{
Expand All @@ -149,6 +108,20 @@ Result ONNXLoader::getLastError() const
return ok;
}

void ONNXLoader::unloadModel()
{
if(currentModel != nullptr)
{
jassert(data->dll != nullptr);

if(auto f = getFunction<destroyModel_f>("destroyModel"))
f(currentModel);

currentModel = nullptr;

}
}

void ONNXLoader::setLastError()
{
if(auto f = getFunction<getError_f>("getError"))
Expand All @@ -165,4 +138,56 @@ void ONNXLoader::setLastError()
}
}

ONNXLoader::SharedData::SharedData():
ok(Result::ok())
{}

ONNXLoader::SharedData::~SharedData()
{
dll = nullptr;
}

Result ONNXLoader::SharedData::initialise(const String& rootDir)
{
if(dll != nullptr)
return ok;

if(File::isAbsolutePath(rootDir) && File(rootDir).isDirectory())
{
auto dllFile = File(rootDir);

#if JUCE_DEBUG
#if JUCE_WINDOWS
dllFile = dllFile.getChildFile("Builds/VisualStudio2022/x64/Debug/Dynamic Library");
#else
dllFile = dllFile.getChildFile("Builds/MacOSX/build/Debug");
#endif
#endif

#if JUCE_WINDOWS
dllFile = dllFile.getChildFile("onnx_hise_library.dll");
#elif JUCE_MAC
dllFile = dllFile.getChildFile("onnx_hise_library.dylib");
#elif JUCE_LINUX
// DAVID!!!!
jassertfalse;
#endif

if(!dllFile.existsAsFile())
{
ok = Result::fail("Missing dynamic library " + dllFile.getFullPathName());
}
else
{
dll = new DynamicLibrary();

if(dll->open(dllFile.getFullPathName()))
ok = Result::ok();
else
dll = nullptr;
}
}

return ok;
}
} // namespace hise
19 changes: 17 additions & 2 deletions hi_tools/hi_neural/onnx_loader.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,14 @@ class ONNXLoader: public ReferenceCountedObject

private:

void unloadModel();

void setLastError();

template <typename Func> Func getFunction(const String& name)
{

if(auto f = reinterpret_cast<Func>(dll->getFunction(name)))
if(auto f = reinterpret_cast<Func>(data->dll->getFunction(name)))
return f;
else
{
Expand All @@ -74,7 +76,20 @@ class ONNXLoader: public ReferenceCountedObject
}
}

ScopedPointer<DynamicLibrary> dll{};
struct SharedData
{
SharedData();

~SharedData();

Result initialise(const String& rootDir);

Result ok;
ScopedPointer<DynamicLibrary> dll;
};

SharedResourcePointer<SharedData> data;

Result ok;
void* currentModel = nullptr;

Expand Down

0 comments on commit 2636d04

Please sign in to comment.