-
Notifications
You must be signed in to change notification settings - Fork 151
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
C, C++, and Python API for Adapters (Multi-LoRA and others) (#911)
- Loading branch information
1 parent
7998f13
commit 47132b6
Showing
23 changed files
with
483 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
#include "../generators.h" | ||
#include "model.h" | ||
|
||
namespace Generators { | ||
|
||
Adapter::Adapter(const char* adapter_file_path, Ort::Allocator* allocator) | ||
: adapter_{OrtLoraAdapter::Create(fs::path(adapter_file_path).c_str(), *allocator)} {} | ||
|
||
const OrtLoraAdapter* Adapter::AcquireRef() { | ||
ref_count_++; | ||
|
||
return adapter_.get(); | ||
} | ||
|
||
void Adapter::ReleaseRef() { | ||
ref_count_--; | ||
if (ref_count_ < 0) { | ||
throw std::runtime_error("Adapter ref count went negative."); | ||
} | ||
} | ||
|
||
int32_t Adapter::RefCount() const { | ||
return ref_count_; | ||
} | ||
|
||
Adapters::Adapters(const Model* model) : model_{model} {} | ||
|
||
void Adapters::LoadAdapter(const char* adapter_file_path, const std::string& adapter_name) { | ||
if (adapters_.find(adapter_name) != adapters_.end()) { | ||
throw std::runtime_error("Adapter already loaded: " + std::string{adapter_name}); | ||
} | ||
|
||
adapters_.emplace(adapter_name, std::make_unique<Adapter>(adapter_file_path, | ||
model_->allocator_device_ == &model_->allocator_cpu_ | ||
? nullptr | ||
: model_->allocator_device_)); | ||
} | ||
|
||
void Adapters::UnloadAdapter(const std::string& adapter_name) { | ||
auto adapter = adapters_.find(adapter_name); | ||
if (adapter == adapters_.end()) { | ||
throw std::runtime_error("Adapter not found: " + std::string{adapter_name}); | ||
} | ||
|
||
if (adapter->second->RefCount() > 0) { | ||
throw std::runtime_error("Adapter still in use: " + std::string{adapter_name}); | ||
} | ||
|
||
adapters_.erase(adapter); | ||
} | ||
|
||
const OrtLoraAdapter* Adapters::AcquireAdapter(const std::string& adapter_name) { | ||
auto adapter = adapters_.find(adapter_name); | ||
if (adapter == adapters_.end()) { | ||
throw std::runtime_error("Adapter not found: " + std::string{adapter_name}); | ||
} | ||
|
||
return adapter->second->AcquireRef(); | ||
} | ||
|
||
void Adapters::ReleaseAdapter(const std::string& adapter_name) { | ||
auto adapter = adapters_.find(adapter_name); | ||
if (adapter == adapters_.end()) { | ||
throw std::runtime_error("Adapter not found: " + std::string{adapter_name}); | ||
} | ||
|
||
adapter->second->ReleaseRef(); | ||
} | ||
|
||
} // namespace Generators |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
#pragma once | ||
|
||
namespace Generators { | ||
|
||
struct Adapter { | ||
Adapter() = delete; | ||
Adapter(const Adapter&) = delete; | ||
Adapter& operator=(const Adapter&) = delete; | ||
|
||
Adapter(const char* adapter_file_path, Ort::Allocator* allocator); | ||
|
||
const OrtLoraAdapter* AcquireRef(); | ||
|
||
void ReleaseRef(); | ||
|
||
int32_t RefCount() const; | ||
|
||
private: | ||
int32_t ref_count_{}; | ||
std::unique_ptr<OrtLoraAdapter> adapter_; | ||
}; | ||
|
||
struct Adapters : std::enable_shared_from_this<Adapters> { | ||
Adapters() = delete; | ||
Adapters(const Adapters&) = delete; | ||
Adapters& operator=(const Adapters&) = delete; | ||
|
||
Adapters(const Model* model); | ||
|
||
void LoadAdapter(const char* adapter_file_path, const std::string& adapter_name); | ||
|
||
void UnloadAdapter(const std::string& adapter_name); | ||
|
||
const OrtLoraAdapter* AcquireAdapter(const std::string& adapter_name); | ||
|
||
void ReleaseAdapter(const std::string& adapter_name); | ||
|
||
std::shared_ptr<Adapters> external_owner_; | ||
|
||
private: | ||
const Model* model_; | ||
std::unordered_map<std::string, std::unique_ptr<Adapter>> adapters_; | ||
}; | ||
|
||
} // namespace Generators |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.