docc 0.8.3 (0d44999b)
C++ API Reference
Loading...
Searching...
No Matches
matmul_node.h
Go to the documentation of this file.
1
38
39#pragma once
40
41#include <optional>
42
45
48
49namespace sdfg {
50namespace math {
51namespace tensor {
52
54
84class MatMulNode : public TensorNode {
85private:
86 ContractionTypes types_;
87 TensorLayout layout_a_;
88 TensorLayout layout_b_;
89 TensorLayout layout_y_;
90
92 static bool has_basic_strides(symbolic::MultiExpression shape, symbolic::MultiExpression strides);
94 static bool has_transposed_strides(symbolic::MultiExpression shape, symbolic::MultiExpression strides);
95
96public:
105 size_t element_id,
106 const DebugInfo& debug_info,
107 const graph::Vertex vertex,
109 const TensorLayout& layout_a,
110 const TensorLayout& layout_b,
112 const TensorLayout* layout_y = nullptr,
114 );
115
121 size_t element_id,
122 const DebugInfo& debug_info,
123 const graph::Vertex vertex,
125 const TensorLayout& layout_a,
126 const TensorLayout& layout_b,
127 const ContractionTypes& types,
128 const TensorLayout* layout_y = nullptr,
130 );
131
132 static auto constexpr Y_INPUT_IDX = 0;
133 static auto constexpr A_INPUT_IDX = 1;
134 static auto constexpr B_INPUT_IDX = 2;
135
139 static constexpr const char* OPERAND_METADATA_KEY = "ml.matmul.operand";
140
141
147
153
155 const ContractionTypes& types() const;
156
158
161
164
167
172 std::optional<types::PrimitiveType> uniform_quantization(const data_flow::DataFlowGraph& dataflow) const;
173
174 [[deprecated("Use input_type(), accumulator_type(), output_type() or uniform_quantization() instead")]]
176 return quantization(get_parent());
177 }
178
179 [[deprecated("Use input_type(), accumulator_type(), output_type() or uniform_quantization() instead")]]
181
182 [[deprecated("Use input_type() instead")]]
184
185 [[deprecated("Use output_type() instead")]]
187
189 [[deprecated("Use types() instead")]]
191
192 [[deprecated("Use set_types(ContractionTypes::uniform(...)) instead")]]
194
195 const TensorLayout& layout_a() const;
196
197 const TensorLayout& layout_b() const;
198
199 const TensorLayout& layout_y() const;
200
202 return layout_a_;
203 }
204
206 return layout_b_;
207 }
208
210 return layout_y_;
211 }
212
214
220
221 void validate(const Function& function) const override;
222
224
240
241 symbolic::SymbolSet symbols() const override;
242
243 void replace(const symbolic::Expression old_expression, const symbolic::Expression new_expression) override;
244
245 void replace(const symbolic::ExpressionMapping& replacements) override;
246
247 std::unique_ptr<data_flow::DataFlowNode>
248 clone(size_t element_id, const graph::Vertex vertex, data_flow::DataFlowGraph& parent) const override;
249
250 std::string toStr() const override;
251
252 symbolic::Expression flop() const override;
253
255
256 bool relocalize_operand(int input_idx, const TensorLayout& packed) override;
257 bool can_relocalize_operand(int input_idx, const TensorLayout& packed) const override;
258
259 bool supports_integer_types() const override {
260 return true;
261 }
262};
263
269public:
270 nlohmann::json serialize(const data_flow::LibraryNode& library_node) override;
271
274 ) override;
275};
276
286
288private:
289 bool supports_half_; //< Not all BLAS implementations support half precision (hgemm)
290
291public:
292 MatMulBlasExpander(bool supports_half = false)
293 : passes::CodeLibNodeExpander<MatMulNode>(LibraryNodeType_MatMul), supports_half_(supports_half) {
294 }
295
296
299 ) const override;
300};
301
302} // namespace tensor
303} // namespace math
304} // namespace sdfg
Definition element.h:30
size_t element_id() const
const DebugInfo & debug_info() const
Definition function.h:43
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.
const DataFlowGraph & get_parent() const
Get the parent dataflow graph (const).
Base class for all library nodes in the dataflow graph.
Definition library_node.h:106
MatMulBlasExpander(bool supports_half=false)
Definition matmul_node.h:292
passes::LibNodeExpander::ExpandOutcome handle_expand(passes::LibNodeExpander::ExpandContext &context, structured_control_flow::Block &block, MatMulNode &node) const override
MatMulNaiveExpander()
Definition matmul_node.h:279
passes::LibNodeExpander::ExpandOutcome handle_expand(passes::LibNodeExpander::ExpandContext &context, structured_control_flow::Block &block, MatMulNode &node) const override
Serializer for MatMulNode.
Definition matmul_node.h:268
nlohmann::json serialize(const data_flow::LibraryNode &library_node) override
Serialize a library node to JSON.
data_flow::LibraryNode & deserialize(const nlohmann::json &j, builder::StructuredSDFGBuilder &builder, structured_control_flow::Block &parent) override
Deserialize a library node from JSON.
Tensor matrix multiplication following ONNX MatMul operator specification.
Definition matmul_node.h:84
const TensorLayout & layout_a() const
TensorLayout & layout_b()
Definition matmul_node.h:205
void replace(const symbolic::ExpressionMapping &replacements) override
types::PrimitiveType output_quantization() const
static auto constexpr A_INPUT_IDX
Definition matmul_node.h:133
std::unique_ptr< data_flow::DataFlowNode > clone(size_t element_id, const graph::Vertex vertex, data_flow::DataFlowGraph &parent) const override
Clone this node for graph transformations.
void set_types(const ContractionTypes &types)
bool can_relocalize_operand(int input_idx, const TensorLayout &packed) const override
Side-effect-free predicate mirroring relocalize_operand: whether the operand at input_idx can be repo...
std::string toStr() const override
Convert node to string representation.
TensorLayout & layout_y()
Definition matmul_node.h:209
symbolic::SymbolSet symbols() const override
Get all symbols used in this node.
void verify_data_types(const data_flow::DataFlowGraph &graph) const override
Called as part of validate.
TensorLayout & layout_a()
Definition matmul_node.h:201
const ContractionTypes & types() const
The configured element types; Void entries are inferred, see input_type(), accumulator_type(),...
bool supports_integer_types() const override
Check if this operation supports integer types.
Definition matmul_node.h:259
void set_fixed_quantization(const QuantizationType quant)
QuantizationType quantization(const data_flow::DataFlowGraph &dataflow) const
const TensorLayout & layout_b() const
symbolic::Expression k() const
Get the K dimension (columns of A, rows of B - contraction dimension).
types::PrimitiveType accumulator_type() const
Element type the products are summed in: configured, or the output type.
types::PrimitiveType input_type() const
Element type of A and B: configured, or inferred from their memlets (Void if they disagree).
symbolic::Expression flop() const override
Calculate floating point operations for this node.
const TensorLayout & layout_y() const
MatMulNode(size_t element_id, const DebugInfo &debug_info, const graph::Vertex vertex, data_flow::DataFlowGraph &parent, const TensorLayout &layout_a, const TensorLayout &layout_b, const ContractionTypes &types, const TensorLayout *layout_y=nullptr, const data_flow::ImplementationType &impl_type=data_flow::ImplementationType_NONE)
Construct a matmul node with explicit element types.
static TensorLayout get_linear_result_layout(const TensorLayout &layout_a, const TensorLayout &layout_b)
data_flow::PointerAccessType pointer_access_type(int input_idx) const override
Describes what a pointer is used for.
types::PrimitiveType input_quantization() const
MatMulNode(size_t element_id, const DebugInfo &debug_info, const graph::Vertex vertex, data_flow::DataFlowGraph &parent, const TensorLayout &layout_a, const TensorLayout &layout_b, QuantizationType quantization=QUANTIZATION_MATCH_INPUTS, const TensorLayout *layout_y=nullptr, const data_flow::ImplementationType &impl_type=data_flow::ImplementationType_NONE)
Construct a matmul node.
static auto constexpr B_INPUT_IDX
Definition matmul_node.h:134
symbolic::Expression m() const
Get the M dimension (rows of A, rows of output).
void replace(const symbolic::Expression old_expression, const symbolic::Expression new_expression) override
static auto constexpr Y_INPUT_IDX
Definition matmul_node.h:132
types::PrimitiveType output_type() const
Element type of Y: configured, or inferred from its memlet.
QuantizationType fixed_quantization() const
The configured type if inputs and output share one (Void otherwise).
static constexpr const char * OPERAND_METADATA_KEY
Metadata key tagging a memlet with the matmul operand it carries ("A", "B", or "Y"),...
Definition matmul_node.h:139
bool relocalize_operand(int input_idx, const TensorLayout &packed) override
Repoint the operand at input_idx to a locally staged buffer.
QuantizationType quantization() const
Definition matmul_node.h:175
void validate(const Function &function) const override
Validate the code node's configuration.
virtual passes::LibNodeExpander::ExpandOutcome expand(passes::LibNodeExpander::ExpandContext &, structured_control_flow::Block &block) override
Expand matmul into nested maps.
symbolic::Expression n() const
Get the N dimension (columns of B, columns of output).
std::optional< types::PrimitiveType > uniform_quantization(const data_flow::DataFlowGraph &dataflow) const
The single type of inputs, accumulator and output, if they all agree.
The metadata associated with a addressing a tensor in elements Meant to be used with TensorNodes to d...
Definition tensor_layout.h:20
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:191
CodeLibNodeExpander(const data_flow::LibraryNodeCode &code)
Definition lib_node_expander.h:195
Definition lib_node_expander.h:122
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
std::vector< Expression > MultiExpression
Vector of symbolic expressions for multi-dimensional operations.
Definition symbolic.h:89
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
StringEnum ImplementationType
Implementation type identifier for library nodes.
Definition library_node.h:75
StringEnum LibraryNodeCode
Definition library_node.h:65
ImplementationType ImplementationType_NONE
Default implementation type indicating no specific implementation.
Definition library_node.h:84
std::unique_ptr< PointerAccessMeta, PtrAccessDeleter > PointerAccessType
Definition pointer_metadata.h:32
Definition graph.h:22
boost::graph_traits< Graph >::vertex_descriptor Vertex
Definition graph.h:25
Definition arange_node.h:8
constexpr QuantizationType QUANTIZATION_MATCH_INPUTS
Definition tensor_node.h:27
types::PrimitiveType QuantizationType
Definition tensor_node.h:26
data_flow::LibraryNodeCode LibraryNodeType_MatMul("ml::MatMul")
Definition batched_gemm_node.h:12
Definition approximate_math.h:12
PrimitiveType
Enumeration of all primitive data types supported by sdfglib.
Definition type.h:35
Definition analysis.h:7
Element types of a contraction such as a matmul.
Definition tensor_node.h:36
Definition lib_node_expander.h:12
Symbolic system for integer-based symbol analysis.
Abstract base class for tensor library nodes.