Skip to content
Open
Show file tree
Hide file tree
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
4 changes: 2 additions & 2 deletions examples/mnist-client/inference.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ auto make_observer_function(std::vector<InferenceResult> &result)


// Read image dataset from a binary file and trasnform it into a vector of boolean frames.
std::vector<std::vector<bool>> read_spike_frames(const std::string &path_to_data)
std::vector<std::vector<bool>> read_spike_frames(const std::filesystem::path &path_to_data)
{
// Image-to-spikes conversion parameters.
constexpr int intensity_levels = 10;
Expand Down Expand Up @@ -127,7 +127,7 @@ std::vector<InferenceResult> do_inference(

// The largest projection is the input image projection. These are numbers for a specific MNIST model.
constexpr size_t img_input_size = 117600;
auto is_input = [](const knp::core::AllProjectionsVariant &proj)
auto is_input = [img_input_size](const knp::core::AllProjectionsVariant &proj)
{ return std::visit([](const auto &p) { return p.size(); }, proj) == img_input_size; };

std::vector<knp::core::UID> input_image_projection_uids = find_projections(model.get_network(), is_input);
Expand Down
19 changes: 16 additions & 3 deletions knp/base-framework/impl/backend_loader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,32 @@
#include <spdlog/spdlog.h>

#include <functional>
#include <unordered_map>

#include <boost/dll/import.hpp>


namespace knp::framework
{
namespace
{
auto &get_backend_creators()
{
// Keep imported backend libraries loaded for the process lifetime.
// Python and C++ backend objects can outlive a particular BackendLoader instance.
static auto *creators =
new std::unordered_map<std::string, std::function<BackendLoader::BackendCreateFunction>>();
return *creators;
}
} // namespace

std::function<BackendLoader::BackendCreateFunction> BackendLoader::make_creator(
const std::filesystem::path &backend_path)
{
auto creator_iter = creators_.find(backend_path.string());
auto &creators = get_backend_creators();
auto creator_iter = creators.find(backend_path.string());

if (creator_iter != creators_.end())
if (creator_iter != creators.end())
{
return creator_iter->second;
}
Expand All @@ -48,7 +61,7 @@ std::function<BackendLoader::BackendCreateFunction> BackendLoader::make_creator(

SPDLOG_DEBUG("Created backend creator.");

creators_[backend_path.string()] = creator;
creators[backend_path.string()] = creator;

return creator;
}
Expand Down
5 changes: 1 addition & 4 deletions knp/base-framework/include/knp/framework/backend_loader.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@
#include <knp/core/impexp.h>

#include <filesystem>
#include <functional>
#include <memory>
#include <string>
#include <unordered_map>


/**
Expand Down Expand Up @@ -70,9 +70,6 @@ class KNP_DECLSPEC BackendLoader
*/
std::function<BackendCreateFunction> make_creator(const std::filesystem::path &backend_path);

private:
// std::filesystem::path doesn't work on any compilers.
std::unordered_map<std::string, std::function<BackendCreateFunction>> creators_;
};

} // namespace knp::framework
11 changes: 10 additions & 1 deletion knp/python-framework/impl/core/cpp/uid_utilities.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

#pragma once

#include <algorithm>
#include <vector>

#include "common.h"
Expand Down Expand Up @@ -52,7 +53,15 @@ struct uid_from_python
py::extract<std::vector<uint8_t>>(py::object(py::handle<>(py::borrowed(obj))).attr("bytes"));
boost::uuids::uuid* res = new (storage) boost::uuids::uuid;

memcpy(res->data, &ba.front(), ba.size());
if (ba.size() != res->size())
{
const auto error_message = "UUID byte array must contain exactly " + std::to_string(res->size()) +
" bytes, got " + std::to_string(ba.size()) + ".";
PyErr_SetString(PyExc_ValueError, error_message.c_str());
py::throw_error_already_set();
}

std::copy(ba.begin(), ba.end(), res->begin());
data->convertible = storage;
}
};
Expand Down
38 changes: 38 additions & 0 deletions knp/python-framework/tests/unit/core/test_uid.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
"""
@file test_uid.py
@brief UID tests.

@kaspersky_support OpenAI Codex.
@license Apache 2.0 License.
@copyright © 2026 AO Kaspersky Lab
@date 08.04.2026.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""

import uuid

import pytest

from knp.core import UID


class BrokenUUID(uuid.UUID):
@property
def bytes(self) -> bytes:
return b"\x00" * 15


def test_uid_rejects_uuid_with_invalid_bytes_size() -> None:
with pytest.raises(ValueError, match="exactly 16 bytes"):
UID(BrokenUUID(bytes=b"\x00" * 16))