From fb245d4aad2575b29ff26ce74cf9e2f592e11f01 Mon Sep 17 00:00:00 2001 From: Olivier BICHLER <olivier.bichler@cea.fr> Date: Mon, 3 Jun 2024 14:29:07 +0200 Subject: [PATCH] Initial commit --- include/aidge/recipes/Recipes.hpp | 2 + src/recipes/FuseToMetaOps.cpp | 46 +++++++++++++++++++++++ unit_tests/recipes/Test_FuseToMetaOps.cpp | 26 +++++++++++++ 3 files changed, 74 insertions(+) create mode 100644 src/recipes/FuseToMetaOps.cpp create mode 100644 unit_tests/recipes/Test_FuseToMetaOps.cpp diff --git a/include/aidge/recipes/Recipes.hpp b/include/aidge/recipes/Recipes.hpp index 97c608cd3..e389d3872 100644 --- a/include/aidge/recipes/Recipes.hpp +++ b/include/aidge/recipes/Recipes.hpp @@ -123,6 +123,8 @@ void explicitCastMove(std::shared_ptr<GraphView> graphView); */ void expandMetaOps(std::shared_ptr<GraphView> graph, bool recursive = false); +void fuseToMetaOps(std::shared_ptr<GraphView> graph, const std::string& query, const std::string& name = ""); + } // namespace Aidge #endif /* AIDGE_CORE_UTILS_RECIPES_H_ */ diff --git a/src/recipes/FuseToMetaOps.cpp b/src/recipes/FuseToMetaOps.cpp new file mode 100644 index 000000000..be6f3c1df --- /dev/null +++ b/src/recipes/FuseToMetaOps.cpp @@ -0,0 +1,46 @@ +/******************************************************************************** + * Copyright (c) 2023 CEA-List + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License 2.0 which is available at + * http://www.eclipse.org/legal/epl-2.0. + * + * SPDX-License-Identifier: EPL-2.0 + * + ********************************************************************************/ + +#include <memory> + +#include "aidge/graph/Node.hpp" +#include "aidge/graph/GraphView.hpp" +#include "aidge/operator/MetaOperator.hpp" +#include "aidge/recipes/Recipes.hpp" + +//Graph Regex +#include "aidge/graphRegex/GraphRegex.hpp" + +void Aidge::fuseToMetaOps(std::shared_ptr<GraphView> graphView, const std::string& query, const std::string& name) { + std::shared_ptr<GraphRegex> regex = std::make_shared<GraphRegex>(); + regex->setKeyFromGraph(graphView); + regex->addQuery(query); + + size_t nbReplaced = 0; + const auto matches = regex->match(graphView); + + for (const auto& solution : matches) { + auto microGraph = std::make_shared<GraphView>(); + microGraph->add(solution->getAll()); + + auto metaOp = MetaOperator(query.c_str(), microGraph, name); + const auto success = GraphView::replace(solution->getAll(), {metaOp}); + + if (!success) { + Log::notice("Could not replace sub-graph with meta operator"); + } + else { + ++nbReplaced; + } + } + + Log::info("Replaced {} (out of {}) matching sub-graph with meta operators", nbReplaced, matches.size()); +} diff --git a/unit_tests/recipes/Test_FuseToMetaOps.cpp b/unit_tests/recipes/Test_FuseToMetaOps.cpp new file mode 100644 index 000000000..34ca595df --- /dev/null +++ b/unit_tests/recipes/Test_FuseToMetaOps.cpp @@ -0,0 +1,26 @@ +/******************************************************************************** + * Copyright (c) 2023 CEA-List + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License 2.0 which is available at + * http://www.eclipse.org/legal/epl-2.0. + * + * SPDX-License-Identifier: EPL-2.0 + * + ********************************************************************************/ + +#include <catch2/catch_test_macros.hpp> +#include <set> + +#include "aidge/data/Tensor.hpp" +#include "aidge/graph/GraphView.hpp" +#include "aidge/recipes/Recipes.hpp" + +namespace Aidge { + + +TEST_CASE("[cpu/recipes] FuseToMetaOps", "[FuseToMetaOps][recipes]") { + +} + +} // namespace Aidge -- GitLab