-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add transform_unary_fusion * add compose operator for node_t, support printing functor_composition_t on graphviz * add predecessors for adjacency_list * update tests * temporary testing workaround on gcc
- Loading branch information
Showing
6 changed files
with
1,060 additions
and
3 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
58 changes: 58 additions & 0 deletions
58
include/nmtools/array/functional/transform/unary_fusion.hpp
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,58 @@ | ||
#ifndef NMTOOLS_ARRAY_FUNCTIONAL_TRANSFORM_UNARY_FUSION_HPP | ||
#define NMTOOLS_ARRAY_FUNCTIONAL_TRANSFORM_UNARY_FUSION_HPP | ||
|
||
#include "nmtools/meta.hpp" | ||
#include "nmtools/utility/ct_digraph.hpp" | ||
#include "nmtools/array/functional/functor.hpp" | ||
|
||
namespace nmtools::functional | ||
{ | ||
// find a node that has single output and single input, in which the input only used by that node | ||
template <typename adjacency_list_t> | ||
constexpr auto find_unary_fusion(const adjacency_list_t& adjacency_list) | ||
{ | ||
auto sorted = utility::topological_sort(adjacency_list); | ||
auto predecessors = utility::predecessors(adjacency_list); | ||
|
||
auto from = -1; | ||
auto to = -1; | ||
for (nm_size_t i=0; i<(nm_size_t)sorted.size(); i++) { | ||
auto node = sorted[i]; | ||
if (predecessors[node].size() != 1) { | ||
continue; | ||
} | ||
auto predecessor = predecessors[node][0]; | ||
if ((predecessors[predecessor].size() == 0) || (adjacency_list[predecessor].size() > 1)) { | ||
continue; | ||
} | ||
from = predecessor; | ||
to = node; | ||
break; | ||
} | ||
return nmtools_tuple{from,to}; | ||
} // find_unary_fusion | ||
|
||
template <typename graph_t> | ||
constexpr auto transform_unary_fusion(const graph_t& graph) | ||
{ | ||
// TODO: check if graph is fn::compute_graph_t or utility::ct_digraph | ||
constexpr auto adjacency_result = utility::adjacency_list(decltype(graph.digraph){}); | ||
constexpr auto adjacency_list = nmtools::get<0>(adjacency_result); | ||
constexpr auto src_id_map = nmtools::get<1>(adjacency_result); | ||
|
||
constexpr auto unary_fusion = find_unary_fusion(adjacency_list); | ||
constexpr auto from = nmtools::get<0>(unary_fusion); | ||
constexpr auto to = nmtools::get<1>(unary_fusion); | ||
|
||
if constexpr ((from < 0) || (to < 0)) { | ||
return graph; | ||
} else { | ||
auto from_ct = meta::ct_v<src_id_map[from]>; | ||
auto to_ct = meta::ct_v<src_id_map[to]>; | ||
auto fused = graph.nodes(to_ct) * graph.nodes(from_ct); | ||
return utility::contracted_edge(graph,nmtools_tuple{from_ct,to_ct},to_ct,fused); | ||
} | ||
} | ||
} // namespace nmtools::functional | ||
|
||
#endif // NMTOOLS_ARRAY_FUNCTIONAL_TRANSFORM_UNARY_FUSION_HPP |
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.