diff --git a/knp/CMakeLists.txt b/knp/CMakeLists.txt index a5d34323..6fdb4edd 100644 --- a/knp/CMakeLists.txt +++ b/knp/CMakeLists.txt @@ -42,7 +42,6 @@ add_subdirectory(meta-library) add_subdirectory(neuron-traits-library) add_subdirectory(synapse-traits-library) add_subdirectory(core-library) -add_subdirectory(devices-library) add_subdirectory(backends) add_subdirectory(base-framework) add_subdirectory(python-framework) diff --git a/knp/autodoc/directories.dox b/knp/autodoc/directories.dox index 0d926c13..84f96958 100644 --- a/knp/autodoc/directories.dox +++ b/knp/autodoc/directories.dox @@ -204,23 +204,23 @@ */ /** - * @dir /knp/devices-library - * @brief Device library directory. + * @dir /knp/backends/cpu/cpu-devices + * @brief CPU device library directory. */ /** - * @dir /knp/devices-library/include - * @brief Include directory for devices library. + * @dir /knp/backends/cpu/cpu-devices/include + * @brief Include directory for CPU devices library. */ /** - * @dir /knp/devices-library/include/knp - * @brief Include directory for devices library. + * @dir /knp/backends/cpu/cpu-devices/include/knp + * @brief Include directory for CPU devices library. */ /** - * @dir /knp/devices-library/include/knp/devices - * @brief Include directory for devices library. + * @dir /knp/backends/cpu/cpu-devices/include/knp/devices + * @brief Include directory for CPU devices library. */ /** diff --git a/knp/backends/cpu/CMakeLists.txt b/knp/backends/cpu/CMakeLists.txt index df89883e..c3787289 100644 --- a/knp/backends/cpu/CMakeLists.txt +++ b/knp/backends/cpu/CMakeLists.txt @@ -14,20 +14,21 @@ See the License for the specific language governing permissions and limitations under the License. ]] -cmake_minimum_required(VERSION 3.25) - -add_subdirectory(cpu-library) -add_subdirectory(thread_pool) - -file(GLOB backends LIST_DIRECTORIES true "[a-zA-Z0-9_]*-backend") - -if(NOT backends) - message(WARNING "No backend was found.") - return() -endif() - -foreach(backend ${backends}) - message(STATUS "Adding backend \"${backend}\"...") - get_filename_component(BACKEND_NAME "${backend}" NAME) - add_subdirectory("${backend}") -endforeach() +cmake_minimum_required(VERSION 3.25) + +add_subdirectory(cpu-devices) +add_subdirectory(cpu-library) +add_subdirectory(thread_pool) + +file(GLOB backends LIST_DIRECTORIES true "[a-zA-Z0-9_]*-backend") + +if(NOT backends) + message(WARNING "No backend was found.") + return() +endif() + +foreach(backend ${backends}) + message(STATUS "Adding backend \"${backend}\"...") + get_filename_component(BACKEND_NAME "${backend}" NAME) + add_subdirectory("${backend}") +endforeach() diff --git a/knp/devices-library/CMakeLists.txt b/knp/backends/cpu/cpu-devices/CMakeLists.txt similarity index 70% rename from knp/devices-library/CMakeLists.txt rename to knp/backends/cpu/cpu-devices/CMakeLists.txt index c7fbb729..f22842dd 100644 --- a/knp/devices-library/CMakeLists.txt +++ b/knp/backends/cpu/cpu-devices/CMakeLists.txt @@ -23,7 +23,7 @@ set(CMAKE_CXX_EXTENSIONS OFF) # Required for the linters. set(CMAKE_EXPORT_COMPILE_COMMANDS ON) -project(knp-devices VERSION "${KNP_VERSION}" LANGUAGES C CXX +project(knp-cpu-devices VERSION "${KNP_VERSION}" LANGUAGES C CXX DESCRIPTION "Kaspersky Neuromorphic Platform device parameters library" HOMEPAGE_URL "https://neuro.kaspersky.ru/neyromorfnye-tekhnologii/") @@ -41,7 +41,7 @@ else() set(${PROJECT_NAME}_CPU_SOURCE impl/unknown_cpu.cpp) endif() -knp_add_library("${PROJECT_NAME}-cpu" +knp_add_library("${PROJECT_NAME}" STATIC ${${PROJECT_NAME}_CPU_SOURCE} @@ -54,41 +54,17 @@ knp_add_library("${PROJECT_NAME}-cpu" ) set_target_properties( - "${PROJECT_NAME}-cpu" + "${PROJECT_NAME}" PROPERTIES VERSION "${CMAKE_PROJECT_VERSION}" SOVERSION "${CMAKE_PROJECT_VERSION_MAJOR}") -knp_add_library("${PROJECT_NAME}-altai" - STATIC - impl/altai.cpp - - include/${${PROJECT_NAME}_PUBLIC_INCLUDE_DIR}/altai.h - - LINK_PRIVATE - spdlog::spdlog_header_only - KNP::Core - ALIAS KNP::Devices::AltAI -) - -set_target_properties( - "${PROJECT_NAME}-altai" - PROPERTIES - VERSION "${CMAKE_PROJECT_VERSION}" - SOVERSION "${CMAKE_PROJECT_VERSION_MAJOR}" -) if (KNP_INSTALL) set(COMPONENT_NAME "cpp-framework") - install(TARGETS "${PROJECT_NAME}-cpu" - EXPORT "${PROJECT_NAME}-cpu" - COMPONENT "${COMPONENT_NAME}-dev" - ARCHIVE) - - # Install for AltAI - install(TARGETS "${PROJECT_NAME}-altai" - EXPORT "${PROJECT_NAME}-altai" + install(TARGETS "${PROJECT_NAME}" + EXPORT "${PROJECT_NAME}" COMPONENT "${COMPONENT_NAME}-dev" ARCHIVE) diff --git a/knp/devices-library/impl/unknown_cpu.cpp b/knp/backends/cpu/cpu-devices/impl/unknown_cpu.cpp similarity index 100% rename from knp/devices-library/impl/unknown_cpu.cpp rename to knp/backends/cpu/cpu-devices/impl/unknown_cpu.cpp diff --git a/knp/devices-library/impl/x86_cpu.cpp b/knp/backends/cpu/cpu-devices/impl/x86_cpu.cpp similarity index 100% rename from knp/devices-library/impl/x86_cpu.cpp rename to knp/backends/cpu/cpu-devices/impl/x86_cpu.cpp diff --git a/knp/devices-library/impl/x86_cpu_power.cpp b/knp/backends/cpu/cpu-devices/impl/x86_cpu_power.cpp similarity index 100% rename from knp/devices-library/impl/x86_cpu_power.cpp rename to knp/backends/cpu/cpu-devices/impl/x86_cpu_power.cpp diff --git a/knp/devices-library/impl/x86_cpu_power.h b/knp/backends/cpu/cpu-devices/impl/x86_cpu_power.h similarity index 100% rename from knp/devices-library/impl/x86_cpu_power.h rename to knp/backends/cpu/cpu-devices/impl/x86_cpu_power.h diff --git a/knp/devices-library/include/knp/devices/cpu.h b/knp/backends/cpu/cpu-devices/include/knp/devices/cpu.h similarity index 99% rename from knp/devices-library/include/knp/devices/cpu.h rename to knp/backends/cpu/cpu-devices/include/knp/devices/cpu.h index 20947f56..c89c46f3 100644 --- a/knp/devices-library/include/knp/devices/cpu.h +++ b/knp/backends/cpu/cpu-devices/include/knp/devices/cpu.h @@ -4,18 +4,18 @@ * @kaspersky_support Artiom N. * @date 30.01.2023 * @license Apache 2.0 - * @copyright © 2024 AO Kaspersky Lab - * - * 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 + * @copyright © 2024 AO Kaspersky Lab + * + * 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. */ diff --git a/knp/devices-library/impl/altai.cpp b/knp/devices-library/impl/altai.cpp deleted file mode 100644 index 4830b60b..00000000 --- a/knp/devices-library/impl/altai.cpp +++ /dev/null @@ -1,162 +0,0 @@ -/** - * @file altai.cpp - * @brief AltAI devices class implementation. - * @author Liubiakin A. - * @date 17.10.2024 - * @license Apache 2.0 - * @copyright © 2024 Motiv NT - * - * 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. - */ - -#include - -#include -#include -#include - -namespace knp::devices::altai -{ - -static bool is_generated = false; -static boost::uuids::uuid altai_gm_uid; - -static constexpr const int compact_board_columns = 16; -static constexpr const int compact_board_rows = 16; -static const std::vector compact_board_ports{ - Port{4, 4, Port::port_side::kNorth}, -}; - -Port make_standart_north_port(size_t begin_core) -{ - return Port{begin_core, 4, Port::port_side::kNorth}; -} - -std::pair AltAI::get_grid_cols_rows() const -{ - return std::pair(columns_, rows_); -} - - -std::vector AltAI::get_grid_ports() const -{ - return ports_; -} - -knp::core::DeviceType AltAI::get_type() const -{ - return knp::core::DeviceType::AltAI1_NPU; -} - - -AltAI_GM::AltAI_GM() -{ - altai_name_ = "AltAIv1_GoldenModel"; - if (!is_generated) - { - boost::uuids::random_generator generator; - altai_gm_uid = generator(); - is_generated = true; - } - Device::base_.uid_ = knp::core::UID(altai_gm_uid); - columns_ = 4; - rows_ = 4; - ports_ = std::vector{make_standart_north_port(0)}; -} - - -AltAI_GM::AltAI_GM(AltAI_GM&& other) : altai_name_{std::move(other.altai_name_)} {} - - -AltAI_GM::~AltAI_GM() {} - - -AltAI_GM& AltAI_GM::operator=(AltAI_GM&& other) noexcept -{ - if (this != &other) - { - altai_name_.swap(other.altai_name_); - } - return *this; -} - - -const std::string& AltAI_GM::get_name() const -{ - return altai_name_; -} - - -float AltAI_GM::get_power() const -{ - return 0.; -} - -void AltAI_GM::load_core_grid_params(size_t rows, size_t columns, const std::vector& ports) -{ - columns_ = columns; - rows_ = rows; - ports_ = ports; -} - -AltAI_HW::AltAI_HW(size_t rows, size_t columns, const std::vector& ports) -{ - altai_name_ = "AltAIv1_Compact_v1.0"; - if (!is_generated) - { - boost::uuids::random_generator generator; - altai_gm_uid = generator(); - is_generated = true; - } - Device::base_.uid_ = - knp::core::UID(boost::uuids::name_generator(knp::core::UID(altai_gm_uid))(altai_name_.c_str())); - columns_ = columns; - rows_ = rows; - ports_ = ports; -} - -AltAI_HW::AltAI_HW(AltAI_HW&& other) : altai_name_(std::move(other.altai_name_)) {} - -AltAI_HW::~AltAI_HW() {} - -AltAI_HW& AltAI_HW::operator=(AltAI_HW&& other) noexcept -{ - if (this != &other) - { - altai_name_.swap(other.altai_name_); - } - return *this; -} - - -const std::string& AltAI_HW::get_name() const -{ - return altai_name_; -} - - -float AltAI_HW::get_power() const -{ - return 0.; -} - -std::vector> list_altai_devices() -{ - std::vector> devices; - devices.push_back(std::unique_ptr(new AltAI_GM())); - devices.push_back( - std::unique_ptr(new AltAI_HW(compact_board_rows, compact_board_columns, compact_board_ports))); - return devices; -} - -} // namespace knp::devices::altai diff --git a/knp/devices-library/include/knp/devices/altai.h b/knp/devices-library/include/knp/devices/altai.h deleted file mode 100644 index f3681a83..00000000 --- a/knp/devices-library/include/knp/devices/altai.h +++ /dev/null @@ -1,288 +0,0 @@ -/** - * @file altai.h - * @brief Class definitions for AltAI, AltAI_GM and AltAI_HW devices. - * @author Liubiakin A. - * @date 17.10.2024 - * @license Apache 2.0 - * @copyright © 2024 Motiv NT - * - * 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. - */ - -#pragma once - -#include - -#include -#include -#include -#include -#include - -/** - * @namespace knp::devices - * @brief Namespace for implementation of concrete devices. - */ - -/** - * @brief AltAI device namespace. - */ -namespace knp::devices::altai -{ - -/** - * @brief Structure used to define location of input and output ports in a grid of - * AltAI cores. - */ -struct Port -{ - /** - * @brief The `port_side` class is an enumeration class that represents sides of port connections to a grid of AltAI - * cores. - */ - enum class port_side - { - /** - * @brief The north side of the grid (y = 0). - */ - kNorth, - /** - * @brief The south side of the grid (y = y_max). - * @note Only the north side for ports is currently supported. - */ - kSouth, - /** - * @brief The east side of the grid (x = 0). - * @note Only the north side for ports is currently supported. - */ - kEast, - /** - * @brief The west side of the grid (x = x_max). - * @note Only the north side for ports is currently supported. - */ - kWest - }; - - /** - * @brief Start core. - */ - // cppcheck-suppress unusedStructMember - size_t start_core; - /** - * @brief Port length from start core. - */ - // cppcheck-suppress unusedStructMember - size_t port_length; - /** - * @brief Side: kNorth, kSouth, kEast, or kWest. - */ - // cppcheck-suppress unusedStructMember - port_side side; -}; - -/** - * @brief Create port of standard size (4) on the north side of the AltAI grid. - * @details The function creates a port with standard length and size, which are used on most of existing AltAI devices. - * @param begin_core position of a core, from which the port starts. - * @return created port. - */ -Port make_standart_north_port(size_t begin_core); - -/** - * @brief Base class for AltAI devices. - */ -class AltAI : public knp::core::Device -{ -public: - /** - * @brief AltAI destructor. - */ - virtual ~AltAI() = default; - - /** - * @brief Get device type. - * @return device type. - */ - [[nodiscard]] knp::core::DeviceType get_type() const override; - - /** - * @brief Get number of columns and rows in device grid. - * @return pair of AltAI grid columns and rows. - */ - [[nodiscard]] std::pair get_grid_cols_rows() const; - - /** - * @brief Get port vector of device grid. - * @return `Port` vector of a given AltAI device. - */ - [[nodiscard]] std::vector get_grid_ports() const; - -protected: - /** - * @brief Number of columns in core grid of AltAI device. - */ - // cppcheck-suppress unusedStructMember - size_t columns_; - /** - * @brief Number of rows in core grid of AltAI device. - */ - // cppcheck-suppress unusedStructMember - size_t rows_; - /** - * @brief Ports in core grid of AltAI device. - */ - // cppcheck-suppress unusedStructMember - std::vector ports_; -}; - - -/** - * @brief The `AltAI_GM` class is a definition of an interface to the AltAI golden model device. - */ -class AltAI_GM : public AltAI -{ -public: - /** - * @brief Avoid copy of a `AltAI_GM` device. - */ - AltAI_GM(const AltAI_GM &) = delete; - - /** - * @brief Avoid copy assignment of a `AltAI_GM` device. - */ - AltAI_GM &operator=(const AltAI_GM &) = delete; - - /** - * @brief `AltAI_GM` device move constructor. - */ - AltAI_GM(AltAI_GM &&); - - /** - * @brief `AltAI_GM` device move operator. - * @return reference to `AltAI_GM` instance. - */ - AltAI_GM &operator=(AltAI_GM &&) noexcept; - - /** - * @brief `AltAI_GM` device destructor. - */ - ~AltAI_GM() override; - -public: - /** - * @brief Get device name. - * @return device name in the arbitrary format. - */ - [[nodiscard]] const std::string &get_name() const override; - - /** - * @brief Get power consumption details for the device. - * @return amount of consumed power. - */ - [[nodiscard]] float get_power() const override; - -public: - /** - * @brief Load parameters to initialization grid of the AltAI core network. - * @param rows number of rows in grid of AltAI cores. - * @param columns number of columns in grid of AltAI cores. - * @param ports port configuration in grid of AltAI cores. - */ - void load_core_grid_params( - size_t rows, size_t columns, const std::vector &ports = {make_standart_north_port(0)}); - -private: - /** - * @brief `AltAI_GM` device constructor. - * @details Create `AltAI_GM` device with 4-by-4 core grid and standard port on the north side. - */ - AltAI_GM(); - friend std::vector> list_altai_devices(); - -private: - // cppcheck-suppress unusedStructMember - std::string altai_name_; -}; - -/** - * @brief The `AltAI_HW` class is a definition of an interface to AltAI hardware device. - */ -class AltAI_HW : public AltAI -{ -public: - /** - * @brief `AltAI_HW` device constructor is deleted. - */ - AltAI_HW() = delete; - - /** - * @brief Avoid copy of a `AltAI_HW` device. - */ - AltAI_HW(const AltAI_HW &) = delete; - - /** - * @brief Avoid copy assignment of a `AltAI_HW` device. - */ - AltAI_HW &operator=(const AltAI_HW &) = delete; - - /** - * @brief `AltAI_HW` device move constructor. - */ - AltAI_HW(AltAI_HW &&); - - /** - * @brief `AltAI_HW` device move operator. - * @return reference to `AltAI_HW` instance. - */ - AltAI_HW &operator=(AltAI_HW &&) noexcept; - - /** - * @brief `AltAI_HW` device destructor. - */ - ~AltAI_HW() override; - -public: - /** - * @brief Get device name. - * @return device name in the arbitrary format. - */ - [[nodiscard]] const std::string &get_name() const override; - - /** - * @brief Get power consumption details for the device. - * @return amount of consumed power. - */ - [[nodiscard]] float get_power() const override; - -private: - /** - * @brief `AltAI_HW` device constructor. - * @param rows number of rows in core grid of the given AltAI hardware. - * @param columns number of columns in core grid of the given AltAI hardware. - * @param ports input and output ports in core grid of the given AltAI hardware. - */ - AltAI_HW(size_t rows, size_t columns, const std::vector &ports); - friend std::vector> list_altai_devices(); - -private: - // cppcheck-suppress unusedStructMember - std::string altai_name_; -}; - -/** - * @brief List all AltAI devices on which backend can be initialized. - * @return vector of unique pointers to base `AltAI` device class, - * which are `AltAI_HW` or `AltAI_GM` devices. - */ -std::vector> list_altai_devices(); -} // namespace knp::devices::altai