docc 0.8.3 (0d44999b)
C++ API Reference
Loading...
Searching...
No Matches
einsum_detection.h
Go to the documentation of this file.
1#pragma once
2
3#include <cstdint>
4#include <list>
5#include <memory>
6#include <string>
7#include <unordered_map>
8#include <unordered_set>
9#include <vector>
10
21
22namespace sdfg::einsum {
23
41
52 std::vector<EinsumIndexContribution> contributions;
54 size_t covered = 0;
55
64 std::vector<symbolic::Expression> get_strides(
65 const std::vector<EinsumIndexContribution>& ordered_contributions,
66 const std::vector<symbolic::Expression>& shape
67 ) const;
68};
69
76 bool contributes = false;
78 size_t index = 0;
80 int64_t factor = 1;
81};
82
91 bool ok = false;
93 std::vector<OperandContribution> inputs;
94};
95
97public:
98 virtual ~EinsumEdge() = default;
99
100 virtual const data_flow::Subset& subset() const = 0;
101 virtual const types::IType& base_type() const = 0;
102 virtual data_flow::Memlet* original_edge() const = 0;
103};
104
106 data_flow::Memlet* edge;
107
108public:
110 }
111
112 const data_flow::Subset& subset() const override {
113 return edge->subset();
114 }
115 const types::IType& base_type() const override {
116 return edge->base_type();
117 }
119 return edge;
120 }
121};
122
124 data_flow::Subset subset_;
125 const data_flow::Memlet* edge;
126
127public:
128 EinsumEdgeVirtual(const data_flow::Memlet* edge, const data_flow::Subset& subset) : subset_(subset), edge(edge) {
129 }
130
131 const data_flow::Subset& subset() const override {
132 return subset_;
133 }
134 const types::IType& base_type() const override {
135 return edge->base_type();
136 }
138 return nullptr;
139 }
140};
141
151public:
154
155 // Core einsum properties (mirroring EinsumNode, but excluding the implicit
156 // "__einsum_out" reduction input, which is represented by output_node/out_indices).
157 std::vector<EinsumDimension> dims;
158 std::vector<std::unique_ptr<EinsumEdge>> in_edges;
159 std::vector<std::string> inputs;
160
162 std::vector<EinsumIndexing> einsum_in_indices;
165
167 std::unique_ptr<EinsumEdge> output_edge = nullptr;
170 std::unordered_map<std::string, data_flow::DataFlowNode*> input_nodes;
172 bool subtraction = false;
173
175 std::list<data_flow::DataFlowNode*> consumed_nodes;
177 std::list<structured_control_flow::StructuredLoop*> consumed_loops;
178
179 std::pair<data_flow::AccessNode*, const data_flow::Subset&> get_input_for(size_t input_idx);
180
181 const std::vector<EinsumIndexing>& get_input_indexings() const;
182
183 std::string toStr() const;
184
185 const data_flow::Subset& get_input_subset(size_t input_idx) const;
186
188
190
192};
193
205protected:
206 struct BlockState {
207 std::unordered_set<data_flow::DataFlowNode*> consumed_nodes;
208 std::list<EinsumCluster*> clusters;
209 };
216
217
219 std::vector<std::unique_ptr<EinsumCluster>> all_einsums_;
221 std::vector<LoopScopeState> loop_stack_;
222
223 static bool subsets_eq(const data_flow::Subset& subset1, const data_flow::Subset& subset2);
224
226
229
232
234 static bool try_extend(BlockState& state, EinsumCluster& cluster);
235
237 static bool subset_contains_symbol(const data_flow::Subset& subset, const symbolic::Symbol& symbol);
238
242
245
248
249public:
250 EinsumDetection() = default;
251
254
258
266
268
271
273 const std::vector<std::unique_ptr<EinsumCluster>>& einsums() const {
274 return all_einsums_;
275 }
276
278
285};
286
287} // namespace sdfg::einsum
Access nodes for reading/writing data in the dataflow graph.
Node representing access to a data container in the dataflow graph.
Definition access_node.h:84
Edge in the dataflow graph representing data movement.
Definition memlet.h:131
Simple computational operation in the dataflow graph.
Definition tasklet.h:348
Non-destructive counterpart of an EinsumNode.
Definition einsum_detection.h:150
std::pair< data_flow::AccessNode *, const data_flow::Subset & > get_input_for(size_t input_idx)
std::vector< EinsumDimension > dims
Definition einsum_detection.h:157
std::vector< std::unique_ptr< EinsumEdge > > in_edges
Definition einsum_detection.h:158
std::list< structured_control_flow::StructuredLoop * > consumed_loops
Surrounding loops folded into this cluster as reduction/free dimensions. From outermost to innermost.
Definition einsum_detection.h:177
bool subtraction
True if the core reduction was a subtraction (carries an implicit -1 factor).
Definition einsum_detection.h:172
const std::vector< EinsumIndexing > & get_input_indexings() const
std::string toStr() const
std::unordered_map< std::string, data_flow::DataFlowNode * > input_nodes
Source node for each input connector (may be null for synthetic constants).
Definition einsum_detection.h:170
symbolic::Expression get_linearized_outer_offset(int input_idx) const
data_flow::AccessNode * output_node
Definition einsum_detection.h:168
const data_flow::Subset & out_indices() const
std::vector< std::string > inputs
Definition einsum_detection.h:159
std::list< data_flow::DataFlowNode * > consumed_nodes
Dataflow nodes folded into this cluster (reduction tasklet, mul tasklets, ...).
Definition einsum_detection.h:175
const EinsumIndexing & get_output_indexing() const
structured_control_flow::Block * block
Block that holds the reduction core of this cluster.
Definition einsum_detection.h:153
EinsumIndexing einsum_out_indices
Inner-index mapping of the output operand.
Definition einsum_detection.h:164
const data_flow::Subset & get_input_subset(size_t input_idx) const
std::unique_ptr< EinsumEdge > output_edge
Reduction/output access node of the cluster.
Definition einsum_detection.h:167
std::vector< EinsumIndexing > einsum_in_indices
Inner-index mapping per input operand (parallel to in_indices/inputs).
Definition einsum_detection.h:162
void apply_promote(EinsumCluster &cluster, structured_control_flow::StructuredLoop &loop, const PromotionCheck &check)
bool try_to_consume_loop(EinsumCluster &einsum, structured_control_flow::StructuredLoop &loop)
If there is a cluster representing the full contents of the loop, try to have it consume this loop.
bool visit(structured_control_flow::AssignmentBlock &node) override
EinsumCluster * from_einsum_node(structured_control_flow::Block &block, EinsumNode &einsum_node)
Mirror an already existing EinsumNode into a cluster.
void consume_input_operations(BlockState &state)
for all the clusters of the block last scanned into BlockState, try to consume all the input operatio...
static bool subsets_eq(const data_flow::Subset &subset1, const data_flow::Subset &subset2)
size_t run(structured_control_flow::ControlFlowNode &start)
Run detection starting from the given control flow node.
void register_cluster_in_scope(EinsumCluster *cluster)
bool visit(structured_control_flow::Block &node) override
static bool try_extend(BlockState &state, EinsumCluster &cluster)
Try to fold a multiplication feeding an input into the cluster. Returns true if applied.
bool visit(structured_control_flow::While &node) override
bool block_fully_consumed(BlockState &state, structured_control_flow::Block &block)
std::vector< std::unique_ptr< EinsumCluster > > all_einsums_
all that have been found
Definition einsum_detection.h:219
static bool subset_contains_symbol(const data_flow::Subset &subset, const symbolic::Symbol &symbol)
std::vector< LoopScopeState > loop_stack_
those that are relevant to the current scope.
Definition einsum_detection.h:221
EinsumCluster * try_lift(structured_control_flow::Block &block, data_flow::Tasklet &tasklet)
Try to lift a reduction tasklet into a fresh einsum cluster core.
const std::vector< std::unique_ptr< EinsumCluster > > & einsums() const
All einsum clusters found during the last run.
Definition einsum_detection.h:273
static symbolic::Expression cnf_to_upper_bound(const symbolic::CNF &cnf, const symbolic::Symbol indvar)
PromotionCheck can_promote(EinsumCluster &cluster, structured_control_flow::StructuredLoop &loop)
Verify the loop can be folded and return the new dimension's per-operand contributions.
EinsumCluster & new_cluster(structured_control_flow::Block &block)
bool visit(structured_control_flow::IfElse &node) override
void recompute_einsum_indices(EinsumCluster &cluster)
Recompute the per-operand inner-index mappings from the current dims and subsets.
void find_einsum_core_ops(BlockState &state, structured_control_flow::Block &block)
Loop for operations (reductions) that can form the core of einsum clusters.
bool visit(structured_control_flow::Return &node) override
bool handleStructuredLoop(structured_control_flow::StructuredLoop &loop) override
const data_flow::Subset & subset() const override
Definition einsum_detection.h:112
data_flow::Memlet * original_edge() const override
Definition einsum_detection.h:118
EinsumEdgeOriginal(data_flow::Memlet *edge)
Definition einsum_detection.h:109
const types::IType & base_type() const override
Definition einsum_detection.h:115
EinsumEdgeVirtual(const data_flow::Memlet *edge, const data_flow::Subset &subset)
Definition einsum_detection.h:128
const data_flow::Subset & subset() const override
Definition einsum_detection.h:131
const types::IType & base_type() const override
Definition einsum_detection.h:134
data_flow::Memlet * original_edge() const override
Definition einsum_detection.h:137
Definition einsum_detection.h:96
virtual const types::IType & base_type() const =0
virtual data_flow::Memlet * original_edge() const =0
virtual ~EinsumEdge()=default
virtual const data_flow::Subset & subset() const =0
Definition einsum_node.h:34
A control flow node representing a basic block with dataflow computations.
Definition block.h:42
Base class for all structured control flow nodes in a StructuredSDFG.
Definition control_flow_node.h:44
Represents a conditional if-else control flow node with multiple cases.
Definition if_else.h:47
Represents a return statement that exits the function with a value.
Definition return.h:32
Base class for structured loop constructs.
Definition structured_loop.h:244
Represents a while-loop with condition-controlled iteration.
Definition while.h:37
Abstract base interface for all types in the sdfglib type system.
Definition type.h:625
Visitor for Structured SDFG (not a builder).
Definition structured_sdfg_visitor.h:71
bool visit(sdfg::structured_control_flow::ControlFlowNode &node)
Conversion of conditions to conjunctive normal form (CNF).
Base class for all nodes in the dataflow graph.
SymEngine::RCP< const SymEngine::Basic > Expression
Reference-counted pointer to a symbolic expression (SymEngine Basic).
Definition symbolic.h:83
SymEngine::RCP< const SymEngine::Symbol > Symbol
Reference-counted pointer to a SymEngine Symbol.
Definition symbolic.h:71
Edges in the dataflow graph representing data movement.
symbolic::MultiExpression Subset
Type alias for a subset specification (array of symbolic expressions).
Definition memlet.h:103
Definition einsum.h:13
std::vector< std::vector< Condition > > CNF
Conjunctive Normal Form representation.
Definition conjunctive_normal_form.h:73
Definition einsum_detection.h:206
std::list< EinsumCluster * > clusters
Definition einsum_detection.h:208
std::unordered_set< data_flow::DataFlowNode * > consumed_nodes
Definition einsum_detection.h:207
Definition einsum_detection.h:210
EinsumCluster * representing_last_block_
Definition einsum_detection.h:214
bool non_reducible
found more than a single block in this scope, or sth.
Definition einsum_detection.h:213
A single einsum dimension's contribution to one operand's access subset.
Definition einsum_detection.h:31
int64_t factor
Integer coefficient of the indvar within that index expression.
Definition einsum_detection.h:37
symbolic::Symbol indvar
Induction variable of the contributing dimension.
Definition einsum_detection.h:33
symbolic::Expression factor_expr() const
size_t index
Index position within the original subset that this dimension contributes to.
Definition einsum_detection.h:35
How the consumed einsum dimensions map onto one operand's access subset.
Definition einsum_detection.h:50
std::vector< EinsumIndexContribution > contributions
Contributions of individual dimensions to this operand's index positions.
Definition einsum_detection.h:52
std::vector< symbolic::Expression > get_strides(const std::vector< EinsumIndexContribution > &ordered_contributions, const std::vector< symbolic::Expression > &shape) const
Compute all strides for shape, given this indexing's contributions reordered so that ordered_contribu...
size_t covered
Number of innermost original index positions fully covered by the dimensions.
Definition einsum_detection.h:54
A single consumed dimension's contribution to one operand, found during promotion.
Definition einsum_detection.h:75
int64_t factor
Integer coefficient of the indvar in that index expression.
Definition einsum_detection.h:80
size_t index
Index position within the operand's subset it contributes to.
Definition einsum_detection.h:78
bool contributes
Definition einsum_detection.h:76
Result of checking whether a loop can be folded into a cluster.
Definition einsum_detection.h:90
OperandContribution output
Definition einsum_detection.h:92
std::vector< OperandContribution > inputs
Definition einsum_detection.h:93
bool ok
Definition einsum_detection.h:91
Symbolic system for integer-based symbol analysis.
Simple computational operations in the dataflow graph.