Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions triton_npu/kernel_registry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,20 @@

#include <filesystem>
#include <fstream>
#include <mutex>
#include <nlohmann/json.hpp>

namespace xllm::kernel::npu {

namespace {

std::mutex& registry_mutex() {
static std::mutex mutex;
return mutex;
}

} // namespace

KernelRegistry& KernelRegistry::get_instance() {
static KernelRegistry instance;
return instance;
Expand Down Expand Up @@ -111,6 +121,7 @@ bool KernelRegistry::parse_json_config(const std::string& json_path,

bool KernelRegistry::register_kernel(const std::string& kernel_name,
const std::string& binary_path) {
std::lock_guard<std::mutex> lock(registry_mutex());
if (kernel_infos_.find(kernel_name) != kernel_infos_.end()) {
LOG(INFO) << "Kernel '" << kernel_name << "' is already registered";
return true;
Expand Down Expand Up @@ -188,6 +199,7 @@ bool KernelRegistry::register_kernel(const std::string& kernel_name,

KernelStubHandle KernelRegistry::get_kernel_stub(
const std::string& kernel_name) const {
std::lock_guard<std::mutex> lock(registry_mutex());
auto it = kernel_infos_.find(kernel_name);
if (it == kernel_infos_.end()) {
return nullptr;
Expand All @@ -197,13 +209,15 @@ KernelStubHandle KernelRegistry::get_kernel_stub(

bool KernelRegistry::is_kernel_registered(
const std::string& kernel_name) const {
std::lock_guard<std::mutex> lock(registry_mutex());
return kernel_infos_.find(kernel_name) != kernel_infos_.end();
}

bool KernelRegistry::get_kernel_workspace_config(const std::string& kernel_name,
int64_t& workspace_size,
int64_t& lock_init_value,
int64_t& lock_num) const {
std::lock_guard<std::mutex> lock(registry_mutex());
auto it = kernel_infos_.find(kernel_name);
if (it != kernel_infos_.end()) {
workspace_size = it->second.workspace_size;
Expand Down Expand Up @@ -296,6 +310,7 @@ bool KernelRegistry::register_binary(KernelInfo& info, uint32_t binary_size) {
}

void KernelRegistry::cleanup() {
std::lock_guard<std::mutex> lock(registry_mutex());
size_t count = kernel_infos_.size();
for (auto& [name, info] : kernel_infos_) {
if (info.buffer) {
Expand Down