docc 0.8.3 (0d44999b)
C++ API Reference
Loading...
Searching...
No Matches
reduce_node.h
Go to the documentation of this file.
1
49
50#pragma once
51
52#include <optional>
54
57
58namespace sdfg {
59namespace math {
60namespace tensor {
61
85class ReduceNode : public TensorNode {
86protected:
87 std::vector<symbolic::Expression> shape_;
88 std::vector<int64_t> axes_;
89 bool keepdims_;
90
91public:
104 size_t element_id,
105 const DebugInfo& debug_info,
106 const graph::Vertex vertex,
109 const std::vector<symbolic::Expression>& shape,
110 const std::vector<int64_t>& axes,
111 bool keepdims
112 );
113
114 static auto constexpr RESULT_PTR_IDX = 0;
115 static auto constexpr X_INPUT_IDX = 1;
116
117 void validate(const Function& function) const override;
118
123 const std::vector<symbolic::Expression>& shape() const {
124 return shape_;
125 }
126
131 const std::vector<int64_t>& axes() const {
132 return axes_;
133 }
134
139 bool keepdims() const {
140 return keepdims_;
141 }
142
143 symbolic::SymbolSet symbols() const override;
144
145 void replace(const symbolic::Expression old_expression, const symbolic::Expression new_expression) override;
146
147 void replace(const symbolic::ExpressionMapping& replacements) override;
148
160
177 virtual bool expand_reduction(
181 const types::Tensor& input_type,
182 const types::Tensor& output_type,
183 const data_flow::Subset& input_subset,
184 const data_flow::Subset& output_subset
185 ) = 0;
186
200 virtual std::string identity(types::PrimitiveType primitive_type) const = 0;
201
210 virtual std::optional<structured_control_flow::ReductionOperation> reduction_operation() const {
211 return std::nullopt;
212 }
213
215
216 std::string toStr() const override;
217
218protected:
222 const data_flow::Memlet* iedge_input,
223 const data_flow::Memlet* iedge_result,
224 const std::vector<symbolic::Expression>& output_shape,
225 const std::vector<int64_t>& sorted_axes
226 );
227};
228
229template<typename T>
231public:
232 nlohmann::json serialize(const data_flow::LibraryNode& library_node) override {
233 const ReduceNode& reduce_node = static_cast<const ReduceNode&>(library_node);
234 nlohmann::json j;
235
236 j["code"] = reduce_node.code().value();
237
239 j["shape"] = nlohmann::json::array();
240 for (auto& dim : reduce_node.shape()) {
241 j["shape"].push_back(serializer.expression(dim));
242 }
243
244 j["axes"] = reduce_node.axes();
245 j["keepdims"] = reduce_node.keepdims();
246
247 return j;
248 }
249
252 ) override {
253 // Assertions for required fields
254 assert(j.contains("element_id"));
255 assert(j.contains("code"));
256 assert(j.contains("debug_info"));
257 assert(j.contains("shape"));
258 assert(j.contains("axes"));
259 assert(j.contains("keepdims"));
260
261 auto code = j["code"].get<std::string>();
262
263 std::vector<symbolic::Expression> shape;
264 for (const auto& dim : j["shape"]) {
265 shape.push_back(symbolic::parse(dim.get<std::string>()));
266 }
267
268 std::vector<int64_t> axes = j["axes"].get<std::vector<int64_t>>();
269 bool keepdims = j["keepdims"].get<bool>();
270
271 // Extract debug info using JSONSerializer
273 DebugInfo debug_info = serializer.json_to_debug_info(j["debug_info"]);
274
275 return static_cast<ReduceNode&>(builder.add_library_node<T>(parent, debug_info, shape, axes, keepdims));
276 }
277};
278
279} // namespace tensor
280} // namespace math
281} // namespace sdfg
Dispatchers for dataflow blocks and library nodes.
Definition element.h:30
size_t element_id() const
const DebugInfo & debug_info() const
Definition function.h:43
std::string value() const
Definition exceptions.h:47
Note: Even though the class references unique_ptr, it will never delete an SDFG it has a reference to...
Definition structured_sdfg_builder.h:44
Definition data_flow_graph.h:29
graph::Vertex vertex() const
Get the graph vertex for this node.
Base class for all library nodes in the dataflow graph.
Definition library_node.h:106
const LibraryNodeCode & code() const
Get the operation code.
Edge in the dataflow graph representing data movement.
Definition memlet.h:131
Definition reduce_node.h:230
data_flow::LibraryNode & deserialize(const nlohmann::json &j, builder::StructuredSDFGBuilder &builder, structured_control_flow::Block &parent) override
Deserialize a library node from JSON.
Definition reduce_node.h:250
nlohmann::json serialize(const data_flow::LibraryNode &library_node) override
Serialize a library node to JSON.
Definition reduce_node.h:232
Base class for tensor reduction operations.
Definition reduce_node.h:85
virtual passes::LibNodeExpander::ExpandOutcome expand_inner(passes::LibNodeExpander::AccessNodeExpand &expansion, structured_control_flow::Block &block, const data_flow::Memlet *iedge_input, const data_flow::Memlet *iedge_result, const std::vector< symbolic::Expression > &output_shape, const std::vector< int64_t > &sorted_axes)
symbolic::SymbolSet symbols() const override
Get all symbols used in this node.
std::vector< int64_t > axes_
Axes to reduce over.
Definition reduce_node.h:88
std::string toStr() const override
Convert node to string representation.
void replace(const symbolic::Expression old_expression, const symbolic::Expression new_expression) override
bool keepdims_
Whether to keep reduced dimensions with size 1.
Definition reduce_node.h:89
static auto constexpr X_INPUT_IDX
Definition reduce_node.h:115
std::vector< symbolic::Expression > shape_
Input tensor shape.
Definition reduce_node.h:87
void validate(const Function &function) const override
Validate the code node's configuration.
ReduceNode(size_t element_id, const DebugInfo &debug_info, const graph::Vertex vertex, data_flow::DataFlowGraph &parent, const data_flow::LibraryNodeCode &code, const std::vector< symbolic::Expression > &shape, const std::vector< int64_t > &axes, bool keepdims)
Construct a reduction node.
passes::LibNodeExpander::ExpandOutcome expand(passes::LibNodeExpander::ExpandContext &context, Block &block) override
Expand into nested maps with reduction logic.
virtual std::optional< structured_control_flow::ReductionOperation > reduction_operation() const
Get the ReductionOperation for this reduction.
Definition reduce_node.h:210
static auto constexpr RESULT_PTR_IDX
Definition reduce_node.h:114
void replace(const symbolic::ExpressionMapping &replacements) override
data_flow::PointerAccessType pointer_access_type(int input_idx) const override
Describes what a pointer is used for.
virtual std::string identity(types::PrimitiveType primitive_type) const =0
Get the identity value for this reduction.
const std::vector< symbolic::Expression > & shape() const
Get the input tensor shape.
Definition reduce_node.h:123
const std::vector< int64_t > & axes() const
Get the reduction axes.
Definition reduce_node.h:131
virtual bool expand_reduction(passes::LibNodeExpander::AccessNodeExpand &expansion, builder::StructuredSDFGBuilder &builder, structured_control_flow::Sequence &body, const types::Tensor &input_type, const types::Tensor &output_type, const data_flow::Subset &input_subset, const data_flow::Subset &output_subset)=0
Generate the actual reduction code.
bool keepdims() const
Check if reduced dimensions are kept.
Definition reduce_node.h:139
types::PrimitiveType primitive_type(const data_flow::DataFlowGraph &graph) const
Get the primitive type of this tensor operation.
TensorNode(size_t element_id, const DebugInfo &debug_info, const graph::Vertex vertex, data_flow::DataFlowGraph &parent, const data_flow::LibraryNodeCode &code, const std::vector< std::string > &outputs, const std::vector< std::string > &inputs, const data_flow::ImplementationType &impl_type)
Construct a tensor node.
Definition lib_node_expander.h:45
Definition lib_node_expander.h:122
Definition json_serializer.h:22
Base class for library node serialization.
Definition json_serializer.h:195
A control flow node representing a basic block with dataflow computations.
Definition block.h:42
A sequential container of control flow nodes.
Definition sequence.h:52
Definition tensor.h:13
Expression parse(const std::string &expr_str)
Parses a string into a symbolic expression.
SymEngine::RCP< const SymEngine::Basic > Expression
Reference-counted pointer to a symbolic expression (SymEngine Basic).
Definition symbolic.h:83
std::set< Symbol, SymEngine::RCPBasicKeyLess > SymbolSet
Ordered set of symbols.
Definition symbolic.h:95
SymEngine::map_basic_basic ExpressionMapping
Definition symbolic.h:109
Definition function_builder.h:10
symbolic::MultiExpression Subset
Type alias for a subset specification (array of symbolic expressions).
Definition memlet.h:103
StringEnum LibraryNodeCode
Definition library_node.h:65
std::unique_ptr< PointerAccessMeta, PtrAccessDeleter > PointerAccessType
Definition pointer_metadata.h:32
boost::graph_traits< Graph >::vertex_descriptor Vertex
Definition graph.h:25
Definition arange_node.h:8
Definition batched_gemm_node.h:12
Definition cutout_serializer.h:13
PrimitiveType
Enumeration of all primitive data types supported by sdfglib.
Definition type.h:35
Definition analysis.h:7
Definition lib_node_expander.h:12
Abstract base class for tensor library nodes.